summaryrefslogtreecommitdiffstats
path: root/src/filecontent.h
blob: 845648f7c5bc934b252a584b062bbd99b3168069 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef FILECONTENT_H
#define FILECONTENT_H

#include <QList>
#include <QString>
#include <QStringList>

struct NamedValue {
  QString name;
  QString value;
};

/**
 * @brief The NamedValues class is mostly a list of NamedValue
 * but also has a method to take a specific NamedValue pair out of the list.
 */
class NamedValues : public QList<NamedValue> {
public:
  NamedValues();
  NamedValues(std::initializer_list<NamedValue> values);

  QString takeValue(const QString &name);
};

class FileContent {
public:
  /**
   * @brief parse parses the given fileContent in a FileContent object.
   * The password is accessible through getPassword.
   * The named value pairs (name: value) are parsed and depeding on the
   * templateFields and allFields parameters accessible through getNamedValues,
   * getRemainingData or getRemainingDataForDisplay.
   *
   * @param fileContent the file content to parse.
   *
   * @param templateFields the fields in the template.
   * Fields in the template will always be in getNamedValues() at the beginning
   * of the list in the same order.
   *
   * @param allFields whether all fields should be considered as named values.
   * If set to false only templateFields are returned in getNamedValues().
   *
   * @return
   */
  static FileContent parse(const QString &fileContent,
                           const QStringList &templateFields, bool allFields);

  /**
   * @return the password from the parsed file.
   */
  QString getPassword() const;

  /**
   * @return the named values in the file in the order of appearence, with
   * template values first.
   */
  NamedValues getNamedValues() const;

  /**
   * @return the data that is not the password and not in getNamedValues.
   */
  QString getRemainingData() const;

  /**
   * @like getRemainingData but without data that should not be displayed
   * (like a TOTP secret).
   */
  QString getRemainingDataForDisplay() const;

private:
  FileContent(const QString &password, const NamedValues &namedValues,
              const QString &remainingData, const QString &remainingDataDisplay);

  QString password;
  NamedValues namedValues;
  QString remainingData, remainingDataDisplay;
};

#endif // FILECONTENT_H