QtPass Developer documentation
QtPass test coverage
Current view: top level - src/qtpasssettings.h (source / functions) Coverage Total Hit
Test: QtPass Lines: 60.0 % 5 3
Test Date: 2026-12-03 23:34:20 Functions: 50.0 % 2 1

            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 QtPassSettings, the QSettings singleton behind every saved setting,
       6              :  * the profiles and the Pass backend accessors.
       7              :  */
       8              : #ifndef SRC_QTPASSSETTINGS_H_
       9              : #define SRC_QTPASSSETTINGS_H_
      10              : 
      11              : #include "appsettings.h"
      12              : #include "enums.h"
      13              : #include "imitatepass.h"
      14              : #include "passwordconfiguration.h"
      15              : #include "profile.h"
      16              : #include "realpass.h"
      17              : #include "settingsconstants.h"
      18              : 
      19              : #include <QByteArray>
      20              : #include <QHash>
      21              : #include <QPoint>
      22              : #include <QSettings>
      23              : #include <QSize>
      24              : #include <QVariant>
      25              : 
      26              : #include <optional>
      27              : 
      28              : /**
      29              :  * @class QtPassSettings
      30              :  * @ingroup settings
      31              :  * @brief Singleton that stores QtPass settings, handles persistence and
      32              :  * defaults.
      33              :  *
      34              :  * QtPassSettings manages all application configuration including:
      35              :  * - Window geometry (QWidget::saveGeometry blobs, main window and dialogs)
      36              :  * - Password store configuration (path, GPG settings)
      37              :  * - Executable paths (pass, git, gpg, pwgen)
      38              :  * - UI preferences (clipboard, autoclear, display options)
      39              :  * - gopass configuration
      40              :  *
      41              :  * Uses QSettings for persistence with a singleton pattern.
      42              :  * All getters accept a default value returned when setting is not yet saved.
      43              :  */
      44              : class QtPassSettings : public QSettings {
      45              : public:
      46              :   /**
      47              :    * @brief Deleted: QtPassSettings is used through its static functions and
      48              :    * never instantiated. Public, so that a call names the deletion rather
      49              :    * than the access.
      50              :    */
      51              :   explicit QtPassSettings() = delete;
      52              : 
      53              : private:
      54              :   /**
      55              :    * @brief Settings in an explicit file: the portable `qtpass.ini` next to
      56              :    * the executable, which getInstance() prefers when it exists.
      57              :    * @param organization The file's path (QSettings' file-name constructor).
      58              :    * @param format The file's format; IniFormat for the portable file.
      59              :    */
      60              :   QtPassSettings(const QString &organization, const QSettings::Format format)
      61            0 :       : QSettings(organization, format) {}
      62              :   /**
      63              :    * @brief Per-user settings in QSettings::defaultFormat().
      64              :    *
      65              :    * QSettings(organization, application) ignores setDefaultFormat() and
      66              :    * always uses NativeFormat; going through the explicit-format constructor
      67              :    * keeps the production default (NativeFormat) while letting the test
      68              :    * suites redirect settings to an ini file in a temporary directory.
      69              :    * @param organization The organisation the settings are filed under
      70              :    *        ("IJHack").
      71              :    * @param application The application name ("QtPass").
      72              :    */
      73           15 :   QtPassSettings(const QString &organization, const QString &application)
      74           15 :       : QSettings(QSettings::defaultFormat(), QSettings::UserScope,
      75           15 :                   organization, application) {}
      76              : 
      77            0 :   ~QtPassSettings() override = default;
      78              : 
      79              :   /// Whether getInstance() has chosen the store and built m_instance.
      80              :   static bool initialized;
      81              :   /// The one instance, built on the first getInstance() and kept for the
      82              :   /// life of the process.
      83              :   static QtPassSettings *m_instance;
      84              : 
      85              :   /**
      86              :    * @brief The active profile's own value of a Git flag.
      87              :    * @param flag Profile::useGit, autoPush or autoPull.
      88              :    * @return The flag; nullopt without an active profile or when it follows
      89              :    *         the Git page.
      90              :    */
      91              :   static auto activeProfileFlag(std::optional<bool> Profile::*flag)
      92              :       -> std::optional<bool>;
      93              : 
      94              : public:
      95              :   /**
      96              :    * @brief Get the singleton instance.
      97              :    * @return Pointer to the QtPassSettings instance.
      98              :    */
      99              :   static auto getInstance() -> QtPassSettings *;
     100              : 
     101              :   /**
     102              :    * @brief Load all flat settings into an AppSettings value object.
     103              :    *
     104              :    * Convenience facade over SettingsSerializer using the singleton store.
     105              :    * Note: nested/keyed settings (profiles, per-dialog geometry) are not part
     106              :    * of AppSettings and still use their dedicated accessors.
     107              :    * @return Current settings.
     108              :    */
     109              :   static auto load() -> AppSettings;
     110              :   /**
     111              :    * @brief load() with the active profile's own Git flags applied: what the
     112              :    *        backends and the Git actions run with.
     113              :    *
     114              :    * load() keeps the Git page's values, which every profile without flags
     115              :    * of its own falls back to. Never save() this: the profile's flags would
     116              :    * replace the Git page's for every other profile.
     117              :    * @return Current settings, Git flags resolved for the active profile.
     118              :    */
     119              :   static auto loadEffective() -> AppSettings;
     120              :   /**
     121              :    * @brief Make @p settings use the profile whose store it opens.
     122              :    *
     123              :    * A store folder typed in the first-run wizard or in Settings kept the
     124              :    * profile, and so the signing key, that was active before: a signed
     125              :    * profile's store typed under an unsigned profile was opened with every
     126              :    * `.gpg-id` check off. When @p settings.passStore is a profile's store,
     127              :    * that profile becomes active with its signing key (of several on one
     128              :    * folder: the active one, else one with a signing key, else the first by
     129              :    * name). A folder inside or above a signed profile's store is not
     130              :    * looked at: a signed list names the folder it was written for, so a
     131              :    * store opened inside another with that store's key would refuse its
     132              :    * lists.
     133              :    * @param settings The settings to adjust; passStore is read.
     134              :    * @param profiles The profiles to look in.
     135              :    */
     136              :   static void takeProfileOfStore(AppSettings &settings,
     137              :                                  const Profiles &profiles);
     138              :   /**
     139              :    * @brief Persist an AppSettings value object to the singleton store.
     140              :    *
     141              :    * Writes via SettingsSerializer and invalidates the cached Pass backend so a
     142              :    * changed "use pass" mode takes effect on the next getPass().
     143              :    * @param settings Values to save.
     144              :    */
     145              :   static void save(const AppSettings &settings);
     146              : 
     147              :   // Window settings
     148              :   /**
     149              :    * @brief Get saved application version.
     150              :    * @param defaultValue String returned if no version saved.
     151              :    * @return Application version string.
     152              :    */
     153              :   static auto getVersion(const QString &defaultValue = QVariant().toString())
     154              :       -> QString;
     155              :   /**
     156              :    * @brief Save application version.
     157              :    * @param version Version string to save.
     158              :    */
     159              :   static void setVersion(const QString &version);
     160              : 
     161              :   /**
     162              :    * @brief Get saved window geometry.
     163              :    * @param defaultValue Byte array returned if no geometry saved.
     164              :    * @return Window geometry as QByteArray.
     165              :    */
     166              :   static auto
     167              :   getGeometry(const QByteArray &defaultValue = QVariant().toByteArray())
     168              :       -> QByteArray;
     169              :   /**
     170              :    * @brief Save window geometry.
     171              :    * @param geometry Window geometry to save.
     172              :    */
     173              :   static void setGeometry(const QByteArray &geometry);
     174              : 
     175              :   /**
     176              :    * @brief Get saved window state.
     177              :    * @param defaultValue Byte array returned if no state saved.
     178              :    * @return Window state as QByteArray.
     179              :    */
     180              :   static auto
     181              :   getSavestate(const QByteArray &defaultValue = QVariant().toByteArray())
     182              :       -> QByteArray;
     183              :   /**
     184              :    * @brief Save window state.
     185              :    * @param saveState Window state to save.
     186              :    */
     187              :   static void setSavestate(const QByteArray &saveState);
     188              : 
     189              :   // Dialog-specific settings
     190              :   /**
     191              :    * @brief Get saved dialog geometry.
     192              :    * @param key Dialog identifier (e.g., "usersDialog").
     193              :    * @param defaultValue Byte array returned if not saved.
     194              :    * @return Dialog geometry as QByteArray.
     195              :    */
     196              :   static auto
     197              :   getDialogGeometry(const QString &key,
     198              :                     const QByteArray &defaultValue = QVariant().toByteArray())
     199              :       -> QByteArray;
     200              :   /**
     201              :    * @brief Save dialog geometry.
     202              :    * @param key Dialog identifier.
     203              :    * @param geometry Dialog geometry to save.
     204              :    */
     205              :   static void setDialogGeometry(const QString &key, const QByteArray &geometry);
     206              : 
     207              :   // Password store settings
     208              :   /**
     209              :    * @brief Save use pass setting.
     210              :    * @param usePass true to use pass, false for GPG.
     211              :    */
     212              :   static void setUsePass(const bool &usePass);
     213              : 
     214              :   // UI settings
     215              : 
     216              :   /**
     217              :    * @brief Get autoclear delay in seconds.
     218              :    * @param defaultValue Integer returned if not saved.
     219              :    * @return Seconds before clipboard is cleared.
     220              :    */
     221              :   static auto getAutoclearSeconds(const int &defaultValue = QVariant().toInt())
     222              :       -> int;
     223              :   // Pass store path
     224              :   /**
     225              :    * @brief Get password store directory path.
     226              :    * @param defaultValue String returned if not saved.
     227              :    * @return Path to password store.
     228              :    */
     229              :   static auto getPassStore(const QString &defaultValue = QVariant().toString())
     230              :       -> QString;
     231              :   /**
     232              :    * @brief Save password store path.
     233              :    * @param passStore Path to password store.
     234              :    */
     235              :   static void setPassStore(const QString &passStore);
     236              : 
     237              :   // Executable paths
     238              :   /**
     239              :    * @brief Initialize executable paths by auto-detecting them.
     240              :    */
     241              :   static void initExecutables();
     242              :   /**
     243              :    * @brief Get pass executable path.
     244              :    * @param defaultValue String returned if not saved.
     245              :    * @return Path to pass executable.
     246              :    */
     247              :   static auto
     248              :   getPassExecutable(const QString &defaultValue = QVariant().toString())
     249              :       -> QString;
     250              :   /**
     251              :    * @brief Get active profile name.
     252              :    * @param defaultValue String returned if not saved.
     253              :    * @return Active profile name.
     254              :    */
     255              :   static auto getProfile(const QString &defaultValue = QVariant().toString())
     256              :       -> QString;
     257              : 
     258              :   /**
     259              :    * @brief Check whether Git integration is enabled.
     260              :    * @param defaultValue Value returned if not saved.
     261              :    * @return True if Git integration is enabled.
     262              :    */
     263              :   static auto isUseGit(const bool &defaultValue = QVariant().toBool()) -> bool;
     264              :   /**
     265              :    * @brief Check whether content search is enabled.
     266              :    * @param defaultValue Value returned if not saved (defaults to false).
     267              :    * @return True if grep search is enabled.
     268              :    */
     269              :   static auto isUseGrepSearch(const bool &defaultValue = false) -> bool;
     270              :   /**
     271              :    * @brief Check whether OTP support is enabled.
     272              :    * @param defaultValue Value returned if not saved (defaults to true).
     273              :    * @return True if OTP support is enabled.
     274              :    */
     275              :   static auto isUseOtp(const bool &defaultValue = true) -> bool;
     276              :   /**
     277              :    * @brief Save qrencode executable path.
     278              :    * @param qrencodeExecutable Path to qrencode executable.
     279              :    */
     280              :   static void setQrencodeExecutable(const QString &qrencodeExecutable);
     281              : 
     282              :   /**
     283              :    * @brief Get complete password generation configuration.
     284              :    * @return Password generation configuration.
     285              :    */
     286              :   static auto getPasswordConfiguration() -> PasswordConfiguration;
     287              :   /**
     288              :    * @brief Check whether closing the window hides the application.
     289              :    * @param defaultValue Value returned if not saved.
     290              :    * @return True if close action hides the application.
     291              :    */
     292              :   static auto isHideOnClose(const bool &defaultValue = QVariant().toBool())
     293              :       -> bool;
     294              :   /**
     295              :    * @brief Check whether main window should stay always on top.
     296              :    * @param defaultValue Value returned if not saved.
     297              :    * @return True if always-on-top is enabled.
     298              :    */
     299              :   static auto isAlwaysOnTop(const bool &defaultValue = QVariant().toBool())
     300              :       -> bool;
     301              :   /**
     302              :    * @brief Check whether automatic push is enabled.
     303              :    * @param defaultValue Value returned if not saved.
     304              :    * @return True if automatic push is enabled.
     305              :    */
     306              :   static auto isAutoPush(const bool &defaultValue = QVariant().toBool())
     307              :       -> bool;
     308              :   /**
     309              :    * @brief Get showProcessOutput setting.
     310              :    * @param defaultValue Default value if not set.
     311              :    * @return Whether process output should be displayed.
     312              :    */
     313              :   static auto isShowProcessOutput(const bool &defaultValue = false) -> bool;
     314              :   /**
     315              :    * @brief Get all configured profiles.
     316              :    * @return Profile map keyed by profile name.
     317              :    */
     318              :   static auto getProfiles() -> Profiles;
     319              :   /**
     320              :    * @brief Save all configured profiles.
     321              :    * @param profiles Profile map keyed by profile name.
     322              :    */
     323              :   static void setProfiles(const Profiles &profiles);
     324              : 
     325              :   /**
     326              :    * @brief Get currently active pass backend instance.
     327              :    * @return Pointer to pass backend.
     328              :    */
     329              :   static auto getPass() -> Pass *;
     330              :   /**
     331              :    * @brief Get real pass backend instance.
     332              :    * @return Pointer to real pass backend.
     333              :    */
     334              :   static auto getRealPass() -> RealPass *;
     335              :   /**
     336              :    * @brief Get imitate pass backend instance.
     337              :    * @return Pointer to imitate pass backend.
     338              :    */
     339              :   static auto getImitatePass() -> ImitatePass *;
     340              :   /**
     341              :    * @brief Whether no backend, the active one or one kept from before a
     342              :    * switch, runs or waits for anything (PassBackendFactory::isIdle()).
     343              :    * @return true when all are idle.
     344              :    */
     345              :   static auto backendsIdle() -> bool;
     346              : };
     347              : 
     348              : #endif // SRC_QTPASSSETTINGS_H_
        

Generated by: LCOV version 2.0-1