Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
CalDAV: WIP CalDAV support
[xestiacalendar/.git] / source / forms / newaccount / frmNewAccount.cpp
1 // frmNewAccount.cpp - frmNewAccount form functions
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar 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.
10 //
11 // Xestia Calendar 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.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "frmNewAccount.h"
21 DEFINE_EVENT_TYPE(UPDATERESULTS);
22 DEFINE_EVENT_TYPE(RUNCALDAVTEST);
24 BEGIN_EVENT_TABLE(frmNewAccount, wxDialog)
25 EVT_COMMAND(wxID_ANY, UPDATERESULTS, frmNewAccount::UpdateResults)
26 EVT_COMMAND(wxID_ANY, RUNCALDAVTEST, frmNewAccount::RunCalDAVTest)
27 END_EVENT_TABLE()
29 frmNewAccount::frmNewAccount( wxWindow* parent )
30 :
31 frmNewAccountADT( parent )
32 {
34         // Disable the previous button upon form creation.
36         btnPrevious->Disable();
37         txtServerAddress->Disable();
38         txtServerPort->Disable();
39         txtUsername->Disable();
40         txtPassword->Disable();
41         chkUseSSL->Disable();
42         
43 }
45 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
46 {
47         // Update the options.
48     
49         if (cmbServerType->GetCurrentSelection() == 1){
50         
51                 txtServerAddress->Enable();
52                 txtServerPort->Enable();
53                 txtUsername->Enable();
54                 txtPassword->Enable();
55                 chkUseSSL->Enable();
56         
57         } else if (cmbServerType->GetCurrentSelection() == 0){
58         
59                 txtServerAddress->Disable();
60                 txtServerPort->Disable();
61                 txtUsername->Disable();
62                 txtPassword->Disable();
63                 chkUseSSL->Disable();
64         
65         }
66 }
68 void frmNewAccount::Navigate( wxCommandEvent& event )
69 {
71         if (event.GetId() == wxID_NEXT)
72                 pageSeek++;
73         else
74                 pageSeek--;
75     
76         if (pageSeek == 1){
77         
78                 // Skip this page.
80                 btnPrevious->Disable();
81                 btnNext->Enable();
83                 tabConn->Hide();
84                 tabFinish->Hide();
85                 tabType->Show();
86                 btnNext->SetLabel(_("Next >"));
87         
88         } else if (pageSeek == 2){
89         
90                 if (cmbServerType->GetCurrentSelection() == 0){
91             
92                         tabType->Hide();
93                         tabConn->Hide();
94                         tabFinish->Show();
95                         szrNewAccount->RecalcSizes();
96                         pageSeek = 2;
97                         
98                         btnNext->Disable();
99                         btnPrevious->Enable();
100                         btnNext->SetLabel(_("Finish"));
101                         return;
102             
103                 }
104                 
105                 // Check if server address matches against the blacklist.
106                 // Bring up warning message if it does.
107                 
108                 if (CheckBlacklist(txtServerAddress->GetValue())){
109                 
110                         int MessageBoxResult = wxMessageBox(_("The server with the address given does not support the CalDAV protocol properly and shouldn't be used.\n\nData loss is very likely.\n\nDo you still want to continue using this server?"), _("Server warning"), wxYES_NO, this);
111                 
112                         if (MessageBoxResult == wxNO){
113                                 pageSeek--;
114                                 return;
115                         }
116                         
117                 }
118                 
119                 btnNext->Disable();
120         
121                 bool ServerResult = FALSE;
122                 bool ServerAction = FALSE;
123                 bool UseSSL = TRUE;
124                 wxString ServerMessage;
125         
126                 // Connection test screen.
127         
128                 tabType->Hide();
129                 tabConn->Show();
130                 tabFinish->Hide();
131                 szrNewAccount->RecalcSizes();
132                 btnPrevious->Disable();
133         
134                 // Reset screen.
135         
136                 lblServerConnResult->SetLabel(wxT(""));
137                 lblServerResponse->SetLabel(wxT(""));
138                 lblServerSSLResult->SetLabel(wxT(""));
139                 lblServerSSLValid->SetLabel(wxT(""));
140                 lblAbleToLoginResult->SetLabel(wxT(""));
141                 lblCalDAVSupportResult->SetLabel(wxT(""));
142         
143                 // Depending on account type, run the test.
144         
145                 if (cmbServerType->GetCurrentSelection() == 1){
146                         wxCommandEvent RunTest(RUNCALDAVTEST);
147                         wxPostEvent(this, RunTest);
148                 }
149         
150         } else if (pageSeek == 3){
151         
152                 // Finish screen.
153         
154                 tabType->Hide();
155                 tabConn->Hide();
156                 tabFinish->Show();
157                 szrNewAccount->RecalcSizes();
158         
159                 btnNext->Disable();
160                 btnPrevious->Enable();
161                 btnNext->SetLabel(_("Finish"));
162         
163                 if (txtAccountName->IsEmpty() && pageSeek == 2){
164         
165                         btnNext->Disable();
166             
167                 } else {
168         
169                         btnNext->Enable();
170             
171                 }
172         
173         } else if (pageSeek == 4){
174         
175                 // Finished.
176         
177                 wxString xestiaCALPrefDirectory;
178                 wxString xestiaCALDirectory;
179                 wxString accountSettingsFile;
180                 //wxFile ASFile;
181                 
182                 srand(time(0));
183                 int randomNumber = rand() % 32767;
184                 wxString randomNumberSuffix = wxString::Format(wxT("%i"), randomNumber);
185                 bool directoryCreated = FALSE;
186         
187 #if defined(__HAIKU__)
188         
189                 //preffilename = wxT("noo");
190         
191 #elif defined(__WIN32__)
192         
193                 xestiaCALPrefDirectory = GetUserPrefDir();
194                 xestiaCALDirectory = GetUserDir();
195         
196                 accountSettingsFile = xestiaCALPrefDirectory + wxT("accounts");
197         
198                 // Open the file for writing.
199         
200                 wxFileConfig *cfgFile = new wxFileConfig("", "", accountSettingsFile);
201         
202                 // Check if account name already exists and return an error message
203                 // if this is the case.
204         
205                 wxString accountName;
206                 long itemIndex = 0;
207                 bool continueAcc;
208                 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
209         
210                 while (continueAcc){
211             
212                         if (txtAccountName->GetValue() == accountName){
213                 
214                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
215                                 return;
216                 
217                         }
218             
219                         cfgFile->SetPath(wxT("/"));
220                         continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
221             
222                 }
223         
224                 if (cmbServerType->GetCurrentSelection() == 1){
225             
226                         // Create the account directory.
227             
228                         wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
229             
230                         if (wxMkdir(xestiaCALDirectory + wxT("\\accounts\\") + directoryName + wxT(".local"), 0740) == TRUE){
231                 
232                                 directoryCreated = TRUE;
233                 
234                         }
235             
236                         if (directoryCreated == TRUE){
237                 
238                                 WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
239                 
240                         } else {
241                 
242                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
243                                 return;
244                 
245                         }
246             
247                 }
248         
249                 delete cfgFile;
250                 cfgFile = NULL;
251         
252                 *reloadAccountConfig = TRUE;
253         
254 #else
255         
256                 xestiaCALPrefDirectory = GetUserPrefDir();
257                 xestiaCALDirectory = GetUserDir();
258         
259                 accountSettingsFile = xestiaCALPrefDirectory + wxT("accounts");
260         
261                 // Open the file for writing.
262         
263                 wxFileConfig *cfgFile = new wxFileConfig("", "", accountSettingsFile);
264         
265                 // Check if account name already exists and return an error message
266                 // if this is the case.
267         
268                 wxString accountName;
269                 long itemIndex = 0;
270                 bool continueAcc;
271                 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
272         
273                 while (continueAcc){
274             
275                         if (txtAccountName->GetValue() == accountName){
276                 
277                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
278                                 return;
279                 
280                         }
281             
282                         cfgFile->SetPath(wxT("/"));
283                         continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
284             
285                 }
286         
287                 if (cmbServerType->GetCurrentSelection() == 1){
288             
289                         // Create the account directory.
290             
291                         wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
292             
293                         if (wxMkdir(xestiaCALDirectory + wxT("/accounts/") + directoryName + wxT(".Local"), 0740) == TRUE){
294                 
295                                 directoryCreated = TRUE;
296                 
297                         }
298             
299                         if (directoryCreated == TRUE){
300                 
301                                 WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
302                 
303                         } else {
304                 
305                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
306                                 return;
307                 
308                         }
309             
310                 }
311         
312                 delete cfgFile;
313                 cfgFile = NULL;
314         
315                 *reloadAccountConfig = true;
316                 
317 #endif
318         
319                 this->Close();
320         
321         }
322         
325 void frmNewAccount::CloseWindow( wxCommandEvent& event )
327         
328         // Close the window.
330         *reloadAccountConfig = FALSE;
331         this->Close();
332         
335 void frmNewAccount::SetupPointers(bool *ReloadAccountInc, CalendarDataStorage *dataStorage){
337         // Setup the pointers for the new account window.
338    
339         reloadAccountConfig = ReloadAccountInc;
340     
343 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
345     
346         // Check that the account name is valid.
347     
348         wxString checkAccName = txtAccountName->GetValue();
349     
350         if ((txtAccountName->IsEmpty() && pageSeek == 2) || checkAccName.Len() < 4){
351         
352                 btnNext->Disable();
353         
354         } else {
355         
356                 btnNext->Enable();
357         
358         }
359     
362 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgFileIn, wxString accountType, wxString directoryName){
363     
364         // Write the new account details.
365     
366         cfgFileIn->SetPath(txtAccountName->GetValue());
367         cfgFileIn->Write(wxT("address"), txtServerAddress->GetValue());
368         cfgFileIn->Write(wxT("port"), txtServerPort->GetValue());
369         cfgFileIn->Write(wxT("username"), txtUsername->GetValue());
370         cfgFileIn->Write(wxT("password"), txtPassword->GetValue());
371         cfgFileIn->Write(wxT("prefix"), serverPrefix);
372         cfgFileIn->Write(wxT("accountdir"), directoryName);
373     
374         if (chkUseSSL->GetValue() == TRUE){
375         
376                 cfgFileIn->Write(wxT("ssl"), wxT("true"));
377         
378         } else {
379         
380                 cfgFileIn->Write(wxT("ssl"), wxT("false"));
381         
382         }
383     
384         cfgFileIn->Write(wxT("refresh"), wxT("1800"));
385         cfgFileIn->Write(wxT("type"), accountType);
386     
389 void frmNewAccount::UpdateResults( wxCommandEvent &event )
391         
394 void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
396         NewAccountResult *resultData = new NewAccountResult;
397         
398         lblServerConnResult->SetLabel(_("Testing..."));
399         lblCalDAVSupportResult->SetLabel(wxT(""));
400         lblServerResponse->SetLabel(wxT(""));
401         lblServerSSLResult->SetLabel(wxT(""));
402         lblServerSSLValid->SetLabel(wxT(""));
403         lblAbleToLoginResult->SetLabel(wxT(""));
404         bool usingSSLBypass = false;
405         
406         // Setup a CalDAV connection object for testing.
408         CalDAVConnectionData connData;
409         connData.hostname = txtServerAddress->GetValue().ToStdString();
410         connData.port = wxAtoi(txtServerPort->GetValue());
411         connData.username = txtUsername->GetValue().ToStdString();
412         connData.password = txtPassword->GetValue().ToStdString();
413         connData.useSSL = chkUseSSL->GetValue() ? true : false;
415         CalDAV testConnection;
416         testConnection.SetupConnectionData(&connData);
418         /*CardDAV2 TestConnection(txtServerAddress->GetValue().ToStdString(),
419                 wxAtoi(txtServerPort->GetValue()),
420                 txtUsername->GetValue().ToStdString(),
421                 txtPassword->GetValue().ToStdString(),
422                 chkUseSSL->GetValue() ? true : false);*/
423         
424         // Test the connection.
425         
426         //testConnection.SetupConnectionObject();
427         CalDAVServerResult testConnectionResult = testConnection.Connect();
428         
429         // If server is using SSL, verify that the SSL connection is valid.
430         
431         if (testConnection.SSLVerify() == COSSL_UNABLETOVERIFY){
432 #if defined(__APPLE__)
433                 
434                 testConnection.BypassSSLVerification(true);
435                 
436                 CalDAVServerResult testConnection = testConnection.Connect(false);
437                 
438                 testConnection.BypassSSLVerification(false);
439                 
440                 int SSLResult = DisplayTrustPanel(&TestConnection);
441                 
442                 if (SSLResult != NSOKButton){
443                         
444                         lblServerConnResult->SetLabel(_("Failed"));
445                         lblServerResponse->SetLabel(_("Not applicable"));
446                         lblServerSSLResult->SetLabel(_("Used"));
447                         lblServerSSLValid->SetLabel(_("No"));
448                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + TestConnection.GetErrorMessage());
449                         btnPrevious->Enable(true);
450                         return;
451                         
452                 } else {
453                         
454                         // Evalulate the trust object.
455                         
456                         SecTrustResultType evalResult = ProcessResultType(&testConnection);
457                         
458                         switch(evalResult){
459                                 case kSecTrustResultProceed:
460                                         lblServerSSLValid->SetLabel(_("Verified"));
461                                         break;
462                                 case kSecTrustResultConfirm:
463                                         lblServerSSLValid->SetLabel(_("Verified (user)"));
464                                         break;
465                                 default:
466                                         lblServerSSLValid->SetLabel(_("Unable to verify"));
467                         }
468                         
469                         lblServerResponse->SetLabel(_("Not applicable"));
470                         lblServerSSLResult->SetLabel(_("Used"));
471                         
472                         if (evalResult != kSecTrustResultProceed){
473                                 return;
474                         }
475                         
476                 }
477                 
478 #elif defined(__WIN32__)
480                 testConnection.BypassSSLVerification(true);
482                 CalDAVServerResult testConnectionResult = testConnection.Connect(false);
484                 testConnection.BypassSSLVerification(false);
486                 bool modifiedCertificateData = false;
488                 CRYPTUI_VIEWCERTIFICATE_STRUCTW certificateDialogData = BuildCertificateData(&testConnection, (HWND)this->GetHandle());
490                 if (!CryptUIDlgViewCertificate(&certificateDialogData, &modifiedCertificateData)){
491                         wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
492                 }
494                 if (modifiedCertificateData == false){
496                         lblServerConnResult->SetLabel(_("Failed"));
497                         lblServerResponse->SetLabel(_("Not applicable"));
498                         lblServerSSLResult->SetLabel(_("Used"));
499                         lblServerSSLValid->SetLabel(_("No"));
500                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + testConnection.GetErrorMessage());
501                         btnPrevious->Enable(true);
502                         return;
504                 }
506 #else
507         
508                 // Connect again and fetch SSL certificate information.
509                 
510                 testConnection.BypassSSLVerification(true);
511                 
512                 CalDAVServerResult testConnectionResult = testConnection.Connect();
513                 
514                 testConnection.BypassSSLVerification(false);
516                 SSLCertCollectionString certData = testConnection.BuildSSLCollection();
517                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
518                 
519                 frmICPtr->LoadDataNew(certData, txtServerAddress->GetValue().ToStdString());
520                 frmICPtr->ShowModal();
521                                                                 
522                 int sslResult = frmICPtr->GetResult();
523                                                         
524                 // Clean up before processing response.
525                                 
526                 delete frmICPtr;
527                 frmICPtr = NULL;
528                                                         
529                 // Process the response from the user.
530                                                         
531                 if (sslResult == 1){
532                                                                 
533                         // Accept the Certificate.
534                         
535                         usingSSLBypass = true;
536                         testConnection.BypassSSLVerification(true);
537                 
538                         CalDAVServerResult testConnectionResult = testConnection.Connect();
539                 
540                         testConnection.BypassSSLVerification(false);
541                                                                 
542                 } else if (sslResult == 2){
543                                                                 
544                         // Reject the certificate, abort the task and mark as failed.
546                         // TODO: Integrate into the code.
547                         
548                         //lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
549                                                 
550                 }               
551                 
552 #endif
553         }
554         
555         testConnectionResult = testConnection.Connect();
557         // Get the server prefix if the connection was successful.
558         
559         if (testConnectionResult.result == CALDAVQUERYRESULT_OK){
561                 if (usingSSLBypass == true){
562                         testConnection.BypassSSLVerification(true);                     
563                 }
564                 
565                 std::string receivedServerPrefix;
566                 
567                 receivedServerPrefix = testConnection.GetUserPrincipal();
568                 serverPrefix = receivedServerPrefix;
569                 
570                 if (usingSSLBypass == true){
571                         testConnection.BypassSSLVerification(true);                     
572                 }
573                 
574         }
575         
576         testConnectionResult.result == CALDAVQUERYRESULT_OK ? resultData->Connected = true : resultData->Connected = false;
577         
578         resultData->SSLStatus = testConnection.CanDoSSL();
579         resultData->SSLVerified = testConnection.SSLVerify();
580         resultData->ValidResponse = testConnection.HasValidResponse();
581         resultData->AuthPassed = testConnection.AbleToLogin();
582         resultData->CanProcess = testConnection.CanDoProcessing();
583         resultData->ErrorMessage = testConnection.GetErrorMessage();
584         
585         // Post event back confirming the tests.
586         
587         wxCommandEvent resultsEvent(UPDATERESULTS);
588         resultsEvent.SetClientData(resultData);
589         wxPostEvent(this, resultsEvent);
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