Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Move INSTALL, LICENSE, README, THANKS and TODO into root directory
[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                 
97                 TestConnection.BypassSSLVerification(true);
98                 
99                 COConnectResult TestConnectionResult = TestConnection.Connect(false);
100                 
101                 TestConnection.BypassSSLVerification(false);
102                 
103                 int SSLResult = DisplayTrustPanel(&TestConnection);
104                 
105                 if (SSLResult != NSOKButton){
106                         
107                         wxMessageBox(_("An error occured whilst connnecting: ") + TestConnection.GetErrorMessage(), _("Failed"), wxOK+wxICON_ERROR);
108                         return;
109                         
110                 }
111                 
112 #elif defined(__WIN32__)
114                 TestConnection.BypassSSLVerification(true);
116                 COConnectResult TestConnectionResult = TestConnection.Connect(false);
118                 TestConnection.BypassSSLVerification(false);
120                 BOOL ModifiedCertificateData = false;
121                 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&TestConnection, (HWND)this->GetHandle());
123                 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
124                         wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
125                         return;
126                 }
128                 if (ModifiedCertificateData == false) {
129                         wxMessageBox(_("An invalid certificate was received from the server."), _("Invalid certificate"));
130                         return;
131                 } else {
132                         TestConnection.BypassSSLVerification(true);
133                         COConnectResult TestConnectionResult = TestConnection.Connect(true);
134                         TestConnection.BypassSSLVerification(false);
135                 }
137 #else
138         
139                 // Connect again and fetch SSL certificate information.
140                 
141                 TestConnection.BypassSSLVerification(true);
142                 
143                 COConnectResult TestConnectionResult = TestConnection.Connect(false);
144                 
145                 TestConnection.BypassSSLVerification(false);
147                 SSLCertCollectionString CertData = TestConnection.BuildSSLCollection();
148                 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
149                 
150                 frmICPtr->LoadDataNew(CertData, txtAddress->GetValue().ToStdString());
151                 frmICPtr->ShowModal();
152                                                                 
153                 int SSLResult = frmICPtr->GetResult();
154                                                         
155                 // Clean up before processing response.
156                                 
157                 delete frmICPtr;
158                 frmICPtr = NULL;
159                                                         
160                 // Process the response from the user.
161                                                         
162                 if (SSLResult == 1){
163                                                                 
164                         // Accept the Certificate.
165                         
166                         UsingSSLBypass = true;
167                         TestConnection.BypassSSLVerification(true);
168                 
169                         COConnectResult TestConnectionResult = TestConnection.Connect(true);
170                                                                 
171                 } else if (SSLResult == 2){
172                                                                 
173                         // Reject the certificate, abort the task.
175                         wxMessageBox(_("Server certficiate rejected. Unable to detect the prefix."), _("Failed"), wxOK+wxICON_ERROR);
176                         
177                         return; 
178                         
179                 }               
180                 
181 #endif
182         }
183         
184         // Get the server prefix if the connection was successful.
186         if (TestConnectionResult == COCONNECT_OK){
188                 COConnectResult TestConnectionResult = TestConnection.Connect(true);
190                 if (UsingSSLBypass == true){
191                         TestConnection.BypassSSLVerification(true);                     
192                 }
193                 
194                 COServerResponse PrefixRequestResult = TestConnection.GetDefaultPrefix(&ReceivedServerPrefix);
196                 if (UsingSSLBypass == true){
197                         TestConnection.BypassSSLVerification(true);                     
198                 }
199                 
200         } else {
201                 
202                 wxMessageBox(_("An error occured whilst detecting the prefix: ") + TestConnection.GetErrorMessage(), _("Failed"), wxOK+wxICON_ERROR);
203                 
204         }
205         
206         txtPrefix->SetValue(ReceivedServerPrefix);
207         
210 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
212         // Setup the account configuration file pointer.
213         
214         cfgfile = cfgin;
218 void frmEditAccount::LoadSettings( wxString AccNameIn ){
220         // Get the data from the accounts settings file and
221         // fill in the account fields.
223         AccName = AccNameIn;
224         long itemindex = 0;
225         bool ContinueAcc = TRUE;
226         wxString AccountName;
227         wxString AccountData;
228         
229         ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
230         
231         while (ContinueAcc){
233                 if (AccountName == AccName){
235                         cfgfile->SetPath(AccountName);
236                         txtAccountName->SetValue(AccountName);
238                         cfgfile->Read("address", &AccountData);
239                         txtAddress->SetValue(AccountData);
241                         cfgfile->Read("port", &AccountData);
242                         txtPort->SetValue(AccountData);
244                         cfgfile->Read("username", &AccountData);
245                         txtUsername->SetValue(AccountData);
247                         cfgfile->Read("password", &AccountData);
248                         txtPassword->SetValue(AccountData);
250                         cfgfile->Read("prefix", &AccountData);
251                         txtPrefix->SetValue(AccountData);
253                         cfgfile->Read("ssl", &AccountData);
254                         if (AccountData == wxT("true")){
255                         
256                                 chkSSL->SetValue(TRUE);
257                         
258                         }
259                         
260                         cfgfile->Read("refresh", &AccountData);
261                         txtRefresh->SetValue(AccountData);
262                         
263                         break;
265                 }
266                 
267                 cfgfile->SetPath(wxT("/"));
268                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
270         }
274 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
277         // Check if server address matches against the blacklist.
278         // Bring up warning message if it does.
279         
280         if (CheckBlacklist(txtAddress->GetValue())){
281                 
282                 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);
283                 
284                 if (MessageBoxResult == wxNO){
285                         return;
286                 }
287                         
288         }
289         
290         // Update the settings for the account.
292         long itemindex = 0;
293         bool ContinueAcc = TRUE;
294         wxString AccountName;
296         // Look for the account
297         
298         while (ContinueAcc){
300                 if (AccountName == AccName){
302                         // Update the settings for the account.
304                         cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
306                         cfgfile->SetPath(txtAccountName->GetValue());
307                         //txtAccountName->SetValue(AccountName);
308                         
309                         cfgfile->DeleteEntry(wxT("address"), FALSE);
310                         cfgfile->Write(wxT("address"), txtAddress->GetValue());
312                         cfgfile->DeleteEntry(wxT("port"), FALSE);
313                         cfgfile->Write(wxT("port"), txtPort->GetValue());
315                         cfgfile->DeleteEntry(wxT("username"), FALSE);
316                         cfgfile->Write(wxT("username"), txtUsername->GetValue());
318                         cfgfile->DeleteEntry(wxT("password"), FALSE);
319                         cfgfile->Write(wxT("password"), txtPassword->GetValue());
321                         cfgfile->DeleteEntry(wxT("prefix"), FALSE);
322                         cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
324                         cfgfile->DeleteEntry(wxT("ssl"), FALSE);
326                         if (chkSSL->GetValue() == TRUE){
327                         
328                                 cfgfile->Write(wxT("ssl"), wxT("true"));
329                         
330                         } else {
331                         
332                                 cfgfile->Write(wxT("ssl"), wxT("false"));
333                         
334                         }
336                         cfgfile->DeleteEntry(wxT("refresh"), FALSE);
337                         cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
339                         break;
341                 }
342                 
343                 cfgfile->SetPath(wxT("/"));
344                 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
346         }
348         // Set the dialog result to true and close the window.
349         
350         DialogResult = true;    
351         this->Close();  
355 void frmEditAccount::CloseWindow( wxCommandEvent& event )
357         
358         // Set the dialog result to false and close the window.
359         
360         DialogResult = false;
361         this->Close();
362         
365 bool frmEditAccount::GetDialogResult(){
367         // Get the result of the dialog.
368         
369         return DialogResult;
370         
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