From b9a089344efeb68ae4e6b5d780a39de398208fe3 Mon Sep 17 00:00:00 2001 From: Yuchen Shi Date: Mon, 17 Oct 2022 21:04:20 +0000 Subject: [PATCH] Add config for clipboard plugin on content types to share. ## Summary This MR adds a config dialog for the Clipboard plugin, where the user can toggle whether to share password and/or anything else with other devices. Implements !39 BUG: 458063 According to previous discussion at !39 (and the bug linked), some users want to skip sending passwords to other devices while others rely on the existing behavior. This MR addresses that by allowing users to control those two types separately and will replace !39 if merged. ![Screenshot with new config dialog](/uploads/3dfc6c6d69b86e6512e6a8948320a839/Screenshot_1661407807.png) Borrowing the idea from !39, the `x-kde-passwordManagerHint` MIME data hint is used to determine whether the content is considered secret. I've tested this method with KeepassXC which [sets this correctly](https://github.com/keepassxreboot/keepassxc/blob/a6d3f973fa8449f0f7dac864b3bd3928c29c649f/src/gui/Clipboard.cpp#L62). See test plan below. In theory, MIME also can be used to test for images and another checkbox can be easily added should we decided to support images later. For now though, the enum and the config supports only passwords or "anything else". Both defaults to true, thus keeping full backwards compatibility. I've been keeping both unchecked since I only share in one direction (Android to Linux), which wasn't possible until this MR. Others may want to make their own choices. ## Test Plan Please reference the screenshot above for the steps. From the indicator, right-click and select `Configure`. Select a device from the left side (or pair one if needed). In "available plugins", make sure "Clipboard" is checked, and verify that a "Configure" icon button is now shown on the right. Clicking that icon should bring up the new config dialog, where the two check boxes can be toggled as desired. Click on "OK" to save and apply the settings. Clicking on "Defaults" should bring both back to checked (the default behavior). Copy normal plain text from your favorite text editor and it should be sent to another device if the "anything else" checkbox is checked. I've tested this on an Android phone but I see no reason it won't work elsewhere given the code for sending has not been changed. (Feel free to test on other devices.) Open KeepassXC and select an entry, right click and select "Copy password". This sets the MIME hint so it should be treated as a password, controlled by the "passwords" checkbox. I've toggled the settings multiple times on and off. Once "OK" is clicked, the next clipboard change should pick up the latest settings. I've only tested on Linux desktops (Arch Linux) since I do not own a Mac. I don't believe any OS X application sets `x-kde-passwordManagerHint` at all, so they may be controlled by "anything else". --- plugins/clipboard/CMakeLists.txt | 11 +++ plugins/clipboard/clipboard_config.cpp | 56 +++++++++++++++ plugins/clipboard/clipboard_config.h | 34 +++++++++ plugins/clipboard/clipboard_config.ui | 69 +++++++++++++++++++ plugins/clipboard/clipboardlistener.cpp | 21 ++++-- plugins/clipboard/clipboardlistener.h | 9 ++- plugins/clipboard/clipboardplugin.cpp | 14 +++- plugins/clipboard/clipboardplugin.h | 4 +- plugins/clipboard/kdeconnect_clipboard.json | 1 + .../clipboard/kdeconnect_clipboard_config.qml | 39 +++++++++++ 10 files changed, 249 insertions(+), 9 deletions(-) create mode 100644 plugins/clipboard/clipboard_config.cpp create mode 100644 plugins/clipboard/clipboard_config.h create mode 100644 plugins/clipboard/clipboard_config.ui create mode 100644 plugins/clipboard/kdeconnect_clipboard_config.qml diff --git a/plugins/clipboard/CMakeLists.txt b/plugins/clipboard/CMakeLists.txt index 0b271c84c..5a118fc21 100644 --- a/plugins/clipboard/CMakeLists.txt +++ b/plugins/clipboard/CMakeLists.txt @@ -18,3 +18,14 @@ target_link_libraries(kdeconnect_clipboard kdeconnectcore KF5::GuiAddons ${kdeconnect_clipboard_WL_LINK_LIBS} ) + +kcoreaddons_add_plugin(kdeconnect_clipboard_config INSTALL_NAMESPACE "kdeconnect/kcms") + +ki18n_wrap_ui(kdeconnect_clipboard_config clipboard_config.ui) +target_sources(kdeconnect_clipboard_config PRIVATE clipboard_config.cpp ${debug_file_SRCS}) +target_link_libraries( kdeconnect_clipboard_config + kdeconnectcore + kdeconnectpluginkcm + KF5::I18n + KF5::KCMUtils +) diff --git a/plugins/clipboard/clipboard_config.cpp b/plugins/clipboard/clipboard_config.cpp new file mode 100644 index 000000000..b07073873 --- /dev/null +++ b/plugins/clipboard/clipboard_config.cpp @@ -0,0 +1,56 @@ +/** + * SPDX-FileCopyrightText: 2022 Yuchen Shi + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#include "clipboard_config.h" +#include "ui_clipboard_config.h" + +#include + +K_PLUGIN_FACTORY(ClipboardConfigFactory, registerPlugin();) + +ClipboardConfig::ClipboardConfig(QWidget* parent, const QVariantList &args) + : KdeConnectPluginKcm(parent, args, QStringLiteral("kdeconnect_clipboard")) + , m_ui(new Ui::ClipboardConfigUi()) +{ + m_ui->setupUi(this); + + connect(m_ui->check_unknown, SIGNAL(toggled(bool)), this, SLOT(changed())); + connect(m_ui->check_password, SIGNAL(toggled(bool)), this, SLOT(changed())); +} + +ClipboardConfig::~ClipboardConfig() +{ + delete m_ui; +} + +void ClipboardConfig::defaults() +{ + KCModule::defaults(); + m_ui->check_unknown->setChecked(true); + m_ui->check_password->setChecked(true); + Q_EMIT changed(true); +} + +void ClipboardConfig::load() +{ + KCModule::load(); + bool unknown = config()->getBool(QStringLiteral("sendUnknown"), true); + bool password = config()->getBool(QStringLiteral("sendPassword"), true); + m_ui->check_unknown->setChecked(unknown); + m_ui->check_password->setChecked(password); + + Q_EMIT changed(false); +} + +void ClipboardConfig::save() +{ + config()->set(QStringLiteral("sendUnknown"), m_ui->check_unknown->isChecked()); + config()->set(QStringLiteral("sendPassword"), m_ui->check_password->isChecked()); + KCModule::save(); + Q_EMIT changed(false); +} + +#include "clipboard_config.moc" diff --git a/plugins/clipboard/clipboard_config.h b/plugins/clipboard/clipboard_config.h new file mode 100644 index 000000000..c30e23f29 --- /dev/null +++ b/plugins/clipboard/clipboard_config.h @@ -0,0 +1,34 @@ +/** + * SPDX-FileCopyrightText: 2022 Yuchen Shi + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#ifndef CLIPBOARD_CONFIG_H +#define CLIPBOARD_CONFIG_H + +#include "kcmplugin/kdeconnectpluginkcm.h" + +namespace Ui { + class ClipboardConfigUi; +} + +class ClipboardConfig + : public KdeConnectPluginKcm +{ + Q_OBJECT +public: + ClipboardConfig(QWidget *parent, const QVariantList&); + ~ClipboardConfig() override; + +public Q_SLOTS: + void save() override; + void load() override; + void defaults() override; + +private: + Ui::ClipboardConfigUi *m_ui; + +}; + +#endif diff --git a/plugins/clipboard/clipboard_config.ui b/plugins/clipboard/clipboard_config.ui new file mode 100644 index 000000000..25398b350 --- /dev/null +++ b/plugins/clipboard/clipboard_config.ui @@ -0,0 +1,69 @@ + + + ClipboardConfigUi + + + Qt::WindowModal + + + + 0 + 0 + 368 + 241 + + + + Clipboard plugin + + + + 20 + + + + + + 0 + 0 + + + + Contents shared to other devices + + + + + + Passwords (as marked by password managers) + + + + + + + Anything else + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + diff --git a/plugins/clipboard/clipboardlistener.cpp b/plugins/clipboard/clipboardlistener.cpp index 1a62c301a..2972aca93 100644 --- a/plugins/clipboard/clipboardlistener.cpp +++ b/plugins/clipboard/clipboardlistener.cpp @@ -16,6 +16,11 @@ QString ClipboardListener::currentContent() return m_currentContent; } +ClipboardListener::ClipboardContentType ClipboardListener::currentContentType() +{ + return m_currentContentType; +} + qint64 ClipboardListener::updateTimestamp() { return m_updateTimestamp; @@ -30,10 +35,11 @@ ClipboardListener *ClipboardListener::instance() return me; } -void ClipboardListener::refreshContent(const QString &content) +void ClipboardListener::refreshContent(const QString &content, ClipboardListener::ClipboardContentType contentType) { m_updateTimestamp = QDateTime::currentDateTime().toMSecsSinceEpoch(); m_currentContent = content; + m_currentContentType = contentType; } ClipboardListener::ClipboardListener() @@ -54,17 +60,22 @@ void ClipboardListener::updateClipboard(QClipboard::Mode mode) return; } + ClipboardListener::ClipboardContentType contentType = ClipboardListener::ClipboardContentTypeUnknown; + if (clipboard->mimeData(mode)->data(QStringLiteral("x-kde-passwordManagerHint")) == QByteArrayLiteral("secret")) { + contentType = ClipboardListener::ClipboardContentTypePassword; + } + const QString content = clipboard->text(QClipboard::Clipboard); - if (content == m_currentContent) { + if (content == m_currentContent && contentType == m_currentContentType) { return; } - refreshContent(content); - Q_EMIT clipboardChanged(content); + refreshContent(content, contentType); + Q_EMIT clipboardChanged(content, contentType); } void ClipboardListener::setText(const QString &content) { - refreshContent(content); + refreshContent(content, ClipboardListener::ClipboardContentTypeUnknown); auto mime = new QMimeData; mime->setText(content); clipboard->setMimeData(mime, QClipboard::Clipboard); diff --git a/plugins/clipboard/clipboardlistener.h b/plugins/clipboard/clipboardlistener.h index 15e7cae82..e84d8a72e 100644 --- a/plugins/clipboard/clipboardlistener.h +++ b/plugins/clipboard/clipboardlistener.h @@ -24,10 +24,14 @@ class ClipboardListener : public QObject { Q_OBJECT +public: + enum ClipboardContentType { ClipboardContentTypeUnknown = 0, ClipboardContentTypePassword = 1 }; + protected: ClipboardListener(); - void refreshContent(const QString &content); + void refreshContent(const QString &content, ClipboardContentType contentType); QString m_currentContent; + ClipboardContentType m_currentContentType; private: qint64 m_updateTimestamp = 0; @@ -38,10 +42,11 @@ public: void setText(const QString &content); QString currentContent(); + ClipboardContentType currentContentType(); qint64 updateTimestamp(); Q_SIGNALS: - void clipboardChanged(const QString &content); + void clipboardChanged(const QString &content, ClipboardContentType contentType); private: void updateClipboard(QClipboard::Mode mode); diff --git a/plugins/clipboard/clipboardplugin.cpp b/plugins/clipboard/clipboardplugin.cpp index 8e4d7175e..f20119cca 100644 --- a/plugins/clipboard/clipboardplugin.cpp +++ b/plugins/clipboard/clipboardplugin.cpp @@ -24,8 +24,20 @@ void ClipboardPlugin::connected() sendConnectPacket(); } -void ClipboardPlugin::propagateClipboard(const QString &content) +void ClipboardPlugin::propagateClipboard(const QString &content, ClipboardListener::ClipboardContentType contentType) { + if (contentType == ClipboardListener::ClipboardContentTypeUnknown) { + if (!config()->getBool(QStringLiteral("sendUnknown"), true)) { + return; + } + } else if (contentType == ClipboardListener::ClipboardContentTypePassword) { + if (!config()->getBool(QStringLiteral("sendPassword"), true)) { + return; + } + } else { + return; + } + NetworkPacket np(PACKET_TYPE_CLIPBOARD, {{QStringLiteral("content"), content}}); sendPacket(np); } diff --git a/plugins/clipboard/clipboardplugin.h b/plugins/clipboard/clipboardplugin.h index 01afd8ab4..24c38604a 100644 --- a/plugins/clipboard/clipboardplugin.h +++ b/plugins/clipboard/clipboardplugin.h @@ -11,6 +11,8 @@ #include #include +#include "clipboardlistener.h" + /** * Packet containing just clipboard contents, sent when a device updates its clipboard. *

@@ -46,7 +48,7 @@ public: bool receivePacket(const NetworkPacket &np) override; void connected() override; private Q_SLOTS: - void propagateClipboard(const QString &content); + void propagateClipboard(const QString &content, ClipboardListener::ClipboardContentType contentType); void sendConnectPacket(); }; diff --git a/plugins/clipboard/kdeconnect_clipboard.json b/plugins/clipboard/kdeconnect_clipboard.json index 8c4ead65b..b0ec663f6 100644 --- a/plugins/clipboard/kdeconnect_clipboard.json +++ b/plugins/clipboard/kdeconnect_clipboard.json @@ -150,6 +150,7 @@ "Version": "0.1", "Website": "https://albertvaka.wordpress.com" }, + "X-KDE-ConfigModule": "kdeconnect/kcms/kdeconnect_clipboard_config", "X-KdeConnect-OutgoingPacketType": [ "kdeconnect.clipboard", "kdeconnect.clipboard.connect" diff --git a/plugins/clipboard/kdeconnect_clipboard_config.qml b/plugins/clipboard/kdeconnect_clipboard_config.qml new file mode 100644 index 000000000..9924333c8 --- /dev/null +++ b/plugins/clipboard/kdeconnect_clipboard_config.qml @@ -0,0 +1,39 @@ +/** + * SPDX-FileCopyrightText: 2022 Yuchen Shi + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +import QtQuick 2.2 +import QtQuick.Controls 2.2 +import QtQuick.Layouts 1.1 +import org.kde.kirigami 2.5 as Kirigami +import org.kde.kdeconnect 1.0 + +Kirigami.FormLayout { + + property string device + + KdeConnectPluginConfig { + id: config + deviceId: device + pluginName: "kdeconnect_clipboard" + } + + Component.onCompleted: { + unknown.checked = config.get("sendUnknown", true) + password.checked = config.get("sendPassword", true) + } + + CheckBox { + id: password + text: i18n("Passwords (as marked by password managers)") + onClicked: config.set("sendPassword", checked) + } + + CheckBox { + id: unknown + text: i18n("Anything else") + onClicked: config.set("sendUnknown", checked) + } +}