diff --git a/plugins/kdeconnect.notifyrc b/plugins/kdeconnect.notifyrc index 00f6e126e..135ad866e 100644 --- a/plugins/kdeconnect.notifyrc +++ b/plugins/kdeconnect.notifyrc @@ -851,3 +851,9 @@ Comment[x-test]=xxRemote lock failedxx Comment[zh_CN]=远程锁定失败 Comment[zh_TW]=無法鎖定遠端裝置 Action=Popup + +[Event/textShareReceived] +Name=Text Received +Comment=Received some text +Action=Popup +Urgency=Low diff --git a/plugins/share/CMakeLists.txt b/plugins/share/CMakeLists.txt index 74a74d9f8..30ffb5ad5 100644 --- a/plugins/share/CMakeLists.txt +++ b/plugins/share/CMakeLists.txt @@ -17,6 +17,7 @@ target_link_libraries(kdeconnect_share Qt::DBus KF5::Notifications KF5::I18n + KF5::GuiAddons KF5::KIOWidgets KF5::Service ) diff --git a/plugins/share/shareplugin.cpp b/plugins/share/shareplugin.cpp index f9dd46d11..a476f5d05 100644 --- a/plugins/share/shareplugin.cpp +++ b/plugins/share/shareplugin.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -20,7 +21,9 @@ #include #include #include +#include #include +#include #include "core/daemon.h" #include "core/filetransferjob.h" @@ -146,27 +149,59 @@ bool SharePlugin::receivePacket(const NetworkPacket &np) } else if (np.has(QStringLiteral("text"))) { QString text = np.get(QStringLiteral("text")); - KService::Ptr service = KApplicationTrader::preferredService(QStringLiteral("text/plain")); - const QString defaultApp = service ? service->desktopEntryName() : QString(); + auto mimeData = new QMimeData; + mimeData->setText(text); + KSystemClipboard::instance()->setMimeData(mimeData, QClipboard::Clipboard); - 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(); - Q_EMIT shareReceived(fileName); - QDesktopServices::openUrl(QUrl::fromLocalFile(fileName)); + 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);