-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleupdatedialog_contacts.cpp
483 lines (384 loc) · 19.3 KB
/
googleupdatedialog_contacts.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//
// googleupdatedialog_contacts.cpp
//
#include "googleupdatedialog.h"
#include "ui_googleupdatedialog.h"
#include "configuration.h"
#include "../Lib/supportfunctions.h"
//
// Process Contact Update
//
// 1. Fetch IDs
// 2. Download Google Contact List (all)
// 3. Add Fake entry in Google list for missing local contacts
// 4. Fix broken ID links
// 5. Store new non-deleted contacts and re-upload
// 6. Handle merges
// 7. Upload new local entries
// 8. Update profile
// 9. Synchronise changes
//
bool GoogleUpdateDialog::uploadContact(Contact& contact, GoogleAccess &google, QString etag)
{
bool success=false ;
QString& googleid = contact.getField(Contact::GoogleRecordId) ;
if (googleid.isEmpty() || etag.isEmpty()) {
success = google.updateContact(contact, GoogleAccess::Post) ;
} else {
success = google.updateContact(contact, GoogleAccess::Patch, etag) ;
}
// Update Groups
// Get and Validate Contact
// Loop until OK
// Store etag on success
return success ;
}
bool GoogleUpdateDialog::processContactUpdate(QDateTime &lastsync, GoogleAccess& google, class ContactDatabase& db)
{
Q_UNUSED(lastsync) ;
bool networkok = true ;
ContactDatabase googledb ;
// ------------------------------------------------------------------------------------
//
// 1. Fetch IDs
//
ui->progressBar->setValue(500) ;
ui->updateStatusReport->append(QString(" Fetching Group IDs:")) ;
if (!google.getGroupIds()) {
ui->updateStatusReport->append(QString(" Creating Missing Groups.")) ;
google.createGroupIds() ;
google.getGroupIds() ;
}
if (state<0) return false ;
// ------------------------------------------------------------------------------------
//
// 2. Download Google Contact List (all)
//
ui->progressBar->setValue(550) ;
networkok &= google.getContacts(googledb) ;
ui->updateStatusReport->append(QString(" Downloaded ") + QString::number(googledb.size()) + QString(" Contacts from Google.")) ;
if (state<0) return false ;
// ------------------------------------------------------------------------------------
//
// 3. Set the default upload / download
//
ui->progressBar->setValue(600) ;
bool googlemaster = gConf->googleMaster() ;
for (int i=0; i<db.size(); i++) {
Contact& localcontact = db.getContact(i) ;
bool upload ;
if (googlemaster) {
// Google Master, So Upload by default
upload=false ;
} else {
// Local Master, So Download by default
upload=true ;
}
localcontact.setFlag(Contact::ToBeDownloaded, !upload) ;
localcontact.setFlag(Contact::ToBeUploaded, upload) ;
}
if (state<0) return false ;
// ------------------------------------------------------------------------------------
//
// 4. Fix broken ID links
//
ui->progressBar->setValue(610) ;
ui->updateStatusReport->append(QString(" Repairing Broken Local Google Record IDs:")) ;
int size5=googledb.size() ;
for (int i=0; i<size5; i++) {
ui->progressBar->setValue(610 + ( i * 10)/size5 ) ;
Contact& googlecontact = googledb.getContact(i) ;
Contact& localcontact = db.getContactById(googlecontact.getField(Contact::ID)) ;
if (!localcontact.isNull() && !googlecontact.isSet(Contact::Deleted) &&
localcontact.getField(Contact::GoogleRecordId).compare(googlecontact.getField(Contact::GoogleRecordId))!=0) {
localcontact.setField(Contact::GoogleRecordId, googlecontact.getField(Contact::GoogleRecordId)) ;
ui->updateStatusReport->append(QString(" ") + localcontact.getFormattedName(false,false)) ;
}
}
if (state<0) return false ;
// ------------------------------------------------------------------------------------
//
// 5. Handle merges
//
ui->progressBar->setValue(620) ;
ui->updateStatusReport->append(QString(" Processing Merged Contacts:")) ;
int size7 = googledb.size() ;
for (int i=0; i<size7; i++) {
ui->progressBar->setValue(620 + ( i * 10)/size7 ) ;
Contact& googlecontact = googledb.getContact(i) ;
Contact& localcontact = db.getContactById(googlecontact.getField(Contact::ID)) ;
if (!localcontact.isNull() && googlecontact.mergedIdCount()>1) {
QStringList ids = googlecontact.mergedIdList() ;
QString localid = localcontact.getField(Contact::ID) ;
for (int i=0; i<ids.length(); i++) {
QString id = ids.at(i) ;
if (id.compare(localid)!=0) {
Contact& contact = db.getContactById(id) ;
if (contact.isSet(Contact::ToBeDownloaded)) {
// Merge all contacts together, then tag as deleted
contact.mergeInto(localcontact) ;
contact.setFlag(Contact::Deleted, true) ;
contact.setField(Contact::GoogleRecordId, QString("")) ;
} else {
// Remove record ID of merged contacts so that they will be re-uploaded as-if new later
contact.setField(Contact::GoogleRecordId, QString("")) ;
}
}
}
// Update Google entry to remove additional IDs
if (!localcontact.isEmpty()) {
// Ensure contact has correct google ID
localcontact.setField(Contact::GoogleRecordId, googlecontact.getField(Contact::GoogleRecordId)) ;
if (uploadContact(localcontact, google, googlecontact.getField(Contact::GoogleEtag))) {
ui->updateStatusReport->append(QString(" ") + localcontact.getFormattedName(false,false)) ;
} else {
ui->updateStatusReport->append(QString(" ") + localcontact.getFormattedName(false,false) + QString(" FAILED")) ;
networkok = false ;
}
}
}
}
if (state<0) return false ;
// ------------------------------------------------------------------------------------
//
// 6. Add Fake entry in Google list for missing contacts, and identify new local records
//
ui->progressBar->setValue(630) ;
ui->updateStatusReport->append(QString(" Identifying local contacts missing on Google:")) ;
int size3 = db.size() ;
for (int i=0; i<size3; i++) {
ui->progressBar->setValue(630 + ( i * 10)/size3 ) ;
Contact& localcontact = db.getContact(i) ;
Contact& googlecontact = googledb.getContactById(localcontact.getField(Contact::ID)) ;
if (googlecontact.isNull()) {
// Add fake 'deleted' entry to Google List
Contact deletedcontact ;
deletedcontact.setField(Contact::ID, localcontact.getField(Contact::ID)) ;
deletedcontact.setFlag(Contact::Deleted, true) ;
googledb.addContact(deletedcontact) ;
if (localcontact.isSet(Contact::Hidden) || localcontact.isSet(Contact::Deleted)) {
// Contact is hidden or deleted, so don't override any options
// But, forget any previous Google Record ID
localcontact.setField(Contact::GoogleRecordId, "") ;
localcontact.setField(Contact::Uploaded, "") ;
} else {
if (localcontact.getField(Contact::GoogleRecordId).isEmpty()) {
// Contact has never been uploaded
// Override upload/download -> upload
localcontact.setFlag(Contact::ToBeUploaded, true) ;
localcontact.setFlag(Contact::ToBeDownloaded, false) ;
ui->updateStatusReport->append(QString(" %1 (new local)").arg(localcontact.getFormattedName(false,false))) ;
} else {
// Contact has previously been uploaded
// So don't override options (stick with default)
// But, forget any previous Google Record ID
localcontact.setField(Contact::GoogleRecordId, "") ;
ui->updateStatusReport->append(QString(" %1 (removed google)").arg(localcontact.getFormattedName(false,false))) ;
}
}
}
}
if (state<0) return false ;
// ------------------------------------------------------------------------------------
//
// 7. Add Fake entry in Local list for missing contacts, and identify new google records
//
ui->progressBar->setValue(640) ;
ui->updateStatusReport->append(QString(" Identifying Google contacts missing locally:")) ;
int size4 = googledb.size() ;
for (int i=0; i<size4; i++) {
ui->progressBar->setValue(640 + ( i * 10)/size4 ) ;
Contact& googlecontact = googledb.getContact(i) ;
Contact& localcontact = db.getContactById(googlecontact.getField(Contact::ID)) ;
if (localcontact.isNull() && !googlecontact.isSet(Contact::Deleted)) {
ui->updateStatusReport->append(QString(" ") + googlecontact.getFormattedName(false,false)) ;
Contact newlocal ;
newlocal.setField(Contact::ID, googlecontact.getField(Contact::ID)) ;
// Download (save) Google details
newlocal.setFlag(Contact::ToBeDownloaded, true) ;
// Re-upload to affix ID if it was not already available on Google
if (!googlecontact.isSet(Contact::Uploaded)) newlocal.setFlag(Contact::ToBeUploaded, true) ;
db.addContact(newlocal) ;
}
}
if (state<0) return false ;
// ------------------------------------------------------------------------------------
//
// 8. Update profile
//
ui->progressBar->setValue(650) ;
ui->updateStatusReport->append(QString(" Storing Google Profile Changes:")) ;
int size8 = db.size() ;
for (int i=0; i<size8; i++) {
ui->progressBar->setValue(650 + ( i * 10)/size8 ) ;
Contact& localcontact = db.getContact(i) ;
Contact& googlecontact = googledb.getContactBy(Contact::ID, localcontact.getField(Contact::ID)) ;
if ( !googlecontact.isEmpty() && !googlecontact.matches(localcontact, Contact::mcProfile)) {
ui->updateStatusReport->append(QString(" ") + localcontact.getFormattedName(false, false)) ;
googlecontact.copyTo(localcontact, Contact::mcProfile) ;
}
}
if (state<0) return false ;
// ------------------------------------------------------------------------------------
//
// 9. Synchronise changes (download first)
//
ui->progressBar->setValue(660) ;
ui->updateStatusReport->append(QString(" Synchronising Changes:")) ;
int size9 = db.size() ;
for (int i=0; i<size9; i++) {
ui->progressBar->setValue(660 + ( i * 240)/size9 ) ;
if (state<0) return false ;
Contact& localcontact = db.getContact(i) ;
Contact& googlecontact = googledb.getContactBy(Contact::ID, localcontact.getField(Contact::ID)) ;
bool deletedlocaldeletedgoogle =
(googlecontact.isNull() || googlecontact.isSet(Contact::Deleted)) &&
(localcontact.isSet(Contact::Hidden) || localcontact.isSet(Contact::Deleted)) ;
bool googlematcheslocal = googlecontact.matches(localcontact, Contact::mcDetails|Contact::mcId) ;
if ( deletedlocaldeletedgoogle ) {
// Deleted everywhere, so nothing is required
localcontact.setFlag(Contact::ToBeUploaded, false) ;
localcontact.setFlag(Contact::ToBeDownloaded, false) ;
} else if (!googlematcheslocal) {
QString changes ;
QString summary ;
// Download Details from Google
if (localcontact.isSet(Contact::ToBeDownloaded)) {
if (googlecontact.isSet(Contact::Deleted)) {
changes = QString("Deleted.") ;
summary = QString("deleted") ;
localcontact.setFlag(Contact::Deleted, true) ;
} else if (googlecontact.isSet(Contact::Hidden)) {
changes = QString("Hidden.") ;
summary = QString("hidden") ;
localcontact.setFlag(Contact::Hidden, true) ;
} else {
changes = QString("Downloaded: ") + googlecontact.mismatch(localcontact, Contact::mcDetailsGroup|Contact::mcId) + QString(". ");
summary = QString("downloaded") ;
googlecontact.copyTo(localcontact, Contact::mcDetailsGroup|Contact::mcGoogleId) ;
}
ui->updateStatusReport->append(QString(" ") + localcontact.getFormattedName(false, false) + QString(" - Updating Local Contact Details (%1)").arg(summary)) ;
}
// Update Details on Google
if (localcontact.isSet(Contact::ToBeUploaded)) {
if (localcontact.isSet(Contact::Deleted)) {
changes = QString("Deleted.") ;
summary = QString("deleted") ;
} else if (localcontact.isSet(Contact::Hidden)) {
changes = QString("Hidden.") ;
summary = QString("hidden") ;
} else {
changes = QString("Uploaded: ") + localcontact.mismatch(googlecontact, Contact::mcDetails|Contact::mcId) + QString(". ");
summary = QString("uploaded") ;
}
if (uploadContact(localcontact, google, googlecontact.getField(Contact::GoogleEtag))) {
ui->updateStatusReport->append(QString(" ") + localcontact.getFormattedName(false, false) + QString(" - Updating Google Contact Details (%1)").arg(summary)) ;
localcontact.copyTo(googlecontact, Contact::mcDetails|Contact::mcId|Contact::mcGoogleId) ;
} else {
ui->updateStatusReport->append(QString(" ") + localcontact.getFormattedName(false, false) + QString(" - FAILED Updating Google Contact Details")) ;
networkok = false ;
}
// If the record has been successfully removed from Google, forget the local details
if (networkok && (localcontact.isSet(Contact::Deleted) || localcontact.isSet(Contact::Hidden))) {
localcontact.setField(Contact::GoogleRecordId, QString("")) ;
localcontact.setField(Contact::GoogleEtag, QString("")) ;
localcontact.setFlag(Contact::ToBeUploaded, false) ;
}
}
if (!changes.isEmpty()) {
localcontact.getHistory().addEntry(changes) ;
}
}
}
if (state<0) return false ;
// 10. Synchronise Groups
ui->progressBar->setValue(900) ;
if (networkok) {
// Upload record groups
if (networkok) { networkok = updateSingleGoogleContactGroup(google, Contact::GroupBusiness, db, googledb) ; }
ui->progressBar->setValue(918) ;
if (state<0) return false ;
if (networkok) { networkok = updateSingleGoogleContactGroup(google, Contact::GroupClient, db, googledb) ; }
ui->progressBar->setValue(936) ;
if (state<0) return false ;
if (networkok) { networkok = updateSingleGoogleContactGroup(google, Contact::GroupFamily, db, googledb) ; }
ui->progressBar->setValue(954) ;
if (state<0) return false ;
if (networkok) { networkok = updateSingleGoogleContactGroup(google, Contact::GroupFriend, db, googledb) ; }
ui->progressBar->setValue(972) ;
if (state<0) return false ;
if (networkok) { networkok = updateSingleGoogleContactGroup(google, Contact::GroupOther, db, googledb) ; }
ui->progressBar->setValue(990) ;
if (state<0) return false ;
}
if (networkok) {
// Download record groups
updateSingleLocalContactGroup(google, Contact::GroupBusiness, db, googledb) ;
ui->progressBar->setValue(992) ;
if (state<0) return false ;
updateSingleLocalContactGroup(google, Contact::GroupClient, db, googledb) ;
ui->progressBar->setValue(994) ;
if (state<0) return false ;
updateSingleLocalContactGroup(google, Contact::GroupFamily, db, googledb) ;
ui->progressBar->setValue(996) ;
if (state<0) return false ;
updateSingleLocalContactGroup(google, Contact::GroupFriend, db, googledb) ;
ui->progressBar->setValue(998) ;
if (state<0) return false ;
updateSingleLocalContactGroup(google, Contact::GroupOther, db, googledb) ;
ui->progressBar->setValue(1000) ;
if (state<0) return false ;
}
ui->progressBar->setValue(1000) ;
if (networkok) {
ui->updateStatusReport->append(QString(" Contact Synchronisation: OK")) ;
} else {
ui->updateStatusReport->append(QString(" Contact Synchronisation: FAILED - Please resync")) ;
}
return networkok ;
}
// Upload group to Google
bool GoogleUpdateDialog::updateSingleGoogleContactGroup(GoogleAccess &google, Contact::ContactRecord rec, ContactDatabase &db, ContactDatabase &googledb)
{
ui->updateStatusReport->append(QString(" Updating Google %1 Group").arg(google.getGroupId(rec, true))) ;
bool networkok=google.updateSingleGoogleContactGroup(rec, db, googledb) ;
QString addedstr = google.contactsAddedToGroup() ;
QString deletedstr = google.contactsDeletedFromGroup() ;
if (!addedstr.isEmpty()) ui->updateStatusReport->append(addedstr) ;
if (!deletedstr.isEmpty()) ui->updateStatusReport->append(deletedstr) ;
return networkok ;
}
// 'Download' group from Google
bool GoogleUpdateDialog::updateSingleLocalContactGroup(GoogleAccess &google, Contact::ContactRecord rec, ContactDatabase &db, ContactDatabase &googledb)
{
QString addedstr, deletedstr ;
ui->updateStatusReport->append(QString(" Updating Local %1 Group").arg(google.getGroupId(rec, true))) ;
for (int i=0; i<googledb.size(); i++) {
Contact& googlecontact = googledb.getContact(i) ;
Contact& localcontact = db.getContactById(googlecontact.getField(Contact::ID)) ;
// Do transfer if valid entry and entry exists on google, and transfer requested
bool dotransfer = true ;
if (localcontact.isNull()) dotransfer=false ;
if (googlecontact.getField(Contact::GoogleRecordId).isEmpty()) dotransfer=false ;
if (!localcontact.isSet(Contact::ToBeDownloaded)) dotransfer=false ;
if (dotransfer) {
bool googlegrp = googlecontact.isSet(rec) ;
bool localgrp = localcontact.isSet(rec) ;
if (googlegrp!=localgrp) {
if (googlegrp) {
localcontact.setFlag(rec, true) ;
if (!addedstr.isEmpty()) addedstr = addedstr + "\n" ;
addedstr = addedstr + " + " + googlecontact.getFormattedName(false, false) ;
} else {
localcontact.setFlag(rec, false) ;
if (!deletedstr.isEmpty()) deletedstr = deletedstr + "\n";
deletedstr = deletedstr + " - " + googlecontact.getFormattedName(false, false) ;
}
}
if (state<0) return false ;
}
}
if (!addedstr.isEmpty()) ui->updateStatusReport->append(addedstr) ;
if (!deletedstr.isEmpty()) ui->updateStatusReport->append(deletedstr) ;
return true ;
}