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