Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added more indentation and removed commented out code in frmNewAccount
[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 "frmInvalidSSLCertificate.h"
29 frmNewAccount::frmNewAccount( wxWindow* parent )
30 :
31 frmNewAccountADT( parent )
32 {
34         // Disable the previous button upon form creation.
36         btnPrevious->Disable();
37         
38 }
40 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
41 {
42     
43         // Check that the account name is valid.
44     
45         wxString CheckAccName = txtAccountName->GetValue();
46     
47         if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
48         
49                 btnNext->Disable();
50         
51         } else {
52         
53                 btnNext->Enable();
54         
55         }
56     
57 }
59 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
60 {
62         // Go to the previous page.
64         PageSeek--;
65     
66         if (PageSeek == 0){
67         
68                 // Currently at the Connection test screen.
69         
70                 tabConn->Hide();
71                 tabFinish->Hide();
72                 tabType->Show();
73                 szrNewAccount->RecalcSizes();
74         
75                 btnPrevious->Disable();
76                 btnNext->Enable();
77         
78         } else if (PageSeek == 1){
79         
80                 if (cmbServerType->GetCurrentSelection() == 0){
81             
82                         tabConn->Hide();
83                         tabFinish->Hide();
84                         tabType->Show();
85                         PageSeek = 0;
86                         btnPrevious->Disable();
87                         btnNext->Enable();
88                         btnNext->SetLabel(_("Next >"));
89                         return;
90             
91                 }
92         
93                 // Currently at the Finish screen.
94         
95                 tabType->Hide();
96                 tabConn->Show();
97                 tabFinish->Hide();
98                 szrNewAccount->RecalcSizes();
99         
100                 btnNext->SetLabel(_("Next >"));
101                 btnNext->Enable();
102         
103         }
104     
107 void frmNewAccount::ProcessNext( wxCommandEvent& event )
110         // Go to the next page or setup the new account.
112         PageSeek++;
113     
114         if (PageSeek == 1){
115         
116                 if (cmbServerType->GetCurrentSelection() == 0){
117             
118                         tabType->Hide();
119                         tabConn->Hide();
120                         tabFinish->Show();
121                         PageSeek = 2;
122                         btnPrevious->Enable();
123                         szrNewAccount->RecalcSizes();
124                         btnNext->Disable();
125                         btnNext->SetLabel(_("Finish"));
126                         return;
127             
128                 }
129         
130                 btnNext->Disable();
131         
132                 bool ServerResult = FALSE;
133                 bool ServerAction = FALSE;
134                 bool UseSSL = TRUE;
135                 wxString ServerMessage;
136         
137                 // Connection test screen.
138         
139                 tabType->Hide();
140                 tabConn->Show();
141                 tabFinish->Hide();
142                 szrNewAccount->RecalcSizes();
143                 btnPrevious->Enable();
144         
145                 // Reset screen.
146         
147                 lblServerConnResult->SetLabel(wxT(""));
148                 lblServerResponse->SetLabel(wxT(""));
149                 lblServerSSLResult->SetLabel(wxT(""));
150                 lblServerSSLValid->SetLabel(wxT(""));
151                 lblAbleToLoginResult->SetLabel(wxT(""));
152                 lblCardDAVSupportResult->SetLabel(wxT(""));
153         
154                 // Spawn a thread and check if server supports CardDAV.
155         
156                 CardDAV CardDAVConn;
157         
158                 lblServerConnResult->SetLabel(_("Testing..."));
159         
160                 UseSSL = chkUseSSL->GetValue();
161                 CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
162                         wxAtoi(txtServerPort->GetValue()),
163                         txtUsername->GetValue(),
164                         txtPassword->GetValue(),
165                         UseSSL);
166                 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
167         
168                 // Verify SSL trust first before doing anything.
170                 if (UseSSL == TRUE){
172                         CURLcode sslcode = CardDAVConn.SSLVerifyTest();
174                         if (sslcode == CURLE_OK){
175                                 
178                         } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
180                                 // Certificate is more than likely a self-signed or
181                                 // expired certificate so display the invalid
182                                 // SSL certificate message.
184                                 // Setup the data to be sent in the wxPostEvent command.
186                                 int SSLResult;
187                         
188                                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
190                                 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
191                                 frmICPtr->ShowModal();
192                                                                 
193                                 SSLResult = frmICPtr->GetResult();
194                                                         
195                                 // Clean up before processing response.
196                                 
197                                 delete frmICPtr;
198                                 frmICPtr = NULL;
199                                                         
200                                 // Process the response from the user.
201                                                         
202                                 if (SSLResult == 1){
203                                                                 
204                                         // Accept the Certificate.
206                                         CardDAVConn.AllowSelfSignTest(TRUE);
207                                                                 
208                                 } else if (SSLResult == 2){
209                                                                 
210                                         // Reject the certificate, abort the task and mark as failed.
212                                         lblServerConnResult->SetLabel(_("Failed"));
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                                 }
218                         } else {
220                                 // Something else happened. Stop the process and
221                                 // display an error message instead.
223                                 lblServerConnResult->SetLabel(_("Failed"));
224                                 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
225                                 return;
227                         }
229                 }
231                 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
232         
233                 ConnTest.join();
234         
235                 if (ServerResult == FALSE){
236             
237                         lblServerConnResult->SetLabel(_("Failed"));
238                         return;
239             
240                 } else {
241             
242                         lblServerConnResult->SetLabel(_("Connected"));
243             
244                 }
245         
246                 if (CardDAVConn.CanDoSSL() == TRUE){
247             
248                         lblServerSSLResult->SetLabel(_("Used"));
249             
250                 } else {
251             
252                         lblServerSSLResult->SetLabel(_("Not Used"));
253                         lblServerSSLValid->SetLabel(_("Not Applicable"));
254             
255                 }
256         
257                 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
258             
259                         lblServerSSLValid->SetLabel(_("Verified"));
260             
261                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){
262         
263                         lblServerSSLValid->SetLabel(_("Verified (user)"));
264         
265                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
266             
267                         lblServerSSLValid->SetLabel(_("Unable to verify"));
268             
269                 }       
270         
271                 if (CardDAVConn.CanDoCardDAV() == TRUE){
272             
273                         lblCardDAVSupportResult->SetLabel(_("Supported"));
274             
275                 } else {
276             
277                         lblCardDAVSupportResult->SetLabel(_("Unsupported"));
278             
279                 }
280         
281                 if (CardDAVConn.AbleToLogin() == TRUE){
282             
283                         lblAbleToLoginResult->SetLabel(_("Yes"));
284             
285                 } else {
286             
287                         lblAbleToLoginResult->SetLabel(_("No"));
288             
289                 }
290         
291                 // Get the address to process CardDAV requests.
292         
293                 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
294         
295                 if (CardDAVConn.HasValidResponse() == TRUE){
296             
297                         lblServerResponse->SetLabel(_("Yes"));
298             
299                 } else {
300             
301                         lblServerResponse->SetLabel(_("No"));
302             
303                 }
304         
305                 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
307                         CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
308             
309                         btnNext->Enable();
310             
311                         lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
312             
313                 } else {
314             
315                         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)."));
316             
317                 }
318         
319         } else if (PageSeek == 2){
320         
321                 // Finish screen.
322         
323                 tabType->Hide();
324                 tabConn->Hide();
325                 tabFinish->Show();
326                 szrNewAccount->RecalcSizes();
327         
328                 btnNext->Disable();
329                 btnNext->SetLabel(_("Finish"));
330         
331                 if (txtAccountName->IsEmpty() && PageSeek == 2){
332         
333                         btnNext->Disable();
334             
335                 } else {
336         
337                         btnNext->Enable();
338             
339                 }
340         
341         } else if (PageSeek == 3){
342         
343                 // Finished.
344         
345                 wxString XestiaABPrefDirectory;
346                 wxString XestiaABDirectory;
347                 wxString AccountSettingsFile;
348                 //wxFile ASFile;
349                 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
350                 bool DirectoryCreated = FALSE;
351         
352 #if defined(__HAIKU__)
353         
354                 //preffilename = wxT("noo");
355         
356 #elif defined(__WIN32__)
357         
358                 XestiaABPrefDirectory = GetUserPrefDir();
359                 XestiaABDirectory = GetUserDir();
360         
361                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
362         
363                 // Open the file for writing.
364         
365                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
366         
367                 // Check if account name already exists and return an error message
368                 // if this is the case.
369         
370                 wxString AccountName;
371                 long itemindex = 0;
372                 bool ContinueAcc;
373                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
374         
375                 while (ContinueAcc){
376             
377                         if (txtAccountName->GetValue() == AccountName){
378                 
379                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
380                                 return;
381                 
382                         }
383             
384                         cfgfile->SetPath(wxT("/"));
385                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
386             
387                 }
388         
389                 if (cmbServerType->GetCurrentSelection() == 1){
390             
391                         // Create the account directory.
392             
393                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
394             
395                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
396                 
397                                 DirectoryCreated = TRUE;
398                 
399                         }
400             
401                         if (DirectoryCreated == TRUE){
402                 
403                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
404                 
405                         } else {
406                 
407                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
408                                 return;
409                 
410                         }
411             
412                 } else if (cmbServerType->GetCurrentSelection() == 0){
413             
414                         // Create the account directory.
415             
416                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
417             
418                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
419                 
420                                 DirectoryCreated = TRUE;
421                 
422                         }
423             
424                         if (DirectoryCreated == TRUE){
425                 
426                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
427                 
428                         } else {
429                 
430                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
431                                 return;
432                 
433                         }
434             
435                 }
436         
437                 delete cfgfile;
438                 cfgfile = NULL;
439         
440                 *ReloadAccountConfig = TRUE;
441         
442 #else
443         
444                 XestiaABPrefDirectory = GetUserPrefDir();
445                 XestiaABDirectory = GetUserDir();
446         
447                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
448         
449                 // Open the file for writing.
450         
451                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
452         
453                 // Check if account name already exists and return an error message
454                 // if this is the case.
455         
456                 wxString AccountName;
457                 long itemindex = 0;
458                 bool ContinueAcc;
459                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
460         
461                 while (ContinueAcc){
462             
463                         if (txtAccountName->GetValue() == AccountName){
464                 
465                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
466                                 return;
467                 
468                         }
469             
470                         cfgfile->SetPath(wxT("/"));
471                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
472             
473                 }
474         
475                 if (cmbServerType->GetCurrentSelection() == 1){
476             
477                         // Create the account directory.
478             
479                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
480             
481                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
482                 
483                                 DirectoryCreated = TRUE;
484                 
485                         }
486             
487                         if (DirectoryCreated == TRUE){
488                 
489                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
490                 
491                         } else {
492                 
493                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
494                                 return;
495                 
496                         }
497             
498                 } else if (cmbServerType->GetCurrentSelection() == 0){
499             
500                         // Create the account directory.
501             
502                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
503             
504                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
505                 
506                                 DirectoryCreated = TRUE;
507                 
508                         }
509             
510                         if (DirectoryCreated == TRUE){
511                 
512                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
513                 
514                         } else {
515                 
516                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
517                                 return;
518                 
519                         }
520             
521                 }
522         
523                 delete cfgfile;
524                 cfgfile = NULL;
525         
526                 *ReloadAccountConfig = TRUE;
527         
528 #endif
529         
530                 this->Close();
531         
532         }
533     
536 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
537     
538         // Write the new account details.
539     
540         cfgfilein->SetPath(txtAccountName->GetValue());
541         cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
542         cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
543         cfgfilein->Write(wxT("username"), txtUsername->GetValue());
544         cfgfilein->Write(wxT("password"), txtPassword->GetValue());
545         cfgfilein->Write(wxT("prefix"), ServerPrefix);
546         cfgfilein->Write(wxT("accountdir"), DirectoryName);
547     
548         if (chkUseSSL->GetValue() == TRUE){
549         
550                 cfgfilein->Write(wxT("ssl"), wxT("true"));
551         
552         } else {
553         
554                 cfgfilein->Write(wxT("ssl"), wxT("false"));
555         
556         }
557     
558         cfgfilein->Write(wxT("refresh"), wxT("1800"));
559         cfgfilein->Write(wxT("type"), AccountType);
560     
563 void frmNewAccount::CloseWindow( wxCommandEvent& event )
566         // Close the window.
568         *ReloadAccountConfig = FALSE;
569         this->Close();
570         
573 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
575     
576         // Update the options.
577     
578         if (cmbServerType->GetCurrentSelection() == 1){
579         
580                 txtServerAddress->Enable();
581                 txtServerPort->Enable();
582                 txtUsername->Enable();
583                 txtPassword->Enable();
584                 chkUseSSL->Enable();
585         
586         } else if (cmbServerType->GetCurrentSelection() == 0){
587         
588                 txtServerAddress->Disable();
589                 txtServerPort->Disable();
590                 txtUsername->Disable();
591                 txtPassword->Disable();
592                 chkUseSSL->Disable();
593         
594         }
595     
598 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
600         // Setup the pointers for the new account window.
601    
602         ReloadAccountConfig = ReloadAccountInc;
603     
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