Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added code to verify the SSL connection when detecting the address book URL.
[xestiaab/.git] / source / frmEditAccount.cpp
1 // frmEditAccount.cpp - Edit Account form.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "frmEditAccount.h"
20 #include "frmInvalidSSLCertificate.h"
21 #include "carddav/carddav.h"
23 frmEditAccount::frmEditAccount( wxWindow* parent )
24 :
25 frmEditAccountADT( parent )
26 {
28 }
30 void frmEditAccount::DetectAddressBook( wxCommandEvent& event )
31 {
33         // Check data before connecting.
35         wxString ValueData;
36         
37         long PortNum;
38         ValueData = txtPort->GetValue();
39         ValueData.ToLong(&PortNum, 10);
40         long RefreshNum;
41         ValueData = txtRefresh->GetValue();
42         ValueData.ToLong(&RefreshNum, 10);
43         bool UseSSL;
44         
45         if (txtAddress->IsEmpty()){
46         
47                 wxMessageBox(wxT("The server address cannot be blank."), wxT("Error"), wxICON_ERROR);
48                 return;
49         
50         }
51         
52         if (txtPort->IsEmpty() || PortNum < 0 || PortNum > 65535){
53         
54                 wxMessageBox(wxT("The server port needs to be between number 1 and 65535."), wxT("Error"), wxICON_ERROR);
55                 return;
56         
57         }
58         
59         if (txtUsername->IsEmpty()){
60         
61                 wxMessageBox(wxT("The server username cannot be blank."), wxT("Error"), wxICON_ERROR);
62                 return;
63         
64         }
66         if (txtPassword->IsEmpty()){
67         
68                 wxMessageBox(wxT("The server password cannot be blank."), wxT("Error"), wxICON_ERROR);  
69                 return;
70         
71         }
72         
73         UseSSL = chkSSL->GetValue();
74         
75         if (txtRefresh->IsEmpty() || RefreshNum < 300 || RefreshNum > 86400){
76         
77                 RefreshNum = 1800;
78                 
79         }
80         
81         // Setup a CardDAV object.
82         
83         CardDAV CDavObj;
84         
85         CDavObj.SetupConnection(txtAddress->GetValue(), (int)PortNum, txtUsername->GetValue(), txtPassword->GetValue(), UseSSL);
86         
87         // Verify SSL trust first before doing anything.
89         if (UseSSL == TRUE){
91                 CURLcode sslcode = CDavObj.SSLVerifyTest();
93                 if (sslcode == CURLE_OK){
94                                 
97                 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
99                         // Certificate is more than likely a self-signed or
100                         // expired certificate so display the invalid
101                         // SSL certificate message.
103                         // Setup the data to be sent in the wxPostEvent command.
105                         int SSLResult;
106                         
107                         frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
109                         frmICPtr->LoadDataNew(CDavObj.GetSSLVerifyResults(), txtAddress->GetValue());
110                         frmICPtr->ShowModal();
111                                         
112                         SSLResult = frmICPtr->GetResult();
113                                                         
114                         // Clean up before processing response.
115                                 
116                         delete frmICPtr;
117                         frmICPtr = NULL;
118                                                 
119                         // Process the response from the user.
120                                                         
121                         if (SSLResult == 1){
122                                                                 
123                                 // Accept the Certificate.
125                                 CDavObj.AllowSelfSignTest(TRUE);
126                                                                 
127                         } else if (SSLResult == 2){
128                                                                 
129                                 // Reject the certificate, abort the task and mark as failed.
131                                 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
132                                 return;
133                                                                 
134                         }
135                                 
137                 } else {
139                         // Something else happened. Stop the process and
140                         // display an error message instead.
142                         wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
143                         return;
145                 }
147         }
148         
149         // Attempt to extract the CardDAV address.
150         
151         wxString ABURL = CDavObj.GetDefaultAddressBookURL();
152         
153         txtPrefix->SetValue(ABURL);
154         
157 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
159         cfgfile = cfgin;
163 void frmEditAccount::LoadSettings( wxString AccNameIn ){
165         // Get the data from the accounts settings file.
167         AccName = AccNameIn;
168         long itemindex = 0;
169         bool ContinueAcc = TRUE;
170         wxString AccountName;
171         wxString AccountData;
173         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
175         while (ContinueAcc){
177                 if (AccountName == AccName){
179                         cfgfile->SetPath(AccountName);
180                         txtAccountName->SetValue(AccountName);
182                         cfgfile->Read("address", &AccountData);
183                         txtAddress->SetValue(AccountData);
185                         cfgfile->Read("port", &AccountData);
186                         txtPort->SetValue(AccountData);
188                         cfgfile->Read("username", &AccountData);
189                         txtUsername->SetValue(AccountData);
191                         cfgfile->Read("password", &AccountData);
192                         txtPassword->SetValue(AccountData);
194                         cfgfile->Read("prefix", &AccountData);
195                         txtPrefix->SetValue(AccountData);
197                         cfgfile->Read("ssl", &AccountData);
198                         if (AccountData == wxT("true")){
199                         
200                                 chkSSL->SetValue(TRUE);
201                         
202                         }
203                         
204                         cfgfile->Read("refresh", &AccountData);
205                         txtRefresh->SetValue(AccountData);
206                         
207                         break;
209                 }
210                 
211                 cfgfile->SetPath(wxT("/"));
212                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
214         }
218 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
221         // Update the settings for the account.
223         long itemindex = 0;
224         bool ContinueAcc = TRUE;
225         wxString AccountName;
227         // Look for the account
228         
229         while (ContinueAcc){
231                 if (AccountName == AccName){
233                         // Update the settings for the account.
235                         cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
237                         cfgfile->SetPath(txtAccountName->GetValue());
238                         //txtAccountName->SetValue(AccountName);
239                         
240                         cfgfile->DeleteEntry(wxT("address"), FALSE);
241                         cfgfile->Write(wxT("address"), txtAddress->GetValue());
243                         cfgfile->DeleteEntry(wxT("port"), FALSE);
244                         cfgfile->Write(wxT("port"), txtPort->GetValue());
246                         cfgfile->DeleteEntry(wxT("username"), FALSE);
247                         cfgfile->Write(wxT("username"), txtUsername->GetValue());
249                         cfgfile->DeleteEntry(wxT("password"), FALSE);
250                         cfgfile->Write(wxT("password"), txtPassword->GetValue());
252                         cfgfile->DeleteEntry(wxT("prefix"), FALSE);
253                         cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
255                         cfgfile->DeleteEntry(wxT("ssl"), FALSE);
257                         if (chkSSL->GetValue() == TRUE){
258                         
259                                 cfgfile->Write(wxT("ssl"), wxT("true"));
260                         
261                         } else {
262                         
263                                 cfgfile->Write(wxT("ssl"), wxT("false"));
264                         
265                         }
267                         cfgfile->DeleteEntry(wxT("refresh"), FALSE);
268                         cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
270                         /*cfgfile->Read("address", &AccountData);
271                         //txtAddress->SetValue(AccountData);
273                         cfgfile->Read("port", &AccountData);
274                         //txtPort->SetValue(AccountData);
276                         cfgfile->Read("username", &AccountData);
277                         //txtUsername->SetValue(AccountData);
279                         cfgfile->Read("password", &AccountData);
280                         //txtPassword->SetValue(AccountData);
282                         cfgfile->Read("prefix", &AccountData);
283                         //txtPrefix->SetValue(AccountData);
285                         cfgfile->Read("ssl", &AccountData);
286                         if (AccountData == wxT("true")){
287                         
288                                 chkSSL->SetValue(TRUE);
289                         
290                         }
291                         
292                         cfgfile->Read("refresh", &AccountData);
293                         //txtRefresh->SetValue(AccountData);*/
295                         
296                         break;
298                 }
299                 
300                 cfgfile->SetPath(wxT("/"));
301                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
303         }
305         DialogResult = true;    
306         this->Close();  
310 void frmEditAccount::CloseWindow( wxCommandEvent& event )
312         DialogResult = false;
313         this->Close();
316 bool frmEditAccount::GetDialogResult(){
317         
318         return DialogResult;
319         
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