QtPass Developer documentation
QtPass test coverage
Current view: top level - src/qtpass.h Coverage Total Hit
Test: QtPass Lines: 100.0 % 2 2
Test Date: 2026-11-14 13:19:18 Functions: - 0 0

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2018 Anne Jan Brouwer
       2              : // SPDX-License-Identifier: GPL-3.0-or-later
       3              : /**
       4              :  * @file
       5              :  * @brief QtPass: UI-neutral glue that turns backend completion signals into
       6              :  *        formatted output, status messages and clipboard handling.
       7              :  */
       8              : #ifndef SRC_QTPASS_H_
       9              : #define SRC_QTPASS_H_
      10              : 
      11              : #include "clipboardmanager.h"
      12              : #include <QDialog>
      13              : #include <QObject>
      14              : #include <QPixmap>
      15              : 
      16              : class Pass;
      17              : 
      18              : /**
      19              :  * @class QtPass
      20              :  * @ingroup support
      21              :  * @brief Application-level glue between the pass backends and whatever shows
      22              :  * their results: turns the backends' completion signals into UI-neutral
      23              :  * signals (formatted output, status messages, "the operation is over"), owns
      24              :  * the ClipboardManager, migrates the settings of older versions on start-up
      25              :  * and renders QR codes. It knows nothing about the main window; the window
      26              :  * connects to the signals below.
      27              :  */
      28              : class QtPass : public QObject {
      29            5 :   Q_OBJECT
      30              : 
      31              : public:
      32              :   /**
      33              :    * @brief Construct the glue and connect it to both pass backends.
      34              :    * @param parent Owner.
      35              :    */
      36              :   explicit QtPass(QObject *parent = nullptr);
      37              :   /**
      38              :    * @brief Destroy the QtPass instance and clean up signal connections.
      39              :    */
      40              :   ~QtPass() override;
      41              : 
      42              :   /**
      43              :    * @brief Resolve the password store and the executables and migrate the
      44              :    * settings of an older version (fresh-install defaults, the native-OTP
      45              :    * switch). Whether the resulting configuration is usable is the caller's
      46              :    * question: see Util::configIsValid().
      47              :    */
      48              :   void init();
      49              : 
      50              :   /**
      51              :    * @brief The clipboard handling for this application instance.
      52              :    * @return The manager; lives as long as this object.
      53              :    */
      54          565 :   auto clipboard() -> ClipboardManager & { return m_clipboard; }
      55              : 
      56              :   /**
      57              :    * @brief Turn a process' plain-text output into the HTML the text browser
      58              :    * shows: http(s) URLs become links, everything else is escaped, line breaks
      59              :    * become `<br />`.
      60              :    * @param output Plain text from a process.
      61              :    * @param prefix HTML placed before the converted output.
      62              :    * @param postfix HTML placed after it.
      63              :    * @return The HTML.
      64              :    */
      65              :   static auto formatOutput(QString output, const QString &prefix = QString(),
      66              :                            const QString &postfix = QString()) -> QString;
      67              : 
      68              :   /**
      69              :    * @brief Create a frameless popup dialog, placed at the cursor, showing the
      70              :    *        given QR code. Split out of showTextAsQRCode() so tests can inspect
      71              :    *        the dialog without running qrencode.
      72              :    * @param image Pixmap containing the rendered QR code.
      73              :    * @return Pointer to a QDialog showing the image; it deletes itself on
      74              :    *         close, the caller shows it.
      75              :    */
      76              :   static QDialog *createQRCodePopup(const QPixmap &image);
      77              : 
      78              : public slots:
      79              :   /**
      80              :    * @brief Render text as a QR code with qrencode and show it in a popup at
      81              :    * the cursor; failures are reported through statusMessage().
      82              :    * @param text Text to convert into a QR code.
      83              :    */
      84              :   void showTextAsQRCode(const QString &text);
      85              : 
      86              : signals:
      87              :   /**
      88              :    * @brief Formatted output (see formatOutput()) or an error rendered in
      89              :    * colour, ready for the text browser.
      90              :    * @param html The HTML to show.
      91              :    */
      92              :   void outputReady(const QString &html);
      93              :   /**
      94              :    * @brief A backend operation is over, successfully or not; the interface
      95              :    * can be enabled again.
      96              :    */
      97              :   void operationFinished();
      98              :   /**
      99              :    * @brief A short message for the status bar.
     100              :    * @param message The text.
     101              :    * @param timeout Milliseconds to keep it, as QStatusBar::showMessage().
     102              :    */
     103              :   void statusMessage(const QString &message, int timeout);
     104              :   /**
     105              :    * @brief The store changed and the settings ask for an automatic push.
     106              :    */
     107              :   void pushRequested();
     108              :   /**
     109              :    * @brief An entry was written; the view of the current entry is stale.
     110              :    */
     111              :   void entryInserted();
     112              : 
     113              : private:
     114              :   /**
     115              :    * @brief The clipboard handling for this application instance, exposed
     116              :    *        through clipboard(); parented to this object.
     117              :    */
     118              :   ClipboardManager m_clipboard;
     119              : 
     120              :   /**
     121              :    * @brief Connect the completion signals of one backend to the slots that
     122              :    *        turn them into output, status and "finished" signals. Both
     123              :    *        backends are wired up at construction because the active one can
     124              :    *        change through the configuration dialog.
     125              :    * @param pass The backend to listen to.
     126              :    */
     127              :   void connectPassSignalHandlers(Pass *pass);
     128              : 
     129              :   /**
     130              :    * @brief Show a process' stderr in colour through outputReady(): red for a
     131              :    *        failure, dark grey for the chatter a successful command prints
     132              :    *        (see issue #111). Nothing is shown for empty output.
     133              :    * @param exitCode The process exit code; 0 selects grey.
     134              :    * @param error The stderr text.
     135              :    */
     136              :   void reportError(int exitCode, const QString &error);
     137              : 
     138              : private slots:
     139              :   /**
     140              :    * @brief Slot for Pass::processErrorExit: report the error in red and emit
     141              :    *        operationFinished().
     142              :    * @param exitCode The exit code of the failed process.
     143              :    * @param error Its stderr.
     144              :    */
     145              :   void processErrorExit(int exitCode, const QString &error);
     146              : 
     147              :   /**
     148              :    * @brief Slot for Pass::finishedGitPull / finishedGitPush and the common
     149              :    *        tail of the other slots: emit the stdout as formatted output (if
     150              :    *        any), the stderr as grey chatter, then operationFinished().
     151              :    * @param output stdout of the process.
     152              :    * @param errout stderr of the process.
     153              :    */
     154              :   void processFinished(const QString &output, const QString &errout);
     155              : 
     156              :   /**
     157              :    * @brief Slot for the backend signals that change the store on disk (git
     158              :    *        init, remove, init, move, copy): processFinished() followed by
     159              :    *        doGitPush().
     160              :    * @param output stdout of the process.
     161              :    * @param errout stderr of the process.
     162              :    */
     163              :   void passStoreChanged(const QString &output, const QString &errout);
     164              : 
     165              :   /** @brief Emit pushRequested() when the settings ask for automatic push. */
     166              :   void doGitPush();
     167              : 
     168              :   /**
     169              :    * @brief Slot for Pass::finishedInsert: like passStoreChanged(), then emit
     170              :    *        entryInserted() so the window refreshes the shown entry.
     171              :    * @param output stdout of the process.
     172              :    * @param errout stderr of the process.
     173              :    */
     174              :   void finishedInsert(const QString &output, const QString &errout);
     175              : 
     176              :   /**
     177              :    * @brief Slot for Pass::finishedGenerateGPGKeys: a status message followed
     178              :    *        by processFinished(). The KeygenDialog listens to the same signal
     179              :    *        and closes itself.
     180              :    * @param output stdout of the key generation process.
     181              :    * @param errout stderr of the key generation process.
     182              :    */
     183              :   void onKeyGenerationComplete(const QString &output, const QString &errout);
     184              : };
     185              : 
     186              : #endif // SRC_QTPASS_H_
        

Generated by: LCOV version 2.0-1