Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Move INSTALL, LICENSE, README, THANKS and TODO into root directory
[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 #if defined(__WIN32__)
26 #include <cryptuiapi.h>
27 #include "common/win32ssl.h"
28 #endif
30 #include "common/dirs.h"
31 #include "common/svrblist.h"
32 #include "frmInvalidSSLCertificate.h"
34 DEFINE_EVENT_TYPE(UPDATERESULTS);
35 DEFINE_EVENT_TYPE(RUNCARDDAV2TEST);
37 BEGIN_EVENT_TABLE(frmNewAccount, wxDialog)
38 EVT_COMMAND(wxID_ANY, UPDATERESULTS, frmNewAccount::UpdateResults)
39 EVT_COMMAND(wxID_ANY, RUNCARDDAV2TEST, frmNewAccount::RunCardDAV2Test)
40 END_EVENT_TABLE()
42 frmNewAccount::frmNewAccount( wxWindow* parent )
43 :
44 frmNewAccountADT( parent )
45 {
47         // Disable the previous button upon form creation.
49         btnPrevious->Disable();
50         
51 }
53 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
54 {
55     
56         // Check that the account name is valid.
57     
58         wxString CheckAccName = txtAccountName->GetValue();
59     
60         if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
61         
62                 btnNext->Disable();
63         
64         } else {
65         
66                 btnNext->Enable();
67         
68         }
69     
70 }
72 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
73 {
75         // Go to the previous page.
77         PageSeek--;
78     
79         if (PageSeek == 0){
80         
81                 // Currently at the Connection test screen.
82         
83                 tabConn->Hide();
84                 tabFinish->Hide();
85                 tabType->Show();
86                 szrNewAccount->RecalcSizes();
87         
88                 btnPrevious->Disable();
89                 btnNext->Enable();
90         
91         } else if (PageSeek == 1){
92         
93                 if (cmbServerType->GetCurrentSelection() == 0){
94             
95                         tabConn->Hide();
96                         tabFinish->Hide();
97                         tabType->Show();
98                         PageSeek = 0;
99                         btnPrevious->Disable();
100                         btnNext->Enable();
101                         btnNext->SetLabel(_("Next >"));
102                         return;
103             
104                 }
105                 
106                 // Currently at the Finish screen.
107         
108                 tabType->Hide();
109                 tabConn->Show();
110                 tabFinish->Hide();
111                 szrNewAccount->RecalcSizes();
112         
113                 btnNext->SetLabel(_("Next >"));
114                 btnNext->Enable();
115         
116         }
117     
120 void frmNewAccount::ProcessNext( wxCommandEvent& event )
123         // Go to the next page or setup the new account.
125         PageSeek++;
126     
127         if (PageSeek == 1){
128         
129                 if (cmbServerType->GetCurrentSelection() == 0){
130             
131                         tabType->Hide();
132                         tabConn->Hide();
133                         tabFinish->Show();
134                         PageSeek = 2;
135                         btnPrevious->Enable();
136                         szrNewAccount->RecalcSizes();
137                         btnNext->Disable();
138                         btnNext->SetLabel(_("Finish"));
139                         return;
140             
141                 }
142         
143                 // Check if server address matches against the blacklist.
144                 // Bring up warning message if it does.
145                 
146                 if (CheckBlacklist(txtServerAddress->GetValue())){
147                 
148                         int MessageBoxResult = wxMessageBox(_("The server with the address given does not support the CardDAV protocol properly and shouldn't be used.\n\nData loss is very likely.\n\nDo you still want to continue using this server?"), _("Server warning"), wxYES_NO, this);
149                 
150                         if (MessageBoxResult == wxNO){
151                                 PageSeek--;
152                                 return;
153                         }
154                         
155                 }
156                 
157                 btnNext->Disable();
158         
159                 bool ServerResult = FALSE;
160                 bool ServerAction = FALSE;
161                 bool UseSSL = TRUE;
162                 wxString ServerMessage;
163         
164                 // Connection test screen.
165         
166                 tabType->Hide();
167                 tabConn->Show();
168                 tabFinish->Hide();
169                 szrNewAccount->RecalcSizes();
170                 btnPrevious->Disable();
171         
172                 // Reset screen.
173         
174                 lblServerConnResult->SetLabel(wxT(""));
175                 lblServerResponse->SetLabel(wxT(""));
176                 lblServerSSLResult->SetLabel(wxT(""));
177                 lblServerSSLValid->SetLabel(wxT(""));
178                 lblAbleToLoginResult->SetLabel(wxT(""));
179                 lblCardDAVSupportResult->SetLabel(wxT(""));
180         
181                 // Depending on account type, run the test.
182         
183                 if (cmbServerType->GetCurrentSelection() == 1){
184                         wxCommandEvent RunTest(RUNCARDDAV2TEST);
185                         wxPostEvent(this, RunTest);
186                 }
187         
188         } else if (PageSeek == 2){
189         
190                 // Finish screen.
191         
192                 tabType->Hide();
193                 tabConn->Hide();
194                 tabFinish->Show();
195                 szrNewAccount->RecalcSizes();
196         
197                 btnNext->Disable();
198                 btnNext->SetLabel(_("Finish"));
199         
200                 if (txtAccountName->IsEmpty() && PageSeek == 2){
201         
202                         btnNext->Disable();
203             
204                 } else {
205         
206                         btnNext->Enable();
207             
208                 }
209         
210         } else if (PageSeek == 3){
211         
212                 // Finished.
213         
214                 wxString XestiaABPrefDirectory;
215                 wxString XestiaABDirectory;
216                 wxString AccountSettingsFile;
217                 //wxFile ASFile;
218                 
219                 srand(time(0));
220                 int RandomNumber = rand() % 32767;
221                 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), RandomNumber);
222                 bool DirectoryCreated = FALSE;
223         
224 #if defined(__HAIKU__)
225         
226                 //preffilename = wxT("noo");
227         
228 #elif defined(__WIN32__)
229         
230                 XestiaABPrefDirectory = GetUserPrefDir();
231                 XestiaABDirectory = GetUserDir();
232         
233                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
234         
235                 // Open the file for writing.
236         
237                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
238         
239                 // Check if account name already exists and return an error message
240                 // if this is the case.
241         
242                 wxString AccountName;
243                 long itemindex = 0;
244                 bool ContinueAcc;
245                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
246         
247                 while (ContinueAcc){
248             
249                         if (txtAccountName->GetValue() == AccountName){
250                 
251                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
252                                 return;
253                 
254                         }
255             
256                         cfgfile->SetPath(wxT("/"));
257                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
258             
259                 }
260         
261                 if (cmbServerType->GetCurrentSelection() == 1){
262             
263                         // Create the account directory.
264             
265                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
266             
267                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
268                 
269                                 DirectoryCreated = TRUE;
270                 
271                         }
272             
273                         if (DirectoryCreated == TRUE){
274                 
275                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
276                 
277                         } else {
278                 
279                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
280                                 return;
281                 
282                         }
283             
284                 } else if (cmbServerType->GetCurrentSelection() == 0){
285             
286                         // Create the account directory.
287             
288                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
289             
290                         if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
291                 
292                                 DirectoryCreated = TRUE;
293                 
294                         }
295             
296                         if (DirectoryCreated == TRUE){
297                 
298                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
299                 
300                         } else {
301                 
302                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
303                                 return;
304                 
305                         }
306             
307                 }
308         
309                 delete cfgfile;
310                 cfgfile = NULL;
311         
312                 *ReloadAccountConfig = TRUE;
313         
314 #else
315         
316                 XestiaABPrefDirectory = GetUserPrefDir();
317                 XestiaABDirectory = GetUserDir();
318         
319                 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
320         
321                 // Open the file for writing.
322         
323                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
324         
325                 // Check if account name already exists and return an error message
326                 // if this is the case.
327         
328                 wxString AccountName;
329                 long itemindex = 0;
330                 bool ContinueAcc;
331                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
332         
333                 while (ContinueAcc){
334             
335                         if (txtAccountName->GetValue() == AccountName){
336                 
337                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
338                                 return;
339                 
340                         }
341             
342                         cfgfile->SetPath(wxT("/"));
343                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
344             
345                 }
346         
347                 if (cmbServerType->GetCurrentSelection() == 1){
348             
349                         // Create the account directory.
350             
351                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
352             
353                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
354                 
355                                 DirectoryCreated = TRUE;
356                 
357                         }
358             
359                         if (DirectoryCreated == TRUE){
360                 
361                                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
362                 
363                         } else {
364                 
365                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
366                                 return;
367                 
368                         }
369             
370                 } else if (cmbServerType->GetCurrentSelection() == 0){
371             
372                         // Create the account directory.
373             
374                         wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
375             
376                         if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
377                 
378                                 DirectoryCreated = TRUE;
379                 
380                         }
381             
382                         if (DirectoryCreated == TRUE){
383                 
384                                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
385                 
386                         } else {
387                 
388                                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
389                                 return;
390                 
391                         }
392             
393                 }
394         
395                 delete cfgfile;
396                 cfgfile = NULL;
397         
398                 *ReloadAccountConfig = TRUE;
399         
400 #endif
401         
402                 this->Close();
403         
404         }
405     
408 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
409     
410         // Write the new account details.
411     
412         cfgfilein->SetPath(txtAccountName->GetValue());
413         cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
414         cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
415         cfgfilein->Write(wxT("username"), txtUsername->GetValue());
416         cfgfilein->Write(wxT("password"), txtPassword->GetValue());
417         cfgfilein->Write(wxT("prefix"), ServerPrefix);
418         cfgfilein->Write(wxT("accountdir"), DirectoryName);
419     
420         if (chkUseSSL->GetValue() == TRUE){
421         
422                 cfgfilein->Write(wxT("ssl"), wxT("true"));
423         
424         } else {
425         
426                 cfgfilein->Write(wxT("ssl"), wxT("false"));
427         
428         }
429     
430         cfgfilein->Write(wxT("refresh"), wxT("1800"));
431         cfgfilein->Write(wxT("type"), AccountType);
432     
435 void frmNewAccount::CloseWindow( wxCommandEvent& event )
438         // Close the window.
440         *ReloadAccountConfig = FALSE;
441         this->Close();
442         
445 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
447     
448         // Update the options.
449     
450         if (cmbServerType->GetCurrentSelection() == 1){
451         
452                 txtServerAddress->Enable();
453                 txtServerPort->Enable();
454                 txtUsername->Enable();
455                 txtPassword->Enable();
456                 chkUseSSL->Enable();
457         
458         } else if (cmbServerType->GetCurrentSelection() == 0){
459         
460                 txtServerAddress->Disable();
461                 txtServerPort->Disable();
462                 txtUsername->Disable();
463                 txtPassword->Disable();
464                 chkUseSSL->Disable();
465         
466         }
467     
470 void frmNewAccount::UpdateResults( wxCommandEvent& event )
473         NewAccountResult *ResultDataPointer = static_cast<NewAccountResult*>(event.GetClientData());
474         NewAccountResult ResultData = (*ResultDataPointer);
475         
476         delete(ResultDataPointer);
477         ResultDataPointer = nullptr;
478         bool ServerResult = true;
479         
480         // Process the result if the server connected.
482         if (ResultData.Connected == false){
483                 lblServerConnResult->SetLabel(_("Failed"));
484                 ServerResult = false;
485         } else {
486                 lblServerConnResult->SetLabel(_("Success"));            
487         }
488         
489         // Check the result if the server gave a valid response.
490         
491         if (ResultData.ValidResponse == false){
492                 lblServerResponse->SetLabel(_("No"));
493                 ServerResult = false;
494         } else {
495                 lblServerResponse->SetLabel(_("Yes"));
496         }
497         
498         // Check the result if the server uses SSL.     
499         
500         if (ResultData.SSLStatus == false){
501                 lblServerSSLResult->SetLabel(_("No"));
502         } else {
503                 lblServerSSLResult->SetLabel(_("Yes"));
504         }
505         
506         // Check the server has a valid SSL certificate.
507         
508         switch(ResultData.SSLVerified){
509                 case COSSL_VERIFIED:
510                         lblServerSSLValid->SetLabel(_("Verified"));
511                         break;
512                 case COSSL_VERIFIED_USER:
513                         lblServerSSLValid->SetLabel(_("Verified (User)"));
514                         break;
515                 case COSSL_UNABLETOVERIFY:
516                         lblServerSSLValid->SetLabel(_("Unable to verify"));
517                         ServerResult = false;
518                         break;
519                 case COSSL_NOTAPPLICABLE:
520                         lblServerSSLValid->SetLabel(_("Not applicable"));
521                         break;
522         }
523         
524         // Check if able to log into the server.
525         
526         if (ResultData.AuthPassed == false){
527                 lblAbleToLoginResult->SetLabel(_("No"));
528                 ServerResult = false;
529         } else {
530                 lblAbleToLoginResult->SetLabel(_("Yes"));               
531         }
532         
533         // Check if server has CardDAV support.
534         
535         if (ResultData.CanProcess == false){
536                 lblCardDAVSupportResult->SetLabel(_("No"));
537                 ServerResult = false;
538         } else {
539                 lblCardDAVSupportResult->SetLabel(_("Yes"));
540         }
541         
542         if (ServerResult == false){
543                 lblConnectionResultText->SetLabel(wxString::Format(_("An error occured whilst connecting: %s"), ResultData.ErrorMessage));
544         } else {
545                 btnNext->Enable();
546                 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
547         }
548         
549         btnPrevious->Enable();
550         
553 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
555         // Setup the pointers for the new account window.
556    
557         ReloadAccountConfig = ReloadAccountInc;
558     
561 void frmNewAccount::SetErrorMessageLabel(){
562         
563         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)."));
564         
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