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:
Jonathan White 2020-01-12 09:15:42 -05:00
parent 0d3eb047c7
commit 6f9907a3cb
2 changed files with 14 additions and 16 deletions

View File

@ -44,20 +44,22 @@ Clipboard::Clipboard(QObject* parent)
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
mime->setText(text);
mime->setData("application/x-nspasteboard-concealed-type", text.toUtf8());
clipboard->setMimeData(mime, QClipboard::Clipboard);
#else
const QString secretStr = "secret";
QByteArray secretBa = secretStr.toUtf8();
mime->setText(text);
mime->setData("x-kde-passwordManagerHint", secretBa);
mime->setData("x-kde-passwordManagerHint", QByteArrayLiteral("secret"));
clipboard->setMimeData(mime, QClipboard::Clipboard);
if (clipboard->supportsSelection()) {
@ -65,7 +67,7 @@ void Clipboard::setText(const QString& text)
}
#endif
if (config()->get("security/clearclipboard").toBool()) {
if (clear && config()->get("security/clearclipboard").toBool()) {
int timeout = config()->get("security/clearclipboardtimeout").toInt();
if (timeout > 0) {
m_lastCopied = text;
@ -84,19 +86,15 @@ void Clipboard::clearCopiedText()
void Clipboard::clearClipboard()
{
QClipboard* clipboard = QApplication::clipboard();
auto* clipboard = QApplication::clipboard();
if (!clipboard) {
qWarning("Unable to access the clipboard.");
return;
}
if (clipboard->text(QClipboard::Clipboard) == m_lastCopied) {
clipboard->clear(QClipboard::Clipboard);
}
if (clipboard->supportsSelection() && (clipboard->text(QClipboard::Selection) == m_lastCopied)) {
clipboard->clear(QClipboard::Selection);
if (m_lastCopied == clipboard->text(QClipboard::Clipboard)
|| m_lastCopied == clipboard->text(QClipboard::Selection)) {
setText("", false);
}
m_lastCopied.clear();

View File

@ -32,7 +32,7 @@ class Clipboard : public QObject
Q_OBJECT
public:
void setText(const QString& text);
void setText(const QString& text, bool clear = true);
static Clipboard* instance();