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"
20 #include "frmInvalidSSLCertificate.h"
21 #include "carddav/carddav.h"
22 #include "common/svrblist.h"
24 frmEditAccount::frmEditAccount( wxWindow* parent )
26 frmEditAccountADT( parent )
31 void frmEditAccount::DetectAddressBook( wxCommandEvent& event )
34 // Check data before connecting.
39 ValueData = txtPort->GetValue();
40 ValueData.ToLong(&PortNum, 10);
42 ValueData = txtRefresh->GetValue();
43 ValueData.ToLong(&RefreshNum, 10);
46 if (txtAddress->IsEmpty()){
48 wxMessageBox(wxT("The server address cannot be blank."), wxT("Error"), wxICON_ERROR);
53 if (txtPort->IsEmpty() || PortNum < 0 || PortNum > 65535){
55 wxMessageBox(wxT("The server port needs to be between number 1 and 65535."), wxT("Error"), wxICON_ERROR);
60 if (txtUsername->IsEmpty()){
62 wxMessageBox(wxT("The server username cannot be blank."), wxT("Error"), wxICON_ERROR);
67 if (txtPassword->IsEmpty()){
69 wxMessageBox(wxT("The server password cannot be blank."), wxT("Error"), wxICON_ERROR);
74 UseSSL = chkSSL->GetValue();
76 if (txtRefresh->IsEmpty() || RefreshNum < 300 || RefreshNum > 86400){
82 // Setup a CardDAV object.
86 CDavObj.SetupConnection(txtAddress->GetValue(), (int)PortNum, txtUsername->GetValue(), txtPassword->GetValue(), UseSSL);
88 // Verify SSL trust first before doing anything.
92 CURLcode sslcode = CDavObj.SSLVerifyTest();
94 if (sslcode == CURLE_OK){
96 // Certificate is okay. Do nothing.
98 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
100 // Certificate is more than likely a self-signed or
101 // expired certificate so display the invalid
102 // SSL certificate message.
104 // Setup the data to be sent in the wxPostEvent command.
108 #if defined(__APPLE__)
110 SSLResult = DisplayTrustPanel(&CDavObj);
114 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
116 frmICPtr->LoadDataNew(CDavObj.GetSSLVerifyResults(), txtAddress->GetValue());
117 frmICPtr->ShowModal();
119 SSLResult = frmICPtr->GetResult();
121 // Clean up before processing response.
128 // Process the response from the user.
132 // Accept the Certificate.
134 CDavObj.AllowSelfSignTest(TRUE);
136 } else if (SSLResult == 2){
138 // Reject the certificate, abort the task and mark as failed.
140 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
147 // Something else happened. Stop the process and
148 // display an error message instead.
150 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
157 // Attempt to extract the CardDAV address.
159 wxString ABURL = CDavObj.GetDefaultAddressBookURL();
161 txtPrefix->SetValue(ABURL);
165 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
167 // Setup the account configuration file pointer.
173 void frmEditAccount::LoadSettings( wxString AccNameIn ){
175 // Get the data from the accounts settings file and
176 // fill in the account fields.
180 bool ContinueAcc = TRUE;
181 wxString AccountName;
182 wxString AccountData;
184 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
188 if (AccountName == AccName){
190 cfgfile->SetPath(AccountName);
191 txtAccountName->SetValue(AccountName);
193 cfgfile->Read("address", &AccountData);
194 txtAddress->SetValue(AccountData);
196 cfgfile->Read("port", &AccountData);
197 txtPort->SetValue(AccountData);
199 cfgfile->Read("username", &AccountData);
200 txtUsername->SetValue(AccountData);
202 cfgfile->Read("password", &AccountData);
203 txtPassword->SetValue(AccountData);
205 cfgfile->Read("prefix", &AccountData);
206 txtPrefix->SetValue(AccountData);
208 cfgfile->Read("ssl", &AccountData);
209 if (AccountData == wxT("true")){
211 chkSSL->SetValue(TRUE);
215 cfgfile->Read("refresh", &AccountData);
216 txtRefresh->SetValue(AccountData);
222 cfgfile->SetPath(wxT("/"));
223 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
229 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
232 // Check if server address matches against the blacklist.
233 // Bring up warning message if it does.
235 if (CheckBlacklist(txtAddress->GetValue())){
237 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);
239 if (MessageBoxResult == wxNO){
245 // Update the settings for the account.
248 bool ContinueAcc = TRUE;
249 wxString AccountName;
251 // Look for the account
255 if (AccountName == AccName){
257 // Update the settings for the account.
259 cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
261 cfgfile->SetPath(txtAccountName->GetValue());
262 //txtAccountName->SetValue(AccountName);
264 cfgfile->DeleteEntry(wxT("address"), FALSE);
265 cfgfile->Write(wxT("address"), txtAddress->GetValue());
267 cfgfile->DeleteEntry(wxT("port"), FALSE);
268 cfgfile->Write(wxT("port"), txtPort->GetValue());
270 cfgfile->DeleteEntry(wxT("username"), FALSE);
271 cfgfile->Write(wxT("username"), txtUsername->GetValue());
273 cfgfile->DeleteEntry(wxT("password"), FALSE);
274 cfgfile->Write(wxT("password"), txtPassword->GetValue());
276 cfgfile->DeleteEntry(wxT("prefix"), FALSE);
277 cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
279 cfgfile->DeleteEntry(wxT("ssl"), FALSE);
281 if (chkSSL->GetValue() == TRUE){
283 cfgfile->Write(wxT("ssl"), wxT("true"));
287 cfgfile->Write(wxT("ssl"), wxT("false"));
291 cfgfile->DeleteEntry(wxT("refresh"), FALSE);
292 cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
298 cfgfile->SetPath(wxT("/"));
299 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
303 // Set the dialog result to true and close the window.
310 void frmEditAccount::CloseWindow( wxCommandEvent& event )
313 // Set the dialog result to false and close the window.
315 DialogResult = false;
320 bool frmEditAccount::GetDialogResult(){
322 // Get the result of the dialog.