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 "frmInvalidSSLCertificate.h"
29 frmNewAccount::frmNewAccount( wxWindow* parent )
31 frmNewAccountADT( parent )
34 // Disable the previous button upon form creation.
36 btnPrevious->Disable();
40 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
43 // Check that the account name is valid.
45 wxString CheckAccName = txtAccountName->GetValue();
47 if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
59 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
62 // Go to the previous page.
68 // Currently at the Connection test screen.
73 szrNewAccount->RecalcSizes();
75 btnPrevious->Disable();
78 } else if (PageSeek == 1){
80 if (cmbServerType->GetCurrentSelection() == 0){
86 btnPrevious->Disable();
88 btnNext->SetLabel(_("Next >"));
93 // Currently at the Finish screen.
98 szrNewAccount->RecalcSizes();
100 btnNext->SetLabel(_("Next >"));
107 void frmNewAccount::ProcessNext( wxCommandEvent& event )
110 // Go to the next page or setup the new account.
116 if (cmbServerType->GetCurrentSelection() == 0){
122 btnPrevious->Enable();
123 szrNewAccount->RecalcSizes();
125 btnNext->SetLabel(_("Finish"));
132 bool ServerResult = FALSE;
133 bool ServerAction = FALSE;
135 wxString ServerMessage;
137 // Connection test screen.
142 szrNewAccount->RecalcSizes();
143 btnPrevious->Enable();
147 lblServerConnResult->SetLabel(wxT(""));
148 lblServerResponse->SetLabel(wxT(""));
149 lblServerSSLResult->SetLabel(wxT(""));
150 lblServerSSLValid->SetLabel(wxT(""));
151 lblAbleToLoginResult->SetLabel(wxT(""));
152 lblCardDAVSupportResult->SetLabel(wxT(""));
154 // Spawn a thread and check if server supports CardDAV.
158 lblServerConnResult->SetLabel(_("Testing..."));
160 UseSSL = chkUseSSL->GetValue();
161 CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
162 wxAtoi(txtServerPort->GetValue()),
163 txtUsername->GetValue(),
164 txtPassword->GetValue(),
166 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
168 // Verify SSL trust first before doing anything.
172 CURLcode sslcode = CardDAVConn.SSLVerifyTest();
174 if (sslcode == CURLE_OK){
178 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
180 // Certificate is more than likely a self-signed or
181 // expired certificate so display the invalid
182 // SSL certificate message.
184 // Setup the data to be sent in the wxPostEvent command.
188 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
190 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
191 frmICPtr->ShowModal();
193 SSLResult = frmICPtr->GetResult();
195 // Clean up before processing response.
200 // Process the response from the user.
204 // Accept the Certificate.
206 CardDAVConn.AllowSelfSignTest(TRUE);
208 } else if (SSLResult == 2){
210 // Reject the certificate, abort the task and mark as failed.
212 lblServerConnResult->SetLabel(_("Failed"));
213 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
220 // Something else happened. Stop the process and
221 // display an error message instead.
223 lblServerConnResult->SetLabel(_("Failed"));
224 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
231 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
235 if (ServerResult == FALSE){
237 lblServerConnResult->SetLabel(_("Failed"));
242 lblServerConnResult->SetLabel(_("Connected"));
246 if (CardDAVConn.CanDoSSL() == TRUE){
248 lblServerSSLResult->SetLabel(_("Used"));
252 lblServerSSLResult->SetLabel(_("Not Used"));
253 lblServerSSLValid->SetLabel(_("Not Applicable"));
257 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
259 lblServerSSLValid->SetLabel(_("Verified"));
261 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){
263 lblServerSSLValid->SetLabel(_("Verified (user)"));
265 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
267 lblServerSSLValid->SetLabel(_("Unable to verify"));
271 if (CardDAVConn.CanDoCardDAV() == TRUE){
273 lblCardDAVSupportResult->SetLabel(_("Supported"));
277 lblCardDAVSupportResult->SetLabel(_("Unsupported"));
281 if (CardDAVConn.AbleToLogin() == TRUE){
283 lblAbleToLoginResult->SetLabel(_("Yes"));
287 lblAbleToLoginResult->SetLabel(_("No"));
291 // Get the address to process CardDAV requests.
293 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
295 if (CardDAVConn.HasValidResponse() == TRUE){
297 lblServerResponse->SetLabel(_("Yes"));
301 lblServerResponse->SetLabel(_("No"));
305 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
307 CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
311 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
315 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)."));
319 } else if (PageSeek == 2){
326 szrNewAccount->RecalcSizes();
329 btnNext->SetLabel(_("Finish"));
331 if (txtAccountName->IsEmpty() && PageSeek == 2){
341 } else if (PageSeek == 3){
345 wxString XestiaABPrefDirectory;
346 wxString XestiaABDirectory;
347 wxString AccountSettingsFile;
349 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
350 bool DirectoryCreated = FALSE;
352 #if defined(__HAIKU__)
354 //preffilename = wxT("noo");
356 #elif defined(__WIN32__)
358 XestiaABPrefDirectory = GetUserPrefDir();
359 XestiaABDirectory = GetUserDir();
361 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
363 // Open the file for writing.
365 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
367 // Check if account name already exists and return an error message
368 // if this is the case.
370 wxString AccountName;
373 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
377 if (txtAccountName->GetValue() == AccountName){
379 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
384 cfgfile->SetPath(wxT("/"));
385 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
389 if (cmbServerType->GetCurrentSelection() == 1){
391 // Create the account directory.
393 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
395 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
397 DirectoryCreated = TRUE;
401 if (DirectoryCreated == TRUE){
403 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
407 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
412 } else if (cmbServerType->GetCurrentSelection() == 0){
414 // Create the account directory.
416 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
418 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
420 DirectoryCreated = TRUE;
424 if (DirectoryCreated == TRUE){
426 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
430 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
440 *ReloadAccountConfig = TRUE;
444 XestiaABPrefDirectory = GetUserPrefDir();
445 XestiaABDirectory = GetUserDir();
447 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
449 // Open the file for writing.
451 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
453 // Check if account name already exists and return an error message
454 // if this is the case.
456 wxString AccountName;
459 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
463 if (txtAccountName->GetValue() == AccountName){
465 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
470 cfgfile->SetPath(wxT("/"));
471 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
475 if (cmbServerType->GetCurrentSelection() == 1){
477 // Create the account directory.
479 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
481 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
483 DirectoryCreated = TRUE;
487 if (DirectoryCreated == TRUE){
489 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
493 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
498 } else if (cmbServerType->GetCurrentSelection() == 0){
500 // Create the account directory.
502 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
504 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
506 DirectoryCreated = TRUE;
510 if (DirectoryCreated == TRUE){
512 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
516 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
526 *ReloadAccountConfig = TRUE;
536 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
538 // Write the new account details.
540 cfgfilein->SetPath(txtAccountName->GetValue());
541 cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
542 cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
543 cfgfilein->Write(wxT("username"), txtUsername->GetValue());
544 cfgfilein->Write(wxT("password"), txtPassword->GetValue());
545 cfgfilein->Write(wxT("prefix"), ServerPrefix);
546 cfgfilein->Write(wxT("accountdir"), DirectoryName);
548 if (chkUseSSL->GetValue() == TRUE){
550 cfgfilein->Write(wxT("ssl"), wxT("true"));
554 cfgfilein->Write(wxT("ssl"), wxT("false"));
558 cfgfilein->Write(wxT("refresh"), wxT("1800"));
559 cfgfilein->Write(wxT("type"), AccountType);
563 void frmNewAccount::CloseWindow( wxCommandEvent& event )
568 *ReloadAccountConfig = FALSE;
573 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
576 // Update the options.
578 if (cmbServerType->GetCurrentSelection() == 1){
580 txtServerAddress->Enable();
581 txtServerPort->Enable();
582 txtUsername->Enable();
583 txtPassword->Enable();
586 } else if (cmbServerType->GetCurrentSelection() == 0){
588 txtServerAddress->Disable();
589 txtServerPort->Disable();
590 txtUsername->Disable();
591 txtPassword->Disable();
592 chkUseSSL->Disable();
598 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
600 // Setup the pointers for the new account window.
602 ReloadAccountConfig = ReloadAccountInc;