QtPass  1.2.3
Multi-platform GUI for pass, the standard unix password manager.
filecontent.cpp
Go to the documentation of this file.
1 #include "filecontent.h"
2 
3 FileContent FileContent::parse(const QString &fileContent,
4  const QStringList &templateFields,
5  bool allFields) {
6  QStringList lines = fileContent.split("\n");
7  QString password = lines.takeFirst();
8  QStringList remainingData;
9  NamedValues namedValues;
10  for (QString line : lines) {
11  if (line.contains(":")) {
12  int colon = line.indexOf(':');
13  QString name = line.left(colon);
14  QString value = line.right(line.length() - colon - 1);
15  if ((allFields &&
16  !value.startsWith(
17  "//")) // if value startswith // colon is probably from a url
18  || templateFields.contains(name)) {
19  namedValues.append({name.trimmed(), value.trimmed()});
20  continue;
21  }
22  }
23  remainingData.append(line);
24  }
25  return FileContent(password, namedValues, remainingData.join("\n"));
26 }
27 
28 QString FileContent::getPassword() const { return this->password; }
29 
30 NamedValues FileContent::getNamedValues() const { return this->namedValues; }
31 
32 QString FileContent::getRemainingData() const { return this->remainingData; }
33 
34 FileContent::FileContent(const QString &password,
35  const NamedValues &namedValues,
36  const QString &remainingData)
37  : password(password), namedValues(namedValues),
38  remainingData(remainingData) {}
39 
41 
42 NamedValues::NamedValues(std::initializer_list<NamedValue> values)
43  : QList(values) {}
44 
45 QString NamedValues::takeValue(const QString &name) {
46  for (int i = 0; i < length(); ++i) {
47  if (at(i).name == name) {
48  return takeAt(i).value;
49  }
50  }
51  return QString();
52 }
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
Definition: filecontent.h:17
NamedValues getNamedValues() const
Definition: filecontent.cpp:30
QString takeValue(const QString &name)
Definition: filecontent.cpp:45
static FileContent parse(const QString &fileContent, const QStringList &templateFields, bool allFields)
parse parses the given fileContent in a FileContent object. The password is accessible through getPas...
Definition: filecontent.cpp:3
QString getRemainingData() const
Definition: filecontent.cpp:32
QString getPassword() const
Definition: filecontent.cpp:28