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 #include "carddav/carddav.h"
26 #include "common/dirs.h"
27 #include "common/svrblist.h"
28 #include "frmInvalidSSLCertificate.h"
30 frmNewAccount::frmNewAccount( wxWindow* parent )
32 frmNewAccountADT( parent )
35 // Disable the previous button upon form creation.
37 btnPrevious->Disable();
41 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
44 // Check that the account name is valid.
46 wxString CheckAccName = txtAccountName->GetValue();
48 if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
60 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
63 // Go to the previous page.
69 // Currently at the Connection test screen.
74 szrNewAccount->RecalcSizes();
76 btnPrevious->Disable();
79 } else if (PageSeek == 1){
81 if (cmbServerType->GetCurrentSelection() == 0){
87 btnPrevious->Disable();
89 btnNext->SetLabel(_("Next >"));
94 // Currently at the Finish screen.
99 szrNewAccount->RecalcSizes();
101 btnNext->SetLabel(_("Next >"));
108 void frmNewAccount::ProcessNext( wxCommandEvent& event )
111 // Go to the next page or setup the new account.
117 if (cmbServerType->GetCurrentSelection() == 0){
123 btnPrevious->Enable();
124 szrNewAccount->RecalcSizes();
126 btnNext->SetLabel(_("Finish"));
131 // Check if server address matches against the blacklist.
132 // Bring up warning message if it does.
134 if (CheckBlacklist(txtServerAddress->GetValue())){
136 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);
138 if (MessageBoxResult == wxNO){
147 bool ServerResult = FALSE;
148 bool ServerAction = FALSE;
150 wxString ServerMessage;
152 // Connection test screen.
157 szrNewAccount->RecalcSizes();
158 btnPrevious->Enable();
162 lblServerConnResult->SetLabel(wxT(""));
163 lblServerResponse->SetLabel(wxT(""));
164 lblServerSSLResult->SetLabel(wxT(""));
165 lblServerSSLValid->SetLabel(wxT(""));
166 lblAbleToLoginResult->SetLabel(wxT(""));
167 lblCardDAVSupportResult->SetLabel(wxT(""));
169 // Spawn a thread and check if server supports CardDAV.
173 lblServerConnResult->SetLabel(_("Testing..."));
175 UseSSL = chkUseSSL->GetValue();
176 CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
177 wxAtoi(txtServerPort->GetValue()),
178 txtUsername->GetValue(),
179 txtPassword->GetValue(),
181 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
183 // Verify SSL trust first before doing anything.
187 CURLcode sslcode = CardDAVConn.SSLVerifyTest();
189 if (sslcode == CURLE_OK){
193 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
195 // Certificate is more than likely a self-signed or
196 // expired certificate so display the invalid
197 // SSL certificate message.
199 // Setup the data to be sent in the wxPostEvent command.
203 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
205 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
206 frmICPtr->ShowModal();
208 SSLResult = frmICPtr->GetResult();
210 // Clean up before processing response.
215 // Process the response from the user.
219 // Accept the Certificate.
221 CardDAVConn.AllowSelfSignTest(TRUE);
223 } else if (SSLResult == 2){
225 // Reject the certificate, abort the task and mark as failed.
227 lblServerConnResult->SetLabel(_("Failed"));
228 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
235 // Something else happened. Stop the process and
236 // display an error message instead.
238 lblServerConnResult->SetLabel(_("Failed"));
239 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
246 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
250 if (ServerResult == FALSE){
252 lblServerConnResult->SetLabel(_("Failed"));
257 lblServerConnResult->SetLabel(_("Connected"));
261 if (CardDAVConn.CanDoSSL() == TRUE){
263 lblServerSSLResult->SetLabel(_("Used"));
267 lblServerSSLResult->SetLabel(_("Not Used"));
268 lblServerSSLValid->SetLabel(_("Not Applicable"));
272 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
274 lblServerSSLValid->SetLabel(_("Verified"));
276 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){
278 lblServerSSLValid->SetLabel(_("Verified (user)"));
280 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
282 lblServerSSLValid->SetLabel(_("Unable to verify"));
286 if (CardDAVConn.CanDoCardDAV() == TRUE){
288 lblCardDAVSupportResult->SetLabel(_("Supported"));
292 lblCardDAVSupportResult->SetLabel(_("Unsupported"));
296 if (CardDAVConn.AbleToLogin() == TRUE){
298 lblAbleToLoginResult->SetLabel(_("Yes"));
302 lblAbleToLoginResult->SetLabel(_("No"));
306 // Get the address to process CardDAV requests.
308 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
310 if (CardDAVConn.HasValidResponse() == TRUE){
312 lblServerResponse->SetLabel(_("Yes"));
316 lblServerResponse->SetLabel(_("No"));
320 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
322 CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
326 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
330 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)."));
334 } else if (PageSeek == 2){
341 szrNewAccount->RecalcSizes();
344 btnNext->SetLabel(_("Finish"));
346 if (txtAccountName->IsEmpty() && PageSeek == 2){
356 } else if (PageSeek == 3){
360 wxString XestiaABPrefDirectory;
361 wxString XestiaABDirectory;
362 wxString AccountSettingsFile;
364 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
365 bool DirectoryCreated = FALSE;
367 #if defined(__HAIKU__)
369 //preffilename = wxT("noo");
371 #elif defined(__WIN32__)
373 XestiaABPrefDirectory = GetUserPrefDir();
374 XestiaABDirectory = GetUserDir();
376 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
378 // Open the file for writing.
380 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
382 // Check if account name already exists and return an error message
383 // if this is the case.
385 wxString AccountName;
388 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
392 if (txtAccountName->GetValue() == AccountName){
394 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
399 cfgfile->SetPath(wxT("/"));
400 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
404 if (cmbServerType->GetCurrentSelection() == 1){
406 // Create the account directory.
408 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
410 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
412 DirectoryCreated = TRUE;
416 if (DirectoryCreated == TRUE){
418 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
422 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
427 } else if (cmbServerType->GetCurrentSelection() == 0){
429 // Create the account directory.
431 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
433 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
435 DirectoryCreated = TRUE;
439 if (DirectoryCreated == TRUE){
441 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
445 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
455 *ReloadAccountConfig = TRUE;
459 XestiaABPrefDirectory = GetUserPrefDir();
460 XestiaABDirectory = GetUserDir();
462 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
464 // Open the file for writing.
466 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
468 // Check if account name already exists and return an error message
469 // if this is the case.
471 wxString AccountName;
474 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
478 if (txtAccountName->GetValue() == AccountName){
480 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
485 cfgfile->SetPath(wxT("/"));
486 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
490 if (cmbServerType->GetCurrentSelection() == 1){
492 // Create the account directory.
494 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
496 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
498 DirectoryCreated = TRUE;
502 if (DirectoryCreated == TRUE){
504 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
508 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
513 } else if (cmbServerType->GetCurrentSelection() == 0){
515 // Create the account directory.
517 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
519 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
521 DirectoryCreated = TRUE;
525 if (DirectoryCreated == TRUE){
527 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
531 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
541 *ReloadAccountConfig = TRUE;
551 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
553 // Write the new account details.
555 cfgfilein->SetPath(txtAccountName->GetValue());
556 cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
557 cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
558 cfgfilein->Write(wxT("username"), txtUsername->GetValue());
559 cfgfilein->Write(wxT("password"), txtPassword->GetValue());
560 cfgfilein->Write(wxT("prefix"), ServerPrefix);
561 cfgfilein->Write(wxT("accountdir"), DirectoryName);
563 if (chkUseSSL->GetValue() == TRUE){
565 cfgfilein->Write(wxT("ssl"), wxT("true"));
569 cfgfilein->Write(wxT("ssl"), wxT("false"));
573 cfgfilein->Write(wxT("refresh"), wxT("1800"));
574 cfgfilein->Write(wxT("type"), AccountType);
578 void frmNewAccount::CloseWindow( wxCommandEvent& event )
583 *ReloadAccountConfig = FALSE;
588 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
591 // Update the options.
593 if (cmbServerType->GetCurrentSelection() == 1){
595 txtServerAddress->Enable();
596 txtServerPort->Enable();
597 txtUsername->Enable();
598 txtPassword->Enable();
601 } else if (cmbServerType->GetCurrentSelection() == 0){
603 txtServerAddress->Disable();
604 txtServerPort->Disable();
605 txtUsername->Disable();
606 txtPassword->Disable();
607 chkUseSSL->Disable();
613 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
615 // Setup the pointers for the new account window.
617 ReloadAccountConfig = ReloadAccountInc;