forked from SmartisanTech/Wrench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contactmodel.cpp
304 lines (256 loc) · 8.56 KB
/
contactmodel.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <QDebug>
#include <QFont>
#include <QBrush>
#include <lua.hpp>
#include <QSettings>
#include <QProcessEnvironment>
#include <QDir>
#include <QFile>
#include "selectoutput.h"
#include "vcard.h"
#include "bhj_help.hpp"
#include <QByteArray>
#include <QPixmap>
#include <algorithm>
#include "contactmodel.h"
#include <QRegularExpression>
#include <QtAlgorithms>
static bool vCardLess(const VCard& v1, const VCard& v2)
{
QString v1Pinyin, v2Pinyin;
if (! v1.mPinyin.isEmpty()) {
v1Pinyin = v1.mPinyin[0];
}
if (! v2.mPinyin.isEmpty()) {
v2Pinyin = v2.mPinyin[0];
}
if (v1Pinyin < v2Pinyin)
return true;
if (v1Pinyin == v2Pinyin)
if (v1.mName < v2.mName)
return true;
return false;
}
ContactModel::ContactModel(QObject *parent) :
FilteringModel(parent),
mDefaultAvatar("emojis/iphone-emoji/WRENCH.png")
{
int error = luaL_loadstring(L, "contacts = require('contacts')") || lua_pcall(L, 0, 0, 0);
if (error) {
qDebug() << "Error loading contacts: " << QString::fromUtf8(lua_tolstring(L, -1, NULL));
return;
}
lua_getglobal(L, "contacts");
#ifdef Q_OS_WIN32
QString homePath = QProcessEnvironment::systemEnvironment().value("USERPROFILE");
#else
QString homePath = QProcessEnvironment::systemEnvironment().value("HOME");
#endif
QDir homeDir = QDir(homePath);
QString vcf = homeDir.absoluteFilePath(".android/contacts.vcf");
QFile vcfFile(vcf);
if (!vcfFile.exists()) {
if (QFile("contacts.vcf").exists()) {
vcf = "contacts.vcf";
} else {
vcf = "test.vcf";
}
}
lua_getfield(L, -1, "read_vcf");
lua_pushstring(L, qPrintable(vcf));
error = lua_pcall(L, 1, 1, 0);
if (error) {
qDebug() << "Error: can't read vcf file: " << vcf;
return;
}
int n = luaL_len(L, -1);
for (int i = 1; i <= n; i++) {
VCard vcard;
lua_rawgeti(L, -1, i);
lua_getfield(L, -1, "FNWrench");
vcard.mName = (QString::fromUtf8(lua_tolstring(L, -1, NULL)));
lua_settop(L, -2);
lua_getfield(L, -1, "TELS");
int nTel = luaL_len(L, -1);
for (int iTel = 1; iTel <= nTel; iTel++) {
lua_rawgeti(L, -1, iTel);
vcard.mTels.push_back(QString::fromUtf8(lua_tolstring(L, -1, NULL)));
lua_settop(L, -2);
}
lua_settop(L, -2);
lua_getfield(L, -1, "EMAILS");
int nEmail = luaL_len(L, -1);
for (int iEmail = 1; iEmail <= nEmail; iEmail++) {
lua_rawgeti(L, -1, iEmail);
vcard.mEmails.push_back(QString::fromUtf8(lua_tolstring(L, -1, NULL)));
lua_settop(L, -2);
}
lua_settop(L, -2);
lua_getfield(L, -1, "photo_data");
size_t len = 0;
const char* photo_data = lua_tolstring(L, -1, &len);
if (photo_data != NULL && len != 0) {
QByteArray photoBytes(photo_data, len);
photoBytes = QByteArray::fromBase64(photoBytes);
if (!vcard.mAvatar.loadFromData(photoBytes)) {
qDebug() << "load for " << vcard.mName << " has failed";
} else {
vcard.mAvatar = vcard.mAvatar.scaled(48, 48);
}
if (vcard.mAvatar.isNull()) {
qDebug() << "my icon is null";
}
} else {
vcard.mAvatar = mDefaultAvatar;
}
lua_settop(L, -2);
lua_settop(L, -2);
mVcards << vcard;
}
lua_settop(L, 0);
foreach(const VCard& vcard, mVcards) {
vcard.mPinyin = getPinyinSpelling(vcard.mName);
}
std::sort(mVcards.begin(), mVcards.end(), vCardLess);
initHistory();
mInputTextHistory = mSettings.value("contactInputHistory").toStringList();
}
static QString normalizedPhone(const QString& phone) {
if (phone.size() <= 11) {
return phone;
}
QRegularExpression re("[-+ ]");
QString res = phone;
res.replace(re, "");
if (res.size() > 11) {
res = res.right(11);
}
return res;
}
static void bug(const QString& stem) {
qDebug() << "bug for " << stem;
}
#define debug_it(x) do { \
if (split.contains("zhang")) { \
bug(x); \
} \
} while (0)
void ContactModel::filterSelectedItems(const QStringList& split)
{
bool mFilterWeixin = false;
QMap <QString, bool> weixinPhoneMap;
foreach(const QString& history, mInputTextHistory) {
int match = 1;
QStringList pinyinHistoryList = getPinyinSpelling(history);
pinyinHistoryList << history;
foreach(const QString& stem, split) {
int matched_1 = 0;
foreach (const QString& p, pinyinHistoryList) {
if (p.contains(stem)) {
matched_1 = 1;
break;
}
}
if (! matched_1) {
match = 0;
break;
}
}
if (match) {
SelectedItem si(history, history);
si.icon = mDefaultAvatar;
if (!mSelectedItemsRevMap.contains(si.displayText)) {
mSelectedItems << si;
mSelectedItemsRevMap[si.displayText] = si;
}
}
}
foreach (const VCard& vcard, mVcards) {
QStringList tels = vcard.mTels;
QStringList mails = vcard.mEmails;
int match = 1;
QStringList namePinyin;
if (mFilterWeixin) {
QStringList tels2;
foreach(const QString&tel, tels) {
if (weixinPhoneMap.contains(normalizedPhone(tel))) {
tels2 << normalizedPhone(tel);
}
}
tels = tels2;
}
if (tels.isEmpty() && ! mIsMail) {
match = 0;
}
foreach(const QString& stem, split) {
if (vcard.mName.contains(stem, Qt::CaseInsensitive)) {
continue;
}
if (namePinyin.isEmpty()) {
namePinyin = getPinyinSpelling(vcard.mName);
}
if (matchOneString(namePinyin, stem)) {
continue;
}
// else if (vcard.mEmails.contains("[email protected]")) {
// debug_it("hello 1 world: " + vcard.mName + " : "
// + namePinyin.join(" ") + " : " + split.join(" x ")
// + " : " + vcard.mEmails.join(" e "));
// }
if (!mIsMail) { // doing tels
if (matchOneString(tels, stem)) {
tels = filterMatchedStrings(tels, stem);
continue;
}
if (matchOneString(vcard.mEmails, stem)) {
continue;
}
} else {
if (matchOneString(mails, stem)) {
// this should be done only if stem does not
// matches name. so this must come after name
// compared and not matched.
// because otherwise, if a user named zhangshuang,
// who has two emails, zhangshuang@XXX and
// 123456@qq, and we filter it with "qq zhang",
// then the 123456@qq will be filtered out when
// zhang matches zhangshuang@XXX.
mails = filterMatchedStrings(mails, stem);
continue;
}
if (matchOneString(vcard.mTels, stem)) {
continue;
}
}
match = 0;
break;
}
if (match) {
QStringList whichList = tels;
if (mIsMail) {
whichList = mails;
}
foreach (const QString& telOrMail, whichList) {
SelectedItem si(telOrMail, vcard.mName + " " + telOrMail);
si.icon = vcard.mAvatar;
if (!mSelectedItemsRevMap.contains(si.displayText)) {
mSelectedItems << si;
mSelectedItemsRevMap[si.displayText] = si;
}
}
}
}
}
QString ContactModel::getHistoryName()
{
return "contact-history";
}
void ContactModel::maybeAddTextIntoHistory(const QString& text)
{
mInputTextHistory.push_front(text);
while (!mInputTextHistory.isEmpty() && mInputTextHistory.length() > mMaxHistEntries) {
mInputTextHistory.pop_back();
}
mSettings.setValue("contactInputHistory", QVariant(mInputTextHistory));
mSettings.sync();
}