File indexing completed on 2025-01-05 03:29:15
0001 // SPDX-FileCopyrightText: 2019 Black Hat <bhat@encom.eu.org> 0002 // SPDX-License-Identifier: GPL-3.0-only 0003 0004 #include "clipboard.h" 0005 0006 #include <QClipboard> 0007 #include <QDateTime> 0008 #include <QDir> 0009 #include <QGuiApplication> 0010 #include <QMimeData> 0011 #include <QRegularExpression> 0012 #include <QStandardPaths> 0013 #include <QUrl> 0014 0015 Clipboard::Clipboard(QObject *parent) 0016 : QObject(parent) 0017 , m_clipboard(QGuiApplication::clipboard()) 0018 { 0019 connect(m_clipboard, &QClipboard::changed, this, &Clipboard::imageChanged); 0020 } 0021 0022 bool Clipboard::hasImage() const 0023 { 0024 return !image().isNull(); 0025 } 0026 0027 QImage Clipboard::image() const 0028 { 0029 return m_clipboard->image(); 0030 } 0031 0032 QString Clipboard::saveImage(QString localPath) const 0033 { 0034 QString imageDir(QStringLiteral("%1/screenshots").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))); 0035 0036 if (!QDir().exists(imageDir)) { 0037 QDir().mkdir(imageDir); 0038 } 0039 0040 if (localPath.isEmpty()) { 0041 localPath = QStringLiteral("file://%1/%2.png").arg(imageDir, QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd-hh-mm-ss"))); 0042 } 0043 QUrl url(localPath); 0044 if (!url.isLocalFile()) { 0045 return {}; 0046 } 0047 auto image = this->image(); 0048 0049 if (image.isNull()) { 0050 return {}; 0051 } 0052 0053 if (image.save(url.toLocalFile())) { 0054 return localPath; 0055 } else { 0056 return {}; 0057 } 0058 } 0059 0060 void Clipboard::saveText(QString message) 0061 { 0062 static QRegularExpression re(QStringLiteral("<[^>]*>")); 0063 auto *mineData = new QMimeData; // ownership is transferred to clipboard 0064 mineData->setHtml(message); 0065 mineData->setText(message.replace(re, QString())); 0066 m_clipboard->setMimeData(mineData); 0067 } 0068 0069 #include "moc_clipboard.cpp"