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 One named password store (Profile) and the name-to-profile map
6 : * (Profiles) the settings trade in.
7 : */
8 : #ifndef SRC_PROFILE_H_
9 : #define SRC_PROFILE_H_
10 :
11 : #include <QMap>
12 : #include <QString>
13 : #include <optional>
14 :
15 : /**
16 : * @struct Profile
17 : * @ingroup settings
18 : * @brief One named password store: where it is, how its .gpg-id is signed
19 : * and, optionally, its own Git switches.
20 : *
21 : * The Git flags are tri-state on disk ("" / "true" / "false"): unset means
22 : * "follow the global setting", which is what every profile written before
23 : * 1.8.0 has. Keys are stored under `profile/<name>/` and are unchanged from
24 : * the QHash-of-QHash days.
25 : */
26 : struct Profile {
27 : QString path; ///< Password store directory.
28 : QString signingKey; ///< Key(s) that sign this store's .gpg-id.
29 : std::optional<bool> useGit; ///< Git on/off; unset follows the global.
30 : std::optional<bool> autoPush; ///< Push after commits; unset = global.
31 : std::optional<bool> autoPull; ///< Pull on start; unset = global.
32 :
33 : /**
34 : * @brief Parse the on-disk tri-state.
35 : * @param value "true", "false" or anything else (unset).
36 : * @return The flag, or nullopt when unset.
37 : */
38 177 : static auto flagFromString(const QString &value) -> std::optional<bool> {
39 177 : if (value == QLatin1String("true")) {
40 2 : return true;
41 : }
42 175 : if (value == QLatin1String("false")) {
43 4 : return false;
44 : }
45 171 : return std::nullopt;
46 : }
47 :
48 : /**
49 : * @brief Format a flag the way it is stored.
50 : * @param flag The flag.
51 : * @return "true", "false" or "" for unset.
52 : */
53 132 : static auto flagToString(std::optional<bool> flag) -> QString {
54 132 : if (!flag.has_value()) {
55 127 : return {};
56 : }
57 7 : return *flag ? QStringLiteral("true") : QStringLiteral("false");
58 : }
59 :
60 : /**
61 : * @brief Field-wise equality, including the unset state of each flag.
62 : * @param other Profile to compare with.
63 : * @return true when every field matches.
64 : */
65 : auto operator==(const Profile &other) const -> bool {
66 : return path == other.path && signingKey == other.signingKey &&
67 : useGit == other.useGit && autoPush == other.autoPush &&
68 : autoPull == other.autoPull;
69 : }
70 : /**
71 : * @brief Negation of operator==.
72 : * @param other Profile to compare with.
73 : * @return true when any field differs.
74 : */
75 : auto operator!=(const Profile &other) const -> bool {
76 : return !(*this == other);
77 : }
78 : };
79 :
80 : /// All profiles by name. QMap so iteration order is the name order.
81 : using Profiles = QMap<QString, Profile>;
82 :
83 : #endif // SRC_PROFILE_H_
|