QtPass Developer documentation
QtPass test coverage
Current view: top level - src/clipboardmanager.h Coverage Total Hit
Test: QtPass Lines: 100.0 % 1 1
Test Date: 2026-12-03 23:34:20 Functions: - 0 0

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
       2              : // SPDX-License-Identifier: GPL-3.0-or-later
       3              : /**
       4              :  * @file
       5              :  * @brief ClipboardManager, which puts secrets on the clipboard and removes
       6              :  * them again, and the MIME data that marks them as secrets.
       7              :  */
       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              : 
      19              : /**
      20              :  * @brief Build clipboard MIME data with platform-specific security hints.
      21              :  * @param text - Plain text to copy
      22              :  * @return QMimeData* - Ownership transferred to caller. Caller must delete
      23              :  *         or transfer to QClipboard::setMimeData which takes ownership.
      24              :  */
      25              : auto buildClipboardMimeData(const QString &text) -> QMimeData *;
      26              : 
      27              : /**
      28              :  * @brief Convert quint32 to byte array for Windows clipboard formats.
      29              :  * @param value - DWORD value
      30              :  * @return QByteArray with raw bytes
      31              :  */
      32              : static inline auto dwordBytes(quint32 value) -> QByteArray {
      33              :   return {reinterpret_cast<const char *>(&value), sizeof(value)};
      34              : }
      35              : 
      36              : /**
      37              :  * @class ClipboardManager
      38              :  * @ingroup secrets
      39              :  * @brief Puts secrets on the system clipboard and takes them off again.
      40              :  *
      41              :  * Remembers what it copied so the autoclear timer (and the quit handler)
      42              :  * only remove that text, never something the user copied in the meantime.
      43              :  * Settings (selection vs. clipboard, autoclear on/off, delay) are read from
      44              :  * QtPassSettings; call setAutoclearTimer() after they change. Status text
      45              :  * for the main window's status bar is emitted, not shown, so this class has
      46              :  * no UI dependency and is unit-testable.
      47              :  */
      48              : class ClipboardManager : public QObject {
      49           78 :   Q_OBJECT
      50              : 
      51              : public:
      52              :   /**
      53              :    * @brief Construct a manager that also clears its text when the
      54              :    *        application quits.
      55              :    * @param parent Owner.
      56              :    */
      57              :   explicit ClipboardManager(QObject *parent = nullptr);
      58              : 
      59              :   /**
      60              :    * @brief Re-read the autoclear delay from the settings.
      61              :    */
      62              :   void setAutoclearTimer();
      63              : 
      64              :   /**
      65              :    * @brief Handle clipboard copying for a shown entry.
      66              :    *
      67              :    * In "always copy" mode (and only then) this copies @p text via copyText(),
      68              :    * which also arms the autoclear timer and records the clipboard contents.
      69              :    * In other modes it does nothing, leaving any pending autoclear tracking
      70              :    * intact.
      71              :    * @param text Password (or value) of the entry being shown.
      72              :    * @param p_output Full decrypted output; copying is skipped when it is empty.
      73              :    */
      74              :   void copyIfAlways(const QString &text, const QString &p_output = QString());
      75              : 
      76              :   /**
      77              :    * @brief The text this manager last put on the clipboard and still tracks.
      78              :    * @return Tracked text, empty once cleared.
      79              :    */
      80              :   [[nodiscard]] auto trackedText() const -> QString { return m_clippedText; }
      81              : 
      82              : public slots:
      83              :   /**
      84              :    * @brief Copy text into the system clipboard (or the X11 primary selection
      85              :    *        when configured) and arm the autoclear timer when enabled.
      86              :    * @param text Text to copy.
      87              :    */
      88              :   void copyText(const QString &text);
      89              : 
      90              :   /**
      91              :    * @brief Track text that got onto the clipboard some other way.
      92              :    *
      93              :    * For a copy QtPass did not make through copyText(): text selected and
      94              :    * copied from a widget showing a secret (Ctrl+C, the context menu, a drag,
      95              :    * or the X11 primary selection that a mouse selection fills). The text is
      96              :    * cleared with the rest when the autoclear timer runs; what copyText()
      97              :    * tracks stays tracked too.
      98              :    * @param text The copied text; nothing happens when it is empty.
      99              :    */
     100              :   void trackCopied(const QString &text);
     101              : 
     102              :   /**
     103              :    * @brief Remove the tracked text from the clipboard if it is still there.
     104              :    *        Silent no-op when nothing is tracked.
     105              :    */
     106              :   void clear();
     107              : 
     108              : signals:
     109              :   /**
     110              :    * @brief Something worth showing in the status bar happened.
     111              :    * @param message Translated text.
     112              :    */
     113              :   void statusMessage(const QString &message);
     114              : 
     115              : private:
     116              :   /**
     117              :    * @brief Single-shot autoclear timer; started by copyText() when the
     118              :    * autoclear setting is on, and calls clear() when it fires.
     119              :    */
     120              :   QTimer m_clearTimer;
     121              :   /**
     122              :    * @brief The secret copyText() last put on the clipboard; clear() removes
     123              :    * the clipboard content only while it still equals this.
     124              :    */
     125              :   QString m_clippedText;
     126              :   /// What was on the clipboard and in the selection before copyText() first
     127              :   /// put a secret there, for clear() to put back (restoreClipboard). Text
     128              :   /// only, as pass -c keeps it; absent when nothing was saved.
     129              :   std::optional<QString> m_previousClipboard;
     130              :   /** @brief As m_previousClipboard, for the X11 primary selection. */
     131              :   std::optional<QString> m_previousSelection;
     132              :   /**
     133              :    * @brief Empty a saved previous text equal to @p text, now copied as a
     134              :    * secret, so clear() does not put the secret back.
     135              :    * @param text The text that has just been copied as a secret.
     136              :    */
     137              :   void forgetIfSaved(const QString &text);
     138              :   /**
     139              :    * @brief Save what @p mode holds before the first secret of an autoclear
     140              :    * window, for clear() to put back; saved as nothing when it is a secret
     141              :    * itself (ours, @p text, or marked by another password manager).
     142              :    * @param clip The application clipboard.
     143              :    * @param mode The buffer about to be written.
     144              :    * @param text The secret about to be copied.
     145              :    */
     146              :   void saveWhatWasThere(QClipboard *clip, QClipboard::Mode mode,
     147              :                         const QString &text);
     148              :   /**
     149              :    * @brief Keep m_otherCopies below its cap before trackCopied() appends:
     150              :    * drop the oldest text that is neither on the clipboard nor in the
     151              :    * selection.
     152              :    */
     153              :   void makeRoomInOtherCopies();
     154              :   QStringList m_otherCopies; ///< trackCopied() texts, cleared with the rest
     155              : };
     156              : 
     157              : #endif // SRC_CLIPBOARDMANAGER_H_
        

Generated by: LCOV version 2.0-1