1/*
2 SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "clipboard.h"
8#include <QDebug>
9#include <QGuiApplication>
10#include <QMimeData>
11#include <QUrl>
12
13Clipboard::Clipboard(QObject *parent)
14 : QObject(parent)
15 , m_clipboard(QGuiApplication::clipboard())
16 , m_mode(QClipboard::Clipboard)
17{
18 connect(sender: m_clipboard, signal: &QClipboard::changed, context: this, slot: &Clipboard::clipboardChanged);
19}
20
21void Clipboard::setMode(QClipboard::Mode mode)
22{
23 m_mode = mode;
24 Q_EMIT modeChanged(mode: m_mode);
25}
26
27void Clipboard::clipboardChanged(QClipboard::Mode m)
28{
29 if (m == m_mode) {
30 Q_EMIT contentChanged();
31 }
32}
33
34void Clipboard::clear()
35{
36 m_clipboard->clear(mode: m_mode);
37}
38
39QClipboard::Mode Clipboard::mode() const
40{
41 return m_mode;
42}
43
44QVariant Clipboard::contentFormat(const QString &format) const
45{
46 const QMimeData *data = m_clipboard->mimeData(mode: m_mode);
47 QVariant ret;
48 if (format == QLatin1String("text/uri-list")) {
49 QVariantList retList;
50 const auto urls = data->urls();
51 for (const QUrl &url : urls) {
52 retList += url;
53 }
54 ret = retList;
55 } else if (format.startsWith(s: QLatin1String("text/"))) {
56 ret = data->text();
57 } else if (format.startsWith(s: QLatin1String("image/"))) {
58 ret = data->imageData();
59 } else {
60 ret = data->data(mimetype: format.isEmpty() ? data->formats().first() : format);
61 }
62
63 return ret;
64}
65
66QVariant Clipboard::content() const
67{
68 return contentFormat(format: m_clipboard->mimeData(mode: m_mode)->formats().first());
69}
70
71void Clipboard::setContent(const QVariant &content)
72{
73 QMimeData *mimeData = new QMimeData;
74 switch (content.userType()) {
75 case QMetaType::QString:
76 mimeData->setText(content.toString());
77 break;
78 case QMetaType::QColor:
79 mimeData->setColorData(content.toString());
80 break;
81 case QMetaType::QPixmap:
82 case QMetaType::QImage:
83 mimeData->setImageData(content);
84 break;
85 default:
86 if (content.userType() == QMetaType::QVariantList) {
87 const QVariantList list = content.toList();
88 QList<QUrl> urls;
89 bool wasUrlList = true;
90 for (const QVariant &url : list) {
91 if (url.userType() != QMetaType::QUrl) {
92 wasUrlList = false;
93 break;
94 }
95 urls += url.toUrl();
96 }
97 if (wasUrlList) {
98 mimeData->setUrls(urls);
99 break;
100 }
101 }
102
103 if (content.canConvert<QString>()) {
104 mimeData->setText(content.toString());
105 } else {
106 mimeData->setData(QStringLiteral("application/octet-stream"), data: content.toByteArray());
107 qWarning() << "Couldn't figure out the content type, storing as application/octet-stream";
108 }
109 break;
110 }
111 m_clipboard->setMimeData(data: mimeData, mode: m_mode);
112}
113
114QStringList Clipboard::formats() const
115{
116 return m_clipboard->mimeData(mode: m_mode)->formats();
117}
118
119#include "moc_clipboard.cpp"
120

source code of kdeclarative/src/qmlcontrols/kquickcontrolsaddons/clipboard.cpp