QtPass Developer documentation
QtPass test coverage
Current view: top level - src/firstrunwizard.h Coverage Total Hit
Test: QtPass Lines: 100.0 % 5 5
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 The first-run wizard and its four pages: programs, key, store and
       6              :  * summary.
       7              :  */
       8              : #ifndef SRC_FIRSTRUNWIZARD_H_
       9              : #define SRC_FIRSTRUNWIZARD_H_
      10              : 
      11              : #include "appsettings.h"
      12              : #include "userinfo.h"
      13              : #include <QList>
      14              : #include <QString>
      15              : #include <QWizard>
      16              : #include <QWizardPage>
      17              : 
      18              : class QCheckBox;
      19              : class QLabel;
      20              : class QLineEdit;
      21              : class QListWidget;
      22              : class QPushButton;
      23              : 
      24              : /**
      25              :  * @class FirstRunWizard
      26              :  * @ingroup ui
      27              :  * @brief The first start of QtPass: find GnuPG (and pass and Git), make sure
      28              :  * there is a secret key, pick or create the password store. Replaces the
      29              :  * chain of message boxes ConfigDialog::wizard() used to run before showing
      30              :  * the whole configuration dialog.
      31              :  *
      32              :  * The wizard works on a copy of the settings and writes it back on Finish,
      33              :  * after it has created and initialised the store when that was needed
      34              :  * (ProfileInit). Cancel leaves the settings as they were; the caller keeps
      35              :  * asking until the configuration is usable or the user gives up.
      36              :  */
      37              : class FirstRunWizard : public QWizard {
      38          232 :   Q_OBJECT
      39              : 
      40              : public:
      41              :   /**
      42              :    * @brief Build the wizard from the current settings.
      43              :    * @param parent The main window.
      44              :    */
      45              :   explicit FirstRunWizard(QWidget *parent = nullptr);
      46              : 
      47              :   /**
      48              :    * @brief The settings as the pages have edited them so far.
      49              :    * @return A copy.
      50              :    */
      51              :   auto settings() const -> AppSettings { return m_settings; }
      52              : 
      53              :   /**
      54              :    * @brief The secret keys gpg lists for @p gpgExecutable.
      55              :    *
      56              :    * gpg exits with 2 after a complete listing when one key holds something
      57              :    * it cannot handle (a subkey of an unknown algorithm, say): the keys it
      58              :    * listed are taken, and what it reported goes to @p problem. Any other
      59              :    * failure lists no keys.
      60              :    * @param gpgExecutable The gpg binary.
      61              :    * @param problem Receives gpg's error output when it did not exit
      62              :    *        cleanly, cut to a few hundred characters and made displayable
      63              :    *        (Util::displayable()); null to not care.
      64              :    * @return The keys, or an empty list when gpg cannot run or failed.
      65              :    */
      66              :   static auto secretKeys(const QString &gpgExecutable,
      67              :                          QString *problem = nullptr) -> QList<UserInfo>;
      68              : 
      69              :   /**
      70              :    * @brief Whether @p path points at an initialised store.
      71              :    * @param path A directory.
      72              :    * @return true when it holds a `.gpg-id`.
      73              :    */
      74              :   static auto isStore(const QString &path) -> bool;
      75              : 
      76              :   /// Page ids, in order.
      77              :   enum Page {
      78              :     IntroPage,    ///< What QtPass is and what the wizard will do.
      79              :     ProgramsPage, ///< ProgramsWizardPage: where gpg, pass and git are.
      80              :     KeyPage,      ///< KeyWizardPage: the secret keys, with generation.
      81              :     StorePage,    ///< StoreWizardPage: the store folder and the Git choice.
      82              :     DonePage      ///< DoneWizardPage: summary and two preferences.
      83              :   };
      84              : 
      85              :   /**
      86              :    * @brief Write the settings and, when the chosen store has no `.gpg-id`
      87              :    * yet, create and initialise it for the chosen keys first.
      88              :    */
      89              :   void accept() override;
      90              : 
      91              : private:
      92              :   /**
      93              :    * @brief Make @p store a password store for the ticked keys: create the
      94              :    * folder when it is missing, write and sign its `.gpg-id`, and set up Git
      95              :    * when chosen. On failure what was written is taken back, so a second
      96              :    * Finish runs the whole initialisation again.
      97              :    * @param store The folder, cleaned.
      98              :    * @return Whether the store is ready (the user was told otherwise).
      99              :    */
     100              :   auto initialiseStore(const QString &store) -> bool;
     101              :   /**
     102              :    * @brief Give an existing store the Git repository it was chosen to have.
     103              :    * @param store The folder, cleaned.
     104              :    * @return Whether the repository is ready (the user was told otherwise).
     105              :    */
     106              :   auto initialiseGit(const QString &store) -> bool;
     107              : 
     108              :   friend class ProgramsWizardPage;
     109              :   friend class KeyWizardPage;
     110              :   friend class StoreWizardPage;
     111              :   friend class DoneWizardPage;
     112              : 
     113              :   /// The pages' working copy of the settings, loaded in the constructor and
     114              :   /// saved by accept(); the pages edit it through their friend access.
     115              :   AppSettings m_settings;
     116              :   /// Whether nothing was chosen yet (SettingsConstants::firstRunPending):
     117              :   /// only then are "Use pass" and "Use Git" turned on from what is
     118              :   /// detected. The wizard also runs again for a store that lost its
     119              :   /// recipient list, and then shows the choices saved before.
     120              :   bool m_detectChoices = false;
     121              :   /// The secret keys as listed on the key page; the enabled ones become the
     122              :   /// recipients of a new store.
     123              :   QList<UserInfo> m_keys;
     124              : };
     125              : 
     126              : /**
     127              :  * @class ProgramsWizardPage
     128              :  * @ingroup ui
     129              :  * @brief Where gpg, pass and git are; gpg is required, the rest optional. A
     130              :  * stored path that no longer runs is replaced by what PATH has.
     131              :  */
     132              : class ProgramsWizardPage : public QWizardPage {
     133          229 :   Q_OBJECT
     134              : 
     135              : public:
     136              :   /**
     137              :    * @brief Build the page.
     138              :    * @param wizard Owner.
     139              :    */
     140              :   explicit ProgramsWizardPage(FirstRunWizard *wizard);
     141              :   /**
     142              :    * @brief Fill the fields from the settings (autodetection already ran).
     143              :    */
     144              :   void initializePage() override;
     145              :   /**
     146              :    * @brief Whether the gpg path points at something that can run.
     147              :    * @return true when gpg exists (or is a `wsl` command).
     148              :    */
     149              :   auto isComplete() const -> bool override;
     150              :   /**
     151              :    * @brief Store the paths and the pass/Git choices in the settings.
     152              :    * @return Always true; isComplete() gates Next.
     153              :    */
     154              :   auto validatePage() -> bool override;
     155              : 
     156              : private:
     157              :   FirstRunWizard *m_wizard; ///< Owner; its settings are what the page edits.
     158              :   QLineEdit *m_gpg;         ///< The gpg executable; required.
     159              :   QLineEdit *m_git;         ///< The git executable; optional.
     160              :   QLineEdit *m_pass;        ///< The pass executable; optional.
     161              :   QCheckBox *m_usePass;     ///< Run pass instead of gpg/git; needs m_pass.
     162              :   QLabel *m_gpgStatus;      ///< Under the gpg field: why it cannot be used.
     163              : 
     164              :   /**
     165              :    * @brief After an edit: say under the gpg field when it is missing or not
     166              :    * executable, allow "use pass" only for a runnable pass (unticking it
     167              :    * otherwise), and re-evaluate isComplete().
     168              :    */
     169              :   void updateStatus();
     170              : };
     171              : 
     172              : /**
     173              :  * @class KeyWizardPage
     174              :  * @ingroup ui
     175              :  * @brief The secret keys gpg knows; a new pair can be generated here. The
     176              :  * enabled ones become the recipients of a store that still has to be
     177              :  * initialised.
     178              :  */
     179              : class KeyWizardPage : public QWizardPage {
     180          100 :   Q_OBJECT
     181              : 
     182              : public:
     183              :   /**
     184              :    * @brief Build the page.
     185              :    * @param wizard Owner.
     186              :    */
     187              :   explicit KeyWizardPage(FirstRunWizard *wizard);
     188              :   /**
     189              :    * @brief List the secret keys for the gpg chosen on the previous page.
     190              :    */
     191              :   void initializePage() override;
     192              :   /**
     193              :    * @brief Always passable: an existing store needs no recipient choice
     194              :    * here (and its key may not be imported yet); the store page insists on a
     195              :    * ticked key when it has to initialise.
     196              :    * @return true.
     197              :    */
     198              :   auto isComplete() const -> bool override;
     199              :   /**
     200              :    * @brief Remember which keys are enabled.
     201              :    * @return Always true.
     202              :    */
     203              :   auto validatePage() -> bool override;
     204              : 
     205              : private:
     206              :   FirstRunWizard *m_wizard; ///< Owner; holds the key list this page fills.
     207              :   QListWidget *m_list;      ///< One checkable row per secret key.
     208              :   QPushButton *m_generate;  ///< Opens KeygenDialog.
     209              :   QLabel *m_status;         ///< Says so when gpg has no secret key yet.
     210              : 
     211              :   /**
     212              :    * @brief List the secret keys of the chosen gpg into the wizard and the
     213              :    * widget, ticking every usable key and greying out expired or invalid
     214              :    * ones.
     215              :    */
     216              :   void reload();
     217              :   /**
     218              :    * @brief Run KeygenDialog on a private ImitatePass built from the wizard's
     219              :    * settings, so the chosen gpg is used and nothing is saved, then reload().
     220              :    */
     221              :   void generate();
     222              : };
     223              : 
     224              : /**
     225              :  * @class StoreWizardPage
     226              :  * @ingroup ui
     227              :  * @brief The folder the passwords live in: an existing store is used as-is,
     228              :  * a folder without `.gpg-id` (or no folder) is initialised on Finish. The
     229              :  * Git box lives here because its sensible default depends on the folder: on
     230              :  * for a repository or a store still to be created, off for an existing
     231              :  * store that is no repository.
     232              :  */
     233              : class StoreWizardPage : public QWizardPage {
     234          197 :   Q_OBJECT
     235              : 
     236              : public:
     237              :   /**
     238              :    * @brief Build the page.
     239              :    * @param wizard Owner.
     240              :    */
     241              :   explicit StoreWizardPage(FirstRunWizard *wizard);
     242              :   /**
     243              :    * @brief Show the configured store path.
     244              :    */
     245              :   void initializePage() override;
     246              :   /**
     247              :    * @brief Whether a path is given and, for a folder that is not a store
     248              :    * yet, at least one key was ticked to encrypt it to.
     249              :    * @return true when Finish can do its job.
     250              :    */
     251              :   auto isComplete() const -> bool override;
     252              :   /**
     253              :    * @brief Store the path and the Git choice.
     254              :    * @return Always true.
     255              :    */
     256              :   auto validatePage() -> bool override;
     257              : 
     258              : private:
     259              :   FirstRunWizard *m_wizard; ///< Owner; its settings get the path and flag.
     260              :   QLineEdit *m_path;        ///< The store folder, in native separators.
     261              :   QCheckBox *m_useGit;      ///< Keep the store under Git; off without git.
     262              :   QLabel *m_status;         ///< What Finish will do with the folder.
     263              : 
     264              :   /**
     265              :    * @brief How many keys are ticked on the key page.
     266              :    * @return The number of enabled entries in the wizard's key list.
     267              :    */
     268              :   auto recipients() const -> int;
     269              :   /**
     270              :    * @brief After an edit: describe the folder (existing store with its
     271              :    * entry count, empty folder, not a store yet, missing) and ask for a
     272              :    * ticked key when it has to be initialised, then re-evaluate isComplete().
     273              :    */
     274              :   void updateStatus();
     275              : };
     276              : 
     277              : /**
     278              :  * @class DoneWizardPage
     279              :  * @ingroup ui
     280              :  * @brief Last page: a summary of what Finish will do and the two
     281              :  * preferences worth asking on day one.
     282              :  *
     283              :  * The summary names the store and says whether it is used as it is or set
     284              :  * up for the ticked keys, with or without Git. The two checkboxes are
     285              :  * "hide passwords until asked" and "show an icon in the system tray";
     286              :  * everything else is left to the settings dialog.
     287              :  */
     288              : class DoneWizardPage : public QWizardPage {
     289          212 :   Q_OBJECT
     290              : 
     291              : public:
     292              :   /**
     293              :    * @brief Build the page.
     294              :    * @param wizard Owner.
     295              :    */
     296              :   explicit DoneWizardPage(FirstRunWizard *wizard);
     297              :   /**
     298              :    * @brief Fill the summary from the settings.
     299              :    */
     300              :   void initializePage() override;
     301              :   /**
     302              :    * @brief Store the preferences.
     303              :    * @return Always true.
     304              :    */
     305              :   auto validatePage() -> bool override;
     306              : 
     307              : private:
     308              :   FirstRunWizard *m_wizard;  ///< Owner; its settings get the preferences.
     309              :   QLabel *m_summary;         ///< Store, what Finish does with it, gpg, mode.
     310              :   QCheckBox *m_hidePassword; ///< Show the password line as dots; on.
     311              :   QCheckBox *m_trayIcon;     ///< Tray icon; hidden when there is no tray.
     312              : };
     313              : 
     314              : #endif // SRC_FIRSTRUNWIZARD_H_
        

Generated by: LCOV version 2.0-1