Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Updated build system.
[xestiaab/.git] / source / frmNewAccount.cpp
1 #include "frmNewAccount.h"
2 #include <thread>
3 #include <cstdlib>
4 #include <wx/filefn.h>
5 #include <wx/fileconf.h>
7 #include "carddav/carddav.h"
8 #include "common/dirs.h"
9 #include "frmInvalidSSLCertificate.h"
11 frmNewAccount::frmNewAccount( wxWindow* parent )
12 :
13 frmNewAccountADT( parent )
14 {
15     btnPrevious->Disable();
16 }
18 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
19 {
20     
21     wxString CheckAccName = txtAccountName->GetValue();
22     
23     if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
24         
25         btnNext->Disable();
26         
27     } else {
28         
29         btnNext->Enable();
30         
31     }
32     
33 }
35 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
36 {
37     PageSeek--;
38     
39     if (PageSeek == 0){
40         
41         // Currently at the Connection test screen.
42         
43         //tbkAccount->ChangeSelection(0);
44         tabConn->Hide();
45         tabFinish->Hide();
46         tabType->Show();
47         szrNewAccount->RecalcSizes();
48         
49         btnPrevious->Disable();
50         btnNext->Enable();
51         
52     } else if (PageSeek == 1){
53         
54         if (cmbServerType->GetCurrentSelection() == 0){
55             
56             tabConn->Hide();
57             tabFinish->Hide();
58             tabType->Show();
59             PageSeek = 0;
60             btnPrevious->Disable();
61             btnNext->Enable();
62             btnNext->SetLabel(_("Next >"));
63             return;
64             
65         }
66         
67         // Currently at the Finish screen.
68         
69         tabType->Hide();
70         tabConn->Show();
71         tabFinish->Hide();
72         szrNewAccount->RecalcSizes();
73         
74         btnNext->SetLabel(_("Next >"));
75         btnNext->Enable();
76         
77     }
78     
79 }
81 void frmNewAccount::ProcessNext( wxCommandEvent& event )
82 {
83     PageSeek++;
84     
85     if (PageSeek == 1){
86         
87         if (cmbServerType->GetCurrentSelection() == 0){
88             
89             tabType->Hide();
90             tabConn->Hide();
91             tabFinish->Show();
92             PageSeek = 2;
93             btnPrevious->Enable();
94             szrNewAccount->RecalcSizes();
95             btnNext->Disable();
96             btnNext->SetLabel(_("Finish"));
97             return;
98             
99         }
100         
101         btnNext->Disable();
102         
103         bool ServerResult = FALSE;
104         bool ServerAction = FALSE;
105         bool UseSSL = TRUE;
106         wxString ServerMessage;
107         
108         // Connection test screen.
109         
110         tabType->Hide();
111         tabConn->Show();
112         tabFinish->Hide();
113         szrNewAccount->RecalcSizes();
114         btnPrevious->Enable();
115         
116         // Reset screen.
117         
118         lblServerConnResult->SetLabel(wxT(""));
119         lblServerResponse->SetLabel(wxT(""));
120         lblServerSSLResult->SetLabel(wxT(""));
121         lblServerSSLValid->SetLabel(wxT(""));
122         lblAbleToLoginResult->SetLabel(wxT(""));
123         lblCardDAVSupportResult->SetLabel(wxT(""));
124         
125         // Spawn a thread and check if server supports CardDAV.
126         
127         CardDAV CardDAVConn;
128         
129         lblServerConnResult->SetLabel(_("Testing..."));
130         
131         UseSSL = chkUseSSL->GetValue();
132         CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
133                                     wxAtoi(txtServerPort->GetValue()),
134                                     txtUsername->GetValue(),
135                                     txtPassword->GetValue(),
136                                     UseSSL);
137         CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
138         
139                 // Verify SSL trust first before doing anything.
141                 if (UseSSL == TRUE){
143                         CURLcode sslcode = CardDAVConn.SSLVerifyTest();
145                         if (sslcode == CURLE_OK){
146                                 
149                         } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
151                                 // Certificate is more than likely a self-signed or
152                                 // expired certificate so display the invalid
153                                 // SSL certificate message.
155                                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
157                                 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
158                                 frmICPtr->ShowModal();
160                         } else {
162                                 // Something else happened. Stop the process and
163                                 // display an error message instead.
165                                 lblServerConnResult->SetLabel(_("Failed"));
166                                 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
167                                 return;
169                         }
171                 }
173         std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
174         
175         ConnTest.join();
176         
177         if (ServerResult == FALSE){
178             
179             lblServerConnResult->SetLabel(_("Failed"));
180             wxMessageBox(CardDAVConn.GetErrorMessage());
181             return;
182             
183         } else {
184             
185             lblServerConnResult->SetLabel(_("Connected"));
186             
187         }
188         
189         if (CardDAVConn.HasValidResponse() == TRUE){
190             
191             lblServerResponse->SetLabel(_("Yes"));
192             
193         } else {
194             
195             lblServerResponse->SetLabel(_("No"));
196             
197         }
198         
199         if (CardDAVConn.CanDoSSL() == TRUE){
200             
201             lblCardDAVSupportResult->SetLabel(_("Used"));
202             
203         } else {
204             
205             lblServerSSLResult->SetLabel(_("Not Used"));
206             lblServerSSLValid->SetLabel(_("Not Applicable"));
207             
208         }
209         
210         if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
211             
212             lblCardDAVSupportResult->SetLabel(_("Verified"));
213             
214         } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
215             
216             lblServerSSLResult->SetLabel(_("Unable to verify"));
217             
218         }
219         
220         if (CardDAVConn.CanDoCardDAV() == TRUE){
221             
222             lblCardDAVSupportResult->SetLabel(_("Supported"));
223             
224         } else {
225             
226             lblCardDAVSupportResult->SetLabel(_("Unsupported"));
227             
228         }
229         
230         if (CardDAVConn.AbleToLogin() == TRUE){
231             
232             lblAbleToLoginResult->SetLabel(_("Yes"));
233             
234         } else {
235             
236             lblAbleToLoginResult->SetLabel(_("No"));
237             
238         }
239         
240         // Get the address to process CardDAV requests.
241         
242         ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
243         
244         wxMessageBox(ServerPrefix);
245         
246         if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
247             //CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.SSLVerify() == TRUE &&
248             CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
249             
250             btnNext->Enable();
251             
252             lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
253             
254         } else {
255             
256             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)."));
257             
258         }
259         
260     } else if (PageSeek == 2){
261         
262         // Finish screen.
263         
264         tabType->Hide();
265         tabConn->Hide();
266         tabFinish->Show();
267         szrNewAccount->RecalcSizes();
268         
269         btnNext->Disable();
270         btnNext->SetLabel(_("Finish"));
271         if (txtAccountName->IsEmpty() && PageSeek == 2){
272             btnNext->Disable();
273         } else {
274             btnNext->Enable();
275         }
276         
277     } else if (PageSeek == 3){
278         
279         // Finished.
280         
281         wxString XestiaABPrefDirectory;
282         wxString AccountSettingsFile;
283         //wxFile ASFile;
284         wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
285         bool DirectoryCreated = FALSE;
286         
287 #if defined(__HAIKU__)
288         
289         //preffilename = wxT("noo");
290         
291 #elif defined(__WIN32__)
292         
293         XestiaABPrefDirectory = GetUserDir();
294         
295         AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
296         
297         // Open the file for writing.
298         
299         wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
300         
301         // Check if account name already exists and return an error message
302         // if this is the case.
303         
304         wxString AccountName;
305         long itemindex = 0;
306         bool ContinueAcc;
307         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
308         
309         while (ContinueAcc){
310             
311             if (txtAccountName->GetValue() == AccountName){
312                 
313                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
314                 return;
315                 
316             }
317             
318             cfgfile->SetPath(wxT("/"));
319             ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
320             
321         }
322         
323         if (cmbServerType->GetCurrentSelection() == 1){
324             
325             // Create the account directory.
326             
327             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
328             
329             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
330                 
331                 DirectoryCreated = TRUE;
332                 
333             }
334             
335             if (DirectoryCreated == TRUE){
336                 
337                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
338                 
339             } else {
340                 
341                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
342                 return;
343                 
344             }
345             
346         } else if (cmbServerType->GetCurrentSelection() == 0){
347             
348             // Create the account directory.
349             
350             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
351             
352             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
353                 
354                 DirectoryCreated = TRUE;
355                 
356             }
357             
358             if (DirectoryCreated == TRUE){
359                 
360                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
361                 
362             } else {
363                 
364                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
365                 return;
366                 
367             }
368             
369         }
370         
371         delete cfgfile;
372         cfgfile = NULL;
373         
374         ReloadAccountConfig = TRUE;
375         
376 #else
377         
378         XestiaABPrefDirectory = GetUserDir();
379         
380         AccountSettingsFile = GetUserPrefDir() + wxT("accounts");
381         
382         // Open the file for writing.
383         
384         wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
385         
386         // Check if account name already exists and return an error message
387         // if this is the case.
388         
389         wxString AccountName;
390         long itemindex = 0;
391         bool ContinueAcc;
392         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
393         
394         while (ContinueAcc){
395             
396             if (txtAccountName->GetValue() == AccountName){
397                 
398                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
399                 return;
400                 
401             }
402             
403             cfgfile->SetPath(wxT("/"));
404             ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
405             
406         }
407         
408         if (cmbServerType->GetCurrentSelection() == 1){
409             
410             // Create the account directory.
411             
412             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
413             
414             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
415                 
416                 DirectoryCreated = TRUE;
417                 
418             }
419             
420             if (DirectoryCreated == TRUE){
421                 
422                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
423                 
424             } else {
425                 
426                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
427                 return;
428                 
429             }
430             
431         } else if (cmbServerType->GetCurrentSelection() == 0){
432             
433             // Create the account directory.
434             
435             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
436             
437             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
438                 
439                 DirectoryCreated = TRUE;
440                 
441             }
442             
443             if (DirectoryCreated == TRUE){
444                 
445                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
446                 
447             } else {
448                 
449                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
450                 return;
451                 
452             }
453             
454         }
455         
456         delete cfgfile;
457         cfgfile = NULL;
458         
459         ReloadAccountConfig = TRUE;
460         
461 #endif
462         
463         this->Close();
464         
465     }
466     
469 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
470     
471     cfgfilein->SetPath(txtAccountName->GetValue());
472     cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
473     cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
474     cfgfilein->Write(wxT("username"), txtUsername->GetValue());
475     cfgfilein->Write(wxT("password"), txtPassword->GetValue());
476     cfgfilein->Write(wxT("prefix"), ServerPrefix);
477     cfgfilein->Write(wxT("accountdir"), DirectoryName);
478     
479     if (chkUseSSL->GetValue() == TRUE){
480         
481         cfgfilein->Write(wxT("ssl"), wxT("true"));
482         
483     } else {
484         
485         cfgfilein->Write(wxT("ssl"), wxT("false"));
486         
487     }
488     
489     cfgfilein->Write(wxT("refresh"), wxT("1800"));
490     cfgfilein->Write(wxT("type"), AccountType);
491     
494 void frmNewAccount::CloseWindow( wxCommandEvent& event )
496     ReloadAccountConfig = FALSE;
497     this->Close();
500 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
502     
503     // Update the options.
504     
505     if (cmbServerType->GetCurrentSelection() == 1){
506         
507         txtServerAddress->Enable();
508         txtServerPort->Enable();
509         txtUsername->Enable();
510         txtPassword->Enable();
511         chkUseSSL->Enable();
512         
513     } else if (cmbServerType->GetCurrentSelection() == 0){
514         
515         txtServerAddress->Disable();
516         txtServerPort->Disable();
517         txtUsername->Disable();
518         txtPassword->Disable();
519         chkUseSSL->Disable();
520         
521     }
522     
525 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
526     
527     ReloadAccountConfig = ReloadAccountInc;
528     
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