11#include <QApplication>
21 connect(QApplication::instance(), &QApplication::aboutToQuit,
this,
41auto markedSecret(
const QMimeData *data) ->
bool {
42 if (data ==
nullptr) {
45 const QStringList formats = data->formats();
46 for (
const QString &format : formats) {
47 for (
const char *mark :
48 {
"passwordManagerHint",
"nspasteboard-concealed-type",
49 "nspasteboard.ConcealedType",
50 "ExcludeClipboardContentFromMonitorProcessing"}) {
51 if (format.contains(QLatin1String(mark), Qt::CaseInsensitive)) {
68auto oneHoldsTheOther(
const QString &a,
const QString &b) ->
bool {
69 const QString &longer = a.size() >= b.size() ? a : b;
70 const QString &part = a.size() >= b.size() ? b : a;
71 const qsizetype m = part.size();
77 std::vector<qsizetype> fallback(
static_cast<size_t>(m), 0);
78 for (qsizetype i = 1, k = 0; i < m; ++i) {
79 while (k > 0 && part.at(i) != part.at(k)) {
80 k = fallback[
static_cast<size_t>(k - 1)];
82 if (part.at(i) == part.at(k)) {
85 fallback[
static_cast<size_t>(i)] = k;
87 for (qsizetype i = 0, k = 0; i < longer.size(); ++i) {
88 while (k > 0 && longer.at(i) != part.at(k)) {
89 k = fallback[
static_cast<size_t>(k - 1)];
91 if (longer.at(i) == part.at(k)) {
103 const QString &p_output) {
118 QClipboard *clip = QApplication::clipboard();
120 QClipboard::Mode mode = QClipboard::Clipboard;
122 mode = QClipboard::Selection;
153 const QString &text) {
154 std::optional<QString> &previous =
156 const QString now = clip->text(mode);
167 !markedSecret(clip->mimeData(mode));
168 previous = safe ? now : QString();
176 for (std::optional<QString> *previous :
178 if (*previous && **previous == text) {
179 *previous = QString();
185 if (text.isEmpty()) {
199 const QString &last =
201 if (!last.isEmpty() && oneHoldsTheOther(text, last) &&
202 QApplication::clipboard()->text(QClipboard::Clipboard) != last &&
203 QApplication::clipboard()->text(QClipboard::Selection) != last) {
219 constexpr qsizetype kMaxOtherCopies = 16;
224 const QString onClipboard =
225 QApplication::clipboard()->text(QClipboard::Clipboard);
226 const QString inSelection =
227 QApplication::clipboard()->text(QClipboard::Selection);
244 const std::optional<QString> previousClipboard =
246 const std::optional<QString> previousSelection =
253 const auto ours = [
this](
const QString &text) {
254 return !text.isEmpty() &&
257 QClipboard *clipboard = QApplication::clipboard();
259 bool cleared =
false;
260 bool restored =
false;
268 const auto putBack = [&](QClipboard::Mode mode,
269 const std::optional<QString> &previous) {
270 const QString now = clipboard->text(mode);
274 if (restore && previous && !previous->isEmpty() && now ==
m_clippedText) {
275 clipboard->setText(*previous, mode);
279 clipboard->clear(mode);
280 if (mode == QClipboard::Selection) {
281 clipboard->setText(QString(
""), QClipboard::Selection);
285 putBack(QClipboard::Selection, previousSelection);
286 putBack(QClipboard::Clipboard, previousClipboard);
288 : cleared ? tr(
"Clipboard cleared")
289 : tr(
"Clipboard not cleared"));
295 auto *mimeData =
new QMimeData();
296 mimeData->setText(text);
299#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
300 mimeData->setData(
"x-kde-passwordManagerHint", QByteArray(
"secret"));
303 mimeData->setData(
"application/x-nspasteboard-concealed-type", QByteArray());
306 mimeData->setData(
"ExcludeClipboardContentFromMonitorProcessing",
308 mimeData->setData(
"CanIncludeInClipboardHistory", dwordBytes(0));
309 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 makeRoomInOtherCopies()
Keep m_otherCopies below its cap before trackCopied() appends: drop the oldest text that is neither o...
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
void saveWhatWasThere(QClipboard *clip, QClipboard::Mode mode, const QString &text)
Save what mode holds before the first secret of an autoclear window, for clear() to put back; saved a...
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.