Purpose

clipboardplugin.cpp
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
18class ClipboardJob : public Purpose::Job
19{
21public:
22 ClipboardJob(QObject *parent)
23 : Purpose::Job(parent)
24 {
25 }
26
27 QList<QUrl> arrayToList(const QJsonArray &array)
28 {
29 QList<QUrl> ret;
30 ret.reserve(array.size());
31 for (const QJsonValue &val : array) {
32 ret += QUrl(val.toString());
33 }
34 return ret;
35 }
36
37 void start() override
38 {
39 QTimer::singleShot(0, this, [this] {
40 QMimeData *mimeData = new QMimeData;
41 mimeData->setUrls(arrayToList(data().value(QLatin1String("urls")).toArray()));
42 qGuiApp->clipboard()->setMimeData(mimeData);
43 emitResult();
44 });
45 }
46};
47
48class ClipboardPlugin : public Purpose::PluginBase
49{
51public:
52 using PluginBase::PluginBase;
53 Purpose::Job *createJob() const override
54 {
55 return new ClipboardJob(nullptr);
56 }
57};
58
59K_PLUGIN_FACTORY_WITH_JSON(Clipboard, "clipboardplugin.json", registerPlugin<ClipboardPlugin>();)
60
61#include "clipboardplugin.moc"
void emitResult()
#define K_PLUGIN_FACTORY_WITH_JSON(name, jsonFile, pluginRegistrations)
Job that will actually perform the sharing.
Definition job.h:34
QJsonObject data
Represents the data the job will have available to perform its task.
Definition job.h:39
Base class to implement by plugins.
Definition pluginbase.h:29
qsizetype size() const const
void reserve(qsizetype size)
void setUrls(const QList< QUrl > &urls)
Q_OBJECTQ_OBJECT
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.