Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Merge branch 'master' of ssh://gelforn.xestia.co.uk/scmrepos/xestiaab
[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"
21 frmEditAccount::frmEditAccount( wxWindow* parent )
22 :
23 frmEditAccountADT( parent )
24 {
26 }
28 void frmEditAccount::DetectAddressBook( wxCommandEvent& event )
29 {
31         // Check data before connecting.
33         wxString ValueData;
34         
35         long PortNum;
36         ValueData = txtPort->GetValue();
37         ValueData.ToLong(&PortNum, 10);
38         long RefreshNum;
39         ValueData = txtRefresh->GetValue();
40         ValueData.ToLong(&RefreshNum, 10);
41         bool UseSSL;
42         
43         if (txtAddress->IsEmpty()){
44         
45                 wxMessageBox(wxT("The server address cannot be blank."), wxT("Error"), wxICON_ERROR);
46                 return;
47         
48         }
49         
50         if (txtPort->IsEmpty() || PortNum < 0 || PortNum > 65535){
51         
52                 wxMessageBox(wxT("The server port needs to be between number 1 and 65535."), wxT("Error"), wxICON_ERROR);
53                 return;
54         
55         }
56         
57         if (txtUsername->IsEmpty()){
58         
59                 wxMessageBox(wxT("The server username cannot be blank."), wxT("Error"), wxICON_ERROR);
60                 return;
61         
62         }
64         if (txtPassword->IsEmpty()){
65         
66                 wxMessageBox(wxT("The server password cannot be blank."), wxT("Error"), wxICON_ERROR);  
67                 return;
68         
69         }
70         
71         UseSSL = chkSSL->GetValue();
72         
73         if (txtRefresh->IsEmpty() || RefreshNum < 300 || RefreshNum > 86400){
74         
75                 RefreshNum = 1800;
76                 
77         }
78         
79         // Setup a CardDAV object.
80         
81         CardDAV CDavObj;
82         
83         CDavObj.SetupConnection(txtAddress->GetValue(), (int)PortNum, txtUsername->GetValue(), txtPassword->GetValue(), UseSSL);
84         
85         // Verify SSL trust first before doing anything.
87         if (UseSSL == TRUE){
89                 CURLcode sslcode = CDavObj.SSLVerifyTest();
91                 if (sslcode == CURLE_OK){
92                                 
93                         // Certificate is okay. Do nothing.
95                 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
97                         // Certificate is more than likely a self-signed or
98                         // expired certificate so display the invalid
99                         // SSL certificate message.
101                         // Setup the data to be sent in the wxPostEvent command.
103                         int SSLResult;
104                         
105 #if defined(__APPLE__)
106         
107                         SSLResult = DisplayTrustPanel(&CDavObj);
109 #elif defined(__WIN32__)
111                         BOOL ModifiedCertificateData;
112                         CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&CDavObj, (HWND)this->GetHandle());
114                         if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)){
115                                 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
116                                 return;
117                         }
119                         if (ModifiedCertificateData == FALSE){
120                                 wxMessageBox(_("Unable to get server prefix without a valid SSL certificate."), _("Error getting server prefix"));
121                                 return;
122                         }
124 #else
125                         
126                         frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
128                         frmICPtr->LoadDataNew(CDavObj.GetSSLVerifyResults(), txtAddress->GetValue());
129                         frmICPtr->ShowModal();
130                                         
131                         SSLResult = frmICPtr->GetResult();
132                                                         
133                         // Clean up before processing response.
134                                 
135                         delete frmICPtr;
136                         frmICPtr = NULL;
137                         
138 #endif
139                         
140                         // Process the response from the user.
141                                                         
142                         if (SSLResult == 1){
143                                                                 
144                                 // Accept the Certificate.
146                                 CDavObj.AllowSelfSignTest(TRUE);
147                                                                 
148                         } else if (SSLResult == 2){
149                                                                 
150                                 // Reject the certificate, abort the task and mark as failed.
152                                 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
153                                 return;
154                                                                 
155                         }
157                 } else {
159                         // Something else happened. Stop the process and
160                         // display an error message instead.
162                         wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
163                         return;
165                 }
167         }
168         
169         // Attempt to extract the CardDAV address.
170         
171         wxString ABURL = CDavObj.GetDefaultAddressBookURL();
172         
173         txtPrefix->SetValue(ABURL);
174         
177 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
179         // Setup the account configuration file pointer.
180         
181         cfgfile = cfgin;
185 void frmEditAccount::LoadSettings( wxString AccNameIn ){
187         // Get the data from the accounts settings file and
188         // fill in the account fields.
190         AccName = AccNameIn;
191         long itemindex = 0;
192         bool ContinueAcc = TRUE;
193         wxString AccountName;
194         wxString AccountData;
195         
196         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
197         
198         while (ContinueAcc){
200                 if (AccountName == AccName){
202                         cfgfile->SetPath(AccountName);
203                         txtAccountName->SetValue(AccountName);
205                         cfgfile->Read("address", &AccountData);
206                         txtAddress->SetValue(AccountData);
208                         cfgfile->Read("port", &AccountData);
209                         txtPort->SetValue(AccountData);
211                         cfgfile->Read("username", &AccountData);
212                         txtUsername->SetValue(AccountData);
214                         cfgfile->Read("password", &AccountData);
215                         txtPassword->SetValue(AccountData);
217                         cfgfile->Read("prefix", &AccountData);
218                         txtPrefix->SetValue(AccountData);
220                         cfgfile->Read("ssl", &AccountData);
221                         if (AccountData == wxT("true")){
222                         
223                                 chkSSL->SetValue(TRUE);
224                         
225                         }
226                         
227                         cfgfile->Read("refresh", &AccountData);
228                         txtRefresh->SetValue(AccountData);
229                         
230                         break;
232                 }
233                 
234                 cfgfile->SetPath(wxT("/"));
235                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
237         }
241 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
244         // Check if server address matches against the blacklist.
245         // Bring up warning message if it does.
246         
247         if (CheckBlacklist(txtAddress->GetValue())){
248                 
249                 int MessageBoxResult = wxMessageBox(_("The server with the address given does not support the CardDAV protocol properly and shouldn't be used.\n\nData loss is very likely.\n\nDo you still want to continue using this server?"), _("Server warning"), wxYES_NO, this);
250                 
251                 if (MessageBoxResult == wxNO){
252                         return;
253                 }
254                         
255         }
256         
257         // Update the settings for the account.
259         long itemindex = 0;
260         bool ContinueAcc = TRUE;
261         wxString AccountName;
263         // Look for the account
264         
265         while (ContinueAcc){
267                 if (AccountName == AccName){
269                         // Update the settings for the account.
271                         cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
273                         cfgfile->SetPath(txtAccountName->GetValue());
274                         //txtAccountName->SetValue(AccountName);
275                         
276                         cfgfile->DeleteEntry(wxT("address"), FALSE);
277                         cfgfile->Write(wxT("address"), txtAddress->GetValue());
279                         cfgfile->DeleteEntry(wxT("port"), FALSE);
280                         cfgfile->Write(wxT("port"), txtPort->GetValue());
282                         cfgfile->DeleteEntry(wxT("username"), FALSE);
283                         cfgfile->Write(wxT("username"), txtUsername->GetValue());
285                         cfgfile->DeleteEntry(wxT("password"), FALSE);
286                         cfgfile->Write(wxT("password"), txtPassword->GetValue());
288                         cfgfile->DeleteEntry(wxT("prefix"), FALSE);
289                         cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
291                         cfgfile->DeleteEntry(wxT("ssl"), FALSE);
293                         if (chkSSL->GetValue() == TRUE){
294                         
295                                 cfgfile->Write(wxT("ssl"), wxT("true"));
296                         
297                         } else {
298                         
299                                 cfgfile->Write(wxT("ssl"), wxT("false"));
300                         
301                         }
303                         cfgfile->DeleteEntry(wxT("refresh"), FALSE);
304                         cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
306                         break;
308                 }
309                 
310                 cfgfile->SetPath(wxT("/"));
311                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
313         }
315         // Set the dialog result to true and close the window.
316         
317         DialogResult = true;    
318         this->Close();  
322 void frmEditAccount::CloseWindow( wxCommandEvent& event )
324         
325         // Set the dialog result to false and close the window.
326         
327         DialogResult = false;
328         this->Close();
329         
332 bool frmEditAccount::GetDialogResult(){
334         // Get the result of the dialog.
335         
336         return DialogResult;
337         
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