Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Fixed new account creation for Win32 using GetUserPrefDir and GetUserDir properly.
[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 XestiaABDirectory;
342         wxString AccountSettingsFile;
343         //wxFile ASFile;
344         wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
345         bool DirectoryCreated = FALSE;
346         
347 #if defined(__HAIKU__)
348         
349         //preffilename = wxT("noo");
350         
351 #elif defined(__WIN32__)
352         
353         XestiaABPrefDirectory = GetUserPrefDir();
354         XestiaABDirectory = GetUserDir();
355         
356         AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
357         
358         // Open the file for writing.
359         
360         wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
361         
362         // Check if account name already exists and return an error message
363         // if this is the case.
364         
365         wxString AccountName;
366         long itemindex = 0;
367         bool ContinueAcc;
368         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
369         
370         while (ContinueAcc){
371             
372             if (txtAccountName->GetValue() == AccountName){
373                 
374                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
375                 return;
376                 
377             }
378             
379             cfgfile->SetPath(wxT("/"));
380             ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
381             
382         }
383         
384         if (cmbServerType->GetCurrentSelection() == 1){
385             
386             // Create the account directory.
387             
388             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
389             
390             if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
391                 
392                 DirectoryCreated = TRUE;
393                 
394             }
395             
396             if (DirectoryCreated == TRUE){
397                 
398                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
399                 
400             } else {
401                 
402                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
403                 return;
404                 
405             }
406             
407         } else if (cmbServerType->GetCurrentSelection() == 0){
408             
409             // Create the account directory.
410             
411             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
412             
413             if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
414                 
415                 DirectoryCreated = TRUE;
416                 
417             }
418             
419             if (DirectoryCreated == TRUE){
420                 
421                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
422                 
423             } else {
424                 
425                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
426                 return;
427                 
428             }
429             
430         }
431         
432         delete cfgfile;
433         cfgfile = NULL;
434         
435         ReloadAccountConfig = TRUE;
436         
437 #else
438         
439         XestiaABPrefDirectory = GetUserPrefDir();
440         XestiaABDirectory = GetUserDir();
441         
442         AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
443         
444         // Open the file for writing.
445         
446         wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
447         
448         // Check if account name already exists and return an error message
449         // if this is the case.
450         
451         wxString AccountName;
452         long itemindex = 0;
453         bool ContinueAcc;
454         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
455         
456         while (ContinueAcc){
457             
458             if (txtAccountName->GetValue() == AccountName){
459                 
460                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
461                 return;
462                 
463             }
464             
465             cfgfile->SetPath(wxT("/"));
466             ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
467             
468         }
469         
470         if (cmbServerType->GetCurrentSelection() == 1){
471             
472             // Create the account directory.
473             
474             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
475             
476             if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
477                 
478                 DirectoryCreated = TRUE;
479                 
480             }
481             
482             if (DirectoryCreated == TRUE){
483                 
484                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
485                 
486             } else {
487                 
488                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
489                 return;
490                 
491             }
492             
493         } else if (cmbServerType->GetCurrentSelection() == 0){
494             
495             // Create the account directory.
496             
497             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
498             
499             if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
500                 
501                 DirectoryCreated = TRUE;
502                 
503             }
504             
505             if (DirectoryCreated == TRUE){
506                 
507                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
508                 
509             } else {
510                 
511                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
512                 return;
513                 
514             }
515             
516         }
517         
518         delete cfgfile;
519         cfgfile = NULL;
520         
521         ReloadAccountConfig = TRUE;
522         
523 #endif
524         
525         this->Close();
526         
527     }
528     
531 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
532     
533     cfgfilein->SetPath(txtAccountName->GetValue());
534     cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
535     cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
536     cfgfilein->Write(wxT("username"), txtUsername->GetValue());
537     cfgfilein->Write(wxT("password"), txtPassword->GetValue());
538     cfgfilein->Write(wxT("prefix"), ServerPrefix);
539     cfgfilein->Write(wxT("accountdir"), DirectoryName);
540     
541     if (chkUseSSL->GetValue() == TRUE){
542         
543         cfgfilein->Write(wxT("ssl"), wxT("true"));
544         
545     } else {
546         
547         cfgfilein->Write(wxT("ssl"), wxT("false"));
548         
549     }
550     
551     cfgfilein->Write(wxT("refresh"), wxT("1800"));
552     cfgfilein->Write(wxT("type"), AccountType);
553     
556 void frmNewAccount::CloseWindow( wxCommandEvent& event )
558     ReloadAccountConfig = FALSE;
559     this->Close();
562 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
564     
565     // Update the options.
566     
567     if (cmbServerType->GetCurrentSelection() == 1){
568         
569         txtServerAddress->Enable();
570         txtServerPort->Enable();
571         txtUsername->Enable();
572         txtPassword->Enable();
573         chkUseSSL->Enable();
574         
575     } else if (cmbServerType->GetCurrentSelection() == 0){
576         
577         txtServerAddress->Disable();
578         txtServerPort->Disable();
579         txtUsername->Disable();
580         txtPassword->Disable();
581         chkUseSSL->Disable();
582         
583     }
584     
587 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
588     
589     ReloadAccountConfig = ReloadAccountInc;
590     
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