Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Implemented support using ConnectionObject/CardDAV2 on Win32 systems.
[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         std::string ReceivedServerPrefix = "";
35         
36         long PortNum = 80;
37         ValueData = txtPort->GetValue();
38         ValueData.ToLong(&PortNum, 10);
39         long RefreshNum = 1800;
40         ValueData = txtRefresh->GetValue();
41         ValueData.ToLong(&RefreshNum, 10);
42         bool UseSSL = true;
43         bool UsingSSLBypass = false;
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         }
81         CardDAV2 TestConnection(txtAddress->GetValue().ToStdString(),
82                 wxAtoi(txtPort->GetValue()),
83                 txtUsername->GetValue().ToStdString(),
84                 txtPassword->GetValue().ToStdString(),
85                 chkSSL->GetValue());
86         
87         // Test the connection.
88         
89         TestConnection.SetupConnectionObject();
90         COConnectResult TestConnectionResult = TestConnection.Connect(false);
91         
92         // If server is using SSL, verify that the SSL connection is valid.
93         
94         if (TestConnection.SSLVerify() == COSSL_UNABLETOVERIFY){
95 #if defined(__APPLE__)
96 #elif defined(__WIN32__)
98                 TestConnection.BypassSSLVerification(true);
100                 COConnectResult TestConnectionResult = TestConnection.Connect(false);
102                 TestConnection.BypassSSLVerification(false);
104                 BOOL ModifiedCertificateData = false;
105                 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&TestConnection, (HWND)this->GetHandle());
107                 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
108                         wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
109                         return;
110                 }
112                 if (ModifiedCertificateData == false) {
113                         wxMessageBox(_("An invalid certificate was received from the server."), _("Invalid certificate"));
114                         return;
115                 } else {
116                         TestConnection.BypassSSLVerification(true);
117                         COConnectResult TestConnectionResult = TestConnection.Connect(true);
118                         TestConnection.BypassSSLVerification(false);
119                 }
121 #else
122         
123                 // Connect again and fetch SSL certificate information.
124                 
125                 TestConnection.BypassSSLVerification(true);
126                 
127                 COConnectResult TestConnectionResult = TestConnection.Connect(false);
128                 
129                 TestConnection.BypassSSLVerification(false);
131                 SSLCertCollectionString CertData = TestConnection.BuildSSLCollection();
132                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
133                 
134                 frmICPtr->LoadDataNew(CertData, txtAddress->GetValue().ToStdString());
135                 frmICPtr->ShowModal();
136                                                                 
137                 int SSLResult = frmICPtr->GetResult();
138                                                         
139                 // Clean up before processing response.
140                                 
141                 delete frmICPtr;
142                 frmICPtr = NULL;
143                                                         
144                 // Process the response from the user.
145                                                         
146                 if (SSLResult == 1){
147                                                                 
148                         // Accept the Certificate.
149                         
150                         UsingSSLBypass = true;
151                         TestConnection.BypassSSLVerification(true);
152                 
153                         COConnectResult TestConnectionResult = TestConnection.Connect(true);
154                 
155                         TestConnection.BypassSSLVerification(false);
156                                                                 
157                 } else if (SSLResult == 2){
158                                                                 
159                         // Reject the certificate, abort the task.
161                         wxMessageBox(_("Server certficiate rejected. Unable to detect the prefix."), _("Failed"), wxOK+wxICON_ERROR);
162                         
163                         return; 
164                         
165                 }               
166                 
167 #endif
168         }
169         
170         // Get the server prefix if the connection was successful.
172         if (TestConnectionResult == COCONNECT_OK){
174                 COConnectResult TestConnectionResult = TestConnection.Connect(true);
176                 if (UsingSSLBypass == true){
177                         TestConnection.BypassSSLVerification(true);                     
178                 }
179                 
180                 COServerResponse PrefixRequestResult = TestConnection.GetDefaultPrefix(&ReceivedServerPrefix);
182                 if (UsingSSLBypass == true){
183                         TestConnection.BypassSSLVerification(true);                     
184                 }
185                 
186         } else {
187                 
188                 wxMessageBox(_("An error occured whilst detecting the prefix: ") + TestConnection.GetErrorMessage(), _("Failed"), wxOK+wxICON_ERROR);
189                 
190         }
191         
192         txtPrefix->SetValue(ReceivedServerPrefix);
193         
196 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
198         // Setup the account configuration file pointer.
199         
200         cfgfile = cfgin;
204 void frmEditAccount::LoadSettings( wxString AccNameIn ){
206         // Get the data from the accounts settings file and
207         // fill in the account fields.
209         AccName = AccNameIn;
210         long itemindex = 0;
211         bool ContinueAcc = TRUE;
212         wxString AccountName;
213         wxString AccountData;
214         
215         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
216         
217         while (ContinueAcc){
219                 if (AccountName == AccName){
221                         cfgfile->SetPath(AccountName);
222                         txtAccountName->SetValue(AccountName);
224                         cfgfile->Read("address", &AccountData);
225                         txtAddress->SetValue(AccountData);
227                         cfgfile->Read("port", &AccountData);
228                         txtPort->SetValue(AccountData);
230                         cfgfile->Read("username", &AccountData);
231                         txtUsername->SetValue(AccountData);
233                         cfgfile->Read("password", &AccountData);
234                         txtPassword->SetValue(AccountData);
236                         cfgfile->Read("prefix", &AccountData);
237                         txtPrefix->SetValue(AccountData);
239                         cfgfile->Read("ssl", &AccountData);
240                         if (AccountData == wxT("true")){
241                         
242                                 chkSSL->SetValue(TRUE);
243                         
244                         }
245                         
246                         cfgfile->Read("refresh", &AccountData);
247                         txtRefresh->SetValue(AccountData);
248                         
249                         break;
251                 }
252                 
253                 cfgfile->SetPath(wxT("/"));
254                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
256         }
260 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
263         // Check if server address matches against the blacklist.
264         // Bring up warning message if it does.
265         
266         if (CheckBlacklist(txtAddress->GetValue())){
267                 
268                 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);
269                 
270                 if (MessageBoxResult == wxNO){
271                         return;
272                 }
273                         
274         }
275         
276         // Update the settings for the account.
278         long itemindex = 0;
279         bool ContinueAcc = TRUE;
280         wxString AccountName;
282         // Look for the account
283         
284         while (ContinueAcc){
286                 if (AccountName == AccName){
288                         // Update the settings for the account.
290                         cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
292                         cfgfile->SetPath(txtAccountName->GetValue());
293                         //txtAccountName->SetValue(AccountName);
294                         
295                         cfgfile->DeleteEntry(wxT("address"), FALSE);
296                         cfgfile->Write(wxT("address"), txtAddress->GetValue());
298                         cfgfile->DeleteEntry(wxT("port"), FALSE);
299                         cfgfile->Write(wxT("port"), txtPort->GetValue());
301                         cfgfile->DeleteEntry(wxT("username"), FALSE);
302                         cfgfile->Write(wxT("username"), txtUsername->GetValue());
304                         cfgfile->DeleteEntry(wxT("password"), FALSE);
305                         cfgfile->Write(wxT("password"), txtPassword->GetValue());
307                         cfgfile->DeleteEntry(wxT("prefix"), FALSE);
308                         cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
310                         cfgfile->DeleteEntry(wxT("ssl"), FALSE);
312                         if (chkSSL->GetValue() == TRUE){
313                         
314                                 cfgfile->Write(wxT("ssl"), wxT("true"));
315                         
316                         } else {
317                         
318                                 cfgfile->Write(wxT("ssl"), wxT("false"));
319                         
320                         }
322                         cfgfile->DeleteEntry(wxT("refresh"), FALSE);
323                         cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
325                         break;
327                 }
328                 
329                 cfgfile->SetPath(wxT("/"));
330                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
332         }
334         // Set the dialog result to true and close the window.
335         
336         DialogResult = true;    
337         this->Close();  
341 void frmEditAccount::CloseWindow( wxCommandEvent& event )
343         
344         // Set the dialog result to false and close the window.
345         
346         DialogResult = false;
347         this->Close();
348         
351 bool frmEditAccount::GetDialogResult(){
353         // Get the result of the dialog.
354         
355         return DialogResult;
356         
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