-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontactsmanager.cpp
182 lines (151 loc) · 5.82 KB
/
contactsmanager.cpp
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
/*
* Copyright 2019 Richard Liebscher <[email protected]>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "contactsmanager.h"
#include <QLoggingCategory>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlDatabase>
#include "vcardbuilder.h"
namespace SailfishConnect {
static Q_LOGGING_CATEGORY(logger, "kdeconnect.contactsmanager");
static QString QTCONTACTS_SQLITE_STORE = QStringLiteral(
"/home/nemo/.local/share/system/Contacts/qtcontacts-sqlite/contacts.db");
namespace {
class SailfishOsContactsManager : public ContactsManager {
public:
SailfishOsContactsManager();
QMap<QString, QDateTime> getLastModifiedTimes() override;
QMap<QString, QString> exportVCards(const QStringList& requestedIds, const QString& deviceId) override;
QString lookUpName(const QString& phoneNumber) override;
private:
QSqlDatabase m_db;
};
SailfishOsContactsManager::SailfishOsContactsManager()
{
m_db = QSqlDatabase::addDatabase(
QStringLiteral("QSQLITE"),
QStringLiteral("QTCONTACTS_SQLITE_STORE"));
if (!m_db.isValid()) {
qCCritical(logger) << "QSQLITE database driver is not available.";
return;
}
m_db.setDatabaseName(QTCONTACTS_SQLITE_STORE);
if (!m_db.open()) {
qCCritical(logger) << "Cannot open contacts database"
<< QTCONTACTS_SQLITE_STORE
<< ":" << m_db.lastError().text();
return;
}
}
QMap<QString, QDateTime> SailfishOsContactsManager::getLastModifiedTimes()
{
QMap<QString, QDateTime> result;
QSqlQuery timeStampQuery(m_db);
if (!timeStampQuery.exec(
QStringLiteral("SELECT contactId, modified FROM Contacts"))) {
qCCritical(logger) << "Getting modified timestamps failed:"
<< timeStampQuery.lastError().text();
return result;
}
while (timeStampQuery.next()) {
QString id = QString::number(timeStampQuery.value(0).toInt());
QDateTime modified = timeStampQuery.value(1).toDateTime();
result.insert(id, modified);
}
return result;
}
QMap<QString, QString> SailfishOsContactsManager::exportVCards(const QStringList& ids, const QString& deviceId)
{
QMap<QString, QString> result;
QSet<QString> requestedIds = ids.toSet();
QSqlQuery phoneNumbersQuery(m_db);
if (!phoneNumbersQuery.exec(QStringLiteral(
"SELECT DISTINCT contactId, phoneNumber FROM PhoneNumbers"))) {
qCCritical(logger) << "Getting phone numbers failed:"
<< phoneNumbersQuery.lastError().text();
return {};
}
QMap<QString, QStringList> phoneNumbers;
while (phoneNumbersQuery.next()) {
QString id = QString::number(phoneNumbersQuery.value(0).toInt());
QString phoneNumber = phoneNumbersQuery.value(1).toString();
if (phoneNumbers.contains(id)) {
phoneNumbers[id].append(phoneNumber);
} else {
phoneNumbers.insert(id, { phoneNumber });
}
}
QSqlQuery dataQuery(m_db);
if (!dataQuery.exec(QStringLiteral(
"SELECT contactId, modified, displayLabel, firstName, lastName"
" FROM Contacts"))) {
qCCritical(logger) << "Getting contact details failed:"
<< dataQuery.lastError().text();
return {};
}
while (dataQuery.next()) {
QString id = QString::number(dataQuery.value(0).toInt());
if (!requestedIds.contains(id))
continue;
QDateTime modified = dataQuery.value(1).toDateTime();
QString displayLabel = dataQuery.value(2).toString();
QString firstName = dataQuery.value(3).toString();
QString lastName = dataQuery.value(4).toString();
VCardBuilder vcf;
vcf.addRawProperty(QStringLiteral("FN"), displayLabel);
for (auto& tel : phoneNumbers.value(id)) {
vcf.addRawProperty(QStringLiteral("TEL;TYPE=home"), tel);
}
vcf.addRawProperty(
QStringLiteral("N"),
QStringLiteral("%1;%2;;;").arg(lastName, firstName));
vcf.addRawProperty(
QStringLiteral("X-KDECONNECT-ID-DEV-") + deviceId,
id);
vcf.addRawProperty(
QStringLiteral("X-KDECONNECT-TIMESTAMP"),
QString::number(modified.toMSecsSinceEpoch()));
result.insert(id, vcf.result());
}
return result;
}
QString SailfishOsContactsManager::lookUpName(const QString& phoneNumber)
{
QSqlQuery dataQuery(m_db);
dataQuery.prepare(QStringLiteral(
"SELECT displayLabel FROM Contacts"
" JOIN Phonenumbers ON Phonenumbers.contactId == Contacts.contactId"
" WHERE Phonenumbers.phoneNumber == ?"));
dataQuery.bindValue(0, phoneNumber);
if (!dataQuery.exec()) {
qCCritical(logger) << "Getting contact name failed:"
<< dataQuery.lastError().text();
return QString();
}
if (dataQuery.next()) {
return dataQuery.value(0).toString();
} else {
qCInfo(logger) << "Getting contact name failed: no entry found";
return phoneNumber;
}
}
} // namespace
ContactsManager& ContactsManager::instance() {
static SailfishOsContactsManager instance;
return instance;
}
} // namespace SailfishConnect