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 std::cout << ETagGetResponse.EntityTag << std::endl;
517 ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag);
524 } else if (TypeIter->second == 1){
528 StringDataIter = ActivityListData.find(iter->first);
529 StringURLIter = ActivityListURL.find(iter->first);
531 AccountDir = StringAccountIter->second;
532 ContactFilename = StringFilenameIter->second;
533 wxString AccountNameFriendly;
537 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
539 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
541 AccountDirCheck.Trim();
542 AccountDirCheck.Append(wxT(".carddav"));
544 if (AccountDirCheck == StringAccountIter->second){
546 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
553 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
555 ETag = StringETagIter->second;
556 ETagOriginal = StringETagOrigIter->second;
557 AccountDir = StringAccountIter->second;
558 ContactFilename = StringFilenameIter->second;
559 FullFilename = StringFullFilenameIter->second;
560 EditMode = ActivityListEditMode.find(iter->first)->second;
562 bool KeepUpdating = true;
564 while(KeepUpdating == true){
566 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
568 bool ExitLoop = false;
570 while(ExitLoop == false){
572 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
574 frmMainPtrGet->PauseAllTimers();
576 #if defined(__APPLE__)
578 ConnObjectIter->second->BypassSSLVerification(true);
580 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
582 ConnObjectIter->second->BypassSSLVerification(false);
584 int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
586 if (SSLResult != NSOKButton){
588 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
594 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
598 #elif defined(__WIN32__)
600 ConnObjectIter->second->BypassSSLVerification(true);
602 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
604 ConnObjectIter->second->BypassSSLVerification(false);
606 BOOL ModifiedCertificateData = false;
607 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(ConnObjectIter->second, (HWND)frmMainPtrGet->GetHandle());
609 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
610 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
613 if (ModifiedCertificateData == FALSE) {
615 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
621 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
627 bool UsingSSLBypass = false;
630 // Connect again and fetch SSL certificate information.
632 ConnObjectIter->second->BypassSSLVerification(true);
634 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
636 ConnObjectIter->second->BypassSSLVerification(false);
638 SSLInvalidCertNotifObjString SSLICNProcData;
640 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
642 bool *PauseMode = new bool;
646 qrn.QResponse = &SSLResult;
647 qrn.PausePtr = PauseMode;
649 SSLICNProcData.CertCollection = certcol;
650 SSLICNProcData.QRNotifData = &qrn;
651 SSLICNProcData.AccountName = AccountNameFriendly;
653 wxCommandEvent event(INVALIDSSLCERTSTRING);
654 event.SetClientData(&SSLICNProcData);
655 wxPostEvent(frmMainPtrGet, event);
657 while (*PauseMode == TRUE){
661 // Process the response from the user.
665 // Accept the Certificate.
667 UsingSSLBypass = true;
668 ConnObjectIter->second->BypassSSLVerification(true);
670 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
671 WriteServerCertificate(AccountDir, certcol);
673 ConnObjectIter->second->BypassSSLVerification(false);
675 } else if (SSLResult == 2){
677 // Reject the certificate, abort the task and mark as failed.
686 frmMainPtrGet->ResumeAllTimers();
689 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
691 ConnectResponse = ConnObjectIter->second->Connect(true);
693 if (ConnectResponse == COCONNECT_OK){
706 } else if (ConnectResponse == COCONNECT_OK){
708 ConnectResponse = ConnObjectIter->second->Connect(true);
723 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
727 COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
729 if (ETagGetResponse.RequestResult != COREQUEST_OK){
734 ETagServer = ETagGetResponse.EntityTag;
736 // Compare the ETag with the Server ETag.
738 if (ETagOriginal != ETagServer){
740 // Server ETag is different from original ETag.
741 // This is a conflict which the user will now
744 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
746 vCard34Conv vCard34Obj;
748 std::string ServerContactData;
749 wxString wxStringServerContactData;
751 vCard ConvertedV4Data;
753 // Load the client data from the filename given.
755 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(StringURLIter->second.ToStdString(),
758 if (GetContactResponse.RequestResult != COREQUEST_OK){
763 wxStringServerContactData = wxString::FromUTF8((const char*)ServerContactData.c_str());
765 // Process v3 version into v4 version.
767 vCard34Obj.ConvertToV4(&wxStringServerContactData, &ConvertedV4Data);
769 vCardConflictObj vCardProcData;
771 vCardProcData.vCardLocalData = &ClientData;
772 vCardProcData.vCardServerData = &ConvertedV4Data;
773 ClientData.LoadFile(FullFilename);
775 // Setup Conflict Resolution Dialog.
777 // Fetch Data from Server and convert.
779 bool *PauseMode = new bool;
784 qrn.QResponse = &ConflictResult;
785 qrn.PausePtr = PauseMode;
787 vCardProcData.QRNotifData = &qrn;
789 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
790 event.SetClientData(&vCardProcData);
791 wxPostEvent(frmMainPtrGet, event);
793 while (*PauseMode == TRUE){
800 if (ConflictResult == 0){
807 } else if (ConflictResult == 1){
809 // Decided to use client version.
811 } else if (ConflictResult == 2){
813 // Decided to use server version.
815 // Download server version.
817 ConvertedV4Data.WriteFile(FullFilename);
823 // Upload the data to the server.
825 COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(StringURLIter->second.ToStdString(),
826 StringDataIter->second.ToStdString());
828 if (EditContactResponse.RequestResult != COREQUEST_OK){
835 ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
837 if (ETagGetResponse.RequestResult != COREQUEST_OK){
842 ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag);
849 } else if (TypeIter->second == 2) {
851 // Delete the contact from the server and update the ETag DB.
853 AccountDir = StringAccountIter->second;
854 ContactFilename = StringFilenameIter->second;
855 wxString AccountNameFriendly;
859 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
861 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
863 AccountDirCheck.Trim();
864 AccountDirCheck.Append(wxT(".carddav"));
866 if (AccountDirCheck == StringAccountIter->second){
868 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
875 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
877 bool KeepUpdating = TRUE;
879 while(KeepUpdating == TRUE){
881 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
883 bool ExitLoop = FALSE;
885 while (ExitLoop == FALSE){
887 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
889 frmMainPtrGet->PauseAllTimers();
891 #if defined(__APPLE__)
893 ConnObjectIter->second->BypassSSLVerification(true);
895 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
897 ConnObjectIter->second->BypassSSLVerification(false);
899 int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
901 if (SSLResult != NSOKButton){
903 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
909 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
913 #elif defined(__WIN32__)
915 ConnObjectIter->second->BypassSSLVerification(true);
917 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
919 ConnObjectIter->second->BypassSSLVerification(false);
921 BOOL ModifiedCertificateData = false;
922 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(ConnObjectIter->second, (HWND)frmMainPtrGet->GetHandle());
924 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
925 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
928 if (ModifiedCertificateData == false) {
930 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
936 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
942 bool UsingSSLBypass = false;
945 // Connect again and fetch SSL certificate information.
947 ConnObjectIter->second->BypassSSLVerification(true);
949 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
951 ConnObjectIter->second->BypassSSLVerification(false);
953 SSLInvalidCertNotifObjString SSLICNProcData;
955 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
957 bool *PauseMode = new bool;
961 qrn.QResponse = &SSLResult;
962 qrn.PausePtr = PauseMode;
964 SSLICNProcData.CertCollection = certcol;
965 SSLICNProcData.QRNotifData = &qrn;
966 SSLICNProcData.AccountName = AccountNameFriendly;
968 wxCommandEvent event(INVALIDSSLCERTSTRING);
969 event.SetClientData(&SSLICNProcData);
970 wxPostEvent(frmMainPtrGet, event);
972 while (*PauseMode == TRUE){
973 //nanosleep(&n1, &n2);
977 // Process the response from the user.
981 // Accept the Certificate.
983 UsingSSLBypass = true;
984 ConnObjectIter->second->BypassSSLVerification(true);
986 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
987 WriteServerCertificate(AccountDir, certcol);
989 ConnObjectIter->second->BypassSSLVerification(false);
991 } else if (SSLResult == 2){
993 // Reject the certificate, abort the task and mark as failed.
1002 frmMainPtrGet->ResumeAllTimers();
1005 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
1007 ConnectResponse = ConnObjectIter->second->Connect(true);
1009 if (ConnectResponse == COCONNECT_OK){
1022 } else if (ConnectResponse == COCONNECT_OK){
1024 ConnectResponse = ConnObjectIter->second->Connect(true);
1039 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1043 #if defined(__APPLE__)
1045 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1047 #elif defined(__WIN32__)
1049 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1053 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1057 COServerResponse DeleteContactResponse = ConnObjectIter->second->DeleteContact(ContactFilename.ToStdString());
1059 if (DeleteContactResponse.RequestResult != COREQUEST_OK){
1064 ETagDBPtr->RemoveETag(ContactFilename);
1071 } else if (TypeIter->second == 3) {
1073 wxString AccountNameFriendly;
1077 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
1079 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
1081 AccountDirCheck.Trim();
1082 AccountDirCheck.Append(wxT(".carddav"));
1084 if (AccountDirCheck == StringAccountIter->second){
1086 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
1093 bool KeepUpdating = TRUE;
1095 while (KeepUpdating == TRUE){
1097 StringTaskDetailIter = ActivityListTaskDetail.find(iter->first);
1099 AccountDir = StringAccountIter->second;
1101 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1103 wxArrayString vCardAdd;
1104 wxArrayString vCardDelete;
1105 wxArrayString vCardList;
1106 wxArrayString vCardUpdateLocal;
1107 wxArrayString vCardUpdateRemote;
1108 wxArrayString vCardUpdateLog;
1109 std::map<wxString,wxString> vCardETag;
1111 std::map<wxString,vCardDownloadDataFileClass> vCardDownloadData;
1113 // Get the list of vCard files locally.
1115 wxString AccountDirFinal;
1116 wxString AccountSyncTokenFilename;
1117 wxString SyncTokenLoad;
1119 #if defined(__HAIKU__)
1121 //preffilename = wxT("noo");
1123 #elif defined(__WIN32__)
1125 AccountDirFinal = GetAccountDir(AccountDir, FALSE);
1126 AccountSyncTokenFilename = AccountDirFinal;
1127 AccountSyncTokenFilename.Append(wxT("synctoken"));
1131 AccountDirFinal = GetAccountDir(AccountDir, FALSE);
1132 AccountSyncTokenFilename = AccountDirFinal;
1133 AccountSyncTokenFilename.Append(wxT("synctoken"));
1137 // Suppress Error Messages.
1139 wxLogNull PleaseDoNotDisturb;
1142 SyncLoad.Open(AccountSyncTokenFilename, wxT("r"));
1144 if (SyncLoad.IsOpened()){
1146 SyncLoad.ReadAll(&SyncTokenLoad, wxMBConvUTF8());
1151 // Get a list of vCard files from the server.
1153 // Get the list of vCard files from the ETagDB.
1155 std::map<wxString,ETagData> ETagDBData = ETagDBPtr->GetETagData();
1157 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
1159 bool ExitLoop = FALSE;
1161 while (ExitLoop == FALSE){
1163 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
1165 frmMainPtrGet->PauseAllTimers();
1167 #if defined(__APPLE__)
1169 ConnObjectIter->second->BypassSSLVerification(true);
1171 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1173 ConnObjectIter->second->BypassSSLVerification(false);
1175 int SSLResult = DisplayTrustPanel(ConnObjectIter->second->BuildSSLCollection(), AccountNameFriendly);
1177 if (SSLResult != NSOKButton){
1179 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
1185 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1189 #elif defined(__WIN32__)
1191 ConnObjectIter->second->BypassSSLVerification(true);
1193 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1195 ConnObjectIter->second->BypassSSLVerification(false);
1197 BOOL ModifiedCertificateData = false;
1198 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(ConnObjectIter->second, (HWND)frmMainPtrGet->GetHandle());
1200 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
1201 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
1204 if (ModifiedCertificateData == false) {
1206 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnObjectIter->second->BuildSSLCollection());
1212 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1218 bool UsingSSLBypass = false;
1221 // Connect again and fetch SSL certificate information.
1223 ConnObjectIter->second->BypassSSLVerification(true);
1225 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1227 ConnObjectIter->second->BypassSSLVerification(false);
1229 SSLInvalidCertNotifObjString SSLICNProcData;
1231 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
1233 bool *PauseMode = new bool;
1237 qrn.QResponse = &SSLResult;
1238 qrn.PausePtr = PauseMode;
1240 SSLICNProcData.CertCollection = certcol;
1241 SSLICNProcData.QRNotifData = &qrn;
1242 SSLICNProcData.AccountName = AccountNameFriendly;
1244 wxCommandEvent event(INVALIDSSLCERTSTRING);
1245 event.SetClientData(&SSLICNProcData);
1246 wxPostEvent(frmMainPtrGet, event);
1248 while (*PauseMode == TRUE){
1249 //nanosleep(&n1, &n2);
1250 SleepFor(250000000);
1253 // Process the response from the user.
1255 if (SSLResult == 1){
1257 // Accept the Certificate.
1259 UsingSSLBypass = true;
1260 ConnObjectIter->second->BypassSSLVerification(true);
1262 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
1263 WriteServerCertificate(AccountDir, certcol);
1265 ConnObjectIter->second->BypassSSLVerification(false);
1267 } else if (SSLResult == 2){
1269 // Reject the certificate, abort the task and mark as failed.
1278 frmMainPtrGet->ResumeAllTimers();
1281 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
1283 ConnectResponse = ConnObjectIter->second->Connect(true);
1285 if (ConnectResponse == COCONNECT_OK){
1298 } else if (ConnectResponse == COCONNECT_OK){
1300 ConnectResponse = ConnObjectIter->second->Connect(true);
1302 if (ConnectResponse == COCONNECT_OK){
1328 #if defined(__APPLE__)
1330 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1332 #elif defined(__WIN32__)
1334 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1338 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnObjectIter->second->BuildSSLCollection());
1342 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1346 // Look in directory and see which vCards to add and delete
1349 // vCard exists but not in ETagDB - Add (and upload).
1350 // vCard missing but in ETagDB - Delete (from server).
1352 // Start by looking for vCards that are not in the ETagDB.
1354 wxDir AccDir(AccountDirFinal);
1355 wxString AccDirFilename;
1357 bool ContinueProcess = AccDir.GetFirst(&AccDirFilename, wxEmptyString, wxDIR_DEFAULT);
1359 COContactList ServerContactList2 = ConnObjectIter->second->GetContactList(SyncTokenLoad.ToStdString());
1361 // Process the server data.
1363 for (std::vector<COContactData>::iterator ContactInfo = ServerContactList2.ListData.begin();
1364 ContactInfo != ServerContactList2.ListData.end(); ContactInfo++){
1366 if (ContactInfo->Status == COCS_UPDATED){
1368 vCardDownloadDataFileClass vCardNewData;
1369 vCardNewData.DataType = 0;
1370 vCardDownloadData.insert(std::make_pair(ContactInfo->Location, vCardNewData));
1372 } else if (ContactInfo->Status == COCS_DELETED){
1374 vCardDelete.Add(ContactInfo->Location, 1);
1380 // Look for vCards that are missing but in the ETagDB.
1382 for (std::map<wxString,ETagData>::iterator etagiter = ETagDBData.begin();
1383 etagiter != ETagDBData.end(); etagiter++){
1385 // Check if the vCard exists.
1387 if (!wxFileExists(AccountDirFinal + etagiter->first)){
1389 // vCard doesn't exist so mark for deleting.
1391 vCardDelete.Add(etagiter->first, 1);
1397 // Compare Server with the ETag DB. Work out which
1398 // needs to updated remotely and which needs to be
1401 for (std::vector<COContactData>::iterator ContactInfo = ServerContactList2.ListData.begin();
1402 ContactInfo != ServerContactList2.ListData.end(); ContactInfo++){
1404 if (ETagDBData.find(ContactInfo->Location) == ETagDBData.end()){
1408 std::map<wxString,ETagData>::iterator lociter = ETagDBData.find(ContactInfo->Location);
1410 ETagData etd = lociter->second;
1412 wxString ETagSrv = ContactInfo->Data;
1414 // Check if the local ETag value has 'DELETED' set and skip
1415 // checking this file if it does.
1417 if (etd.ETagValue == "DELETED"){
1423 // Compare local ETag with original local ETag.
1424 // If different, update to server is required.
1426 if (etd.ETagValue != etd.ETagOrigValue){
1428 // Check if local original ETag is different from server ETag.
1429 // If it is then throw up a conflict resolution dialog.
1431 if (etd.ETagOrigValue != ETagSrv){
1433 vCard34Conv vCard34Obj;
1435 wxString FullFilename;
1437 vCard ConvertedV4Data;
1439 std::string ServerContactDataString;
1440 wxString ServerContactData;
1442 // Conflict. Bring up conflict resolution dialog.
1444 // Get the server contact data for comparison.
1446 ConnObjectIter->second->GetContact(ContactInfo->Location, &ServerContactDataString);
1447 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1449 FullFilename = CreateFilenamePath(AccountDirFinal, ContactInfo->Location);
1451 // Convert it to v4.
1453 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1455 vCardConflictObj vCardProcData;
1457 vCardProcData.vCardLocalData = &ClientData;
1458 vCardProcData.vCardServerData = &ConvertedV4Data;
1460 // Load the client data from the filename given.
1462 ClientData.LoadFile(FullFilename);
1466 bool *PauseMode = new bool;
1469 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1472 qrn.QResponse = &ConflictResult;
1473 qrn.PausePtr = PauseMode;
1475 vCardProcData.QRNotifData = &qrn;
1477 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1478 event.SetClientData(&vCardProcData);
1479 wxPostEvent(frmMainPtrGet, event);
1481 while (*PauseMode == TRUE){
1482 //nanosleep(&n1, &n2);
1483 SleepFor(250000000);
1489 // If replacing local version, store copy for now.
1490 // Otherwise upload to server.
1492 if (ConflictResult == 0){
1498 } else if (ConflictResult == 1){
1500 // Decided to use client version.
1502 vCardUpdateLocal.Remove(ContactInfo->Location);
1503 vCardUpdateRemote.Add(ContactInfo->Location, 1);
1505 } else if (ConflictResult == 2){
1507 // Decided to use server version.
1508 // Store the converted downloaded copy for
1509 // processing later.
1511 //vCardUpdateLocal.Add(srviter->first, 1);
1513 vCardDownloadDataFileClass vCardNewData;
1514 vCardNewData.DataType = 1;
1515 vCardNewData.FileData = ConvertedV4Data.WriteString();
1517 ConvertedV4Data.WriteFile(FullFilename);
1519 vCardDownloadData.insert(std::make_pair(ContactInfo->Location, vCardNewData));
1527 // Send vCard to the server.
1529 vCardUpdateRemote.Add(ContactInfo->Location, 1);
1535 // Check if local ETag is different from server ETag.
1536 // If different, obtain the vCard from the server.
1538 if (etd.ETagOrigValue != ETagSrv){
1540 vCardUpdateLocal.Add(ContactInfo->Location, 1);
1548 // Process the vCards that need to be downloaded and added/overwritten.
1550 wxString vCardFilenameFinal;
1551 wxString vCardOutputData;
1552 wxString vCardInputData;
1553 wxString ETagFileData;
1556 for (std::map<wxString,vCardDownloadDataFileClass>::iterator additer = vCardDownloadData.begin();
1557 additer != vCardDownloadData.end(); additer++){
1559 vCardFilenameFinal.Clear();
1560 vCardInputData.Clear();
1561 vCardOutputData.Clear();
1562 ETagFileData.Clear();
1564 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1566 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1568 if (additer->second.DataType == 0){
1570 vCard ConvertedV4Data;
1572 // Grab the file from the server and write the contact out.
1574 vCard34Conv vCard34Obj;
1576 wxString ServerContactData;
1577 std::string ServerContactDataString;
1579 // Conflict. Bring up conflict resolution dialog.
1581 // Get the server contact data for comparison.
1583 ConnObjectIter->second->GetContact(additer->first.ToStdString(), &ServerContactDataString);
1584 vCardInputData = wxString::FromUTF8(ServerContactDataString.c_str());
1586 vCard34Obj.ConvertToV4(&vCardInputData, &ConvertedV4Data);
1588 ConvertedV4Data.WriteFile(vCardFilenameFinal);
1590 // Get the server ETag.
1592 wxString ServerETag;
1594 COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(additer->first.ToStdString());
1596 if (ServerEntityTag.RequestResult != COREQUEST_OK){
1601 ServerETag = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1603 // Add to the ETag Database.
1605 ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1607 } else if (additer->second.DataType == 1){
1609 // Write out the contact to the account directory.
1611 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
1612 vCardOutputData = additer->second.FileData;
1614 vCardFile.Open(vCardFilenameFinal, wxT("w"));
1616 if (vCardFile.IsOpened()){
1618 vCardFile.Write(vCardOutputData, wxConvAuto());
1623 // Update the ETagDB.
1625 wxString ServerETag;
1627 COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(additer->first.ToStdString());
1629 if (ServerEntityTag.RequestResult != COREQUEST_OK){
1634 ServerETag = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1636 // Add to the ETag Database.
1638 ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
1643 // Check if any errors occured before continuing.
1645 if (iter->second != 1){
1649 // Look for vCard files which aren't in the ETagDB.
1651 while(ContinueProcess){
1653 // Check and make sure that the filename ends in .vcf/.vcard
1654 // Skip any file that beings with a dot.
1656 if (AccDirFilename.Left(1) == wxT(".")){
1658 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1663 if (AccDirFilename.Right(4) != wxT(".vcf") &&
1664 AccDirFilename.Right(6) != wxT(".vcard")){
1666 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1671 // Look in the ETagDB for the file.
1673 bool ETagExists = ETagDBPtr->CheckETagExists(AccDirFilename);
1675 if (ETagExists == FALSE){
1677 // Add the vCard to the list to be added.
1679 vCardAdd.Add(AccDirFilename, 1);
1683 ContinueProcess = AccDir.GetNext(&AccDirFilename);
1687 // Process the vCards to add/update to the server.
1689 for (int vi = 0; vi < vCardAdd.GetCount(); vi++){
1691 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1692 ETag = ETagDBPtr->GetETag(vCardAdd[vi]);
1693 ETagOriginal = ETagDBPtr->GetETagOriginal(vCardAdd[vi]);
1695 ContactFilename = vCardAdd[vi];
1696 FullFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1698 ContactFilename.Trim();
1700 if (ETag == ETagOriginal && ETagDBPtr->ETagExists(vCardAdd[vi])){
1702 // Compare the ETag with the Server ETag.
1704 COServerResponse ServerEntityTag = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1706 if (ServerEntityTag.RequestResult != COREQUEST_OK){
1711 // TODO: Continue from here!
1713 ETagServer = wxString::FromUTF8(ServerEntityTag.EntityTag.c_str());
1715 if (ETagOriginal != ETagServer){
1717 // Server ETag is different from original ETag.
1718 // This is a conflict which the user will now
1721 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1723 vCard34Conv vCard34Obj;
1725 wxString ServerContactData;
1726 std::string ServerContactDataString;
1728 vCard ConvertedV4Data;
1730 // Load the client data from the filename given.
1732 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(vCardAdd[vi].ToStdString(), &ServerContactDataString);
1734 if (GetContactResponse.RequestResult != COREQUEST_OK){
1739 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1741 // Process v3 version into v4 version.
1743 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1745 vCardConflictObj vCardProcData;
1747 vCardProcData.vCardLocalData = &ClientData;
1748 vCardProcData.vCardServerData = &ConvertedV4Data;
1749 ClientData.LoadFile(FullFilename);
1751 // Setup Conflict Resolution Dialog.
1753 // Fetch Data from Server and convert.
1755 bool *PauseMode = new bool;
1760 qrn.QResponse = &ConflictResult;
1761 qrn.PausePtr = PauseMode;
1763 vCardProcData.QRNotifData = &qrn;
1765 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1766 event.SetClientData(&vCardProcData);
1767 wxPostEvent(frmMainPtrGet, event);
1769 while (*PauseMode == TRUE){
1770 //nanosleep(&n1, &n2);
1771 SleepFor(250000000);
1777 if (ConflictResult == 0){
1784 } else if (ConflictResult == 1){
1786 // Decided to use client version.
1788 COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(vCardAdd[vi].ToStdString(), ClientData.WriteString().ToStdString());
1790 if (EditContactResponse.RequestResult != COREQUEST_OK){
1797 } else if (ConflictResult == 2){
1799 // Decided to use server version.
1801 // Download server version.
1803 ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1811 // Setup the filename for the contact being added.
1813 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1814 wxString wxSCardData;
1817 // Load the data from the file.
1820 wxFCard.Open(wxSFilename, wxT("r"));
1822 if (wxFCard.IsOpened()){
1824 wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1829 // Convert the data to v3.
1831 vCard34Conv vCard34Obj;
1833 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1835 // Upload the data to the server.
1837 COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(vCardAdd[vi].ToStdString(), TaskData.ToStdString());
1839 if (AddContactResponse.RequestResult != COREQUEST_OK){
1844 // Get the Server ETag and put it in the ETag database.
1846 COServerResponse EntityTagResponse = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1848 if (EntityTagResponse.RequestResult != COREQUEST_OK){
1853 ETagServer = wxString::FromUTF8(EntityTagResponse.EntityTag.c_str());
1855 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
1861 if (ETagOriginal != ETagServer){
1863 // Server ETag is different from original ETag.
1864 // This is a conflict which the user will now
1867 frmMain *frmMainPtrGet = (frmMain *)frmMainPtr;
1869 vCard34Conv vCard34Obj;
1871 std::string ServerContactDataString;
1872 wxString ServerContactData;
1874 vCard ConvertedV4Data;
1876 // Load the client data from the filename given.
1878 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(vCardAdd[vi].ToStdString(), &ServerContactDataString);
1880 if (GetContactResponse.RequestResult != COREQUEST_OK){
1885 ServerContactData = wxString::FromUTF8(ServerContactDataString.c_str());
1887 ClientData.LoadFile(FullFilename);
1889 // Process v3 version into v4 version.
1891 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1893 vCardConflictObj vCardProcData;
1895 vCardProcData.vCardLocalData = &ClientData;
1896 vCardProcData.vCardServerData = &ConvertedV4Data;
1898 bool *PauseMode = new bool;
1903 qrn.QResponse = &ConflictResult;
1904 qrn.PausePtr = PauseMode;
1906 vCardProcData.QRNotifData = &qrn;
1908 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1909 event.SetClientData(&vCardProcData);
1910 wxPostEvent(frmMainPtrGet, event);
1912 while (*PauseMode == TRUE){
1913 SleepFor(250000000);
1919 if (ConflictResult == 0){
1926 } else if (ConflictResult == 1){
1928 // Decided to use client version.
1930 COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(vCardAdd[vi].ToStdString(), ClientData.WriteString().ToStdString());
1932 if (EditContactResponse.RequestResult != COREQUEST_OK){
1939 } else if (ConflictResult == 2){
1941 // Decided to use server version.
1943 // Write server version to file.
1945 ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
1953 // Setup the filename for the contact being added.
1955 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
1956 wxString wxSCardData;
1959 // Load the data from the file.
1962 wxFCard.Open(wxSFilename, wxT("r"));
1964 if (wxFCard.IsOpened()){
1966 wxFCard.ReadAll(&wxSCardData, wxConvAuto());
1971 // Convert the data to v3.
1973 vCard34Conv vCard34Obj;
1975 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
1977 // Upload the data to the server.
1979 COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(vCardAdd[vi].ToStdString(), TaskData.ToStdString());
1981 if (AddContactResponse.RequestResult != COREQUEST_OK){
1986 // Get the Server ETag and put it in the ETag database.
1988 COServerResponse EntityTagResponse = ConnObjectIter->second->GetServerEntityTagValue(vCardAdd[vi].ToStdString());
1990 if (EntityTagResponse.RequestResult != COREQUEST_OK){
1995 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
2003 if (iter->second != 1){
2007 // Process the vCards to delete locally.
2009 for (int vi = 0; vi < vCardDelete.GetCount(); vi++){
2011 if (!wxRemoveFile(CreateFilenamePath(AccountDirFinal, vCardDelete[vi]))){
2015 ETagDBPtr->RemoveETag(vCardDelete[vi]);
2021 // Process the vCards to delete from the server.
2023 std::map<wxString,wxString> *ETagFilenameMap = ETagDBPtr->GetFilenamePointer();
2024 std::list<wxString> DeleteList;
2026 for (std::map<wxString,wxString>::iterator deliter = ETagFilenameMap->begin();
2027 deliter != ETagFilenameMap->end(); deliter++){
2029 // Look through the ETagDB and find contacts which are marked as DELETED.
2031 if (deliter->second == wxT("DELETED")){
2033 ConnObjectIter->second->DeleteContact(deliter->first.ToStdString());
2035 // Delete the ETag as the file has now been deleted.
2037 DeleteList.push_back(deliter->first);
2043 for (std::list<wxString>::iterator removeetagiter = DeleteList.begin();
2044 removeetagiter != DeleteList.end(); removeetagiter++){
2046 ETagDBPtr->RemoveETag(*removeetagiter);
2050 // Write out the update server sync token.
2052 wxString ServerSyncToken = wxString::FromUTF8(ServerContactList2.SyncToken.c_str());
2054 ServerSyncToken.Trim();
2056 if (!ServerSyncToken.IsEmpty()){
2059 SyncWrite.Open(AccountSyncTokenFilename, wxT("w"));
2061 if (SyncWrite.IsOpened()){
2063 SyncWrite.Write(ServerSyncToken, wxConvAuto());
2070 // Check for another sync token. If there is another
2071 // sync token then repeat this. If token matches then stop processing.
2073 if (SyncTokenLoad == ServerSyncToken){
2075 KeepUpdating = FALSE;
2081 // Mark as completed.
2083 wxString AccountNameFinal;
2084 AccountNameFinal = PrefData.accounts.GetAccountDirectory(AccountID);
2085 AccountNameFinal.Append(wxT(".carddav"));
2087 wxCommandEvent reloadevent(RELOADCONTACTLIST);
2088 reloadevent.SetString(AccountNameFinal);
2089 wxPostEvent(this->GetParent(), reloadevent);
2096 // Update the data to the server.
2103 while (iter->second == 1){
2105 SleepFor(125000000);
2115 if (TasksFoundProc == FALSE){
2117 TasksActive = FALSE;
2118 ProcessTasksData = FALSE;
2122 wxCommandEvent eventp(ACTMGR_STARTTIMER);
2123 wxPostEvent(this, eventp);
2127 wxCommandEvent event(ACTMGR_STARTPROCESSTIMER);
2128 wxPostEvent(this, event);
2132 void frmActivityMgr::ProcessTasks()
2135 std::thread ProcessThread(&frmActivityMgr::ProcessTasksThread, this);
2136 ProcessThread.detach();
2140 void frmActivityMgr::SetupPointers(wxETagProcessTimer *ETagTmrPtrInc, void *frmMainPtrInc)
2143 ETagTmrPtr = ETagTmrPtrInc;
2145 frmMainPtr = frmMainPtrInc;
2149 void frmActivityMgr::ResumeProcessing(wxCommandEvent &event){
2151 QRNotif *qrn = (QRNotif *)event.GetClientData();
2153 int *QRes = qrn->QResponse;
2155 *qrn->PausePtr = FALSE;
2157 *QRes = event.GetInt();
2161 void frmActivityMgr::ToggleConnectionStatus( wxCommandEvent &event ){
2163 wxCommandEvent connevent(CONNSTAT_UPDATE);
2165 if (ApplicationSleepMode == FALSE){
2167 connevent.SetInt(1);
2168 ApplicationSleepMode = TRUE;
2169 mnuSleepMode->Check(true);
2173 connevent.SetInt(0);
2174 ApplicationSleepMode = FALSE;
2175 mnuSleepMode->Check(false);
2177 wxCommandEvent event(ACTMGR_STARTTIMER);
2178 wxPostEvent(this, event);
2180 wxCommandEvent eventp(ACTMGR_STARTPROCESSTIMER);
2181 wxPostEvent(this, eventp);
2185 wxPostEvent(GetParent(), connevent);
2189 void frmActivityMgr::SleepMode( wxCommandEvent &event ){
2191 wxCommandEvent connevent(ACTMGR_TOGGLECONN);
2193 wxPostEvent(this, connevent);
2197 void frmActivityMgr::WriteServerCertificate(wxString AccountName, SSLCertCollection SSLCertInc){
2199 wxString ServerCertFinal;
2201 // Get the Cert section of the certificate and write it to the file.
2203 ServerCertFinal = GetAccountDir(AccountName, TRUE);
2207 std::map<int, SSLCertData>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
2208 std::multimap<wxString,wxString>::iterator SSLDataIter = SSLCDIter->second.CertData.find(wxT("Cert"));
2210 CertFile.Open(ServerCertFinal, wxFile::write);
2212 CertFile.Write(SSLDataIter->second, wxConvUTF8);
2217 void frmActivityMgr::WriteServerCertificate(wxString AccountName, SSLCertCollectionString SSLCertInc){
2219 wxString ServerCertFinal;
2221 // Get the Cert section of the certificate and write it to the file.
2223 ServerCertFinal = GetAccountDir(AccountName, TRUE);
2227 std::map<int, SSLCertDataString>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
2228 std::multimap<string,string>::iterator SSLDataIter = SSLCDIter->second.CertData.find("Cert");
2230 CertFile.Open(ServerCertFinal, wxFile::write);
2232 CertFile.Write(SSLDataIter->second, wxConvUTF8);
2237 void frmActivityMgr::StartTimer(wxCommandEvent& event){
2239 ActListProcTimer.Start(1000, FALSE);
2243 void frmActivityMgr::StopTimer(wxCommandEvent& event){
2245 ActListProcTimer.Stop();
2249 void frmActivityMgr::StartProcessTimer(wxCommandEvent& event){
2251 ActProcessTimer.Start(1000, FALSE);
2255 void frmActivityMgr::StopProcessTimer(wxCommandEvent& event){
2257 ActProcessTimer.Stop();
2261 void frmActivityMgr::StartAnimationTimer(wxCommandEvent& event){
2263 AnimationTimer.StartAnimation();
2267 void frmActivityMgr::UpdateStatusLabel(wxCommandEvent& event){
2269 ActivityMgrLabelUpdate *actmgrudata = (ActivityMgrLabelUpdate*)event.GetClientData();
2271 if (actmgrudata != NULL){
2273 lstActivity->SetItem(actmgrudata->ItemIndex, 2, actmgrudata->ItemLabel);