mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-07-25 15:55:38 -04:00
Fix clearing clipboad on Gnome
* Prefer clearing clipboard by explicitly setting the clipboard to an empty string. Qt's QClipboard::clear() method is unreliable under X11 environment. * Fixes #4126
This commit is contained in:
parent
0d3eb047c7
commit
6f9907a3cb
2 changed files with 14 additions and 16 deletions
|
@ -44,20 +44,22 @@ Clipboard::Clipboard(QObject* parent)
|
||||||
connect(qApp, SIGNAL(aboutToQuit()), SLOT(clearCopiedText()));
|
connect(qApp, SIGNAL(aboutToQuit()), SLOT(clearCopiedText()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clipboard::setText(const QString& text)
|
void Clipboard::setText(const QString& text, bool clear)
|
||||||
{
|
{
|
||||||
QClipboard* clipboard = QApplication::clipboard();
|
auto* clipboard = QApplication::clipboard();
|
||||||
|
if (!clipboard) {
|
||||||
|
qWarning("Unable to access the clipboard.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
QMimeData* mime = new QMimeData;
|
auto* mime = new QMimeData;
|
||||||
#ifdef Q_OS_MACOS
|
#ifdef Q_OS_MACOS
|
||||||
mime->setText(text);
|
mime->setText(text);
|
||||||
mime->setData("application/x-nspasteboard-concealed-type", text.toUtf8());
|
mime->setData("application/x-nspasteboard-concealed-type", text.toUtf8());
|
||||||
clipboard->setMimeData(mime, QClipboard::Clipboard);
|
clipboard->setMimeData(mime, QClipboard::Clipboard);
|
||||||
#else
|
#else
|
||||||
const QString secretStr = "secret";
|
|
||||||
QByteArray secretBa = secretStr.toUtf8();
|
|
||||||
mime->setText(text);
|
mime->setText(text);
|
||||||
mime->setData("x-kde-passwordManagerHint", secretBa);
|
mime->setData("x-kde-passwordManagerHint", QByteArrayLiteral("secret"));
|
||||||
clipboard->setMimeData(mime, QClipboard::Clipboard);
|
clipboard->setMimeData(mime, QClipboard::Clipboard);
|
||||||
|
|
||||||
if (clipboard->supportsSelection()) {
|
if (clipboard->supportsSelection()) {
|
||||||
|
@ -65,7 +67,7 @@ void Clipboard::setText(const QString& text)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (config()->get("security/clearclipboard").toBool()) {
|
if (clear && config()->get("security/clearclipboard").toBool()) {
|
||||||
int timeout = config()->get("security/clearclipboardtimeout").toInt();
|
int timeout = config()->get("security/clearclipboardtimeout").toInt();
|
||||||
if (timeout > 0) {
|
if (timeout > 0) {
|
||||||
m_lastCopied = text;
|
m_lastCopied = text;
|
||||||
|
@ -84,19 +86,15 @@ void Clipboard::clearCopiedText()
|
||||||
|
|
||||||
void Clipboard::clearClipboard()
|
void Clipboard::clearClipboard()
|
||||||
{
|
{
|
||||||
QClipboard* clipboard = QApplication::clipboard();
|
auto* clipboard = QApplication::clipboard();
|
||||||
|
|
||||||
if (!clipboard) {
|
if (!clipboard) {
|
||||||
qWarning("Unable to access the clipboard.");
|
qWarning("Unable to access the clipboard.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clipboard->text(QClipboard::Clipboard) == m_lastCopied) {
|
if (m_lastCopied == clipboard->text(QClipboard::Clipboard)
|
||||||
clipboard->clear(QClipboard::Clipboard);
|
|| m_lastCopied == clipboard->text(QClipboard::Selection)) {
|
||||||
}
|
setText("", false);
|
||||||
|
|
||||||
if (clipboard->supportsSelection() && (clipboard->text(QClipboard::Selection) == m_lastCopied)) {
|
|
||||||
clipboard->clear(QClipboard::Selection);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_lastCopied.clear();
|
m_lastCopied.clear();
|
||||||
|
|
|
@ -32,7 +32,7 @@ class Clipboard : public QObject
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void setText(const QString& text);
|
void setText(const QString& text, bool clear = true);
|
||||||
|
|
||||||
static Clipboard* instance();
|
static Clipboard* instance();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue