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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
* qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
* Copyright (C) 2026 Danilo M. <danix@danix.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "contactstore.h"
#include <QDirIterator>
#include <QFile>
#include <QHash>
#include <QStringList>
#include <algorithm>
namespace {
/// Index of the colon separating property from value: the first one that is
/// not inside a double-quoted parameter value.
///
/// `EMAIL;TYPE=INTERNET;LABEL="work: main":a@example.org` has a colon inside
/// the quoted label, and splitting on the first colon would hand the parser
/// `main":a@example.org` as the address. A backslash inside the quotes escapes
/// the next character, so `\"` does not close the quote.
int colonOutsideQuotes(const QString &line)
{
bool inQuotes = false;
for (int i = 0; i < line.size(); ++i) {
const QChar c = line.at(i);
if (c == QLatin1Char('\\')) {
++i; // Skip the escaped character; it cannot be a delimiter.
continue;
}
if (c == QLatin1Char('"')) {
inQuotes = !inQuotes;
continue;
}
if (c == QLatin1Char(':') && !inQuotes)
return i;
}
return -1;
}
/// Decodes the vCard TEXT escape set, applied to FN: a name written
/// `Rossi\, Mario` is one name with a comma, and left escaped the backslash
/// reaches the completion popup. N carries the same escapes, but the struct
/// holds no N and FN is what a display name is.
QString unescapeText(const QString &value)
{
QString out;
out.reserve(value.size());
for (int i = 0; i < value.size(); ++i) {
const QChar c = value.at(i);
if (c != QLatin1Char('\\') || i + 1 >= value.size()) {
out.append(c);
continue;
}
const QChar escaped = value.at(++i);
switch (escaped.unicode()) {
case 'n':
case 'N':
out.append(QLatin1Char('\n'));
break;
case '\\':
case ',':
case ';':
out.append(escaped);
break;
default:
// An unknown escape keeps its character, dropping the backslash
// rather than guessing at a meaning it does not have.
out.append(escaped);
break;
}
}
return out;
}
} // namespace
namespace ContactStore {
QString unfold(const QString &text)
{
QStringList logical;
const QStringList lines = text.split(QLatin1Char('\n'));
for (const QString &rawLine : lines) {
// The store's own files are LF; a CRLF server is handled here rather
// than left to reach a value with a stray carriage return in it.
QString line = rawLine;
if (line.endsWith(QLatin1Char('\r')))
line.chop(1);
const bool isContinuation =
!line.isEmpty()
&& (line.at(0) == QLatin1Char(' ') || line.at(0) == QLatin1Char('\t'));
if (isContinuation && !logical.isEmpty()) {
// Join to the line that precedes it, removing only the marker
// whitespace character. A trailing space on the first line is
// content and survives.
logical.last() += line.mid(1);
} else {
logical.append(line);
}
}
return logical.join(QLatin1Char('\n'));
}
QList<Contact> parseCard(const QString &unfoldedText)
{
QString name;
QStringList emails;
const QStringList lines = unfoldedText.split(QLatin1Char('\n'));
for (QString line : lines) {
if (line.endsWith(QLatin1Char('\r')))
line.chop(1);
if (line.isEmpty())
continue;
const int colon = colonOutsideQuotes(line);
if (colon < 0)
continue;
// The property name is everything before the first ';', and the
// parameters that follow belong to it. Matched case-insensitively per
// RFC 2426, so `email:` is an EMAIL.
const QString property =
line.left(colon).section(QLatin1Char(';'), 0, 0).trimmed();
const QString value = line.mid(colon + 1).trimmed();
if (property.compare(QLatin1String("FN"), Qt::CaseInsensitive) == 0) {
// Last one wins if a malformed card carries two, rather than
// merging two names into one.
name = unescapeText(value);
} else if (property.compare(QLatin1String("EMAIL"), Qt::CaseInsensitive) == 0) {
// PHOTO, ADR, TEL and the rest are not matched and fall through
// unread; N is deliberately not used as a fallback name, because
// FN is what a display name is.
if (!value.isEmpty())
emails.append(value);
}
}
QList<Contact> contacts;
contacts.reserve(emails.size());
for (const QString &email : emails)
contacts.append(Contact{ name, email });
return contacts;
}
QList<Contact> loadDirectory(const QString &directory)
{
QList<Contact> found;
if (directory.isEmpty())
return found;
// Recursive because a vdir keeps one subdirectory per collection. A
// missing root simply yields nothing; no warning, since a machine with no
// vdir is the ordinary case.
QDirIterator it(directory, QStringList{ QStringLiteral("*.vcf") },
QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
QFile file(it.next());
if (!file.open(QIODevice::ReadOnly))
continue; // Unreadable: skip it, do not lose the rest.
const QString text = QString::fromUtf8(file.readAll());
found += parseCard(unfold(text));
}
// De-duplicated on the address, case-insensitively: two collections
// holding the same person is the ordinary case, not an error. The first
// card seen wins.
QList<Contact> unique;
QHash<QString, int> seen;
unique.reserve(found.size());
for (const Contact &contact : found) {
const QString key = contact.email.toLower();
if (seen.contains(key))
continue;
seen.insert(key, unique.size());
unique.append(contact);
}
// Sorted by name then address, case-insensitively. A locale-aware compare
// is deliberately not used: the order is a completion list, not prose.
std::sort(unique.begin(), unique.end(),
[](const Contact &a, const Contact &b) {
const int byName =
QString::compare(a.name, b.name, Qt::CaseInsensitive);
if (byName != 0)
return byName < 0;
return QString::compare(a.email, b.email, Qt::CaseInsensitive) < 0;
});
return unique;
}
} // namespace ContactStore
|