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 = GetUserPrefDir();
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 cfgfile->SetPath(txtAccountName->GetValue());
290 cfgfile->Write(wxT("address"), txtServerAddress->GetValue());
291 cfgfile->Write(wxT("port"), txtServerPort->GetValue());
292 cfgfile->Write(wxT("username"), txtUsername->GetValue());
293 cfgfile->Write(wxT("password"), txtPassword->GetValue());
294 cfgfile->Write(wxT("prefix"), ServerPrefix);
296 if (chkUseSSL->GetValue() == TRUE){
298 cfgfile->Write(wxT("ssl"), wxT("true"));
302 cfgfile->Write(wxT("ssl"), wxT("false"));
306 cfgfile->Write(wxT("refresh"), wxT("1800"));
308 if (cmbServerType->GetCurrentSelection() == 1){
310 cfgfile->Write(wxT("type"), wxT("CardDAV"));
312 while(DirectoryCreated == FALSE){
314 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
315 wxString XestiaABUserDirectory = GetUserDir();
317 if (wxMkdir(XestiaABUserDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
319 DirectoryCreated = TRUE;
320 cfgfile->Write(wxT("accountdir"), DirectoryName);
330 } else if (cmbServerType->GetCurrentSelection() == 0){
332 // Create the account directory.
334 cfgfile->Write(wxT("type"), wxT("Local"));
336 while(DirectoryCreated == FALSE){
338 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
339 wxString XestiaABUserDirectory = GetUserDir();
341 if (wxMkdir(XestiaABUserDirectory + wxT("\\accounts\\") + DirectoryName + wxT(".local"), 0740) == TRUE){
343 DirectoryCreated = TRUE;
344 cfgfile->Write(wxT("accountdir"), DirectoryName);
355 ReloadAccountConfig = TRUE;
359 XestiaABUserDirectory = GetUserPrefDir();
361 AccountSettingsFile = XestiaABUserDirectory + wxT("accounts");
363 // Open the file for writing.
365 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
367 // Check if account name already exists and return an error message
368 // if this is the case.
370 wxString AccountName;
373 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
377 if (txtAccountName->GetValue() == AccountName){
379 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
384 cfgfile->SetPath(wxT("/"));
385 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
389 cfgfile->SetPath(txtAccountName->GetValue());
390 cfgfile->Write(wxT("address"), txtServerAddress->GetValue());
391 cfgfile->Write(wxT("port"), txtServerPort->GetValue());
392 cfgfile->Write(wxT("username"), txtUsername->GetValue());
393 cfgfile->Write(wxT("password"), txtPassword->GetValue());
394 cfgfile->Write(wxT("prefix"), ServerPrefix);
396 if (chkUseSSL->GetValue() == TRUE){
398 cfgfile->Write(wxT("ssl"), wxT("true"));
402 cfgfile->Write(wxT("ssl"), wxT("false"));
406 cfgfile->Write(wxT("refresh"), wxT("1800"));
408 if (cmbServerType->GetCurrentSelection() == 1){
410 while(DirectoryCreated == FALSE){
412 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
414 if (wxMkdir(XestiaABUserDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
416 DirectoryCreated = TRUE;
417 cfgfile->Write(wxT("accountdir"), DirectoryName);
423 } else if (cmbServerType->GetCurrentSelection() == 0){
425 // Create the account directory.
427 while(DirectoryCreated == FALSE){
429 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
431 if (wxMkdir(XestiaABUserDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
433 DirectoryCreated = TRUE;
434 cfgfile->Write(wxT("accountdir"), DirectoryName);
445 ReloadAccountConfig = TRUE;
455 void frmNewAccount::CloseWindow( wxCommandEvent& event )
457 ReloadAccountConfig = FALSE;
461 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
464 // Update the options.
466 if (cmbServerType->GetCurrentSelection() == 1){
468 txtServerAddress->Enable();
469 txtServerPort->Enable();
470 txtUsername->Enable();
471 txtPassword->Enable();
474 } else if (cmbServerType->GetCurrentSelection() == 0){
476 txtServerAddress->Disable();
477 txtServerPort->Disable();
478 txtUsername->Disable();
479 txtPassword->Disable();
480 chkUseSSL->Disable();
486 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
488 ReloadAccountConfig = ReloadAccountInc;