File indexing completed on 2024-05-12 08:02:43
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 <QFileInfo> 0010 #include <QGuiApplication> 0011 #include <QImage> 0012 #include <QMimeData> 0013 #include <QRegularExpression> 0014 #include <QStandardPaths> 0015 #include <QUrl> 0016 0017 Clipboard::Clipboard(QObject *parent) 0018 : QObject(parent) 0019 , m_clipboard(QGuiApplication::clipboard()) 0020 { 0021 connect(m_clipboard, &QClipboard::changed, this, &Clipboard::imageChanged); 0022 } 0023 0024 bool Clipboard::hasImage() const 0025 { 0026 return !image().isNull(); 0027 } 0028 0029 QImage Clipboard::image() const 0030 { 0031 return m_clipboard->image(); 0032 } 0033 0034 QString Clipboard::saveImage(QString localPath) const 0035 { 0036 if (!QDir().exists(QStringLiteral("%1/screenshots").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)))) { 0037 QDir().mkdir(QStringLiteral("%1/screenshots").arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation))); 0038 } 0039 if (localPath.isEmpty()) { 0040 localPath = QStringLiteral("file://%1/screenshots/%2.png") 0041 .arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), 0042 QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd-hh-mm-ss"))); 0043 } 0044 QUrl url(localPath); 0045 if (!url.isLocalFile()) { 0046 return {}; 0047 } 0048 auto image = this->image(); 0049 0050 if (image.isNull()) { 0051 return {}; 0052 } 0053 0054 QDir dir; 0055 if (!dir.exists(localPath)) { 0056 dir.mkpath(localPath); 0057 } 0058 0059 image.save(url.toLocalFile()); 0060 0061 return localPath; 0062 } 0063 0064 void Clipboard::saveText(QString message) 0065 { 0066 QRegularExpression re(QStringLiteral("<[^>]*>")); 0067 auto *mineData = new QMimeData; // ownership is transferred to clipboard 0068 mineData->setHtml(message); 0069 mineData->setText(message.replace(re, QString())); 0070 m_clipboard->setMimeData(mineData); 0071 } 0072 0073 #include "moc_clipboard.cpp"