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 "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)
42 frmNewAccount::frmNewAccount( wxWindow* parent )
44 frmNewAccountADT( parent )
47 // Disable the previous button upon form creation.
49 btnPrevious->Disable();
53 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
56 // Check that the account name is valid.
58 wxString CheckAccName = txtAccountName->GetValue();
60 if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
72 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
75 // Go to the previous page.
81 // Currently at the Connection test screen.
86 szrNewAccount->RecalcSizes();
88 btnPrevious->Disable();
91 } else if (PageSeek == 1){
93 if (cmbServerType->GetCurrentSelection() == 0){
99 btnPrevious->Disable();
101 btnNext->SetLabel(_("Next >"));
106 // Currently at the Finish screen.
111 szrNewAccount->RecalcSizes();
113 btnNext->SetLabel(_("Next >"));
120 void frmNewAccount::ProcessNext( wxCommandEvent& event )
123 // Go to the next page or setup the new account.
129 if (cmbServerType->GetCurrentSelection() == 0){
135 btnPrevious->Enable();
136 szrNewAccount->RecalcSizes();
138 btnNext->SetLabel(_("Finish"));
143 // Check if server address matches against the blacklist.
144 // Bring up warning message if it does.
146 if (CheckBlacklist(txtServerAddress->GetValue())){
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);
150 if (MessageBoxResult == wxNO){
159 bool ServerResult = FALSE;
160 bool ServerAction = FALSE;
162 wxString ServerMessage;
164 // Connection test screen.
169 szrNewAccount->RecalcSizes();
170 btnPrevious->Disable();
174 lblServerConnResult->SetLabel(wxT(""));
175 lblServerResponse->SetLabel(wxT(""));
176 lblServerSSLResult->SetLabel(wxT(""));
177 lblServerSSLValid->SetLabel(wxT(""));
178 lblAbleToLoginResult->SetLabel(wxT(""));
179 lblCardDAVSupportResult->SetLabel(wxT(""));
181 // Depending on account type, run the test.
183 if (cmbServerType->GetCurrentSelection() == 1){
184 wxCommandEvent RunTest(RUNCARDDAV2TEST);
185 wxPostEvent(this, RunTest);
188 } else if (PageSeek == 2){
195 szrNewAccount->RecalcSizes();
198 btnNext->SetLabel(_("Finish"));
200 if (txtAccountName->IsEmpty() && PageSeek == 2){
210 } else if (PageSeek == 3){
214 wxString XestiaABPrefDirectory;
215 wxString XestiaABDirectory;
216 wxString AccountSettingsFile;
220 int RandomNumber = rand() % 32767;
221 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), RandomNumber);
222 bool DirectoryCreated = FALSE;
224 #if defined(__HAIKU__)
226 //preffilename = wxT("noo");
228 #elif defined(__WIN32__)
230 XestiaABPrefDirectory = GetUserPrefDir();
231 XestiaABDirectory = GetUserDir();
233 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
235 // Open the file for writing.
237 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
239 // Check if account name already exists and return an error message
240 // if this is the case.
242 wxString AccountName;
245 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
249 if (txtAccountName->GetValue() == AccountName){
251 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
256 cfgfile->SetPath(wxT("/"));
257 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
261 if (cmbServerType->GetCurrentSelection() == 1){
263 // Create the account directory.
265 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
267 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
269 DirectoryCreated = TRUE;
273 if (DirectoryCreated == TRUE){
275 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
279 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
284 } else if (cmbServerType->GetCurrentSelection() == 0){
286 // Create the account directory.
288 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
290 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
292 DirectoryCreated = TRUE;
296 if (DirectoryCreated == TRUE){
298 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
302 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
312 *ReloadAccountConfig = TRUE;
316 XestiaABPrefDirectory = GetUserPrefDir();
317 XestiaABDirectory = GetUserDir();
319 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
321 // Open the file for writing.
323 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
325 // Check if account name already exists and return an error message
326 // if this is the case.
328 wxString AccountName;
331 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
335 if (txtAccountName->GetValue() == AccountName){
337 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
342 cfgfile->SetPath(wxT("/"));
343 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
347 if (cmbServerType->GetCurrentSelection() == 1){
349 // Create the account directory.
351 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
353 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
355 DirectoryCreated = TRUE;
359 if (DirectoryCreated == TRUE){
361 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
365 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
370 } else if (cmbServerType->GetCurrentSelection() == 0){
372 // Create the account directory.
374 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
376 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
378 DirectoryCreated = TRUE;
382 if (DirectoryCreated == TRUE){
384 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
388 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
398 *ReloadAccountConfig = TRUE;
408 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
410 // Write the new account details.
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);
420 if (chkUseSSL->GetValue() == TRUE){
422 cfgfilein->Write(wxT("ssl"), wxT("true"));
426 cfgfilein->Write(wxT("ssl"), wxT("false"));
430 cfgfilein->Write(wxT("refresh"), wxT("1800"));
431 cfgfilein->Write(wxT("type"), AccountType);
435 void frmNewAccount::CloseWindow( wxCommandEvent& event )
440 *ReloadAccountConfig = FALSE;
445 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
448 // Update the options.
450 if (cmbServerType->GetCurrentSelection() == 1){
452 txtServerAddress->Enable();
453 txtServerPort->Enable();
454 txtUsername->Enable();
455 txtPassword->Enable();
458 } else if (cmbServerType->GetCurrentSelection() == 0){
460 txtServerAddress->Disable();
461 txtServerPort->Disable();
462 txtUsername->Disable();
463 txtPassword->Disable();
464 chkUseSSL->Disable();
470 void frmNewAccount::UpdateResults( wxCommandEvent& event )
473 NewAccountResult *ResultDataPointer = static_cast<NewAccountResult*>(event.GetClientData());
474 NewAccountResult ResultData = (*ResultDataPointer);
476 delete(ResultDataPointer);
477 ResultDataPointer = nullptr;
478 bool ServerResult = true;
480 // Process the result if the server connected.
482 if (ResultData.Connected == false){
483 lblServerConnResult->SetLabel(_("Failed"));
484 ServerResult = false;
486 lblServerConnResult->SetLabel(_("Success"));
489 // Check the result if the server gave a valid response.
491 if (ResultData.ValidResponse == false){
492 lblServerResponse->SetLabel(_("No"));
493 ServerResult = false;
495 lblServerResponse->SetLabel(_("Yes"));
498 // Check the result if the server uses SSL.
500 if (ResultData.SSLStatus == false){
501 lblServerSSLResult->SetLabel(_("No"));
503 lblServerSSLResult->SetLabel(_("Yes"));
506 // Check the server has a valid SSL certificate.
508 switch(ResultData.SSLVerified){
510 lblServerSSLValid->SetLabel(_("Verified"));
512 case COSSL_VERIFIED_USER:
513 lblServerSSLValid->SetLabel(_("Verified (User)"));
515 case COSSL_UNABLETOVERIFY:
516 lblServerSSLValid->SetLabel(_("Unable to verify"));
517 ServerResult = false;
519 case COSSL_NOTAPPLICABLE:
520 lblServerSSLValid->SetLabel(_("Not applicable"));
524 // Check if able to log into the server.
526 if (ResultData.AuthPassed == false){
527 lblAbleToLoginResult->SetLabel(_("No"));
528 ServerResult = false;
530 lblAbleToLoginResult->SetLabel(_("Yes"));
533 // Check if server has CardDAV support.
535 if (ResultData.CanProcess == false){
536 lblCardDAVSupportResult->SetLabel(_("No"));
537 ServerResult = false;
539 lblCardDAVSupportResult->SetLabel(_("Yes"));
542 if (ServerResult == false){
543 lblConnectionResultText->SetLabel(wxString::Format(_("An error occured whilst connecting: %s"), ResultData.ErrorMessage));
546 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
549 btnPrevious->Enable();
553 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
555 // Setup the pointers for the new account window.
557 ReloadAccountConfig = ReloadAccountInc;
561 void frmNewAccount::SetErrorMessageLabel(){
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)."));