1 #include "frmNewAccount.h"
5 #include <wx/fileconf.h>
7 #include "carddav/carddav.h"
8 #include "common/dirs.h"
9 #include "frmInvalidSSLCertificate.h"
11 frmNewAccount::frmNewAccount( wxWindow* parent )
13 frmNewAccountADT( parent )
15 btnPrevious->Disable();
18 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
21 wxString CheckAccName = txtAccountName->GetValue();
23 if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
35 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
41 // Currently at the Connection test screen.
43 //tbkAccount->ChangeSelection(0);
47 szrNewAccount->RecalcSizes();
49 btnPrevious->Disable();
52 } else if (PageSeek == 1){
54 if (cmbServerType->GetCurrentSelection() == 0){
60 btnPrevious->Disable();
62 btnNext->SetLabel(_("Next >"));
67 // Currently at the Finish screen.
72 szrNewAccount->RecalcSizes();
74 btnNext->SetLabel(_("Next >"));
81 void frmNewAccount::ProcessNext( wxCommandEvent& event )
87 if (cmbServerType->GetCurrentSelection() == 0){
93 btnPrevious->Enable();
94 szrNewAccount->RecalcSizes();
96 btnNext->SetLabel(_("Finish"));
103 bool ServerResult = FALSE;
104 bool ServerAction = FALSE;
106 wxString ServerMessage;
108 // Connection test screen.
113 szrNewAccount->RecalcSizes();
114 btnPrevious->Enable();
118 lblServerConnResult->SetLabel(wxT(""));
119 lblServerResponse->SetLabel(wxT(""));
120 lblServerSSLResult->SetLabel(wxT(""));
121 lblServerSSLValid->SetLabel(wxT(""));
122 lblAbleToLoginResult->SetLabel(wxT(""));
123 lblCardDAVSupportResult->SetLabel(wxT(""));
125 // Spawn a thread and check if server supports CardDAV.
129 lblServerConnResult->SetLabel(_("Testing..."));
131 UseSSL = chkUseSSL->GetValue();
132 CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
133 wxAtoi(txtServerPort->GetValue()),
134 txtUsername->GetValue(),
135 txtPassword->GetValue(),
137 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
139 // Verify SSL trust first before doing anything.
143 CURLcode sslcode = CardDAVConn.SSLVerifyTest();
145 if (sslcode == CURLE_OK){
149 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
151 // Certificate is more than likely a self-signed or
152 // expired certificate so display the invalid
153 // SSL certificate message.
155 // Setup the data to be sent in the wxPostEvent command.
157 //SSLInvalidCertNotifObj SSLICNProcData;
159 //bool *PauseMode = new bool;
164 //qrn.QResponse = &SSLResult;
165 //qrn.PausePtr = PauseMode;
167 //SSLICNProcData.CertCollection = CardDAVConn.GetSSLVerifyResults();
168 //SSLICNProcData.QRNotifData = &qrn;
169 //SSLICNProcData.AccountName = _("New account");
171 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
173 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
174 frmICPtr->ShowModal();
176 //wxCommandEvent event(INVALIDSSLCERT);
177 //event.SetClientData(&SSLICNProcData);
178 //wxPostEvent(this->GetParent(), event);
182 // Fall asleep until we get an response.
185 n1.tv_nsec = 250000000L;*/
187 SSLResult = frmICPtr->GetResult();
189 // Clean up before processing response.
194 // Process the response from the user.
198 // Accept the Certificate.
200 CardDAVConn.AllowSelfSignTest(TRUE);
202 } else if (SSLResult == 2){
204 // Reject the certificate, abort the task and mark as failed.
206 lblServerConnResult->SetLabel(_("Failed"));
207 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
212 //frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
214 //frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
215 //frmICPtr->ShowModal();
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 &&
306 //CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.SSLVerify() == 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"));
330 if (txtAccountName->IsEmpty() && PageSeek == 2){
336 } else if (PageSeek == 3){
340 wxString XestiaABPrefDirectory;
341 wxString XestiaABDirectory;
342 wxString AccountSettingsFile;
344 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
345 bool DirectoryCreated = FALSE;
347 #if defined(__HAIKU__)
349 //preffilename = wxT("noo");
351 #elif defined(__WIN32__)
353 XestiaABPrefDirectory = GetUserPrefDir();
354 XestiaABDirectory = GetUserDir();
356 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
358 // Open the file for writing.
360 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
362 // Check if account name already exists and return an error message
363 // if this is the case.
365 wxString AccountName;
368 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
372 if (txtAccountName->GetValue() == AccountName){
374 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
379 cfgfile->SetPath(wxT("/"));
380 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
384 if (cmbServerType->GetCurrentSelection() == 1){
386 // Create the account directory.
388 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
390 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
392 DirectoryCreated = TRUE;
396 if (DirectoryCreated == TRUE){
398 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
402 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
407 } else if (cmbServerType->GetCurrentSelection() == 0){
409 // Create the account directory.
411 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
413 if (wxMkdir(XestiaABDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
415 DirectoryCreated = TRUE;
419 if (DirectoryCreated == TRUE){
421 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
425 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
435 ReloadAccountConfig = TRUE;
439 XestiaABPrefDirectory = GetUserPrefDir();
440 XestiaABDirectory = GetUserDir();
442 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
444 // Open the file for writing.
446 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
448 // Check if account name already exists and return an error message
449 // if this is the case.
451 wxString AccountName;
454 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
458 if (txtAccountName->GetValue() == AccountName){
460 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
465 cfgfile->SetPath(wxT("/"));
466 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
470 if (cmbServerType->GetCurrentSelection() == 1){
472 // Create the account directory.
474 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
476 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
478 DirectoryCreated = TRUE;
482 if (DirectoryCreated == TRUE){
484 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
488 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
493 } else if (cmbServerType->GetCurrentSelection() == 0){
495 // Create the account directory.
497 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
499 if (wxMkdir(XestiaABDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
501 DirectoryCreated = TRUE;
505 if (DirectoryCreated == TRUE){
507 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
511 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
521 ReloadAccountConfig = TRUE;
531 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
533 cfgfilein->SetPath(txtAccountName->GetValue());
534 cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
535 cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
536 cfgfilein->Write(wxT("username"), txtUsername->GetValue());
537 cfgfilein->Write(wxT("password"), txtPassword->GetValue());
538 cfgfilein->Write(wxT("prefix"), ServerPrefix);
539 cfgfilein->Write(wxT("accountdir"), DirectoryName);
541 if (chkUseSSL->GetValue() == TRUE){
543 cfgfilein->Write(wxT("ssl"), wxT("true"));
547 cfgfilein->Write(wxT("ssl"), wxT("false"));
551 cfgfilein->Write(wxT("refresh"), wxT("1800"));
552 cfgfilein->Write(wxT("type"), AccountType);
556 void frmNewAccount::CloseWindow( wxCommandEvent& event )
558 ReloadAccountConfig = FALSE;
562 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
565 // Update the options.
567 if (cmbServerType->GetCurrentSelection() == 1){
569 txtServerAddress->Enable();
570 txtServerPort->Enable();
571 txtUsername->Enable();
572 txtPassword->Enable();
575 } else if (cmbServerType->GetCurrentSelection() == 0){
577 txtServerAddress->Disable();
578 txtServerPort->Disable();
579 txtUsername->Disable();
580 txtPassword->Disable();
581 chkUseSSL->Disable();
587 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
589 ReloadAccountConfig = ReloadAccountInc;