2
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
3
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 2 or (at your option)
8
* version 3 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include <QApplication>
27
#include "core/Config.h"
29
Clipboard* Clipboard::m_instance(nullptr);
31
QPointer<MacPasteboard> Clipboard::m_pasteboard(nullptr);
34
Clipboard::Clipboard(QObject* parent)
36
, m_timer(new QTimer(this))
40
m_pasteboard = new MacPasteboard();
43
connect(m_timer, SIGNAL(timeout()), SLOT(countdownTick()));
44
connect(qApp, SIGNAL(aboutToQuit()), SLOT(clearCopiedText()));
47
void Clipboard::setText(const QString& text, bool clear)
49
auto* clipboard = QApplication::clipboard();
51
qWarning("Unable to access the clipboard.");
55
auto* mime = new QMimeData;
57
#if defined(Q_OS_MACOS)
58
mime->setData("application/x-nspasteboard-concealed-type", text.toUtf8());
59
#elif defined(Q_OS_UNIX)
60
mime->setData("x-kde-passwordManagerHint", QByteArrayLiteral("secret"));
61
#elif defined(Q_OS_WIN)
62
mime->setData("ExcludeClipboardContentFromMonitorProcessing", QByteArrayLiteral("1"));
65
if (clipboard->supportsSelection()) {
66
clipboard->setMimeData(mime, QClipboard::Selection);
68
clipboard->setMimeData(mime, QClipboard::Clipboard);
72
if (config()->get(Config::Security_ClearClipboard).toBool()) {
73
int timeout = config()->get(Config::Security_ClearClipboardTimeout).toInt();
75
m_secondsToClear = timeout;
76
sendCountdownStatus();
85
int Clipboard::secondsToClear()
87
return m_secondsToClear;
90
void Clipboard::clearCopiedText()
93
emit updateCountdown(-1, "");
95
auto* clipboard = QApplication::clipboard();
97
qWarning("Unable to access the clipboard.");
101
if (m_lastCopied == clipboard->text(QClipboard::Clipboard)
102
|| m_lastCopied == clipboard->text(QClipboard::Selection)) {
103
clipboard->clear(QClipboard::Clipboard);
104
clipboard->clear(QClipboard::Selection);
106
// Gnome Wayland doesn't let apps modify the clipboard when not in focus, so force clear
107
if (QProcessEnvironment::systemEnvironment().contains("WAYLAND_DISPLAY")) {
108
QProcess::startDetached("wl-copy", {"-c"});
113
m_lastCopied.clear();
116
void Clipboard::countdownTick()
118
if (--m_secondsToClear <= 0) {
121
sendCountdownStatus();
125
void Clipboard::sendCountdownStatus()
127
emit updateCountdown(
128
100 * m_secondsToClear / config()->get(Config::Security_ClearClipboardTimeout).toInt(),
129
QObject::tr("Clearing the clipboard in %1 second(s)…", "", m_secondsToClear).arg(m_secondsToClear));
132
Clipboard* Clipboard::instance()
135
m_instance = new Clipboard(qApp);