-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontactdatabase.cpp
382 lines (316 loc) · 9.75 KB
/
contactdatabase.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <QtCore>
#include <QDir>
#include <QFileInfo>
#include <QStringList>
#include <QtAlgorithms>
#include <QTextStream>
#include "../Lib/supportfunctions.h"
#include "contactdatabase.h"
#include "configuration.h"
ContactDatabase::ContactDatabase()
{
nullitem.setNull() ;
clear() ;
}
ContactDatabase::~ContactDatabase()
{
}
void ContactDatabase::clear()
{
contacts.clear() ;
selectedcontact=-1 ;
}
bool ContactDatabase::load()
{
Encryption *enc = gConf->encryption() ;
if (!enc) {
dbg("ContactDatabase::load - call without enc") ;
return false ;
}
QStringList nameFilter;
//QFileInfoList list ;
QStringList files ;
// Login if not already done so
if (!enc->loggedIn()) {
enc->login() ;
}
if (!enc->loggedIn()) {
return false ;
}
// Load Contacts and Journal Entries
QDir dir(gContactSavePath) ;
nameFilter << "*.*contact" ;
dir.setNameFilters(nameFilter) ;
files = dir.entryList() ;
for (int i=files.size()-1; i>=0; i--) {
QString filename ;
Contact di ;
filename = files.at(i) ;
filename = filename.replace(".contact","").replace(".zcontact","") ;
di.load(gContactSavePath, filename) ;
// TODO: Need to remove googlerecordid if entry no longer on google
if (di.isSet(Contact::Deleted) && di.getField(Contact::GoogleRecordId).isEmpty()) {
// Archive Deleted
di.getHistory().addEntry("Moving entry to deleted folder") ;
addLog("Moving record " + di.getFormattedName() + " (" + di.getField(Contact::ID) + ") to deleted folder") ;
QString deletedPath = gContactSavePath + "deleted" + QDir::toNativeSeparators("/") ;
QDir path(deletedPath) ;
if (!path.exists()) path.mkpath(".") ;
QFile::remove(deletedPath + filename + ".contact") ;
QFile::remove(deletedPath + filename + ".todolist") ;
QFile::remove(deletedPath + filename + ".history") ;
QFile::rename(gContactSavePath + filename + ".contact", deletedPath + filename + ".contact") ;
QFile::rename(gContactSavePath + filename + ".todolist", deletedPath + filename + ".todolist") ;
QFile::rename(gContactSavePath + filename + ".history", deletedPath + filename + ".history") ;
QFile::remove(deletedPath + filename + ".zcontact") ;
QFile::remove(deletedPath + filename + ".ztodolist") ;
QFile::remove(deletedPath + filename + ".zhistory") ;
QFile::rename(gContactSavePath + filename + ".zcontact", deletedPath + filename + ".zcontact") ;
QFile::rename(gContactSavePath + filename + ".ztodolist", deletedPath + filename + ".ztodolist") ;
QFile::rename(gContactSavePath + filename + ".zhistory", deletedPath + filename + ".zhistory") ;
} else {
// Mark empty entries as deleted
if (di.isEmpty()) {
di.getHistory().addEntry("Marking empty record as 'deleted'") ;
addLog("Marking empty record (" + di.getField(Contact::ID) + ") as deleted") ;
di.setFlag(Contact::Deleted, true) ;
contacts.push_back(di) ;
} else {
// Add entry
addLog("Loading " + di.getFormattedName() + " (" + di.getField(Contact::ID) + ")") ;
contacts.push_back(di) ;
}
}
}
return true ;
}
// Reload the journal
bool ContactDatabase::reloadJournal(Contact& contact)
{
return contact.getHistory().load(gContactSavePath, contact.getField(Contact::ID)) ;
}
// Create a HTML Index File of the contacts
bool ContactDatabase::saveIndex(int j, QString path, QString contactid, QString name, QString phone, QString email, QString address)
{
QString fullPath = path + QString("../contactdatabase.html") ;
QFile file(fullPath);
QFile::OpenMode mode = QIODevice::WriteOnly | QIODevice::Text ;
if (j>=0) {
mode |= QIODevice::Append ;
}
if (!file.open(mode)) {
return false;
}
QTextStream out(&file);
out.setEncoding(QStringConverter::Utf8) ;
if (j<0) {
out << "<html><body><table border='1'>\n" ;
}
out << "<tr>\n" ;
out << " <td><a target=\"details\" href=\"" << path << contactid << ".contact\"> ... </a></td>\n";
out << " <td>" << name << "</td>\n" ;
out << " <td>" << phone << "</td>\n" ;
out << " <td>" << email << "</td>\n" ;
out << " <td>" << address << "</td>\n" ;
out << "</tr>\n" ;
if (j>0) {
out << "</table></body></html>\n" ;
}
file.close() ;
return true ;
}
bool ContactDatabase::save()
{
Encryption *enc = gConf->encryption() ;
if (!enc) {
dbg("ContactDatabase::save call without enc") ;
return false ;
}
sort() ;
bool success=true ;
int sz = size() ;
if (sz>0) {
// Login if not already done so
if (!enc->loggedIn()) {
enc->login() ;
}
if (!enc->loggedIn()) {
return false ;
}
QDir cdir(gSavePath) ;
cdir.mkpath("contact") ;
for (int i=0; i<sz; i++) {
int j ;
if (i==0) j=-1 ;
else if (i>=(sz-1)) j=1 ;
else j=0 ;
success |= getContact(i).save(gContactSavePath) ;
if (gConf->debugGoogleEnabled()) {
saveIndex(j, gContactSavePath, getContact(i).getField(Contact::ID),
getContact(i).getFormattedName(false, true),
getContact(i).getField(Contact::Phone),
getContact(i).getField(Contact::Email),
getContact(i).getField(Contact::Address)) ;
}
}
}
return success ;
}
bool ContactDatabase::isNull()
{
return contacts.isEmpty() ;
}
//
// Sort
//
// Sorts the database into order
//
void ContactDatabase::sort()
{
QString id = getContact(selectedcontact).getField(Contact::ID) ;
std::sort(contacts.begin(), contacts.end()) ;
selectedcontact=getIndex(id) ;
}
Contact& ContactDatabase::selectContact(int n)
{
if (n<-1 || n>=contacts.size()) selectedcontact=n ;
else selectedcontact=n ;
return getSelected() ;
}
Contact& ContactDatabase::selectContact(QString id)
{
selectedcontact=getIndex(id) ;
return getSelected() ;
}
Contact& ContactDatabase::selectContact(Contact &contact)
{
selectedcontact=getIndex(contact) ;
return getSelected() ;
}
Contact& ContactDatabase::getSelected()
{
if (selectedcontact<0) {
return nullitem ;
} else {
return contacts[selectedcontact];
}
}
//
// AddRecord
//
// Adds a new empty record to the end of the
// database and returns a pointer to it.
//
Contact& ContactDatabase::addContact()
{
Contact record ;
contacts.push_back(record) ;
return contacts.back() ;
}
bool ContactDatabase::addContact(Contact &contact, bool dosort)
{
// Find the record for the contact, and if not found, create / add one
Contact& record = getContactBy(Contact::ID, contact.getField(Contact::ID)) ;
if (record.isNull()) {
// Create a new entry and copy the contents to it
Contact& newcontact = addContact() ;
contact.copyTo(newcontact, Contact::mcDetailsGroupProfile|Contact::mcId|Contact::mcGoogleId|Contact::mcEtag|Contact::mcControlFlags) ;
} else {
// Update the found record with the contact details
contact.copyTo(record, Contact::mcDetailsGroupProfile) ;
}
// Sort the contacts list
if (dosort) sort() ;
return true ;
}
//
// GetIndex
//
// Returns the index of the given record in the database
// This is achieved by looking up the ID, and then searching the
// database for a matching ID.
//
int ContactDatabase::getIndex(Contact &record)
{
return (getIndex(record.getField(Contact::ID))) ;
}
//
// GetIndexFromID
//
// Searches the database, and returns
// the index of the record with the given id, or
// -1 if the record could not be found
//
int ContactDatabase::getIndex(QString searchid)
{
for (int i=0, ne=contacts.size(); i<ne; i++) {
QString& recordid = contacts[i].getField(Contact::ID) ;
if (searchid.compare(recordid)==0) return i ;
}
return -1 ;
}
Contact& ContactDatabase::getContactBy(Contact::ContactRecord type, QString& data)
{
for (int i=0, ne=contacts.size(); i<ne; i++) {
Contact &c = contacts[i] ;
if (c.getField(type).compare(data)==0) {
return c ;
}
}
return nullitem ;
}
//
// GetNumRecords
//
// Returns the number of records
// in the database
//
int ContactDatabase::size()
{
return contacts.size() ;
}
//
// getContact
//
// Returns a pointer to the requested record
// or a pointer to the null record if the
// requested entry does not exist.
//
// To guarantee that a valid record is returned,
// n must be between 0 and size()-1.
//
// If this function is used to get a blank null
// contact record, the record should be copied,
// as the nullcontact object is re-used on the
// next getContact call.
//
Contact& ContactDatabase::getContact(int n)
{
if (n<0 || n >= contacts.size()) {
return nullitem ;
} else {
return contacts[n];
}
}
Contact& ContactDatabase::getContactById(QString& searchid)
{
return getContact(getIndex(searchid)) ;
}
Contact& ContactDatabase::getContactByGoogleId(QString& searchid)
{
for (int i=0, sz=size(); i<sz; i++) {
Contact &c = contacts[i] ;
if (c.getField(Contact::GoogleRecordId).compare(searchid)==0) {
return c ;
}
}
return nullitem ;
}
bool ContactDatabase::deleteContact(int n)
{
if (n<0 || n>=contacts.size()) return false ;
contacts[n].setFlag(Contact::Deleted, true) ;
contacts[n].setFlag(Contact::Hidden, true) ;
sort() ;
return true ;
}