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.
186 //SSLInvalidCertNotifObj SSLICNProcData;
188 //bool *PauseMode = new bool;
193 //qrn.QResponse = &SSLResult;
194 //qrn.PausePtr = PauseMode;
196 //SSLICNProcData.CertCollection = CardDAVConn.GetSSLVerifyResults();
197 //SSLICNProcData.QRNotifData = &qrn;
198 //SSLICNProcData.AccountName = _("New account");
200 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
202 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
203 frmICPtr->ShowModal();
205 //wxCommandEvent event(INVALIDSSLCERT);
206 //event.SetClientData(&SSLICNProcData);
207 //wxPostEvent(this->GetParent(), event);
211 // Fall asleep until we get an response.
214 n1.tv_nsec = 250000000L;*/
216 SSLResult = frmICPtr->GetResult();
218 // Clean up before processing response.
223 // Process the response from the user.
227 // Accept the Certificate.
229 CardDAVConn.AllowSelfSignTest(TRUE);
231 } else if (SSLResult == 2){
233 // Reject the certificate, abort the task and mark as failed.
235 lblServerConnResult->SetLabel(_("Failed"));
236 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
241 //frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
243 //frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
244 //frmICPtr->ShowModal();
249 // Something else happened. Stop the process and
250 // display an error message instead.
252 lblServerConnResult->SetLabel(_("Failed"));
253 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
260 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
264 if (ServerResult == FALSE){
266 lblServerConnResult->SetLabel(_("Failed"));
271 lblServerConnResult->SetLabel(_("Connected"));
275 if (CardDAVConn.CanDoSSL() == TRUE){
277 lblServerSSLResult->SetLabel(_("Used"));
281 lblServerSSLResult->SetLabel(_("Not Used"));
282 lblServerSSLValid->SetLabel(_("Not Applicable"));
286 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
288 lblServerSSLValid->SetLabel(_("Verified"));
290 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){
292 lblServerSSLValid->SetLabel(_("Verified (user)"));
294 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
296 lblServerSSLValid->SetLabel(_("Unable to verify"));
300 if (CardDAVConn.CanDoCardDAV() == TRUE){
302 lblCardDAVSupportResult->SetLabel(_("Supported"));
306 lblCardDAVSupportResult->SetLabel(_("Unsupported"));
310 if (CardDAVConn.AbleToLogin() == TRUE){
312 lblAbleToLoginResult->SetLabel(_("Yes"));
316 lblAbleToLoginResult->SetLabel(_("No"));
320 // Get the address to process CardDAV requests.
322 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
324 if (CardDAVConn.HasValidResponse() == TRUE){
326 lblServerResponse->SetLabel(_("Yes"));
330 lblServerResponse->SetLabel(_("No"));
334 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
335 //CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.SSLVerify() == TRUE &&
336 CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
340 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
344 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)."));
348 } else if (PageSeek == 2){
355 szrNewAccount->RecalcSizes();
358 btnNext->SetLabel(_("Finish"));
360 if (txtAccountName->IsEmpty() && PageSeek == 2){
370 } else if (PageSeek == 3){
374 wxString XestiaABPrefDirectory;
375 wxString XestiaABDirectory;
376 wxString AccountSettingsFile;
378 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
379 bool DirectoryCreated = FALSE;
381 #if defined(__HAIKU__)
383 //preffilename = wxT("noo");
385 #elif defined(__WIN32__)
387 XestiaABPrefDirectory = GetUserPrefDir();
388 XestiaABDirectory = GetUserDir();
390 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
392 // Open the file for writing.
394 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
396 // Check if account name already exists and return an error message
397 // if this is the case.
399 wxString AccountName;
402 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
406 if (txtAccountName->GetValue() == AccountName){
408 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
413 cfgfile->SetPath(wxT("/"));
414 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
418 if (cmbServerType->GetCurrentSelection() == 1){
420 // Create the account directory.
422 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
424 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
426 DirectoryCreated = TRUE;
430 if (DirectoryCreated == TRUE){
432 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
436 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
441 } else if (cmbServerType->GetCurrentSelection() == 0){
443 // Create the account directory.
445 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
447 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
449 DirectoryCreated = TRUE;
453 if (DirectoryCreated == TRUE){
455 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
459 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
469 *ReloadAccountConfig = TRUE;
473 XestiaABPrefDirectory = GetUserPrefDir();
474 XestiaABDirectory = GetUserDir();
476 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
478 // Open the file for writing.
480 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
482 // Check if account name already exists and return an error message
483 // if this is the case.
485 wxString AccountName;
488 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
492 if (txtAccountName->GetValue() == AccountName){
494 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
499 cfgfile->SetPath(wxT("/"));
500 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
504 if (cmbServerType->GetCurrentSelection() == 1){
506 // Create the account directory.
508 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
510 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
512 DirectoryCreated = TRUE;
516 if (DirectoryCreated == TRUE){
518 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
522 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
527 } else if (cmbServerType->GetCurrentSelection() == 0){
529 // Create the account directory.
531 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
533 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
535 DirectoryCreated = TRUE;
539 if (DirectoryCreated == TRUE){
541 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
545 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
555 *ReloadAccountConfig = TRUE;
565 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
567 // Write the new account details.
569 cfgfilein->SetPath(txtAccountName->GetValue());
570 cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
571 cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
572 cfgfilein->Write(wxT("username"), txtUsername->GetValue());
573 cfgfilein->Write(wxT("password"), txtPassword->GetValue());
574 cfgfilein->Write(wxT("prefix"), ServerPrefix);
575 cfgfilein->Write(wxT("accountdir"), DirectoryName);
577 if (chkUseSSL->GetValue() == TRUE){
579 cfgfilein->Write(wxT("ssl"), wxT("true"));
583 cfgfilein->Write(wxT("ssl"), wxT("false"));
587 cfgfilein->Write(wxT("refresh"), wxT("1800"));
588 cfgfilein->Write(wxT("type"), AccountType);
592 void frmNewAccount::CloseWindow( wxCommandEvent& event )
597 *ReloadAccountConfig = FALSE;
602 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
605 // Update the options.
607 if (cmbServerType->GetCurrentSelection() == 1){
609 txtServerAddress->Enable();
610 txtServerPort->Enable();
611 txtUsername->Enable();
612 txtPassword->Enable();
615 } else if (cmbServerType->GetCurrentSelection() == 0){
617 txtServerAddress->Disable();
618 txtServerPort->Disable();
619 txtUsername->Disable();
620 txtPassword->Disable();
621 chkUseSSL->Disable();
627 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
629 // Setup the pointers for the new account window.
631 ReloadAccountConfig = ReloadAccountInc;