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 SettingsSerializer: one SettingsConstants key per
6 : : * AppSettings field, with the defaults applied in load() and the obsolete
7 : : * WebDAV keys dropped in save().
8 : : */
9 : : #include "settingsserializer.h"
10 : : #include "settingsconstants.h"
11 : :
12 : : #include <QDebug>
13 : : #include <QSettings>
14 : :
15 : 3908 : auto SettingsSerializer::loadPasswordConfiguration(QSettings &qs)
16 : : -> PasswordConfiguration {
17 : 3908 : PasswordConfiguration config;
18 : :
19 : 3908 : int length = qs.value(SettingsConstants::passwordLength, 16).toInt();
20 [ - + ]: 3908 : if (length <= 0) {
21 : : length = 16;
22 : : }
23 : 3908 : 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 : 3908 : qs.value(SettingsConstants::passwordCharsSelection, 0).toInt();
30 [ + + ]: 3908 : if (selected < 0 || selected >= PasswordConfiguration::CHARSETS_COUNT) {
31 : 8 : qWarning() << "Ignoring out-of-range passwordCharsSelection" << selected;
32 : 8 : config.selected = PasswordConfiguration::ALLCHARS;
33 : : } else {
34 : 3900 : config.selected =
35 : : static_cast<PasswordConfiguration::CharacterSet>(selected);
36 : : }
37 : :
38 : : config.Characters[PasswordConfiguration::CUSTOM] =
39 : 7816 : qs.value(SettingsConstants::passwordChars, QString()).toString();
40 : :
41 : 3908 : return config;
42 : : }
43 : :
44 : 3893 : auto SettingsSerializer::load(QSettings &qs) -> AppSettings {
45 : 3893 : AppSettings s;
46 : :
47 : : // Window / session
48 : 7786 : s.version = qs.value(SettingsConstants::version).toString();
49 : 7786 : s.geometry = qs.value(SettingsConstants::geometry).toByteArray();
50 : 7786 : s.savestate = qs.value(SettingsConstants::savestate).toByteArray();
51 : 7786 : s.activeProfile = qs.value(SettingsConstants::profile).toString();
52 : :
53 : : // Backend selection / store
54 : 3893 : s.usePass = qs.value(SettingsConstants::usePass, false).toBool();
55 : 7786 : s.passStore = qs.value(SettingsConstants::passStore).toString();
56 : 7786 : s.passSigningKey = qs.value(SettingsConstants::passSigningKey).toString();
57 : :
58 : : // Executables
59 : 7786 : s.passExecutable = qs.value(SettingsConstants::passExecutable).toString();
60 : 7786 : s.gitExecutable = qs.value(SettingsConstants::gitExecutable).toString();
61 : 7786 : s.gpgExecutable = qs.value(SettingsConstants::gpgExecutable).toString();
62 : 7786 : s.pwgenExecutable = qs.value(SettingsConstants::pwgenExecutable).toString();
63 : : s.qrencodeExecutable =
64 : 7786 : qs.value(SettingsConstants::qrencodeExecutable).toString();
65 : 7786 : s.gpgHome = qs.value(SettingsConstants::gpgHome).toString();
66 : : s.sshAuthSockOverride =
67 : 7786 : qs.value(SettingsConstants::sshAuthSockOverride).toString();
68 : :
69 : : // Clipboard / autoclear
70 : 3893 : s.clipBoardType = static_cast<Enums::ClipBoardType>(
71 : 7786 : qs.value(SettingsConstants::clipBoardType,
72 : 7786 : static_cast<int>(Enums::CLIPBOARD_NEVER))
73 : 3893 : .toInt());
74 : 3893 : s.useSelection = qs.value(SettingsConstants::useSelection, false).toBool();
75 : 3893 : s.useAutoclear = qs.value(SettingsConstants::useAutoclear, false).toBool();
76 : 3893 : s.restoreClipboard =
77 : 3893 : qs.value(SettingsConstants::restoreClipboard, true).toBool();
78 : 3893 : s.autoclearSeconds = qs.value(SettingsConstants::autoclearSeconds, 0).toInt();
79 : 3893 : s.useAutoclearPanel =
80 : 3893 : qs.value(SettingsConstants::useAutoclearPanel, false).toBool();
81 : 3893 : s.autoclearPanelSeconds =
82 : 3893 : qs.value(SettingsConstants::autoclearPanelSeconds, 0).toInt();
83 : :
84 : : // Content display
85 : 3893 : s.hidePassword = qs.value(SettingsConstants::hidePassword, false).toBool();
86 : 3893 : s.hideSecretFields =
87 : 3893 : qs.value(SettingsConstants::hideSecretFields, true).toBool();
88 : 3893 : 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 [ + + + ]: 3893 : if (qs.contains(SettingsConstants::secretFieldNames)) {
92 : 3612 : s.secretFieldNames.clear();
93 : : const QStringList names =
94 : 3612 : qs.value(SettingsConstants::secretFieldNames).toStringList();
95 [ + + ]: 52312 : for (const QString &name : names) {
96 [ + + ]: 48700 : if (!name.trimmed().isEmpty()) {
97 : 48699 : s.secretFieldNames << name.trimmed();
98 : : }
99 : : }
100 : : } else {
101 : 562 : s.secretFieldNames = AppSettings::defaultSecretFieldNames();
102 : : }
103 : 3893 : s.hideContent = qs.value(SettingsConstants::hideContent, false).toBool();
104 : 3893 : s.useMonospace = qs.value(SettingsConstants::useMonospace, false).toBool();
105 : 3893 : s.displayAsIs = qs.value(SettingsConstants::displayAsIs, false).toBool();
106 : 3893 : s.noLineWrapping =
107 : 3893 : qs.value(SettingsConstants::noLineWrapping, false).toBool();
108 : :
109 : : // Features
110 : : // Default true: AppSettings::addGPGId defaults to true.
111 : 3893 : s.addGPGId = qs.value(SettingsConstants::addGPGId, true).toBool();
112 : 3893 : s.useGit = qs.value(SettingsConstants::useGit, false).toBool();
113 : 3893 : s.useGrepSearch = qs.value(SettingsConstants::useGrepSearch, false).toBool();
114 : : // Defaults to true: OTP no longer depends on an external extension, and a
115 : : // stored value from an older profile still wins over this default.
116 : 3893 : s.useOtp = qs.value(SettingsConstants::useOtp, true).toBool();
117 : 3893 : s.useQrencode = qs.value(SettingsConstants::useQrencode, false).toBool();
118 : 3893 : s.usePwgen = qs.value(SettingsConstants::usePwgen, false).toBool();
119 : 3893 : s.autoPull = qs.value(SettingsConstants::autoPull, false).toBool();
120 : 3893 : s.autoPush = qs.value(SettingsConstants::autoPush, false).toBool();
121 : 3893 : s.gitUseParentRepository =
122 : 3893 : qs.value(SettingsConstants::gitUseParentRepository, false).toBool();
123 : 3893 : s.showProcessOutput =
124 : 3893 : qs.value(SettingsConstants::showProcessOutput, false).toBool();
125 : 3893 : s.showMenuBar = qs.value(SettingsConstants::showMenuBar, false).toBool();
126 : :
127 : : // Templates
128 : 7786 : s.passTemplate = qs.value(SettingsConstants::passTemplate).toString();
129 : 3893 : s.useTemplate = qs.value(SettingsConstants::useTemplate, false).toBool();
130 : 3893 : s.templateAllFields =
131 : 7786 : qs.value(SettingsConstants::templateAllFields, false).toBool();
132 : :
133 : : // Password generation
134 : 3893 : s.passwordConfiguration = loadPasswordConfiguration(qs);
135 : 3893 : s.avoidCapitals = qs.value(SettingsConstants::avoidCapitals, false).toBool();
136 : 3893 : s.avoidNumbers = qs.value(SettingsConstants::avoidNumbers, false).toBool();
137 : 3893 : s.lessRandom = qs.value(SettingsConstants::lessRandom, false).toBool();
138 : 3893 : s.useSymbols = qs.value(SettingsConstants::useSymbols, false).toBool();
139 : :
140 : : // System / tray
141 : 3893 : s.useTrayIcon = qs.value(SettingsConstants::useTrayIcon, false).toBool();
142 : 3893 : s.hideOnClose = qs.value(SettingsConstants::hideOnClose, false).toBool();
143 : 3893 : s.startMinimized =
144 : 3893 : qs.value(SettingsConstants::startMinimized, false).toBool();
145 : 3893 : s.alwaysOnTop = qs.value(SettingsConstants::alwaysOnTop, false).toBool();
146 : :
147 : 3893 : return s;
148 : 0 : }
149 : :
150 : 1063 : void SettingsSerializer::save(QSettings &qs, const AppSettings &s) {
151 : : // Window / session
152 : 1063 : qs.setValue(SettingsConstants::version, s.version);
153 : 1063 : qs.setValue(SettingsConstants::geometry, s.geometry);
154 : 1063 : qs.setValue(SettingsConstants::savestate, s.savestate);
155 : 1063 : qs.setValue(SettingsConstants::profile, s.activeProfile);
156 : :
157 : : // Backend selection / store
158 : 1063 : qs.setValue(SettingsConstants::usePass, s.usePass);
159 : 1063 : qs.setValue(SettingsConstants::passStore, s.passStore);
160 : 1063 : qs.setValue(SettingsConstants::passSigningKey, s.passSigningKey);
161 : :
162 : : // Executables
163 : 1063 : qs.setValue(SettingsConstants::passExecutable, s.passExecutable);
164 : 1063 : qs.setValue(SettingsConstants::gitExecutable, s.gitExecutable);
165 : 1063 : qs.setValue(SettingsConstants::gpgExecutable, s.gpgExecutable);
166 : 1063 : qs.setValue(SettingsConstants::pwgenExecutable, s.pwgenExecutable);
167 : 1063 : qs.setValue(SettingsConstants::qrencodeExecutable, s.qrencodeExecutable);
168 : 1063 : qs.setValue(SettingsConstants::gpgHome, s.gpgHome);
169 : 1063 : qs.setValue(SettingsConstants::sshAuthSockOverride, s.sshAuthSockOverride);
170 : :
171 : : // Clipboard / autoclear
172 : 1063 : qs.setValue(SettingsConstants::clipBoardType,
173 : 2126 : static_cast<int>(s.clipBoardType));
174 : 1063 : qs.setValue(SettingsConstants::useSelection, s.useSelection);
175 : 1063 : qs.setValue(SettingsConstants::useAutoclear, s.useAutoclear);
176 : 1063 : qs.setValue(SettingsConstants::restoreClipboard, s.restoreClipboard);
177 : 1063 : qs.setValue(SettingsConstants::autoclearSeconds, s.autoclearSeconds);
178 : 1063 : qs.setValue(SettingsConstants::useAutoclearPanel, s.useAutoclearPanel);
179 : 1063 : qs.setValue(SettingsConstants::autoclearPanelSeconds,
180 : 2126 : s.autoclearPanelSeconds);
181 : :
182 : : // Content display
183 : 1063 : qs.setValue(SettingsConstants::hidePassword, s.hidePassword);
184 : 1063 : qs.setValue(SettingsConstants::hideSecretFields, s.hideSecretFields);
185 : 1063 : qs.setValue(SettingsConstants::hideAllFields, s.hideAllFields);
186 : 1063 : qs.setValue(SettingsConstants::secretFieldNames, s.secretFieldNames);
187 : 1063 : qs.setValue(SettingsConstants::hideContent, s.hideContent);
188 : 1063 : qs.setValue(SettingsConstants::useMonospace, s.useMonospace);
189 : 1063 : qs.setValue(SettingsConstants::displayAsIs, s.displayAsIs);
190 : 1063 : qs.setValue(SettingsConstants::noLineWrapping, s.noLineWrapping);
191 : :
192 : : // Features
193 : 1063 : qs.setValue(SettingsConstants::addGPGId, s.addGPGId);
194 : 1063 : qs.setValue(SettingsConstants::useGit, s.useGit);
195 : 1063 : qs.setValue(SettingsConstants::useGrepSearch, s.useGrepSearch);
196 : 1063 : qs.setValue(SettingsConstants::useOtp, s.useOtp);
197 : 1063 : qs.setValue(SettingsConstants::useQrencode, s.useQrencode);
198 : 1063 : qs.setValue(SettingsConstants::usePwgen, s.usePwgen);
199 : : // The WebDAV mount was removed in 2.0; drop its keys, including the
200 : : // plaintext webDavPassword, from configurations written by older versions.
201 : 5315 : for (const auto &obsolete :
202 : 1063 : {QStringLiteral("useWebDav"), QStringLiteral("webDavUrl"),
203 [ + + ]: 10630 : QStringLiteral("webDavUser"), QStringLiteral("webDavPassword")}) {
204 : 4252 : qs.remove(obsolete);
205 : 5315 : }
206 : 1063 : qs.setValue(SettingsConstants::autoPull, s.autoPull);
207 : 1063 : qs.setValue(SettingsConstants::autoPush, s.autoPush);
208 : 1063 : qs.setValue(SettingsConstants::gitUseParentRepository,
209 : 2126 : s.gitUseParentRepository);
210 : 1063 : qs.setValue(SettingsConstants::showProcessOutput, s.showProcessOutput);
211 : 1063 : qs.setValue(SettingsConstants::showMenuBar, s.showMenuBar);
212 : :
213 : : // Templates
214 : 1063 : qs.setValue(SettingsConstants::passTemplate, s.passTemplate);
215 : 1063 : qs.setValue(SettingsConstants::useTemplate, s.useTemplate);
216 : 1063 : qs.setValue(SettingsConstants::templateAllFields, s.templateAllFields);
217 : :
218 : : // Password generation
219 : 1063 : qs.setValue(SettingsConstants::passwordLength,
220 : 2126 : s.passwordConfiguration.length);
221 : 1063 : qs.setValue(SettingsConstants::passwordCharsSelection,
222 : 2126 : static_cast<int>(s.passwordConfiguration.selected));
223 : 1063 : qs.setValue(
224 : : SettingsConstants::passwordChars,
225 : : s.passwordConfiguration.Characters[PasswordConfiguration::CUSTOM]);
226 : 1063 : qs.setValue(SettingsConstants::avoidCapitals, s.avoidCapitals);
227 : 1063 : qs.setValue(SettingsConstants::avoidNumbers, s.avoidNumbers);
228 : 1063 : qs.setValue(SettingsConstants::lessRandom, s.lessRandom);
229 : 1063 : qs.setValue(SettingsConstants::useSymbols, s.useSymbols);
230 : :
231 : : // System / tray
232 : 1063 : qs.setValue(SettingsConstants::useTrayIcon, s.useTrayIcon);
233 : 1063 : qs.setValue(SettingsConstants::hideOnClose, s.hideOnClose);
234 : 1063 : qs.setValue(SettingsConstants::startMinimized, s.startMinimized);
235 : 1063 : qs.setValue(SettingsConstants::alwaysOnTop, s.alwaysOnTop);
236 : 1063 : }
|