QtPass  1.3.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 static bool isLineHidden(const QString &line) {
4  return line.startsWith("otpauth://", Qt::CaseInsensitive);
5 }
6 
7 FileContent FileContent::parse(const QString &fileContent,
8  const QStringList &templateFields,
9  bool allFields) {
10  QStringList lines = fileContent.split("\n");
11  QString password = lines.takeFirst();
12  QStringList remainingData, remainingDataDisplay;
13  NamedValues namedValues;
14  for (const QString &line : lines) {
15  if (line.contains(":")) {
16  int colon = line.indexOf(':');
17  QString name = line.left(colon);
18  QString value = line.right(line.length() - colon - 1);
19  if ((allFields &&
20  !value.startsWith(
21  "//")) // if value startswith // colon is probably from a url
22  || templateFields.contains(name)) {
23  namedValues.append({name.trimmed(), value.trimmed()});
24  continue;
25  }
26  }
27 
28  remainingData.append(line);
29  if (!isLineHidden(line))
30  remainingDataDisplay.append(line);
31  }
32  return FileContent(password, namedValues, remainingData.join("\n"),
33  remainingDataDisplay.join("\n"));
34 }
35 
36 QString FileContent::getPassword() const { return this->password; }
37 
38 NamedValues FileContent::getNamedValues() const { return this->namedValues; }
39 
40 QString FileContent::getRemainingData() const { return this->remainingData; }
41 
43  return this->remainingDataDisplay;
44 }
45 
46 FileContent::FileContent(const QString &password,
47  const NamedValues &namedValues,
48  const QString &remainingData,
49  const QString &remainingDataDisplay)
50  : password(password), namedValues(namedValues),
51  remainingData(remainingData), remainingDataDisplay(remainingDataDisplay) {
52 }
53 
55 
56 NamedValues::NamedValues(std::initializer_list<NamedValue> values)
57  : QList(values) {}
58 
59 QString NamedValues::takeValue(const QString &name) {
60  for (int i = 0; i < length(); ++i) {
61  if (at(i).name == name) {
62  return takeAt(i).value;
63  }
64  }
65  return QString();
66 }
FileContent::getNamedValues
NamedValues getNamedValues() const
Definition: filecontent.cpp:38
NamedValues
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
Definition: filecontent.h:17
filecontent.h
FileContent
Definition: filecontent.h:25
FileContent::getRemainingData
QString getRemainingData() const
Definition: filecontent.cpp:40
NamedValues::takeValue
QString takeValue(const QString &name)
Definition: filecontent.cpp:59
NamedValues::NamedValues
NamedValues()
Definition: filecontent.cpp:54
FileContent::getRemainingDataForDisplay
QString getRemainingDataForDisplay() const
Definition: filecontent.cpp:42
FileContent::getPassword
QString getPassword() const
Definition: filecontent.cpp:36
FileContent::parse
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:7