2016-09-09 15:18:56 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2016 Albert Vaca <albertvaka@gmail.com>
|
2016-09-09 15:18:56 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2016-09-09 15:18:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "clipboardlistener.h"
|
2021-03-03 14:24:24 +00:00
|
|
|
|
2021-12-13 15:06:25 +00:00
|
|
|
#include <KSystemClipboard>
|
2016-09-09 15:18:56 +01:00
|
|
|
|
2021-12-13 15:06:25 +00:00
|
|
|
#include <QDateTime>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QMimeData>
|
2021-03-03 14:24:24 +00:00
|
|
|
|
|
|
|
QString ClipboardListener::currentContent()
|
|
|
|
{
|
|
|
|
return m_currentContent;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
qint64 ClipboardListener::updateTimestamp()
|
|
|
|
{
|
2021-03-03 14:24:24 +00:00
|
|
|
return m_updateTimestamp;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
ClipboardListener *ClipboardListener::instance()
|
2021-03-03 14:24:24 +00:00
|
|
|
{
|
2021-12-13 15:06:25 +00:00
|
|
|
static ClipboardListener *me = nullptr;
|
2021-03-03 14:24:24 +00:00
|
|
|
if (!me) {
|
2021-12-13 15:06:25 +00:00
|
|
|
me = new ClipboardListener();
|
2021-03-03 14:24:24 +00:00
|
|
|
}
|
|
|
|
return me;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void ClipboardListener::refreshContent(const QString &content)
|
2021-03-03 14:24:24 +00:00
|
|
|
{
|
|
|
|
m_updateTimestamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
|
|
|
m_currentContent = content;
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:25 +00:00
|
|
|
ClipboardListener::ClipboardListener()
|
|
|
|
: clipboard(KSystemClipboard::instance())
|
2016-09-09 15:18:56 +01:00
|
|
|
{
|
2019-07-18 12:54:41 +01:00
|
|
|
#ifdef Q_OS_MAC
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(&m_clipboardMonitorTimer, &QTimer::timeout, this, [this]() {
|
|
|
|
updateClipboard(QClipboard::Clipboard);
|
|
|
|
});
|
|
|
|
m_clipboardMonitorTimer.start(1000); // Refresh 1s
|
2019-07-18 12:54:41 +01:00
|
|
|
#endif
|
2021-12-13 15:06:25 +00:00
|
|
|
connect(clipboard, &KSystemClipboard::changed, this, &ClipboardListener::updateClipboard);
|
2016-09-09 15:18:56 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:25 +00:00
|
|
|
void ClipboardListener::updateClipboard(QClipboard::Mode mode)
|
2016-09-09 15:18:56 +01:00
|
|
|
{
|
|
|
|
if (mode != QClipboard::Clipboard) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:25 +00:00
|
|
|
const QString content = clipboard->text(QClipboard::Clipboard);
|
2019-07-23 15:21:39 +01:00
|
|
|
if (content == m_currentContent) {
|
2016-09-09 15:18:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-03-03 14:24:24 +00:00
|
|
|
refreshContent(content);
|
2016-09-09 15:18:56 +01:00
|
|
|
Q_EMIT clipboardChanged(content);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void ClipboardListener::setText(const QString &content)
|
2016-09-09 15:18:56 +01:00
|
|
|
{
|
2021-03-03 14:24:24 +00:00
|
|
|
refreshContent(content);
|
|
|
|
auto mime = new QMimeData;
|
|
|
|
mime->setText(content);
|
2021-12-13 15:06:25 +00:00
|
|
|
clipboard->setMimeData(mime, QClipboard::Clipboard);
|
2021-03-03 14:24:24 +00:00
|
|
|
}
|