Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added OS X support for frmNewAccount itself.
[xestiaab/.git] / source / frmNewAccount.cpp
1 // frmNewAccount.cpp - New Account form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmNewAccount.h"
20 #include <thread>
21 #include <cstdlib>
22 #include <wx/filefn.h>
23 #include <wx/fileconf.h>
25 #include "carddav/carddav.h"
26 #include "common/dirs.h"
27 #include "common/svrblist.h"
28 #include "frmInvalidSSLCertificate.h"
30 frmNewAccount::frmNewAccount( wxWindow* parent )
31 :
32 frmNewAccountADT( parent )
33 {
35         // Disable the previous button upon form creation.
37         btnPrevious->Disable();
38         
39 }
41 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
42 {
43     
44         // Check that the account name is valid.
45     
46         wxString CheckAccName = txtAccountName->GetValue();
47     
48         if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
49         
50                 btnNext->Disable();
51         
52         } else {
53         
54                 btnNext->Enable();
55         
56         }
57     
58 }
60 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
61 {
63         // Go to the previous page.
65         PageSeek--;
66     
67         if (PageSeek == 0){
68         
69                 // Currently at the Connection test screen.
70         
71                 tabConn->Hide();
72                 tabFinish->Hide();
73                 tabType->Show();
74                 szrNewAccount->RecalcSizes();
75         
76                 btnPrevious->Disable();
77                 btnNext->Enable();
78         
79         } else if (PageSeek == 1){
80         
81                 if (cmbServerType->GetCurrentSelection() == 0){
82             
83                         tabConn->Hide();
84                         tabFinish->Hide();
85                         tabType->Show();
86                         PageSeek = 0;
87                         btnPrevious->Disable();
88                         btnNext->Enable();
89                         btnNext->SetLabel(_("Next >"));
90                         return;
91             
92                 }
93         
94                 // Currently at the Finish screen.
95         
96                 tabType->Hide();
97                 tabConn->Show();
98                 tabFinish->Hide();
99                 szrNewAccount->RecalcSizes();
100         
101                 btnNext->SetLabel(_("Next >"));
102                 btnNext->Enable();
103         
104         }
105     
108 void frmNewAccount::ProcessNext( wxCommandEvent& event )
111         // Go to the next page or setup the new account.
113         PageSeek++;
114     
115         if (PageSeek == 1){
116         
117                 if (cmbServerType->GetCurrentSelection() == 0){
118             
119                         tabType->Hide();
120                         tabConn->Hide();
121                         tabFinish->Show();
122                         PageSeek = 2;
123                         btnPrevious->Enable();
124                         szrNewAccount->RecalcSizes();
125                         btnNext->Disable();
126                         btnNext->SetLabel(_("Finish"));
127                         return;
128             
129                 }
130         
131                 // Check if server address matches against the blacklist.
132                 // Bring up warning message if it does.
133                 
134                 if (CheckBlacklist(txtServerAddress->GetValue())){
135                 
136                         int MessageBoxResult = wxMessageBox(_("The server with the address given does not support the CardDAV 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);
137                 
138                         if (MessageBoxResult == wxNO){
139                                 PageSeek--;
140                                 return;
141                         }
142                         
143                 }
144                 
145                 btnNext->Disable();
146         
147                 bool ServerResult = FALSE;
148                 bool ServerAction = FALSE;
149                 bool UseSSL = TRUE;
150                 wxString ServerMessage;
151         
152                 // Connection test screen.
153         
154                 tabType->Hide();
155                 tabConn->Show();
156                 tabFinish->Hide();
157                 szrNewAccount->RecalcSizes();
158                 btnPrevious->Enable();
159         
160                 // Reset screen.
161         
162                 lblServerConnResult->SetLabel(wxT(""));
163                 lblServerResponse->SetLabel(wxT(""));
164                 lblServerSSLResult->SetLabel(wxT(""));
165                 lblServerSSLValid->SetLabel(wxT(""));
166                 lblAbleToLoginResult->SetLabel(wxT(""));
167                 lblCardDAVSupportResult->SetLabel(wxT(""));
168         
169                 // Spawn a thread and check if server supports CardDAV.
170         
171                 CardDAV CardDAVConn;
172         
173                 lblServerConnResult->SetLabel(_("Testing..."));
174         
175                 UseSSL = chkUseSSL->GetValue();
176                 CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
177                         wxAtoi(txtServerPort->GetValue()),
178                         txtUsername->GetValue(),
179                         txtPassword->GetValue(),
180                         UseSSL);
181                 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
182         
183                 // Verify SSL trust first before doing anything.
185                 if (UseSSL == TRUE){
187                         CURLcode sslcode = CardDAVConn.SSLVerifyTest();
189                         if (sslcode == CURLE_OK){
190                                 
193                         } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
195                                 // Certificate is more than likely a self-signed or
196                                 // expired certificate so display the invalid
197                                 // SSL certificate message.
199                                 // Setup the data to be sent in the wxPostEvent command.
201                                 // TODO: Port to OS X.
202                                 
203                                 int SSLResult;
204                                 
205 #if defined(__APPLE__)
206         
207                                 SSLResult = DisplayTrustPanel(&CardDAVConn);
209                                 if (SSLResult != NSOKButton){
210                                         
211                                         lblServerConnResult->SetLabel(_("Failed"));
212                                         lblServerResponse->SetLabel(_("Not applicable"));
213                                         lblServerSSLResult->SetLabel(_("Used"));
214                                         lblServerSSLValid->SetLabel(_("No"));
215                                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
216                                         return;
217                                         
218                                 } else {
219                                         
220                                         // Evalulate the trust object.
221                                         
222                                         SecTrustResultType EvalResult = ProcessResultType(&CardDAVConn);
223                                         
224                                         switch(EvalResult){
225                                                 case kSecTrustResultProceed:
226                                                         lblServerSSLValid->SetLabel(_("Verified"));
227                                                         break;
228                                                 case kSecTrustResultConfirm:
229                                                         lblServerSSLValid->SetLabel(_("Verified (user)"));
230                                                         break;
231                                                 default:
232                                                         lblServerSSLValid->SetLabel(_("Unable to verify"));
233                                         }
234                                         
235                                         lblServerResponse->SetLabel(_("Not applicable"));
236                                         lblServerSSLResult->SetLabel(_("Used"));
237                                         
238                                         if (EvalResult != kSecTrustResultProceed){
239                                                 return;
240                                         }
241                                         
242                                 }
243                                 
244 #else
245                         
246                                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
248                                 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
249                                 frmICPtr->ShowModal();
250                                                                 
251                                 SSLResult = frmICPtr->GetResult();
252                                                         
253                                 // Clean up before processing response.
254                                 
255                                 delete frmICPtr;
256                                 frmICPtr = NULL;
257                                                         
258                                 // Process the response from the user.
259                                                         
260                                 if (SSLResult == 1){
261                                                                 
262                                         // Accept the Certificate.
264                                         CardDAVConn.AllowSelfSignTest(TRUE);
265                                                                 
266                                 } else if (SSLResult == 2){
267                                                                 
268                                         // Reject the certificate, abort the task and mark as failed.
270                                         lblServerConnResult->SetLabel(_("Failed"));
271                                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
272                                         return;
273                                                                 
274                                 }
275                                 
276 #endif
278                         } else {
280                                 // Something else happened. Stop the process and
281                                 // display an error message instead.
283                                 CURLcode sslcode = CardDAVConn.SSLVerifyTest();
284                                 
285                                 lblServerConnResult->SetLabel(_("Failed"));
286                                 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
287                                 return;
289                         }
291                 }
293                 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
294         
295                 ConnTest.join();
296         
297                 if (ServerResult == FALSE){
298             
299                         lblServerConnResult->SetLabel(_("Failed"));
300                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage());
301                         return;
302                         
303                 } else {
304             
305                         lblServerConnResult->SetLabel(_("Connected"));
306             
307                 }
308         
309                 if (CardDAVConn.CanDoSSL() == TRUE){
310             
311                         lblServerSSLResult->SetLabel(_("Used"));
312             
313                 } else {
314             
315                         lblServerSSLResult->SetLabel(_("Not Used"));
316                         lblServerSSLValid->SetLabel(_("Not Applicable"));
317             
318                 }
319                 
320 #if defined(__APPLE__)
322                 // Evalulate the trust object.
323                 
324                 SecTrustResultType EvalResult = ProcessResultType(&CardDAVConn);
325                 
326                 switch(EvalResult){
327                         case kSecTrustResultProceed:
328                                 lblServerSSLValid->SetLabel(_("Verified"));
329                                 break;
330                         case kSecTrustResultConfirm:
331                                 lblServerSSLValid->SetLabel(_("Verified (user)"));
332                                 break;
333                         default:
334                                 lblServerSSLValid->SetLabel(_("Unable to verify"));
335                 }
336                 
337 #else
338                 
339                 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
340             
341                         lblServerSSLValid->SetLabel(_("Verified"));
342             
343                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){
344         
345                         lblServerSSLValid->SetLabel(_("Verified (user)"));
346         
347                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
348             
349                         lblServerSSLValid->SetLabel(_("Unable to verify"));
350             
351                 }
352                 
353 #endif
354                 
355                 if (CardDAVConn.CanDoCardDAV() == TRUE){
356             
357                         lblCardDAVSupportResult->SetLabel(_("Supported"));
358             
359                 } else {
360             
361                         lblCardDAVSupportResult->SetLabel(_("Unsupported"));
362                         SetErrorMessageLabel();
363                         return;
364             
365                 }
366         
367                 if (CardDAVConn.AbleToLogin() == TRUE){
368             
369                         lblAbleToLoginResult->SetLabel(_("Yes"));
370             
371                 } else {
372             
373                         lblAbleToLoginResult->SetLabel(_("No"));
374                         SetErrorMessageLabel();
375                         return;
376             
377                 }
378         
379                 // Get the address to process CardDAV requests.
380         
381                 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
382         
383                 if (CardDAVConn.HasValidResponse() == TRUE){
384             
385                         lblServerResponse->SetLabel(_("Yes"));
386             
387                 } else {
388             
389                         lblServerResponse->SetLabel(_("No"));
390                         SetErrorMessageLabel();
391                         return;
392             
393                 }
394         
395                 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
397                         CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
398             
399                         btnNext->Enable();
400             
401                         lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
402             
403                 } else {
404             
405                         SetErrorMessageLabel();
406             
407                 }
408         
409         } else if (PageSeek == 2){
410         
411                 // Finish screen.
412         
413                 tabType->Hide();
414                 tabConn->Hide();
415                 tabFinish->Show();
416                 szrNewAccount->RecalcSizes();
417         
418                 btnNext->Disable();
419                 btnNext->SetLabel(_("Finish"));
420         
421                 if (txtAccountName->IsEmpty() && PageSeek == 2){
422         
423                         btnNext->Disable();
424             
425                 } else {
426         
427                         btnNext->Enable();
428             
429                 }
430         
431         } else if (PageSeek == 3){
432         
433                 // Finished.
434         
435                 wxString XestiaABPrefDirectory;
436                 wxString XestiaABDirectory;
437                 wxString AccountSettingsFile;
438                 //wxFile ASFile;
439                 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
440                 bool DirectoryCreated = FALSE;
441         
442 #if defined(__HAIKU__)
443         
444                 //preffilename = wxT("noo");
445         
446 #elif defined(__WIN32__)
447         
448                 XestiaABPrefDirectory = GetUserPrefDir();
449                 XestiaABDirectory = GetUserDir();
450         
451                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
452         
453                 // Open the file for writing.
454         
455                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
456         
457                 // Check if account name already exists and return an error message
458                 // if this is the case.
459         
460                 wxString AccountName;
461                 long itemindex = 0;
462                 bool ContinueAcc;
463                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
464         
465                 while (ContinueAcc){
466             
467                         if (txtAccountName->GetValue() == AccountName){
468                 
469                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
470                                 return;
471                 
472                         }
473             
474                         cfgfile->SetPath(wxT("/"));
475                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
476             
477                 }
478         
479                 if (cmbServerType->GetCurrentSelection() == 1){
480             
481                         // Create the account directory.
482             
483                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
484             
485                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
486                 
487                                 DirectoryCreated = TRUE;
488                 
489                         }
490             
491                         if (DirectoryCreated == TRUE){
492                 
493                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
494                 
495                         } else {
496                 
497                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
498                                 return;
499                 
500                         }
501             
502                 } else if (cmbServerType->GetCurrentSelection() == 0){
503             
504                         // Create the account directory.
505             
506                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
507             
508                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
509                 
510                                 DirectoryCreated = TRUE;
511                 
512                         }
513             
514                         if (DirectoryCreated == TRUE){
515                 
516                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
517                 
518                         } else {
519                 
520                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
521                                 return;
522                 
523                         }
524             
525                 }
526         
527                 delete cfgfile;
528                 cfgfile = NULL;
529         
530                 *ReloadAccountConfig = TRUE;
531         
532 #else
533         
534                 XestiaABPrefDirectory = GetUserPrefDir();
535                 XestiaABDirectory = GetUserDir();
536         
537                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
538         
539                 // Open the file for writing.
540         
541                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
542         
543                 // Check if account name already exists and return an error message
544                 // if this is the case.
545         
546                 wxString AccountName;
547                 long itemindex = 0;
548                 bool ContinueAcc;
549                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
550         
551                 while (ContinueAcc){
552             
553                         if (txtAccountName->GetValue() == AccountName){
554                 
555                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
556                                 return;
557                 
558                         }
559             
560                         cfgfile->SetPath(wxT("/"));
561                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
562             
563                 }
564         
565                 if (cmbServerType->GetCurrentSelection() == 1){
566             
567                         // Create the account directory.
568             
569                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
570             
571                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
572                 
573                                 DirectoryCreated = TRUE;
574                 
575                         }
576             
577                         if (DirectoryCreated == TRUE){
578                 
579                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
580                 
581                         } else {
582                 
583                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
584                                 return;
585                 
586                         }
587             
588                 } else if (cmbServerType->GetCurrentSelection() == 0){
589             
590                         // Create the account directory.
591             
592                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
593             
594                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
595                 
596                                 DirectoryCreated = TRUE;
597                 
598                         }
599             
600                         if (DirectoryCreated == TRUE){
601                 
602                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
603                 
604                         } else {
605                 
606                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
607                                 return;
608                 
609                         }
610             
611                 }
612         
613                 delete cfgfile;
614                 cfgfile = NULL;
615         
616                 *ReloadAccountConfig = TRUE;
617         
618 #endif
619         
620                 this->Close();
621         
622         }
623     
626 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
627     
628         // Write the new account details.
629     
630         cfgfilein->SetPath(txtAccountName->GetValue());
631         cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
632         cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
633         cfgfilein->Write(wxT("username"), txtUsername->GetValue());
634         cfgfilein->Write(wxT("password"), txtPassword->GetValue());
635         cfgfilein->Write(wxT("prefix"), ServerPrefix);
636         cfgfilein->Write(wxT("accountdir"), DirectoryName);
637     
638         if (chkUseSSL->GetValue() == TRUE){
639         
640                 cfgfilein->Write(wxT("ssl"), wxT("true"));
641         
642         } else {
643         
644                 cfgfilein->Write(wxT("ssl"), wxT("false"));
645         
646         }
647     
648         cfgfilein->Write(wxT("refresh"), wxT("1800"));
649         cfgfilein->Write(wxT("type"), AccountType);
650     
653 void frmNewAccount::CloseWindow( wxCommandEvent& event )
656         // Close the window.
658         *ReloadAccountConfig = FALSE;
659         this->Close();
660         
663 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
665     
666         // Update the options.
667     
668         if (cmbServerType->GetCurrentSelection() == 1){
669         
670                 txtServerAddress->Enable();
671                 txtServerPort->Enable();
672                 txtUsername->Enable();
673                 txtPassword->Enable();
674                 chkUseSSL->Enable();
675         
676         } else if (cmbServerType->GetCurrentSelection() == 0){
677         
678                 txtServerAddress->Disable();
679                 txtServerPort->Disable();
680                 txtUsername->Disable();
681                 txtPassword->Disable();
682                 chkUseSSL->Disable();
683         
684         }
685     
688 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
690         // Setup the pointers for the new account window.
691    
692         ReloadAccountConfig = ReloadAccountInc;
693     
696 void frmNewAccount::SetErrorMessageLabel(){
697         
698         lblConnectionResultText->SetLabel(_("A problem has occured whilst connecting to the CardDAV server.\nPlease review the above information and change the server details if needed.\nIf there are still problems, please speak to your system administrator(s)."));
699         
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