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