Line data Source code
1 : // SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : #ifndef SRC_FILECONTENT_H_
4 : #define SRC_FILECONTENT_H_
5 :
6 : #include <QList>
7 : #include <QString>
8 : #include <QStringList>
9 :
10 1774 : struct NamedValue {
11 : QString name;
12 : QString value;
13 : };
14 :
15 : /**
16 : * @brief The NamedValues class is mostly a list of NamedValue
17 : * but also has a method to take a specific NamedValue pair out of the list.
18 : */
19 1908 : class NamedValues : public QList<NamedValue> {
20 : public:
21 : NamedValues();
22 : NamedValues(std::initializer_list<NamedValue> values);
23 :
24 : auto takeValue(const QString &name) -> QString;
25 : };
26 :
27 : class FileContent {
28 : public:
29 : /**
30 : * @brief parse parses the given fileContent in a FileContent object.
31 : * The password is accessible through getPassword.
32 : * The named value pairs (name: value) are parsed and depeding on the
33 : * templateFields and allFields parameters accessible through getNamedValues,
34 : * getRemainingData or getRemainingDataForDisplay.
35 : *
36 : * @param fileContent the file content to parse.
37 : *
38 : * @param templateFields the fields in the template.
39 : * Fields in the template will always be in getNamedValues() at the beginning
40 : * of the list in the same order.
41 : *
42 : * @param allFields whether all fields should be considered as named values.
43 : * If set to false only templateFields are returned in getNamedValues().
44 : *
45 : * @return
46 : */
47 : static auto parse(const QString &fileContent,
48 : const QStringList &templateFields, bool allFields)
49 : -> FileContent;
50 :
51 : /**
52 : * @return the password from the parsed file.
53 : */
54 : [[nodiscard]] auto getPassword() const -> QString;
55 :
56 : /**
57 : * @return the named values in the file in the order of appearence, with
58 : * template values first.
59 : */
60 : [[nodiscard]] auto getNamedValues() const -> NamedValues;
61 :
62 : /**
63 : * @return the data that is not the password and not in getNamedValues.
64 : */
65 : [[nodiscard]] auto getRemainingData() const -> QString;
66 :
67 : /**
68 : * @like getRemainingData but without data that should not be displayed
69 : * (like a TOTP secret).
70 : */
71 : [[nodiscard]] auto getRemainingDataForDisplay() const -> QString;
72 :
73 : private:
74 : FileContent(QString password, NamedValues namedValues, QString remainingData,
75 : QString remainingDataDisplay);
76 :
77 : QString password;
78 : NamedValues namedValues;
79 : QString remainingData, remainingDataDisplay;
80 : };
81 :
82 : #endif // SRC_FILECONTENT_H_
|