1 // frmActivityMgr.cpp - Activity Manager form.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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>
23 #include <wx/tokenzr.h>
25 #include <wx/filefn.h>
28 #include <curl/curl.h>
30 #include "../bitmaps.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)
61 frmActivityMgr::frmActivityMgr( wxWindow* parent )
63 frmActivityMgrADT( parent )
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);
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."));
81 ActListProcTimer.SetupPointers(&ActivityList, &ActivityListIndex, &ActivityListTaskDetail,
82 &TasksActive, lstActivity, lblDetails, GauProgress, this, &AnimationTimer,
83 &ActProcessTimer, &ActivityListFinDate, &ActivityListConnObject);
85 AnimationTimer.SetBitmap(bmpActivity);
86 AnimationTimer.SetSleepBitmap(&SleepBitmap);
87 AnimationTimer.SetupPointer(this->GetParent());
89 wxListItem ColumnData;
91 ColumnData.SetText(_("Activity"));
92 ColumnData.SetWidth(175);
93 lstActivity->InsertColumn(0, ColumnData);
95 wxListItem ColumnData2;
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);
107 //AnimationTimer.StartAnimation();
109 // Create recovery database if it doesn't exist.
111 wxString RecoveryFilename = GetRecoveryDB();
115 if (wxFile::Exists(RecoveryFilename) == FALSE){
117 // Create the recovery database.
119 RecoveryDB.Open(RecoveryFilename.c_str(), wxFile::write);
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);
131 #if wxABI_VERSION < 20900
132 RecoveryDBF.Open(RecoveryFilename.c_str(), wxT("r"));
134 RecoveryDBF.Open(RecoveryFilename, wxT("r"));
137 // Load the recovery database for tasks not done.
139 if (RecoveryDBF.IsOpened() == TRUE){
141 wxString wxSRecoveryDB;
142 wxString RecoveryLine;
144 // Check if we are using wxWidgets version 2.8 or less and
145 // execute the required command accordingly.
147 RecoveryDBF.ReadAll(&wxSRecoveryDB, wxConvAuto());
151 std::map<int, wxString> RecoveryFileLines;
152 std::map<int, wxString>::iterator striter;
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));
170 //TestTimer.Start(50, FALSE);
173 void frmActivityMgr::OpenWindow()
178 void frmActivityMgr::CloseWindow( wxCommandEvent& event )
184 void frmActivityMgr::CloseWindow( wxCloseEvent& event )
190 void frmActivityMgr::ProcessTasksThread()
193 if (ProcessTasksData == FALSE){
199 if (ApplicationSleepMode == TRUE){
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;
222 for (std::map<int,int>::iterator iter = ActivityList.begin();
223 iter != ActivityList.end(); ++iter){
225 // Check for any queued tasks and start one.
227 if (iter->second > 4){
231 wxCommandEvent StartAnimationEvent(ACTMGR_STARTANIMATIONTIMER);
233 wxPostEvent(this, StartAnimationEvent);
235 // Start the animation timer if it hasn't started.
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);
246 // Setup the CardDAV object.
253 // 5 = Failed (UI Updated).
254 // 6 = Stopped (UI Updated).
255 // 7 = (Reserved) (UI Updated).
256 // 8 = Completed. (UI Updated).
258 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
259 XABPreferences PrefData(GetUserPrefDir());
261 if (iter->second == 0){
263 // Wait for process to finish.
266 TasksFoundProc = TRUE;
268 wxString ETagOriginal;
271 wxString ContactFilename;
272 wxString FullFilename;
274 bool EditMode = FALSE;
278 // Compare the ETags and work out if a conflict has occured.
280 if (TypeIter->second == 0){
284 StringDataIter = ActivityListData.find(iter->first);
285 StringURLIter = ActivityListURL.find(iter->first);
287 AccountDir = StringAccountIter->second;
288 ContactFilename = StringFilenameIter->second;
289 wxString AccountNameFriendly;
293 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
295 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
297 AccountDirCheck.Trim();
298 AccountDirCheck.Append(wxT(".carddav"));
300 if (AccountDirCheck == StringAccountIter->second){
302 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
309 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
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;
318 bool KeepUpdating = true;
320 while(KeepUpdating == true){
322 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
324 bool ExitLoop = false;
326 while (ExitLoop == false){
328 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
330 frmMainPtrGet->PauseAllTimers();
332 #if defined(__APPLE__)
334 ConnObjectIter->second->BypassSSLVerification(true);
336 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
338 ConnObjectIter->second->BypassSSLVerification(false);
340 int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
342 if (SSLResult != NSOKButton){
344 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
350 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
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"));
369 if (ModifiedCertificateData == false) {
371 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
377 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
383 bool UsingSSLBypass = false;
386 // Connect again and fetch SSL certificate information.
388 ConnObjectIter->second->BypassSSLVerification(true);
390 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
392 ConnObjectIter->second->BypassSSLVerification(false);
394 SSLInvalidCertNotifObjString SSLICNProcData;
396 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
398 bool *PauseMode = new bool;
402 qrn.QResponse = &SSLResult;
403 qrn.PausePtr = PauseMode;
405 SSLICNProcData.CertCollection = certcol;
406 SSLICNProcData.QRNotifData = &qrn;
407 SSLICNProcData.AccountName = AccountNameFriendly;
409 wxCommandEvent event(INVALIDSSLCERTSTRING);
410 event.SetClientData(&SSLICNProcData);
411 wxPostEvent(frmMainPtrGet, event);
413 while (*PauseMode == TRUE){
414 //nanosleep(&n1, &n2);
418 // Process the response from the user.
422 // Accept the Certificate.
424 UsingSSLBypass = true;
425 ConnObjectIter->second->BypassSSLVerification(true);
427 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
428 WriteServerCertificate(AccountDir, certcol);
430 ConnObjectIter->second->BypassSSLVerification(false);
432 } else if (SSLResult == 2){
434 // Reject the certificate, abort the task and mark as failed.
443 frmMainPtrGet->ResumeAllTimers();
446 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
448 ConnectResponse = ConnObjectIter->second->Connect(true);
450 if (ConnectResponse == COCONNECT_OK){
463 } else if (ConnectResponse == COCONNECT_OK){
465 ConnectResponse = ConnObjectIter->second->Connect(true);
480 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
484 #if defined(__APPLE__)
486 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
488 #elif defined(__WIN32__)
490 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
494 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
498 COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(StringURLIter->second.ToStdString(),
499 StringDataIter->second.ToStdString());
501 if (AddContactResponse.RequestResult != COREQUEST_OK){
506 // Get the entity tag for the new contact.
508 COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
510 if (ETagGetResponse.RequestResult != COREQUEST_OK){
515 ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag);
522 } else if (TypeIter->second == 1){
526 StringDataIter = ActivityListData.find(iter->first);
527 StringURLIter = ActivityListURL.find(iter->first);
529 AccountDir = StringAccountIter->second;
530 ContactFilename = StringFilenameIter->second;
531 wxString AccountNameFriendly;
535 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
537 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
539 AccountDirCheck.Trim();
540 AccountDirCheck.Append(wxT(".carddav"));
542 if (AccountDirCheck == StringAccountIter->second){
544 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
551 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
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;
560 bool KeepUpdating = true;
562 while(KeepUpdating == true){
564 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
566 bool ExitLoop = false;
568 while(ExitLoop == false){
570 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
572 frmMainPtrGet->PauseAllTimers();
574 #if defined(__APPLE__)
576 ConnObjectIter->second->BypassSSLVerification(true);
578 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
580 ConnObjectIter->second->BypassSSLVerification(false);
582 int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
584 if (SSLResult != NSOKButton){
586 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
592 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
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"));
611 if (ModifiedCertificateData == FALSE) {
613 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
619 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
625 bool UsingSSLBypass = false;
628 // Connect again and fetch SSL certificate information.
630 ConnObjectIter->second->BypassSSLVerification(true);
632 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
634 ConnObjectIter->second->BypassSSLVerification(false);
636 SSLInvalidCertNotifObjString SSLICNProcData;
638 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
640 bool *PauseMode = new bool;
644 qrn.QResponse = &SSLResult;
645 qrn.PausePtr = PauseMode;
647 SSLICNProcData.CertCollection = certcol;
648 SSLICNProcData.QRNotifData = &qrn;
649 SSLICNProcData.AccountName = AccountNameFriendly;
651 wxCommandEvent event(INVALIDSSLCERTSTRING);
652 event.SetClientData(&SSLICNProcData);
653 wxPostEvent(frmMainPtrGet, event);
655 while (*PauseMode == TRUE){
659 // Process the response from the user.
663 // Accept the Certificate.
665 UsingSSLBypass = true;
666 ConnObjectIter->second->BypassSSLVerification(true);
668 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
669 WriteServerCertificate(AccountDir, certcol);
671 ConnObjectIter->second->BypassSSLVerification(false);
673 } else if (SSLResult == 2){
675 // Reject the certificate, abort the task and mark as failed.
684 frmMainPtrGet->ResumeAllTimers();
687 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
689 ConnectResponse = ConnObjectIter->second->Connect(true);
691 if (ConnectResponse == COCONNECT_OK){
704 } else if (ConnectResponse == COCONNECT_OK){
706 ConnectResponse = ConnObjectIter->second->Connect(true);
721 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
725 COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
727 if (ETagGetResponse.RequestResult != COREQUEST_OK){
732 ETagServer = ETagGetResponse.EntityTag;
734 // Compare the ETag with the Server ETag.
736 if (ETagOriginal != ETagServer){
738 // Server ETag is different from original ETag.
739 // This is a conflict which the user will now
742 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
744 vCard34Conv vCard34Obj;
746 std::string ServerContactData;
747 wxString wxStringServerContactData;
749 vCard ConvertedV4Data;
751 // Load the client data from the filename given.
753 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(StringURLIter->second.ToStdString(),
756 if (GetContactResponse.RequestResult != COREQUEST_OK){
761 wxStringServerContactData = wxString::FromUTF8((const char*)ServerContactData.c_str());
763 // Process v3 version into v4 version.
765 vCard34Obj.ConvertToV4(&wxStringServerContactData, &ConvertedV4Data);
767 vCardConflictObj vCardProcData;
769 vCardProcData.vCardLocalData = &ClientData;
770 vCardProcData.vCardServerData = &ConvertedV4Data;
771 ClientData.LoadFile(FullFilename);
773 // Setup Conflict Resolution Dialog.
775 // Fetch Data from Server and convert.
777 bool *PauseMode = new bool;
782 qrn.QResponse = &ConflictResult;
783 qrn.PausePtr = PauseMode;
785 vCardProcData.QRNotifData = &qrn;
787 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
788 event.SetClientData(&vCardProcData);
789 wxPostEvent(frmMainPtrGet, event);
791 while (*PauseMode == TRUE){
798 if (ConflictResult == 0){
805 } else if (ConflictResult == 1){
807 // Decided to use client version.
809 } else if (ConflictResult == 2){
811 // Decided to use server version.
813 // Download server version.
815 ConvertedV4Data.WriteFile(FullFilename);
821 // Upload the data to the server.
823 COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(StringURLIter->second.ToStdString(),
824 StringDataIter->second.ToStdString());
826 if (EditContactResponse.RequestResult != COREQUEST_OK){
833 ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
835 if (ETagGetResponse.RequestResult != COREQUEST_OK){
840 ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag);
847 } else if (TypeIter->second == 2) {
849 // Delete the contact from the server and update the ETag DB.
851 AccountDir = StringAccountIter->second;
852 ContactFilename = StringFilenameIter->second;
853 wxString AccountNameFriendly;
857 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
859 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
861 AccountDirCheck.Trim();
862 AccountDirCheck.Append(wxT(".carddav"));
864 if (AccountDirCheck == StringAccountIter->second){
866 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
873 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
875 bool KeepUpdating = TRUE;
877 while(KeepUpdating == TRUE){
879 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
881 bool ExitLoop = FALSE;
883 while (ExitLoop == FALSE){
885 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
887 frmMainPtrGet->PauseAllTimers();
889 #if defined(__APPLE__)
891 ConnObjectIter->second->BypassSSLVerification(true);
893 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
895 ConnObjectIter->second->BypassSSLVerification(false);
897 int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
899 if (SSLResult != NSOKButton){
901 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
907 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
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"));
926 if (ModifiedCertificateData == false) {
928 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
934 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
940 bool UsingSSLBypass = false;
943 // Connect again and fetch SSL certificate information.
945 ConnObjectIter->second->BypassSSLVerification(true);
947 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
949 ConnObjectIter->second->BypassSSLVerification(false);
951 SSLInvalidCertNotifObjString SSLICNProcData;
953 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
955 bool *PauseMode = new bool;
959 qrn.QResponse = &SSLResult;
960 qrn.PausePtr = PauseMode;
962 SSLICNProcData.CertCollection = certcol;
963 SSLICNProcData.QRNotifData = &qrn;
964 SSLICNProcData.AccountName = AccountNameFriendly;
966 wxCommandEvent event(INVALIDSSLCERTSTRING);
967 event.SetClientData(&SSLICNProcData);
968 wxPostEvent(frmMainPtrGet, event);
970 while (*PauseMode == TRUE){
971 //nanosleep(&n1, &n2);
975 // Process the response from the user.
979 // Accept the Certificate.
981 UsingSSLBypass = true;
982 ConnObjectIter->second->BypassSSLVerification(true);
984 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
985 WriteServerCertificate(AccountDir, certcol);
987 ConnObjectIter->second->BypassSSLVerification(false);
989 } else if (SSLResult == 2){
991 // Reject the certificate, abort the task and mark as failed.
1000 frmMainPtrGet->ResumeAllTimers();
1003 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
1005 ConnectResponse = ConnObjectIter->second->Connect(true);
1007 if (ConnectResponse == COCONNECT_OK){
1020 } else if (ConnectResponse == COCONNECT_OK){
1022 ConnectResponse = ConnObjectIter->second->Connect(true);
1037 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1041 #if defined(__APPLE__)
1043 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1045 #elif defined(__WIN32__)
1047 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1051 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1055 COServerResponse DeleteContactResponse = ConnObjectIter->second->DeleteContact(ContactFilename.ToStdString());
1057 if (DeleteContactResponse.RequestResult != COREQUEST_OK){
1062 ETagDBPtr->RemoveETag(ContactFilename);
1069 } else if (TypeIter->second == 3) {
1071 wxString AccountNameFriendly;
1075 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
1077 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
1079 AccountDirCheck.Trim();
1080 AccountDirCheck.Append(wxT(".carddav"));
1082 if (AccountDirCheck == StringAccountIter->second){
1084 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
1091 bool KeepUpdating = TRUE;
1093 while (KeepUpdating == TRUE){
1095 StringTaskDetailIter = ActivityListTaskDetail.find(iter->first);
1097 AccountDir = StringAccountIter->second;
1099 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1101 wxArrayString vCardAdd;
1102 wxArrayString vCardDelete;
1103 wxArrayString vCardList;
1104 wxArrayString vCardUpdateLocal;
1105 wxArrayString vCardUpdateRemote;
1106 wxArrayString vCardUpdateLog;
1107 std::map<wxString,wxString> vCardETag;
1109 std::map<wxString,vCardDownloadDataFileClass> vCardDownloadData;
1111 // Get the list of vCard files locally.
1113 wxString AccountDirFinal;
1114 wxString AccountSyncTokenFilename;
1115 wxString SyncTokenLoad;
1117 #if defined(__HAIKU__)
1119 //preffilename = wxT("noo");
1121 #elif defined(__WIN32__)
1123 AccountDirFinal = GetAccountDir(AccountDir, FALSE);
1124 AccountSyncTokenFilename = AccountDirFinal;
1125 AccountSyncTokenFilename.Append(wxT("synctoken"));
1129 AccountDirFinal = GetAccountDir(AccountDir, FALSE);
1130 AccountSyncTokenFilename = AccountDirFinal;
1131 AccountSyncTokenFilename.Append(wxT("synctoken"));
1135 // Suppress Error Messages.
1137 wxLogNull PleaseDoNotDisturb;
1140 SyncLoad.Open(AccountSyncTokenFilename, wxT("r"));
1142 if (SyncLoad.IsOpened()){
1144 SyncLoad.ReadAll(&SyncTokenLoad, wxMBConvUTF8());
1149 // Get a list of vCard files from the server.
1151 // Get the list of vCard files from the ETagDB.
1153 std::map<wxString,ETagData> ETagDBData = ETagDBPtr->GetETagData();
1155 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
1157 bool ExitLoop = FALSE;
1159 while (ExitLoop == FALSE){
1161 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
1163 frmMainPtrGet->PauseAllTimers();
1165 #if defined(__APPLE__)
1167 ConnObjectIter->second->BypassSSLVerification(true);
1169 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1171 ConnObjectIter->second->BypassSSLVerification(false);
1173 int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
1175 if (SSLResult != NSOKButton){
1177 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
1183 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
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"));
1202 if (ModifiedCertificateData == false) {
1204 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
1210 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1216 bool UsingSSLBypass = false;
1219 // Connect again and fetch SSL certificate information.
1221 ConnObjectIter->second->BypassSSLVerification(true);
1223 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1225 ConnObjectIter->second->BypassSSLVerification(false);
1227 SSLInvalidCertNotifObjString SSLICNProcData;
1229 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
1231 bool *PauseMode = new bool;
1235 qrn.QResponse = &SSLResult;
1236 qrn.PausePtr = PauseMode;
1238 SSLICNProcData.CertCollection = certcol;
1239 SSLICNProcData.QRNotifData = &qrn;
1240 SSLICNProcData.AccountName = AccountNameFriendly;
1242 wxCommandEvent event(INVALIDSSLCERTSTRING);
1243 event.SetClientData(&SSLICNProcData);
1244 wxPostEvent(frmMainPtrGet, event);
1246 while (*PauseMode == TRUE){
1247 //nanosleep(&n1, &n2);
1248 SleepFor(250000000);
1251 // Process the response from the user.
1253 if (SSLResult == 1){
1255 // Accept the Certificate.
1257 UsingSSLBypass = true;
1258 ConnObjectIter->second->BypassSSLVerification(true);
1260 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
1261 WriteServerCertificate(AccountDir, certcol);
1263 ConnObjectIter->second->BypassSSLVerification(false);
1265 } else if (SSLResult == 2){
1267 // Reject the certificate, abort the task and mark as failed.
1276 frmMainPtrGet->ResumeAllTimers();
1279 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
1281 ConnectResponse = ConnObjectIter->second->Connect(true);
1283 if (ConnectResponse == COCONNECT_OK){
1296 } else if (ConnectResponse == COCONNECT_OK){
1298 ConnectResponse = ConnObjectIter->second->Connect(true);
1300 if (ConnectResponse == COCONNECT_OK){
1326 #if defined(__APPLE__)
1328 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1330 #elif defined(__WIN32__)
1332 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1336 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1340 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1344 // Look in directory and see which vCards to add and delete
1347 // vCard exists but not in ETagDB - Add (and upload).
1348 // vCard missing but in ETagDB - Delete (from server).
1350 // Start by looking for vCards that are not in the ETagDB.
1352 wxDir AccDir(AccountDirFinal);
1353 wxString AccDirFilename;
1355 bool ContinueProcess = AccDir.GetFirst(&AccDirFilename, wxEmptyString, wxDIR_DEFAULT);
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++){
1364 if (ContactInfo->Status == COCS_UPDATED){
1366 vCardDownloadDataFileClass vCardNewData;
1367 vCardNewData.DataType = 0;
1368 vCardDownloadData.insert(std::make_pair(ContactInfo->Location, vCardNewData));
1370 } else if (ContactInfo->Status == COCS_DELETED){
1372 vCardDelete.Add(ContactInfo->Location, 1);
1378 // Look for vCards that are missing but in the ETagDB.
1380 for (std::map<wxString,ETagData>::iterator etagiter = ETagDBData.begin();
1381 etagiter != ETagDBData.end(); etagiter++){
1383 // Check if the vCard exists.
1385 if (!wxFileExists(AccountDirFinal + etagiter->first)){
1387 // vCard doesn't exist so mark for deleting.
1389 vCardDelete.Add(etagiter->first, 1);
1395 // Compare Server with the ETag DB. Work out which
1396 // needs to updated remotely and which needs to be
1399 for (std::vector<COContactData>::iterator ContactInfo = ServerContactList2.ListData.begin();
1400 ContactInfo != ServerContactList2.ListData.end(); ContactInfo++){
1402 if (ETagDBData.find(ContactInfo->Location) == ETagDBData.end()){
1406 std::map<wxString,ETagData>::iterator lociter = ETagDBData.find(ContactInfo->Location);
1408 ETagData etd = lociter->second;
1410 wxString ETagSrv = ContactInfo->Data;
1412 // Check if the local ETag value has 'DELETED' set and skip
1413 // checking this file if it does.
1415 if (etd.ETagValue == "DELETED"){
1421 // Compare local ETag with original local ETag.
1422 // If different, update to server is required.
1424 if (etd.ETagValue != etd.ETagOrigValue){
1426 // Check if local original ETag is different from server ETag.
1427 // If it is then throw up a conflict resolution dialog.
1429 if (etd.ETagOrigValue != ETagSrv){
1431 vCard34Conv vCard34Obj;
1433 wxString FullFilename;
1435 vCard ConvertedV4Data;
1437 std::string ServerContactDataString;
1438 wxString ServerContactData;
1440 // Conflict. Bring up conflict resolution dialog.
1442 // Get the server contact data for comparison.
1444 ConnObjectIter->second->GetContact(ContactInfo->Location, &ServerContactDataString);
1445 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1447 FullFilename = CreateFilenamePath(AccountDirFinal, ContactInfo->Location);
1449 // Convert it to v4.
1451 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1453 vCardConflictObj vCardProcData;
1455 vCardProcData.vCardLocalData = &ClientData;
1456 vCardProcData.vCardServerData = &ConvertedV4Data;
1458 // Load the client data from the filename given.
1460 ClientData.LoadFile(FullFilename);
1464 bool *PauseMode = new bool;
1467 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1470 qrn.QResponse = &ConflictResult;
1471 qrn.PausePtr = PauseMode;
1473 vCardProcData.QRNotifData = &qrn;
1475 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1476 event.SetClientData(&vCardProcData);
1477 wxPostEvent(frmMainPtrGet, event);
1479 while (*PauseMode == TRUE){
1480 //nanosleep(&n1, &n2);
1481 SleepFor(250000000);
1487 // If replacing local version, store copy for now.
1488 // Otherwise upload to server.
1490 if (ConflictResult == 0){
1496 } else if (ConflictResult == 1){
1498 // Decided to use client version.
1500 vCardUpdateLocal.Remove(ContactInfo->Location);
1501 vCardUpdateRemote.Add(ContactInfo->Location, 1);
1503 } else if (ConflictResult == 2){
1505 // Decided to use server version.
1506 // Store the converted downloaded copy for
1507 // processing later.
1509 //vCardUpdateLocal.Add(srviter->first, 1);
1511 vCardDownloadDataFileClass vCardNewData;
1512 vCardNewData.DataType = 1;
1513 vCardNewData.FileData = ConvertedV4Data.WriteString();
1515 ConvertedV4Data.WriteFile(FullFilename);
1517 vCardDownloadData.insert(std::make_pair(ContactInfo->Location, vCardNewData));
1525 // Send vCard to the server.
1527 vCardUpdateRemote.Add(ContactInfo->Location, 1);
1533 // Check if local ETag is different from server ETag.
1534 // If different, obtain the vCard from the server.
1536 if (etd.ETagOrigValue != ETagSrv){
1538 vCardUpdateLocal.Add(ContactInfo->Location, 1);
1546 // Process the vCards that need to be downloaded and added/overwritten.
1548 wxString vCardFilenameFinal;
1549 wxString vCardOutputData;
1550 wxString vCardInputData;
1551 wxString ETagFileData;
1554 for (std::map<wxString,vCardDownloadDataFileClass>::iterator additer = vCardDownloadData.begin();
1555 additer != vCardDownloadData.end(); additer++){
1557 vCardFilenameFinal.Clear();
1558 vCardInputData.Clear();
1559 vCardOutputData.Clear();
1560 ETagFileData.Clear();
1562 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1564 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1566 if (additer->second.DataType == 0){
1568 vCard ConvertedV4Data;
1570 // Grab the file from the server and write the contact out.
1572 vCard34Conv vCard34Obj;
1574 wxString ServerContactData;
1575 std::string ServerContactDataString;
1577 // Conflict. Bring up conflict resolution dialog.
1579 // Get the server contact data for comparison.
1581 ConnObjectIter->second->GetContact(additer->first.ToStdString(), &ServerContactDataString);
1582 vCardInputData = wxString::FromUTF8(ServerContactDataString.c_str());
1584 vCard34Obj.ConvertToV4(&vCardInputData, &ConvertedV4Data);
1586 ConvertedV4Data.WriteFile(vCardFilenameFinal);
1588 // Get the server ETag.
1590 wxString ServerETag;
1592 COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(additer->first.ToStdString());
1594 if (ServerEntityTag.RequestResult != COREQUEST_OK){
1599 ServerETag = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1601 // Add to the ETag Database.
1603 ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1605 } else if (additer->second.DataType == 1){
1607 // Write out the contact to the account directory.
1609 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1610 vCardOutputData = additer->second.FileData;
1612 vCardFile.Open(vCardFilenameFinal, wxT("w"));
1614 if (vCardFile.IsOpened()){
1616 vCardFile.Write(vCardOutputData, wxConvAuto());
1621 // Update the ETagDB.
1623 wxString ServerETag;
1625 COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(additer->first.ToStdString());
1627 if (ServerEntityTag.RequestResult != COREQUEST_OK){
1632 ServerETag = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1634 // Add to the ETag Database.
1636 ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1641 // Check if any errors occured before continuing.
1643 if (iter->second != 1){
1647 // Look for vCard files which aren't in the ETagDB.
1649 while(ContinueProcess){
1651 // Check and make sure that the filename ends in .vcf/.vcard
1652 // Skip any file that beings with a dot.
1654 if (AccDirFilename.Left(1) == wxT(".")){
1656 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1661 if (AccDirFilename.Right(4) != wxT(".vcf") &&
1662 AccDirFilename.Right(6) != wxT(".vcard")){
1664 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1669 // Look in the ETagDB for the file.
1671 bool ETagExists = ETagDBPtr->CheckETagExists(AccDirFilename);
1673 if (ETagExists == FALSE){
1675 // Add the vCard to the list to be added.
1677 vCardAdd.Add(AccDirFilename, 1);
1681 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1685 // Process the vCards to add/update to the server.
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]);
1693 ContactFilename = vCardAdd[vi];
1694 FullFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1696 ContactFilename.Trim();
1698 if (ETag == ETagOriginal && ETagDBPtr->ETagExists(vCardAdd[vi])){
1700 // Compare the ETag with the Server ETag.
1702 COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1704 if (ServerEntityTag.RequestResult != COREQUEST_OK){
1709 // TODO: Continue from here!
1711 ETagServer = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1713 if (ETagOriginal != ETagServer){
1715 // Server ETag is different from original ETag.
1716 // This is a conflict which the user will now
1719 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1721 vCard34Conv vCard34Obj;
1723 wxString ServerContactData;
1724 std::string ServerContactDataString;
1726 vCard ConvertedV4Data;
1728 // Load the client data from the filename given.
1730 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(vCardAdd[vi].ToStdString(), &ServerContactDataString);
1732 if (GetContactResponse.RequestResult != COREQUEST_OK){
1737 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1739 // Process v3 version into v4 version.
1741 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1743 vCardConflictObj vCardProcData;
1745 vCardProcData.vCardLocalData = &ClientData;
1746 vCardProcData.vCardServerData = &ConvertedV4Data;
1747 ClientData.LoadFile(FullFilename);
1749 // Setup Conflict Resolution Dialog.
1751 // Fetch Data from Server and convert.
1753 bool *PauseMode = new bool;
1758 qrn.QResponse = &ConflictResult;
1759 qrn.PausePtr = PauseMode;
1761 vCardProcData.QRNotifData = &qrn;
1763 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1764 event.SetClientData(&vCardProcData);
1765 wxPostEvent(frmMainPtrGet, event);
1767 while (*PauseMode == TRUE){
1768 //nanosleep(&n1, &n2);
1769 SleepFor(250000000);
1775 if (ConflictResult == 0){
1782 } else if (ConflictResult == 1){
1784 // Decided to use client version.
1786 COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(vCardAdd[vi].ToStdString(), ClientData.WriteString().ToStdString());
1788 if (EditContactResponse.RequestResult != COREQUEST_OK){
1795 } else if (ConflictResult == 2){
1797 // Decided to use server version.
1799 // Download server version.
1801 ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1809 // Setup the filename for the contact being added.
1811 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1812 wxString wxSCardData;
1815 // Load the data from the file.
1818 wxFCard.Open(wxSFilename, wxT("r"));
1820 if (wxFCard.IsOpened()){
1822 wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1827 // Convert the data to v3.
1829 vCard34Conv vCard34Obj;
1831 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1833 // Upload the data to the server.
1835 COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(vCardAdd[vi].ToStdString(), TaskData.ToStdString());
1837 if (AddContactResponse.RequestResult != COREQUEST_OK){
1842 // Get the Server ETag and put it in the ETag database.
1844 COServerResponse EntityTagResponse = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1846 if (EntityTagResponse.RequestResult != COREQUEST_OK){
1851 ETagServer = wxString::FromUTF8(EntityTagResponse.EntityTag.c_str());
1853 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
1859 if (ETagOriginal != ETagServer){
1861 // Server ETag is different from original ETag.
1862 // This is a conflict which the user will now
1865 frmMain *frmMainPtrGet = (frmMain *)frmMainPtr;
1867 vCard34Conv vCard34Obj;
1869 std::string ServerContactDataString;
1870 wxString ServerContactData;
1872 vCard ConvertedV4Data;
1874 // Load the client data from the filename given.
1876 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(vCardAdd[vi].ToStdString(), &ServerContactDataString);
1878 if (GetContactResponse.RequestResult != COREQUEST_OK){
1883 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1885 ClientData.LoadFile(FullFilename);
1887 // Process v3 version into v4 version.
1889 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1891 vCardConflictObj vCardProcData;
1893 vCardProcData.vCardLocalData = &ClientData;
1894 vCardProcData.vCardServerData = &ConvertedV4Data;
1896 bool *PauseMode = new bool;
1901 qrn.QResponse = &ConflictResult;
1902 qrn.PausePtr = PauseMode;
1904 vCardProcData.QRNotifData = &qrn;
1906 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1907 event.SetClientData(&vCardProcData);
1908 wxPostEvent(frmMainPtrGet, event);
1910 while (*PauseMode == TRUE){
1911 SleepFor(250000000);
1917 if (ConflictResult == 0){
1924 } else if (ConflictResult == 1){
1926 // Decided to use client version.
1928 COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(vCardAdd[vi].ToStdString(), ClientData.WriteString().ToStdString());
1930 if (EditContactResponse.RequestResult != COREQUEST_OK){
1937 } else if (ConflictResult == 2){
1939 // Decided to use server version.
1941 // Write server version to file.
1943 ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1951 // Setup the filename for the contact being added.
1953 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1954 wxString wxSCardData;
1957 // Load the data from the file.
1960 wxFCard.Open(wxSFilename, wxT("r"));
1962 if (wxFCard.IsOpened()){
1964 wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1969 // Convert the data to v3.
1971 vCard34Conv vCard34Obj;
1973 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1975 // Upload the data to the server.
1977 COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(vCardAdd[vi].ToStdString(), TaskData.ToStdString());
1979 if (AddContactResponse.RequestResult != COREQUEST_OK){
1984 // Get the Server ETag and put it in the ETag database.
1986 COServerResponse EntityTagResponse = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1988 if (EntityTagResponse.RequestResult != COREQUEST_OK){
1993 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
2001 if (iter->second != 1){
2005 // Process the vCards to delete locally.
2007 for (int vi = 0; vi < vCardDelete.GetCount(); vi++){
2009 if (!wxRemoveFile(CreateFilenamePath(AccountDirFinal, vCardDelete[vi]))){
2013 ETagDBPtr->RemoveETag(vCardDelete[vi]);
2019 // Process the vCards to delete from the server.
2021 std::map<wxString,wxString> *ETagFilenameMap = ETagDBPtr->GetFilenamePointer();
2022 std::list<wxString> DeleteList;
2024 for (std::map<wxString,wxString>::iterator deliter = ETagFilenameMap->begin();
2025 deliter != ETagFilenameMap->end(); deliter++){
2027 // Look through the ETagDB and find contacts which are marked as DELETED.
2029 if (deliter->second == wxT("DELETED")){
2031 ConnObjectIter->second->DeleteContact(deliter->first.ToStdString());
2033 // Delete the ETag as the file has now been deleted.
2035 DeleteList.push_back(deliter->first);
2041 for (std::list<wxString>::iterator removeetagiter = DeleteList.begin();
2042 removeetagiter != DeleteList.end(); removeetagiter++){
2044 ETagDBPtr->RemoveETag(*removeetagiter);
2048 // Write out the update server sync token.
2050 wxString ServerSyncToken = wxString::FromUTF8(ServerContactList2.SyncToken.c_str());
2052 ServerSyncToken.Trim();
2054 if (!ServerSyncToken.IsEmpty()){
2057 SyncWrite.Open(AccountSyncTokenFilename, wxT("w"));
2059 if (SyncWrite.IsOpened()){
2061 SyncWrite.Write(ServerSyncToken, wxConvAuto());
2068 // Check for another sync token. If there is another
2069 // sync token then repeat this. If token matches then stop processing.
2071 if (SyncTokenLoad == ServerSyncToken){
2073 KeepUpdating = FALSE;
2079 // Mark as completed.
2081 wxString AccountNameFinal;
2082 AccountNameFinal = PrefData.accounts.GetAccountDirectory(AccountID);
2083 AccountNameFinal.Append(wxT(".carddav"));
2085 wxCommandEvent reloadevent(RELOADCONTACTLIST);
2086 reloadevent.SetString(AccountNameFinal);
2087 wxPostEvent(this->GetParent(), reloadevent);
2094 // Update the data to the server.
2101 while (iter->second == 1){
2103 SleepFor(125000000);
2113 if (TasksFoundProc == FALSE){
2115 TasksActive = FALSE;
2116 ProcessTasksData = FALSE;
2120 wxCommandEvent eventp(ACTMGR_STARTTIMER);
2121 wxPostEvent(this, eventp);
2125 wxCommandEvent event(ACTMGR_STARTPROCESSTIMER);
2126 wxPostEvent(this, event);
2130 void frmActivityMgr::ProcessTasks()
2133 std::thread ProcessThread(&frmActivityMgr::ProcessTasksThread, this);
2134 ProcessThread.detach();
2138 void frmActivityMgr::SetupPointers(wxETagProcessTimer *ETagTmrPtrInc, void *frmMainPtrInc)
2141 ETagTmrPtr = ETagTmrPtrInc;
2143 frmMainPtr = frmMainPtrInc;
2147 void frmActivityMgr::ResumeProcessing(wxCommandEvent &event){
2149 QRNotif *qrn = (QRNotif *)event.GetClientData();
2151 int *QRes = qrn->QResponse;
2153 *qrn->PausePtr = FALSE;
2155 *QRes = event.GetInt();
2159 void frmActivityMgr::ToggleConnectionStatus( wxCommandEvent &event ){
2161 wxCommandEvent connevent(CONNSTAT_UPDATE);
2163 if (ApplicationSleepMode == FALSE){
2165 connevent.SetInt(1);
2166 ApplicationSleepMode = TRUE;
2167 mnuSleepMode->Check(true);
2171 connevent.SetInt(0);
2172 ApplicationSleepMode = FALSE;
2173 mnuSleepMode->Check(false);
2175 wxCommandEvent event(ACTMGR_STARTTIMER);
2176 wxPostEvent(this, event);
2178 wxCommandEvent eventp(ACTMGR_STARTPROCESSTIMER);
2179 wxPostEvent(this, eventp);
2183 wxPostEvent(GetParent(), connevent);
2187 void frmActivityMgr::SleepMode( wxCommandEvent &event ){
2189 wxCommandEvent connevent(ACTMGR_TOGGLECONN);
2191 wxPostEvent(this, connevent);
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);
2205 std::map<int, SSLCertData>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
2206 std::multimap<wxString,wxString>::iterator SSLDataIter = SSLCDIter->second.CertData.find(wxT("Cert"));
2208 CertFile.Open(ServerCertFinal, wxFile::write);
2210 CertFile.Write(SSLDataIter->second, wxConvUTF8);
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);
2225 std::map<int, SSLCertDataString>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
2226 std::multimap<string,string>::iterator SSLDataIter = SSLCDIter->second.CertData.find("Cert");
2228 CertFile.Open(ServerCertFinal, wxFile::write);
2230 CertFile.Write(SSLDataIter->second, wxConvUTF8);
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){
2261 AnimationTimer.StartAnimation();
2265 void frmActivityMgr::UpdateStatusLabel(wxCommandEvent& event){
2267 ActivityMgrLabelUpdate *actmgrudata = (ActivityMgrLabelUpdate*)event.GetClientData();
2269 if (actmgrudata != NULL){
2271 lstActivity->SetItem(actmgrudata->ItemIndex, 2, actmgrudata->ItemLabel);