1 // frmNewAccount.cpp - New Account form.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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"
22 #include <wx/filefn.h>
23 #include <wx/fileconf.h>
25 #if defined(__WIN32__)
26 #include <cryptuiapi.h>
27 #include "common/win32ssl.h"
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)
43 frmNewAccount::frmNewAccount( wxWindow* parent )
45 frmNewAccountADT( parent )
48 // Disable the previous button upon form creation.
50 btnPrevious->Disable();
54 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
57 // Check that the account name is valid.
59 wxString CheckAccName = txtAccountName->GetValue();
61 if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
73 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
76 // Go to the previous page.
82 // Currently at the Connection test screen.
87 szrNewAccount->RecalcSizes();
89 btnPrevious->Disable();
92 } else if (PageSeek == 1){
94 if (cmbServerType->GetCurrentSelection() == 0){
100 btnPrevious->Disable();
102 btnNext->SetLabel(_("Next >"));
107 // Currently at the Finish screen.
112 szrNewAccount->RecalcSizes();
114 btnNext->SetLabel(_("Next >"));
121 void frmNewAccount::ProcessNext( wxCommandEvent& event )
124 // Go to the next page or setup the new account.
130 if (cmbServerType->GetCurrentSelection() == 0){
136 btnPrevious->Enable();
137 szrNewAccount->RecalcSizes();
139 btnNext->SetLabel(_("Finish"));
144 // Check if server address matches against the blacklist.
145 // Bring up warning message if it does.
147 if (CheckBlacklist(txtServerAddress->GetValue())){
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);
151 if (MessageBoxResult == wxNO){
160 bool ServerResult = FALSE;
161 bool ServerAction = FALSE;
163 wxString ServerMessage;
165 // Connection test screen.
170 szrNewAccount->RecalcSizes();
171 btnPrevious->Disable();
175 lblServerConnResult->SetLabel(wxT(""));
176 lblServerResponse->SetLabel(wxT(""));
177 lblServerSSLResult->SetLabel(wxT(""));
178 lblServerSSLValid->SetLabel(wxT(""));
179 lblAbleToLoginResult->SetLabel(wxT(""));
180 lblCardDAVSupportResult->SetLabel(wxT(""));
182 // Depending on account type, run the test.
184 if (cmbServerType->GetCurrentSelection() == 1){
185 wxCommandEvent RunTest(RUNCARDDAV2TEST);
186 wxPostEvent(this, RunTest);
189 } else if (PageSeek == 2){
196 szrNewAccount->RecalcSizes();
199 btnNext->SetLabel(_("Finish"));
201 if (txtAccountName->IsEmpty() && PageSeek == 2){
211 } else if (PageSeek == 3){
215 wxString XestiaABPrefDirectory;
216 wxString XestiaABDirectory;
217 wxString AccountSettingsFile;
221 int RandomNumber = rand() % 32767;
222 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), RandomNumber);
223 bool DirectoryCreated = FALSE;
225 #if defined(__HAIKU__)
227 //preffilename = wxT("noo");
229 #elif defined(__WIN32__)
231 XestiaABPrefDirectory = GetUserPrefDir();
232 XestiaABDirectory = GetUserDir();
234 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
236 // Open the file for writing.
238 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
240 // Check if account name already exists and return an error message
241 // if this is the case.
243 wxString AccountName;
246 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
250 if (txtAccountName->GetValue() == AccountName){
252 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
257 cfgfile->SetPath(wxT("/"));
258 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
262 if (cmbServerType->GetCurrentSelection() == 1){
264 // Create the account directory.
266 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
268 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
270 DirectoryCreated = TRUE;
274 if (DirectoryCreated == TRUE){
276 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
280 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
285 } else if (cmbServerType->GetCurrentSelection() == 0){
287 // Create the account directory.
289 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
291 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
293 DirectoryCreated = TRUE;
297 if (DirectoryCreated == TRUE){
299 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
303 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
313 *ReloadAccountConfig = TRUE;
317 XestiaABPrefDirectory = GetUserPrefDir();
318 XestiaABDirectory = GetUserDir();
320 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
322 // Open the file for writing.
324 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
326 // Check if account name already exists and return an error message
327 // if this is the case.
329 wxString AccountName;
332 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
336 if (txtAccountName->GetValue() == AccountName){
338 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
343 cfgfile->SetPath(wxT("/"));
344 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
348 if (cmbServerType->GetCurrentSelection() == 1){
350 // Create the account directory.
352 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
354 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
356 DirectoryCreated = TRUE;
360 if (DirectoryCreated == TRUE){
362 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
366 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
371 } else if (cmbServerType->GetCurrentSelection() == 0){
373 // Create the account directory.
375 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
377 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
379 DirectoryCreated = TRUE;
383 if (DirectoryCreated == TRUE){
385 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
389 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
399 *ReloadAccountConfig = TRUE;
409 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
411 // Write the new account details.
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);
421 if (chkUseSSL->GetValue() == TRUE){
423 cfgfilein->Write(wxT("ssl"), wxT("true"));
427 cfgfilein->Write(wxT("ssl"), wxT("false"));
431 cfgfilein->Write(wxT("refresh"), wxT("1800"));
432 cfgfilein->Write(wxT("type"), AccountType);
436 void frmNewAccount::CloseWindow( wxCommandEvent& event )
441 *ReloadAccountConfig = FALSE;
446 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
449 // Update the options.
451 if (cmbServerType->GetCurrentSelection() == 1){
453 txtServerAddress->Enable();
454 txtServerPort->Enable();
455 txtUsername->Enable();
456 txtPassword->Enable();
459 } else if (cmbServerType->GetCurrentSelection() == 0){
461 txtServerAddress->Disable();
462 txtServerPort->Disable();
463 txtUsername->Disable();
464 txtPassword->Disable();
465 chkUseSSL->Disable();
471 void frmNewAccount::UpdateResults( wxCommandEvent& event )
474 NewAccountResult *ResultDataPointer = static_cast<NewAccountResult*>(event.GetClientData());
475 NewAccountResult ResultData = (*ResultDataPointer);
477 delete(ResultDataPointer);
478 ResultDataPointer = nullptr;
479 bool ServerResult = true;
481 // Process the result if the server connected.
483 if (ResultData.Connected == false){
484 lblServerConnResult->SetLabel(_("Failed"));
485 ServerResult = false;
487 lblServerConnResult->SetLabel(_("Success"));
490 // Check the result if the server gave a valid response.
492 if (ResultData.ValidResponse == false){
493 lblServerResponse->SetLabel(_("No"));
494 ServerResult = false;
496 lblServerResponse->SetLabel(_("Yes"));
499 // Check the result if the server uses SSL.
501 if (ResultData.SSLStatus == false){
502 lblServerSSLResult->SetLabel(_("No"));
504 lblServerSSLResult->SetLabel(_("Yes"));
507 // Check the server has a valid SSL certificate.
509 switch(ResultData.SSLVerified){
511 lblServerSSLValid->SetLabel(_("Verified"));
513 case COSSL_VERIFIED_USER:
514 lblServerSSLValid->SetLabel(_("Verified (User)"));
516 case COSSL_UNABLETOVERIFY:
517 lblServerSSLValid->SetLabel(_("Unable to verify"));
518 ServerResult = false;
520 case COSSL_NOTAPPLICABLE:
521 lblServerSSLValid->SetLabel(_("Not applicable"));
525 // Check if able to log into the server.
527 if (ResultData.AuthPassed == false){
528 lblAbleToLoginResult->SetLabel(_("No"));
529 ServerResult = false;
531 lblAbleToLoginResult->SetLabel(_("Yes"));
534 // Check if server has CardDAV support.
536 if (ResultData.CanProcess == false){
537 lblCardDAVSupportResult->SetLabel(_("No"));
538 ServerResult = false;
540 lblCardDAVSupportResult->SetLabel(_("Yes"));
543 if (ServerResult == false){
544 lblConnectionResultText->SetLabel(wxString::Format(_("An error occured whilst connecting: %s"), ResultData.ErrorMessage));
547 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
550 btnPrevious->Enable();
554 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
556 // Setup the pointers for the new account window.
558 ReloadAccountConfig = ReloadAccountInc;
562 void frmNewAccount::SetErrorMessageLabel(){
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)."));