-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathclientconf.cpp
More file actions
149 lines (119 loc) · 3.7 KB
/
Copy pathclientconf.cpp
File metadata and controls
149 lines (119 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
License: GPL-2
*/
#include "clientconf.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QTextStream>
QString ClientConfig::configPath()
{
QByteArray override = qgetenv("PAPERMAN_CONFIG");
if (!override.isEmpty())
return QString::fromLocal8Bit(override);
QString d = QStandardPaths::writableLocation(
QStandardPaths::GenericConfigLocation)
+ "/paperman";
return d + "/client.conf";
}
/* Canonical form used to decide whether two config lines name the same
server. QUrl::matches() leaves an empty path and "/" distinct, so
http://host:8080 and http://host:8080/ would both get listed; compare
on a normalised string instead. */
static QString urlKey(const QUrl &url)
{
QUrl u = url.adjusted(QUrl::NormalizePathSegments
| QUrl::StripTrailingSlash);
/* StripTrailingSlash leaves a lone root "/" in place, so
http://host:8080/ and http://host:8080 still differ; drop it. */
if (u.path() == QLatin1String("/"))
u.setPath(QString());
return u.toString(QUrl::FullyEncoded).toLower();
}
/* Turn one non-comment line into an entry. Returns false (leaving
@entry untouched) when the URL part is empty or doesn't parse. */
static bool parseLine(const QString &line, ServerEntry *entry)
{
QString name;
QString urlText = line;
/* An '=' before the URL names the server. URLs never contain a
bare '=' before the authority, so the first one is the separator. */
int eq = line.indexOf('=');
if (eq >= 0) {
name = line.left(eq).trimmed();
urlText = line.mid(eq + 1).trimmed();
}
if (urlText.isEmpty())
return false;
QUrl url(urlText);
if (!url.isValid() || url.scheme().isEmpty())
return false;
entry->url = url;
entry->name = name.isEmpty() ? url.host() : name;
return true;
}
QList<ServerEntry> ClientConfig::load(QStringList *badLines)
{
QList<ServerEntry> entries;
QFile f(configPath());
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
return entries; // missing file is simply an empty directory
QTextStream in(&f);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
if (line.isEmpty() || line.startsWith('#'))
continue;
ServerEntry entry;
if (parseLine(line, &entry))
entries.append(entry);
else if (badLines)
badLines->append(line);
}
return entries;
}
bool ClientConfig::sameServer(const QUrl &a, const QUrl &b)
{
return urlKey(a) == urlKey(b);
}
bool ClientConfig::findByName(const QString &name, ServerEntry *out)
{
const QList<ServerEntry> entries = load();
for (const ServerEntry &e : entries) {
if (e.name.compare(name, Qt::CaseInsensitive) == 0) {
if (out)
*out = e;
return true;
}
}
return false;
}
bool ClientConfig::add(const QUrl &url, const QString &name,
QString *errorOut, bool *added)
{
if (added)
*added = false;
/* Already listed? Compare on the normalised URL so trailing-slash
and case differences in the host don't create a duplicate. */
const QList<ServerEntry> entries = load();
for (const ServerEntry &e : entries) {
if (sameServer(e.url, url))
return true;
}
QString path = configPath();
QDir().mkpath(QFileInfo(path).absolutePath());
QFile f(path);
if (!f.open(QIODevice::Append | QIODevice::Text)) {
if (errorOut)
*errorOut = f.errorString();
return false;
}
QTextStream out(&f);
if (added)
*added = true;
if (name.isEmpty())
out << url.toString() << '\n';
else
out << name << " = " << url.toString() << '\n';
return true;
}