QtPass Developer documentation
QtPass test coverage
Current view: top level - src/qtpasssettings.cpp (source / functions) Coverage Total Hit
Test: QtPass Lines: 93.2 % 221 206
Test Date: 2026-12-03 23:34:20 Functions: 97.4 % 38 37
Branches: 79.1 % 139 110

             Branch data     Line data    Source code
       1                 :             : // SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
       2                 :             : // SPDX-License-Identifier: GPL-3.0-or-later
       3                 :             : /**
       4                 :             :  * @file
       5                 :             :  * @brief Implements QtPassSettings; getInstance() picks the portable
       6                 :             :  * `qtpass.ini` over the platform store, and getProfiles()/setProfiles()
       7                 :             :  * read and write the `profile/<name>/` groups.
       8                 :             :  */
       9                 :             : #include "qtpasssettings.h"
      10                 :             : #include "pass.h"
      11                 :             : #include "passbackendfactory.h"
      12                 :             : #include "settingsserializer.h"
      13                 :             : 
      14                 :             : #include "util.h"
      15                 :             : 
      16                 :             : #include <QCoreApplication>
      17                 :             : #include <QCursor>
      18                 :             : #include <QDebug>
      19                 :             : #include <QDir>
      20                 :             : #include <QFile>
      21                 :             : #include <QFileInfo>
      22                 :             : #include <QGuiApplication>
      23                 :             : #include <QScreen>
      24                 :             : #include <utility>
      25                 :             : 
      26                 :             : bool QtPassSettings::initialized = false;
      27                 :             : 
      28                 :             : QtPassSettings *QtPassSettings::m_instance = nullptr;
      29                 :             : // Portable mode: a qtpass.ini next to the executable wins over the platform
      30                 :             : // settings store.
      31                 :       16464 : auto QtPassSettings::getInstance() -> QtPassSettings * {
      32         [ +  + ]:       16464 :   if (!QtPassSettings::initialized) {
      33                 :          15 :     QString portable_ini = QCoreApplication::applicationDirPath() +
      34                 :          15 :                            QDir::separator() + "qtpass.ini";
      35   [ +  +  -  + ]:          15 :     if (QFile(portable_ini).exists()) {
      36                 :           0 :       m_instance = new QtPassSettings(portable_ini, QSettings::IniFormat);
      37                 :             :     } else {
      38                 :          15 :       m_instance = new QtPassSettings("IJHack", "QtPass");
      39                 :             :     }
      40                 :             : 
      41                 :          15 :     initialized = true;
      42                 :             :   }
      43                 :             : 
      44                 :       16464 :   return m_instance;
      45                 :             : }
      46                 :             : 
      47                 :        3873 : auto QtPassSettings::load() -> AppSettings {
      48                 :        3873 :   AppSettings s = SettingsSerializer::load(*getInstance());
      49         [ +  + ]:        3873 :   if (!s.passStore.isEmpty()) {
      50                 :        6522 :     s.passStore = QDir::cleanPath(QDir(s.passStore).absolutePath());
      51      [ +  +  + ]:        3261 :     if (!s.passStore.endsWith('/'))
      52                 :        3223 :       s.passStore += '/';
      53                 :             :   }
      54                 :        3873 :   return s;
      55                 :           0 : }
      56                 :             : 
      57                 :          46 : auto QtPassSettings::activeProfileFlag(std::optional<bool> Profile::*flag)
      58                 :             :     -> std::optional<bool> {
      59                 :          92 :   const QString name = getProfile();
      60         [ +  + ]:          46 :   if (name.isEmpty()) {
      61                 :          40 :     return std::nullopt;
      62                 :             :   }
      63                 :           6 :   const Profiles profiles = getProfiles();
      64                 :             :   const auto active = profiles.constFind(name);
      65         [ +  + ]:           6 :   return active == profiles.constEnd() ? std::nullopt : (*active).*flag;
      66                 :             : }
      67                 :             : 
      68                 :             : #ifndef Q_OS_WIN
      69                 :             : #include <sys/stat.h>
      70                 :             : #endif
      71                 :             : 
      72                 :             : namespace {
      73                 :             : /// Spellings compare without case where file systems usually ignore it
      74                 :             : /// (Windows, macOS). On a macOS volume that tells case apart, two folders
      75                 :             : /// differing only in case then count as one profile's store: the safe way
      76                 :             : /// round here, a signing key applied rather than dropped.
      77                 :             : #if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
      78                 :             : constexpr auto kStoreCase = Qt::CaseInsensitive;
      79                 :             : #else
      80                 :             : constexpr auto kStoreCase = Qt::CaseSensitive;
      81                 :             : #endif
      82                 :             : 
      83                 :             : /**
      84                 :             :  * @brief A folder as compared with a profile's store: resolved when it
      85                 :             :  *        exists, cleaned otherwise.
      86                 :             :  * @param path A folder.
      87                 :             :  * @return The path to compare.
      88                 :             :  */
      89                 :          14 : auto comparableFolder(const QString &path) -> QString {
      90                 :          14 :   const QString real = QFileInfo(path).canonicalFilePath();
      91   [ +  +  +  +  :          28 :   return real.isEmpty() ? QDir::cleanPath(QDir(path).absolutePath()) : real;
             +  +  +  -  
                      - ]
      92                 :             : }
      93                 :             : 
      94                 :             : /**
      95                 :             :  * @brief Whether @p a and @p b are one folder: decided by the file system
      96                 :             :  *        when both exist (whatever case each is spelled in), else by
      97                 :             :  *        comparing the spellings.
      98                 :             :  * @param a One folder.
      99                 :             :  * @param b The other.
     100                 :             :  * @return true when they are the same folder.
     101                 :             :  */
     102                 :          34 : auto sameFolder(const QString &a, const QString &b) -> bool {
     103                 :             : #ifndef Q_OS_WIN
     104                 :          34 :   struct stat first{};
     105                 :          34 :   struct stat second{};
     106   [ +  +  +  +  :         100 :   if (::stat(QFile::encodeName(a).constData(), &first) == 0 &&
                   +  + ]
     107         [ -  - ]:          66 :       ::stat(QFile::encodeName(b).constData(), &second) == 0) {
     108   [ +  -  +  + ]:          44 :     return first.st_dev == second.st_dev && first.st_ino == second.st_ino;
     109                 :             :   }
     110                 :             : #endif
     111                 :           7 :   return comparableFolder(a).compare(comparableFolder(b), kStoreCase) == 0;
     112                 :             : }
     113                 :             : 
     114                 :             : } // namespace
     115                 :             : 
     116                 :          49 : void QtPassSettings::takeProfileOfStore(AppSettings &settings,
     117                 :             :                                         const Profiles &profiles) {
     118         [ -  + ]:          49 :   if (settings.passStore.isEmpty()) {
     119                 :           0 :     return;
     120                 :             :   }
     121                 :             :   // Several profiles can share a folder. The active one wins when it is
     122                 :             :   // among them (an OK that changed nothing changes nothing); else one with
     123                 :             :   // a signing key wins over one without, so a typed folder never opens
     124                 :             :   // unsigned while a signed profile claims it; else the first by name.
     125                 :          49 :   QString exact;
     126         [ +  + ]:          81 :   for (auto i = profiles.cbegin(); i != profiles.cend(); ++i) {
     127   [ +  -  -  + ]:          34 :     if (i.key().isEmpty() || i.value().path.isEmpty()) {
     128                 :           0 :       continue;
     129                 :             :     }
     130      [ +  +  + ]:          34 :     if (sameFolder(settings.passStore, i.value().path)) {
     131         [ +  + ]:          11 :       if (i.key() == settings.activeProfile) {
     132                 :           2 :         exact = i.key();
     133                 :           2 :         break;
     134                 :             :       }
     135   [ +  +  +  -  :          11 :       if (exact.isEmpty() || (profiles.value(exact).signingKey.isEmpty() &&
          -  +  +  +  +  
                      - ]
     136                 :             :                               !i.value().signingKey.isEmpty())) {
     137                 :           9 :         exact = i.key();
     138                 :             :       }
     139                 :             :     }
     140                 :             :   }
     141         [ +  + ]:          49 :   if (!exact.isEmpty()) {
     142                 :           9 :     settings.activeProfile = exact;
     143                 :          18 :     settings.passSigningKey = profiles.value(exact).signingKey;
     144                 :             :   }
     145                 :             : }
     146                 :             : 
     147                 :         790 : auto QtPassSettings::loadEffective() -> AppSettings {
     148                 :         790 :   AppSettings s = load();
     149                 :         790 :   const Profiles profiles = getProfiles();
     150                 :         790 :   const auto active = profiles.constFind(s.activeProfile);
     151   [ +  +  +  - ]:         819 :   if (!s.activeProfile.isEmpty() && active != profiles.constEnd()) {
     152                 :          29 :     s.useGit = active->useGit.value_or(s.useGit);
     153                 :          29 :     s.autoPush = active->autoPush.value_or(s.autoPush);
     154                 :          29 :     s.autoPull = active->autoPull.value_or(s.autoPull);
     155                 :             :   }
     156                 :         790 :   return s;
     157                 :           0 : }
     158                 :             : 
     159                 :        1059 : void QtPassSettings::save(const AppSettings &settings) {
     160                 :        1059 :   SettingsSerializer::save(*getInstance(), settings);
     161                 :             :   // The "use pass" mode may have changed; rebuild the backend on next use.
     162                 :        1059 :   PassBackendFactory::invalidate();
     163                 :        1059 : }
     164                 :             : 
     165                 :          15 : auto QtPassSettings::getPasswordConfiguration() -> PasswordConfiguration {
     166                 :          15 :   return SettingsSerializer::loadPasswordConfiguration(*getInstance());
     167                 :             : }
     168                 :             : 
     169                 :        1199 : auto QtPassSettings::getProfiles() -> Profiles {
     170                 :        1199 :   getInstance()->beginGroup(SettingsConstants::profile);
     171                 :        1199 :   Profiles profiles;
     172                 :             : 
     173                 :             :   // migration from version <= v1.3.2: profiles datastructure
     174                 :        1199 :   QStringList childKeys = getInstance()->childKeys();
     175         [ -  + ]:        1199 :   for (const auto &key : std::as_const(childKeys)) {
     176                 :           0 :     Profile profile;
     177                 :           0 :     profile.path = getInstance()->value(key).toString();
     178                 :           0 :     profiles.insert(key, profile);
     179                 :             :   }
     180                 :             :   // /migration from version <= v1.3.2
     181                 :             : 
     182                 :        1199 :   QStringList childGroups = getInstance()->childGroups();
     183         [ +  + ]:        1352 :   for (const auto &group : std::as_const(childGroups)) {
     184                 :         153 :     Profile profile;
     185                 :         306 :     profile.path = getInstance()->value(group + "/path").toString();
     186                 :         306 :     profile.signingKey = getInstance()->value(group + "/signingKey").toString();
     187                 :         153 :     profile.useGit = Profile::flagFromString(
     188   [ +  +  +  + ]:         306 :         getInstance()->value(group + "/useGit").toString());
     189                 :         153 :     profile.autoPush = Profile::flagFromString(
     190   [ +  +  +  + ]:         306 :         getInstance()->value(group + "/autoPush").toString());
     191                 :         153 :     profile.autoPull = Profile::flagFromString(
     192   [ +  +  +  + ]:         306 :         getInstance()->value(group + "/autoPull").toString());
     193                 :         153 :     profiles.insert(group, profile);
     194                 :             :   }
     195                 :             : 
     196                 :        1199 :   getInstance()->endGroup();
     197                 :             : 
     198                 :        1199 :   return profiles;
     199                 :             : }
     200                 :             : 
     201                 :         118 : void QtPassSettings::setProfiles(const Profiles &profiles) {
     202                 :             :   // "profile" is both the active-profile scalar (getProfile()) and the
     203                 :             :   // profile/<name>/ group; remove() wipes both, which would switch the active
     204                 :             :   // store on every save, so preserve the active name across the rewrite.
     205                 :             :   const QString activeProfile =
     206                 :         118 :       getInstance()->value(SettingsConstants::profile).toString();
     207                 :         118 :   getInstance()->remove(SettingsConstants::profile);
     208         [ +  + ]:         118 :   if (!activeProfile.isEmpty()) {
     209                 :          51 :     getInstance()->setValue(SettingsConstants::profile, activeProfile);
     210                 :             :   }
     211                 :         118 :   getInstance()->beginGroup(SettingsConstants::profile);
     212                 :             : 
     213         [ +  + ]:         187 :   for (auto i = profiles.cbegin(); i != profiles.cend(); ++i) {
     214                 :         138 :     getInstance()->setValue(i.key() + "/path", i.value().path);
     215                 :         138 :     getInstance()->setValue(i.key() + "/signingKey", i.value().signingKey);
     216                 :         138 :     getInstance()->setValue(i.key() + "/useGit",
     217                 :         138 :                             Profile::flagToString(i.value().useGit));
     218                 :         138 :     getInstance()->setValue(i.key() + "/autoPush",
     219                 :         138 :                             Profile::flagToString(i.value().autoPush));
     220                 :         138 :     getInstance()->setValue(i.key() + "/autoPull",
     221                 :         207 :                             Profile::flagToString(i.value().autoPull));
     222                 :             :   }
     223                 :             : 
     224                 :         118 :   getInstance()->endGroup();
     225                 :         118 : }
     226                 :             : 
     227                 :         850 : auto QtPassSettings::getPass() -> Pass * {
     228                 :         850 :   return PassBackendFactory::getPass();
     229                 :             : }
     230                 :             : 
     231                 :         213 : auto QtPassSettings::getVersion(const QString &defaultValue) -> QString {
     232                 :         213 :   return getInstance()
     233                 :         426 :       ->value(SettingsConstants::version, defaultValue)
     234                 :         426 :       .toString();
     235                 :             : }
     236                 :         211 : void QtPassSettings::setVersion(const QString &version) {
     237                 :         211 :   getInstance()->setValue(SettingsConstants::version, version);
     238                 :         211 : }
     239                 :             : 
     240                 :         220 : auto QtPassSettings::getGeometry(const QByteArray &defaultValue) -> QByteArray {
     241                 :         220 :   return getInstance()
     242                 :         440 :       ->value(SettingsConstants::geometry, defaultValue)
     243                 :         440 :       .toByteArray();
     244                 :             : }
     245                 :         215 : void QtPassSettings::setGeometry(const QByteArray &geometry) {
     246                 :         215 :   getInstance()->setValue(SettingsConstants::geometry, geometry);
     247                 :         215 : }
     248                 :             : 
     249                 :         218 : auto QtPassSettings::getSavestate(const QByteArray &defaultValue)
     250                 :             :     -> QByteArray {
     251                 :         218 :   return getInstance()
     252                 :         436 :       ->value(SettingsConstants::savestate, defaultValue)
     253                 :         436 :       .toByteArray();
     254                 :             : }
     255                 :         211 : void QtPassSettings::setSavestate(const QByteArray &saveState) {
     256                 :         211 :   getInstance()->setValue(SettingsConstants::savestate, saveState);
     257                 :         211 : }
     258                 :             : 
     259                 :         196 : auto QtPassSettings::getDialogGeometry(const QString &key,
     260                 :             :                                        const QByteArray &defaultValue)
     261                 :             :     -> QByteArray {
     262                 :         196 :   return getInstance()
     263                 :         588 :       ->value(SettingsConstants::dialogGeometry + "/" + key, defaultValue)
     264                 :         392 :       .toByteArray();
     265                 :             : }
     266                 :          45 : void QtPassSettings::setDialogGeometry(const QString &key,
     267                 :             :                                        const QByteArray &geometry) {
     268                 :          90 :   getInstance()->setValue(SettingsConstants::dialogGeometry + "/" + key,
     269                 :          90 :                           geometry);
     270                 :          45 : }
     271                 :             : 
     272                 :         167 : void QtPassSettings::setUsePass(const bool &usePass) {
     273                 :         167 :   getInstance()->setValue(SettingsConstants::usePass, usePass);
     274                 :             :   // Backend selection changed: force re-selection on next getPass().
     275                 :         167 :   PassBackendFactory::invalidate();
     276                 :         167 : }
     277                 :             : 
     278                 :         249 : auto QtPassSettings::getAutoclearSeconds(const int &defaultValue) -> int {
     279                 :         249 :   return getInstance()
     280                 :         498 :       ->value(SettingsConstants::autoclearSeconds, defaultValue)
     281                 :         498 :       .toInt();
     282                 :             : }
     283                 :             : // Creates the directory if missing; the result always ends in a separator.
     284                 :         496 : auto QtPassSettings::getPassStore(const QString &defaultValue) -> QString {
     285                 :         496 :   QString returnValue = getInstance()
     286                 :         992 :                             ->value(SettingsConstants::passStore, defaultValue)
     287                 :         496 :                             .toString();
     288                 :             : 
     289                 :         992 :   returnValue = QDir(returnValue).absolutePath();
     290                 :             : 
     291                 :             :   // ensure directory exists if never used pass or misconfigured.
     292                 :             :   // otherwise process->setWorkingDirectory(passStore); will fail on execution.
     293   [ +  +  +  + ]:         496 :   if (!QDir(returnValue).exists()) {
     294      [ +  -  + ]:           3 :     if (!Util::mkpathForStore(returnValue)) {
     295                 :           0 :       qWarning() << "Failed to create password store directory:" << returnValue;
     296                 :             :     }
     297                 :             :   }
     298                 :             : 
     299   [ +  +  +  -  :         992 :   if (!returnValue.endsWith("/") && !returnValue.endsWith(QDir::separator())) {
             +  -  +  +  
                      - ]
     300                 :         496 :     returnValue += QDir::separator();
     301                 :             :   }
     302                 :             : 
     303                 :         496 :   return returnValue;
     304                 :             : }
     305                 :         430 : void QtPassSettings::setPassStore(const QString &passStore) {
     306                 :         430 :   getInstance()->setValue(SettingsConstants::passStore, passStore);
     307                 :         430 : }
     308                 :         209 : void QtPassSettings::initExecutables() {
     309                 :         209 :   AppSettings s = QtPassSettings::load();
     310         [ +  + ]:         209 :   if (s.passExecutable.isEmpty())
     311                 :         404 :     s.passExecutable = Util::findBinaryInPath("pass");
     312         [ +  + ]:         209 :   if (s.gitExecutable.isEmpty())
     313                 :          12 :     s.gitExecutable = Util::findBinaryInPath("git");
     314         [ +  + ]:         209 :   if (s.gpgExecutable.isEmpty()) {
     315                 :           4 :     s.gpgExecutable = Util::findBinaryInPath("gpg2");
     316         [ -  + ]:           2 :     if (s.gpgExecutable.isEmpty())
     317                 :           0 :       s.gpgExecutable = Util::findBinaryInPath("gpg");
     318                 :             :   }
     319         [ +  + ]:         209 :   if (s.pwgenExecutable.isEmpty())
     320                 :         416 :     s.pwgenExecutable = Util::findBinaryInPath("pwgen");
     321                 :         209 :   QtPassSettings::save(s);
     322                 :         209 : }
     323                 :           0 : auto QtPassSettings::getPassExecutable(const QString &defaultValue) -> QString {
     324                 :           0 :   return getInstance()
     325                 :           0 :       ->value(SettingsConstants::passExecutable, defaultValue)
     326                 :           0 :       .toString();
     327                 :             : }
     328                 :             : 
     329                 :         157 : auto QtPassSettings::getProfile(const QString &defaultValue) -> QString {
     330                 :         157 :   return getInstance()
     331                 :         314 :       ->value(SettingsConstants::profile, defaultValue)
     332                 :         314 :       .toString();
     333                 :             : }
     334                 :             : // With a true default and nothing stored, a store with a .git dir means Git.
     335                 :          23 : auto QtPassSettings::isUseGit(const bool &defaultValue) -> bool {
     336                 :             :   // The active profile's own flag, else the Git page's.
     337         [ -  + ]:          23 :   if (const std::optional<bool> own = activeProfileFlag(&Profile::useGit)) {
     338                 :           0 :     return *own;
     339                 :             :   }
     340                 :             :   bool storedValue =
     341                 :          23 :       getInstance()->value(SettingsConstants::useGit, defaultValue).toBool();
     342   [ +  +  +  + ]:          23 :   if (storedValue == defaultValue && defaultValue) {
     343                 :           4 :     QString passStore = getPassStore();
     344   [ +  +  +  -  :           4 :     if (QFileInfo(passStore).isDir() &&
                   +  + ]
     345   [ +  +  +  +  :           6 :         QFileInfo(passStore + QDir::separator() + ".git").isDir()) {
          +  -  +  -  -  
             -  -  -  -  
                      - ]
     346                 :             :       return true;
     347                 :             :     }
     348                 :             :   }
     349                 :             :   return storedValue;
     350                 :             : }
     351                 :         211 : auto QtPassSettings::isUseGrepSearch(const bool &defaultValue) -> bool {
     352                 :         211 :   return getInstance()
     353                 :         422 :       ->value(SettingsConstants::useGrepSearch, defaultValue)
     354                 :         422 :       .toBool();
     355                 :             : }
     356                 :             : 
     357                 :         500 : auto QtPassSettings::isUseOtp(const bool &defaultValue) -> bool {
     358                 :         500 :   return getInstance()->value(SettingsConstants::useOtp, defaultValue).toBool();
     359                 :             : }
     360                 :             : 
     361                 :           1 : void QtPassSettings::setQrencodeExecutable(const QString &qrencodeExecutable) {
     362                 :           2 :   getInstance()->setValue(SettingsConstants::qrencodeExecutable,
     363                 :           2 :                           qrencodeExecutable);
     364                 :           1 : }
     365                 :             : 
     366                 :           4 : auto QtPassSettings::isHideOnClose(const bool &defaultValue) -> bool {
     367                 :           4 :   return getInstance()
     368                 :           8 :       ->value(SettingsConstants::hideOnClose, defaultValue)
     369                 :           8 :       .toBool();
     370                 :             : }
     371                 :         222 : auto QtPassSettings::isAlwaysOnTop(const bool &defaultValue) -> bool {
     372                 :         222 :   return getInstance()
     373                 :         444 :       ->value(SettingsConstants::alwaysOnTop, defaultValue)
     374                 :         444 :       .toBool();
     375                 :             : }
     376                 :             : 
     377                 :          23 : auto QtPassSettings::isAutoPush(const bool &defaultValue) -> bool {
     378         [ +  + ]:          23 :   if (const std::optional<bool> own = activeProfileFlag(&Profile::autoPush)) {
     379                 :           1 :     return *own;
     380                 :             :   }
     381                 :          22 :   return getInstance()
     382                 :          44 :       ->value(SettingsConstants::autoPush, defaultValue)
     383                 :          22 :       .toBool();
     384                 :             : }
     385                 :             : 
     386                 :         507 : auto QtPassSettings::isShowProcessOutput(const bool &defaultValue) -> bool {
     387                 :         507 :   return getInstance()
     388                 :        1014 :       ->value(SettingsConstants::showProcessOutput, defaultValue)
     389                 :        1014 :       .toBool();
     390                 :             : }
     391                 :         468 : auto QtPassSettings::getRealPass() -> RealPass * {
     392                 :         468 :   return PassBackendFactory::getRealPass();
     393                 :             : }
     394                 :         665 : auto QtPassSettings::getImitatePass() -> ImitatePass * {
     395                 :         665 :   return PassBackendFactory::getImitatePass();
     396                 :             : }
     397                 :             : 
     398                 :         216 : auto QtPassSettings::backendsIdle() -> bool {
     399                 :         216 :   return PassBackendFactory::isIdle();
     400                 :             : }
        

Generated by: LCOV version 2.0-1