Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
In Win32 section, replace GetUserDir with GetUserPrefDir to get the correct directory.
[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                                 // Setup the data to be sent in the wxPostEvent command.
157                                 //SSLInvalidCertNotifObj SSLICNProcData;
159                                 //bool *PauseMode = new bool;
160                                 int SSLResult;
161                                 //QRNotif qrn;
163                                 //*PauseMode = TRUE;            
164                                 //qrn.QResponse = &SSLResult;
165                                 //qrn.PausePtr = PauseMode;
166                                 
167                                 //SSLICNProcData.CertCollection = CardDAVConn.GetSSLVerifyResults();
168                                 //SSLICNProcData.QRNotifData = &qrn;
169                                 //SSLICNProcData.AccountName = _("New account");
170                         
171                                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
173                                 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
174                                 frmICPtr->ShowModal();
175                         
176                                 //wxCommandEvent event(INVALIDSSLCERT);
177                                 //event.SetClientData(&SSLICNProcData);
178                                 //wxPostEvent(this->GetParent(), event);
179         
180                                 /*timespec n1, n2;
181                 
182                                 // Fall asleep until we get an response.
183                 
184                                 n1.tv_sec = 0;
185                                 n1.tv_nsec = 250000000L;*/
186                                         
187                                 SSLResult = frmICPtr->GetResult();
188                                                         
189                                 // Clean up before processing response.
190                                 
191                                 delete frmICPtr;
192                                 frmICPtr = NULL;
193                                                         
194                                 // Process the response from the user.
195                                                         
196                                 if (SSLResult == 1){
197                                                                 
198                                         // Accept the Certificate.
200                                         CardDAVConn.AllowSelfSignTest(TRUE);
201                                                                 
202                                 } else if (SSLResult == 2){
203                                                                 
204                                         // Reject the certificate, abort the task and mark as failed.
206                                         lblServerConnResult->SetLabel(_("Failed"));
207                                         lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
208                                         return;
209                                                                 
210                                 }
212                                 //frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
214                                 //frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
215                                 //frmICPtr->ShowModal();
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 &&
306             //CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.SSLVerify() == 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         if (txtAccountName->IsEmpty() && PageSeek == 2){
331             btnNext->Disable();
332         } else {
333             btnNext->Enable();
334         }
335         
336     } else if (PageSeek == 3){
337         
338         // Finished.
339         
340         wxString XestiaABPrefDirectory;
341         wxString AccountSettingsFile;
342         //wxFile ASFile;
343         wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
344         bool DirectoryCreated = FALSE;
345         
346 #if defined(__HAIKU__)
347         
348         //preffilename = wxT("noo");
349         
350 #elif defined(__WIN32__)
351         
352         XestiaABPrefDirectory = GetUserPrefDir();
353         
354         AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
355         
356         // Open the file for writing.
357         
358         wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
359         
360         // Check if account name already exists and return an error message
361         // if this is the case.
362         
363         wxString AccountName;
364         long itemindex = 0;
365         bool ContinueAcc;
366         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
367         
368         while (ContinueAcc){
369             
370             if (txtAccountName->GetValue() == AccountName){
371                 
372                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
373                 return;
374                 
375             }
376             
377             cfgfile->SetPath(wxT("/"));
378             ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
379             
380         }
381         
382         if (cmbServerType->GetCurrentSelection() == 1){
383             
384             // Create the account directory.
385             
386             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
387             
388             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
389                 
390                 DirectoryCreated = TRUE;
391                 
392             }
393             
394             if (DirectoryCreated == TRUE){
395                 
396                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
397                 
398             } else {
399                 
400                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
401                 return;
402                 
403             }
404             
405         } else if (cmbServerType->GetCurrentSelection() == 0){
406             
407             // Create the account directory.
408             
409             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
410             
411             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
412                 
413                 DirectoryCreated = TRUE;
414                 
415             }
416             
417             if (DirectoryCreated == TRUE){
418                 
419                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
420                 
421             } else {
422                 
423                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
424                 return;
425                 
426             }
427             
428         }
429         
430         delete cfgfile;
431         cfgfile = NULL;
432         
433         ReloadAccountConfig = TRUE;
434         
435 #else
436         
437         XestiaABPrefDirectory = GetUserDir();
438         
439         AccountSettingsFile = GetUserPrefDir() + wxT("accounts");
440         
441         // Open the file for writing.
442         
443         wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
444         
445         // Check if account name already exists and return an error message
446         // if this is the case.
447         
448         wxString AccountName;
449         long itemindex = 0;
450         bool ContinueAcc;
451         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
452         
453         while (ContinueAcc){
454             
455             if (txtAccountName->GetValue() == AccountName){
456                 
457                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
458                 return;
459                 
460             }
461             
462             cfgfile->SetPath(wxT("/"));
463             ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
464             
465         }
466         
467         if (cmbServerType->GetCurrentSelection() == 1){
468             
469             // Create the account directory.
470             
471             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
472             
473             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
474                 
475                 DirectoryCreated = TRUE;
476                 
477             }
478             
479             if (DirectoryCreated == TRUE){
480                 
481                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
482                 
483             } else {
484                 
485                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
486                 return;
487                 
488             }
489             
490         } else if (cmbServerType->GetCurrentSelection() == 0){
491             
492             // Create the account directory.
493             
494             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
495             
496             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
497                 
498                 DirectoryCreated = TRUE;
499                 
500             }
501             
502             if (DirectoryCreated == TRUE){
503                 
504                 WriteAccountDetails(cfgfile, wxT("Local"), 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         }
514         
515         delete cfgfile;
516         cfgfile = NULL;
517         
518         ReloadAccountConfig = TRUE;
519         
520 #endif
521         
522         this->Close();
523         
524     }
525     
528 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
529     
530     cfgfilein->SetPath(txtAccountName->GetValue());
531     cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
532     cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
533     cfgfilein->Write(wxT("username"), txtUsername->GetValue());
534     cfgfilein->Write(wxT("password"), txtPassword->GetValue());
535     cfgfilein->Write(wxT("prefix"), ServerPrefix);
536     cfgfilein->Write(wxT("accountdir"), DirectoryName);
537     
538     if (chkUseSSL->GetValue() == TRUE){
539         
540         cfgfilein->Write(wxT("ssl"), wxT("true"));
541         
542     } else {
543         
544         cfgfilein->Write(wxT("ssl"), wxT("false"));
545         
546     }
547     
548     cfgfilein->Write(wxT("refresh"), wxT("1800"));
549     cfgfilein->Write(wxT("type"), AccountType);
550     
553 void frmNewAccount::CloseWindow( wxCommandEvent& event )
555     ReloadAccountConfig = FALSE;
556     this->Close();
559 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
561     
562     // Update the options.
563     
564     if (cmbServerType->GetCurrentSelection() == 1){
565         
566         txtServerAddress->Enable();
567         txtServerPort->Enable();
568         txtUsername->Enable();
569         txtPassword->Enable();
570         chkUseSSL->Enable();
571         
572     } else if (cmbServerType->GetCurrentSelection() == 0){
573         
574         txtServerAddress->Disable();
575         txtServerPort->Disable();
576         txtUsername->Disable();
577         txtPassword->Disable();
578         chkUseSSL->Disable();
579         
580     }
581     
584 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
585     
586     ReloadAccountConfig = ReloadAccountInc;
587     
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