keepassxc

Форк
0
/
Clipboard.cpp 
139 строк · 3.9 Кб
1
/*
2
 *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
3
 *  Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
4
 *
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.
9
 *
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.
14
 *
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/>.
17
 */
18

19
#include "Clipboard.h"
20

21
#include <QApplication>
22
#include <QClipboard>
23
#include <QMimeData>
24
#include <QProcess>
25
#include <QTimer>
26

27
#include "core/Config.h"
28

29
Clipboard* Clipboard::m_instance(nullptr);
30
#ifdef Q_OS_MACOS
31
QPointer<MacPasteboard> Clipboard::m_pasteboard(nullptr);
32
#endif
33

34
Clipboard::Clipboard(QObject* parent)
35
    : QObject(parent)
36
    , m_timer(new QTimer(this))
37
{
38
#ifdef Q_OS_MACOS
39
    if (!m_pasteboard) {
40
        m_pasteboard = new MacPasteboard();
41
    }
42
#endif
43
    connect(m_timer, SIGNAL(timeout()), SLOT(countdownTick()));
44
    connect(qApp, SIGNAL(aboutToQuit()), SLOT(clearCopiedText()));
45
}
46

47
void Clipboard::setText(const QString& text, bool clear)
48
{
49
    auto* clipboard = QApplication::clipboard();
50
    if (!clipboard) {
51
        qWarning("Unable to access the clipboard.");
52
        return;
53
    }
54

55
    auto* mime = new QMimeData;
56
    mime->setText(text);
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"));
63
#endif
64

65
    if (clipboard->supportsSelection()) {
66
        clipboard->setMimeData(mime, QClipboard::Selection);
67
    }
68
    clipboard->setMimeData(mime, QClipboard::Clipboard);
69

70
    if (clear) {
71
        m_lastCopied = text;
72
        if (config()->get(Config::Security_ClearClipboard).toBool()) {
73
            int timeout = config()->get(Config::Security_ClearClipboardTimeout).toInt();
74
            if (timeout > 0) {
75
                m_secondsToClear = timeout;
76
                sendCountdownStatus();
77
                m_timer->start(1000);
78
            } else {
79
                clearCopiedText();
80
            }
81
        }
82
    }
83
}
84

85
int Clipboard::secondsToClear()
86
{
87
    return m_secondsToClear;
88
}
89

90
void Clipboard::clearCopiedText()
91
{
92
    m_timer->stop();
93
    emit updateCountdown(-1, "");
94

95
    auto* clipboard = QApplication::clipboard();
96
    if (!clipboard) {
97
        qWarning("Unable to access the clipboard.");
98
        return;
99
    }
100

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);
105
#ifdef Q_OS_UNIX
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"});
109
        }
110
#endif
111
    }
112

113
    m_lastCopied.clear();
114
}
115

116
void Clipboard::countdownTick()
117
{
118
    if (--m_secondsToClear <= 0) {
119
        clearCopiedText();
120
    } else {
121
        sendCountdownStatus();
122
    }
123
}
124

125
void Clipboard::sendCountdownStatus()
126
{
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));
130
}
131

132
Clipboard* Clipboard::instance()
133
{
134
    if (!m_instance) {
135
        m_instance = new Clipboard(qApp);
136
    }
137

138
    return m_instance;
139
}
140

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.