QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
settingsserializer.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
10#include "settingsconstants.h"
11
12#include <QDebug>
13#include <QSettings>
14
18
19 int length = qs.value(SettingsConstants::passwordLength, 16).toInt();
20 if (length <= 0) {
21 length = 16;
22 }
23 config.length = length;
24
25 // The ini value is untrusted: anything outside the enum would be used as an
26 // index into PasswordConfiguration::Characters, so clamp it here, once, and
27 // every consumer sees a valid selection.
28 const int selected =
29 qs.value(SettingsConstants::passwordCharsSelection, 0).toInt();
30 if (selected < 0 || selected >= PasswordConfiguration::CHARSETS_COUNT) {
31 qWarning() << "Ignoring out-of-range passwordCharsSelection" << selected;
33 } else {
34 config.selected =
35 static_cast<PasswordConfiguration::CharacterSet>(selected);
36 }
37
39 qs.value(SettingsConstants::passwordChars, QString()).toString();
40
41 return config;
42}
43
44auto SettingsSerializer::load(QSettings &qs) -> AppSettings {
46
47 // Window / session
48 s.version = qs.value(SettingsConstants::version).toString();
49 s.geometry = qs.value(SettingsConstants::geometry).toByteArray();
50 s.savestate = qs.value(SettingsConstants::savestate).toByteArray();
51 s.activeProfile = qs.value(SettingsConstants::profile).toString();
52
53 // Backend selection / store
54 s.usePass = qs.value(SettingsConstants::usePass, false).toBool();
55 s.passStore = qs.value(SettingsConstants::passStore).toString();
56 s.passSigningKey = qs.value(SettingsConstants::passSigningKey).toString();
57
58 // Executables
59 s.passExecutable = qs.value(SettingsConstants::passExecutable).toString();
60 s.gitExecutable = qs.value(SettingsConstants::gitExecutable).toString();
61 s.gpgExecutable = qs.value(SettingsConstants::gpgExecutable).toString();
62 s.pwgenExecutable = qs.value(SettingsConstants::pwgenExecutable).toString();
64 qs.value(SettingsConstants::qrencodeExecutable).toString();
65 s.gpgHome = qs.value(SettingsConstants::gpgHome).toString();
67 qs.value(SettingsConstants::sshAuthSockOverride).toString();
68
69 // Clipboard / autoclear
70 s.clipBoardType = static_cast<Enums::ClipBoardType>(
71 qs.value(SettingsConstants::clipBoardType,
72 static_cast<int>(Enums::CLIPBOARD_NEVER))
73 .toInt());
74 s.useSelection = qs.value(SettingsConstants::useSelection, false).toBool();
75 s.useAutoclear = qs.value(SettingsConstants::useAutoclear, false).toBool();
77 qs.value(SettingsConstants::restoreClipboard, true).toBool();
78 s.autoclearSeconds = qs.value(SettingsConstants::autoclearSeconds, 0).toInt();
80 qs.value(SettingsConstants::useAutoclearPanel, false).toBool();
82 qs.value(SettingsConstants::autoclearPanelSeconds, 0).toInt();
83
84 // Content display
85 s.hidePassword = qs.value(SettingsConstants::hidePassword, false).toBool();
87 qs.value(SettingsConstants::hideSecretFields, true).toBool();
88 s.hideAllFields = qs.value(SettingsConstants::hideAllFields, false).toBool();
89 // Never saved (a new settings file, or one from 1.8): the default set.
90 // Saved, it is what the user left, an empty list included.
91 if (qs.contains(SettingsConstants::secretFieldNames)) {
92 s.secretFieldNames.clear();
93 for (const QString &name :
94 qs.value(SettingsConstants::secretFieldNames).toStringList()) {
95 if (!name.trimmed().isEmpty()) {
96 s.secretFieldNames << name.trimmed();
97 }
98 }
99 } else {
101 }
102 s.hideContent = qs.value(SettingsConstants::hideContent, false).toBool();
103 s.useMonospace = qs.value(SettingsConstants::useMonospace, false).toBool();
104 s.displayAsIs = qs.value(SettingsConstants::displayAsIs, false).toBool();
106 qs.value(SettingsConstants::noLineWrapping, false).toBool();
107
108 // Features
109 // Default true: AppSettings::addGPGId defaults to true.
110 s.addGPGId = qs.value(SettingsConstants::addGPGId, true).toBool();
111 s.useGit = qs.value(SettingsConstants::useGit, false).toBool();
112 s.useGrepSearch = qs.value(SettingsConstants::useGrepSearch, false).toBool();
113 // Defaults to true: OTP no longer depends on an external extension, and a
114 // stored value from an older profile still wins over this default.
115 s.useOtp = qs.value(SettingsConstants::useOtp, true).toBool();
116 s.useQrencode = qs.value(SettingsConstants::useQrencode, false).toBool();
117 s.usePwgen = qs.value(SettingsConstants::usePwgen, false).toBool();
118 s.autoPull = qs.value(SettingsConstants::autoPull, false).toBool();
119 s.autoPush = qs.value(SettingsConstants::autoPush, false).toBool();
121 qs.value(SettingsConstants::gitUseParentRepository, false).toBool();
123 qs.value(SettingsConstants::showProcessOutput, false).toBool();
124 s.showMenuBar = qs.value(SettingsConstants::showMenuBar, false).toBool();
125
126 // Templates
127 s.passTemplate = qs.value(SettingsConstants::passTemplate).toString();
128 s.useTemplate = qs.value(SettingsConstants::useTemplate, false).toBool();
130 qs.value(SettingsConstants::templateAllFields, false).toBool();
131
132 // Password generation
134 s.avoidCapitals = qs.value(SettingsConstants::avoidCapitals, false).toBool();
135 s.avoidNumbers = qs.value(SettingsConstants::avoidNumbers, false).toBool();
136 s.lessRandom = qs.value(SettingsConstants::lessRandom, false).toBool();
137 s.useSymbols = qs.value(SettingsConstants::useSymbols, false).toBool();
138
139 // System / tray
140 s.useTrayIcon = qs.value(SettingsConstants::useTrayIcon, false).toBool();
141 s.hideOnClose = qs.value(SettingsConstants::hideOnClose, false).toBool();
143 qs.value(SettingsConstants::startMinimized, false).toBool();
144 s.alwaysOnTop = qs.value(SettingsConstants::alwaysOnTop, false).toBool();
145
146 return s;
147}
148
149void SettingsSerializer::save(QSettings &qs, const AppSettings &s) {
150 // Window / session
151 qs.setValue(SettingsConstants::version, s.version);
152 qs.setValue(SettingsConstants::geometry, s.geometry);
153 qs.setValue(SettingsConstants::savestate, s.savestate);
154 qs.setValue(SettingsConstants::profile, s.activeProfile);
155
156 // Backend selection / store
157 qs.setValue(SettingsConstants::usePass, s.usePass);
158 qs.setValue(SettingsConstants::passStore, s.passStore);
159 qs.setValue(SettingsConstants::passSigningKey, s.passSigningKey);
160
161 // Executables
162 qs.setValue(SettingsConstants::passExecutable, s.passExecutable);
163 qs.setValue(SettingsConstants::gitExecutable, s.gitExecutable);
164 qs.setValue(SettingsConstants::gpgExecutable, s.gpgExecutable);
165 qs.setValue(SettingsConstants::pwgenExecutable, s.pwgenExecutable);
166 qs.setValue(SettingsConstants::qrencodeExecutable, s.qrencodeExecutable);
167 qs.setValue(SettingsConstants::gpgHome, s.gpgHome);
168 qs.setValue(SettingsConstants::sshAuthSockOverride, s.sshAuthSockOverride);
169
170 // Clipboard / autoclear
171 qs.setValue(SettingsConstants::clipBoardType,
172 static_cast<int>(s.clipBoardType));
173 qs.setValue(SettingsConstants::useSelection, s.useSelection);
174 qs.setValue(SettingsConstants::useAutoclear, s.useAutoclear);
175 qs.setValue(SettingsConstants::restoreClipboard, s.restoreClipboard);
176 qs.setValue(SettingsConstants::autoclearSeconds, s.autoclearSeconds);
177 qs.setValue(SettingsConstants::useAutoclearPanel, s.useAutoclearPanel);
178 qs.setValue(SettingsConstants::autoclearPanelSeconds,
180
181 // Content display
182 qs.setValue(SettingsConstants::hidePassword, s.hidePassword);
183 qs.setValue(SettingsConstants::hideSecretFields, s.hideSecretFields);
184 qs.setValue(SettingsConstants::hideAllFields, s.hideAllFields);
185 qs.setValue(SettingsConstants::secretFieldNames, s.secretFieldNames);
186 qs.setValue(SettingsConstants::hideContent, s.hideContent);
187 qs.setValue(SettingsConstants::useMonospace, s.useMonospace);
188 qs.setValue(SettingsConstants::displayAsIs, s.displayAsIs);
189 qs.setValue(SettingsConstants::noLineWrapping, s.noLineWrapping);
190
191 // Features
192 qs.setValue(SettingsConstants::addGPGId, s.addGPGId);
193 qs.setValue(SettingsConstants::useGit, s.useGit);
194 qs.setValue(SettingsConstants::useGrepSearch, s.useGrepSearch);
195 qs.setValue(SettingsConstants::useOtp, s.useOtp);
196 qs.setValue(SettingsConstants::useQrencode, s.useQrencode);
197 qs.setValue(SettingsConstants::usePwgen, s.usePwgen);
198 // The WebDAV mount was removed in 2.0; drop its keys, including the
199 // plaintext webDavPassword, from configurations written by older versions.
200 for (const auto &obsolete :
201 {QStringLiteral("useWebDav"), QStringLiteral("webDavUrl"),
202 QStringLiteral("webDavUser"), QStringLiteral("webDavPassword")}) {
203 qs.remove(obsolete);
204 }
205 qs.setValue(SettingsConstants::autoPull, s.autoPull);
206 qs.setValue(SettingsConstants::autoPush, s.autoPush);
207 qs.setValue(SettingsConstants::gitUseParentRepository,
209 qs.setValue(SettingsConstants::showProcessOutput, s.showProcessOutput);
210 qs.setValue(SettingsConstants::showMenuBar, s.showMenuBar);
211
212 // Templates
213 qs.setValue(SettingsConstants::passTemplate, s.passTemplate);
214 qs.setValue(SettingsConstants::useTemplate, s.useTemplate);
215 qs.setValue(SettingsConstants::templateAllFields, s.templateAllFields);
216
217 // Password generation
218 qs.setValue(SettingsConstants::passwordLength,
220 qs.setValue(SettingsConstants::passwordCharsSelection,
221 static_cast<int>(s.passwordConfiguration.selected));
222 qs.setValue(
223 SettingsConstants::passwordChars,
225 qs.setValue(SettingsConstants::avoidCapitals, s.avoidCapitals);
226 qs.setValue(SettingsConstants::avoidNumbers, s.avoidNumbers);
227 qs.setValue(SettingsConstants::lessRandom, s.lessRandom);
228 qs.setValue(SettingsConstants::useSymbols, s.useSymbols);
229
230 // System / tray
231 qs.setValue(SettingsConstants::useTrayIcon, s.useTrayIcon);
232 qs.setValue(SettingsConstants::hideOnClose, s.hideOnClose);
233 qs.setValue(SettingsConstants::startMinimized, s.startMinimized);
234 qs.setValue(SettingsConstants::alwaysOnTop, s.alwaysOnTop);
235}
static auto loadPasswordConfiguration(QSettings &qs) -> PasswordConfiguration
Read only the password generation settings from a QSettings store.
static void save(QSettings &qs, const AppSettings &settings)
Write all flat settings to a QSettings store.
static auto load(QSettings &qs) -> AppSettings
Read all flat settings from a QSettings store.
ClipBoardType
Defines when to copy passwords to clipboard.
Definition enums.h:24
@ CLIPBOARD_NEVER
Definition enums.h:25
SettingsSerializer: the flat AppSettings fields to and from their QSettings keys, without side effect...
Plain value-object holding all flat QtPass configuration.
Definition appsettings.h:36
bool displayAsIs
Show file content unmodified.
Definition appsettings.h:97
QByteArray geometry
Main-window saved geometry.
Definition appsettings.h:54
bool useGrepSearch
Enable content (grep) search.
QString gpgExecutable
Path to the gpg/gpg2 executable.
Definition appsettings.h:66
QString qrencodeExecutable
Path to the qrencode executable.
Definition appsettings.h:68
QString sshAuthSockOverride
Manual SSH_AUTH_SOCK override path.
Definition appsettings.h:70
bool lessRandom
Generate memorable (less random) passwords.
bool useSymbols
Include special symbols.
bool hideOnClose
Hide to tray instead of quitting on close.
QStringList secretFieldNames
Definition appsettings.h:91
QString passSigningKey
GPG key used to sign .gpg-id files.
Definition appsettings.h:61
bool usePass
Use the pass CLI (true) or native git/gpg.
Definition appsettings.h:59
QString pwgenExecutable
Path to the pwgen executable.
Definition appsettings.h:67
bool hideContent
Hide the entire content panel.
Definition appsettings.h:95
bool useOtp
Enable native TOTP (OTP) support.
QString passStore
Password-store root path.
Definition appsettings.h:60
bool hidePassword
Definition appsettings.h:85
bool useAutoclearPanel
Clear the content panel after a delay.
Definition appsettings.h:81
bool noLineWrapping
Disable line wrapping in the panel.
Definition appsettings.h:98
bool showMenuBar
Show the menu bar (Ctrl+M toggles it).
QString gitExecutable
Path to the git executable.
Definition appsettings.h:65
QString gpgHome
GPG home directory (read-only externally).
Definition appsettings.h:69
static auto defaultSecretFieldNames() -> QStringList
The secret field names a new or emptied setting starts with: what an entry commonly holds besides its...
Definition appsettings.h:42
bool autoPull
Automatically git pull on open.
bool alwaysOnTop
Keep the main window always on top.
int autoclearPanelSeconds
Panel autoclear delay (seconds).
Definition appsettings.h:82
bool hideSecretFields
Definition appsettings.h:88
bool showProcessOutput
Show external process output.
bool startMinimized
Start the window minimised.
bool usePwgen
Use pwgen for password generation.
bool useTrayIcon
Show a system tray icon.
bool gitUseParentRepository
QString passExecutable
Path to the pass executable.
Definition appsettings.h:64
bool avoidCapitals
Exclude uppercase letters.
int autoclearSeconds
Clipboard autoclear delay (seconds).
Definition appsettings.h:80
bool useSelection
Use the X11 primary selection.
Definition appsettings.h:75
bool useGit
Enable git integration.
bool restoreClipboard
Definition appsettings.h:79
bool addGPGId
Auto-add .gpg-id files to git.
Enums::ClipBoardType clipBoardType
Clipboard copy behaviour.
Definition appsettings.h:73
bool hideAllFields
Definition appsettings.h:94
QString version
Last-run application version.
Definition appsettings.h:53
QByteArray savestate
Main-window saved dock/toolbar state.
Definition appsettings.h:55
QString passTemplate
Newline-separated template field names.
bool useAutoclear
Definition appsettings.h:76
bool useMonospace
Render content in a monospace font.
Definition appsettings.h:96
bool templateAllFields
Treat every key: line as a field.
bool avoidNumbers
Exclude digits.
bool useQrencode
Enable qrencode support.
QString activeProfile
Name of the active profile.
Definition appsettings.h:56
bool useTemplate
Use the password template.
PasswordConfiguration passwordConfiguration
Length/charset/custom chars.
Holds the password generator settings: which character set is selected, the alphabet of every set,...
int length
Length of the generated password, in characters.
CharacterSet selected
Currently active character set selection.
QString Characters[CHARSETS_COUNT]
The alphabet of each character set, indexed by CharacterSet. Only the CUSTOM entry is meant to be cha...
CharacterSet
Character set options for password generation; also the index into Characters.