plugins/share: Copy text to clipboard, and provide notification with more actions
This commit is contained in:
parent
d0dc27106a
commit
f9d10a5849
3 changed files with 61 additions and 19 deletions
|
@ -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
|
||||
|
|
|
@ -17,6 +17,7 @@ target_link_libraries(kdeconnect_share
|
|||
Qt::DBus
|
||||
KF5::Notifications
|
||||
KF5::I18n
|
||||
KF5::GuiAddons
|
||||
KF5::KIOWidgets
|
||||
KF5::Service
|
||||
)
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <QDateTime>
|
||||
#include <QDesktopServices>
|
||||
#include <QDir>
|
||||
#include <QMimeData>
|
||||
#include <QProcess>
|
||||
#include <QStandardPaths>
|
||||
#include <QTemporaryFile>
|
||||
|
@ -20,7 +21,9 @@
|
|||
#include <KIO/MkpathJob>
|
||||
#include <KJobTrackerInterface>
|
||||
#include <KLocalizedString>
|
||||
#include <KNotification>
|
||||
#include <KPluginFactory>
|
||||
#include <KSystemClipboard>
|
||||
|
||||
#include "core/daemon.h"
|
||||
#include "core/filetransferjob.h"
|
||||
|
@ -146,6 +149,29 @@ bool SharePlugin::receivePacket(const NetworkPacket &np)
|
|||
} else if (np.has(QStringLiteral("text"))) {
|
||||
QString text = np.get<QString>(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();
|
||||
|
||||
|
@ -164,9 +190,18 @@ bool SharePlugin::receivePacket(const NetworkPacket &np)
|
|||
tmpFile.close();
|
||||
|
||||
const QString fileName = tmpFile.fileName();
|
||||
Q_EMIT shareReceived(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<QByteArray>(QStringLiteral("url")));
|
||||
QDesktopServices::openUrl(url);
|
||||
|
|
Loading…
Reference in a new issue