| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2024 Aleix Pol Gonzalez <aleixpol@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #include <KLocalizedString> |
| 8 | #include <KPluginFactory> |
| 9 | |
| 10 | #include <QClipboard> |
| 11 | #include <QFile> |
| 12 | #include <QGuiApplication> |
| 13 | #include <QImage> |
| 14 | #include <QJsonArray> |
| 15 | #include <QTimer> |
| 16 | #include <purpose/pluginbase.h> |
| 17 | |
| 18 | class ClipboardJob : public Purpose::Job |
| 19 | { |
| 20 | Q_OBJECT |
| 21 | public: |
| 22 | ClipboardJob(QObject *parent) |
| 23 | : Purpose::Job(parent) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | void start() override |
| 28 | { |
| 29 | QTimer::singleShot(interval: 0, receiver: this, slot: [this] { |
| 30 | QMimeData *mimeData = new QMimeData; |
| 31 | |
| 32 | QString text; |
| 33 | QList<QUrl> urls; |
| 34 | for (const QJsonValue &jsonUrl : data().value(key: QLatin1String("urls" )).toArray()) { |
| 35 | const QString jsonUrlString = jsonUrl.toString(); |
| 36 | |
| 37 | const QUrl url{jsonUrlString}; |
| 38 | // Keep TolerantMode for QUrl but filter out obvious garbage. |
| 39 | if (!url.scheme().isEmpty()) { |
| 40 | urls.append(t: url); |
| 41 | } |
| 42 | if (!text.isEmpty()) { |
| 43 | text.append(c: QLatin1Char('\n')); |
| 44 | } |
| 45 | text.append(s: jsonUrlString); |
| 46 | } |
| 47 | |
| 48 | mimeData->setText(text); |
| 49 | mimeData->setUrls(urls); |
| 50 | qGuiApp->clipboard()->setMimeData(data: mimeData); |
| 51 | emitResult(); |
| 52 | }); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | class ClipboardPlugin : public Purpose::PluginBase |
| 57 | { |
| 58 | Q_OBJECT |
| 59 | public: |
| 60 | using PluginBase::PluginBase; |
| 61 | Purpose::Job *createJob() const override |
| 62 | { |
| 63 | return new ClipboardJob(nullptr); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | K_PLUGIN_FACTORY_WITH_JSON(Clipboard, "clipboardplugin.json" , registerPlugin<ClipboardPlugin>();) |
| 68 | |
| 69 | #include "clipboardplugin.moc" |
| 70 | |