QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
filecontent.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2018 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8#ifndef SRC_FILECONTENT_H_
9#define SRC_FILECONTENT_H_
10
11#include <QList>
12#include <QString>
13#include <QStringList>
14#include <functional>
15
21struct NamedValue {
25 QString name;
29 QString value;
38 QString originalLine;
39};
40
49inline bool operator==(const NamedValue &a, const NamedValue &b) {
50 return a.name == b.name && a.value == b.value;
51}
52
58class NamedValues : public QList<NamedValue> {
59public:
66 NamedValues(std::initializer_list<NamedValue> values);
67
73 auto takeValue(const QString &name) -> QString;
74
81 auto takeNamedValue(const QString &name) -> NamedValue;
82};
83
93public:
103 static constexpr int kMaxNamedFields = 1000;
104
114 static constexpr int kMaxDisplayLines = 10000;
115
124 [[nodiscard]] static auto boundedForDisplay(const QString &text) -> QString;
125
149 static auto parse(const QString &fileContent,
150 const QStringList &templateFields, bool allFields,
151 const std::function<bool(const QString &)> &alsoField = {})
152 -> FileContent;
153
157 [[nodiscard]] auto getPassword() const -> QString;
158
169 [[nodiscard]] auto getPasswordForDisplay() const -> QString;
170
175 [[nodiscard]] auto getNamedValues() const -> NamedValues;
176
180 [[nodiscard]] auto getRemainingData() const -> QString;
181
187 [[nodiscard]] auto getRemainingDataForDisplay() const -> QString;
188
193 [[nodiscard]] auto getRemainingLineCount() const -> qsizetype;
194
201 [[nodiscard]] static auto isOtpFieldName(const QString &name) -> bool;
202
211 [[nodiscard]] static auto isSecretFieldName(const QString &name,
212 const QStringList &secrets)
213 -> bool;
214
224 [[nodiscard]] static auto isOtpUriValue(const QString &value) -> bool;
225
242 [[nodiscard]] auto getOtpUri() const -> QString;
243
244private:
256 QString remainingDataDisplay, QString otpUri,
257 qsizetype remainingLines);
258
260 QString password;
268 QString otpUri;
270 qsizetype remainingLines = 0;
271};
272
273#endif // SRC_FILECONTENT_H_
Represents the parsed contents of a password file.
Definition filecontent.h:92
static auto boundedForDisplay(const QString &text) -> QString
text cut to kMaxDisplayLines lines, as getRemainingDataForDisplay() is: for showing an entry as it is...
FileContent(QString password, NamedValues namedValues, QString remainingData, QString remainingDataDisplay, QString otpUri, qsizetype remainingLines)
Assemble a parsed entry; only parse() calls this.
qsizetype remainingLines
Number of lines in remainingData.
QString remainingData
Body lines that are not fields, joined with newlines; complete.
auto getRemainingLineCount() const -> qsizetype
How many lines getRemainingData() holds.
QString password
The first line of the entry, verbatim.
static auto isOtpFieldName(const QString &name) -> bool
Whether a field name designates the one-time password field.
QString otpUri
Raw OTP configuration (URI or bare base32), or empty.
static constexpr int kMaxDisplayLines
Most lines of remaining text getRemainingDataForDisplay() gives.
auto getRemainingData() const -> QString
auto getNamedValues() const -> NamedValues
static constexpr int kMaxNamedFields
Most named fields parse() takes from one entry.
static auto parse(const QString &fileContent, const QStringList &templateFields, bool allFields, const std::function< bool(const QString &)> &alsoField={}) -> FileContent
parse parses the given fileContent in a FileContent object. The password is accessible through getPas...
static auto isSecretFieldName(const QString &name, const QStringList &secrets) -> bool
Whether a field name names a secret: one of secrets occurs in it as whole words, compared case-insens...
NamedValues namedValues
Fields in order of appearance, template fields first.
auto getRemainingDataForDisplay() const -> QString
Like getRemainingData but without data that should not be displayed (like a TOTP secret).
auto getPassword() const -> QString
auto getOtpUri() const -> QString
The entry's raw one-time password configuration, if it has one.
QString remainingDataDisplay
auto getPasswordForDisplay() const -> QString
Like getPassword but empty when the password line is itself an otpauth URI.
static auto isOtpUriValue(const QString &value) -> bool
Whether a field value is an otpauth URI, and therefore a secret.
The NamedValues class is mostly a list of NamedValue but also has a method to take a specific NamedVa...
Definition filecontent.h:58
auto takeValue(const QString &name) -> QString
Remove and return the value for the named field.
auto takeNamedValue(const QString &name) -> NamedValue
Remove and return the named field, its original line included.
NamedValues()
Construct an empty list.
bool operator==(const NamedValue &a, const NamedValue &b)
Two fields are equal when name and value match; originalLine is formatting, not content,...
Definition filecontent.h:49
A name/value pair parsed from a password file field.
Definition filecontent.h:21
QString name
Field name (the part before the colon in "name: value").
Definition filecontent.h:25
QString originalLine
The field's verbatim source line, colon and spacing intact.
Definition filecontent.h:38
QString value
Field value (the part after the colon in "name: value").
Definition filecontent.h:29