Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmNewAccount: Implemented Win32 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 #if defined(__WIN32__)
22 #include "../common/win32ssl.h"
23 #endif
25 DEFINE_EVENT_TYPE(UPDATERESULTS);
26 DEFINE_EVENT_TYPE(RUNCALDAVTEST);
28 BEGIN_EVENT_TABLE(frmNewAccount, wxDialog)
29 EVT_COMMAND(wxID_ANY, UPDATERESULTS, frmNewAccount::UpdateResults)
30 EVT_COMMAND(wxID_ANY, RUNCALDAVTEST, frmNewAccount::RunCalDAVTest)
31 END_EVENT_TABLE()
33 frmNewAccount::frmNewAccount( wxWindow* parent )
34 :
35 frmNewAccountADT( parent )
36 {
38         // Disable the previous button upon form creation.
40         btnPrevious->Disable();
41         txtServerAddress->Disable();
42         txtServerPort->Disable();
43         txtUsername->Disable();
44         txtPassword->Disable();
45         chkUseSSL->Disable();
46         
47 }
49 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
50 {
51         // Update the options.
52     
53         if (cmbServerType->GetCurrentSelection() == 1){
54         
55                 txtServerAddress->Enable();
56                 txtServerPort->Enable();
57                 txtUsername->Enable();
58                 txtPassword->Enable();
59                 chkUseSSL->Enable();
60         
61         } else if (cmbServerType->GetCurrentSelection() == 0){
62         
63                 txtServerAddress->Disable();
64                 txtServerPort->Disable();
65                 txtUsername->Disable();
66                 txtPassword->Disable();
67                 chkUseSSL->Disable();
68         
69         }
70 }
72 void frmNewAccount::Navigate( wxCommandEvent& event )
73 {
75         if (event.GetId() == wxID_NEXT)
76                 pageSeek++;
77         else
78                 pageSeek--;
79     
80         if (pageSeek == 1){
81         
82                 // Skip this page.
84                 btnPrevious->Disable();
85                 btnNext->Enable();
87                 tabConn->Hide();
88                 tabFinish->Hide();
89                 tabType->Show();
90                 btnNext->SetLabel(_("Next >"));
91         
92         } else if (pageSeek == 2){
93         
94                 if (cmbServerType->GetCurrentSelection() == 0){
95             
96                         tabType->Hide();
97                         tabConn->Hide();
98                         tabFinish->Show();
99                         szrNewAccount->RecalcSizes();
100                         pageSeek = 2;
101                         
102                         btnNext->Disable();
103                         btnPrevious->Enable();
104                         btnNext->SetLabel(_("Finish"));
105                         return;
106             
107                 }
108                 
109                 // Check if server address matches against the blacklist.
110                 // Bring up warning message if it does.
111                 
112                 if (CheckBlacklist(txtServerAddress->GetValue())){
113                 
114                         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);
115                 
116                         if (MessageBoxResult == wxNO){
117                                 pageSeek--;
118                                 return;
119                         }
120                         
121                 }
122                 
123                 btnNext->Disable();
124                 btnNext->SetLabel(_("Next >"));
125         
126                 bool ServerResult = FALSE;
127                 bool ServerAction = FALSE;
128                 bool UseSSL = TRUE;
129                 wxString ServerMessage;
130         
131                 // Connection test screen.
132         
133                 tabType->Hide();
134                 tabConn->Show();
135                 tabFinish->Hide();
136                 szrNewAccount->RecalcSizes();
137                 btnPrevious->Disable();
138         
139                 // Reset screen.
140         
141                 lblServerConnResult->SetLabel(wxT(""));
142                 lblServerResponse->SetLabel(wxT(""));
143                 lblServerSSLResult->SetLabel(wxT(""));
144                 lblServerSSLValid->SetLabel(wxT(""));
145                 lblAbleToLoginResult->SetLabel(wxT(""));
146                 lblCalDAVSupportResult->SetLabel(wxT(""));
147         
148                 // Depending on account type, run the test.
149         
150                 if (cmbServerType->GetCurrentSelection() == 1){
151                         wxCommandEvent RunTest(RUNCALDAVTEST);
152                         wxPostEvent(this, RunTest);
153                 }
154         
155         } else if (pageSeek == 3){
156         
157                 // Finish screen.
158         
159                 tabType->Hide();
160                 tabConn->Hide();
161                 tabFinish->Show();
162                 szrNewAccount->RecalcSizes();
164                 btnPrevious->Enable();
165                 btnNext->SetLabel(_("Finish"));
166         
167                 if (txtAccountName->IsEmpty() || txtAccountName->GetValue().Len() < 4 && pageSeek == 3){
168         
169                         btnNext->Disable();
170             
171                 } else {
172         
173                         btnNext->Enable();
174             
175                 }
176         
177         } else if (pageSeek == 4){
178         
179                 // Finished.
180         
181                 wxString xestiaCALPrefDirectory;
182                 wxString xestiaCALDirectory;
183                 wxString accountSettingsFile;
184                 //wxFile ASFile;
185                 
186                 srand(time(0));
187                 int randomNumber = rand() % 32767;
188                 wxString randomNumberSuffix = wxString::Format(wxT("%i"), randomNumber);
189                 bool directoryCreated = FALSE;
190         
191 #if defined(__HAIKU__)
192         
193                 //preffilename = wxT("noo");
194         
195 #elif defined(__WIN32__)
196         
197                 xestiaCALPrefDirectory = GetUserPrefDir();
198                 xestiaCALDirectory = GetUserDir();
199         
200                 accountSettingsFile = xestiaCALPrefDirectory + wxT("accounts");
201         
202                 // Open the file for writing.
203         
204                 wxFileConfig *cfgFile = new wxFileConfig("", "", accountSettingsFile);
205         
206                 // Check if account name already exists and return an error message
207                 // if this is the case.
208         
209                 wxString accountName;
210                 long itemIndex = 0;
211                 bool continueAcc;
212                 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
213         
214                 while (continueAcc){
215             
216                         if (txtAccountName->GetValue() == accountName){
217                 
218                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
219                                 return;
220                 
221                         }
222             
223                         cfgFile->SetPath(wxT("/"));
224                         continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
225             
226                 }
227         
228                 wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
229         
230                 if (cmbServerType->GetCurrentSelection() == 0){
231             
232                         // Create the account directory.
233             
234                         if (wxMkdir(xestiaCALDirectory + wxT("\\accounts\\") + directoryName + wxT(".Local"), 0740) == true){
235                 
236                                 WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
237                 
238                         } else {
239                 
240                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
241                                 return;
242                 
243                         }
244             
245                 } else if (cmbServerType->GetCurrentSelection() == 1){
246                                                 
247                         // Create the account directory.
248             
249                         if (wxMkdir(xestiaCALDirectory + wxT("\\accounts\\") + directoryName + wxT(".CalDAV"), 0740) == true){
250                 
251                                 WriteAccountDetails(cfgFile, wxT("CalDAV"), directoryName);
252                 
253                         } else {
254                 
255                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
256                                 return;
257                 
258                         }
259                         
260                 }
261         
262                 delete cfgFile;
263                 cfgFile = NULL;
264         
265                 *reloadAccountConfig = TRUE;
266         
267 #else
268         
269                 xestiaCALPrefDirectory = GetUserPrefDir();
270                 xestiaCALDirectory = GetUserDir();
271         
272                 accountSettingsFile = xestiaCALPrefDirectory + wxT("accounts");
273         
274                 // Open the file for writing.
275         
276                 wxFileConfig *cfgFile = new wxFileConfig("", "", accountSettingsFile);
277         
278                 // Check if account name already exists and return an error message
279                 // if this is the case.
280         
281                 wxString accountName;
282                 long itemIndex = 0;
283                 bool continueAcc;
284                 continueAcc = cfgFile->GetFirstGroup(accountName, itemIndex);
285         
286                 while (continueAcc){
287             
288                         if (txtAccountName->GetValue() == accountName){
289                 
290                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
291                                 return;
292                 
293                         }
294             
295                         cfgFile->SetPath(wxT("/"));
296                         continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
297             
298                 }
300                 wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
301         
302                 if (cmbServerType->GetCurrentSelection() == 0){
303             
304                         // Create the account directory.
305             
306                         if (wxMkdir(xestiaCALDirectory + wxT("/accounts/") + directoryName + wxT(".Local"), 0740) == true){
307                 
308                                 WriteAccountDetails(cfgFile, wxT("Local"), directoryName);
309                         
310                         } else {
311                 
312                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
313                                 return;
314                 
315                         }
316             
317                 } else if (cmbServerType->GetCurrentSelection() == 1){
318             
319                         // Create the account directory.
320             
321                         wxString directoryName = txtAccountName->GetValue().Mid(0, 30) + randomNumberSuffix;
322             
323                         if (wxMkdir(xestiaCALDirectory + wxT("/accounts/") + directoryName + wxT(".CalDAV"), 0740) == true){
324                 
325                                 WriteAccountDetails(cfgFile, wxT("CalDAV"), directoryName);
326                         
327                         } else {
328                 
329                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
330                                 return;
331                 
332                         }
333             
334                 }
335         
336                 delete cfgFile;
337                 cfgFile = nullptr;
338         
339                 *reloadAccountConfig = true;
340                 
341 #endif
342         
343                 this->Close();
344         
345         }
346         
349 void frmNewAccount::CloseWindow( wxCommandEvent& event )
351         
352         // Close the window.
354         *reloadAccountConfig = FALSE;
355         this->Close();
356         
359 void frmNewAccount::SetupPointers(bool *ReloadAccountInc, CalendarDataStorage *dataStorage){
361         // Setup the pointers for the new account window.
362    
363         reloadAccountConfig = ReloadAccountInc;
364     
367 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
369     
370         // Check that the account name is valid.
371     
372         wxString checkAccName = txtAccountName->GetValue();
373     
374         if ((txtAccountName->IsEmpty() && pageSeek == 2) || checkAccName.Len() < 4){
375         
376                 btnNext->Disable();
377         
378         } else {
379         
380                 btnNext->Enable();
381         
382         }
383     
386 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgFileIn, wxString accountType, wxString directoryName){
387     
388         // Write the new account details.
389     
390         cfgFileIn->SetPath(txtAccountName->GetValue());
391         cfgFileIn->Write(wxT("address"), txtServerAddress->GetValue());
392         cfgFileIn->Write(wxT("port"), txtServerPort->GetValue());
393         cfgFileIn->Write(wxT("username"), txtUsername->GetValue());
394         cfgFileIn->Write(wxT("password"), txtPassword->GetValue());
395         cfgFileIn->Write(wxT("prefix"), serverPrefix);
396         cfgFileIn->Write(wxT("accountdir"), directoryName);
397     
398         if (chkUseSSL->GetValue() == TRUE){
399         
400                 cfgFileIn->Write(wxT("ssl"), wxT("true"));
401         
402         } else {
403         
404                 cfgFileIn->Write(wxT("ssl"), wxT("false"));
405         
406         }
407     
408         cfgFileIn->Write(wxT("refresh"), wxT("1800"));
409         cfgFileIn->Write(wxT("type"), accountType);
410     
413 void frmNewAccount::UpdateResults( wxCommandEvent &event )
415         NewAccountResult *resultData = static_cast<NewAccountResult*>(event.GetClientData());
417         lblServerConnResult->SetLabel((resultData->Connected ? _("Successful") : _("Failed")));
418         lblServerResponse->SetLabel((resultData->ValidResponse ? _("Yes") : _("No")));
419         if (chkUseSSL->GetValue())
420         {
421                 lblServerSSLResult->SetLabel((resultData->SSLStatus ? _("Yes") : _("No")));
422                 lblServerSSLValid->SetLabel((resultData->SSLVerified == COSSL_VERIFIED ? _("Yes") : _("No")));
423         } else {
424                 lblServerSSLResult->SetLabel(_("Not Applicable"));
425                 lblServerSSLValid->SetLabel(_("Not Applicable"));
426         }
427         lblAbleToLoginResult->SetLabel((resultData->AuthPassed ? _("Yes") : _("No")));
428         lblCalDAVSupportResult->SetLabel((resultData->CanProcess ? _("Yes") : _("No")));
429         
430         if (resultData->ErrorMessage != "")
431         {
432                 lblConnectionResultText->SetLabel(_("An error occured whilst connecting to the server: ") + resultData->ErrorMessage);
433         }
434         
435         if (VerifyResultData(resultData))
436         {
437                 lblConnectionResultText->SetLabel(_("Successfully connected to the server. Press Next to set the account name."));
438                 btnNext->Enable(true);
439         }
440         else
441         {
442                 btnNext->Enable(false);
443         }
444         
445         tabConn->Layout();
446         btnPrevious->Enable(true);
447         
448         delete resultData;
449         resultData = nullptr;
452 bool frmNewAccount::VerifyResultData(NewAccountResult *resultData)
454         if (!resultData->Connected) return false;
455         if (!resultData->ValidResponse) return false;
456         if (chkUseSSL->GetValue())
457         {
458                 if (!resultData->SSLStatus) return false;
459                 if (resultData->SSLVerified != COSSL_VERIFIED) return false;
460         }
461         if (!resultData->AuthPassed) return false;
462         if (!resultData->CanProcess) return false;
463         
464         return true;
467 void frmNewAccount::RunCalDAVTest( wxCommandEvent &event )
469         NewAccountResult *resultData = new NewAccountResult;
470         
471         lblServerConnResult->SetLabel(_("Testing..."));
472         lblCalDAVSupportResult->SetLabel(wxT(""));
473         lblServerResponse->SetLabel(wxT(""));
474         lblServerSSLResult->SetLabel(wxT(""));
475         lblServerSSLValid->SetLabel(wxT(""));
476         lblAbleToLoginResult->SetLabel(wxT(""));
477         lblConnectionResultText->SetLabel(wxT(""));
478         bool usingSSLBypass = false;
479         
480         // Setup a CalDAV connection object for testing.
482         CalDAVConnectionData connData;
483         connData.hostname = txtServerAddress->GetValue().ToStdString();
484         connData.port = wxAtoi(txtServerPort->GetValue());
485         connData.username = txtUsername->GetValue().ToStdString();
486         connData.password = txtPassword->GetValue().ToStdString();
487         connData.useSSL = chkUseSSL->GetValue() ? true : false;
489         CalDAV testConnection;
490         testConnection.SetupConnectionData(&connData);
492         /*CardDAV2 TestConnection(txtServerAddress->GetValue().ToStdString(),
493                 wxAtoi(txtServerPort->GetValue()),
494                 txtUsername->GetValue().ToStdString(),
495                 txtPassword->GetValue().ToStdString(),
496                 chkUseSSL->GetValue() ? true : false);*/
497         
498         // Test the connection.
499         
500         //testConnection.SetupConnectionObject();
501         CalDAVServerResult testConnectionResult = testConnection.Connect(false);
502         
503         // If server is using SSL, verify that the SSL connection is valid.
504         
505         if (testConnection.SSLVerify() == COSSL_UNABLETOVERIFY){
506 #if defined(__APPLE__)
507                 
508                 testConnection.BypassSSLVerification(true);
509                 
510                 CalDAVServerResult testConnection = testConnection.Connect(false);
511                 
512                 testConnection.BypassSSLVerification(false);
513                 
514                 int SSLResult = DisplayTrustPanel(&TestConnection);
515                 
516                 if (SSLResult != NSOKButton){
517                         
518                         lblServerConnResult->SetLabel(_("Failed"));
519                         lblServerResponse->SetLabel(_("Not applicable"));
520                         lblServerSSLResult->SetLabel(_("Used"));
521                         lblServerSSLValid->SetLabel(_("No"));
522                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + TestConnection.GetErrorMessage());
523                         btnPrevious->Enable(true);
524                         return;
525                         
526                 } else {
527                         
528                         // Evalulate the trust object.
529                         
530                         SecTrustResultType evalResult = ProcessResultType(&testConnection);
531                         
532                         switch(evalResult){
533                                 case kSecTrustResultProceed:
534                                         lblServerSSLValid->SetLabel(_("Verified"));
535                                         break;
536                                 case kSecTrustResultConfirm:
537                                         lblServerSSLValid->SetLabel(_("Verified (user)"));
538                                         break;
539                                 default:
540                                         lblServerSSLValid->SetLabel(_("Unable to verify"));
541                         }
542                         
543                         lblServerResponse->SetLabel(_("Not applicable"));
544                         lblServerSSLResult->SetLabel(_("Used"));
545                         
546                         if (evalResult != kSecTrustResultProceed){
547                                 return;
548                         }
549                         
550                 }
551                 
552 #elif defined(__WIN32__)
554                 testConnection.BypassSSLVerification(true);
556                 CalDAVServerResult testConnectionResult = testConnection.Connect(false);
558                 testConnection.BypassSSLVerification(false);
560                 BOOL modifiedCertificateData = false;
562                 CRYPTUI_VIEWCERTIFICATE_STRUCTW certificateDialogData = BuildCertificateData(&testConnection, (HWND)this->GetHandle());
564                 if (!CryptUIDlgViewCertificate(&certificateDialogData, &modifiedCertificateData)){
565                         wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
566                 }
568                 if (modifiedCertificateData == false){
570                         lblServerConnResult->SetLabel(_("Failed"));
571                         lblServerResponse->SetLabel(_("Not applicable"));
572                         lblServerSSLResult->SetLabel(_("Used"));
573                         lblServerSSLValid->SetLabel(_("No"));
574                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + testConnection.GetErrorMessage());
575                         btnPrevious->Enable(true);
576                         return;
578                 }
580 #else
581         
582                 // Connect again and fetch SSL certificate information.
583                 
584                 testConnection.BypassSSLVerification(true);
585                 
586                 CalDAVServerResult testConnectionResult = testConnection.Connect(false);
587                 
588                 testConnection.BypassSSLVerification(false);
590                 SSLCertCollectionString certData = testConnection.BuildSSLCollection();
591                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
592                 
593                 frmICPtr->LoadDataNew(certData, txtServerAddress->GetValue().ToStdString());
594                 frmICPtr->ShowModal();
595                                                                 
596                 int sslResult = frmICPtr->GetResult();
597                                                         
598                 // Clean up before processing response.
599                                 
600                 delete frmICPtr;
601                 frmICPtr = nullptr;
602                                                         
603                 // Process the response from the user.
604                                                         
605                 if (sslResult == 1){
606                                                                 
607                         // Accept the Certificate.
608                         
609                         usingSSLBypass = true;
610                         testConnection.BypassSSLVerification(true);
611                 
612                         CalDAVServerResult testConnectionResult = testConnection.Connect(true);
613                 
614                         testConnection.BypassSSLVerification(false);
615                                                                 
616                 } else if (sslResult == 2){
617                                                                 
618                         // Reject the certificate, abort the task and mark as failed.
620                         // TODO: Integrate into the code.
621                         
622                         //lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
623                         
624                         resultData->Connected = true;
625                         resultData->SSLStatus = true;
626                         resultData->SSLVerified = COSSL_UNABLETOVERIFY;
627                         resultData->ValidResponse = false;
628                         resultData->AuthPassed = false;
629                         resultData->CanProcess = false;
630                         resultData->ErrorMessage = _("Server is using self-signed certificate and was rejected.");
631                         
632                         wxCommandEvent resultsEvent(UPDATERESULTS);
633                         resultsEvent.SetClientData(resultData);
634                         wxPostEvent(this, resultsEvent);
635                         
636                         return;
637                                                 
638                 }               
639                 
640 #endif
641         }
642         
643         if (usingSSLBypass == true){
644                 testConnection.BypassSSLVerification(true);                     
645         }
646         
647         testConnectionResult = testConnection.Connect(true);
649         // Get the server prefix if the connection was successful.
650         
651         if (testConnectionResult.result == CALDAVQUERYRESULT_OK){
652                 
653                 std::string receivedServerPrefix;
654                 
655                 receivedServerPrefix = testConnection.GetUserPrincipal();
656                 serverPrefix = receivedServerPrefix;
657                 
658         }
659         
660         CalDAVServerSupport testConnectionSupport = testConnection.GetServerSupport();
661         
662         if (usingSSLBypass == true){
663                 testConnection.BypassSSLVerification(false);
664         }
665         
666         (testConnectionResult.result == CALDAVQUERYRESULT_OK || testConnectionResult.result == CALDAVQUERYRESULT_SSLFAILURE) ? 
667                 resultData->Connected = true : resultData->Connected = false;
668         
669         resultData->SSLStatus = testConnection.CanDoSSL();
670         resultData->SSLVerified = testConnection.SSLVerify();
671         resultData->ValidResponse = testConnection.HasValidResponse();
672         resultData->AuthPassed = testConnection.AbleToLogin();
673         resultData->CanProcess = testConnectionSupport.basicSupport;
674         resultData->ErrorMessage = testConnection.GetErrorMessage();
675         
676         // Post event back confirming the tests.
677         
678         wxCommandEvent resultsEvent(UPDATERESULTS);
679         resultsEvent.SetClientData(resultData);
680         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