11#include <QApplication>
19 connect(QApplication::instance(), &QApplication::aboutToQuit,
this,
29 const QString &p_output) {
44 QClipboard *clip = QApplication::clipboard();
46 QClipboard::Mode mode = QClipboard::Clipboard;
48 mode = QClipboard::Selection;
57 std::optional<QString> &previous = mode == QClipboard::Selection
60 const QString now = clip->text(mode);
68 previous = safe ? now : QString();
85 for (std::optional<QString> *previous :
87 if (*previous && **previous == text) {
88 *previous = QString();
100 constexpr qsizetype kMaxOtherCopies = 16;
107 const QString &last =
109 if (!last.isEmpty() && (text.contains(last) || last.contains(text)) &&
110 QApplication::clipboard()->text(QClipboard::Clipboard) != last) {
117 const QString onClipboard =
118 QApplication::clipboard()->text(QClipboard::Clipboard);
119 const QString inSelection =
120 QApplication::clipboard()->text(QClipboard::Selection);
140 const std::optional<QString> previousClipboard =
142 const std::optional<QString> previousSelection =
149 const auto ours = [
this](
const QString &text) {
150 return !text.isEmpty() &&
153 QClipboard *clipboard = QApplication::clipboard();
155 bool cleared =
false;
156 bool restored =
false;
164 const auto putBack = [&](QClipboard::Mode mode,
165 const std::optional<QString> &previous) {
166 const QString now = clipboard->text(mode);
170 if (restore && previous && !previous->isEmpty() && now ==
m_clippedText) {
171 clipboard->setText(*previous, mode);
175 clipboard->clear(mode);
176 if (mode == QClipboard::Selection) {
177 clipboard->setText(QString(
""), QClipboard::Selection);
181 putBack(QClipboard::Selection, previousSelection);
182 putBack(QClipboard::Clipboard, previousClipboard);
184 : cleared ? tr(
"Clipboard cleared")
185 : tr(
"Clipboard not cleared"));
191 auto *mimeData =
new QMimeData();
192 mimeData->setText(text);
195#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
196 mimeData->setData(
"x-kde-passwordManagerHint", QByteArray(
"secret"));
199 mimeData->setData(
"application/x-nspasteboard-concealed-type", QByteArray());
202 mimeData->setData(
"ExcludeClipboardContentFromMonitorProcessing",
204 mimeData->setData(
"CanIncludeInClipboardHistory", dwordBytes(0));
205 mimeData->setData(
"CanUploadToCloudClipboard", dwordBytes(0));
void trackCopied(const QString &text)
Track text that got onto the clipboard some other way.
QStringList m_otherCopies
trackCopied() texts, cleared with the rest
void copyIfAlways(const QString &text, const QString &p_output=QString())
Handle clipboard copying for a shown entry.
void setAutoclearTimer()
Re-read the autoclear delay from the settings.
void forgetIfSaved(const QString &text)
Empty a saved previous text equal to text, now copied as a secret, so clear() does not put the secret...
void copyText(const QString &text)
Copy text into the system clipboard (or the X11 primary selection when configured) and arm the autocl...
QTimer m_clearTimer
Single-shot autoclear timer; started by copyText() when the autoclear setting is on,...
void clear()
Remove the tracked text from the clipboard if it is still there. Silent no-op when nothing is tracked...
std::optional< QString > m_previousSelection
As m_previousClipboard, for the X11 primary selection.
QString m_clippedText
The secret copyText() last put on the clipboard; clear() removes the clipboard content only while it ...
void statusMessage(const QString &message)
Something worth showing in the status bar happened.
ClipboardManager(QObject *parent=nullptr)
Construct a manager that also clears its text when the application quits.
std::optional< QString > m_previousClipboard
static auto load() -> AppSettings
Load all flat settings into an AppSettings value object.
static auto getAutoclearSeconds(const int &defaultValue=QVariant().toInt()) -> int
Get autoclear delay in seconds.
auto buildClipboardMimeData(const QString &text) -> QMimeData *
Build clipboard MIME data with platform-specific security hints.
ClipboardManager, which puts secrets on the clipboard and removes them again, and the MIME data that ...
auto buildClipboardMimeData(const QString &text) -> QMimeData *
Build clipboard MIME data with platform-specific security hints.
QtPassSettings, the QSettings singleton behind every saved setting, the profiles and the Pass backend...
Plain value-object holding all flat QtPass configuration.
bool useSelection
Use the X11 primary selection.
Enums::ClipBoardType clipBoardType
Clipboard copy behaviour.
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...
constexpr int MS_PER_SECOND
Milliseconds in a second, for timers set from a setting in seconds.