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 )
33 btnPrevious->Disable();
36 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
39 wxString CheckAccName = txtAccountName->GetValue();
41 if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
53 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
59 // Currently at the Connection test screen.
61 //tbkAccount->ChangeSelection(0);
65 szrNewAccount->RecalcSizes();
67 btnPrevious->Disable();
70 } else if (PageSeek == 1){
72 if (cmbServerType->GetCurrentSelection() == 0){
78 btnPrevious->Disable();
80 btnNext->SetLabel(_("Next >"));
85 // Currently at the Finish screen.
90 szrNewAccount->RecalcSizes();
92 btnNext->SetLabel(_("Next >"));
99 void frmNewAccount::ProcessNext( wxCommandEvent& event )
105 if (cmbServerType->GetCurrentSelection() == 0){
111 btnPrevious->Enable();
112 szrNewAccount->RecalcSizes();
114 btnNext->SetLabel(_("Finish"));
121 bool ServerResult = FALSE;
122 bool ServerAction = FALSE;
124 wxString ServerMessage;
126 // Connection test screen.
131 szrNewAccount->RecalcSizes();
132 btnPrevious->Enable();
136 lblServerConnResult->SetLabel(wxT(""));
137 lblServerResponse->SetLabel(wxT(""));
138 lblServerSSLResult->SetLabel(wxT(""));
139 lblServerSSLValid->SetLabel(wxT(""));
140 lblAbleToLoginResult->SetLabel(wxT(""));
141 lblCardDAVSupportResult->SetLabel(wxT(""));
143 // Spawn a thread and check if server supports CardDAV.
147 lblServerConnResult->SetLabel(_("Testing..."));
149 UseSSL = chkUseSSL->GetValue();
150 CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
151 wxAtoi(txtServerPort->GetValue()),
152 txtUsername->GetValue(),
153 txtPassword->GetValue(),
155 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
157 // Verify SSL trust first before doing anything.
161 CURLcode sslcode = CardDAVConn.SSLVerifyTest();
163 if (sslcode == CURLE_OK){
167 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
169 // Certificate is more than likely a self-signed or
170 // expired certificate so display the invalid
171 // SSL certificate message.
173 // Setup the data to be sent in the wxPostEvent command.
175 //SSLInvalidCertNotifObj SSLICNProcData;
177 //bool *PauseMode = new bool;
182 //qrn.QResponse = &SSLResult;
183 //qrn.PausePtr = PauseMode;
185 //SSLICNProcData.CertCollection = CardDAVConn.GetSSLVerifyResults();
186 //SSLICNProcData.QRNotifData = &qrn;
187 //SSLICNProcData.AccountName = _("New account");
189 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
191 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
192 frmICPtr->ShowModal();
194 //wxCommandEvent event(INVALIDSSLCERT);
195 //event.SetClientData(&SSLICNProcData);
196 //wxPostEvent(this->GetParent(), event);
200 // Fall asleep until we get an response.
203 n1.tv_nsec = 250000000L;*/
205 SSLResult = frmICPtr->GetResult();
207 // Clean up before processing response.
212 // Process the response from the user.
216 // Accept the Certificate.
218 CardDAVConn.AllowSelfSignTest(TRUE);
220 } else if (SSLResult == 2){
222 // Reject the certificate, abort the task and mark as failed.
224 lblServerConnResult->SetLabel(_("Failed"));
225 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
230 //frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
232 //frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
233 //frmICPtr->ShowModal();
238 // Something else happened. Stop the process and
239 // display an error message instead.
241 lblServerConnResult->SetLabel(_("Failed"));
242 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
249 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
253 if (ServerResult == FALSE){
255 lblServerConnResult->SetLabel(_("Failed"));
260 lblServerConnResult->SetLabel(_("Connected"));
264 if (CardDAVConn.CanDoSSL() == TRUE){
266 lblServerSSLResult->SetLabel(_("Used"));
270 lblServerSSLResult->SetLabel(_("Not Used"));
271 lblServerSSLValid->SetLabel(_("Not Applicable"));
275 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
277 lblServerSSLValid->SetLabel(_("Verified"));
279 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.IsSelfSigned() == TRUE){
281 lblServerSSLValid->SetLabel(_("Verified (user)"));
283 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
285 lblServerSSLValid->SetLabel(_("Unable to verify"));
289 if (CardDAVConn.CanDoCardDAV() == TRUE){
291 lblCardDAVSupportResult->SetLabel(_("Supported"));
295 lblCardDAVSupportResult->SetLabel(_("Unsupported"));
299 if (CardDAVConn.AbleToLogin() == TRUE){
301 lblAbleToLoginResult->SetLabel(_("Yes"));
305 lblAbleToLoginResult->SetLabel(_("No"));
309 // Get the address to process CardDAV requests.
311 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
313 if (CardDAVConn.HasValidResponse() == TRUE){
315 lblServerResponse->SetLabel(_("Yes"));
319 lblServerResponse->SetLabel(_("No"));
323 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
324 //CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.SSLVerify() == TRUE &&
325 CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
329 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
333 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)."));
337 } else if (PageSeek == 2){
344 szrNewAccount->RecalcSizes();
347 btnNext->SetLabel(_("Finish"));
348 if (txtAccountName->IsEmpty() && PageSeek == 2){
354 } else if (PageSeek == 3){
358 wxString XestiaABPrefDirectory;
359 wxString XestiaABDirectory;
360 wxString AccountSettingsFile;
362 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
363 bool DirectoryCreated = FALSE;
365 #if defined(__HAIKU__)
367 //preffilename = wxT("noo");
369 #elif defined(__WIN32__)
371 XestiaABPrefDirectory = GetUserPrefDir();
372 XestiaABDirectory = GetUserDir();
374 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
376 // Open the file for writing.
378 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
380 // Check if account name already exists and return an error message
381 // if this is the case.
383 wxString AccountName;
386 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
390 if (txtAccountName->GetValue() == AccountName){
392 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
397 cfgfile->SetPath(wxT("/"));
398 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
402 if (cmbServerType->GetCurrentSelection() == 1){
404 // Create the account directory.
406 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
408 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
410 DirectoryCreated = TRUE;
414 if (DirectoryCreated == TRUE){
416 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
420 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
425 } else if (cmbServerType->GetCurrentSelection() == 0){
427 // Create the account directory.
429 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
431 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
433 DirectoryCreated = TRUE;
437 if (DirectoryCreated == TRUE){
439 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
443 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
453 *ReloadAccountConfig = TRUE;
457 XestiaABPrefDirectory = GetUserPrefDir();
458 XestiaABDirectory = GetUserDir();
460 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
462 // Open the file for writing.
464 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
466 // Check if account name already exists and return an error message
467 // if this is the case.
469 wxString AccountName;
472 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
476 if (txtAccountName->GetValue() == AccountName){
478 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
483 cfgfile->SetPath(wxT("/"));
484 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
488 if (cmbServerType->GetCurrentSelection() == 1){
490 // Create the account directory.
492 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
494 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
496 DirectoryCreated = TRUE;
500 if (DirectoryCreated == TRUE){
502 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
506 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
511 } else if (cmbServerType->GetCurrentSelection() == 0){
513 // Create the account directory.
515 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
517 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
519 DirectoryCreated = TRUE;
523 if (DirectoryCreated == TRUE){
525 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
529 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
539 *ReloadAccountConfig = TRUE;
549 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
551 cfgfilein->SetPath(txtAccountName->GetValue());
552 cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
553 cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
554 cfgfilein->Write(wxT("username"), txtUsername->GetValue());
555 cfgfilein->Write(wxT("password"), txtPassword->GetValue());
556 cfgfilein->Write(wxT("prefix"), ServerPrefix);
557 cfgfilein->Write(wxT("accountdir"), DirectoryName);
559 if (chkUseSSL->GetValue() == TRUE){
561 cfgfilein->Write(wxT("ssl"), wxT("true"));
565 cfgfilein->Write(wxT("ssl"), wxT("false"));
569 cfgfilein->Write(wxT("refresh"), wxT("1800"));
570 cfgfilein->Write(wxT("type"), AccountType);
574 void frmNewAccount::CloseWindow( wxCommandEvent& event )
576 *ReloadAccountConfig = FALSE;
580 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
583 // Update the options.
585 if (cmbServerType->GetCurrentSelection() == 1){
587 txtServerAddress->Enable();
588 txtServerPort->Enable();
589 txtUsername->Enable();
590 txtPassword->Enable();
593 } else if (cmbServerType->GetCurrentSelection() == 0){
595 txtServerAddress->Disable();
596 txtServerPort->Disable();
597 txtUsername->Disable();
598 txtPassword->Disable();
599 chkUseSSL->Disable();
605 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
607 ReloadAccountConfig = ReloadAccountInc;