15#include <QRegularExpression>
17#include <QStringConverter>
26constexpr qint64 kMaxTemplateFileBytes = qint64{64} * 1024;
37auto readTemplateFile(
const QString &path) -> std::optional<QString> {
42 const QByteArray bytes = file.read(kMaxTemplateFileBytes + 1);
43 if (bytes.size() > kMaxTemplateFileBytes) {
44 qWarning() <<
"Ignoring" << path <<
"; a template file is at most"
45 << kMaxTemplateFileBytes <<
"bytes";
50 const QByteArray bom(
"\xEF\xBB\xBF");
51 return QString::fromUtf8(bytes.startsWith(bom) ? bytes.mid(bom.size())
57auto sectionHeader(
const QString &line) -> std::optional<QString> {
58 if (!line.startsWith(
'[') || !line.endsWith(
']')) {
61 return line.mid(1, line.length() - 2).trimmed();
65auto defaultTemplateIn(
const QDir &dir) -> QString {
66 const std::optional<QString> contents =
67 readTemplateFile(dir.filePath(
".default_template"));
71 const QString name = contents->section(QLatin1Char(
'\n'), 0, 0).trimmed();
72 return name.startsWith(
'#') ? QString() : name;
78 -> QHash<QString, QStringList> {
79 QHash<QString, QStringList> result;
80 std::optional<QString> contents =
81 readTemplateFile(QDir(storePath).filePath(
".templates"));
85 QTextStream in(&*contents, QIODevice::ReadOnly);
88 std::optional<QString> section;
90 const auto flush = [&] {
92 result.insert(*section, fields);
97 const QString line = in.readLine().trimmed();
98 if (
const std::optional<QString> header = sectionHeader(line)) {
100 section = header->isEmpty() ? std::nullopt : header;
103 <<
"Empty template section in .templates file, ignoring fields";
105 }
else if (section && !line.isEmpty() && !line.startsWith(
'#')) {
114 static const QRegularExpression marker(
115 QStringLiteral(
"^#\\s*secret\\s*:(.*)$"),
116 QRegularExpression::CaseInsensitiveOption);
118 const QStringList lines = templateText.split(QLatin1Char(
'\n'));
119 for (
const QString &line : lines) {
120 const QRegularExpressionMatch match = marker.match(line.trimmed());
121 if (!match.hasMatch()) {
124 const QStringList listed = match.captured(1).split(QLatin1Char(
','));
125 for (
const QString &name : listed) {
126 const QString trimmed = name.trimmed();
127 if (!trimmed.isEmpty()) {
136 const std::optional<QString> contents =
137 readTemplateFile(QDir(storePath).filePath(
".templates"));
148 const QStringList lines = templateText.split(QLatin1Char(
'\n'));
149 for (
const QString &line : lines) {
150 if (line.trimmed().startsWith(QLatin1Char(
'#'))) {
155 if (!line.isEmpty()) {
158 <<
"fields; ignoring the rest";
169 const QHash<QString, QStringList> &templates)
171 QSaveFile saveFile(QDir(storePath).filePath(
".templates"));
172 if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
175 QTextStream out(&saveFile);
176 out.setEncoding(QStringConverter::Utf8);
177 out <<
"# QtPass templates configuration\n";
178 out <<
"# Format: INI-style with [template_name] sections,\n";
179 out <<
"# followed by field names (one per line)\n\n";
181 QStringList sortedKeys = templates.keys();
182 std::sort(sortedKeys.begin(), sortedKeys.end());
183 for (
const QString &key : std::as_const(sortedKeys)) {
184 out <<
"[" << key <<
"]\n";
185 for (
const QString &field : templates.value(key)) {
186 out << field <<
"\n";
191 if (out.status() != QTextStream::Ok) {
194 return saveFile.commit();
198 const QString &storePath) -> QString {
199 const QString store = QDir::cleanPath(QDir(storePath).absolutePath());
202 const auto inStore = [&store](
const QString &path) {
203 return path == store ||
204 (path.startsWith(store) && path.length() > store.length() &&
205 path.at(store.length()) == QChar(
'/'));
207 QDir dir(folderPath);
208 while (inStore(QDir::cleanPath(dir.absolutePath()))) {
209 const QString name = defaultTemplateIn(dir);
210 if (!name.isEmpty()) {
213 if (QDir::cleanPath(dir.absolutePath()) == store || !dir.cdUp()) {
static constexpr int kMaxNamedFields
Most named fields parse() takes from one entry.
static auto writeTemplates(const QString &storePath, const QHash< QString, QStringList > &templates) -> bool
Write templates to .templates file in password store.
static auto readTemplates(const QString &storePath) -> QHash< QString, QStringList >
Read templates from .templates file in password store.
static auto fieldsIn(const QString &templateText) -> QStringList
The field names of the template in the settings: one per line, with comment lines (starting with #,...
static auto getFolderTemplate(const QString &folderPath, const QString &storePath) -> QString
Get default template for a folder. Looks in folder, then parent folders up to root.
static auto readSecretFields(const QString &storePath) -> QStringList
The field names the store's .templates file marks as secret, in any of its sections (see secretFields...
static auto secretFieldsIn(const QString &templateText) -> QStringList
The field names a template marks as secret.
static auto openRegularFile(const QString &path, QFile &file) -> bool
Open path for reading as the regular file it is, not as what a link under that name points to: the ob...
Parser for a decrypted password entry: password line, key: value fields, remaining text and the OTP c...
TemplateIO: the store's .templates and .default_template files and the # secret: marker in template t...
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...