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