Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Remove 'TODO: Port to OS X' message as this has been completed.
[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.
200                                 
201                                 int SSLResult;
202                                 
203 #if defined(__APPLE__)
204         
205                                 SSLResult = DisplayTrustPanel(&CardDAVConn);
207                                 if (SSLResult != NSOKButton){
208                                         
209                                         lblServerConnResult->SetLabel(_("Failed"));
210                                         lblServerResponse->SetLabel(_("Not applicable"));
211                                         lblServerSSLResult->SetLabel(_("Used"));
212                                         lblServerSSLValid->SetLabel(_("No"));
213                                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
214                                         return;
215                                         
216                                 } else {
217                                         
218                                         // Evalulate the trust object.
219                                         
220                                         SecTrustResultType EvalResult = ProcessResultType(&CardDAVConn);
221                                         
222                                         switch(EvalResult){
223                                                 case kSecTrustResultProceed:
224                                                         lblServerSSLValid->SetLabel(_("Verified"));
225                                                         break;
226                                                 case kSecTrustResultConfirm:
227                                                         lblServerSSLValid->SetLabel(_("Verified (user)"));
228                                                         break;
229                                                 default:
230                                                         lblServerSSLValid->SetLabel(_("Unable to verify"));
231                                         }
232                                         
233                                         lblServerResponse->SetLabel(_("Not applicable"));
234                                         lblServerSSLResult->SetLabel(_("Used"));
235                                         
236                                         if (EvalResult != kSecTrustResultProceed){
237                                                 return;
238                                         }
239                                         
240                                 }
241                                 
242 #else
243                         
244                                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
246                                 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
247                                 frmICPtr->ShowModal();
248                                                                 
249                                 SSLResult = frmICPtr->GetResult();
250                                                         
251                                 // Clean up before processing response.
252                                 
253                                 delete frmICPtr;
254                                 frmICPtr = NULL;
255                                                         
256                                 // Process the response from the user.
257                                                         
258                                 if (SSLResult == 1){
259                                                                 
260                                         // Accept the Certificate.
262                                         CardDAVConn.AllowSelfSignTest(TRUE);
263                                                                 
264                                 } else if (SSLResult == 2){
265                                                                 
266                                         // Reject the certificate, abort the task and mark as failed.
268                                         lblServerConnResult->SetLabel(_("Failed"));
269                                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
270                                         return;
271                                                                 
272                                 }
273                                 
274 #endif
276                         } else {
278                                 // Something else happened. Stop the process and
279                                 // display an error message instead.
281                                 CURLcode sslcode = CardDAVConn.SSLVerifyTest();
282                                 
283                                 lblServerConnResult->SetLabel(_("Failed"));
284                                 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
285                                 return;
287                         }
289                 }
291                 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
292         
293                 ConnTest.join();
294         
295                 if (ServerResult == FALSE){
296             
297                         lblServerConnResult->SetLabel(_("Failed"));
298                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage());
299                         return;
300                         
301                 } else {
302             
303                         lblServerConnResult->SetLabel(_("Connected"));
304             
305                 }
306         
307                 if (CardDAVConn.CanDoSSL() == TRUE){
308             
309                         lblServerSSLResult->SetLabel(_("Used"));
310             
311                 } else {
312             
313                         lblServerSSLResult->SetLabel(_("Not Used"));
314                         lblServerSSLValid->SetLabel(_("Not Applicable"));
315             
316                 }
317                 
318 #if defined(__APPLE__)
320                 // Evalulate the trust object.
321                 
322                 SecTrustResultType EvalResult = ProcessResultType(&CardDAVConn);
323                 
324                 switch(EvalResult){
325                         case kSecTrustResultProceed:
326                                 lblServerSSLValid->SetLabel(_("Verified"));
327                                 break;
328                         case kSecTrustResultConfirm:
329                                 lblServerSSLValid->SetLabel(_("Verified (user)"));
330                                 break;
331                         default:
332                                 lblServerSSLValid->SetLabel(_("Unable to verify"));
333                 }
334                 
335 #else
336                 
337                 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
338             
339                         lblServerSSLValid->SetLabel(_("Verified"));
340             
341                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){
342         
343                         lblServerSSLValid->SetLabel(_("Verified (user)"));
344         
345                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
346             
347                         lblServerSSLValid->SetLabel(_("Unable to verify"));
348             
349                 }
350                 
351 #endif
352                 
353                 if (CardDAVConn.CanDoCardDAV() == TRUE){
354             
355                         lblCardDAVSupportResult->SetLabel(_("Supported"));
356             
357                 } else {
358             
359                         lblCardDAVSupportResult->SetLabel(_("Unsupported"));
360                         SetErrorMessageLabel();
361                         return;
362             
363                 }
364         
365                 if (CardDAVConn.AbleToLogin() == TRUE){
366             
367                         lblAbleToLoginResult->SetLabel(_("Yes"));
368             
369                 } else {
370             
371                         lblAbleToLoginResult->SetLabel(_("No"));
372                         SetErrorMessageLabel();
373                         return;
374             
375                 }
376         
377                 // Get the address to process CardDAV requests.
378         
379                 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
380         
381                 if (CardDAVConn.HasValidResponse() == TRUE){
382             
383                         lblServerResponse->SetLabel(_("Yes"));
384             
385                 } else {
386             
387                         lblServerResponse->SetLabel(_("No"));
388                         SetErrorMessageLabel();
389                         return;
390             
391                 }
392         
393                 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
395                         CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
396             
397                         btnNext->Enable();
398             
399                         lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
400             
401                 } else {
402             
403                         SetErrorMessageLabel();
404             
405                 }
406         
407         } else if (PageSeek == 2){
408         
409                 // Finish screen.
410         
411                 tabType->Hide();
412                 tabConn->Hide();
413                 tabFinish->Show();
414                 szrNewAccount->RecalcSizes();
415         
416                 btnNext->Disable();
417                 btnNext->SetLabel(_("Finish"));
418         
419                 if (txtAccountName->IsEmpty() && PageSeek == 2){
420         
421                         btnNext->Disable();
422             
423                 } else {
424         
425                         btnNext->Enable();
426             
427                 }
428         
429         } else if (PageSeek == 3){
430         
431                 // Finished.
432         
433                 wxString XestiaABPrefDirectory;
434                 wxString XestiaABDirectory;
435                 wxString AccountSettingsFile;
436                 //wxFile ASFile;
437                 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
438                 bool DirectoryCreated = FALSE;
439         
440 #if defined(__HAIKU__)
441         
442                 //preffilename = wxT("noo");
443         
444 #elif defined(__WIN32__)
445         
446                 XestiaABPrefDirectory = GetUserPrefDir();
447                 XestiaABDirectory = GetUserDir();
448         
449                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
450         
451                 // Open the file for writing.
452         
453                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
454         
455                 // Check if account name already exists and return an error message
456                 // if this is the case.
457         
458                 wxString AccountName;
459                 long itemindex = 0;
460                 bool ContinueAcc;
461                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
462         
463                 while (ContinueAcc){
464             
465                         if (txtAccountName->GetValue() == AccountName){
466                 
467                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
468                                 return;
469                 
470                         }
471             
472                         cfgfile->SetPath(wxT("/"));
473                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
474             
475                 }
476         
477                 if (cmbServerType->GetCurrentSelection() == 1){
478             
479                         // Create the account directory.
480             
481                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
482             
483                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
484                 
485                                 DirectoryCreated = TRUE;
486                 
487                         }
488             
489                         if (DirectoryCreated == TRUE){
490                 
491                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
492                 
493                         } else {
494                 
495                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
496                                 return;
497                 
498                         }
499             
500                 } else if (cmbServerType->GetCurrentSelection() == 0){
501             
502                         // Create the account directory.
503             
504                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
505             
506                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
507                 
508                                 DirectoryCreated = TRUE;
509                 
510                         }
511             
512                         if (DirectoryCreated == TRUE){
513                 
514                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
515                 
516                         } else {
517                 
518                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
519                                 return;
520                 
521                         }
522             
523                 }
524         
525                 delete cfgfile;
526                 cfgfile = NULL;
527         
528                 *ReloadAccountConfig = TRUE;
529         
530 #else
531         
532                 XestiaABPrefDirectory = GetUserPrefDir();
533                 XestiaABDirectory = GetUserDir();
534         
535                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
536         
537                 // Open the file for writing.
538         
539                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
540         
541                 // Check if account name already exists and return an error message
542                 // if this is the case.
543         
544                 wxString AccountName;
545                 long itemindex = 0;
546                 bool ContinueAcc;
547                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
548         
549                 while (ContinueAcc){
550             
551                         if (txtAccountName->GetValue() == AccountName){
552                 
553                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
554                                 return;
555                 
556                         }
557             
558                         cfgfile->SetPath(wxT("/"));
559                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
560             
561                 }
562         
563                 if (cmbServerType->GetCurrentSelection() == 1){
564             
565                         // Create the account directory.
566             
567                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
568             
569                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
570                 
571                                 DirectoryCreated = TRUE;
572                 
573                         }
574             
575                         if (DirectoryCreated == TRUE){
576                 
577                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
578                 
579                         } else {
580                 
581                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
582                                 return;
583                 
584                         }
585             
586                 } else if (cmbServerType->GetCurrentSelection() == 0){
587             
588                         // Create the account directory.
589             
590                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
591             
592                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
593                 
594                                 DirectoryCreated = TRUE;
595                 
596                         }
597             
598                         if (DirectoryCreated == TRUE){
599                 
600                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
601                 
602                         } else {
603                 
604                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
605                                 return;
606                 
607                         }
608             
609                 }
610         
611                 delete cfgfile;
612                 cfgfile = NULL;
613         
614                 *ReloadAccountConfig = TRUE;
615         
616 #endif
617         
618                 this->Close();
619         
620         }
621     
624 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
625     
626         // Write the new account details.
627     
628         cfgfilein->SetPath(txtAccountName->GetValue());
629         cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
630         cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
631         cfgfilein->Write(wxT("username"), txtUsername->GetValue());
632         cfgfilein->Write(wxT("password"), txtPassword->GetValue());
633         cfgfilein->Write(wxT("prefix"), ServerPrefix);
634         cfgfilein->Write(wxT("accountdir"), DirectoryName);
635     
636         if (chkUseSSL->GetValue() == TRUE){
637         
638                 cfgfilein->Write(wxT("ssl"), wxT("true"));
639         
640         } else {
641         
642                 cfgfilein->Write(wxT("ssl"), wxT("false"));
643         
644         }
645     
646         cfgfilein->Write(wxT("refresh"), wxT("1800"));
647         cfgfilein->Write(wxT("type"), AccountType);
648     
651 void frmNewAccount::CloseWindow( wxCommandEvent& event )
654         // Close the window.
656         *ReloadAccountConfig = FALSE;
657         this->Close();
658         
661 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
663     
664         // Update the options.
665     
666         if (cmbServerType->GetCurrentSelection() == 1){
667         
668                 txtServerAddress->Enable();
669                 txtServerPort->Enable();
670                 txtUsername->Enable();
671                 txtPassword->Enable();
672                 chkUseSSL->Enable();
673         
674         } else if (cmbServerType->GetCurrentSelection() == 0){
675         
676                 txtServerAddress->Disable();
677                 txtServerPort->Disable();
678                 txtUsername->Disable();
679                 txtPassword->Disable();
680                 chkUseSSL->Disable();
681         
682         }
683     
686 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
688         // Setup the pointers for the new account window.
689    
690         ReloadAccountConfig = ReloadAccountInc;
691     
694 void frmNewAccount::SetErrorMessageLabel(){
695         
696         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)."));
697         
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