QtPass 1.4.0
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
filecontent.cpp
Go to the documentation of this file.
1#include "filecontent.h"
2
3static bool isLineHidden(const QString &line) {
4 return line.startsWith("otpauth://", Qt::CaseInsensitive);
5}
6
7FileContent 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 : qAsConst(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
36QString FileContent::getPassword() const { return this->password; }
37
38NamedValues FileContent::getNamedValues() const { return this->namedValues; }
39
40QString FileContent::getRemainingData() const { return this->remainingData; }
41
43 return this->remainingDataDisplay;
44}
45
46FileContent::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
56NamedValues::NamedValues(std::initializer_list<NamedValue> values)
57 : QList(values) {}
58
59QString 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}
QString getRemainingData() const
Definition: filecontent.cpp:40
QString getRemainingDataForDisplay() const
Definition: filecontent.cpp:42
QString getPassword() const
Definition: filecontent.cpp:36
NamedValues getNamedValues() const
Definition: filecontent.cpp:38
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
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
Definition: filecontent.h:17
QString takeValue(const QString &name)
Definition: filecontent.cpp:59