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 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
157 frmICPtr->LoadDataNew(CardDAVConn.GetSSLVerifyResults(), txtServerAddress->GetValue());
158 frmICPtr->ShowModal();
162 // Something else happened. Stop the process and
163 // display an error message instead.
165 lblServerConnResult->SetLabel(_("Failed"));
166 lblConnectionResultText->SetLabel(_("An error occured whilst connnecting: ") + CardDAVConn.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CardDAVConn.GetErrorBuffer().mb_str()));
173 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
177 if (ServerResult == FALSE){
179 lblServerConnResult->SetLabel(_("Failed"));
180 wxMessageBox(CardDAVConn.GetErrorMessage());
185 lblServerConnResult->SetLabel(_("Connected"));
189 if (CardDAVConn.HasValidResponse() == TRUE){
191 lblServerResponse->SetLabel(_("Yes"));
195 lblServerResponse->SetLabel(_("No"));
199 if (CardDAVConn.CanDoSSL() == TRUE){
201 lblCardDAVSupportResult->SetLabel(_("Used"));
205 lblServerSSLResult->SetLabel(_("Not Used"));
206 lblServerSSLValid->SetLabel(_("Not Applicable"));
210 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
212 lblCardDAVSupportResult->SetLabel(_("Verified"));
214 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
216 lblServerSSLResult->SetLabel(_("Unable to verify"));
220 if (CardDAVConn.CanDoCardDAV() == TRUE){
222 lblCardDAVSupportResult->SetLabel(_("Supported"));
226 lblCardDAVSupportResult->SetLabel(_("Unsupported"));
230 if (CardDAVConn.AbleToLogin() == TRUE){
232 lblAbleToLoginResult->SetLabel(_("Yes"));
236 lblAbleToLoginResult->SetLabel(_("No"));
240 // Get the address to process CardDAV requests.
242 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
244 wxMessageBox(ServerPrefix);
246 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
247 //CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.SSLVerify() == TRUE &&
248 CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
252 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
256 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)."));
260 } else if (PageSeek == 2){
267 szrNewAccount->RecalcSizes();
270 btnNext->SetLabel(_("Finish"));
271 if (txtAccountName->IsEmpty() && PageSeek == 2){
277 } else if (PageSeek == 3){
281 wxString XestiaABPrefDirectory;
282 wxString AccountSettingsFile;
284 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
285 bool DirectoryCreated = FALSE;
287 #if defined(__HAIKU__)
289 //preffilename = wxT("noo");
291 #elif defined(__WIN32__)
293 XestiaABPrefDirectory = GetUserDir();
295 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
297 // Open the file for writing.
299 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
301 // Check if account name already exists and return an error message
302 // if this is the case.
304 wxString AccountName;
307 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
311 if (txtAccountName->GetValue() == AccountName){
313 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
318 cfgfile->SetPath(wxT("/"));
319 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
323 if (cmbServerType->GetCurrentSelection() == 1){
325 // Create the account directory.
327 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
329 if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
331 DirectoryCreated = TRUE;
335 if (DirectoryCreated == TRUE){
337 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
341 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
346 } else if (cmbServerType->GetCurrentSelection() == 0){
348 // Create the account directory.
350 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
352 if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
354 DirectoryCreated = TRUE;
358 if (DirectoryCreated == TRUE){
360 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
364 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
374 ReloadAccountConfig = TRUE;
378 XestiaABPrefDirectory = GetUserDir();
380 AccountSettingsFile = GetUserPrefDir() + wxT("accounts");
382 // Open the file for writing.
384 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
386 // Check if account name already exists and return an error message
387 // if this is the case.
389 wxString AccountName;
392 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
396 if (txtAccountName->GetValue() == AccountName){
398 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
403 cfgfile->SetPath(wxT("/"));
404 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
408 if (cmbServerType->GetCurrentSelection() == 1){
410 // Create the account directory.
412 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
414 if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
416 DirectoryCreated = TRUE;
420 if (DirectoryCreated == TRUE){
422 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
426 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
431 } else if (cmbServerType->GetCurrentSelection() == 0){
433 // Create the account directory.
435 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
437 if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
439 DirectoryCreated = TRUE;
443 if (DirectoryCreated == TRUE){
445 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
449 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
459 ReloadAccountConfig = TRUE;
469 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
471 cfgfilein->SetPath(txtAccountName->GetValue());
472 cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
473 cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
474 cfgfilein->Write(wxT("username"), txtUsername->GetValue());
475 cfgfilein->Write(wxT("password"), txtPassword->GetValue());
476 cfgfilein->Write(wxT("prefix"), ServerPrefix);
477 cfgfilein->Write(wxT("accountdir"), DirectoryName);
479 if (chkUseSSL->GetValue() == TRUE){
481 cfgfilein->Write(wxT("ssl"), wxT("true"));
485 cfgfilein->Write(wxT("ssl"), wxT("false"));
489 cfgfilein->Write(wxT("refresh"), wxT("1800"));
490 cfgfilein->Write(wxT("type"), AccountType);
494 void frmNewAccount::CloseWindow( wxCommandEvent& event )
496 ReloadAccountConfig = FALSE;
500 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
503 // Update the options.
505 if (cmbServerType->GetCurrentSelection() == 1){
507 txtServerAddress->Enable();
508 txtServerPort->Enable();
509 txtUsername->Enable();
510 txtPassword->Enable();
513 } else if (cmbServerType->GetCurrentSelection() == 0){
515 txtServerAddress->Disable();
516 txtServerPort->Disable();
517 txtUsername->Disable();
518 txtPassword->Disable();
519 chkUseSSL->Disable();
525 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
527 ReloadAccountConfig = ReloadAccountInc;