Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
macOS: Implemented support for adding, editing and deleting a CalDAV account
[xestiacalendar/.git] / source / forms / newaccount / frmNewAccount.cpp
index fc1ae8a..c50cf84 100644 (file)
 
 #include "frmNewAccount.h"
 
+#if defined(__WIN32__)
+#include "../common/win32ssl.h"
+#endif
+
 DEFINE_EVENT_TYPE(UPDATERESULTS);
 DEFINE_EVENT_TYPE(RUNCALDAVTEST);
 
@@ -117,6 +121,7 @@ void frmNewAccount::Navigate( wxCommandEvent& event )
                }
                
                btnNext->Disable();
+               btnNext->SetLabel(_("Next >"));
         
                bool ServerResult = FALSE;
                bool ServerAction = FALSE;
@@ -155,12 +160,11 @@ void frmNewAccount::Navigate( wxCommandEvent& event )
                tabConn->Hide();
                tabFinish->Show();
                szrNewAccount->RecalcSizes();
-        
-               btnNext->Disable();
+
                btnPrevious->Enable();
                btnNext->SetLabel(_("Finish"));
        
-               if (txtAccountName->IsEmpty() && pageSeek == 2){
+               if (txtAccountName->IsEmpty() || txtAccountName->GetValue().Len() < 4 && pageSeek == 3){
        
                        btnNext->Disable();
            
@@ -221,21 +225,30 @@ void frmNewAccount::Navigate( wxCommandEvent& event )
             
                }
         
-               if (cmbServerType->GetCurrentSelection() == 1){
+               wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
+       
+               if (cmbServerType->GetCurrentSelection() == 0){
             
                        // Create the account directory.
             
-                       wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
-            
-                       if (wxMkdir(xestiaCALDirectory + wxT("\\accounts\\") + directoryName + wxT(".local"), 0740) == TRUE){
+                       if (wxMkdir(xestiaCALDirectory + wxT("\\accounts\\") + directoryName + wxT(".Local"), 0740) == true){
+                
+                               WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
+                
+                       } else {
                 
-                               directoryCreated = TRUE;
+                               wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
+                               return;
                 
                        }
             
-                       if (directoryCreated == TRUE){
+               } else if (cmbServerType->GetCurrentSelection() == 1){
+                                               
+                       // Create the account directory.
+            
+                       if (wxMkdir(xestiaCALDirectory + wxT("\\accounts\\") + directoryName + wxT(".CalDAV"), 0740) == true){
                 
-                               WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
+                               WriteAccountDetails(cfgFile, wxT("CalDAV"), directoryName);
                 
                        } else {
                 
@@ -243,7 +256,7 @@ void frmNewAccount::Navigate( wxCommandEvent& event )
                                return;
                 
                        }
-            
+                       
                }
         
                delete cfgFile;
@@ -283,23 +296,34 @@ void frmNewAccount::Navigate( wxCommandEvent& event )
                        continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
             
                }
+
+               wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
         
-               if (cmbServerType->GetCurrentSelection() == 1){
+               if (cmbServerType->GetCurrentSelection() == 0){
             
                        // Create the account directory.
             
-                       wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
-            
-                       if (wxMkdir(xestiaCALDirectory + wxT("/accounts/") + directoryName + wxT(".Local"), 0740) == TRUE){
+                       if (wxMkdir(xestiaCALDirectory + wxT("/accounts/") + directoryName + wxT(".Local"), 0740) == true){
+                
+                               WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
+                       
+                       } else {
                 
-                               directoryCreated = TRUE;
+                               wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
+                               return;
                 
                        }
             
-                       if (directoryCreated == TRUE){
-                
-                               WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
+               } else if (cmbServerType->GetCurrentSelection() == 1){
+            
+                       // Create the account directory.
+            
+                       wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
+            
+                       if (wxMkdir(xestiaCALDirectory + wxT("/accounts/") + directoryName + wxT(".CalDAV"), 0740) == true){
                 
+                               WriteAccountDetails(cfgFile, wxT("CalDAV"), directoryName);
+                       
                        } else {
                 
                                wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
@@ -310,7 +334,7 @@ void frmNewAccount::Navigate( wxCommandEvent& event )
                }
         
                delete cfgFile;
-               cfgFile = NULL;
+               cfgFile = nullptr;
         
                *reloadAccountConfig = true;
                
@@ -388,7 +412,56 @@ void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgFileIn, wxString accoun
 
 void frmNewAccount::UpdateResults( wxCommandEvent &event )
 {
+       NewAccountResult *resultData = static_cast<NewAccountResult*>(event.GetClientData());
+
+       lblServerConnResult->SetLabel((resultData->Connected ? _("Successful") : _("Failed")));
+       lblServerResponse->SetLabel((resultData->ValidResponse ? _("Yes") : _("No")));
+       if (chkUseSSL->GetValue())
+       {
+               lblServerSSLResult->SetLabel((resultData->SSLStatus ? _("Yes") : _("No")));
+               lblServerSSLValid->SetLabel((resultData->SSLVerified == COSSL_VERIFIED ? _("Yes") : _("No")));
+       } else {
+               lblServerSSLResult->SetLabel(_("Not Applicable"));
+               lblServerSSLValid->SetLabel(_("Not Applicable"));
+       }
+       lblAbleToLoginResult->SetLabel((resultData->AuthPassed ? _("Yes") : _("No")));
+       lblCalDAVSupportResult->SetLabel((resultData->CanProcess ? _("Yes") : _("No")));
+       
+       if (resultData->ErrorMessage != "")
+       {
+               lblConnectionResultText->SetLabel(_("An error occured whilst connecting to the server: ") + resultData->ErrorMessage);
+       }
+       
+       if (VerifyResultData(resultData))
+       {
+               lblConnectionResultText->SetLabel(_("Successfully connected to the server. Press Next to set the account name."));
+               btnNext->Enable(true);
+       }
+       else
+       {
+               btnNext->Enable(false);
+       }
+       
+       tabConn->Layout();
+       btnPrevious->Enable(true);
+       
+       delete resultData;
+       resultData = nullptr;
+}
+
+bool frmNewAccount::VerifyResultData(NewAccountResult *resultData)
+{
+       if (!resultData->Connected) return false;
+       if (!resultData->ValidResponse) return false;
+       if (chkUseSSL->GetValue())
+       {
+               if (!resultData->SSLStatus) return false;
+               if (resultData->SSLVerified != COSSL_VERIFIED) return false;
+       }
+       if (!resultData->AuthPassed) return false;
+       if (!resultData->CanProcess) return false;
        
+       return true;
 }
 
 void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
@@ -401,6 +474,7 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
        lblServerSSLResult->SetLabel(wxT(""));
        lblServerSSLValid->SetLabel(wxT(""));
        lblAbleToLoginResult->SetLabel(wxT(""));
+       lblConnectionResultText->SetLabel(wxT(""));
        bool usingSSLBypass = false;
        
        // Setup a CalDAV connection object for testing.
@@ -424,20 +498,20 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
        // Test the connection.
        
        //testConnection.SetupConnectionObject();
-       CalDAVServerResult testConnectionResult = testConnection.Connect();
+       CalDAVServerResult testConnectionResult = testConnection.Connect(false);
        
        // If server is using SSL, verify that the SSL connection is valid.
        
        if (testConnection.SSLVerify() == COSSL_UNABLETOVERIFY){
 #if defined(__APPLE__)
-               
+        
                testConnection.BypassSSLVerification(true);
                
-               CalDAVServerResult testConnection = testConnection.Connect(false);
+               CalDAVServerResult testConnectionResult = testConnection.Connect(false);
                
                testConnection.BypassSSLVerification(false);
                
-               int SSLResult = DisplayTrustPanel(&TestConnection);
+               int SSLResult = DisplayTrustPanel(&testConnection);
                
                if (SSLResult != NSOKButton){
                        
@@ -445,7 +519,7 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
                        lblServerResponse->SetLabel(_("Not applicable"));
                        lblServerSSLResult->SetLabel(_("Used"));
                        lblServerSSLValid->SetLabel(_("No"));
-                       lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + TestConnection.GetErrorMessage());
+                       lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + testConnection.GetErrorMessage());
                        btnPrevious->Enable(true);
                        return;
                        
@@ -458,7 +532,8 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
                        switch(evalResult){
                                case kSecTrustResultProceed:
                                        lblServerSSLValid->SetLabel(_("Verified"));
-                                       break;
+                    break;
+                case kSecTrustResultUnspecified:
                                case kSecTrustResultConfirm:
                                        lblServerSSLValid->SetLabel(_("Verified (user)"));
                                        break;
@@ -466,10 +541,14 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
                                        lblServerSSLValid->SetLabel(_("Unable to verify"));
                        }
                        
+            lblServerConnResult->SetLabel(_("Connected"));
                        lblServerResponse->SetLabel(_("Not applicable"));
                        lblServerSSLResult->SetLabel(_("Used"));
                        
-                       if (evalResult != kSecTrustResultProceed){
+            btnPrevious->Enable(true);
+            
+                       if (evalResult != kSecTrustResultProceed && evalResult != kSecTrustResultUnspecified){
+                lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + testConnection.GetErrorMessage());
                                return;
                        }
                        
@@ -483,7 +562,7 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
 
                testConnection.BypassSSLVerification(false);
 
-               bool modifiedCertificateData = false;
+               BOOL modifiedCertificateData = false;
 
                CRYPTUI_VIEWCERTIFICATE_STRUCTW certificateDialogData = BuildCertificateData(&testConnection, (HWND)this->GetHandle());
 
@@ -509,7 +588,7 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
                
                testConnection.BypassSSLVerification(true);
                
-               CalDAVServerResult testConnectionResult = testConnection.Connect();
+               CalDAVServerResult testConnectionResult = testConnection.Connect(false);
                
                testConnection.BypassSSLVerification(false);
 
@@ -524,7 +603,7 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
                // Clean up before processing response.
                                
                delete frmICPtr;
-               frmICPtr = NULL;
+               frmICPtr = nullptr;
                                                        
                // Process the response from the user.
                                                        
@@ -535,7 +614,7 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
                        usingSSLBypass = true;
                        testConnection.BypassSSLVerification(true);
                
-                       CalDAVServerResult testConnectionResult = testConnection.Connect();
+                       CalDAVServerResult testConnectionResult = testConnection.Connect(true);
                
                        testConnection.BypassSSLVerification(false);
                                                                
@@ -546,40 +625,57 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
                        // TODO: Integrate into the code.
                        
                        //lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
+                       
+                       resultData->Connected = true;
+                       resultData->SSLStatus = true;
+                       resultData->SSLVerified = COSSL_UNABLETOVERIFY;
+                       resultData->ValidResponse = false;
+                       resultData->AuthPassed = false;
+                       resultData->CanProcess = false;
+                       resultData->ErrorMessage = _("Server is using self-signed certificate and was rejected.");
+                       
+                       wxCommandEvent resultsEvent(UPDATERESULTS);
+                       resultsEvent.SetClientData(resultData);
+                       wxPostEvent(this, resultsEvent);
+                       
+                       return;
                                                
                }               
                
 #endif
        }
        
-       testConnectionResult = testConnection.Connect();
+       if (usingSSLBypass == true){
+               testConnection.BypassSSLVerification(true);                     
+       }
+       
+       testConnectionResult = testConnection.Connect(true);
 
        // Get the server prefix if the connection was successful.
        
        if (testConnectionResult.result == CALDAVQUERYRESULT_OK){
-
-               if (usingSSLBypass == true){
-                       testConnection.BypassSSLVerification(true);                     
-               }
                
                std::string receivedServerPrefix;
                
                receivedServerPrefix = testConnection.GetUserPrincipal();
                serverPrefix = receivedServerPrefix;
                
-               if (usingSSLBypass == true){
-                       testConnection.BypassSSLVerification(true);                     
-               }
-               
        }
        
-       testConnectionResult.result == CALDAVQUERYRESULT_OK ? resultData->Connected = true : resultData->Connected = false;
+       CalDAVServerSupport testConnectionSupport = testConnection.GetServerSupport();
+       
+       if (usingSSLBypass == true){
+               testConnection.BypassSSLVerification(false);
+       }
+       
+       (testConnectionResult.result == CALDAVQUERYRESULT_OK || testConnectionResult.result == CALDAVQUERYRESULT_SSLFAILURE) ? 
+               resultData->Connected = true : resultData->Connected = false;
        
        resultData->SSLStatus = testConnection.CanDoSSL();
        resultData->SSLVerified = testConnection.SSLVerify();
        resultData->ValidResponse = testConnection.HasValidResponse();
        resultData->AuthPassed = testConnection.AbleToLogin();
-       resultData->CanProcess = testConnection.CanDoProcessing();
+       resultData->CanProcess = testConnectionSupport.basicSupport;
        resultData->ErrorMessage = testConnection.GetErrorMessage();
        
        // Post event back confirming the tests.
@@ -587,4 +683,4 @@ void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
        wxCommandEvent resultsEvent(UPDATERESULTS);
        resultsEvent.SetClientData(resultData);
        wxPostEvent(this, resultsEvent);
-}
\ No newline at end of file
+}
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy