1 // frmEditAccount.cpp - Edit Account form.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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 )
23 frmEditAccountADT( parent )
28 void frmEditAccount::DetectAddressBook( wxCommandEvent& event )
31 // Check data before connecting.
33 wxString ValueData = "";
34 std::string ReceivedServerPrefix = "";
37 ValueData = txtPort->GetValue();
38 ValueData.ToLong(&PortNum, 10);
39 long RefreshNum = 1800;
40 ValueData = txtRefresh->GetValue();
41 ValueData.ToLong(&RefreshNum, 10);
43 bool UsingSSLBypass = false;
45 if (txtAddress->IsEmpty()){
47 wxMessageBox(wxT("The server address cannot be blank."), wxT("Error"), wxICON_ERROR);
52 if (txtPort->IsEmpty() || PortNum < 0 || PortNum > 65535){
54 wxMessageBox(wxT("The server port needs to be between number 1 and 65535."), wxT("Error"), wxICON_ERROR);
59 if (txtUsername->IsEmpty()){
61 wxMessageBox(wxT("The server username cannot be blank."), wxT("Error"), wxICON_ERROR);
66 if (txtPassword->IsEmpty()){
68 wxMessageBox(wxT("The server password cannot be blank."), wxT("Error"), wxICON_ERROR);
73 UseSSL = chkSSL->GetValue();
75 if (txtRefresh->IsEmpty() || RefreshNum < 300 || RefreshNum > 86400){
81 CardDAV2 TestConnection(txtAddress->GetValue().ToStdString(),
82 wxAtoi(txtPort->GetValue()),
83 txtUsername->GetValue().ToStdString(),
84 txtPassword->GetValue().ToStdString(),
87 // Test the connection.
89 TestConnection.SetupConnectionObject();
90 COConnectResult TestConnectionResult = TestConnection.Connect(false);
92 // If server is using SSL, verify that the SSL connection is valid.
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"));
112 if (ModifiedCertificateData == false) {
113 wxMessageBox(_("An invalid certificate was received from the server."), _("Invalid certificate"));
116 TestConnection.BypassSSLVerification(true);
117 COConnectResult TestConnectionResult = TestConnection.Connect(true);
118 TestConnection.BypassSSLVerification(false);
123 // Connect again and fetch SSL certificate information.
125 TestConnection.BypassSSLVerification(true);
127 COConnectResult TestConnectionResult = TestConnection.Connect(false);
129 TestConnection.BypassSSLVerification(false);
131 SSLCertCollectionString CertData = TestConnection.BuildSSLCollection();
132 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
134 frmICPtr->LoadDataNew(CertData, txtAddress->GetValue().ToStdString());
135 frmICPtr->ShowModal();
137 int SSLResult = frmICPtr->GetResult();
139 // Clean up before processing response.
144 // Process the response from the user.
148 // Accept the Certificate.
150 UsingSSLBypass = true;
151 TestConnection.BypassSSLVerification(true);
153 COConnectResult TestConnectionResult = TestConnection.Connect(true);
155 TestConnection.BypassSSLVerification(false);
157 } else if (SSLResult == 2){
159 // Reject the certificate, abort the task.
161 wxMessageBox(_("Server certficiate rejected. Unable to detect the prefix."), _("Failed"), wxOK+wxICON_ERROR);
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);
180 COServerResponse PrefixRequestResult = TestConnection.GetDefaultPrefix(&ReceivedServerPrefix);
182 if (UsingSSLBypass == true){
183 TestConnection.BypassSSLVerification(true);
188 wxMessageBox(_("An error occured whilst detecting the prefix: ") + TestConnection.GetErrorMessage(), _("Failed"), wxOK+wxICON_ERROR);
192 txtPrefix->SetValue(ReceivedServerPrefix);
196 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
198 // Setup the account configuration file pointer.
204 void frmEditAccount::LoadSettings( wxString AccNameIn ){
206 // Get the data from the accounts settings file and
207 // fill in the account fields.
211 bool ContinueAcc = TRUE;
212 wxString AccountName;
213 wxString AccountData;
215 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
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")){
242 chkSSL->SetValue(TRUE);
246 cfgfile->Read("refresh", &AccountData);
247 txtRefresh->SetValue(AccountData);
253 cfgfile->SetPath(wxT("/"));
254 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
260 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
263 // Check if server address matches against the blacklist.
264 // Bring up warning message if it does.
266 if (CheckBlacklist(txtAddress->GetValue())){
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);
270 if (MessageBoxResult == wxNO){
276 // Update the settings for the account.
279 bool ContinueAcc = TRUE;
280 wxString AccountName;
282 // Look for the account
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);
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){
314 cfgfile->Write(wxT("ssl"), wxT("true"));
318 cfgfile->Write(wxT("ssl"), wxT("false"));
322 cfgfile->DeleteEntry(wxT("refresh"), FALSE);
323 cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
329 cfgfile->SetPath(wxT("/"));
330 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
334 // Set the dialog result to true and close the window.
341 void frmEditAccount::CloseWindow( wxCommandEvent& event )
344 // Set the dialog result to false and close the window.
346 DialogResult = false;
351 bool frmEditAccount::GetDialogResult(){
353 // Get the result of the dialog.