QtPass Developer documentation
QtPass test coverage
Current view: top level - src/firstrunwizard.cpp (source / functions) Coverage Total Hit
Test: QtPass Lines: 99.7 % 337 336
Test Date: 2026-12-03 23:34:20 Functions: 100.0 % 32 32
Branches: 88.0 % 208 183

             Branch data     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 Implements FirstRunWizard and its pages; the store is created and
       6                 :             :  * initialised in FirstRunWizard::initialiseStore() on Finish.
       7                 :             :  */
       8                 :             : #include "firstrunwizard.h"
       9                 :             : #include "executor.h"
      10                 :             : #include "gpgkeystate.h"
      11                 :             : #include "imitatepass.h"
      12                 :             : #include "keygendialog.h"
      13                 :             : #include "passbackendfactory.h"
      14                 :             : #include "plainmessagebox.h"
      15                 :             : #include "profileinit.h"
      16                 :             : #include "qtpasssettings.h"
      17                 :             : #include "settingsconstants.h"
      18                 :             : #include "util.h"
      19                 :             : #include <QCheckBox>
      20                 :             : #include <QDateTime>
      21                 :             : #include <QDir>
      22                 :             : #include <QFile>
      23                 :             : #include <QFileDialog>
      24                 :             : #include <QFileInfo>
      25                 :             : #include <QFormLayout>
      26                 :             : #include <QHBoxLayout>
      27                 :             : #include <QLabel>
      28                 :             : #include <QLineEdit>
      29                 :             : #include <QListWidget>
      30                 :             : #include <QMessageBox>
      31                 :             : #include <QPushButton>
      32                 :             : #include <QSystemTrayIcon>
      33                 :             : #include <QToolButton>
      34                 :             : #include <QVBoxLayout>
      35                 :             : #include <functional>
      36                 :             : #ifdef Q_OS_WIN
      37                 :             : #include <windows.h>
      38                 :             : #endif
      39                 :             : 
      40                 :             : namespace {
      41                 :             : 
      42                 :             : /// A line edit with a "…" button that opens a file or folder picker.
      43                 :         128 : auto pathRow(QLineEdit *edit, QWidget *parent,
      44                 :             :              const std::function<QString()> &pick) -> QWidget * {
      45                 :         128 :   auto *row = new QWidget(parent);
      46                 :         128 :   auto *layout = new QHBoxLayout(row);
      47                 :         128 :   layout->setContentsMargins(0, 0, 0, 0);
      48                 :         128 :   layout->addWidget(edit);
      49                 :         128 :   auto *browse = new QToolButton(row);
      50                 :         256 :   browse->setText(QStringLiteral("…"));
      51                 :         128 :   browse->setToolTip(FirstRunWizard::tr("Browse"));
      52                 :         128 :   layout->addWidget(browse);
      53         [ +  + ]:         256 :   QObject::connect(browse, &QToolButton::clicked, edit, [edit, pick]() {
      54                 :           3 :     const QString chosen = pick();
      55         [ +  + ]:           3 :     if (!chosen.isEmpty()) {
      56                 :           2 :       edit->setText(chosen);
      57                 :           2 :       emit edit->textEdited(chosen);
      58                 :             :     }
      59                 :           3 :   });
      60                 :         128 :   return row;
      61                 :             : }
      62                 :             : 
      63                 :         269 : auto runnable(const QString &exe) -> bool {
      64   [ +  +  -  +  :         269 :   return Executor::parseWslCommand(exe).has_value() ||
                      + ]
      65   [ +  +  -  - ]:         269 :          (!exe.isEmpty() && QFileInfo(exe).isExecutable());
      66                 :             : }
      67                 :             : 
      68                 :             : } // namespace
      69                 :             : 
      70                 :             : // --------------------------------------------------------------------------
      71                 :             : 
      72                 :          32 : FirstRunWizard::FirstRunWizard(QWidget *parent)
      73                 :          32 :     : QWizard(parent), m_settings(QtPassSettings::load()),
      74                 :          32 :       m_detectChoices(QtPassSettings::getInstance()
      75                 :          64 :                           ->value(SettingsConstants::firstRunPending, false)
      76         [ +  + ]:          64 :                           .toBool()) {
      77                 :          32 :   setWindowTitle(tr("Welcome to QtPass"));
      78                 :          32 :   setWizardStyle(QWizard::ModernStyle);
      79                 :          32 :   setOption(QWizard::NoBackButtonOnStartPage);
      80                 :          32 :   setOption(QWizard::HaveHelpButton, false);
      81                 :             : 
      82                 :          32 :   auto *intro = new QWizardPage(this);
      83                 :          32 :   intro->setTitle(tr("Welcome to QtPass"));
      84                 :          32 :   auto *introLayout = new QVBoxLayout(intro);
      85                 :             :   auto *introText = new QLabel(
      86                 :          32 :       tr("QtPass is a graphical front-end for <i>pass</i>, the standard Unix "
      87                 :             :          "password manager: every password is a file encrypted with GnuPG, "
      88                 :             :          "kept in a folder you can put under Git.<br><br>The next pages find "
      89                 :             :          "GnuPG, make sure you have a key to encrypt to and pick the folder "
      90                 :             :          "your passwords live in. Nothing is written until you press "
      91                 :             :          "Finish."),
      92      [ +  +  + ]:          32 :       intro);
      93                 :          32 :   introText->setWordWrap(true);
      94                 :          32 :   introText->setTextFormat(Qt::RichText);
      95                 :          32 :   introLayout->addWidget(introText);
      96                 :          32 :   setPage(IntroPage, intro);
      97                 :          32 :   setPage(ProgramsPage, new ProgramsWizardPage(this));
      98                 :          32 :   setPage(KeyPage, new KeyWizardPage(this));
      99                 :          32 :   setPage(StorePage, new StoreWizardPage(this));
     100                 :          32 :   setPage(DonePage, new DoneWizardPage(this));
     101                 :          32 :   setStartId(IntroPage);
     102                 :             :   resize(640, 480);
     103                 :          32 : }
     104                 :             : 
     105                 :          27 : auto FirstRunWizard::secretKeys(const QString &gpgExecutable, QString *problem)
     106                 :             :     -> QList<UserInfo> {
     107         [ +  + ]:          27 :   if (!runnable(gpgExecutable)) {
     108                 :           2 :     return {};
     109                 :             :   }
     110                 :          25 :   QString out;
     111                 :          25 :   QString err;
     112                 :         150 :   const int rc = Executor::executeBlocking(
     113                 :             :       gpgExecutable,
     114                 :          25 :       {QStringLiteral("--no-tty"), QStringLiteral("--with-colons"),
     115                 :          25 :        QStringLiteral("--with-fingerprint"),
     116                 :          25 :        QStringLiteral("--list-secret-keys")},
     117                 :             :       &out, &err);
     118         [ +  + ]:          25 :   if (rc != 0 && problem != nullptr) {
     119                 :             :     constexpr qsizetype kMaxProblem = 400;
     120                 :             :     // It may quote a user ID someone else chose: no bidi overrides or other
     121                 :             :     // controls that change how the text around them reads.
     122                 :           8 :     *problem = Util::displayable(err.trimmed());
     123         [ -  + ]:           4 :     if (problem->size() > kMaxProblem) {
     124                 :           0 :       *problem = problem->left(kMaxProblem) + QChar(0x2026);
     125                 :             :     }
     126                 :             :   }
     127                 :             :   // Exit 2 is gpg's "an error occurred", also after a complete listing in
     128                 :             :   // which one key could not be handled: setup must not stall on that key.
     129         [ +  + ]:          25 :   if (rc != 0 && rc != 2) {
     130                 :           2 :     return {};
     131                 :             :   }
     132                 :          23 :   return parseGpgColonOutput(out, true);
     133                 :             : }
     134                 :             : 
     135                 :         206 : auto FirstRunWizard::isStore(const QString &path) -> bool {
     136         [ +  - ]:         206 :   return !path.isEmpty() &&
     137                 :         618 :          QFile::exists(QDir(path).filePath(QStringLiteral(".gpg-id")));
     138                 :             : }
     139                 :             : 
     140                 :           6 : auto FirstRunWizard::initialiseStore(const QString &store) -> bool {
     141                 :           6 :   QList<UserInfo> recipients;
     142         [ +  + ]:          12 :   for (const UserInfo &key : std::as_const(m_keys)) {
     143         [ +  - ]:           6 :     if (key.enabled) {
     144                 :             :       recipients.append(key);
     145                 :             :     }
     146                 :             :   }
     147         [ +  + ]:           6 :   const bool created = !QDir(store).exists();
     148   [ +  +  +  +  :           6 :   if (created && !Util::mkpathForStore(store)) {
                      + ]
     149                 :           1 :     PlainMessageBox::warning(
     150                 :           1 :         this, tr("Error"),
     151                 :           2 :         tr("Failed to create password-store at: %1").arg(store));
     152                 :           1 :     return false;
     153                 :             :   }
     154                 :             : #ifdef Q_OS_WIN
     155                 :             :   // Only a folder made here is hidden, like ~/.password-store on Unix; a
     156                 :             :   // folder the user picked keeps its attributes.
     157                 :             :   if (created) {
     158                 :             :     SetFileAttributes(store.toStdWString().c_str(), FILE_ATTRIBUTE_HIDDEN);
     159                 :             :   }
     160                 :             : #endif
     161                 :           5 :   QString note;
     162      [ +  +  + ]:           5 :   if (!ProfileInit::initialise(store, recipients, m_settings, m_settings.useGit,
     163                 :             :                                &note)) {
     164                 :             :     // ProfileInit writes .gpg-id before it signs and commits; take it back so
     165                 :             :     // a second Finish runs the whole initialisation again instead of finding
     166                 :             :     // a store that looks finished.
     167                 :           4 :     QFile::remove(QDir(store).filePath(QStringLiteral(".gpg-id")));
     168                 :           4 :     QFile::remove(QDir(store).filePath(QStringLiteral(".gpg-id.sig")));
     169                 :           2 :     PlainMessageBox::warning(this, tr("Password store not initialised"), note);
     170                 :           2 :     return false;
     171                 :             :   }
     172         [ +  + ]:           3 :   if (!note.isEmpty()) {
     173                 :             :     // ProfileInit's own note talks of switching profiles; on the first run
     174                 :             :     // the folder simply becomes the store.
     175                 :           2 :     PlainMessageBox::show(
     176                 :           1 :         this, QMessageBox::Information, tr("Password store"),
     177                 :           1 :         tr("%1 already contains encrypted files; they were not "
     178                 :             :            "re-encrypted to the ticked keys. Open Users after the start "
     179                 :             :            "to do that.")
     180         [ +  + ]:           2 :             .arg(QDir::toNativeSeparators(store)));
     181                 :             :   }
     182                 :             :   return true;
     183                 :             : }
     184                 :             : 
     185                 :           3 : auto FirstRunWizard::initialiseGit(const QString &store) -> bool {
     186                 :           3 :   QString note;
     187      [ +  +  + ]:           3 :   if (ProfileInit::initGit(store, m_settings, &note)) {
     188                 :             :     return true;
     189                 :             :   }
     190                 :             :   // Take the repository back too, so a second Finish retries the whole thing
     191                 :             :   // instead of skipping a .git that never got its first commit.
     192                 :           4 :   QDir(QDir(store).filePath(QStringLiteral(".git"))).removeRecursively();
     193                 :           2 :   PlainMessageBox::warning(this, tr("Password store not initialised"), note);
     194                 :           2 :   return false;
     195                 :             : }
     196                 :             : 
     197                 :          12 : void FirstRunWizard::accept() {
     198                 :             :   // The Finish button validates the page before calling this; a programmatic
     199                 :             :   // accept() gets the same treatment so the last page's choices are in.
     200   [ +  -  +  - ]:          12 :   if (currentPage() != nullptr && !currentPage()->validatePage()) {
     201                 :           5 :     return;
     202                 :             :   }
     203                 :          12 :   const QString store = QDir::cleanPath(m_settings.passStore);
     204                 :             :   // A profile's store opens as that profile, with its signing key: taken
     205                 :             :   // before a new store's list is written, so it is signed with that key.
     206                 :             :   // A refused or failed setup puts the previous choice back for a retry.
     207                 :             :   const QString wasActive = m_settings.activeProfile;
     208                 :             :   const QString wasKey = m_settings.passSigningKey;
     209                 :             :   const QString wasStore = m_settings.passStore;
     210                 :             :   // Matched as it is saved: the typed spelling can name another folder (a
     211                 :             :   // link followed by "..").
     212                 :          12 :   m_settings.passStore = Util::normalizeFolderPath(store);
     213                 :          12 :   QtPassSettings::takeProfileOfStore(m_settings, QtPassSettings::getProfiles());
     214                 :             :   const bool needsGit =
     215   [ +  +  +  +  :          16 :       m_settings.useGit && !QDir(store).exists(QStringLiteral(".git"));
          +  -  +  +  +  
             +  -  -  -  
                      - ]
     216   [ +  +  +  + ]:          12 :   const bool ready = !isStore(store) ? initialiseStore(store)
     217      [ +  +  + ]:           6 :                      : needsGit      ? initialiseGit(store)
     218                 :             :                                      : true;
     219         [ +  + ]:           9 :   if (!ready) {
     220                 :           5 :     m_settings.activeProfile = wasActive;
     221                 :           5 :     m_settings.passSigningKey = wasKey;
     222                 :           5 :     m_settings.passStore = wasStore;
     223                 :             :     return;
     224                 :             :   }
     225                 :           7 :   m_settings.passStore = Util::normalizeFolderPath(store);
     226                 :           7 :   QtPassSettings::save(m_settings);
     227                 :             :   // Chosen now: a later run (the store lost its list) keeps these choices.
     228                 :           7 :   QtPassSettings::getInstance()->remove(SettingsConstants::firstRunPending);
     229                 :             :   // The backend cached the old paths at construction.
     230                 :           7 :   PassBackendFactory::invalidate();
     231                 :           7 :   QWizard::accept();
     232                 :             : }
     233                 :             : 
     234                 :             : // --------------------------------------------------------------------------
     235                 :             : 
     236                 :          32 : ProgramsWizardPage::ProgramsWizardPage(FirstRunWizard *wizard)
     237                 :          32 :     : QWizardPage(wizard), m_wizard(wizard), m_gpg(new QLineEdit(this)),
     238                 :          32 :       m_git(new QLineEdit(this)), m_pass(new QLineEdit(this)),
     239                 :          64 :       m_usePass(new QCheckBox(tr("Use the pass command-line tool"), this)),
     240                 :          64 :       m_gpgStatus(new QLabel(this)) {
     241                 :          64 :   setTitle(tr("Programs"));
     242                 :          32 :   setSubTitle(tr("GnuPG does the encrypting. pass and Git are optional; "
     243                 :             :                  "QtPass can do their work itself."));
     244                 :          32 :   auto *layout = new QFormLayout(this);
     245                 :           2 :   auto pickExe = [this]() {
     246                 :           2 :     return QFileDialog::getOpenFileName(this, tr("Select executable"),
     247         [ +  + ]:           4 :                                         QString(), tr("All files (*)"));
     248                 :             :   };
     249                 :          96 :   layout->addRow(tr("GnuPG"), pathRow(m_gpg, this, pickExe));
     250                 :          32 :   m_gpgStatus->setWordWrap(true);
     251                 :          64 :   layout->addRow(QString(), m_gpgStatus);
     252                 :          96 :   layout->addRow(tr("Git"), pathRow(m_git, this, pickExe));
     253                 :          96 :   layout->addRow(tr("pass"), pathRow(m_pass, this, pickExe));
     254                 :          32 :   m_usePass->setToolTip(tr("Run the pass script for every operation instead "
     255                 :             :                            "of calling gpg and git directly"));
     256                 :          32 :   layout->addRow(QString(), m_usePass);
     257         [ +  + ]:         128 :   for (QLineEdit *edit : {m_gpg, m_git, m_pass}) {
     258                 :          96 :     connect(edit, &QLineEdit::textEdited, this,
     259                 :         192 :             &ProgramsWizardPage::updateStatus);
     260                 :             :   }
     261                 :          32 : }
     262                 :             : 
     263                 :          19 : void ProgramsWizardPage::initializePage() {
     264                 :          19 :   AppSettings &s = m_wizard->m_settings;
     265                 :             :   // QtPassSettings::initExecutables() fills empty paths from PATH only; a
     266                 :             :   // stored path whose binary has moved would be shown as an error here, so
     267                 :             :   // look it up again in that case.
     268                 :          76 :   auto detect = [](QString &stored, const QStringList &names) {
     269         [ +  + ]:          76 :     if (runnable(stored)) {
     270                 :             :       return;
     271                 :             :     }
     272         [ +  + ]:          98 :     for (const QString &name : names) {
     273                 :          51 :       const QString found = Util::findBinaryInPath(name);
     274         [ +  + ]:          51 :       if (!found.isEmpty()) {
     275                 :           1 :         stored = found;
     276                 :             :         return;
     277                 :             :       }
     278                 :             :     }
     279                 :             :   };
     280                 :          76 :   detect(s.gpgExecutable, {QStringLiteral("gpg2"), QStringLiteral("gpg")});
     281                 :          57 :   detect(s.gitExecutable, {QStringLiteral("git")});
     282                 :          57 :   detect(s.passExecutable, {QStringLiteral("pass")});
     283                 :          57 :   detect(s.pwgenExecutable, {QStringLiteral("pwgen")});
     284                 :          19 :   m_gpg->setText(s.gpgExecutable);
     285                 :          19 :   m_git->setText(s.gitExecutable);
     286                 :          19 :   m_pass->setText(s.passExecutable);
     287                 :             :   // Detected only the first time: a choice saved before (the wizard also
     288                 :             :   // runs for a store whose recipient list went missing) stays, or Next and
     289                 :             :   // Finish switched the backend without a word.
     290         [ +  + ]:          38 :   m_usePass->setChecked(m_wizard->m_detectChoices
     291   [ +  -  +  + ]:          16 :                             ? s.usePass || runnable(s.passExecutable)
     292                 :           3 :                             : s.usePass);
     293                 :          19 :   updateStatus();
     294                 :          19 : }
     295                 :             : 
     296                 :          22 : void ProgramsWizardPage::updateStatus() {
     297                 :          44 :   const QString gpg = m_gpg->text().trimmed();
     298         [ +  + ]:          22 :   if (gpg.isEmpty()) {
     299                 :           4 :     m_gpgStatus->setText(tr("GnuPG was not found. Install it (gpg or gpg2) "
     300                 :             :                             "and enter its location here."));
     301      [ +  +  + ]:          20 :   } else if (!runnable(gpg)) {
     302                 :           2 :     m_gpgStatus->setText(tr("%1 is not an executable file.").arg(gpg));
     303                 :             :   } else {
     304                 :          38 :     m_gpgStatus->setText(QString());
     305                 :             :   }
     306                 :          44 :   m_usePass->setEnabled(runnable(m_pass->text().trimmed()));
     307         [ +  + ]:          22 :   if (!m_usePass->isEnabled()) {
     308                 :          19 :     m_usePass->setChecked(false);
     309                 :             :   }
     310                 :          22 :   emit completeChanged();
     311                 :          22 : }
     312                 :             : 
     313                 :          83 : auto ProgramsWizardPage::isComplete() const -> bool {
     314                 :         249 :   return runnable(m_gpg->text().trimmed());
     315                 :             : }
     316                 :             : 
     317                 :          15 : auto ProgramsWizardPage::validatePage() -> bool {
     318                 :          15 :   AppSettings &s = m_wizard->m_settings;
     319                 :          15 :   s.gpgExecutable = m_gpg->text().trimmed();
     320                 :          15 :   s.gitExecutable = m_git->text().trimmed();
     321                 :          15 :   s.passExecutable = m_pass->text().trimmed();
     322                 :          15 :   s.usePass = m_usePass->isChecked();
     323                 :          15 :   return true;
     324                 :             : }
     325                 :             : 
     326                 :             : // --------------------------------------------------------------------------
     327                 :             : 
     328                 :          32 : KeyWizardPage::KeyWizardPage(FirstRunWizard *wizard)
     329                 :          32 :     : QWizardPage(wizard), m_wizard(wizard), m_list(new QListWidget(this)),
     330                 :          64 :       m_generate(new QPushButton(tr("Generate a new key pair…"), this)),
     331                 :          64 :       m_status(new QLabel(this)) {
     332                 :          64 :   setTitle(tr("Your key"));
     333                 :          32 :   setSubTitle(tr("Passwords are encrypted to GnuPG keys. Tick the keys that "
     334                 :             :                  "should be able to open a new store; you need at least one "
     335                 :             :                  "with its secret half on this machine."));
     336                 :          32 :   auto *layout = new QVBoxLayout(this);
     337                 :          32 :   layout->addWidget(m_list);
     338                 :             :   // What gpg reports is shown here: as text, never as markup.
     339                 :          32 :   m_status->setTextFormat(Qt::PlainText);
     340                 :          32 :   m_status->setWordWrap(true);
     341                 :          32 :   layout->addWidget(m_status);
     342                 :          32 :   auto *buttons = new QHBoxLayout;
     343                 :          32 :   buttons->addStretch();
     344                 :          32 :   buttons->addWidget(m_generate);
     345                 :          32 :   layout->addLayout(buttons);
     346                 :          32 :   connect(m_generate, &QPushButton::clicked, this, &KeyWizardPage::generate);
     347                 :          32 :   connect(m_list, &QListWidget::itemChanged, this,
     348                 :          85 :           [this]() { emit completeChanged(); });
     349                 :          32 : }
     350                 :             : 
     351                 :          21 : void KeyWizardPage::initializePage() { reload(); }
     352                 :             : 
     353                 :          22 : void KeyWizardPage::reload() {
     354                 :          22 :   QString problem;
     355                 :          22 :   m_wizard->m_keys =
     356                 :          22 :       FirstRunWizard::secretKeys(m_wizard->m_settings.gpgExecutable, &problem);
     357                 :          22 :   m_list->clear();
     358                 :          22 :   const QDateTime now = QDateTime::currentDateTime();
     359   [ +  +  +  + ]:          69 :   for (UserInfo &key : m_wizard->m_keys) {
     360                 :             :     // An expired or otherwise unusable key would make every encryption to
     361                 :             :     // the new store fail; list it, but do not tick it.
     362                 :          25 :     QString why;
     363   [ +  +  +  +  :          26 :     if (key.expiry.isValid() && key.expiry < now) {
                      - ]
     364                 :           1 :       why = tr("expired");
     365                 :             :     } else if (!key.isValid()) {
     366                 :           1 :       why = tr("not usable");
     367                 :             :     }
     368                 :             :     const bool usable = why.isEmpty();
     369                 :             :     // Only a key the user trusts ultimately, which gpg gives the keys made
     370                 :             :     // here, starts ticked. A colleague's key can be in this listing too: a
     371                 :             :     // secret key block imported from them, or a public one bound to the
     372                 :             :     // user's own subkey (gpg lists it as sec#). Certified, it shows as fully
     373                 :             :     // valid, and ticked by default every new entry would be encrypted to them.
     374   [ +  +  +  +  :          29 :     key.enabled = usable && key.validity == 'u';
                      + ]
     375                 :          50 :     QString label = QStringLiteral("%1\n%2").arg(key.name, key.key_id);
     376         [ +  + ]:          25 :     if (!usable) {
     377                 :           4 :       label += QStringLiteral(" (%1)").arg(why);
     378                 :             :     }
     379                 :          25 :     auto *item = new QListWidgetItem(label, m_list);
     380      [ +  +  + ]:          50 :     item->setFlags(usable ? (item->flags() | Qt::ItemIsUserCheckable)
     381                 :             :                           : (item->flags() & ~Qt::ItemIsEnabled));
     382      [ +  +  + ]:          29 :     item->setCheckState(key.enabled ? Qt::Checked : Qt::Unchecked);
     383                 :             :   }
     384                 :             :   // gpg's own words when it did not exit cleanly: "no secret key yet" would
     385                 :             :   // send the user to make a key they may well have.
     386         [ +  + ]:          22 :   if (!problem.isEmpty()) {
     387                 :             :     //: %1 is what GnuPG reported, in its own words.
     388                 :           4 :     m_status->setText(tr("GnuPG reported a problem while listing the secret "
     389                 :             :                          "keys:\n%1")
     390                 :           4 :                           .arg(problem));
     391                 :             :   } else {
     392                 :          20 :     m_status->setText(
     393                 :          20 :         m_wizard->m_keys.isEmpty()
     394         [ -  + ]:          40 :             ? tr("GnuPG has no secret key yet. Generate one here, "
     395                 :             :                  "or import your existing key with gpg first.")
     396                 :             :             : QString());
     397                 :             :   }
     398                 :          22 :   emit completeChanged();
     399                 :          44 : }
     400                 :             : 
     401                 :           1 : void KeyWizardPage::generate() {
     402                 :             :   // A backend of the wizard's own, set up from its copy of the settings, so
     403                 :             :   // the chosen gpg is used and nothing is written before Finish. Each Pass
     404                 :             :   // owns its Executor; the shared backend is not involved.
     405                 :           1 :   ImitatePass pass;
     406                 :           1 :   pass.init(m_wizard->m_settings);
     407                 :           1 :   KeygenDialog d(m_wizard->m_settings.gpgExecutable, &pass, this);
     408                 :           1 :   d.exec();
     409                 :           1 :   reload();
     410                 :           1 : }
     411                 :             : 
     412                 :         186 : auto KeyWizardPage::isComplete() const -> bool { return true; }
     413                 :             : 
     414                 :          20 : auto KeyWizardPage::validatePage() -> bool {
     415   [ +  +  +  - ]:          46 :   for (int i = 0; i < m_list->count() && i < m_wizard->m_keys.size(); ++i) {
     416                 :          26 :     m_wizard->m_keys[i].enabled = m_list->item(i)->checkState() == Qt::Checked;
     417                 :             :   }
     418                 :          20 :   return true;
     419                 :             : }
     420                 :             : 
     421                 :             : // --------------------------------------------------------------------------
     422                 :             : 
     423                 :          32 : StoreWizardPage::StoreWizardPage(FirstRunWizard *wizard)
     424                 :          32 :     : QWizardPage(wizard), m_wizard(wizard), m_path(new QLineEdit(this)),
     425                 :          64 :       m_useGit(new QCheckBox(tr("Keep the store under Git"), this)),
     426                 :          64 :       m_status(new QLabel(this)) {
     427                 :          64 :   setTitle(tr("Password store"));
     428                 :          32 :   setSubTitle(tr("The folder your passwords live in. An existing store is "
     429                 :             :                  "used as it is; an empty or missing folder is set up for "
     430                 :             :                  "the keys you ticked."));
     431                 :          32 :   auto *layout = new QFormLayout(this);
     432         [ +  + ]:          96 :   layout->addRow(tr("Folder"), pathRow(m_path, this, [this]() {
     433                 :             :                    return QFileDialog::getExistingDirectory(
     434                 :           1 :                        this, tr("Choose the password store folder"),
     435                 :           3 :                        m_path->text());
     436                 :             :                  }));
     437                 :          32 :   m_status->setWordWrap(true);
     438                 :          32 :   layout->addRow(QString(), m_status);
     439                 :          32 :   layout->addRow(QString(), m_useGit);
     440                 :          32 :   connect(m_path, &QLineEdit::textEdited, this, &StoreWizardPage::updateStatus);
     441                 :          32 : }
     442                 :             : 
     443                 :          25 : void StoreWizardPage::initializePage() {
     444                 :          25 :   QString path = m_wizard->m_settings.passStore;
     445         [ +  + ]:          25 :   if (path.isEmpty()) {
     446                 :          12 :     path = Util::findPasswordStore();
     447                 :             :   }
     448                 :          25 :   m_path->setText(QDir::toNativeSeparators(QDir::cleanPath(path)));
     449                 :          25 :   const QString clean = QDir::cleanPath(path);
     450                 :          50 :   const bool repository = QDir(clean).exists(QStringLiteral(".git"));
     451                 :          25 :   bool haveGit = runnable(m_wizard->m_settings.gitExecutable);
     452                 :             :   // A first commit needs a name and an e-mail; without them Finish would
     453                 :             :   // fail at the very end. Ask git now and say what to do instead.
     454                 :             :   const bool identity =
     455   [ +  +  +  +  :          34 :       haveGit && (repository || ProfileInit::gitIdentityConfigured(
                      + ]
     456                 :           9 :                                     QDir::homePath(), m_wizard->m_settings));
     457                 :          25 :   m_useGit->setToolTip(
     458                 :          25 :       haveGit && !identity
     459         [ +  + ]:          25 :           ? tr("Git has no name and e-mail to commit with yet. Run\n"
     460                 :             :                "git config --global user.name \"Your Name\"\n"
     461                 :             :                "git config --global user.email you@example.org\n"
     462                 :             :                "and turn Git on in Settings afterwards.")
     463                 :             :           : tr("Every change becomes a commit; a folder that is no "
     464                 :             :                "repository yet gets one"));
     465                 :          25 :   haveGit = haveGit && identity;
     466                 :          25 :   m_useGit->setEnabled(haveGit);
     467                 :             :   // On for a repository or a store still to be made, off for an existing
     468                 :             :   // store that is no repository: turning Git on there would make every
     469                 :             :   // remove fail in a folder git knows nothing about. A choice saved before
     470                 :             :   // stays (see ProgramsWizardPage::initializePage()).
     471      [ +  +  + ]:          50 :   m_useGit->setChecked(haveGit &&
     472         [ +  + ]:          11 :                        (m_wizard->m_detectChoices
     473   [ +  +  +  +  :          13 :                             ? m_wizard->m_settings.useGit || repository ||
                   +  + ]
     474                 :           4 :                                   !FirstRunWizard::isStore(clean)
     475         [ +  + ]:           2 :                             : m_wizard->m_settings.useGit));
     476                 :          25 :   updateStatus();
     477                 :          25 : }
     478                 :             : 
     479                 :         103 : auto StoreWizardPage::recipients() const -> int {
     480                 :             :   int ticked = 0;
     481         [ +  + ]:         220 :   for (const UserInfo &key : std::as_const(m_wizard->m_keys)) {
     482         [ +  + ]:         167 :     ticked += key.enabled ? 1 : 0;
     483                 :             :   }
     484                 :         103 :   return ticked;
     485                 :             : }
     486                 :             : 
     487                 :          34 : void StoreWizardPage::updateStatus() {
     488                 :          68 :   const QString path = QDir::cleanPath(m_path->text().trimmed());
     489                 :          34 :   QString text;
     490         [ +  + ]:          34 :   if (path.isEmpty()) {
     491                 :           1 :     text = tr("Enter a folder.");
     492      [ +  +  + ]:          33 :   } else if (FirstRunWizard::isStore(path)) {
     493                 :             :     const int entries = static_cast<int>(
     494   [ +  +  +  +  :          27 :         Util::regularFilesUnder(path, {QStringLiteral("*.gpg")}).size());
                   -  - ]
     495                 :           9 :     text = tr("An existing password store with %n entries.", nullptr, entries);
     496   [ +  +  +  + ]:          24 :   } else if (QDir(path).exists()) {
     497                 :          12 :     text = QDir(path).isEmpty()
     498         [ +  + ]:          12 :                ? tr("An empty folder; it will be set up as a password store.")
     499                 :             :                : tr("This folder is not a password store yet; a .gpg-id for "
     500                 :             :                     "the ticked keys will be written into it.");
     501                 :             :   } else {
     502                 :          18 :     text = tr("The folder does not exist yet; it will be created.");
     503                 :             :   }
     504   [ +  +  +  +  :          34 :   if (!path.isEmpty() && !FirstRunWizard::isStore(path) && recipients() == 0) {
             +  +  +  + ]
     505                 :           9 :     text += QLatin1Char(' ') +
     506                 :          18 :             tr("Go back and tick at least one key to encrypt it to.");
     507                 :             :   }
     508                 :          34 :   m_status->setText(text);
     509                 :          34 :   emit completeChanged();
     510                 :          34 : }
     511                 :             : 
     512                 :         114 : auto StoreWizardPage::isComplete() const -> bool {
     513                 :         342 :   const QString path = QDir::cleanPath(m_path->text().trimmed());
     514         [ +  + ]:         114 :   if (path.isEmpty()) {
     515                 :             :     return false;
     516                 :             :   }
     517   [ +  +  +  +  :         111 :   return FirstRunWizard::isStore(path) || recipients() > 0;
                   +  + ]
     518                 :             : }
     519                 :             : 
     520                 :          14 : auto StoreWizardPage::validatePage() -> bool {
     521                 :          14 :   m_wizard->m_settings.passStore =
     522                 :          28 :       QDir::fromNativeSeparators(m_path->text().trimmed());
     523   [ +  +  -  + ]:          14 :   m_wizard->m_settings.useGit = m_useGit->isEnabled() && m_useGit->isChecked();
     524                 :          14 :   return true;
     525                 :             : }
     526                 :             : 
     527                 :             : // --------------------------------------------------------------------------
     528                 :             : 
     529                 :          32 : DoneWizardPage::DoneWizardPage(FirstRunWizard *wizard)
     530                 :          32 :     : QWizardPage(wizard), m_wizard(wizard), m_summary(new QLabel(this)),
     531                 :          64 :       m_hidePassword(new QCheckBox(tr("Hide passwords until asked"), this)),
     532                 :          96 :       m_trayIcon(new QCheckBox(tr("Show an icon in the system tray"), this)) {
     533                 :          64 :   setTitle(tr("Ready"));
     534                 :          32 :   setSubTitle(tr("Everything else can be changed later in Settings."));
     535                 :          32 :   auto *layout = new QVBoxLayout(this);
     536                 :          32 :   m_summary->setWordWrap(true);
     537                 :          32 :   m_summary->setTextFormat(Qt::RichText);
     538                 :          32 :   layout->addWidget(m_summary);
     539                 :          32 :   layout->addSpacing(12);
     540                 :          32 :   m_hidePassword->setToolTip(
     541                 :          32 :       tr("Show the password line as dots; Show password reveals it"));
     542                 :          32 :   layout->addWidget(m_hidePassword);
     543                 :          32 :   layout->addWidget(m_trayIcon);
     544                 :          32 :   layout->addStretch();
     545                 :          32 :   setFinalPage(true);
     546                 :          32 : }
     547                 :             : 
     548                 :          13 : void DoneWizardPage::initializePage() {
     549                 :          13 :   const AppSettings &s = m_wizard->m_settings;
     550                 :          13 :   const QString store = QDir::toNativeSeparators(QDir::cleanPath(s.passStore));
     551                 :          13 :   QStringList lines;
     552                 :          26 :   lines << tr("Store: %1").arg(store.toHtmlEscaped());
     553      [ +  +  + ]:          13 :   if (FirstRunWizard::isStore(s.passStore)) {
     554                 :           6 :     lines << tr("It is already a password store and is used as it is.");
     555         [ +  + ]:           7 :   } else if (s.useGit) {
     556                 :           2 :     lines << tr("It will be set up for the ticked keys and put under Git.");
     557                 :             :   } else {
     558                 :           5 :     lines << tr("It will be set up for the ticked keys.");
     559                 :             :   }
     560                 :          26 :   lines << tr("GnuPG: %1").arg(s.gpgExecutable.toHtmlEscaped());
     561         [ -  + ]:          13 :   lines << (s.usePass ? tr("Operations run through pass.")
     562                 :             :                       : tr("Operations run through gpg and git directly."));
     563                 :          26 :   m_summary->setText(lines.join(QStringLiteral("<br>")));
     564                 :          13 :   m_hidePassword->setChecked(true);
     565                 :          13 :   const bool tray = QSystemTrayIcon::isSystemTrayAvailable();
     566                 :          13 :   m_trayIcon->setVisible(tray);
     567      [ -  +  + ]:          13 :   m_trayIcon->setChecked(tray && s.useTrayIcon);
     568                 :          13 : }
     569                 :             : 
     570                 :          19 : auto DoneWizardPage::validatePage() -> bool {
     571                 :          19 :   m_wizard->m_settings.hidePassword = m_hidePassword->isChecked();
     572                 :          19 :   m_wizard->m_settings.useTrayIcon =
     573   [ -  +  -  - ]:          19 :       m_trayIcon->isVisible() && m_trayIcon->isChecked();
     574                 :          19 :   return true;
     575                 :             : }
        

Generated by: LCOV version 2.0-1