From 049b4010cd6ff8663503ab22f1c51f72a3c3af8f Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Mon, 11 Apr 2016 21:54:58 +0100 Subject: [PATCH] Altered frmActivityMgr to accommodate for SSL support for OS X (and other OSes in the future). --- source/actmgr/frmActivityMgr-tasks.cpp | 23 +- source/actmgr/frmActivityMgr.cpp | 884 +++++++++++++++++++------ source/actmgr/frmActivityMgr.h | 2 + 3 files changed, 690 insertions(+), 219 deletions(-) diff --git a/source/actmgr/frmActivityMgr-tasks.cpp b/source/actmgr/frmActivityMgr-tasks.cpp index fe6eb3d..8aa7d1e 100644 --- a/source/actmgr/frmActivityMgr-tasks.cpp +++ b/source/actmgr/frmActivityMgr-tasks.cpp @@ -79,6 +79,15 @@ int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAcco } + ActivityMgrAccountSettings AccountSettings; + AccountSettings.Address = AccountAddress; + AccountSettings.Port = AccountPort; + AccountSettings.Username = AccountUsername; + AccountSettings.Password = AccountPassword; + AccountSettings.SSL = AccountSSL; + AccountSettings.Prefix = AccountPrefix; + AccountSettings.Dir = AccountDir; + CardDAVConn->SetupConnection(AccountAddress, AccountPort, AccountUsername, AccountPassword, AccountSSL, AccountPrefix, AccountDir); CardDAVConn->SetupVariables(&ActivityList, ActivityTaskID); CardDAVConn->SetupResultBools(&BlankBool, &BlankBool); @@ -106,12 +115,15 @@ int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAcco ActivityListType.insert(std::make_pair(ActivityTaskID, 0)); ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData)); + ActivityListURL.insert(std::make_pair(ActivityTaskID, TaskURL)); + ActivityListEditMode.insert(std::make_pair(ActivityTaskID, false)); ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn)); ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex)); ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail)); ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount)); ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename)); ActivityListFullFilename.insert(std::make_pair(ActivityTaskID, TaskFullFilename)); + ActivityListAccSettings.insert(std::make_pair(ActivityTaskID, AccountSettings)); ActivityListETag.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETag(TaskFilename))); ActivityListETagOriginal.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETagOriginal(TaskFilename))); @@ -125,20 +137,18 @@ int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAcco lstActivity->SetItem(itemindex, 1, AccountName); lstActivity->SetItem(itemindex, 2, _("Queued")); - CardDAVConn->SetUploadMode(TRUE); - CardDAVConn->SetupData(wxT("PUT"), TaskURL, TaskData); - CardDAVConn->SetEditMode(TRUE); - ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir); ActivityListType.insert(std::make_pair(ActivityTaskID, 1)); ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData)); - ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn)); + ActivityListURL.insert(std::make_pair(ActivityTaskID, TaskURL)); + ActivityListEditMode.insert(std::make_pair(ActivityTaskID, true)); ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex)); ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, TaskDetail)); ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount)); ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename)); ActivityListFullFilename.insert(std::make_pair(ActivityTaskID, TaskFullFilename)); + ActivityListAccSettings.insert(std::make_pair(ActivityTaskID, AccountSettings)); ActivityListETag.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETag(TaskFilename))); ActivityListETagOriginal.insert(std::make_pair(ActivityTaskID, ETagDBPtr->GetETagOriginal(TaskFilename))); @@ -158,9 +168,9 @@ int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAcco ActivityListType.insert(std::make_pair(ActivityTaskID, 2)); ActivityListData.insert(std::make_pair(ActivityTaskID, TaskData)); ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount)); - ActivityListConn.insert(std::make_pair(ActivityTaskID, CardDAVConn)); ActivityListFilename.insert(std::make_pair(ActivityTaskID, TaskFilename)); ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex)); + ActivityListAccSettings.insert(std::make_pair(ActivityTaskID, AccountSettings)); } else if (TaskType == 3){ @@ -177,6 +187,7 @@ int frmActivityMgr::AddTask(int TaskType, wxString TaskDetail, wxString TaskAcco ActivityListTaskDetail.insert(std::make_pair(ActivityTaskID, AccountPrefix)); ActivityListAccount.insert(std::make_pair(ActivityTaskID, TaskAccount)); ActivityListIndex.insert(std::make_pair(ActivityTaskID, itemindex)); + ActivityListAccSettings.insert(std::make_pair(ActivityTaskID, AccountSettings)); } diff --git a/source/actmgr/frmActivityMgr.cpp b/source/actmgr/frmActivityMgr.cpp index aeb28cb..ebb425c 100644 --- a/source/actmgr/frmActivityMgr.cpp +++ b/source/actmgr/frmActivityMgr.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "../bitmaps.h" @@ -198,6 +199,8 @@ void frmActivityMgr::ProcessTasksThread() std::map::iterator TypeIter; std::map::iterator StringIter; + std::map::iterator StringDataIter; + std::map::iterator StringURLIter; std::map::iterator StringDetailIter; std::map::iterator StringAccountIter; std::map::iterator StringFilenameIter; @@ -206,10 +209,11 @@ void frmActivityMgr::ProcessTasksThread() std::map::iterator StringETagIter; std::map::iterator StringETagOrigIter; std::map::iterator CardDAVIter; + std::map::iterator AccountSettingsIter; std::map::iterator LongIter; bool TasksFoundProc = FALSE; wxString TaskDetail; - + for (std::map::iterator iter = ActivityList.begin(); iter != ActivityList.end(); ++iter){ @@ -226,6 +230,21 @@ void frmActivityMgr::ProcessTasksThread() StringAccountIter = ActivityListAccount.find(iter->first); StringFilenameIter = ActivityListFilename.find(iter->first); StringFullFilenameIter = ActivityListFullFilename.find(iter->first); + AccountSettingsIter = ActivityListAccSettings.find(iter->first); + + // Setup the CardDAV object. + + CardDAV ConnHandle; + + ConnHandle.SetupConnection(AccountSettingsIter->second.Address, + AccountSettingsIter->second.Port, + AccountSettingsIter->second.Username, + AccountSettingsIter->second.Password, + AccountSettingsIter->second.SSL, + AccountSettingsIter->second.Prefix, + AccountSettingsIter->second.Dir); + ConnHandle.SetupVariables(&ActivityList, iter->first); + ConnHandle.SetupResultBools(&BlankBool, &BlankBool); // 0 = Queued. // 1 = Processing. @@ -237,6 +256,9 @@ void frmActivityMgr::ProcessTasksThread() // 7 = (Reserved) (UI Updated). // 8 = Completed. (UI Updated). + frmMain *frmMainPtrGet = static_cast(frmMainPtr); + XABPreferences PrefData(GetUserPrefDir()); + if (iter->second == 0){ //CardDAVIter->second. @@ -252,240 +274,474 @@ void frmActivityMgr::ProcessTasksThread() wxString ContactFilename; wxString FullFilename; wxString ETagResult; + bool EditMode = FALSE; iter->second = 1; - if (TypeIter->second == 1){ - CardDAVIter->second->GetServerETagValueThread(); - ETagServer = CardDAVIter->second->ETagValueResult(); - } - // Compare the ETags and work out if a conflict has occured. - if (TypeIter->second == 1){ - - ETag = StringETagIter->second; - ETagOriginal = StringETagOrigIter->second; + if (TypeIter->second == 0){ + + // Add a contact. + + StringDataIter = ActivityListData.find(iter->first); + StringURLIter = ActivityListURL.find(iter->first); + AccountDir = StringAccountIter->second; ContactFilename = StringFilenameIter->second; - FullFilename = StringFullFilenameIter->second; - - ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir); - - /*if (ETag == ETagOriginal){*/ - - // Compare the ETag with the Server ETag. + wxString AccountNameFriendly; - if (ETagOriginal != ETagServer){ + int AccountID = 0; - // Server ETag is different from original ETag. - // This is a conflict which the user will now - // need to resolve. + for (int i = 0; i < PrefData.accounts.GetCount(); i++){ - vCard Moo1; - vCard Moo2; + wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i); - frmMain *frmMainPtrGet = static_cast(frmMainPtr); + AccountDirCheck.Trim(); + AccountDirCheck.Append(wxT(".carddav")); - vCard34Conv vCard34Obj; + if (AccountDirCheck == StringAccountIter->second){ - wxString ServerContactData; - vCard ClientData; - vCard ConvertedV4Data; + AccountNameFriendly = PrefData.accounts.GetAccountName(i); + AccountID = i; + + } - // Load the client data from the filename given. + } + + ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir); + + ETag = StringETagIter->second; + ETagOriginal = StringETagOrigIter->second; + AccountDir = StringAccountIter->second; + ContactFilename = StringFilenameIter->second; + FullFilename = StringFullFilenameIter->second; + EditMode = ActivityListEditMode.find(iter->first)->second; + + bool KeepUpdating = TRUE; + + while(KeepUpdating == TRUE){ + + bool ExitLoop = FALSE; - CardDAVIter->second->GetServerContactData(); - ServerContactData = CardDAVIter->second->GetPageData(); + while (ExitLoop == FALSE){ - // Process v3 version into v4 version. + ConnHandle.SetUploadMode(TRUE); + ConnHandle.SetupData(wxT("PUT"), StringURLIter->second, StringDataIter->second); + ConnHandle.SetEditMode(EditMode); + ConnHandle.ProcessData(); - vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data); - - vCardConflictObj vCardProcData; - - vCardProcData.vCardLocalData = &ClientData; - vCardProcData.vCardServerData = &ConvertedV4Data; - ClientData.LoadFile(FullFilename); - - //ContactConflictEvent event(ContactConflictCmdEnv); - //event.SetData(ContactFilename, Moo1, Moo2); + int ErrorCode = ConnHandle.GetResultCode(); + + if (ErrorCode != 0){ + + if (ErrorCode == 60 || ErrorCode == 51){ + + // Invalid SSL certificate so bring up a dialog to the user + // explaining what has happened and the options available. + + // Stop all the timers. + + frmMainPtrGet->PauseAllTimers(); + + int SSLResult; + +#if defined(__APPLE__) + + // Bring up the trust panel and display the SSL certificate dialog. + + SSLResult = DisplayTrustPanel(ConnHandle.GetTrustObject(), AccountNameFriendly); + + // Update the SSL account with the trust object. + + if (SSLResult != NSOKButton){ + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetTrustObject()); + iter->second = 2; + break; + + } else { + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject()); + + } +#else + + // Setup the data to be sent in the wxPostEvent command. + + SSLInvalidCertNotifObj SSLICNProcData; + + bool *PauseMode = new bool; + QRNotif qrn; + + *PauseMode = TRUE; + qrn.QResponse = &SSLResult; + qrn.PausePtr = PauseMode; + + SSLICNProcData.CertCollection = certcol; + SSLICNProcData.QRNotifData = &qrn; + SSLICNProcData.AccountName = AccountNameFriendly; + + wxCommandEvent event(INVALIDSSLCERT); + event.SetClientData(&SSLICNProcData); + wxPostEvent(frmMainPtrGet, event); + + /*timespec n1, n2; + + // Fall asleep until we get an response. + + n1.tv_sec = 0; + n1.tv_nsec = 250000000L;*/ + + while (*PauseMode == TRUE){ + //nanosleep(&n1, &n2); + SleepFor(250000000); + } + + // Process the response from the user. + + if (SSLResult == 1){ + + // Accept the certificate. Write the certificate into the account + // directory (as server.crt) - will overwrite old certificate. + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData()); + WriteServerCertificate(AccountDir, ConnHandle.GetCertificateData()); + + } else if (SSLResult == 2){ + + // Reject the certificate, abort the task and mark as failed. + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateData()); + iter->second = 2; + break; + + } + +#endif + + // Resume all the timers. + + frmMainPtrGet->ResumeAllTimers(); + ExitLoop = TRUE; + + } else { + + iter->second = 2; + break; + + } + + } else { + + ExitLoop = TRUE; + + } + + } - //wxPostEvent(frmMainPtrGet, event); + if (iter->second == 2 || iter->second == 3 || iter->second == 4){ + break; + } - // Setup Conflict Resolution Dialog. +#if defined(__APPLE__) - // Fetch Data from Server and convert. + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject()); - bool *PauseMode = new bool; - int ConflictResult; - QRNotif qrn; - - *PauseMode = TRUE; - qrn.QResponse = &ConflictResult; - qrn.PausePtr = PauseMode; - - vCardProcData.QRNotifData = &qrn; +#else - wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED); - event.SetClientData(&vCardProcData); - wxPostEvent(frmMainPtrGet, event); - - /*timespec n1, n2; - - n1.tv_sec = 0; - n1.tv_nsec = 250000000L;*/ - - while (*PauseMode == TRUE){ - //nanosleep(&n1, &n2); - SleepFor(250000000); - } + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData()); - delete PauseMode; - PauseMode = NULL; +#endif - //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2); + // Add contact to the ETag DB. - if (ConflictResult == 0){ + ConnHandle.GetServerETagValueThread(); + ETagServer = ConnHandle.ETagValueResult(); + ETagDBPtr->AddETag(ContactFilename, ETagServer, ETagServer); - // Decide Later. + iter->second = 4; + CardDAVIter->second = NULL; + break; - iter->second = 4; - continue; + } + + } else if (TypeIter->second == 1){ + + // Update a contact. + + StringDataIter = ActivityListData.find(iter->first); + StringURLIter = ActivityListURL.find(iter->first); + + AccountDir = StringAccountIter->second; + ContactFilename = StringFilenameIter->second; + wxString AccountNameFriendly; + + int AccountID = 0; + + for (int i = 0; i < PrefData.accounts.GetCount(); i++){ - } else if (ConflictResult == 1){ + wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i); - // Decided to use client version. + AccountDirCheck.Trim(); + AccountDirCheck.Append(wxT(".carddav")); - CardDAVIter->second->ProcessData(); + if (AccountDirCheck == StringAccountIter->second){ - iter->second = 4; + AccountNameFriendly = PrefData.accounts.GetAccountName(i); + AccountID = i; + + } - } else if (ConflictResult == 2){ + } + + ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir); + + ETag = StringETagIter->second; + ETagOriginal = StringETagOrigIter->second; + AccountDir = StringAccountIter->second; + ContactFilename = StringFilenameIter->second; + FullFilename = StringFullFilenameIter->second; + EditMode = ActivityListEditMode.find(iter->first)->second; + + bool KeepUpdating = TRUE; + + while(KeepUpdating == TRUE){ - // Decided to use server version. + bool ExitLoop = FALSE; + + while (ExitLoop == FALSE){ - // Download server version. + ConnHandle.SetUploadMode(TRUE); + ConnHandle.SetupData(wxT("PUT"), StringURLIter->second, StringDataIter->second); + ConnHandle.SetEditMode(EditMode); + ConnHandle.GetServerETagValueThread(); + ETagServer = ConnHandle.ETagValueResult(); - ConvertedV4Data.WriteFile(FullFilename); + int ErrorCode = ConnHandle.GetResultCode(); - iter->second = 4; + if (ErrorCode != 0){ + + if (ErrorCode == 60 || ErrorCode == 51){ + + // Invalid SSL certificate so bring up a dialog to the user + // explaining what has happened and the options available. + + // Stop all the timers. + + frmMainPtrGet->PauseAllTimers(); + + int SSLResult; + +#if defined(__APPLE__) + + // Bring up the trust panel and display the SSL certificate dialog. + + SSLResult = DisplayTrustPanel(ConnHandle.GetTrustObject(), AccountNameFriendly); + + // Update the SSL account with the trust object. + + if (SSLResult != NSOKButton){ + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetTrustObject()); + iter->second = 2; + break; + + } else { + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject()); + + } +#else + + // Setup the data to be sent in the wxPostEvent command. + + SSLInvalidCertNotifObj SSLICNProcData; + + bool *PauseMode = new bool; + QRNotif qrn; + + *PauseMode = TRUE; + qrn.QResponse = &SSLResult; + qrn.PausePtr = PauseMode; + + SSLICNProcData.CertCollection = certcol; + SSLICNProcData.QRNotifData = &qrn; + SSLICNProcData.AccountName = AccountNameFriendly; + + wxCommandEvent event(INVALIDSSLCERT); + event.SetClientData(&SSLICNProcData); + wxPostEvent(frmMainPtrGet, event); + + while (*PauseMode == TRUE){ + //nanosleep(&n1, &n2); + SleepFor(250000000); + } + + // Process the response from the user. + + if (SSLResult == 1){ + + // Accept the certificate. Write the certificate into the account + // directory (as server.crt) - will overwrite old certificate. + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData()); + WriteServerCertificate(AccountDir, ConnHandle.GetCertificateData()); + + } else if (SSLResult == 2){ + + // Reject the certificate, abort the task and mark as failed. + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateData()); + iter->second = 2; + break; + + } + +#endif + + // Resume all the timers. + + frmMainPtrGet->ResumeAllTimers(); + ExitLoop = TRUE; + + } else { + + iter->second = 2; + break; + + } + + } else { + + ExitLoop = TRUE; + + } + + } + + if (iter->second == 2 || iter->second == 3 || iter->second == 4){ + break; + } + +#if defined(__APPLE__) + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject()); + +#else - } - - } else { - - // Update the data to the server. + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData()); + +#endif + + // Compare the ETag with the Server ETag. - CardDAVIter->second->ProcessData(); - - } - - /*} else { - if (ETagOriginal != ETagServer){ - + // Server ETag is different from original ETag. // This is a conflict which the user will now // need to resolve. - - frmMain *frmMainPtrGet = (frmMain *)frmMainPtr; - - //ContactConflictEvent event(ContactConflictCmdEnv); - //event.SetData(ContactFilename, Moo1, Moo2); - + + vCard Moo1; + vCard Moo2; + + frmMain *frmMainPtrGet = static_cast(frmMainPtr); + vCard34Conv vCard34Obj; wxString ServerContactData; vCard ClientData; vCard ConvertedV4Data; - + // Load the client data from the filename given. - - CardDAVIter->second->GetServerContactData(); - ServerContactData = CardDAVIter->second->GetPageData(); - ClientData.LoadFile(FullFilename); + + ConnHandle.GetServerContactData(); + ServerContactData = ConnHandle.GetPageData(); // Process v3 version into v4 version. vCard34Obj.ConvertToV4(&ServerContactData, &ConvertedV4Data); - + vCardConflictObj vCardProcData; - + vCardProcData.vCardLocalData = &ClientData; vCardProcData.vCardServerData = &ConvertedV4Data; - + ClientData.LoadFile(FullFilename); + + //ContactConflictEvent event(ContactConflictCmdEnv); + //event.SetData(ContactFilename, Moo1, Moo2); + + //wxPostEvent(frmMainPtrGet, event); + + // Setup Conflict Resolution Dialog. + + // Fetch Data from Server and convert. + bool *PauseMode = new bool; int ConflictResult; QRNotif qrn; - - *PauseMode = TRUE; + + *PauseMode = TRUE; qrn.QResponse = &ConflictResult; qrn.PausePtr = PauseMode; vCardProcData.QRNotifData = &qrn; - + wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED); event.SetClientData(&vCardProcData); wxPostEvent(frmMainPtrGet, event); - + while (*PauseMode == TRUE){ //nanosleep(&n1, &n2); SleepFor(250000000); } - + delete PauseMode; PauseMode = NULL; - + //intResult = frmMainPtrGet->ShowConflictDialog(&Moo1, &Moo2); - + if (ConflictResult == 0){ - + // Decide Later. - + iter->second = 4; - continue; - + break; + } else if (ConflictResult == 1){ - + // Decided to use client version. - - CardDAVIter->second->ProcessData(); - //ETagServer = CardDAVIter->second.GetServerETagValueThread(); - //ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer); - - iter->second = 4; - + + ConnHandle.ProcessData(); + } else if (ConflictResult == 2){ - + // Decided to use server version. - - // Write server version to file. - + + // Download server version. + ConvertedV4Data.WriteFile(FullFilename); - - iter->second = 4; - + } - + } else { - + // Update the data to the server. - - CardDAVIter->second->ProcessData(); - + + ConnHandle.ProcessData(); + } - - }*/ - - // Update the ETag DB. - - CardDAVIter->second->GetServerETagValueThread(); - ETagServer = CardDAVIter->second->ETagValueResult(); - ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer); - - iter->second = 4; - CardDAVIter->second = NULL; + + // Update the ETag DB. + + ConnHandle.GetServerETagValueThread(); + ETagServer = ConnHandle.ETagValueResult(); + ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer); + + iter->second = 4; + break; + + } } else if (TypeIter->second == 2) { @@ -493,26 +749,179 @@ void frmActivityMgr::ProcessTasksThread() AccountDir = StringAccountIter->second; ContactFilename = StringFilenameIter->second; - + wxString AccountNameFriendly; + + int AccountID = 0; + + for (int i = 0; i < PrefData.accounts.GetCount(); i++){ + + wxString AccountDirCheck = PrefData.accounts.GetAccountDirectory(i); + + AccountDirCheck.Trim(); + AccountDirCheck.Append(wxT(".carddav")); + + if (AccountDirCheck == StringAccountIter->second){ + + AccountNameFriendly = PrefData.accounts.GetAccountName(i); + AccountID = i; + + } + + } + ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir); - CardDAVIter->second->SetUploadMode(FALSE); - CardDAVIter->second->SetupData(wxT("DELETE"), ContactFilename, wxT("")); - //CardDAVIter->second.SetServerFilename(); - CardDAVIter->second->ProcessData(); - ETagDBPtr->RemoveETag(ContactFilename); + bool KeepUpdating = TRUE; - iter->second = 4; - CardDAVIter->second = NULL; + while(KeepUpdating == TRUE){ + + bool ExitLoop = FALSE; + + while (ExitLoop == FALSE){ + + ConnHandle.SetUploadMode(FALSE); + ConnHandle.SetupData(wxT("DELETE"), ContactFilename, wxT("")); + ConnHandle.ProcessData(); + SSLCertCollection certcol = ConnHandle.GetCertificateData(); + + // Check if any errors have occured whilst getting the data. + + int ErrorCode = ConnHandle.GetResultCode(); + + if (ErrorCode != 0){ + + if (ErrorCode == 60 || ErrorCode == 51){ + + // Invalid SSL certificate so bring up a dialog to the user + // explaining what has happened and the options available. + + // Stop all the timers. + + frmMainPtrGet->PauseAllTimers(); + + int SSLResult; + +#if defined(__APPLE__) + + // Bring up the trust panel and display the SSL certificate dialog. + + SSLResult = DisplayTrustPanel(ConnHandle.GetTrustObject(), AccountNameFriendly); + + // Update the SSL account with the trust object. + + if (SSLResult != NSOKButton){ + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetTrustObject()); + iter->second = 2; + break; + + } else { + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject()); + + } +#else + + // Setup the data to be sent in the wxPostEvent command. + + SSLInvalidCertNotifObj SSLICNProcData; + + bool *PauseMode = new bool; + QRNotif qrn; + + *PauseMode = TRUE; + qrn.QResponse = &SSLResult; + qrn.PausePtr = PauseMode; + + SSLICNProcData.CertCollection = certcol; + SSLICNProcData.QRNotifData = &qrn; + SSLICNProcData.AccountName = AccountNameFriendly; + + wxCommandEvent event(INVALIDSSLCERT); + event.SetClientData(&SSLICNProcData); + wxPostEvent(frmMainPtrGet, event); + + /*timespec n1, n2; + + // Fall asleep until we get an response. + + n1.tv_sec = 0; + n1.tv_nsec = 250000000L;*/ + + while (*PauseMode == TRUE){ + //nanosleep(&n1, &n2); + SleepFor(250000000); + } + + // Process the response from the user. + + if (SSLResult == 1){ + + // Accept the certificate. Write the certificate into the account + // directory (as server.crt) - will overwrite old certificate. + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData()); + WriteServerCertificate(AccountDir, ConnHandle.GetCertificateData()); + + } else if (SSLResult == 2){ + + // Reject the certificate, abort the task and mark as failed. + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateData()); + iter->second = 2; + break; + + } + +#endif + + // Resume all the timers. + + frmMainPtrGet->ResumeAllTimers(); + ExitLoop = TRUE; + + } else { + + iter->second = 4; + break; + + } + + } else { + + ExitLoop = TRUE; + + } + + } + + if (iter->second == 2 || iter->second == 3 || iter->second == 4){ + break; + } - } else if (TypeIter->second == 3) { +#if defined(__APPLE__) + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject()); + +#else + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData()); + +#endif + + + ETagDBPtr->RemoveETag(ContactFilename); + + iter->second = 4; + CardDAVIter->second = NULL; + break; + + } - frmMain *frmMainPtrGet = static_cast(frmMainPtr); + } else if (TypeIter->second == 3) { wxString AccountNameFriendly; - wxString PrefFilename = GetUserPrefDir(); - XABPreferences PrefData(PrefFilename); int AccountID = 0; for (int i = 0; i < PrefData.accounts.GetCount(); i++){ @@ -604,8 +1013,8 @@ void frmActivityMgr::ProcessTasksThread() while (ExitLoop == FALSE){ - ServerContactList = CardDAVIter->second->GetContactList(SyncTokenLoad.ToUTF8()); - SSLCertCollection certcol = CardDAVIter->second->GetCertificateData(); + ServerContactList = ConnHandle.GetContactList(SyncTokenLoad.ToUTF8()); + SSLCertCollection certcol = ConnHandle.GetCertificateData(); // Create a pointer for the std::map. @@ -613,7 +1022,7 @@ void frmActivityMgr::ProcessTasksThread() // Check if any errors have occured whilst getting the data. - int ErrorCode = CardDAVIter->second->GetResultCode(); + int ErrorCode = ConnHandle.GetResultCode(); if (ErrorCode != 0){ @@ -626,12 +1035,34 @@ void frmActivityMgr::ProcessTasksThread() frmMainPtrGet->PauseAllTimers(); + int SSLResult; + +#if defined(__APPLE__) + + // Bring up the trust panel and display the SSL certificate dialog. + + SSLResult = DisplayTrustPanel(ConnHandle.GetTrustObject(), AccountNameFriendly); + + // Update the SSL account with the trust object. + + if (SSLResult != NSOKButton){ + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetTrustObject()); + iter->second = 2; + break; + + } else { + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject()); + + } +#else + // Setup the data to be sent in the wxPostEvent command. SSLInvalidCertNotifObj SSLICNProcData; bool *PauseMode = new bool; - int SSLResult; QRNotif qrn; *PauseMode = TRUE; @@ -665,22 +1096,25 @@ void frmActivityMgr::ProcessTasksThread() // Accept the certificate. Write the certificate into the account // directory (as server.crt) - will overwrite old certificate. - frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, CardDAVIter->second->GetCertificateData()); - WriteServerCertificate(AccountDir, CardDAVIter->second->GetCertificateData()); + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData()); + WriteServerCertificate(AccountDir, ConnHandle.GetCertificateData()); } else if (SSLResult == 2){ // Reject the certificate, abort the task and mark as failed. - frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, CardDAVIter->second->GetCertificateData()); + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 1, ConnHandle.GetCertificateData()); iter->second = 2; break; } + +#endif // Resume all the timers. frmMainPtrGet->ResumeAllTimers(); + ExitLoop = TRUE; } else { @@ -701,8 +1135,16 @@ void frmActivityMgr::ProcessTasksThread() break; } - frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, CardDAVIter->second->GetCertificateData()); - +#if defined(__APPLE__) + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetTrustObject()); + +#else + + frmMainPtrGet->UpdateSSLAccountStatus(AccountID, 0, ConnHandle.GetCertificateData()); + +#endif + // Look in directory and see which vCards to add and delete // In short: @@ -793,6 +1235,15 @@ void frmActivityMgr::ProcessTasksThread() wxString ETagSrv = fsd.ETagData; + // Check if the local ETag value has 'DELETED' set and skip + // checking this file if it does. + + if (etd.ETagValue == "DELETED"){ + + continue; + + } + // Compare local ETag with original local ETag. // If different, update to server is required. @@ -814,9 +1265,9 @@ void frmActivityMgr::ProcessTasksThread() // Get the server contact data for comparison. - CardDAVIter->second->SetServerFilename(srviter->first); - CardDAVIter->second->GetServerContactData(); - ServerContactData = CardDAVIter->second->GetPageData(); + ConnHandle.SetServerFilename(srviter->first); + ConnHandle.GetServerContactData(); + ServerContactData = ConnHandle.GetPageData(); FullFilename = CreateFilenamePath(AccountDirFinal, srviter->first); @@ -950,9 +1401,9 @@ void frmActivityMgr::ProcessTasksThread() vCard34Conv vCard34Obj; - CardDAVIter->second->SetServerFilename(additer->first); - CardDAVIter->second->GetServerContactData(); - vCardInputData = CardDAVIter->second->GetPageData(); + ConnHandle.SetServerFilename(additer->first); + ConnHandle.GetServerContactData(); + vCardInputData = ConnHandle.GetPageData(); vCard34Obj.ConvertToV4(&vCardInputData, &ConvertedV4Data); @@ -972,8 +1423,8 @@ void frmActivityMgr::ProcessTasksThread() // Get the server ETag. wxString ServerETag; - CardDAVIter->second->GetServerETagValueThread(); - ServerETag = CardDAVIter->second->ETagValueResult(); + ConnHandle.GetServerETagValueThread(); + ServerETag = ConnHandle.ETagValueResult(); // Add to the ETag Database. @@ -998,8 +1449,8 @@ void frmActivityMgr::ProcessTasksThread() // Update the ETagDB. wxString ServerETag; - CardDAVIter->second->GetServerETagValueThread(); - ServerETag = CardDAVIter->second->ETagValueResult(); + ConnHandle.GetServerETagValueThread(); + ServerETag = ConnHandle.ETagValueResult(); // Add to the ETag Database. @@ -1063,9 +1514,9 @@ void frmActivityMgr::ProcessTasksThread() // Compare the ETag with the Server ETag. - CardDAVIter->second->SetServerFilename(vCardAdd[vi]); - CardDAVIter->second->GetServerETagValueThread(); - ETagServer = CardDAVIter->second->ETagValueResult(); + ConnHandle.SetServerFilename(vCardAdd[vi]); + ConnHandle.GetServerETagValueThread(); + ETagServer = ConnHandle.ETagValueResult(); if (ETagOriginal != ETagServer){ @@ -1083,8 +1534,8 @@ void frmActivityMgr::ProcessTasksThread() // Load the client data from the filename given. - CardDAVIter->second->GetServerContactData(); - ServerContactData = CardDAVIter->second->GetPageData(); + ConnHandle.GetServerContactData(); + ServerContactData = ConnHandle.GetPageData(); // Process v3 version into v4 version. @@ -1145,7 +1596,7 @@ void frmActivityMgr::ProcessTasksThread() // Decided to use client version. - CardDAVIter->second->ProcessData(); + ConnHandle.ProcessData(); iter->second = 4; @@ -1194,15 +1645,15 @@ void frmActivityMgr::ProcessTasksThread() // Upload the data to the server. - CardDAVIter->second->SetUploadMode(TRUE); - CardDAVIter->second->SetupData(wxT("PUT"), vCardAdd[vi], TaskData); - CardDAVIter->second->ProcessData(); + ConnHandle.SetUploadMode(TRUE); + ConnHandle.SetupData(wxT("PUT"), vCardAdd[vi], TaskData); + ConnHandle.ProcessData(); // Get the Server ETag and put it in the ETag database. - CardDAVIter->second->SetServerFilename(vCardAdd[vi]); - CardDAVIter->second->GetServerETagValueThread(); - ETagServer = CardDAVIter->second->ETagValueResult(); + ConnHandle.SetServerFilename(vCardAdd[vi]); + ConnHandle.GetServerETagValueThread(); + ETagServer = ConnHandle.ETagValueResult(); ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer); @@ -1229,8 +1680,8 @@ void frmActivityMgr::ProcessTasksThread() // Load the client data from the filename given. - CardDAVIter->second->GetServerContactData(); - ServerContactData = CardDAVIter->second->GetPageData(); + ConnHandle.GetServerContactData(); + ServerContactData = ConnHandle.GetPageData(); ClientData.LoadFile(FullFilename); // Process v3 version into v4 version. @@ -1282,7 +1733,7 @@ void frmActivityMgr::ProcessTasksThread() // Decided to use client version. - CardDAVIter->second->ProcessData(); + ConnHandle.ProcessData(); //ETagServer = CardDAVIter->second.GetServerETagValueThread(); //ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer); @@ -1333,15 +1784,15 @@ void frmActivityMgr::ProcessTasksThread() // Upload the data to the server. - CardDAVIter->second->SetUploadMode(TRUE); - CardDAVIter->second->SetupData(wxT("PUT"), vCardAdd[vi], TaskData); - CardDAVIter->second->ProcessData(); + ConnHandle.SetUploadMode(TRUE); + ConnHandle.SetupData(wxT("PUT"), vCardAdd[vi], TaskData); + ConnHandle.ProcessData(); // Get the Server ETag and put it in the ETag database. - CardDAVIter->second->SetServerFilename(vCardAdd[vi]); - CardDAVIter->second->GetServerETagValueThread(); - ETagServer = CardDAVIter->second->ETagValueResult(); + ConnHandle.SetServerFilename(vCardAdd[vi]); + ConnHandle.GetServerETagValueThread(); + ETagServer = ConnHandle.ETagValueResult(); ETagDBPtr->AddETag(vCardAdd[vi], ETagServer, ETagServer); @@ -1369,7 +1820,7 @@ void frmActivityMgr::ProcessTasksThread() // Process the vCards to delete from the server. std::map *ETagFilenameMap = ETagDBPtr->GetFilenamePointer(); - + std::list DeleteList; for (std::map::iterator deliter = ETagFilenameMap->begin(); deliter != ETagFilenameMap->end(); deliter++){ @@ -1378,17 +1829,24 @@ void frmActivityMgr::ProcessTasksThread() if (deliter->second == wxT("DELETED")){ - CardDAVIter->second->SetUploadMode(FALSE); - CardDAVIter->second->SetupData(wxT("DELETE"), deliter->first, wxT("")); - CardDAVIter->second->ProcessData(); + ConnHandle.SetUploadMode(FALSE); + ConnHandle.SetupData(wxT("DELETE"), deliter->first, wxT("")); + ConnHandle.ProcessDataThread(); // Delete the ETag as the file has now been deleted. - ETagDBPtr->RemoveETag(deliter->first); + DeleteList.push_back(deliter->first); } } + + for (std::list::iterator removeetagiter = DeleteList.begin(); + removeetagiter != DeleteList.end(); removeetagiter++){ + + ETagDBPtr->RemoveETag(*removeetagiter); + + } // Write out the update server sync token. @@ -1438,7 +1896,7 @@ void frmActivityMgr::ProcessTasksThread() // Update the data to the server. - CardDAVIter->second->ProcessData(); + ConnHandle.ProcessData(); iter->second = 4; continue; @@ -1462,8 +1920,8 @@ void frmActivityMgr::ProcessTasksThread() AccountDir = StringAccountIter->second; ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir); - CardDAVIter->second->GetServerETagValueThread(); - ETagServer = CardDAVIter->second->ETagValueResult(); + ConnHandle.GetServerETagValueThread(); + ETagServer = ConnHandle.ETagValueResult(); ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer); } @@ -1474,8 +1932,8 @@ void frmActivityMgr::ProcessTasksThread() AccountDir = StringAccountIter->second; ETagDB *ETagDBPtr = ETagTmrPtr->GetPointer(AccountDir); - CardDAVIter->second->GetServerETagValueThread(); - ETagServer = CardDAVIter->second->ETagValueResult(); + ConnHandle.GetServerETagValueThread(); + ETagServer = ConnHandle.ETagValueResult(); ETagDBPtr->UpdateETag(ContactFilename, ETagServer, ETagServer); } diff --git a/source/actmgr/frmActivityMgr.h b/source/actmgr/frmActivityMgr.h index 55458b1..cae74e7 100644 --- a/source/actmgr/frmActivityMgr.h +++ b/source/actmgr/frmActivityMgr.h @@ -95,6 +95,8 @@ class frmActivityMgr : public frmActivityMgrADT std::map ActivityListType; std::map ActivityListIndex; std::map ActivityListData; + std::map ActivityListURL; + std::map ActivityListEditMode; std::map ActivityListTaskDetail; std::map ActivityListAccount; std::map ActivityListFilename; -- 2.39.5