1 #include "frmNewAccount.h"
5 #include <wx/fileconf.h>
7 #include "carddav/carddav.h"
8 #include "common/dirs.h"
10 frmNewAccount::frmNewAccount( wxWindow* parent )
12 frmNewAccountADT( parent )
14 btnPrevious->Disable();
17 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
20 wxString CheckAccName = txtAccountName->GetValue();
22 if ((txtAccountName->IsEmpty() && PageSeek == 2) || CheckAccName.Len() < 4){
34 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
40 // Currently at the Connection test screen.
42 //tbkAccount->ChangeSelection(0);
46 szrNewAccount->RecalcSizes();
48 btnPrevious->Disable();
51 } else if (PageSeek == 1){
53 if (cmbServerType->GetCurrentSelection() == 0){
59 btnPrevious->Disable();
61 btnNext->SetLabel(_("Next >"));
66 // Currently at the Finish screen.
71 szrNewAccount->RecalcSizes();
73 btnNext->SetLabel(_("Next >"));
80 void frmNewAccount::ProcessNext( wxCommandEvent& event )
86 if (cmbServerType->GetCurrentSelection() == 0){
92 btnPrevious->Enable();
93 szrNewAccount->RecalcSizes();
95 btnNext->SetLabel(_("Finish"));
102 bool ServerResult = FALSE;
103 bool ServerAction = FALSE;
105 wxString ServerMessage;
107 // Connection test screen.
112 szrNewAccount->RecalcSizes();
113 btnPrevious->Enable();
117 lblServerConnResult->SetLabel(wxT(""));
118 lblServerResponse->SetLabel(wxT(""));
119 lblServerSSLResult->SetLabel(wxT(""));
120 lblServerSSLValid->SetLabel(wxT(""));
121 lblAbleToLoginResult->SetLabel(wxT(""));
122 lblCardDAVSupportResult->SetLabel(wxT(""));
124 // Spawn a thread and check if server supports CardDAV.
128 lblServerConnResult->SetLabel(_("Testing..."));
130 UseSSL = chkUseSSL->GetValue();
132 CardDAVConn.SetupConnection(txtServerAddress->GetValue(),
133 wxAtoi(txtServerPort->GetValue()),
134 txtUsername->GetValue(),
135 txtPassword->GetValue(),
137 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
139 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
143 if (ServerResult == FALSE){
145 lblServerConnResult->SetLabel(_("Failed"));
146 wxMessageBox(CardDAVConn.GetErrorMessage());
151 lblServerConnResult->SetLabel(_("Connected"));
155 if (CardDAVConn.HasValidResponse() == TRUE){
157 lblServerResponse->SetLabel(_("Yes"));
161 lblServerResponse->SetLabel(_("No"));
165 if (CardDAVConn.CanDoSSL() == TRUE){
167 lblCardDAVSupportResult->SetLabel(_("Used"));
171 lblServerSSLResult->SetLabel(_("Not Used"));
172 lblServerSSLValid->SetLabel(_("Not Applicable"));
176 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
178 lblCardDAVSupportResult->SetLabel(_("Verified"));
180 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
182 lblServerSSLResult->SetLabel(_("Unable to verify"));
186 if (CardDAVConn.CanDoCardDAV() == TRUE){
188 lblCardDAVSupportResult->SetLabel(_("Supported"));
192 lblCardDAVSupportResult->SetLabel(_("Unsupported"));
196 if (CardDAVConn.AbleToLogin() == TRUE){
198 lblAbleToLoginResult->SetLabel(_("Yes"));
202 lblAbleToLoginResult->SetLabel(_("No"));
206 // Get the address to process CardDAV requests.
208 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
210 wxMessageBox(ServerPrefix);
212 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
213 //CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.SSLVerify() == TRUE &&
214 CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
218 lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
222 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)."));
226 } else if (PageSeek == 2){
233 szrNewAccount->RecalcSizes();
236 btnNext->SetLabel(_("Finish"));
237 if (txtAccountName->IsEmpty() && PageSeek == 2){
243 } else if (PageSeek == 3){
247 wxString XestiaABPrefDirectory;
248 wxString AccountSettingsFile;
250 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
251 bool DirectoryCreated = FALSE;
253 #if defined(__HAIKU__)
255 //preffilename = wxT("noo");
257 #elif defined(__WIN32__)
259 XestiaABPrefDirectory = GetUserDir();
261 AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
263 // Open the file for writing.
265 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
267 // Check if account name already exists and return an error message
268 // if this is the case.
270 wxString AccountName;
273 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
277 if (txtAccountName->GetValue() == AccountName){
279 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
284 cfgfile->SetPath(wxT("/"));
285 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
289 if (cmbServerType->GetCurrentSelection() == 1){
291 // Create the account directory.
293 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
295 if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
297 DirectoryCreated = TRUE;
301 if (DirectoryCreated == TRUE){
303 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
307 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
312 } else if (cmbServerType->GetCurrentSelection() == 0){
314 // Create the account directory.
316 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
318 if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
320 DirectoryCreated = TRUE;
324 if (DirectoryCreated == TRUE){
326 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
330 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
340 ReloadAccountConfig = TRUE;
344 XestiaABPrefDirectory = GetUserDir();
346 AccountSettingsFile = GetUserPrefDir() + wxT("accounts");
348 // Open the file for writing.
350 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
352 // Check if account name already exists and return an error message
353 // if this is the case.
355 wxString AccountName;
358 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
362 if (txtAccountName->GetValue() == AccountName){
364 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
369 cfgfile->SetPath(wxT("/"));
370 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
374 if (cmbServerType->GetCurrentSelection() == 1){
376 // Create the account directory.
378 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
380 if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
382 DirectoryCreated = TRUE;
386 if (DirectoryCreated == TRUE){
388 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
392 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
397 } else if (cmbServerType->GetCurrentSelection() == 0){
399 // Create the account directory.
401 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
403 if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
405 DirectoryCreated = TRUE;
409 if (DirectoryCreated == TRUE){
411 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
415 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
425 ReloadAccountConfig = TRUE;
435 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
437 cfgfilein->SetPath(txtAccountName->GetValue());
438 cfgfilein->Write(wxT("address"), txtServerAddress->GetValue());
439 cfgfilein->Write(wxT("port"), txtServerPort->GetValue());
440 cfgfilein->Write(wxT("username"), txtUsername->GetValue());
441 cfgfilein->Write(wxT("password"), txtPassword->GetValue());
442 cfgfilein->Write(wxT("prefix"), ServerPrefix);
443 cfgfilein->Write(wxT("accountdir"), DirectoryName);
445 if (chkUseSSL->GetValue() == TRUE){
447 cfgfilein->Write(wxT("ssl"), wxT("true"));
451 cfgfilein->Write(wxT("ssl"), wxT("false"));
455 cfgfilein->Write(wxT("refresh"), wxT("1800"));
456 cfgfilein->Write(wxT("type"), AccountType);
460 void frmNewAccount::CloseWindow( wxCommandEvent& event )
462 ReloadAccountConfig = FALSE;
466 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
469 // Update the options.
471 if (cmbServerType->GetCurrentSelection() == 1){
473 txtServerAddress->Enable();
474 txtServerPort->Enable();
475 txtUsername->Enable();
476 txtPassword->Enable();
479 } else if (cmbServerType->GetCurrentSelection() == 0){
481 txtServerAddress->Disable();
482 txtServerPort->Disable();
483 txtUsername->Disable();
484 txtPassword->Disable();
485 chkUseSSL->Disable();
491 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
493 ReloadAccountConfig = ReloadAccountInc;