File indexing completed on 2024-04-21 03:41:33

0001 /*
0002  *    Copyright 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *    This library is free software; you can redistribute it and/or
0005  *    modify it under the terms of the GNU Library General Public
0006  *    License as published by the Free Software Foundation; either
0007  *    version 2 of the License, or (at your option) any later version.
0008  *
0009  *    This library is distributed in the hope that it will be useful,
0010  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  *    Library General Public License for more details.
0013  *
0014  *    You should have received a copy of the GNU Library General Public License
0015  *    along with this library; see the file COPYING.LIB.  If not, write to
0016  *    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  *    Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "clipboard.h"
0021 #include <QDebug>
0022 #include <QGuiApplication>
0023 #include <QMimeData>
0024 #include <QUrl>
0025 
0026 Clipboard::Clipboard(QObject *parent)
0027     : QObject(parent)
0028     , m_clipboard(QGuiApplication::clipboard())
0029     , m_mode(QClipboard::Clipboard)
0030 {
0031     connect(m_clipboard, &QClipboard::changed, this, &Clipboard::clipboardChanged);
0032 }
0033 
0034 void Clipboard::setMode(QClipboard::Mode mode)
0035 {
0036     m_mode = mode;
0037     Q_EMIT modeChanged(m_mode);
0038 }
0039 
0040 void Clipboard::clipboardChanged(QClipboard::Mode m)
0041 {
0042     if (m == m_mode) {
0043         Q_EMIT contentChanged();
0044     }
0045 }
0046 
0047 void Clipboard::clear()
0048 {
0049     m_clipboard->clear(m_mode);
0050 }
0051 
0052 QClipboard::Mode Clipboard::mode() const
0053 {
0054     return m_mode;
0055 }
0056 
0057 QVariant Clipboard::contentFormat(const QString &format) const
0058 {
0059     const QMimeData *data = m_clipboard->mimeData(m_mode);
0060     QVariant ret;
0061     if (format == QStringLiteral("text/uri-list")) {
0062         QVariantList retList;
0063         const auto urls = data->urls();
0064         for (const QUrl &url : urls)
0065             retList += url;
0066         ret = retList;
0067     } else if (format.startsWith(QStringLiteral("text/"))) {
0068         ret = data->text();
0069     } else if (format.startsWith(QStringLiteral("image/"))) {
0070         ret = data->imageData();
0071     } else
0072         ret = data->data(format.isEmpty() ? data->formats().first() : format);
0073 
0074     return ret;
0075 }
0076 
0077 QVariant Clipboard::content() const
0078 {
0079     return contentFormat(m_clipboard->mimeData(m_mode)->formats().first());
0080 }
0081 
0082 void Clipboard::setContent(const QVariant &content)
0083 {
0084     QMimeData *mimeData = new QMimeData;
0085     switch (content.userType()) {
0086     case QMetaType::QString:
0087         mimeData->setText(content.toString());
0088         break;
0089     case QMetaType::QColor:
0090         mimeData->setColorData(content.toString());
0091         break;
0092     case QMetaType::QPixmap:
0093     case QMetaType::QImage:
0094         mimeData->setImageData(content);
0095         break;
0096     default:
0097         if (content.userType() == QMetaType::QVariantList) {
0098             const QVariantList list = content.toList();
0099             QList<QUrl> urls;
0100             bool wasUrlList = true;
0101             for (const QVariant &url : list) {
0102                 if (url.userType() != QMetaType::QUrl) {
0103                     wasUrlList = false;
0104                     break;
0105                 }
0106                 urls += url.toUrl();
0107             }
0108             if (wasUrlList) {
0109                 mimeData->setUrls(urls);
0110                 break;
0111             }
0112         }
0113         if (content.canConvert<QString>()) {
0114             mimeData->setText(content.toString());
0115         } else {
0116             mimeData->setData(QStringLiteral("application/octet-stream"), content.toByteArray());
0117             qWarning() << "Couldn't figure out the content type, storing as application/octet-stream";
0118         }
0119         break;
0120     }
0121     m_clipboard->setMimeData(mimeData, m_mode);
0122 }
0123 
0124 QStringList Clipboard::formats() const
0125 {
0126     return m_clipboard->mimeData(m_mode)->formats();
0127 }