Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Initial import of code already done for Xestia Address Book
[xestiaab/.git] / source / frmNewAccount.cpp
1 #include "frmNewAccount.h"
2 #include <thread>
3 #include <cstdlib>
4 #include <wx/filefn.h>
5 #include <wx/fileconf.h>
7 #include "carddav/carddav.h"
8 #include "common/dirs.h"
10 frmNewAccount::frmNewAccount( wxWindow* parent )
11 :
12 frmNewAccountADT( parent )
13 {
14         btnPrevious->Disable();
15 }
17 void frmNewAccount::CheckAccountName( wxCommandEvent& event )
18 {
20         wxString CheckAccName = txtAccountName->GetValue();
22         if (txtAccountName->IsEmpty() && PageSeek == 2 || CheckAccName.Len() < 4){
23         
24                 btnNext->Disable();
25         
26         } else {
27         
28                 btnNext->Enable();
29         
30         }
31         
32 }
34 void frmNewAccount::ProcessPrevious( wxCommandEvent& event )
35 {
36         PageSeek--;
37         
38         if (PageSeek == 0){
39         
40                 // Currently at the Connection test screen.
41                 
42                 //tbkAccount->ChangeSelection(0);
43                 tabConn->Hide();
44                 tabFinish->Hide();
45                 tabType->Show();
46                 szrNewAccount->RecalcSizes();
47                 
48                 btnPrevious->Disable();
49                 btnNext->Enable();
50         
51         } else if (PageSeek == 1){
52         
53                 if (cmbServerType->GetCurrentSelection() == 0){
54                 
55                         tabConn->Hide();
56                         tabFinish->Hide();
57                         tabType->Show();
58                         PageSeek = 0;
59                         btnPrevious->Disable();
60                         btnNext->Enable();
61                         btnNext->SetLabel(_("Next >"));                 
62                         return;
63                 
64                 }
65         
66                 // Currently at the Finish screen.
67                 
68                 tabType->Hide();
69                 tabConn->Show();
70                 tabFinish->Hide();
71                 szrNewAccount->RecalcSizes();
72                 
73                 btnNext->SetLabel(_("Next >"));
74                 btnNext->Enable();
75         
76         }       
77         
78 }
80 void frmNewAccount::ProcessNext( wxCommandEvent& event )
81 {
82         PageSeek++;
83         
84         if (PageSeek == 1){
85         
86                 if (cmbServerType->GetCurrentSelection() == 0){
87                 
88                         tabType->Hide();
89                         tabConn->Hide();
90                         tabFinish->Show();
91                         PageSeek = 2;
92                         btnPrevious->Enable();
93                         szrNewAccount->RecalcSizes();
94                         btnNext->Disable();
95                         btnNext->SetLabel(_("Finish"));
96                         return;
97                 
98                 }
99         
100                 btnNext->Disable();
101         
102                 bool ServerResult = FALSE;
103                 bool ServerAction = FALSE;
104                 bool UseSSL = TRUE;
105                 wxString ServerMessage;
106         
107                 // Connection test screen.
108                 
109                 tabType->Hide();
110                 tabConn->Show();
111                 tabFinish->Hide();
112                 szrNewAccount->RecalcSizes();
113                 btnPrevious->Enable();
114                 
115                 // Reset screen.
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.
125                 
126                 CardDAV CardDAVConn;
127                 
128                 lblServerConnResult->SetLabel(_("Testing..."));
129                 
130                 UseSSL = chkUseSSL->GetValue();
131                 
132                 CardDAVConn.SetupConnection(txtServerAddress->GetValue(), 
133                         wxAtoi(txtServerPort->GetValue()), 
134                         txtUsername->GetValue(), 
135                         txtPassword->GetValue(),
136                         UseSSL);
137                 CardDAVConn.SetupResultBools(&ServerResult, &ServerAction);
138                 
139                 std::thread ConnTest(&CardDAV::Connect, &CardDAVConn);
140                 
141                 ConnTest.join();
142                 
143                 if (ServerResult == FALSE){
144                 
145                         lblServerConnResult->SetLabel(_("Failed"));
146                         wxMessageBox(CardDAVConn.GetErrorMessage());
147                         return;
148                 
149                 } else {
150                 
151                         lblServerConnResult->SetLabel(_("Connected"));
152                 
153                 }
154                 
155                 if (CardDAVConn.HasValidResponse() == TRUE){
156                 
157                         lblServerResponse->SetLabel(_("Yes"));
158                 
159                 } else {
160                 
161                         lblServerResponse->SetLabel(_("No"));
162                 
163                 }               
164                 
165                 if (CardDAVConn.CanDoSSL() == TRUE){
166                 
167                         lblCardDAVSupportResult->SetLabel(_("Used"));
168                 
169                 } else {
170                 
171                         lblServerSSLResult->SetLabel(_("Not Used"));
172                         lblServerSSLValid->SetLabel(_("Not Applicable"));
173                 
174                 }               
175                 
176                 if (CardDAVConn.SSLVerify() == TRUE && CardDAVConn.CanDoSSL() == TRUE){
178                         lblCardDAVSupportResult->SetLabel(_("Verified"));
179                 
180                 } else if (CardDAVConn.SSLVerify() == FALSE && CardDAVConn.CanDoSSL() == TRUE) {
181                 
182                         lblServerSSLResult->SetLabel(_("Unable to verify"));
183                 
184                 }
185                 
186                 if (CardDAVConn.CanDoCardDAV() == TRUE){
187                 
188                         lblCardDAVSupportResult->SetLabel(_("Supported"));
189                 
190                 } else {
191                 
192                         lblCardDAVSupportResult->SetLabel(_("Unsupported"));            
193                 
194                 }
195                 
196                 if (CardDAVConn.AbleToLogin() == TRUE){
197                 
198                         lblAbleToLoginResult->SetLabel(_("Yes"));
199                 
200                 } else {
201                 
202                         lblAbleToLoginResult->SetLabel(_("No"));
203                 
204                 }
205                 
206                 // Get the address to process CardDAV requests.
207                 
208                 ServerPrefix = CardDAVConn.GetDefaultAddressBookURL();
209                 
210                 wxMessageBox(ServerPrefix);
212                 if (ServerResult == TRUE && CardDAVConn.HasValidResponse() == TRUE &&
213                 //CardDAVConn.CanDoSSL() == TRUE && CardDAVConn.SSLVerify() == TRUE &&
214                 CardDAVConn.CanDoCardDAV() == TRUE && CardDAVConn.AbleToLogin() == TRUE){
215                 
216                         btnNext->Enable();
217                         
218                         lblConnectionResultText->SetLabel(_("Click on Next to set the account name."));
219                 
220                 } else {
221                 
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)."));
223                 
224                 }
225         
226         } else if (PageSeek == 2){
227         
228                 // Finish screen.
229                 
230                 tabType->Hide();
231                 tabConn->Hide();
232                 tabFinish->Show();
233                 szrNewAccount->RecalcSizes();
234                 
235                 btnNext->Disable();
236                 btnNext->SetLabel(_("Finish"));
237                 if (txtAccountName->IsEmpty() && PageSeek == 2){
238                         btnNext->Disable();
239                 } else {
240                         btnNext->Enable();
241                 }
242         
243         } else if (PageSeek == 3){
244         
245                 // Finished.
246                 
247                 wxString XestiaABPrefDirectory;
248                 wxString AccountSettingsFile;
249                 //wxFile ASFile;
250                 wxString RandomNumberSuffix = wxString::Format(wxT("%i"), rand() % 32767);
251                 bool DirectoryCreated = FALSE;
253 #if defined(__HAIKU__)
254     
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;
271                 long itemindex = 0;
272                 bool ContinueAcc;       
273                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
275                 while (ContinueAcc){
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);
280                                 return;
282                         }
284                         cfgfile->SetPath(wxT("/"));
285                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
287                 }
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"));
300                 } else {
302                         cfgfile->Write(wxT("ssl"), wxT("false"));
304                 }
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);
322                                 } else {
326                                 }
328                         }
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);
346                                 }
348                         }
350                 }
352                 delete cfgfile;
353                 cfgfile = NULL;
355                 ReloadAccountConfig = TRUE;             
357 #else
359                 XestiaABUserDirectory = GetUserPrefDir();
361                 AccountSettingsFile = XestiaABUserDirectory + wxT("accounts");
362                 
363                 // Open the file for writing.
364                 
365                 wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
366                 
367                 // Check if account name already exists and return an error message
368                 // if this is the case.
369                 
370                 wxString AccountName;
371                 long itemindex = 0;
372                 bool ContinueAcc;       
373                 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
375                 while (ContinueAcc){
376                 
377                         if (txtAccountName->GetValue() == AccountName){
378                         
379                                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
380                                 return;
381                         
382                         }
383                 
384                         cfgfile->SetPath(wxT("/"));
385                         ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
386                 
387                 }
388                 
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);
395                 
396                 if (chkUseSSL->GetValue() == TRUE){
397                 
398                         cfgfile->Write(wxT("ssl"), wxT("true"));
399                 
400                 } else {
401                 
402                         cfgfile->Write(wxT("ssl"), wxT("false"));
403                 
404                 }
406                 cfgfile->Write(wxT("refresh"), wxT("1800"));
408                 if (cmbServerType->GetCurrentSelection() == 1){
409                 
410                         while(DirectoryCreated == FALSE){
412                                 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
413         
414                                 if (wxMkdir(XestiaABUserDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
415                 
416                                         DirectoryCreated = TRUE;
417                                         cfgfile->Write(wxT("accountdir"), DirectoryName);
418                 
419                                 }
420                         
421                         }
422                 
423                 } else if (cmbServerType->GetCurrentSelection() == 0){
424                 
425                         // Create the account directory.
426                         
427                         while(DirectoryCreated == FALSE){
428                         
429                                 wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
430                         
431                                 if (wxMkdir(XestiaABUserDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
432                                 
433                                         DirectoryCreated = TRUE;
434                                         cfgfile->Write(wxT("accountdir"), DirectoryName);
435                                 
436                                 }
437                         
438                         }
439                         
440                 }
442                 delete cfgfile;
443                 cfgfile = NULL;
444                 
445                 ReloadAccountConfig = TRUE;
447 #endif
449                 this->Close();
450         
451         }
455 void frmNewAccount::CloseWindow( wxCommandEvent& event )
457         ReloadAccountConfig = FALSE;
458         this->Close();
461 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
464         // Update the options.
465         
466         if (cmbServerType->GetCurrentSelection() == 1){
468                 txtServerAddress->Enable();
469                 txtServerPort->Enable();
470                 txtUsername->Enable();
471                 txtPassword->Enable();
472                 chkUseSSL->Enable();
473         
474         } else if (cmbServerType->GetCurrentSelection() == 0){
475         
476                 txtServerAddress->Disable();
477                 txtServerPort->Disable();
478                 txtUsername->Disable();
479                 txtPassword->Disable();
480                 chkUseSSL->Disable();
481         
482         }
486 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
488         ReloadAccountConfig = ReloadAccountInc;
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy