Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added code so that OSX is now supported.
[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 {
19     
20     wxString CheckAccName = txtAccountName->GetValue();
21     
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.
116         
117         lblServerConnResult->SetLabel(wxT(""));
118         lblServerResponse->SetLabel(wxT(""));
119         lblServerSSLResult->SetLabel(wxT(""));
120         lblServerSSLValid->SetLabel(wxT(""));
121         lblAbleToLoginResult->SetLabel(wxT(""));
122         lblCardDAVSupportResult->SetLabel(wxT(""));
123         
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){
177             
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);
211         
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;
252         
253 #if defined(__HAIKU__)
254         
255         //preffilename = wxT("noo");
256         
257 #elif defined(__WIN32__)
258         
259         XestiaABPrefDirectory = GetUserDir();
260         
261         AccountSettingsFile = XestiaABPrefDirectory + wxT("accounts");
262         
263         // Open the file for writing.
264         
265         wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
266         
267         // Check if account name already exists and return an error message
268         // if this is the case.
269         
270         wxString AccountName;
271         long itemindex = 0;
272         bool ContinueAcc;
273         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
274         
275         while (ContinueAcc){
276             
277             if (txtAccountName->GetValue() == AccountName){
278                 
279                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
280                 return;
281                 
282             }
283             
284             cfgfile->SetPath(wxT("/"));
285             ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
286             
287         }
288         
289         if (cmbServerType->GetCurrentSelection() == 1){
290             
291             // Create the account directory.
292             
293             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
294             
295             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
296                 
297                 DirectoryCreated = TRUE;
298                 
299             }
300             
301             if (DirectoryCreated == TRUE){
302                 
303                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
304                 
305             } else {
306                 
307                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
308                 return;
309                 
310             }
311             
312         } else if (cmbServerType->GetCurrentSelection() == 0){
313             
314             // Create the account directory.
315             
316             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
317             
318             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
319                 
320                 DirectoryCreated = TRUE;
321                 
322             }
323             
324             if (DirectoryCreated == TRUE){
325                 
326                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
327                 
328             } else {
329                 
330                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
331                 return;
332                 
333             }
334             
335         }
336         
337         delete cfgfile;
338         cfgfile = NULL;
339         
340         ReloadAccountConfig = TRUE;
341         
342 #else
343         
344         XestiaABPrefDirectory = GetUserDir();
345         
346         AccountSettingsFile = GetUserPrefDir() + wxT("accounts");
347         
348         // Open the file for writing.
349         
350         wxFileConfig *cfgfile = new wxFileConfig("", "", AccountSettingsFile);
351         
352         // Check if account name already exists and return an error message
353         // if this is the case.
354         
355         wxString AccountName;
356         long itemindex = 0;
357         bool ContinueAcc;
358         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
359         
360         while (ContinueAcc){
361             
362             if (txtAccountName->GetValue() == AccountName){
363                 
364                 wxMessageBox(_("The selected account name is already used, please use another account name."), _("Account name already used"), wxICON_ERROR);
365                 return;
366                 
367             }
368             
369             cfgfile->SetPath(wxT("/"));
370             ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
371             
372         }
373         
374         if (cmbServerType->GetCurrentSelection() == 1){
375             
376             // Create the account directory.
377             
378             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
379             
380             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".carddav"), 0740) == TRUE){
381                 
382                 DirectoryCreated = TRUE;
383                 
384             }
385             
386             if (DirectoryCreated == TRUE){
387                 
388                 WriteAccountDetails(cfgfile, wxT("CardDAV"), DirectoryName);
389                 
390             } else {
391                 
392                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
393                 return;
394                 
395             }
396             
397         } else if (cmbServerType->GetCurrentSelection() == 0){
398             
399             // Create the account directory.
400             
401             wxString DirectoryName = txtAccountName->GetValue().Mid(0, 30) + RandomNumberSuffix;
402             
403             if (wxMkdir(XestiaABPrefDirectory + wxT("/accounts/") + DirectoryName + wxT(".local"), 0740) == TRUE){
404                 
405                 DirectoryCreated = TRUE;
406                 
407             }
408             
409             if (DirectoryCreated == TRUE){
410                 
411                 WriteAccountDetails(cfgfile, wxT("Local"), DirectoryName);
412                 
413             } else {
414                 
415                 wxMessageBox(_("An error occured whilst creating the account directory."), _("Cannot create account directory"));
416                 return;
417                 
418             }
419             
420         }
421         
422         delete cfgfile;
423         cfgfile = NULL;
424         
425         ReloadAccountConfig = TRUE;
426         
427 #endif
428         
429         this->Close();
430         
431     }
432     
435 void frmNewAccount::WriteAccountDetails(wxFileConfig *cfgfilein, wxString AccountType, wxString DirectoryName){
436     
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);
444     
445     if (chkUseSSL->GetValue() == TRUE){
446         
447         cfgfilein->Write(wxT("ssl"), wxT("true"));
448         
449     } else {
450         
451         cfgfilein->Write(wxT("ssl"), wxT("false"));
452         
453     }
454     
455     cfgfilein->Write(wxT("refresh"), wxT("1800"));
456     cfgfilein->Write(wxT("type"), AccountType);
457     
460 void frmNewAccount::CloseWindow( wxCommandEvent& event )
462     ReloadAccountConfig = FALSE;
463     this->Close();
466 void frmNewAccount::UpdateRequirements( wxCommandEvent& event )
468     
469     // Update the options.
470     
471     if (cmbServerType->GetCurrentSelection() == 1){
472         
473         txtServerAddress->Enable();
474         txtServerPort->Enable();
475         txtUsername->Enable();
476         txtPassword->Enable();
477         chkUseSSL->Enable();
478         
479     } else if (cmbServerType->GetCurrentSelection() == 0){
480         
481         txtServerAddress->Disable();
482         txtServerPort->Disable();
483         txtUsername->Disable();
484         txtPassword->Disable();
485         chkUseSSL->Disable();
486         
487     }
488     
491 void frmNewAccount::SetupPointers(bool *ReloadAccountInc){
492     
493     ReloadAccountConfig = ReloadAccountInc;
494     
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