QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
clipboardmanager.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#ifndef SRC_CLIPBOARDMANAGER_H_
9#define SRC_CLIPBOARDMANAGER_H_
10
11#include <QClipboard>
12#include <QMimeData>
13#include <QObject>
14#include <QString>
15#include <QStringList>
16#include <QTimer>
17#include <optional>
18
25auto buildClipboardMimeData(const QString &text) -> QMimeData *;
26
32static inline auto dwordBytes(quint32 value) -> QByteArray {
33 return {reinterpret_cast<const char *>(&value), sizeof(value)};
34}
35
48class ClipboardManager : public QObject {
49 Q_OBJECT
50
51public:
57 explicit ClipboardManager(QObject *parent = nullptr);
58
62 void setAutoclearTimer();
63
74 void copyIfAlways(const QString &text, const QString &p_output = QString());
75
80 [[nodiscard]] auto trackedText() const -> QString { return m_clippedText; }
81
82public slots:
88 void copyText(const QString &text);
89
100 void trackCopied(const QString &text);
101
106 void clear();
107
108signals:
113 void statusMessage(const QString &message);
114
115private:
129 std::optional<QString> m_previousClipboard;
131 std::optional<QString> m_previousSelection;
137 void forgetIfSaved(const QString &text);
146 void saveWhatWasThere(QClipboard *clip, QClipboard::Mode mode,
147 const QString &text);
154 QStringList m_otherCopies;
155};
156
157#endif // SRC_CLIPBOARDMANAGER_H_
auto trackedText() const -> QString
The text this manager last put on the clipboard and still tracks.
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...
auto buildClipboardMimeData(const QString &text) -> QMimeData *
Build clipboard MIME data with platform-specific security hints.
static auto dwordBytes(quint32 value) -> QByteArray
Convert quint32 to byte array for Windows clipboard formats.