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, &ActivityListConn);
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,CardDAV*>::iterator CardDAVIter;
217 std::map<int,ConnectionObject*>::iterator ConnObjectIter;
218 std::map<int,ActivityMgrAccountSettings>::iterator AccountSettingsIter;
219 std::map<int,long>::iterator LongIter;
220 bool TasksFoundProc = FALSE;
223 for (std::map<int,int>::iterator iter = ActivityList.begin();
224 iter != ActivityList.end(); ++iter){
226 // Check for any queued tasks and start one.
228 if (iter->second > 4){
232 wxCommandEvent StartAnimationEvent(ACTMGR_STARTANIMATIONTIMER);
234 wxPostEvent(this, StartAnimationEvent);
236 // Start the animation timer if it hasn't started.
238 CardDAVIter = ActivityListConn.find(iter->first);
239 ConnObjectIter = ActivityListConnObject.find(iter->first);
240 TypeIter = ActivityListType.find(iter->first);
241 StringETagIter = ActivityListETag.find(iter->first);
242 StringETagOrigIter = ActivityListETagOriginal.find(iter->first);
243 StringAccountIter = ActivityListAccount.find(iter->first);
244 StringFilenameIter = ActivityListFilename.find(iter->first);
245 StringFullFilenameIter = ActivityListFullFilename.find(iter->first);
246 AccountSettingsIter = ActivityListAccSettings.find(iter->first);
248 // Setup the CardDAV object.
252 ConnHandle.SetupConnection(AccountSettingsIter->second.Address,
253 AccountSettingsIter->second.Port,
254 AccountSettingsIter->second.Username,
255 AccountSettingsIter->second.Password,
256 AccountSettingsIter->second.SSL,
257 AccountSettingsIter->second.Prefix,
258 AccountSettingsIter->second.Dir);
259 ConnHandle.SetupVariables(&ActivityList, iter->first);
260 ConnHandle.SetupResultBools(&BlankBool, &BlankBool);
267 // 5 = Failed (UI Updated).
268 // 6 = Stopped (UI Updated).
269 // 7 = (Reserved) (UI Updated).
270 // 8 = Completed. (UI Updated).
272 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
273 XABPreferences PrefData(GetUserPrefDir());
275 if (iter->second == 0){
277 //CardDAVIter->second.
279 // Wait for process to finish.
282 TasksFoundProc = TRUE;
284 wxString ETagOriginal;
287 wxString ContactFilename;
288 wxString FullFilename;
290 bool EditMode = FALSE;
294 // Compare the ETags and work out if a conflict has occured.
296 if (TypeIter->second == 0){
300 StringDataIter = ActivityListData.find(iter->first);
301 StringURLIter = ActivityListURL.find(iter->first);
303 AccountDir = StringAccountIter->second;
304 ContactFilename = StringFilenameIter->second;
305 wxString AccountNameFriendly;
309 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
311 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
313 AccountDirCheck.Trim();
314 AccountDirCheck.Append(wxT(".carddav"));
316 if (AccountDirCheck == StringAccountIter->second){
318 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
325 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
327 ETag = StringETagIter->second;
328 ETagOriginal = StringETagOrigIter->second;
329 AccountDir = StringAccountIter->second;
330 ContactFilename = StringFilenameIter->second;
331 FullFilename = StringFullFilenameIter->second;
332 EditMode = ActivityListEditMode.find(iter->first)->second;
334 bool KeepUpdating = true;
336 while(KeepUpdating == true){
338 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
340 bool ExitLoop = false;
342 while (ExitLoop == false){
344 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
346 frmMainPtrGet->PauseAllTimers();
348 #if defined(__APPLE__)
349 #elif defined(__WIN32__)
352 bool UsingSSLBypass = false;
355 // Connect again and fetch SSL certificate information.
357 ConnObjectIter->second->BypassSSLVerification(true);
359 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
361 ConnObjectIter->second->BypassSSLVerification(false);
363 SSLInvalidCertNotifObjString SSLICNProcData;
365 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
367 bool *PauseMode = new bool;
371 qrn.QResponse = &SSLResult;
372 qrn.PausePtr = PauseMode;
374 SSLICNProcData.CertCollection = certcol;
375 SSLICNProcData.QRNotifData = &qrn;
376 SSLICNProcData.AccountName = AccountNameFriendly;
378 wxCommandEvent event(INVALIDSSLCERTSTRING);
379 event.SetClientData(&SSLICNProcData);
380 wxPostEvent(frmMainPtrGet, event);
382 while (*PauseMode == TRUE){
383 //nanosleep(&n1, &n2);
387 // Process the response from the user.
391 // Accept the Certificate.
393 UsingSSLBypass = true;
394 ConnObjectIter->second->BypassSSLVerification(true);
396 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
397 WriteServerCertificate(AccountDir, certcol);
399 ConnObjectIter->second->BypassSSLVerification(false);
401 } else if (SSLResult == 2){
403 // Reject the certificate, abort the task and mark as failed.
412 frmMainPtrGet->ResumeAllTimers();
415 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
417 ConnectResponse = ConnObjectIter->second->Connect(true);
419 if (ConnectResponse == COCONNECT_OK){
432 } else if (ConnectResponse == COCONNECT_OK){
434 ConnectResponse = ConnObjectIter->second->Connect(true);
449 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
453 #if defined(__APPLE__)
455 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
457 #elif defined(__WIN32__)
459 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
463 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData());
467 COServerResponse AddContactResponse = ConnObjectIter->second->AddContact(StringURLIter->second.ToStdString(),
468 StringDataIter->second.ToStdString());
470 if (AddContactResponse.RequestResult != COREQUEST_OK){
475 /*while (ExitLoop == FALSE){
477 // Verify that we have a trusted SSL connection first.
479 int ErrorCode = ConnHandle.SSLVerifyTest();
481 if (ErrorCode != CURLE_OK){
483 if (ErrorCode == CURLE_SSL_CACERT ||
484 ErrorCode == CURLE_PEER_FAILED_VERIFICATION ||
485 ErrorCode == CURLE_SSL_CONNECT_ERROR){
487 // Invalid SSL certificate so bring up a dialog to the user
488 // explaining what has happened and the options available.
490 // Stop all the timers.
492 frmMainPtrGet->PauseAllTimers();
496 #if defined(__APPLE__)
498 // Bring up the trust panel and display the SSL certificate dialog.
500 SSLResult = DisplayTrustPanel(ConnHandle.GetTrustObject(), AccountNameFriendly);
502 // Update the SSL account with the trust object.
504 if (SSLResult != NSOKButton){
506 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetTrustObject());
512 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
517 #elif defined(__WIN32__)
519 BOOL ModifiedCertificateData;
520 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&ConnHandle, (HWND)frmMainPtrGet->GetHandle());
522 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)){
523 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
526 if (ModifiedCertificateData == FALSE){
528 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateContextPointer());
534 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
540 // Setup the data to be sent in the wxPostEvent command.
542 SSLInvalidCertNotifObj SSLICNProcData;
544 SSLCertCollection certcol = ConnHandle.GetSSLVerifyResults();
546 bool *PauseMode = new bool;
550 qrn.QResponse = &SSLResult;
551 qrn.PausePtr = PauseMode;
553 SSLICNProcData.CertCollection = certcol;
554 SSLICNProcData.QRNotifData = &qrn;
555 SSLICNProcData.AccountName = AccountNameFriendly;
557 wxCommandEvent event(INVALIDSSLCERT);
558 event.SetClientData(&SSLICNProcData);
559 wxPostEvent(frmMainPtrGet, event);
561 while (*PauseMode == TRUE){
562 //nanosleep(&n1, &n2);
566 // Process the response from the user.
570 // Accept the certificate. Write the certificate into the account
571 // directory (as server.crt) - will overwrite old certificate.
573 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData());
574 WriteServerCertificate(AccountDir, ConnHandle.GetCertificateData());
576 } else if (SSLResult == 2){
578 // Reject the certificate, abort the task and mark as failed.
580 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateData());
588 // Resume all the timers.
590 frmMainPtrGet->ResumeAllTimers();
608 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
612 #if defined(__APPLE__)
614 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
616 #elif defined(__WIN32__)
618 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
622 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData());
626 ConnHandle.SetUploadMode(TRUE);
627 ConnHandle.SetupData(wxT("PUT"), StringURLIter->second, StringDataIter->second);
628 ConnHandle.SetEditMode(EditMode);
629 ConnHandle.ProcessData();
631 int ErrorCode = ConnHandle.GetResultCode();
633 if (ErrorCode != CURLE_OK){
640 // Add contact to the ETag DB.
642 ConnHandle.GetServerETagValueThread();
643 ETagServer = ConnHandle.ETagValueResult();*/
645 // TODO: Get the entity tag for the new contact.
647 COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
649 if (ETagGetResponse.RequestResult != COREQUEST_OK){
654 std::cout << ETagGetResponse.EntityTag << std::endl;
656 ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag);
663 } else if (TypeIter->second == 1){
667 StringDataIter = ActivityListData.find(iter->first);
668 StringURLIter = ActivityListURL.find(iter->first);
670 AccountDir = StringAccountIter->second;
671 ContactFilename = StringFilenameIter->second;
672 wxString AccountNameFriendly;
676 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
678 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
680 AccountDirCheck.Trim();
681 AccountDirCheck.Append(wxT(".carddav"));
683 if (AccountDirCheck == StringAccountIter->second){
685 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
692 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
694 ETag = StringETagIter->second;
695 ETagOriginal = StringETagOrigIter->second;
696 AccountDir = StringAccountIter->second;
697 ContactFilename = StringFilenameIter->second;
698 FullFilename = StringFullFilenameIter->second;
699 EditMode = ActivityListEditMode.find(iter->first)->second;
701 bool KeepUpdating = true;
703 while(KeepUpdating == true){
705 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
707 bool ExitLoop = false;
709 while(ExitLoop == false){
711 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
713 frmMainPtrGet->PauseAllTimers();
715 #if defined(__APPLE__)
716 #elif defined(__WIN32__)
719 bool UsingSSLBypass = false;
722 // Connect again and fetch SSL certificate information.
724 ConnObjectIter->second->BypassSSLVerification(true);
726 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
728 ConnObjectIter->second->BypassSSLVerification(false);
730 SSLInvalidCertNotifObjString SSLICNProcData;
732 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
734 bool *PauseMode = new bool;
738 qrn.QResponse = &SSLResult;
739 qrn.PausePtr = PauseMode;
741 SSLICNProcData.CertCollection = certcol;
742 SSLICNProcData.QRNotifData = &qrn;
743 SSLICNProcData.AccountName = AccountNameFriendly;
745 wxCommandEvent event(INVALIDSSLCERTSTRING);
746 event.SetClientData(&SSLICNProcData);
747 wxPostEvent(frmMainPtrGet, event);
749 while (*PauseMode == TRUE){
750 //nanosleep(&n1, &n2);
754 // Process the response from the user.
758 // Accept the Certificate.
760 UsingSSLBypass = true;
761 ConnObjectIter->second->BypassSSLVerification(true);
763 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
764 WriteServerCertificate(AccountDir, certcol);
766 ConnObjectIter->second->BypassSSLVerification(false);
768 } else if (SSLResult == 2){
770 // Reject the certificate, abort the task and mark as failed.
779 frmMainPtrGet->ResumeAllTimers();
782 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
784 ConnectResponse = ConnObjectIter->second->Connect(true);
786 if (ConnectResponse == COCONNECT_OK){
799 } else if (ConnectResponse == COCONNECT_OK){
801 ConnectResponse = ConnObjectIter->second->Connect(true);
816 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
820 COServerResponse ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
822 if (ETagGetResponse.RequestResult != COREQUEST_OK){
827 ETagServer = ETagGetResponse.EntityTag;
829 // Compare the ETag with the Server ETag.
831 if (ETagOriginal != ETagServer){
833 // Server ETag is different from original ETag.
834 // This is a conflict which the user will now
837 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
839 vCard34Conv vCard34Obj;
841 std::string ServerContactData;
842 wxString wxStringServerContactData;
844 vCard ConvertedV4Data;
846 // Load the client data from the filename given.
848 //ConnHandle.GetServerContactData();
849 //ServerContactData = ConnHandle.GetPageData();
851 COServerResponse GetContactResponse = ConnObjectIter->second->GetContact(StringURLIter->second.ToStdString(),
854 if (GetContactResponse.RequestResult != COREQUEST_OK){
859 wxStringServerContactData = wxString::FromUTF8((const char*)ServerContactData.c_str());
861 // Process v3 version into v4 version.
863 vCard34Obj.ConvertToV4(&wxStringServerContactData, &ConvertedV4Data);
865 vCardConflictObj vCardProcData;
867 vCardProcData.vCardLocalData = &ClientData;
868 vCardProcData.vCardServerData = &ConvertedV4Data;
869 ClientData.LoadFile(FullFilename);
871 // Setup Conflict Resolution Dialog.
873 // Fetch Data from Server and convert.
875 bool *PauseMode = new bool;
880 qrn.QResponse = &ConflictResult;
881 qrn.PausePtr = PauseMode;
883 vCardProcData.QRNotifData = &qrn;
885 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
886 event.SetClientData(&vCardProcData);
887 wxPostEvent(frmMainPtrGet, event);
889 while (*PauseMode == TRUE){
896 if (ConflictResult == 0){
903 } else if (ConflictResult == 1){
905 // Decided to use client version.
907 } else if (ConflictResult == 2){
909 // Decided to use server version.
911 // Download server version.
913 ConvertedV4Data.WriteFile(FullFilename);
919 // Upload the data to the server.
921 COServerResponse EditContactResponse = ConnObjectIter->second->EditContact(StringURLIter->second.ToStdString(),
922 StringDataIter->second.ToStdString());
924 if (EditContactResponse.RequestResult != COREQUEST_OK){
931 /*while (ExitLoop == FALSE){
933 int ErrorCode = ConnHandle.SSLVerifyTest();
935 if (ErrorCode != CURLE_OK){
937 if (ErrorCode == CURLE_SSL_CACERT ||
938 ErrorCode == CURLE_PEER_FAILED_VERIFICATION ||
939 ErrorCode == CURLE_SSL_CONNECT_ERROR){
941 // Invalid SSL certificate so bring up a dialog to the user
942 // explaining what has happened and the options available.
944 // Stop all the timers.
946 frmMainPtrGet->PauseAllTimers();
950 #if defined(__APPLE__)
952 // Bring up the trust panel and display the SSL certificate dialog.
954 SSLResult = DisplayTrustPanel(ConnHandle.GetTrustObject(), AccountNameFriendly);
956 // Update the SSL account with the trust object.
958 if (SSLResult != NSOKButton){
960 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetTrustObject());
966 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
970 #elif defined(__WIN32__)
972 BOOL ModifiedCertificateData;
973 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&ConnHandle, (HWND)frmMainPtrGet->GetHandle());
975 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)){
976 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
979 if (ModifiedCertificateData == FALSE){
981 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateContextPointer());
987 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
993 // Setup the data to be sent in the wxPostEvent command.
995 SSLInvalidCertNotifObj SSLICNProcData;
997 SSLCertCollection certcol = ConnHandle.GetSSLVerifyResults();
999 bool *PauseMode = new bool;
1003 qrn.QResponse = &SSLResult;
1004 qrn.PausePtr = PauseMode;
1006 SSLICNProcData.CertCollection = certcol;
1007 SSLICNProcData.QRNotifData = &qrn;
1008 SSLICNProcData.AccountName = AccountNameFriendly;
1010 wxCommandEvent event(INVALIDSSLCERT);
1011 event.SetClientData(&SSLICNProcData);
1012 wxPostEvent(frmMainPtrGet, event);
1014 while (*PauseMode == TRUE){
1015 //nanosleep(&n1, &n2);
1016 SleepFor(250000000);
1019 // Process the response from the user.
1021 if (SSLResult == 1){
1023 // Accept the certificate. Write the certificate into the account
1024 // directory (as server.crt) - will overwrite old certificate.
1026 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetSSLVerifyResults());
1027 WriteServerCertificate(AccountDir, ConnHandle.GetSSLVerifyResults());
1029 } else if (SSLResult == 2){
1031 // Reject the certificate, abort the task and mark as failed.
1033 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetSSLVerifyResults());
1041 // Resume all the timers.
1043 frmMainPtrGet->ResumeAllTimers();
1061 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1065 #if defined(__APPLE__)
1067 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
1069 #elif defined(__WIN32__)
1071 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
1075 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetSSLVerifyResults());
1079 ConnHandle.SetUploadMode(TRUE);
1080 ConnHandle.SetupData(wxT("PUT"), StringURLIter->second, StringDataIter->second);
1081 ConnHandle.SetEditMode(EditMode);
1082 ConnHandle.GetServerETagValueThread();
1083 ETagServer = ConnHandle.ETagValueResult();
1085 int ErrorCode = ConnHandle.GetResultCode();
1087 if (ErrorCode != CURLE_OK){
1092 // Compare the ETag with the Server ETag.
1094 if (ETagOriginal != ETagServer){
1096 // Server ETag is different from original ETag.
1097 // This is a conflict which the user will now
1103 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1105 vCard34Conv vCard34Obj;
1107 wxString ServerContactData;
1109 vCard ConvertedV4Data;
1111 // Load the client data from the filename given.
1113 ConnHandle.GetServerContactData();
1114 ServerContactData = ConnHandle.GetPageData();
1116 // Process v3 version into v4 version.
1118 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1120 vCardConflictObj vCardProcData;
1122 vCardProcData.vCardLocalData = &ClientData;
1123 vCardProcData.vCardServerData = &ConvertedV4Data;
1124 ClientData.LoadFile(FullFilename);
1126 //ContactConflictEvent event(ContactConflictCmdEnv);
1127 //event.SetData(ContactFilename, Moo1, Moo2);
1129 //wxPostEvent(frmMainPtrGet, event);
1131 // Setup Conflict Resolution Dialog.
1133 // Fetch Data from Server and convert.
1135 bool *PauseMode = new bool;
1140 qrn.QResponse = &ConflictResult;
1141 qrn.PausePtr = PauseMode;
1143 vCardProcData.QRNotifData = &qrn;
1145 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1146 event.SetClientData(&vCardProcData);
1147 wxPostEvent(frmMainPtrGet, event);
1149 while (*PauseMode == TRUE){
1150 //nanosleep(&n1, &n2);
1151 SleepFor(250000000);
1157 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
1159 if (ConflictResult == 0){
1166 } else if (ConflictResult == 1){
1168 // Decided to use client version.
1170 //ConnHandle.ProcessData();
1172 } else if (ConflictResult == 2){
1174 // Decided to use server version.
1176 // Download server version.
1178 ConvertedV4Data.WriteFile(FullFilename);
1184 // Update the data to the server.
1186 ConnHandle.ProcessData();
1190 // Update the ETag DB.
1192 //ConnHandle.GetServerETagValueThread();
1193 //ETagServer = ConnHandle.ETagValueResult();
1195 ETagGetResponse = ConnObjectIter->second->GetServerEntityTagValue(StringURLIter->second.ToStdString());
1197 if (ETagGetResponse.RequestResult != COREQUEST_OK){
1202 ETagDBPtr->UpdateETag(ContactFilename, ETagGetResponse.EntityTag, ETagGetResponse.EntityTag);
1209 } else if (TypeIter->second == 2) {
1211 // Delete the contact from the server and update the ETag DB.
1213 AccountDir = StringAccountIter->second;
1214 ContactFilename = StringFilenameIter->second;
1215 wxString AccountNameFriendly;
1219 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
1221 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
1223 AccountDirCheck.Trim();
1224 AccountDirCheck.Append(wxT(".carddav"));
1226 if (AccountDirCheck == StringAccountIter->second){
1228 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
1235 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1237 bool KeepUpdating = TRUE;
1239 while(KeepUpdating == TRUE){
1241 COConnectResult ConnectResponse = ConnObjectIter->second->Connect(false);
1243 bool ExitLoop = FALSE;
1245 while (ExitLoop == FALSE){
1247 if (ConnObjectIter->second->SSLVerify() == COSSL_UNABLETOVERIFY){
1249 frmMainPtrGet->PauseAllTimers();
1251 #if defined(__APPLE__)
1252 #elif defined(__WIN32__)
1255 bool UsingSSLBypass = false;
1258 // Connect again and fetch SSL certificate information.
1260 ConnObjectIter->second->BypassSSLVerification(true);
1262 COConnectResult ConnectionSSLResult = ConnObjectIter->second->Connect(false);
1264 ConnObjectIter->second->BypassSSLVerification(false);
1266 SSLInvalidCertNotifObjString SSLICNProcData;
1268 SSLCertCollectionString certcol = ConnObjectIter->second->BuildSSLCollection();
1270 bool *PauseMode = new bool;
1274 qrn.QResponse = &SSLResult;
1275 qrn.PausePtr = PauseMode;
1277 SSLICNProcData.CertCollection = certcol;
1278 SSLICNProcData.QRNotifData = &qrn;
1279 SSLICNProcData.AccountName = AccountNameFriendly;
1281 wxCommandEvent event(INVALIDSSLCERTSTRING);
1282 event.SetClientData(&SSLICNProcData);
1283 wxPostEvent(frmMainPtrGet, event);
1285 while (*PauseMode == TRUE){
1286 //nanosleep(&n1, &n2);
1287 SleepFor(250000000);
1290 // Process the response from the user.
1292 if (SSLResult == 1){
1294 // Accept the Certificate.
1296 UsingSSLBypass = true;
1297 ConnObjectIter->second->BypassSSLVerification(true);
1299 COConnectResult TestConnectionResult = ConnObjectIter->second->Connect(true);
1300 WriteServerCertificate(AccountDir, certcol);
1302 ConnObjectIter->second->BypassSSLVerification(false);
1304 } else if (SSLResult == 2){
1306 // Reject the certificate, abort the task and mark as failed.
1315 frmMainPtrGet->ResumeAllTimers();
1318 } else if (ConnectResponse == COCONNECT_AUTHFAIL){
1320 ConnectResponse = ConnObjectIter->second->Connect(true);
1322 if (ConnectResponse == COCONNECT_OK){
1335 } else if (ConnectResponse == COCONNECT_OK){
1337 ConnectResponse = ConnObjectIter->second->Connect(true);
1350 /*int ErrorCode = ConnHandle.SSLVerifyTest();
1352 if (ErrorCode != CURLE_OK){
1354 if (ErrorCode == CURLE_SSL_CACERT ||
1355 ErrorCode == CURLE_PEER_FAILED_VERIFICATION ||
1356 ErrorCode == CURLE_SSL_CONNECT_ERROR){
1358 // Invalid SSL certificate so bring up a dialog to the user
1359 // explaining what has happened and the options available.
1361 // Stop all the timers.
1363 frmMainPtrGet->PauseAllTimers();
1367 #if defined(__APPLE__)
1369 // Bring up the trust panel and display the SSL certificate dialog.
1371 SSLResult = DisplayTrustPanel(ConnHandle.GetTrustObject(), AccountNameFriendly);
1373 // Update the SSL account with the trust object.
1375 if (SSLResult != NSOKButton){
1377 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetTrustObject());
1383 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
1387 #elif defined(__WIN32__)
1389 BOOL ModifiedCertificateData;
1390 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&ConnHandle, (HWND)frmMainPtrGet->GetHandle());
1392 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)){
1393 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
1396 if (ModifiedCertificateData == FALSE){
1398 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateContextPointer());
1404 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
1410 // Setup the data to be sent in the wxPostEvent command.
1412 SSLInvalidCertNotifObj SSLICNProcData;
1414 SSLCertCollection certcol = ConnHandle.GetSSLVerifyResults();
1416 bool *PauseMode = new bool;
1420 qrn.QResponse = &SSLResult;
1421 qrn.PausePtr = PauseMode;
1423 SSLICNProcData.CertCollection = certcol;
1424 SSLICNProcData.QRNotifData = &qrn;
1425 SSLICNProcData.AccountName = AccountNameFriendly;
1427 wxCommandEvent event(INVALIDSSLCERT);
1428 event.SetClientData(&SSLICNProcData);
1429 wxPostEvent(frmMainPtrGet, event);
1431 while (*PauseMode == TRUE){
1432 //nanosleep(&n1, &n2);
1433 SleepFor(250000000);
1436 // Process the response from the user.
1438 if (SSLResult == 1){
1440 // Accept the certificate. Write the certificate into the account
1441 // directory (as server.crt) - will overwrite old certificate.
1443 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetSSLVerifyResults());
1444 WriteServerCertificate(AccountDir, ConnHandle.GetSSLVerifyResults());
1446 } else if (SSLResult == 2){
1448 // Reject the certificate, abort the task and mark as failed.
1450 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetSSLVerifyResults());
1458 // Resume all the timers.
1460 frmMainPtrGet->ResumeAllTimers();
1478 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1482 #if defined(__APPLE__)
1484 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
1486 #elif defined(__WIN32__)
1488 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
1492 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetSSLVerifyResults());
1496 /*ConnHandle.SetUploadMode(FALSE);
1497 ConnHandle.SetupData(wxT("DELETE"), ContactFilename, wxT(""));
1498 ConnHandle.ProcessData();
1500 SSLCertCollection certcol = ConnHandle.GetCertificateData();
1502 // Check if any errors have occured whilst getting the data.
1504 int ErrorCode = ConnHandle.GetResultCode();
1506 if (ErrorCode != CURLE_OK){
1511 COServerResponse DeleteContactResponse = ConnObjectIter->second->DeleteContact(ContactFilename.ToStdString());
1513 if (DeleteContactResponse.RequestResult != COREQUEST_OK){
1518 ETagDBPtr->RemoveETag(ContactFilename);
1521 //CardDAVIter->second = NULL;
1526 } else if (TypeIter->second == 3) {
1528 wxString AccountNameFriendly;
1532 for (int i = 0; i < PrefData.accounts.GetCount(); i++){
1534 wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i);
1536 AccountDirCheck.Trim();
1537 AccountDirCheck.Append(wxT(".carddav"));
1539 if (AccountDirCheck == StringAccountIter->second){
1541 AccountNameFriendly = PrefData.accounts.GetAccountName(i);
1548 bool KeepUpdating = TRUE;
1550 while (KeepUpdating == TRUE){
1552 StringTaskDetailIter = ActivityListTaskDetail.find(iter->first);
1554 AccountDir = StringAccountIter->second;
1556 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
1558 wxArrayString vCardAdd;
1559 wxArrayString vCardDelete;
1560 wxArrayString vCardList;
1561 wxArrayString vCardUpdateLocal;
1562 wxArrayString vCardUpdateRemote;
1563 wxArrayString vCardUpdateLog;
1564 std::map<wxString,wxString> vCardETag;
1565 ContactListData ServerContactList;
1567 std::map<wxString,vCardDownloadDataFileClass> vCardDownloadData;
1568 //wxArrayString vCardDownload;
1569 //std::map<wxString,wxString> vCardDownloadData;
1571 // Get the list of vCard files locally.
1573 wxString AccountDirFinal;
1574 wxString AccountSyncTokenFilename;
1575 wxString SyncTokenLoad;
1577 #if defined(__HAIKU__)
1579 //preffilename = wxT("noo");
1581 #elif defined(__WIN32__)
1583 AccountDirFinal = GetAccountDir(AccountDir, FALSE);
1584 AccountSyncTokenFilename = AccountDirFinal;
1585 AccountSyncTokenFilename.Append(wxT("synctoken"));
1589 AccountDirFinal = GetAccountDir(AccountDir, FALSE);
1590 AccountSyncTokenFilename = AccountDirFinal;
1591 AccountSyncTokenFilename.Append(wxT("synctoken"));
1595 // Suppress Error Messages.
1597 wxLogNull PleaseDoNotDisturb;
1600 SyncLoad.Open(AccountSyncTokenFilename, wxT("r"));
1602 if (SyncLoad.IsOpened()){
1604 SyncLoad.ReadAll(&SyncTokenLoad, wxMBConvUTF8());
1609 // Get a list of vCard files from the server.
1611 // Get the list of vCard files from the ETagDB.
1613 std::map<wxString,ETagData> ETagDBData = ETagDBPtr->GetETagData();
1615 bool ExitLoop = FALSE;
1617 std::map<wxString,FileSyncData> *SCListData = NULL;
1619 while (ExitLoop == FALSE){
1621 // Check if any errors have occured whilst getting the data.
1623 int ErrorCode = ConnHandle.SSLVerifyTest();
1625 if (ErrorCode != CURLE_OK){
1627 if (ErrorCode == CURLE_SSL_CACERT ||
1628 ErrorCode == CURLE_PEER_FAILED_VERIFICATION ||
1629 ErrorCode == CURLE_SSL_CONNECT_ERROR){
1631 // Invalid SSL certificate so bring up a dialog to the user
1632 // explaining what has happened and the options available.
1634 // Stop all the timers.
1636 frmMainPtrGet->PauseAllTimers();
1640 #if defined(__APPLE__)
1642 // Bring up the trust panel and display the SSL certificate dialog.
1644 SSLResult = DisplayTrustPanel(ConnHandle.GetTrustObject(), AccountNameFriendly);
1646 // Update the SSL account with the trust object.
1648 if (SSLResult != NSOKButton){
1650 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetTrustObject());
1656 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
1660 #elif defined(__WIN32__)
1662 BOOL ModifiedCertificateData;
1663 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&ConnHandle, (HWND)frmMainPtrGet->GetHandle());
1665 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)){
1666 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
1669 if (ModifiedCertificateData == FALSE){
1671 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateContextPointer());
1677 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
1682 // Setup the data to be sent in the wxPostEvent command.
1684 SSLInvalidCertNotifObj SSLICNProcData;
1686 SSLCertCollection certcol = ConnHandle.GetSSLVerifyResults();
1688 bool *PauseMode = new bool;
1692 qrn.QResponse = &SSLResult;
1693 qrn.PausePtr = PauseMode;
1695 SSLICNProcData.CertCollection = certcol;
1696 SSLICNProcData.QRNotifData = &qrn;
1697 SSLICNProcData.AccountName = AccountNameFriendly;
1699 wxCommandEvent event(INVALIDSSLCERT);
1700 event.SetClientData(&SSLICNProcData);
1701 wxPostEvent(frmMainPtrGet, event);
1705 // Fall asleep until we get an response.
1708 n1.tv_nsec = 250000000L;*/
1710 while (*PauseMode == TRUE){
1711 //nanosleep(&n1, &n2);
1712 SleepFor(250000000);
1715 // Process the response from the user.
1717 if (SSLResult == 1){
1719 // Accept the certificate. Write the certificate into the account
1720 // directory (as server.crt) - will overwrite old certificate.
1722 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetSSLVerifyResults());
1723 WriteServerCertificate(AccountDir, ConnHandle.GetSSLVerifyResults());
1725 } else if (SSLResult == 2){
1727 // Reject the certificate, abort the task and mark as failed.
1729 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetSSLVerifyResults());
1737 // Resume all the timers.
1739 frmMainPtrGet->ResumeAllTimers();
1757 if (iter->second == 2 || iter->second == 3 || iter->second == 4){
1761 #if defined(__APPLE__)
1763 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject());
1765 #elif defined(__WIN32__)
1767 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateContextPointer());
1771 frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetSSLVerifyResults());
1775 // Look in directory and see which vCards to add and delete
1778 // vCard exists but not in ETagDB - Add (and upload).
1779 // vCard missing but in ETagDB - Delete (from server).
1781 // Start by looking for vCards that are not in the ETagDB.
1783 wxDir AccDir(AccountDirFinal);
1784 wxString AccDirFilename;
1786 bool ContinueProcess = AccDir.GetFirst(&AccDirFilename, wxEmptyString, wxDIR_DEFAULT);
1788 ServerContactList = ConnHandle.GetContactList(SyncTokenLoad.ToUTF8());
1790 int ErrorCode = ConnHandle.GetResultCode();
1792 if (ErrorCode != CURLE_OK){
1799 SSLCertCollection certcol = ConnHandle.GetCertificateData();
1801 // Create a pointer for the std::map<wxString,FileSyncData>.
1803 SCListData = &ServerContactList.ListData;
1805 // Process the server data.
1807 if (SCListData != NULL){
1809 for (std::map<wxString, FileSyncData>::iterator fsiter = SCListData->begin();
1810 fsiter != SCListData->end(); fsiter++){
1812 // Check the mode of the file before adding.
1814 // Get the HTTP status of the file from the list.
1816 FileSyncData fsd = fsiter->second;
1818 if (fsd.DataFlag == 1){
1820 // Added or modified contact.
1822 //vCardAdd.Add(fsiter->first, 1);
1824 vCardDownloadDataFileClass vCardNewData;
1825 vCardNewData.DataType = 0;
1826 vCardDownloadData.insert(std::make_pair(fsiter->first, vCardNewData));
1828 //} else if (fsd.DataFlag == 1){
1830 // Contact has been modified on the server.
1832 // vCardUpdateLocal.Add(fsiter->first, 1);
1835 else if (fsd.DataFlag == 2){
1837 // Contact has been deleted.
1839 vCardDelete.Add(fsiter->first, 1);
1847 // Look for vCards that are missing but in the ETagDB.
1849 for (std::map<wxString,ETagData>::iterator etagiter = ETagDBData.begin();
1850 etagiter != ETagDBData.end(); etagiter++){
1852 // Check if the vCard exists.
1854 if (!wxFileExists(AccountDirFinal + etagiter->first)){
1856 // vCard doesn't exist so mark for deleting.
1858 vCardDelete.Add(etagiter->first, 1);
1864 // Compare Server with the ETag DB. Work out which
1865 // needs to updated remotely and which needs to be
1868 for (std::map<wxString,FileSyncData>::iterator srviter = SCListData->begin();
1869 srviter != SCListData->end(); srviter++){
1871 if (ETagDBData.find(srviter->first) == ETagDBData.end()){
1875 std::map<wxString,ETagData>::iterator lociter = ETagDBData.find(srviter->first);
1877 ETagData etd = lociter->second;
1878 FileSyncData fsd = srviter->second;
1880 wxString ETagSrv = fsd.ETagData;
1882 // Check if the local ETag value has 'DELETED' set and skip
1883 // checking this file if it does.
1885 if (etd.ETagValue == "DELETED"){
1891 // Compare local ETag with original local ETag.
1892 // If different, update to server is required.
1894 if (etd.ETagValue != etd.ETagOrigValue){
1896 // Check if local original ETag is different from server ETag.
1897 // If it is then throw up a conflict resolution dialog.
1899 if (etd.ETagOrigValue != ETagSrv){
1901 vCard34Conv vCard34Obj;
1903 wxString ServerContactData;
1904 wxString FullFilename;
1906 vCard ConvertedV4Data;
1908 // Conflict. Bring up conflict resolution dialog.
1910 // Get the server contact data for comparison.
1912 ConnHandle.SetServerFilename(srviter->first);
1913 ConnHandle.GetServerContactData();
1914 ServerContactData = ConnHandle.GetPageData();
1916 FullFilename = CreateFilenamePath(AccountDirFinal, srviter->first);
1918 // Convert it to v4.
1920 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
1922 vCardConflictObj vCardProcData;
1924 vCardProcData.vCardLocalData = &ClientData;
1925 vCardProcData.vCardServerData = &ConvertedV4Data;
1927 // Load the client data from the filename given.
1929 ClientData.LoadFile(FullFilename);
1933 bool *PauseMode = new bool;
1936 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
1939 qrn.QResponse = &ConflictResult;
1940 qrn.PausePtr = PauseMode;
1942 vCardProcData.QRNotifData = &qrn;
1944 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
1945 event.SetClientData(&vCardProcData);
1946 wxPostEvent(frmMainPtrGet, event);
1951 n1.tv_nsec = 250000000L;*/
1953 while (*PauseMode == TRUE){
1954 //nanosleep(&n1, &n2);
1955 SleepFor(250000000);
1961 // If replacing local version, store copy for now.
1962 // Otherwise upload to server.
1964 if (ConflictResult == 0){
1970 } else if (ConflictResult == 1){
1972 // Decided to use client version.
1974 vCardUpdateLocal.Remove(srviter->first);
1975 vCardUpdateRemote.Add(srviter->first, 1);
1977 } else if (ConflictResult == 2){
1979 // Decided to use server version.
1980 // Store the converted downloaded copy for
1981 // processing later.
1983 //vCardUpdateLocal.Add(srviter->first, 1);
1985 vCardDownloadDataFileClass vCardNewData;
1986 vCardNewData.DataType = 1;
1987 vCardNewData.FileData = ConvertedV4Data.WriteString();
1989 ConvertedV4Data.WriteFile(FullFilename);
1991 vCardDownloadData.insert(std::make_pair(srviter->first, vCardNewData));
1999 // Send vCard to the server.
2001 vCardUpdateRemote.Add(srviter->first, 1);
2007 // Check if local ETag is different from server ETag.
2008 // If different, obtain the vCard from the server.
2010 if (etd.ETagOrigValue != ETagSrv){
2012 vCardUpdateLocal.Add(srviter->first, 1);
2020 // Process the vCards that need to be downloaded and added/overwritten.
2022 wxString vCardFilenameFinal;
2023 wxString vCardOutputData;
2024 wxString vCardInputData;
2025 wxString ETagFileData;
2028 for (std::map<wxString,vCardDownloadDataFileClass>::iterator additer = vCardDownloadData.begin();
2029 additer != vCardDownloadData.end(); additer++){
2031 vCardFilenameFinal.Clear();
2032 vCardInputData.Clear();
2033 vCardOutputData.Clear();
2034 ETagFileData.Clear();
2036 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
2038 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
2040 if (additer->second.DataType == 0){
2042 vCard ConvertedV4Data;
2044 // Grab the file from the server and write the contact out.
2046 vCard34Conv vCard34Obj;
2048 ConnHandle.SetServerFilename(additer->first);
2049 ConnHandle.GetServerContactData();
2050 vCardInputData = ConnHandle.GetPageData();
2052 vCard34Obj.ConvertToV4(&vCardInputData, &ConvertedV4Data);
2054 ConvertedV4Data.WriteFile(vCardFilenameFinal);
2056 // Update the ETagDB.
2058 //vCardFile.Open(vCardFilenameFinal, wxT("w"));
2060 //if (vCardFile.IsOpened()){
2062 // vCardFile.Write(vCardOutputData, wxConvAuto());
2063 // vCardFile.Close();
2067 // Get the server ETag.
2069 wxString ServerETag;
2070 ConnHandle.GetServerETagValueThread();
2071 ServerETag = ConnHandle.ETagValueResult();
2073 // Add to the ETag Database.
2075 ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
2077 } else if (additer->second.DataType == 1){
2079 // Write out the contact to the account directory.
2081 vCardFilenameFinal = CreateFilenamePath(AccountDirFinal, additer->first);
2082 vCardOutputData = additer->second.FileData;
2084 vCardFile.Open(vCardFilenameFinal, wxT("w"));
2086 if (vCardFile.IsOpened()){
2088 vCardFile.Write(vCardOutputData, wxConvAuto());
2093 // Update the ETagDB.
2095 wxString ServerETag;
2096 ConnHandle.GetServerETagValueThread();
2097 ServerETag = ConnHandle.ETagValueResult();
2099 // Add to the ETag Database.
2101 ETagDBPtr->AddETag(additer->first, ServerETag, ServerETag);
2106 // Look for vCard files which aren't in the ETagDB.
2108 while(ContinueProcess){
2110 // Check and make sure that the filename ends in .vcf/.vcard
2111 // Skip any file that beings with a dot.
2113 if (AccDirFilename.Left(1) == wxT(".")){
2115 ContinueProcess = AccDir.GetNext(&AccDirFilename);
2120 if (AccDirFilename.Right(4) != wxT(".vcf") &&
2121 AccDirFilename.Right(6) != wxT(".vcard")){
2123 ContinueProcess = AccDir.GetNext(&AccDirFilename);
2128 // Look in the ETagDB for the file.
2130 bool ETagExists = ETagDBPtr->CheckETagExists(AccDirFilename);
2132 if (ETagExists == FALSE){
2134 // Add the vCard to the list to be added.
2136 vCardAdd.Add(AccDirFilename, 1);
2140 ContinueProcess = AccDir.GetNext(&AccDirFilename);
2144 // Process the vCards to add/update to the server.
2146 for (int vi = 0; vi < vCardAdd.GetCount(); vi++){
2148 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
2149 ETag = ETagDBPtr->GetETag(vCardAdd[vi]);
2150 ETagOriginal = ETagDBPtr->GetETagOriginal(vCardAdd[vi]);
2152 ContactFilename = vCardAdd[vi];
2153 FullFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
2155 ContactFilename.Trim();
2157 if (ETag == ETagOriginal && ETagDBPtr->ETagExists(vCardAdd[vi])){
2159 // Compare the ETag with the Server ETag.
2161 ConnHandle.SetServerFilename(vCardAdd[vi]);
2162 ConnHandle.GetServerETagValueThread();
2163 ETagServer = ConnHandle.ETagValueResult();
2165 if (ETagOriginal != ETagServer){
2167 // Server ETag is different from original ETag.
2168 // This is a conflict which the user will now
2171 frmMain *frmMainPtrGet = static_cast<frmMain*>(frmMainPtr);
2173 vCard34Conv vCard34Obj;
2175 wxString ServerContactData;
2177 vCard ConvertedV4Data;
2179 // Load the client data from the filename given.
2181 ConnHandle.GetServerContactData();
2182 ServerContactData = ConnHandle.GetPageData();
2184 // Process v3 version into v4 version.
2186 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
2188 vCardConflictObj vCardProcData;
2190 vCardProcData.vCardLocalData = &ClientData;
2191 vCardProcData.vCardServerData = &ConvertedV4Data;
2192 ClientData.LoadFile(FullFilename);
2194 //ContactConflictEvent event(ContactConflictCmdEnv);
2195 //event.SetData(ContactFilename, Moo1, Moo2);
2197 //wxPostEvent(frmMainPtrGet, event);
2199 // Setup Conflict Resolution Dialog.
2201 // Fetch Data from Server and convert.
2203 bool *PauseMode = new bool;
2208 qrn.QResponse = &ConflictResult;
2209 qrn.PausePtr = PauseMode;
2211 vCardProcData.QRNotifData = &qrn;
2213 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
2214 event.SetClientData(&vCardProcData);
2215 wxPostEvent(frmMainPtrGet, event);
2220 n1.tv_nsec = 250000000L;*/
2222 while (*PauseMode == TRUE){
2223 //nanosleep(&n1, &n2);
2224 SleepFor(250000000);
2230 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
2232 if (ConflictResult == 0){
2239 } else if (ConflictResult == 1){
2241 // Decided to use client version.
2243 ConnHandle.ProcessData();
2247 } else if (ConflictResult == 2){
2249 // Decided to use server version.
2251 // Download server version.
2253 ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
2261 // Setup the filename for the contact being added.
2263 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
2264 wxString wxSCardData;
2268 // Load the data from the file.
2271 wxFCard.Open(wxSFilename, wxT("r"));
2273 if (wxFCard.IsOpened()){
2275 wxFCard.ReadAll(&wxSCardData, wxConvAuto());
2280 // Convert the data to v3.
2282 vCard34Conv vCard34Obj;
2284 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
2286 // Setup the URL address.
2288 //TaskURL = StringTaskDetailIter->second + wxT("/") + vCardAdd[vi];
2290 // Upload the data to the server.
2292 ConnHandle.SetUploadMode(TRUE);
2293 ConnHandle.SetupData(wxT("PUT"), vCardAdd[vi], TaskData);
2294 ConnHandle.ProcessData();
2296 // Get the Server ETag and put it in the ETag database.
2298 ConnHandle.SetServerFilename(vCardAdd[vi]);
2299 ConnHandle.GetServerETagValueThread();
2300 ETagServer = ConnHandle.ETagValueResult();
2302 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
2308 if (ETagOriginal != ETagServer){
2310 // Server ETag is different from original ETag.
2311 // This is a conflict which the user will now
2314 frmMain *frmMainPtrGet = (frmMain *)frmMainPtr;
2316 //ContactConflictEvent event(ContactConflictCmdEnv);
2317 //event.SetData(ContactFilename, Moo1, Moo2);
2319 vCard34Conv vCard34Obj;
2321 wxString ServerContactData;
2323 vCard ConvertedV4Data;
2325 // Load the client data from the filename given.
2327 ConnHandle.GetServerContactData();
2328 ServerContactData = ConnHandle.GetPageData();
2329 ClientData.LoadFile(FullFilename);
2331 // Process v3 version into v4 version.
2333 vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data);
2335 vCardConflictObj vCardProcData;
2337 vCardProcData.vCardLocalData = &ClientData;
2338 vCardProcData.vCardServerData = &ConvertedV4Data;
2340 bool *PauseMode = new bool;
2345 qrn.QResponse = &ConflictResult;
2346 qrn.PausePtr = PauseMode;
2348 vCardProcData.QRNotifData = &qrn;
2350 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED);
2351 event.SetClientData(&vCardProcData);
2352 wxPostEvent(frmMainPtrGet, event);
2357 n1.tv_nsec = 250000000L;*/
2359 while (*PauseMode == TRUE){
2360 //nanosleep(&n1, &n2);
2361 SleepFor(250000000);
2367 //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2);
2369 if (ConflictResult == 0){
2376 } else if (ConflictResult == 1){
2378 // Decided to use client version.
2380 ConnHandle.ProcessData();
2381 //ETagServer = CardDAVIter->second.GetServerETagValueThread();
2382 //ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
2386 } else if (ConflictResult == 2){
2388 // Decided to use server version.
2390 // Write server version to file.
2392 ConvertedV4Data.WriteFile(CreateFilenamePath(AccountDirFinal, vCardAdd[vi]));
2400 // Setup the filename for the contact being added.
2402 wxString wxSFilename = CreateFilenamePath(AccountDirFinal, vCardAdd[vi]);
2403 wxString wxSCardData;
2407 // Load the data from the file.
2410 wxFCard.Open(wxSFilename, wxT("r"));
2412 if (wxFCard.IsOpened()){
2414 wxFCard.ReadAll(&wxSCardData, wxConvAuto());
2419 // Convert the data to v3.
2421 vCard34Conv vCard34Obj;
2423 vCard34Obj.ConvertToV3(wxSFilename, &TaskData);
2425 // Setup the URL address.
2427 //TaskURL = StringTaskDetailIter->second + wxT("/") + vCardAdd[vi];
2429 // Upload the data to the server.
2431 ConnHandle.SetUploadMode(TRUE);
2432 ConnHandle.SetupData(wxT("PUT"), vCardAdd[vi], TaskData);
2433 ConnHandle.ProcessData();
2435 // Get the Server ETag and put it in the ETag database.
2437 ConnHandle.SetServerFilename(vCardAdd[vi]);
2438 ConnHandle.GetServerETagValueThread();
2439 ETagServer = ConnHandle.ETagValueResult();
2442 ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer);
2450 // Process the vCards to delete locally.
2452 for (int vi = 0; vi < vCardDelete.GetCount(); vi++){
2454 if (!wxRemoveFile(CreateFilenamePath(AccountDirFinal, vCardDelete[vi]))){
2458 ETagDBPtr->RemoveETag(vCardDelete[vi]);
2464 // Process the vCards to delete from the server.
2466 std::map<wxString,wxString> *ETagFilenameMap = ETagDBPtr->GetFilenamePointer();
2467 std::list<wxString> DeleteList;
2469 for (std::map<wxString,wxString>::iterator deliter = ETagFilenameMap->begin();
2470 deliter != ETagFilenameMap->end(); deliter++){
2472 // Look through the ETagDB and find contacts which are marked as DELETED.
2474 if (deliter->second == wxT("DELETED")){
2476 ConnHandle.SetUploadMode(FALSE);
2477 ConnHandle.SetupData(wxT("DELETE"), deliter->first, wxT(""));
2478 ConnHandle.ProcessDataThread();
2480 // Delete the ETag as the file has now been deleted.
2482 DeleteList.push_back(deliter->first);
2488 for (std::list<wxString>::iterator removeetagiter = DeleteList.begin();
2489 removeetagiter != DeleteList.end(); removeetagiter++){
2491 ETagDBPtr->RemoveETag(*removeetagiter);
2495 // Write out the update server sync token.
2497 wxString ServerSyncToken = ServerContactList.SyncToken;
2499 ServerSyncToken.Trim();
2501 if (!ServerSyncToken.IsEmpty()){
2504 SyncWrite.Open(AccountSyncTokenFilename, wxT("w"));
2506 if (SyncWrite.IsOpened()){
2508 SyncWrite.Write(ServerSyncToken, wxConvAuto());
2515 // Check for another sync token. If there is another
2516 // sync token then repeat this. If token matches then stop processing.
2518 if (SyncTokenLoad == ServerSyncToken){
2520 KeepUpdating = FALSE;
2526 // Mark as completed.
2528 wxString AccountNameFinal;
2529 AccountNameFinal = PrefData.accounts.GetAccountDirectory(AccountID);
2530 AccountNameFinal.Append(wxT(".carddav"));
2532 wxCommandEvent reloadevent(RELOADCONTACTLIST);
2533 reloadevent.SetString(AccountNameFinal);
2534 wxPostEvent(this->GetParent(), reloadevent);
2541 // Update the data to the server.
2543 ConnHandle.ProcessData();
2552 n1.tv_nsec = 250000000L;*/
2554 while (iter->second == 1){
2556 SleepFor(125000000);
2557 //nanosleep(&n1, &n2);
2561 /*if ((iter->second == 4 && TypeIter->second == 0) ||
2562 (iter->second == 8 && TypeIter->second == 0)){
2564 AccountDir = StringAccountIter->second;
2565 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
2567 ConnHandle.GetServerETagValueThread();
2568 ETagServer = ConnHandle.ETagValueResult();
2569 ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
2573 if ((iter->second == 4 && TypeIter->second == 1) ||
2574 (iter->second == 8 && TypeIter->second == 1)){
2576 AccountDir = StringAccountIter->second;
2577 ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir);
2579 ConnHandle.GetServerETagValueThread();
2580 ETagServer = ConnHandle.ETagValueResult();
2581 ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer);
2591 if (TasksFoundProc == FALSE){
2593 TasksActive = FALSE;
2594 ProcessTasksData = FALSE;
2598 wxCommandEvent eventp(ACTMGR_STARTTIMER);
2599 wxPostEvent(this, eventp);
2601 //AnimationTimer.StartAnimation();
2602 //ActListProcTimer.Start();
2606 wxCommandEvent event(ACTMGR_STARTPROCESSTIMER);
2607 wxPostEvent(this, event);
2609 //ActProcessTimer.Start();
2613 void frmActivityMgr::ProcessTasks()
2616 std::thread ProcessThread(&frmActivityMgr::ProcessTasksThread, this);
2617 ProcessThread.detach();
2621 void frmActivityMgr::SetupPointers(wxETagProcessTimer *ETagTmrPtrInc, void *frmMainPtrInc)
2624 ETagTmrPtr = ETagTmrPtrInc;
2626 frmMainPtr = frmMainPtrInc;
2630 void frmActivityMgr::ResumeProcessing(wxCommandEvent &event){
2632 QRNotif *qrn = (QRNotif *)event.GetClientData();
2634 int *QRes = qrn->QResponse;
2636 *qrn->PausePtr = FALSE;
2638 *QRes = event.GetInt();
2642 void frmActivityMgr::ToggleConnectionStatus( wxCommandEvent &event ){
2644 wxCommandEvent connevent(CONNSTAT_UPDATE);
2646 if (ApplicationSleepMode == FALSE){
2648 connevent.SetInt(1);
2649 ApplicationSleepMode = TRUE;
2650 mnuSleepMode->Check(true);
2654 connevent.SetInt(0);
2655 ApplicationSleepMode = FALSE;
2656 mnuSleepMode->Check(false);
2658 wxCommandEvent event(ACTMGR_STARTTIMER);
2659 wxPostEvent(this, event);
2661 wxCommandEvent eventp(ACTMGR_STARTPROCESSTIMER);
2662 wxPostEvent(this, eventp);
2666 wxPostEvent(GetParent(), connevent);
2670 void frmActivityMgr::SleepMode( wxCommandEvent &event ){
2672 wxCommandEvent connevent(ACTMGR_TOGGLECONN);
2674 wxPostEvent(this, connevent);
2678 void frmActivityMgr::WriteServerCertificate(wxString AccountName, SSLCertCollection SSLCertInc){
2680 wxString ServerCertFinal;
2682 // Get the Cert section of the certificate and write it to the file.
2684 ServerCertFinal = GetAccountDir(AccountName, TRUE);
2688 std::map<int, SSLCertData>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
2689 std::multimap<wxString,wxString>::iterator SSLDataIter = SSLCDIter->second.CertData.find(wxT("Cert"));
2691 CertFile.Open(ServerCertFinal, wxFile::write);
2693 CertFile.Write(SSLDataIter->second, wxConvUTF8);
2698 void frmActivityMgr::WriteServerCertificate(wxString AccountName, SSLCertCollectionString SSLCertInc){
2700 wxString ServerCertFinal;
2702 // Get the Cert section of the certificate and write it to the file.
2704 ServerCertFinal = GetAccountDir(AccountName, TRUE);
2708 std::map<int, SSLCertDataString>::iterator SSLCDIter = SSLCertInc.SSLCollection.find(0);
2709 std::multimap<string,string>::iterator SSLDataIter = SSLCDIter->second.CertData.find("Cert");
2711 CertFile.Open(ServerCertFinal, wxFile::write);
2713 CertFile.Write(SSLDataIter->second, wxConvUTF8);
2718 void frmActivityMgr::StartTimer(wxCommandEvent& event){
2720 ActListProcTimer.Start(1000, FALSE);
2724 void frmActivityMgr::StopTimer(wxCommandEvent& event){
2726 ActListProcTimer.Stop();
2730 void frmActivityMgr::StartProcessTimer(wxCommandEvent& event){
2732 ActProcessTimer.Start(1000, FALSE);
2736 void frmActivityMgr::StopProcessTimer(wxCommandEvent& event){
2738 ActProcessTimer.Stop();
2742 void frmActivityMgr::StartAnimationTimer(wxCommandEvent& event){
2744 AnimationTimer.StartAnimation();
2748 void frmActivityMgr::UpdateStatusLabel(wxCommandEvent& event){
2750 ActivityMgrLabelUpdate *actmgrudata = (ActivityMgrLabelUpdate*)event.GetClientData();
2752 if (actmgrudata != NULL){
2754 lstActivity->SetItem(actmgrudata->ItemIndex, 2, actmgrudata->ItemLabel);
2756 /*if (!actmgrudata->ItemDes.IsEmpty()){
2757 lstActivity->SetItem(actmgrudata->ItemIndex, 0, actmgrudata->ItemDes);