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"
|
|
|
|
|
2019-07-23 15:21:39 +01:00
|
|
|
ClipboardListener::ClipboardListener()
|
2016-09-09 15:18:56 +01:00
|
|
|
: clipboard(QGuiApplication::clipboard())
|
|
|
|
{
|
2019-07-18 12:54:41 +01:00
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
connect(&m_clipboardMonitorTimer, &QTimer::timeout, this, [this](){ updateClipboard(QClipboard::Clipboard); });
|
|
|
|
m_clipboardMonitorTimer.start(1000); // Refresh 1s
|
|
|
|
#endif
|
2016-09-09 15:18:56 +01:00
|
|
|
connect(clipboard, &QClipboard::changed, this, &ClipboardListener::updateClipboard);
|
|
|
|
}
|
|
|
|
|
2019-07-23 15:21:39 +01:00
|
|
|
void ClipboardListener::updateClipboard(QClipboard::Mode mode)
|
2016-09-09 15:18:56 +01:00
|
|
|
{
|
|
|
|
if (mode != QClipboard::Clipboard) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString content = clipboard->text();
|
|
|
|
|
2019-07-23 15:21:39 +01:00
|
|
|
if (content == m_currentContent) {
|
2016-09-09 15:18:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-07-23 15:21:39 +01:00
|
|
|
m_updateTimestamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
|
|
|
m_currentContent = content;
|
2016-09-09 15:18:56 +01:00
|
|
|
|
|
|
|
Q_EMIT clipboardChanged(content);
|
|
|
|
}
|
|
|
|
|
2019-07-23 15:21:39 +01:00
|
|
|
QString ClipboardListener::currentContent()
|
|
|
|
{
|
|
|
|
return m_currentContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 ClipboardListener::updateTimestamp(){
|
|
|
|
|
|
|
|
return m_updateTimestamp;
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:18:56 +01:00
|
|
|
void ClipboardListener::setText(const QString& content)
|
|
|
|
{
|
2019-07-23 15:21:39 +01:00
|
|
|
m_updateTimestamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
|
|
|
|
m_currentContent = content;
|
2016-09-09 15:18:56 +01:00
|
|
|
clipboard->setText(content);
|
|
|
|
}
|