bool SharePlugin::receivePacket(const NetworkPacket &np)
{
/*
//TODO: Write a test like this
if (np.type() == PACKET_TYPE_PING) {
qCDebug(KDECONNECT_PLUGIN_SHARE) << "sending file" << (QDesktopServices::storageLocation(QDesktopServices::HomeLocation) + "/.bashrc");
NetworkPacket out(PACKET_TYPE_SHARE_REQUEST);
out.set("filename", mDestinationDir + "itworks.txt");
AutoClosingQFile* file = new AutoClosingQFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation) + "/.bashrc"); //Test file to
transfer
out.setPayload(file, file->size());
device()->sendPacket(out);
return true;
}
*/
qCDebug(KDECONNECT_PLUGIN_SHARE) << "File transfer";
if (np.hasPayload() || np.has(QStringLiteral("filename"))) {
// qCDebug(KDECONNECT_PLUGIN_SHARE) << "receiving file" << filename << "in" << dir << "into" << destination;
const QString filename = cleanFilename(np.get(QStringLiteral("filename"), QString::number(QDateTime::currentMSecsSinceEpoch())));
QUrl destination = getFileDestination(filename);
if (np.hasPayload()) {
qint64 dateCreated = np.get(QStringLiteral("creationTime"), QDateTime::currentMSecsSinceEpoch());
qint64 dateModified = np.get(QStringLiteral("lastModified"), QDateTime::currentMSecsSinceEpoch());
const bool open = np.get(QStringLiteral("open"), false);
if (!m_compositeJob) {
m_compositeJob = new CompositeFileTransferJob(device()->id());
m_compositeJob->setProperty("destUrl", destinationDir().toString());
m_compositeJob->setProperty("immediateProgressReporting", true);
Daemon::instance()->jobTracker()->registerJob(m_compositeJob);
}
FileTransferJob *job = np.createPayloadTransferJob(destination);
job->setOriginName(device()->name() + QStringLiteral(": ") + filename);
connect(job, &KJob::result, this, [this, dateCreated, dateModified, open](KJob *job) -> void {
finished(job, dateCreated, dateModified, open);
});
m_compositeJob->addSubjob(job);
if (!m_compositeJob->isRunning()) {
m_compositeJob->start();
}
} else {
QFile file(destination.toLocalFile());
file.open(QIODevice::WriteOnly);
file.close();
}
} else if (np.has(QStringLiteral("text"))) {
QString text = np.get(QStringLiteral("text"));
auto mimeData = new QMimeData;
mimeData->setText(text);
KSystemClipboard::instance()->setMimeData(mimeData, QClipboard::Clipboard);
QUrl url;
QStringList lines = text.split(QStringLiteral("\n"), Qt::SkipEmptyParts);
if (lines.count()) {
url.setUrl(lines[lines.count() - 1].trimmed());
}
KNotification *notif = new KNotification(QStringLiteral("textShareReceived"));
notif->setComponentName(QStringLiteral("kdeconnect"));
notif->setText(text);
notif->setTitle(i18nc("@info Some piece of text was received from a connected device", "Shared text from %1 copied to clipboard", device()->name()));
QStringList actions;
actions << i18nc("@action:button Edit text with default text editor", "Open in Text Editor");
if (url.isValid() && (url.scheme() == QStringLiteral("http") || url.scheme() == QStringLiteral("https"))) {
qDebug() << url;
actions << i18nc("@action:button Open URL with default app", "Open Link");
}
notif->setActions(actions);
connect(notif, &KNotification::action1Activated, this, [this, text]() {
KService::Ptr service = KApplicationTrader::preferredService(QStringLiteral("text/plain"));
const QString defaultApp = service ? service->desktopEntryName() : QString();
if (defaultApp == QLatin1String("org.kde.kate") || defaultApp == QLatin1String("org.kde.kwrite")) {
QProcess *proc = new QProcess();
connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
proc->start(defaultApp.section(QStringLiteral("."), 2, 2), QStringList(QStringLiteral("--stdin")));
proc->write(text.toUtf8());
proc->closeWriteChannel();
} else {
QTemporaryFile tmpFile;
tmpFile.setFileTemplate(QStringLiteral("kdeconnect-XXXXXX.txt"));
tmpFile.setAutoRemove(false);
tmpFile.open();
tmpFile.write(text.toUtf8());
tmpFile.close();
const QString fileName = tmpFile.fileName();
QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
Q_EMIT shareReceived(fileName);
}
});
connect(notif, &KNotification::action2Activated, this, [this, url]() {
QDesktopServices::openUrl(url);
Q_EMIT shareReceived(url.toString());
});
notif->sendEvent();
} else if (np.has(QStringLiteral("url"))) {
QUrl url = QUrl::fromEncoded(np.get(QStringLiteral("url")));
QDesktopServices::openUrl(url);
Q_EMIT shareReceived(url.toString());
} else {
qCDebug(KDECONNECT_PLUGIN_SHARE) << "Error: Nothing attached!";
}
return true;
}
This method builds a network package object from an existing network packet. It creates a File object, sets the destination filename, and starts a composite file transfer job, and opens a new file or URL if it doesn't exist. It also creates a FileTransferJob object, sets the dateCreated, dateModified, and open flag to true.
If the plugin is running, it creates a FileTransferJob object, sets the result of the operation as well, and sets the notification title and mime data. If the file is opened and opened, it is opened in the default application. It also connects the notification to the default app and sends an signal.
If the plugin is not enabled, an error is emitted.