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