Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Version 0.18 release
[xestiaab/.git] / source / actmgr / frmActivityMgr.cpp
1 // frmActivityMgr.cpp - Activity Manager form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmActivityMgr.h"
20 #include <wx/mstream.h>
21 #include <wx/file.h>
22 #include <wx/ffile.h>
23 #include <wx/tokenzr.h>
24 #include <wx/dir.h>
25 #include <wx/filefn.h>
26 #include <thread>
27 #include <list>
28 #include <curl/curl.h>
30 #include "../bitmaps.h"
31 #include "../enums.h"
32 #include "../common/preferences.h"
33 #include "../frmConflictResolution.h"
34 #include "../frmInvalidSSLCertificate.h"
35 #include "../frmMain.h"
36 #include "../common/events.h"
37 #include "../common/filename.h"
38 #include "../vcard/vcard34conv.h"
39 #include "../vcard/vcard.h"
41 DEFINE_EVENT_TYPE(ACTMGR_RESUMEPROC);
42 DEFINE_EVENT_TYPE(ACTMGR_TOGGLECONN);
43 DEFINE_EVENT_TYPE(ACTMGR_STARTTIMER);
44 DEFINE_EVENT_TYPE(ACTMGR_STOPTIMER);
45 DEFINE_EVENT_TYPE(ACTMGR_STARTPROCESSTIMER);
46 DEFINE_EVENT_TYPE(ACTMGR_STOPPROCESSTIMER);
47 DEFINE_EVENT_TYPE(ACTMGR_UPDATESTATUSLABEL);
48 DEFINE_EVENT_TYPE(ACTMGR_STARTANIMATIONTIMER);
50 BEGIN_EVENT_TABLE(frmActivityMgr, wxWindow)
51   EVT_COMMAND(wxID_ANY, ACTMGR_RESUMEPROC, frmActivityMgr::ResumeProcessing)
52   EVT_COMMAND(wxID_ANY, ACTMGR_TOGGLECONN, frmActivityMgr::ToggleConnectionStatus)
53   EVT_COMMAND(wxID_ANY, ACTMGR_STARTTIMER, frmActivityMgr::StartTimer)
54   EVT_COMMAND(wxID_ANY, ACTMGR_STOPTIMER, frmActivityMgr::StopTimer)
55   EVT_COMMAND(wxID_ANY, ACTMGR_STARTPROCESSTIMER, frmActivityMgr::StartProcessTimer)
56   EVT_COMMAND(wxID_ANY, ACTMGR_STOPPROCESSTIMER, frmActivityMgr::StopProcessTimer)
57   EVT_COMMAND(wxID_ANY, ACTMGR_UPDATESTATUSLABEL, frmActivityMgr::UpdateStatusLabel)
58   EVT_COMMAND(wxID_ANY, ACTMGR_STARTANIMATIONTIMER, frmActivityMgr::StartAnimationTimer)
59 END_EVENT_TABLE()
61 frmActivityMgr::frmActivityMgr( wxWindow* parent )
62 :
63 frmActivityMgrADT( parent )
64 {
66         wxMemoryInputStream istream(icons_activitywindow_png, sizeof(icons_activitywindow_png));
67         wxImage icons_activitywindow_pngi(istream, wxBITMAP_TYPE_PNG);
68         wxBitmap activityiconbmp(icons_activitywindow_pngi, -1);
69         wxIcon activityicon;
70         activityicon.CopyFromBitmap(activityiconbmp);
71         this->SetIcon(activityicon); 
73         wxMemoryInputStream istream2(misc_activitysleep_png, sizeof(misc_activitysleep_png));
74         wxImage misc_activitysleep_pngi(istream2, wxBITMAP_TYPE_PNG);
75         wxBitmap sleepiconbmp(misc_activitysleep_pngi, -1);
76         SleepBitmap = sleepiconbmp;
77         bmpActivity->SetBitmap(SleepBitmap);
78         ActProcessTimer.SetupPointers(this);
79         lblDetails->SetLabel(_("No activity."));
80         
81         ActListProcTimer.SetupPointers(&ActivityList, &ActivityListIndex, &ActivityListTaskDetail, 
82                                         &TasksActive, lstActivity, lblDetails, GauProgress, this, &AnimationTimer,
83                                         &ActProcessTimer, &ActivityListFinDate, &ActivityListConnObject);
84         
85         AnimationTimer.SetBitmap(bmpActivity);
86         AnimationTimer.SetSleepBitmap(&SleepBitmap);
87         AnimationTimer.SetupPointer(this->GetParent());
88         
89         wxListItem ColumnData;
90         ColumnData.SetId(0);
91         ColumnData.SetText(_("Activity"));
92         ColumnData.SetWidth(175);
93         lstActivity->InsertColumn(0, ColumnData);
94         
95         wxListItem ColumnData2;
96         ColumnData2.SetId(1);
97         ColumnData2.SetText(_("Account"));
98         ColumnData2.SetWidth(100);
99         lstActivity->InsertColumn(1, ColumnData2);
101         wxListItem ColumnData3;
102         ColumnData3.SetId(2);
103         ColumnData3.SetText(_("Status"));
104         ColumnData3.SetWidth(100);
105         lstActivity->InsertColumn(2, ColumnData3);
106         
107         //AnimationTimer.StartAnimation();
108         
109         // Create recovery database if it doesn't exist.
110         
111         wxString RecoveryFilename = GetRecoveryDB();
112         
113         wxFile RecoveryDB;
114         
115         if (wxFile::Exists(RecoveryFilename) == FALSE){
116         
117                 // Create the recovery database.
119                 RecoveryDB.Open(RecoveryFilename.c_str(), wxFile::write);       
120         
121                 RecoveryDB.Write(wxT("BEGIN:XAB-RECOVERYDB\r\n"), wxConvUTF8);
122                 RecoveryDB.Write(wxT("VERSION:1.0\r\n"), wxConvUTF8);
123                 RecoveryDB.Write(wxT("COMMENT:Please do not alter or delete this file. DATA LOSS IS POSSIBLE!\r\n"), wxConvUTF8);
124                 RecoveryDB.Write(wxT("END:XAB-RECOVERYDB\r\n"), wxConvUTF8);
125                 RecoveryDB.Close();
127         }
128         
129         wxFFile RecoveryDBF;
130         
131 #if wxABI_VERSION < 20900
132         RecoveryDBF.Open(RecoveryFilename.c_str(), wxT("r"));
133 #else
134         RecoveryDBF.Open(RecoveryFilename, wxT("r"));
135 #endif  
136         
137         // Load the recovery database for tasks not done.
138         
139         if (RecoveryDBF.IsOpened() == TRUE){
140         
141                 wxString wxSRecoveryDB;
142                 wxString RecoveryLine;
143         
144                 // Check if we are using wxWidgets version 2.8 or less and
145                 // execute the required command accordingly.
146         
147                 RecoveryDBF.ReadAll(&wxSRecoveryDB, wxConvAuto());
148         
149                 // Split the lines.
150         
151                 std::map<int, wxString> RecoveryFileLines;
152                 std::map<int, wxString>::iterator striter;
153         
154                 wxStringTokenizer wSTRecoveryDBLines(wxSRecoveryDB, wxT("\r\n"));
156                 int RecoveryLineSeek = 0;
158                 while (wSTRecoveryDBLines.HasMoreTokens() == TRUE){
160                         RecoveryLine = wSTRecoveryDBLines.GetNextToken();
161                         RecoveryFileLines.insert(std::make_pair(RecoveryLineSeek, RecoveryLine));
162                         RecoveryLineSeek++;             
163         
164                 }
165         
166         }
167         
170         //TestTimer.Start(50, FALSE);
173 void frmActivityMgr::OpenWindow()
175         WindowOpen = TRUE;
178 void frmActivityMgr::CloseWindow( wxCommandEvent& event )
180         WindowOpen = FALSE;
181         this->Hide();
184 void frmActivityMgr::CloseWindow( wxCloseEvent& event )
186         WindowOpen = FALSE;
187         this->Hide();
190 void frmActivityMgr::ProcessTasksThread()
192         
193         if (ProcessTasksData == FALSE){
194         
195                 return;
196         
197         }
198         
199         if (ApplicationSleepMode == TRUE){
200                 
201                 return;
202                 
203         }
204         
205         std::map<int,int>::iterator TypeIter;
206         std::map<int,wxString>::iterator StringIter;
207         std::map<int,wxString>::iterator StringDataIter;
208         std::map<int,wxString>::iterator StringURLIter;
209         std::map<int,wxString>::iterator StringDetailIter;
210         std::map<int,wxString>::iterator StringAccountIter;
211         std::map<int,wxString>::iterator StringFilenameIter;
212         std::map<int,wxString>::iterator StringFullFilenameIter;
213         std::map<int,wxString>::iterator StringTaskDetailIter;
214         std::map<int,wxString>::iterator StringETagIter;
215         std::map<int,wxString>::iterator StringETagOrigIter;
216         std::map<int,ConnectionObject*>::iterator ConnObjectIter;
217         std::map<int,ActivityMgrAccountSettings>::iterator AccountSettingsIter;
218         std::map<int,long>::iterator LongIter;
219         bool TasksFoundProc = FALSE;
220         wxString TaskDetail;
221         
222         for (std::map<int,int>::iterator iter = ActivityList.begin(); 
223          iter != ActivityList.end(); ++iter){
224          
225                 // Check for any queued tasks and start one.
226                 
227                 if (iter->second > 4){
228                         continue;
229                 }
230                 
231                 wxCommandEvent StartAnimationEvent(ACTMGR_STARTANIMATIONTIMER);
232                 
233                 wxPostEvent(this, StartAnimationEvent);
234                 
235                 // Start the animation timer if it hasn't started.
236                 
237                 ConnObjectIter = ActivityListConnObject.find(iter->first);
238                 TypeIter = ActivityListType.find(iter->first);
239                 StringETagIter = ActivityListETag.find(iter->first);
240                 StringETagOrigIter = ActivityListETagOriginal.find(iter->first);
241                 StringAccountIter = ActivityListAccount.find(iter->first);
242                 StringFilenameIter = ActivityListFilename.find(iter->first);
243                 StringFullFilenameIter = ActivityListFullFilename.find(iter->first);
244                 AccountSettingsIter = ActivityListAccSettings.find(iter->first);
245                 
246                 // Setup the CardDAV object.
247                 
248                 // 0 = Queued.
249                 // 1 = Processing.
250                 // 2 = Failed.
251                 // 3 = Stopped.
252                 // 4 = Completed.
253                 // 5 = Failed (UI Updated).
254                 // 6 = Stopped (UI Updated).
255                 // 7 = (Reserved) (UI Updated).
256                 // 8 = Completed. (UI Updated). 
257                 
258                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
259                 XABPreferences PrefData(GetUserPrefDir());
260                 
261                 if (iter->second == 0){
262                         
263                         // Wait for process to finish.
265                         TasksActive = TRUE;
266                         TasksFoundProc = TRUE;
267                         wxString ETag;
268                         wxString ETagOriginal;
269                         wxString ETagServer;
270                         wxString AccountDir;
271                         wxString ContactFilename;
272                         wxString FullFilename;
273                         wxString ETagResult;
274                         bool EditMode = FALSE;
276                         iter->second = 1;
277                         
278                         // Compare the ETags and work out if a conflict has occured.
279                         
280                         if (TypeIter->second == 0){
281                         
282                                 // Add a contact.
283                                 
284                                 StringDataIter = ActivityListData.find(iter->first);
285                                 StringURLIter = ActivityListURL.find(iter->first);
286                                 
287                                 AccountDir = StringAccountIter->second;
288                                 ContactFilename = StringFilenameIter->second;
289                                 wxString AccountNameFriendly;
290                                 
291                                 int AccountID = 0;
292                                 
293                                 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
294                                         
295                                         wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
296                                         
297                                         AccountDirCheck.Trim();
298                                         AccountDirCheck.Append(wxT(".carddav"));
299                                         
300                                         if (AccountDirCheck == StringAccountIter->second){
301                                                 
302                                                 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
303                                                 AccountID = i;
304                                                 
305                                         }
306                                         
307                                 }
308                                 
309                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
310                                 
311                                 ETag = StringETagIter->second;
312                                 ETagOriginal = StringETagOrigIter->second;
313                                 AccountDir = StringAccountIter->second;
314                                 ContactFilename = StringFilenameIter->second;
315                                 FullFilename = StringFullFilenameIter->second;
316                                 EditMode = ActivityListEditMode.find(iter->first)->second;
317                                 
318                                 bool KeepUpdating = true;
319                                 
320                                 while(KeepUpdating == true){
321                                         
322                                         COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
323                                         
324                                         bool ExitLoop = false;
325                                         
326                                         while (ExitLoop == false){
327                                                 
328                                                 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
329                                                         
330                                                         frmMainPtrGet->PauseAllTimers();
331                                                         
332 #if defined(__APPLE__)
334                                                         ConnObjectIter->second->BypassSSLVerification(true);
335                                                         
336                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
337                                                         
338                                                         ConnObjectIter->second->BypassSSLVerification(false);
339                                                         
340                                                         int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
341                                                         
342                                                         if (SSLResult != NSOKButton){
343                                                                 
344                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
345                                                                 iter->second = 2;
346                                                                 break;
347                                                                 
348                                                         } else {
349                                                                 
350                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
351                                                                 
352                                                         }
353                                                         
354 #elif defined(__WIN32__)
356                                                         ConnObjectIter->second->BypassSSLVerification(true);
358                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
360                                                         ConnObjectIter->second->BypassSSLVerification(false);
362                                                         BOOL ModifiedCertificateData = false;
363                                                         CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(ConnObjectIter->second, (HWND)frmMainPtrGet->GetHandle());
365                                                         if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
366                                                                 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
367                                                         }
369                                                         if (ModifiedCertificateData == false) {
371                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
372                                                                 iter->second = 2;
373                                                                 break;
375                                                         } else {
377                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
379                                                         }
381 #else
382         
383                                                         bool UsingSSLBypass = false;
384                                                         int SSLResult = 0;
385                                                         
386                                                         // Connect again and fetch SSL certificate information.
387                 
388                                                         ConnObjectIter->second->BypassSSLVerification(true);
389                 
390                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
392                                                         ConnObjectIter->second->BypassSSLVerification(false);
394                                                         SSLInvalidCertNotifObjString SSLICNProcData;
395                                                         
396                                                         SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
397                                                                 
398                                                         bool *PauseMode = new bool;
399                                                         QRNotif qrn;
400                                                                 
401                                                         *PauseMode = TRUE;
402                                                         qrn.QResponse = &SSLResult;
403                                                         qrn.PausePtr = PauseMode;
404                                                                 
405                                                         SSLICNProcData.CertCollection = certcol;
406                                                         SSLICNProcData.QRNotifData = &qrn;
407                                                         SSLICNProcData.AccountName = AccountNameFriendly;
408                                                                 
409                                                         wxCommandEvent event(INVALIDSSLCERTSTRING);
410                                                         event.SetClientData(&SSLICNProcData);
411                                                         wxPostEvent(frmMainPtrGet, event);
412                                                                 
413                                                         while (*PauseMode == TRUE){
414                                                                 //nanosleep(&n1, &n2);
415                                                                 SleepFor(250000000);
416                                                         }
417                                                         
418                                                         // Process the response from the user.
419                                                         
420                                                         if (SSLResult == 1){
421                                                                 
422                                                                 // Accept the Certificate.
424                                                                 UsingSSLBypass = true;
425                                                                 ConnObjectIter->second->BypassSSLVerification(true);
426                 
427                                                                 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
428                                                                 WriteServerCertificate(AccountDir, certcol);
429                                                                 
430                                                                 ConnObjectIter->second->BypassSSLVerification(false);
431                                                                 
432                                                         } else if (SSLResult == 2){
433                                                 
434                                                                 // Reject the certificate, abort the task and mark as failed.
436                                                                 iter->second = 2;
437                                                                 break;
438                                                                 
439                                                         }
440                 
441 #endif
442                                                         
443                                                         frmMainPtrGet->ResumeAllTimers();
444                                                         ExitLoop = true;
445                                                         
446                                                 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
447                                                         
448                                                         ConnectResponse = ConnObjectIter->second->Connect(true);
449                                                         
450                                                         if (ConnectResponse == COCONNECT_OK){
451                                                                 
452                                                                 ExitLoop = true;
453                                                                 break;
454                                                                 
455                                                         } else {
456                                                                 
457                                                                 ExitLoop = true;                                                        
458                                                                 iter->second = 2;
459                                                                 break;
460                                                                 
461                                                         }
462                                                         
463                                                 } else if (ConnectResponse == COCONNECT_OK){
464                                                         
465                                                         ConnectResponse = ConnObjectIter->second->Connect(true);
466                                                         
467                                                         ExitLoop = true;
468                                                         break;
469                                                         
470                                                 } else {
471                                                         
472                                                         ExitLoop = true;                                                        
473                                                         iter->second = 2;
474                                                         break;
475                                                         
476                                                 }
477                                                 
478                                         }
479                                         
480                                         if (iter->second == 2 || iter->second == 3 || iter->second == 4){
481                                                 break;
482                                         }
483                                         
484 #if defined(__APPLE__)
485                                         
486                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
488 #elif defined(__WIN32__)
490                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
492 #else
493                                         
494                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
495                                         
496 #endif
497                                         
498                                         COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(StringURLIter->second.ToStdString(), 
499                                                         StringDataIter->second.ToStdString());
500                                         
501                                         if (AddContactResponse.RequestResult != COREQUEST_OK){
502                                                 iter->second = 2;
503                                                 break;
504                                         }
505                                         
506                                         // Get the entity tag for the new contact.
507                                         
508                                         COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
509                                         
510                                         if (ETagGetResponse.RequestResult != COREQUEST_OK){
511                                                 iter->second = 2;
512                                                 break;
513                                         }
514                                         
515                                         ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag);
516                                         
517                                         iter->second = 4;
518                                         break;
519                                         
520                                 }
521                                 
522                         } else if (TypeIter->second == 1){
523                                 
524                                 // Update a contact.
525                                 
526                                 StringDataIter = ActivityListData.find(iter->first);
527                                 StringURLIter = ActivityListURL.find(iter->first);
528                                 
529                                 AccountDir = StringAccountIter->second;
530                                 ContactFilename = StringFilenameIter->second;
531                                 wxString AccountNameFriendly;
532                                 
533                                 int AccountID = 0;
534                                 
535                                 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
536                                         
537                                         wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
538                                         
539                                         AccountDirCheck.Trim();
540                                         AccountDirCheck.Append(wxT(".carddav"));
541                                         
542                                         if (AccountDirCheck == StringAccountIter->second){
543                                                 
544                                                 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
545                                                 AccountID = i;
546                                                 
547                                         }
548                                         
549                                 }
550                                 
551                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
552                                 
553                                 ETag = StringETagIter->second;
554                                 ETagOriginal = StringETagOrigIter->second;
555                                 AccountDir = StringAccountIter->second;
556                                 ContactFilename = StringFilenameIter->second;
557                                 FullFilename = StringFullFilenameIter->second;
558                                 EditMode = ActivityListEditMode.find(iter->first)->second;
559                                 
560                                 bool KeepUpdating = true;
561                                 
562                                 while(KeepUpdating == true){
563                                         
564                                         COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
565                                         
566                                         bool ExitLoop = false;
567                                         
568                                         while(ExitLoop == false){
569                                                 
570                                                 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
571                                                         
572                                                         frmMainPtrGet->PauseAllTimers();
573                                                         
574 #if defined(__APPLE__)
575                                                         
576                                                         ConnObjectIter->second->BypassSSLVerification(true);
577                                                         
578                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
579                                                         
580                                                         ConnObjectIter->second->BypassSSLVerification(false);
581                                                         
582                                                         int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
583                                                         
584                                                         if (SSLResult != NSOKButton){
585                                                                 
586                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
587                                                                 iter->second = 2;
588                                                                 break;
589                                                                 
590                                                         } else {
591                                                                 
592                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
593                                                                 
594                                                         }
595                                                         
596 #elif defined(__WIN32__)
598                                                         ConnObjectIter->second->BypassSSLVerification(true);
600                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
602                                                         ConnObjectIter->second->BypassSSLVerification(false);
604                                                         BOOL ModifiedCertificateData = false;
605                                                         CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(ConnObjectIter->second, (HWND)frmMainPtrGet->GetHandle());
607                                                         if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
608                                                                 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
609                                                         }
611                                                         if (ModifiedCertificateData == FALSE) {
613                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
614                                                                 iter->second = 2;
615                                                                 break;
617                                                         } else {
619                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
621                                                         }
623 #else
624         
625                                                         bool UsingSSLBypass = false;
626                                                         int SSLResult = 0;
627                                                         
628                                                         // Connect again and fetch SSL certificate information.
629                 
630                                                         ConnObjectIter->second->BypassSSLVerification(true);
631                 
632                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
634                                                         ConnObjectIter->second->BypassSSLVerification(false);
636                                                         SSLInvalidCertNotifObjString SSLICNProcData;
637                                                         
638                                                         SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
639                                                                 
640                                                         bool *PauseMode = new bool;
641                                                         QRNotif qrn;
642                                                                 
643                                                         *PauseMode = TRUE;
644                                                         qrn.QResponse = &SSLResult;
645                                                         qrn.PausePtr = PauseMode;
646                                                                 
647                                                         SSLICNProcData.CertCollection = certcol;
648                                                         SSLICNProcData.QRNotifData = &qrn;
649                                                         SSLICNProcData.AccountName = AccountNameFriendly;
650                                                                 
651                                                         wxCommandEvent event(INVALIDSSLCERTSTRING);
652                                                         event.SetClientData(&SSLICNProcData);
653                                                         wxPostEvent(frmMainPtrGet, event);
654                                                                 
655                                                         while (*PauseMode == TRUE){
656                                                                 SleepFor(250000000);
657                                                         }
658                                                         
659                                                         // Process the response from the user.
660                                                         
661                                                         if (SSLResult == 1){
662                                                                 
663                                                                 // Accept the Certificate.
665                                                                 UsingSSLBypass = true;
666                                                                 ConnObjectIter->second->BypassSSLVerification(true);
667                 
668                                                                 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
669                                                                 WriteServerCertificate(AccountDir, certcol);
670                                                                 
671                                                                 ConnObjectIter->second->BypassSSLVerification(false);
672                                                                 
673                                                         } else if (SSLResult == 2){
674                                                 
675                                                                 // Reject the certificate, abort the task and mark as failed.
677                                                                 iter->second = 2;
678                                                                 break;
679                                                                 
680                                                         }
681                 
682 #endif
683                                                         
684                                                         frmMainPtrGet->ResumeAllTimers();
685                                                         ExitLoop = true;
686                                                         
687                                                 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
688                                                         
689                                                         ConnectResponse = ConnObjectIter->second->Connect(true);
690                                                         
691                                                         if (ConnectResponse == COCONNECT_OK){
692                                                                 
693                                                                 ExitLoop = true;
694                                                                 break;
695                                                                 
696                                                         } else {
697                                                                 
698                                                                 ExitLoop = true;                                                        
699                                                                 iter->second = 2;
700                                                                 break;
701                                                                 
702                                                         }
703                                                         
704                                                 } else if (ConnectResponse == COCONNECT_OK){
705                                                         
706                                                         ConnectResponse = ConnObjectIter->second->Connect(true);
707                                                         
708                                                         ExitLoop = true;
709                                                         break;
710                                                         
711                                                 } else {
712                                                         
713                                                         ExitLoop = true;                                                        
714                                                         iter->second = 2;
715                                                         break;
716                                                         
717                                                 }
718                                                 
719                                         }
720                                         
721                                         if (iter->second == 2 || iter->second == 3 || iter->second == 4){
722                                                 break;
723                                         }
724                                         
725                                         COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
726                                         
727                                         if (ETagGetResponse.RequestResult != COREQUEST_OK){
728                                                 iter->second = 2;
729                                                 break;
730                                         }
731                                         
732                                         ETagServer = ETagGetResponse.EntityTag;
733                                         
734                                         // Compare the ETag with the Server ETag.
735                                         
736                                         if (ETagOriginal != ETagServer){
737                                                 
738                                                 // Server ETag is different from original ETag.
739                                                 // This is a conflict which the user will now
740                                                 // need to resolve.
741                                                 
742                                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
743                                                 
744                                                 vCard34Conv vCard34Obj;
745                                                 
746                                                 std::string ServerContactData;
747                                                 wxString wxStringServerContactData;
748                                                 vCard ClientData;
749                                                 vCard ConvertedV4Data;
750                                                 
751                                                 // Load the client data from the filename given.
752                                                 
753                                                 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(StringURLIter->second.ToStdString(),
754                                                         &ServerContactData);
755                                                 
756                                                 if (GetContactResponse.RequestResult != COREQUEST_OK){
757                                                         iter->second = 2;
758                                                         break;
759                                                 }
760                                                 
761                                                 wxStringServerContactData = wxString::FromUTF8((const char*)ServerContactData.c_str());
762                                                 
763                                                 // Process v3 version into v4 version.
764                                                 
765                                                 vCard34Obj.ConvertToV4(&wxStringServerContactData, &ConvertedV4Data);
766                                                 
767                                                 vCardConflictObj vCardProcData;
768                                                 
769                                                 vCardProcData.vCardLocalData = &ClientData;
770                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
771                                                 ClientData.LoadFile(FullFilename);
772                                                 
773                                                 // Setup Conflict Resolution Dialog.
774                                                 
775                                                 // Fetch Data from Server and convert.
776                                                 
777                                                 bool *PauseMode = new bool;
778                                                 int ConflictResult;
779                                                 QRNotif qrn;
780                                                 
781                                                 *PauseMode = TRUE;
782                                                 qrn.QResponse = &ConflictResult;
783                                                 qrn.PausePtr = PauseMode;
784                                                 
785                                                 vCardProcData.QRNotifData = &qrn;
786                                                 
787                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
788                                                 event.SetClientData(&vCardProcData);
789                                                 wxPostEvent(frmMainPtrGet, event);
790                                                 
791                                                 while (*PauseMode == TRUE){
792                                                         SleepFor(250000000);
793                                                 }
794                                                 
795                                                 delete PauseMode;
796                                                 PauseMode = NULL;
797                                                 
798                                                 if (ConflictResult == 0){
799                                                         
800                                                         // Decide Later.
801                                                         
802                                                         iter->second = 4;
803                                                         break;
804                                                         
805                                                 } else if (ConflictResult == 1){
806                                                         
807                                                         // Decided to use client version.
808                                                         
809                                                 } else if (ConflictResult == 2){
810                                                         
811                                                         // Decided to use server version.
812                                                         
813                                                         // Download server version.
814                                                         
815                                                         ConvertedV4Data.WriteFile(FullFilename);
816                                                         
817                                                 }
818                                                 
819                                         } else {
820                                                 
821                                                 // Upload the data to the server.
822                                                 
823                                                 COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(StringURLIter->second.ToStdString(), 
824                                                         StringDataIter->second.ToStdString());
825                                         
826                                                 if (EditContactResponse.RequestResult != COREQUEST_OK){
827                                                         iter->second = 2;
828                                                         break;
829                                                 }
830                                                 
831                                         }
832                                         
833                                         ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
834                                         
835                                         if (ETagGetResponse.RequestResult != COREQUEST_OK){
836                                                 iter->second = 2;
837                                                 break;
838                                         }
839                                         
840                                         ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag);
841                                         
842                                         iter->second = 4;
843                                         break;
844                                         
845                                 }
846                         
847                         } else if (TypeIter->second == 2) {
848                         
849                                 // Delete the contact from the server and update the ETag DB.
850                                 
851                                 AccountDir = StringAccountIter->second;
852                                 ContactFilename = StringFilenameIter->second;
853                                 wxString AccountNameFriendly;
854                                 
855                                 int AccountID = 0;
856                                 
857                                 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
858                                         
859                                         wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
860                                         
861                                         AccountDirCheck.Trim();
862                                         AccountDirCheck.Append(wxT(".carddav"));
863                                         
864                                         if (AccountDirCheck == StringAccountIter->second){
865                                                 
866                                                 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
867                                                 AccountID = i;
868                                                 
869                                         }
870                                         
871                                 }
872                                 
873                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
874                                 
875                                 bool KeepUpdating = TRUE;
876                                 
877                                 while(KeepUpdating == TRUE){
878                                 
879                                         COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
880                                         
881                                         bool ExitLoop = FALSE;
882                                         
883                                         while (ExitLoop == FALSE){
884                                                 
885                                                 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
886                                                         
887                                                         frmMainPtrGet->PauseAllTimers();
888                                                         
889 #if defined(__APPLE__)
890                                                         
891                                                         ConnObjectIter->second->BypassSSLVerification(true);
892                                                         
893                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
894                                                         
895                                                         ConnObjectIter->second->BypassSSLVerification(false);
896                                                         
897                                                         int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
898                                                         
899                                                         if (SSLResult != NSOKButton){
900                                                                 
901                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
902                                                                 iter->second = 2;
903                                                                 break;
904                                                                 
905                                                         } else {
906                                                                 
907                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
908                                                                 
909                                                         }
910                                                         
911 #elif defined(__WIN32__)
913                                                         ConnObjectIter->second->BypassSSLVerification(true);
915                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
917                                                         ConnObjectIter->second->BypassSSLVerification(false);
919                                                         BOOL ModifiedCertificateData = false;
920                                                         CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(ConnObjectIter->second, (HWND)frmMainPtrGet->GetHandle());
922                                                         if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
923                                                                 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
924                                                         }
926                                                         if (ModifiedCertificateData == false) {
928                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
929                                                                 iter->second = 2;
930                                                                 break;
932                                                         } else {
934                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
936                                                         }
938 #else
939         
940                                                         bool UsingSSLBypass = false;
941                                                         int SSLResult = 0;
942                                                         
943                                                         // Connect again and fetch SSL certificate information.
944                 
945                                                         ConnObjectIter->second->BypassSSLVerification(true);
946                 
947                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
949                                                         ConnObjectIter->second->BypassSSLVerification(false);
951                                                         SSLInvalidCertNotifObjString SSLICNProcData;
952                                                         
953                                                         SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
954                                                                 
955                                                         bool *PauseMode = new bool;
956                                                         QRNotif qrn;
957                                                                 
958                                                         *PauseMode = TRUE;
959                                                         qrn.QResponse = &SSLResult;
960                                                         qrn.PausePtr = PauseMode;
961                                                                 
962                                                         SSLICNProcData.CertCollection = certcol;
963                                                         SSLICNProcData.QRNotifData = &qrn;
964                                                         SSLICNProcData.AccountName = AccountNameFriendly;
965                                                                 
966                                                         wxCommandEvent event(INVALIDSSLCERTSTRING);
967                                                         event.SetClientData(&SSLICNProcData);
968                                                         wxPostEvent(frmMainPtrGet, event);
969                                                                 
970                                                         while (*PauseMode == TRUE){
971                                                                 //nanosleep(&n1, &n2);
972                                                                 SleepFor(250000000);
973                                                         }
974                                                         
975                                                         // Process the response from the user.
976                                                         
977                                                         if (SSLResult == 1){
978                                                                 
979                                                                 // Accept the Certificate.
981                                                                 UsingSSLBypass = true;
982                                                                 ConnObjectIter->second->BypassSSLVerification(true);
983                 
984                                                                 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
985                                                                 WriteServerCertificate(AccountDir, certcol);
986                                                                 
987                                                                 ConnObjectIter->second->BypassSSLVerification(false);
988                                                                 
989                                                         } else if (SSLResult == 2){
990                                                 
991                                                                 // Reject the certificate, abort the task and mark as failed.
993                                                                 iter->second = 2;
994                                                                 break;
995                                                                 
996                                                         }
997                 
998 #endif
999                                                         
1000                                                         frmMainPtrGet->ResumeAllTimers();
1001                                                         ExitLoop = true;
1002                                                         
1003                                                 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
1004                                                         
1005                                                         ConnectResponse = ConnObjectIter->second->Connect(true);
1006                                                         
1007                                                         if (ConnectResponse == COCONNECT_OK){
1008                                                                 
1009                                                                 ExitLoop = true;
1010                                                                 break;
1011                                                                 
1012                                                         } else {
1013                                                                 
1014                                                                 ExitLoop = true;                                                        
1015                                                                 iter->second = 2;
1016                                                                 break;
1017                                                                 
1018                                                         }
1019                                                         
1020                                                 } else if (ConnectResponse == COCONNECT_OK){
1021                                                         
1022                                                         ConnectResponse = ConnObjectIter->second->Connect(true);
1023                                                         
1024                                                         ExitLoop = true;
1025                                                         break;
1026                                                         
1027                                                 } else {
1028                                                         
1029                                                         ExitLoop = true;                                                        
1030                                                         iter->second = 2;
1031                                                         break;
1032                                                         
1033                                                 }
1034                                         
1035                                         }
1036                                 
1037                                         if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1038                                                 break;
1039                                         }
1040                         
1041 #if defined(__APPLE__)
1042                                 
1043                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1045 #elif defined(__WIN32__)
1047                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1049 #else
1050                                 
1051                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1052                                 
1053 #endif
1054                                         
1055                                         COServerResponse DeleteContactResponse = ConnObjectIter->second->DeleteContact(ContactFilename.ToStdString());
1056                                         
1057                                         if (DeleteContactResponse.RequestResult != COREQUEST_OK){
1058                                                 iter->second = 2;
1059                                                 break;
1060                                         }
1061                                 
1062                                         ETagDBPtr->RemoveETag(ContactFilename);
1063                                 
1064                                         iter->second = 4;
1065                                         break;
1066                                 
1067                                 }
1068                         
1069                         } else if (TypeIter->second == 3) {
1071                                 wxString AccountNameFriendly;
1072         
1073                                 int AccountID = 0;
1075                                 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
1076         
1077                                         wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
1078                 
1079                                         AccountDirCheck.Trim();
1080                                         AccountDirCheck.Append(wxT(".carddav"));
1081         
1082                                         if (AccountDirCheck == StringAccountIter->second){
1083                                         
1084                                                 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
1085                                                 AccountID = i;
1086                 
1087                                         }
1088         
1089                                 }
1090                         
1091                                 bool KeepUpdating = TRUE;
1093                                 while (KeepUpdating == TRUE){
1095                                         StringTaskDetailIter = ActivityListTaskDetail.find(iter->first);
1096                         
1097                                         AccountDir = StringAccountIter->second;
1099                                         ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1100                                 
1101                                         wxArrayString vCardAdd;
1102                                         wxArrayString vCardDelete;
1103                                         wxArrayString vCardList;
1104                                         wxArrayString vCardUpdateLocal;
1105                                         wxArrayString vCardUpdateRemote;
1106                                         wxArrayString vCardUpdateLog;
1107                                         std::map<wxString,wxString> vCardETag;
1108                                 
1109                                         std::map<wxString,vCardDownloadDataFileClass> vCardDownloadData;
1110                                 
1111                                         // Get the list of vCard files locally.
1112         
1113                                         wxString AccountDirFinal;
1114                                         wxString AccountSyncTokenFilename;
1115                                         wxString SyncTokenLoad;
1116         
1117 #if defined(__HAIKU__)
1118     
1119                                             //preffilename = wxT("noo");
1121 #elif defined(__WIN32__)
1123                                         AccountDirFinal = GetAccountDir(AccountDir, FALSE);
1124                                         AccountSyncTokenFilename = AccountDirFinal;
1125                                         AccountSyncTokenFilename.Append(wxT("synctoken"));
1127 #else
1129                                         AccountDirFinal = GetAccountDir(AccountDir, FALSE);
1130                                         AccountSyncTokenFilename = AccountDirFinal;
1131                                         AccountSyncTokenFilename.Append(wxT("synctoken"));
1132         
1133 #endif
1135                                         // Suppress Error Messages.
1136                                 
1137                                         wxLogNull PleaseDoNotDisturb;
1139                                         wxFFile SyncLoad;
1140                                         SyncLoad.Open(AccountSyncTokenFilename, wxT("r"));
1141                                 
1142                                         if (SyncLoad.IsOpened()){
1143                                 
1144                                                 SyncLoad.ReadAll(&SyncTokenLoad, wxMBConvUTF8());
1145                                                 SyncLoad.Close();
1146                                 
1147                                         }
1149                                         // Get a list of vCard files from the server.
1151                                         // Get the list of vCard files from the ETagDB.
1152                                 
1153                                         std::map<wxString,ETagData> ETagDBData = ETagDBPtr->GetETagData();
1154                                         
1155                                         COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
1156                                         
1157                                         bool ExitLoop = FALSE;
1158                                         
1159                                         while (ExitLoop == FALSE){                                              
1160                                                 
1161                                                 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
1162                                                         
1163                                                         frmMainPtrGet->PauseAllTimers();
1164                                                         
1165 #if defined(__APPLE__)
1166                                                         
1167                                                         ConnObjectIter->second->BypassSSLVerification(true);
1168                                                         
1169                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1170                                                         
1171                                                         ConnObjectIter->second->BypassSSLVerification(false);
1172                                                         
1173                                                         int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
1174                                                         
1175                                                         if (SSLResult != NSOKButton){
1176                                                                 
1177                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
1178                                                                 iter->second = 2;
1179                                                                 break;
1180                                                                 
1181                                                         } else {
1182                                                                 
1183                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1184                                                                 
1185                                                         }
1186                                                         
1187 #elif defined(__WIN32__)
1189                                                         ConnObjectIter->second->BypassSSLVerification(true);
1191                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1193                                                         ConnObjectIter->second->BypassSSLVerification(false);
1195                                                         BOOL ModifiedCertificateData = false;
1196                                                         CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(ConnObjectIter->second, (HWND)frmMainPtrGet->GetHandle());
1198                                                         if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
1199                                                                 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
1200                                                         }
1202                                                         if (ModifiedCertificateData == false) {
1204                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
1205                                                                 iter->second = 2;
1206                                                                 break;
1208                                                         } else {
1210                                                                 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1212                                                         }
1214 #else
1215         
1216                                                         bool UsingSSLBypass = false;
1217                                                         int SSLResult = 0;
1218                                                         
1219                                                         // Connect again and fetch SSL certificate information.
1220                 
1221                                                         ConnObjectIter->second->BypassSSLVerification(true);
1222                 
1223                                                         COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1225                                                         ConnObjectIter->second->BypassSSLVerification(false);
1227                                                         SSLInvalidCertNotifObjString SSLICNProcData;
1228                                                         
1229                                                         SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
1230                                                                 
1231                                                         bool *PauseMode = new bool;
1232                                                         QRNotif qrn;
1233                                                                 
1234                                                         *PauseMode = TRUE;
1235                                                         qrn.QResponse = &SSLResult;
1236                                                         qrn.PausePtr = PauseMode;
1237                                                                 
1238                                                         SSLICNProcData.CertCollection = certcol;
1239                                                         SSLICNProcData.QRNotifData = &qrn;
1240                                                         SSLICNProcData.AccountName = AccountNameFriendly;
1241                                                                 
1242                                                         wxCommandEvent event(INVALIDSSLCERTSTRING);
1243                                                         event.SetClientData(&SSLICNProcData);
1244                                                         wxPostEvent(frmMainPtrGet, event);
1245                                                                 
1246                                                         while (*PauseMode == TRUE){
1247                                                                 //nanosleep(&n1, &n2);
1248                                                                 SleepFor(250000000);
1249                                                         }
1250                                                         
1251                                                         // Process the response from the user.
1252                                                         
1253                                                         if (SSLResult == 1){
1254                                                                 
1255                                                                 // Accept the Certificate.
1257                                                                 UsingSSLBypass = true;
1258                                                                 ConnObjectIter->second->BypassSSLVerification(true);
1259                 
1260                                                                 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
1261                                                                 WriteServerCertificate(AccountDir, certcol);
1262                                                                 
1263                                                                 ConnObjectIter->second->BypassSSLVerification(false);
1264                                                                 
1265                                                         } else if (SSLResult == 2){
1266                                                 
1267                                                                 // Reject the certificate, abort the task and mark as failed.
1269                                                                 iter->second = 2;
1270                                                                 break;
1271                                                                 
1272                                                         }
1273                 
1274 #endif
1275                                                         
1276                                                         frmMainPtrGet->ResumeAllTimers();
1277                                                         ExitLoop = true;
1278                                                         
1279                                                 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
1280                                                         
1281                                                         ConnectResponse = ConnObjectIter->second->Connect(true);
1282                                                         
1283                                                         if (ConnectResponse == COCONNECT_OK){
1284                                                                 
1285                                                                 ExitLoop = true;
1286                                                                 break;
1287                                                                 
1288                                                         } else {
1289                                                                 
1290                                                                 ExitLoop = true;                                                        
1291                                                                 iter->second = 2;
1292                                                                 break;
1293                                                                 
1294                                                         }
1295                                                         
1296                                                 } else if (ConnectResponse == COCONNECT_OK){
1297                                                         
1298                                                         ConnectResponse = ConnObjectIter->second->Connect(true);
1299                                                         
1300                                                         if (ConnectResponse == COCONNECT_OK){
1301                                                                 
1302                                                                 ExitLoop = true;
1303                                                                 break;
1304                                                                 
1305                                                         } else {
1306                                                                 
1307                                                                 ExitLoop = true;
1308                                                                 iter->second = 2;
1309                                                                 break;
1310                                                                 
1311                                                         }
1312                                                         
1313                                                         ExitLoop = true;
1314                                                         break;
1315                                                         
1316                                                 } else {
1317                                                         
1318                                                         ExitLoop = true;                                                        
1319                                                         iter->second = 2;
1320                                                         break;
1321                                                         
1322                                                 }
1323                                         
1324                                         }
1325                                 
1326 #if defined(__APPLE__)
1327                                         
1328                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1330 #elif defined(__WIN32__)
1332                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1334 #else
1335                                         
1336                                         frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1337                         
1338 #endif
1339                                         
1340                                         if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1341                                                 break;
1342                                         }
1343                                         
1344                                         // Look in directory and see which vCards to add and delete
1345                                 
1346                                         // In short:
1347                                         // vCard exists but not in ETagDB - Add (and upload).
1348                                         // vCard missing but in ETagDB - Delete (from server).
1349                                 
1350                                         // Start by looking for vCards that are not in the ETagDB.
1351                                 
1352                                         wxDir AccDir(AccountDirFinal);
1353                                         wxString AccDirFilename;
1354                                 
1355                                         bool ContinueProcess = AccDir.GetFirst(&AccDirFilename, wxEmptyString, wxDIR_DEFAULT);
1356                                 
1357                                         COContactList ServerContactList2 = ConnObjectIter->second->GetContactList(SyncTokenLoad.ToStdString());
1359                                         // Process the server data.
1361                                         for (std::vector<COContactData>::iterator ContactInfo = ServerContactList2.ListData.begin();
1362                                                 ContactInfo != ServerContactList2.ListData.end(); ContactInfo++){
1363                                                 
1364                                                 if (ContactInfo->Status == COCS_UPDATED){
1365                                                         
1366                                                         vCardDownloadDataFileClass vCardNewData;
1367                                                         vCardNewData.DataType = 0;
1368                                                         vCardDownloadData.insert(std::make_pair(ContactInfo->Location, vCardNewData));
1369                                                         
1370                                                 } else if (ContactInfo->Status == COCS_DELETED){
1371                                                         
1372                                                         vCardDelete.Add(ContactInfo->Location, 1);
1373                                                         
1374                                                 }
1375                                                         
1376                                         }
1377                                 
1378                                         // Look for vCards that are missing but in the ETagDB.
1379         
1380                                         for (std::map<wxString,ETagData>::iterator etagiter = ETagDBData.begin();
1381                                                 etagiter != ETagDBData.end(); etagiter++){
1382                                 
1383                                                 // Check if the vCard exists.
1384                                         
1385                                                 if (!wxFileExists(AccountDirFinal + etagiter->first)){
1386                                         
1387                                                         // vCard doesn't exist so mark for deleting.
1388                                         
1389                                                         vCardDelete.Add(etagiter->first, 1);
1390                                         
1391                                                 }
1392                                 
1393                                         }
1395                                         // Compare Server with the ETag DB. Work out which
1396                                         // needs to updated remotely and which needs to be
1397                                         // updated locally.
1399                                         for (std::vector<COContactData>::iterator ContactInfo = ServerContactList2.ListData.begin();
1400                                                 ContactInfo != ServerContactList2.ListData.end(); ContactInfo++){
1401                                                 
1402                                                 if (ETagDBData.find(ContactInfo->Location) == ETagDBData.end()){
1403                                                         continue;
1404                                                 }
1406                                                 std::map<wxString,ETagData>::iterator lociter = ETagDBData.find(ContactInfo->Location);
1407                                                 
1408                                                 ETagData etd = lociter->second;
1409                                                 
1410                                                 wxString ETagSrv = ContactInfo->Data;
1412                                                 // Check if the local ETag value has 'DELETED' set and skip
1413                                                 // checking this file if it does.
1414                                         
1415                                                 if (etd.ETagValue == "DELETED"){
1416                                                         
1417                                                         continue;
1418                                                         
1419                                                 }
1420                                                 
1421                                                 // Compare local ETag with original local ETag.
1422                                                 // If different, update to server is required.
1423                                 
1424                                                 if (etd.ETagValue != etd.ETagOrigValue){
1425                                 
1426                                                         // Check if local original ETag is different from server ETag.
1427                                                         // If it is then throw up a conflict resolution dialog.
1428                                 
1429                                                         if (etd.ETagOrigValue != ETagSrv){
1430                                         
1431                                                                 vCard34Conv vCard34Obj;
1432                                                 
1433                                                                 wxString FullFilename;
1434                                                                 vCard ClientData;
1435                                                                 vCard ConvertedV4Data;
1436                                         
1437                                                                 std::string ServerContactDataString;
1438                                                                 wxString ServerContactData;
1439                                                                 
1440                                                                 // Conflict. Bring up conflict resolution dialog.
1441                                                         
1442                                                                 // Get the server contact data for comparison.
1443                                                         
1444                                                                 ConnObjectIter->second->GetContact(ContactInfo->Location, &ServerContactDataString);
1445                                                                 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1446                                                         
1447                                                                 FullFilename = CreateFilenamePath(AccountDirFinal, ContactInfo->Location);
1448                                                         
1449                                                                 // Convert it to v4.
1450                                         
1451                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1452                                         
1453                                                                 vCardConflictObj vCardProcData;
1454                                         
1455                                                                 vCardProcData.vCardLocalData = &ClientData;
1456                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1457                                                         
1458                                                                 // Load the client data from the filename given.
1459                                                         
1460                                                                 ClientData.LoadFile(FullFilename);
1461                                                         
1462                                                                 // Show dialog.
1463                                         
1464                                                                 bool *PauseMode = new bool;
1465                                                                 int ConflictResult;
1466                                                                 QRNotif qrn;
1467                                                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1469                                                                 *PauseMode = TRUE;
1470                                                                 qrn.QResponse = &ConflictResult;
1471                                                                 qrn.PausePtr = PauseMode;
1472                                                 
1473                                                                 vCardProcData.QRNotifData = &qrn;
1474                                         
1475                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1476                                                                 event.SetClientData(&vCardProcData);
1477                                                                 wxPostEvent(frmMainPtrGet, event);
1478                                                 
1479                                                                 while (*PauseMode == TRUE){
1480                                                                         //nanosleep(&n1, &n2);
1481                                                                         SleepFor(250000000);
1482                                                                 }
1483                                         
1484                                                                 delete PauseMode;
1485                                                                 PauseMode = NULL;
1486                                                         
1487                                                                 // If replacing local version, store copy for now.
1488                                                                 // Otherwise upload to server.
1489                                         
1490                                                                 if (ConflictResult == 0){
1491                                         
1492                                                                         // Decide Later.
1493                                         
1494                                                                         continue;
1495                                         
1496                                                                 } else if (ConflictResult == 1){
1497                                         
1498                                                                         // Decided to use client version.
1499                                         
1500                                                                         vCardUpdateLocal.Remove(ContactInfo->Location);
1501                                                                         vCardUpdateRemote.Add(ContactInfo->Location, 1);                                                                
1502                                         
1503                                                                 } else if (ConflictResult == 2){
1504                                         
1505                                                                         // Decided to use server version.
1506                                                                         // Store the converted downloaded copy for
1507                                                                         // processing later.
1509                                                                         //vCardUpdateLocal.Add(srviter->first, 1);
1510                                                                 
1511                                                                         vCardDownloadDataFileClass vCardNewData;
1512                                                                         vCardNewData.DataType = 1;
1513                                                                         vCardNewData.FileData = ConvertedV4Data.WriteString();
1515                                                                         ConvertedV4Data.WriteFile(FullFilename);
1516                                                                 
1517                                                                         vCardDownloadData.insert(std::make_pair(ContactInfo->Location, vCardNewData));  
1518                                                                 
1519                                                                 
1520                                                                 
1521                                                                 }
1522                                         
1523                                                         } else {
1524                                         
1525                                                                 // Send vCard to the server.
1526                                                 
1527                                                                 vCardUpdateRemote.Add(ContactInfo->Location, 1);
1528                                         
1529                                                         }
1530                                 
1531                                                 } else {
1532                                 
1533                                                         // Check if local ETag is different from server ETag.
1534                                                         // If different, obtain the vCard from the server.
1535                                         
1536                                                         if (etd.ETagOrigValue != ETagSrv){
1537                                         
1538                                                                 vCardUpdateLocal.Add(ContactInfo->Location, 1);
1539                                         
1540                                                         }
1541                                 
1542                                                 }
1543                                                 
1544                                         }
1545                                 
1546                                         // Process the vCards that need to be downloaded and added/overwritten.
1547                                 
1548                                         wxString vCardFilenameFinal;
1549                                         wxString vCardOutputData;
1550                                         wxString vCardInputData;
1551                                         wxString ETagFileData;
1552                                         wxFFile vCardFile;
1553                                 
1554                                         for (std::map<wxString,vCardDownloadDataFileClass>::iterator additer = vCardDownloadData.begin();
1555                                                 additer != vCardDownloadData.end(); additer++){
1556                                         
1557                                                 vCardFilenameFinal.Clear();
1558                                                 vCardInputData.Clear();
1559                                                 vCardOutputData.Clear();
1560                                                 ETagFileData.Clear();
1561                                         
1562                                                 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1563                                         
1564                                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1565                                         
1566                                                 if (additer->second.DataType == 0){
1567                                         
1568                                                         vCard ConvertedV4Data;
1569                                         
1570                                                         // Grab the file from the server and write the contact out.
1571                                         
1572                                                         vCard34Conv vCard34Obj;
1573                                                         
1574                                                         wxString ServerContactData;
1575                                                         std::string ServerContactDataString;
1576                                                                 
1577                                                         // Conflict. Bring up conflict resolution dialog.
1578                                                         
1579                                                         // Get the server contact data for comparison.
1580                                                         
1581                                                         ConnObjectIter->second->GetContact(additer->first.ToStdString(), &ServerContactDataString);
1582                                                         vCardInputData = wxString::FromUTF8(ServerContactDataString.c_str());
1583                                                 
1584                                                         vCard34Obj.ConvertToV4(&vCardInputData, &ConvertedV4Data);
1585                                                 
1586                                                         ConvertedV4Data.WriteFile(vCardFilenameFinal);
1587                                                 
1588                                                         // Get the server ETag.
1589                                                 
1590                                                         wxString ServerETag;
1591                                                         
1592                                                         COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(additer->first.ToStdString());
1593                                                         
1594                                                         if (ServerEntityTag.RequestResult != COREQUEST_OK){
1595                                                                 iter->second = 2;
1596                                                                 break;
1597                                                         }
1598                                                         
1599                                                         ServerETag = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1601                                                         // Add to the ETag Database.
1602                                                 
1603                                                         ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1604                                                 
1605                                                 } else if (additer->second.DataType == 1){
1606                                         
1607                                                         // Write out the contact to the account directory.
1608                                         
1609                                                         vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1610                                                         vCardOutputData = additer->second.FileData;
1611                                                 
1612                                                         vCardFile.Open(vCardFilenameFinal, wxT("w"));
1613                                 
1614                                                         if (vCardFile.IsOpened()){
1615                                 
1616                                                                 vCardFile.Write(vCardOutputData, wxConvAuto());
1617                                                                 vCardFile.Close();
1618                                 
1619                                                         }
1620                                                 
1621                                                         // Update the ETagDB.
1623                                                         wxString ServerETag;
1624                                                         
1625                                                         COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(additer->first.ToStdString());
1626                                                         
1627                                                         if (ServerEntityTag.RequestResult != COREQUEST_OK){
1628                                                                 iter->second = 2;
1629                                                                 break;
1630                                                         }
1631                                                         
1632                                                         ServerETag = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1633                                                 
1634                                                         // Add to the ETag Database.
1635                                                 
1636                                                         ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1637                                                                                         
1638                                                 }
1639                                         }
1640                                         
1641                                         // Check if any errors occured before continuing.
1642                                         
1643                                         if (iter->second != 1){
1644                                                 break;
1645                                         }
1646         
1647                                         // Look for vCard files which aren't in the ETagDB.
1648         
1649                                         while(ContinueProcess){
1650                                 
1651                                                 // Check and make sure that the filename ends in .vcf/.vcard
1652                                                 // Skip any file that beings with a dot.
1653                                         
1654                                                 if (AccDirFilename.Left(1) == wxT(".")){
1655                                         
1656                                                         ContinueProcess = AccDir.GetNext(&AccDirFilename);
1657                                                         continue;
1658                                         
1659                                                 }
1660                                         
1661                                                 if (AccDirFilename.Right(4) != wxT(".vcf") && 
1662                                                 AccDirFilename.Right(6) != wxT(".vcard")){
1663                                         
1664                                                         ContinueProcess = AccDir.GetNext(&AccDirFilename);
1665                                                         continue;
1666                                         
1667                                                 }
1668                                 
1669                                                 // Look in the ETagDB for the file.
1670                                         
1671                                                 bool ETagExists = ETagDBPtr->CheckETagExists(AccDirFilename);
1672                                         
1673                                                 if (ETagExists == FALSE){
1674                                         
1675                                                         // Add the vCard to the list to be added.
1676                                                 
1677                                                         vCardAdd.Add(AccDirFilename, 1);
1678                                         
1679                                                 }
1680                                         
1681                                                 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1682                                 
1683                                         }
1684                                 
1685                                         // Process the vCards to add/update to the server.
1686                                 
1687                                         for (int vi = 0; vi < vCardAdd.GetCount(); vi++){
1689                                                 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1690                                                 ETag = ETagDBPtr->GetETag(vCardAdd[vi]);
1691                                                 ETagOriginal = ETagDBPtr->GetETagOriginal(vCardAdd[vi]);
1692                                                 
1693                                                 ContactFilename = vCardAdd[vi];
1694                                                 FullFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1696                                                 ContactFilename.Trim();
1697                         
1698                                                 if (ETag == ETagOriginal && ETagDBPtr->ETagExists(vCardAdd[vi])){
1699                         
1700                                                         // Compare the ETag with the Server ETag.
1701                                                         
1702                                                         COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1703                                                         
1704                                                         if (ServerEntityTag.RequestResult != COREQUEST_OK){
1705                                                                 iter->second = 2;
1706                                                                 break;
1707                                                         }
1708                                                         
1709                                                         // TODO: Continue from here!
1710                                                         
1711                                                         ETagServer = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1712                                 
1713                                                         if (ETagOriginal != ETagServer){
1714                                 
1715                                                                 // Server ETag is different from original ETag.
1716                                                                 // This is a conflict which the user will now
1717                                                                 // need to resolve.
1718                                         
1719                                                                 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1720                                         
1721                                                                 vCard34Conv vCard34Obj;
1722                                                 
1723                                                                 wxString ServerContactData;
1724                                                                 std::string ServerContactDataString;
1725                                                                 vCard ClientData;
1726                                                                 vCard ConvertedV4Data;
1727                                         
1728                                                                 // Load the client data from the filename given.
1729                                                                 
1730                                                                 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(vCardAdd[vi].ToStdString(), &ServerContactDataString);
1731                                                 
1732                                                                 if (GetContactResponse.RequestResult != COREQUEST_OK){
1733                                                                         iter->second = 2;
1734                                                                         break;
1735                                                                 }
1736                                                                 
1737                                                                 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1738                                                 
1739                                                                 // Process v3 version into v4 version.
1740                                                 
1741                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1742                                         
1743                                                                 vCardConflictObj vCardProcData;
1744                                         
1745                                                                 vCardProcData.vCardLocalData = &ClientData;
1746                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1747                                                                 ClientData.LoadFile(FullFilename);
1749                                                                 // Setup Conflict Resolution Dialog.
1750                                         
1751                                                                 // Fetch Data from Server and convert.
1752                                         
1753                                                                 bool *PauseMode = new bool;
1754                                                                 int ConflictResult;
1755                                                                 QRNotif qrn;
1757                                                                 *PauseMode = TRUE;                                              
1758                                                                 qrn.QResponse = &ConflictResult;
1759                                                                 qrn.PausePtr = PauseMode;
1760                                                 
1761                                                                 vCardProcData.QRNotifData = &qrn;
1762                                         
1763                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1764                                                                 event.SetClientData(&vCardProcData);
1765                                                                 wxPostEvent(frmMainPtrGet, event);
1766                                                 
1767                                                                 while (*PauseMode == TRUE){
1768                                                                         //nanosleep(&n1, &n2);
1769                                                                         SleepFor(250000000);
1770                                                                 }
1771                                         
1772                                                                 delete PauseMode;
1773                                                                 PauseMode = NULL;
1774                                         
1775                                                                 if (ConflictResult == 0){
1776                                         
1777                                                                         // Decide Later.
1778                                         
1779                                                                         iter->second = 4;
1780                                                                         continue;
1781                                         
1782                                                                 } else if (ConflictResult == 1){
1783                                         
1784                                                                         // Decided to use client version.
1785                                         
1786                                                                         COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(vCardAdd[vi].ToStdString(), ClientData.WriteString().ToStdString());
1787                                                 
1788                                                                         if (EditContactResponse.RequestResult != COREQUEST_OK){
1789                                                                                 iter->second = 2;
1790                                                                                 break;
1791                                                                         }
1792                                                                         
1793                                                                         iter->second = 4;
1794                                         
1795                                                                 } else if (ConflictResult == 2){
1796                                         
1797                                                                         // Decided to use server version.
1798                                                 
1799                                                                         // Download server version.
1800                                                                 
1801                                                                         ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1802                                                 
1803                                                                         iter->second = 4;
1804                                         
1805                                                                 }
1806                                 
1807                                                         } else {
1808                                 
1809                                                                 // Setup the filename for the contact being added.
1810                                                         
1811                                                                 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1812                                                                 wxString wxSCardData;
1813                                                                 wxString TaskData;
1814                                                         
1815                                                                 // Load the data from the file.
1816                                                         
1817                                                                 wxFFile wxFCard;
1818                                                                 wxFCard.Open(wxSFilename, wxT("r"));
1819                                 
1820                                                                 if (wxFCard.IsOpened()){
1821                                 
1822                                                                         wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1823                                                                         wxFCard.Close();
1824                                 
1825                                                                 }
1826                                                         
1827                                                                 // Convert the data to v3.
1828                                                         
1829                                                                 vCard34Conv vCard34Obj;
1830                                                         
1831                                                                 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1833                                                                 // Upload the data to the server.
1834                                                                 
1835                                                                 COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(vCardAdd[vi].ToStdString(), TaskData.ToStdString());
1836                                                         
1837                                                                 if (AddContactResponse.RequestResult != COREQUEST_OK){
1838                                                                         iter->second = 2;
1839                                                                         break;
1840                                                                 }
1841                                                                 
1842                                                                 // Get the Server ETag and put it in the ETag database.
1844                                                                 COServerResponse EntityTagResponse = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1845                                                                 
1846                                                                 if (EntityTagResponse.RequestResult != COREQUEST_OK){
1847                                                                         iter->second = 2;
1848                                                                         break;
1849                                                                 }
1850                                                                 
1851                                                                 ETagServer = wxString::FromUTF8(EntityTagResponse.EntityTag.c_str());
1852                                 
1853                                                                 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
1854                                 
1855                                                         }
1856                         
1857                                                 } else {
1858                         
1859                                                         if (ETagOriginal != ETagServer){
1860                                 
1861                                                                 // Server ETag is different from original ETag.
1862                                                                 // This is a conflict which the user will now
1863                                                                 // need to resolve.
1864                                         
1865                                                                 frmMain *frmMainPtrGet = (frmMain *)frmMainPtr;
1866                                         
1867                                                                 vCard34Conv vCard34Obj;
1868                                                 
1869                                                                 std::string ServerContactDataString;
1870                                                                 wxString ServerContactData;
1871                                                                 vCard ClientData;
1872                                                                 vCard ConvertedV4Data;
1873                                         
1874                                                                 // Load the client data from the filename given.
1875                                         
1876                                                                 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(vCardAdd[vi].ToStdString(), &ServerContactDataString);
1877                                                 
1878                                                                 if (GetContactResponse.RequestResult != COREQUEST_OK){
1879                                                                         iter->second = 2;
1880                                                                         break;
1881                                                                 }
1882                                                                 
1883                                                                 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1884                                                                 
1885                                                                 ClientData.LoadFile(FullFilename);
1886                                                 
1887                                                                 // Process v3 version into v4 version.
1888                                                 
1889                                                                 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1890                                         
1891                                                                 vCardConflictObj vCardProcData;
1892                                         
1893                                                                 vCardProcData.vCardLocalData = &ClientData;
1894                                                                 vCardProcData.vCardServerData = &ConvertedV4Data;
1895                                         
1896                                                                 bool *PauseMode = new bool;
1897                                                                 int ConflictResult;
1898                                                                 QRNotif qrn;
1900                                                                 *PauseMode = TRUE;                                              
1901                                                                 qrn.QResponse = &ConflictResult;
1902                                                                 qrn.PausePtr = PauseMode;
1903                                                 
1904                                                                 vCardProcData.QRNotifData = &qrn;
1905                                         
1906                                                                 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1907                                                                 event.SetClientData(&vCardProcData);
1908                                                                 wxPostEvent(frmMainPtrGet, event);
1910                                                                 while (*PauseMode == TRUE){
1911                                                                         SleepFor(250000000);
1912                                                                 }
1913                                         
1914                                                                 delete PauseMode;
1915                                                                 PauseMode = NULL;
1916                                         
1917                                                                 if (ConflictResult == 0){
1918                                         
1919                                                                         // Decide Later.
1920                                         
1921                                                                         iter->second = 4;
1922                                                                         continue;
1923                                         
1924                                                                 } else if (ConflictResult == 1){
1925                                         
1926                                                                         // Decided to use client version.
1927                                                                 
1928                                                                         COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(vCardAdd[vi].ToStdString(), ClientData.WriteString().ToStdString());
1930                                                                         if (EditContactResponse.RequestResult != COREQUEST_OK){
1931                                                                                 iter->second = 2;
1932                                                                                 break;
1933                                                                         }
1934                                                                         
1935                                                                         iter->second = 4;
1936                                         
1937                                                                 } else if (ConflictResult == 2){
1938                                         
1939                                                                         // Decided to use server version.
1940                                                 
1941                                                                         // Write server version to file.
1942                                                                 
1943                                                                         ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1944                                                 
1945                                                                         iter->second = 4;
1946                                         
1947                                                                 }
1948                                 
1949                                                         } else {
1950                                 
1951                                                                 // Setup the filename for the contact being added.
1952                                                         
1953                                                                 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1954                                                                 wxString wxSCardData;
1955                                                                 wxString TaskData;
1956                                                         
1957                                                                 // Load the data from the file.
1958                                                         
1959                                                                 wxFFile wxFCard;
1960                                                                 wxFCard.Open(wxSFilename, wxT("r"));
1961                                 
1962                                                                 if (wxFCard.IsOpened()){
1963                                 
1964                                                                         wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1965                                                                         wxFCard.Close();
1966                                 
1967                                                                 }
1968                                                         
1969                                                                 // Convert the data to v3.
1970                                                         
1971                                                                 vCard34Conv vCard34Obj;
1972                                                         
1973                                                                 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1975                                                                 // Upload the data to the server.
1976                                                                 
1977                                                                 COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(vCardAdd[vi].ToStdString(), TaskData.ToStdString());
1979                                                                 if (AddContactResponse.RequestResult != COREQUEST_OK){
1980                                                                         iter->second = 2;
1981                                                                         break;
1982                                                                 }
1983                                                                 
1984                                                                 // Get the Server ETag and put it in the ETag database.
1985                                                         
1986                                                                 COServerResponse EntityTagResponse = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1987                                 
1988                                                                 if (EntityTagResponse.RequestResult != COREQUEST_OK){
1989                                                                         iter->second = 2;
1990                                                                         break;
1991                                                                 }
1992                                                                 
1993                                                                 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
1994                                 
1995                                                         }
1996                                         
1997                                                 }
1999                                         }                       
2000                                 
2001                                         if (iter->second != 1){
2002                                                 break;
2003                                         }
2004                                         
2005                                         // Process the vCards to delete locally.
2006                                 
2007                                         for (int vi = 0; vi < vCardDelete.GetCount(); vi++){
2009                                                 if (!wxRemoveFile(CreateFilenamePath(AccountDirFinal, vCardDelete[vi]))){
2011                                                 } else {
2012                                         
2013                                                         ETagDBPtr->RemoveETag(vCardDelete[vi]);
2014                                         
2015                                                 }
2017                                         }
2018                                 
2019                                         // Process the vCards to delete from the server.
2020                                 
2021                                         std::map<wxString,wxString> *ETagFilenameMap = ETagDBPtr->GetFilenamePointer();
2022                                         std::list<wxString> DeleteList;
2023                                 
2024                                         for (std::map<wxString,wxString>::iterator deliter = ETagFilenameMap->begin();
2025                                                 deliter != ETagFilenameMap->end(); deliter++){
2026                                 
2027                                                 // Look through the ETagDB and find contacts which are marked as DELETED.
2028                                         
2029                                                 if (deliter->second == wxT("DELETED")){
2031                                                         ConnObjectIter->second->DeleteContact(deliter->first.ToStdString());
2032                                         
2033                                                         // Delete the ETag as the file has now been deleted.
2034                                         
2035                                                         DeleteList.push_back(deliter->first);
2036                                         
2037                                                 }
2038                                 
2039                                         }
2040                                         
2041                                         for (std::list<wxString>::iterator removeetagiter = DeleteList.begin();
2042                                                 removeetagiter != DeleteList.end(); removeetagiter++){
2043                                         
2044                                                 ETagDBPtr->RemoveETag(*removeetagiter);
2045                                                 
2046                                         }
2047                                 
2048                                         // Write out the update server sync token.
2050                                         wxString ServerSyncToken = wxString::FromUTF8(ServerContactList2.SyncToken.c_str());
2051                                 
2052                                         ServerSyncToken.Trim();
2053                                 
2054                                         if (!ServerSyncToken.IsEmpty()){
2055                                 
2056                                                 wxFFile SyncWrite;
2057                                                 SyncWrite.Open(AccountSyncTokenFilename, wxT("w"));
2058                                 
2059                                                 if (SyncWrite.IsOpened()){
2060                                 
2061                                                         SyncWrite.Write(ServerSyncToken, wxConvAuto());
2062                                                         SyncWrite.Close();
2063                                 
2064                                                 }
2065                                 
2066                                         }
2067                                 
2068                                         // Check for another sync token. If there is another 
2069                                         // sync token then repeat this. If token matches then stop processing.
2070                                 
2071                                         if (SyncTokenLoad == ServerSyncToken){
2072                                         
2073                                                 KeepUpdating = FALSE;
2074                                         
2075                                         }
2076                                         
2077                                 }
2078                                 
2079                                 // Mark as completed.
2080                         
2081                                 wxString AccountNameFinal;
2082                                 AccountNameFinal = PrefData.accounts.GetAccountDirectory(AccountID);
2083                                 AccountNameFinal.Append(wxT(".carddav"));
2084                                 
2085                                 wxCommandEvent reloadevent(RELOADCONTACTLIST);
2086                                 reloadevent.SetString(AccountNameFinal);
2087                                 wxPostEvent(this->GetParent(), reloadevent);
2088                                 
2089                                 iter->second = 4;
2090                                 continue;
2091                         
2092                         } else {
2093                         
2094                                 // Update the data to the server.
2096                                 iter->second = 4;
2097                                 continue;
2098                         
2099                         }
2101                         while (iter->second == 1){
2103                                 SleepFor(125000000);
2104         
2105                         }
2106                         
2107                         break;
2108                         
2109                 }
2111         }
2112         
2113         if (TasksFoundProc == FALSE){
2114         
2115                 TasksActive = FALSE;
2116                 ProcessTasksData = FALSE;
2117         
2118         } else {
2120                 wxCommandEvent eventp(ACTMGR_STARTTIMER);
2121                 wxPostEvent(this, eventp);
2122         
2123         }
2124         
2125         wxCommandEvent event(ACTMGR_STARTPROCESSTIMER);
2126         wxPostEvent(this, event);
2130 void frmActivityMgr::ProcessTasks()
2132         
2133         std::thread ProcessThread(&frmActivityMgr::ProcessTasksThread, this);
2134         ProcessThread.detach();
2138 void frmActivityMgr::SetupPointers(wxETagProcessTimer *ETagTmrPtrInc, void *frmMainPtrInc)
2141         ETagTmrPtr = ETagTmrPtrInc;
2142         
2143         frmMainPtr = frmMainPtrInc;
2147 void frmActivityMgr::ResumeProcessing(wxCommandEvent &event){
2149         QRNotif *qrn = (QRNotif *)event.GetClientData(); 
2150         
2151         int *QRes = qrn->QResponse;
2153         *qrn->PausePtr = FALSE;
2155         *QRes = event.GetInt();
2156         
2159 void frmActivityMgr::ToggleConnectionStatus( wxCommandEvent &event ){
2161         wxCommandEvent connevent(CONNSTAT_UPDATE);
2162         
2163         if (ApplicationSleepMode == FALSE){
2164         
2165                 connevent.SetInt(1);
2166                 ApplicationSleepMode = TRUE;
2167                 mnuSleepMode->Check(true);
2168         
2169         } else {
2170         
2171                 connevent.SetInt(0);
2172                 ApplicationSleepMode = FALSE;
2173                 mnuSleepMode->Check(false);
2174                 
2175                 wxCommandEvent event(ACTMGR_STARTTIMER);
2176                 wxPostEvent(this, event);
2178                 wxCommandEvent eventp(ACTMGR_STARTPROCESSTIMER);
2179                 wxPostEvent(this, eventp);
2180         
2181         }
2183         wxPostEvent(GetParent(), connevent);
2187 void frmActivityMgr::SleepMode( wxCommandEvent &event ){
2188         
2189         wxCommandEvent connevent(ACTMGR_TOGGLECONN);
2190         
2191         wxPostEvent(this, connevent);
2192         
2195 void frmActivityMgr::WriteServerCertificate(wxString AccountName, SSLCertCollection SSLCertInc){
2197         wxString ServerCertFinal;
2199         // Get the Cert section of the certificate and write it to the file.
2201         ServerCertFinal = GetAccountDir(AccountName, TRUE);
2203         wxFile CertFile;
2204         
2205         std::map<int, SSLCertData>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
2206         std::multimap<wxString,wxString>::iterator SSLDataIter = SSLCDIter->second.CertData.find(wxT("Cert"));
2207         
2208         CertFile.Open(ServerCertFinal, wxFile::write);  
2209         
2210         CertFile.Write(SSLDataIter->second, wxConvUTF8);
2211         CertFile.Close();
2215 void frmActivityMgr::WriteServerCertificate(wxString AccountName, SSLCertCollectionString SSLCertInc){
2217         wxString ServerCertFinal;
2219         // Get the Cert section of the certificate and write it to the file.
2221         ServerCertFinal = GetAccountDir(AccountName, TRUE);
2223         wxFile CertFile;
2224         
2225         std::map<int, SSLCertDataString>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
2226         std::multimap<string,string>::iterator SSLDataIter = SSLCDIter->second.CertData.find("Cert");
2227         
2228         CertFile.Open(ServerCertFinal, wxFile::write);  
2229         
2230         CertFile.Write(SSLDataIter->second, wxConvUTF8);
2231         CertFile.Close();
2235 void frmActivityMgr::StartTimer(wxCommandEvent& event){
2237         ActListProcTimer.Start(1000, FALSE);
2241 void frmActivityMgr::StopTimer(wxCommandEvent& event){
2243         ActListProcTimer.Stop();
2247 void frmActivityMgr::StartProcessTimer(wxCommandEvent& event){
2249         ActProcessTimer.Start(1000, FALSE);
2253 void frmActivityMgr::StopProcessTimer(wxCommandEvent& event){
2255         ActProcessTimer.Stop();
2259 void frmActivityMgr::StartAnimationTimer(wxCommandEvent& event){
2260         
2261         AnimationTimer.StartAnimation();
2262         
2265 void frmActivityMgr::UpdateStatusLabel(wxCommandEvent& event){
2267         ActivityMgrLabelUpdate *actmgrudata = (ActivityMgrLabelUpdate*)event.GetClientData();
2268         
2269         if (actmgrudata != NULL){
2270         
2271                 lstActivity->SetItem(actmgrudata->ItemIndex, 2, actmgrudata->ItemLabel);
2272         
2273                 delete actmgrudata;
2274                 actmgrudata = NULL;
2275         
2276         }
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy