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 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
110 frmICPtr->LoadDataNew(CDavObj.GetSSLVerifyResults(), txtAddress->GetValue());
111 frmICPtr->ShowModal();
113 SSLResult = frmICPtr->GetResult();
115 // Clean up before processing response.
120 // Process the response from the user.
124 // Accept the Certificate.
126 CDavObj.AllowSelfSignTest(TRUE);
128 } else if (SSLResult == 2){
130 // Reject the certificate, abort the task and mark as failed.
132 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
140 // Something else happened. Stop the process and
141 // display an error message instead.
143 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
150 // Attempt to extract the CardDAV address.
152 wxString ABURL = CDavObj.GetDefaultAddressBookURL();
154 txtPrefix->SetValue(ABURL);
158 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
160 // Setup the account configuration file pointer.
166 void frmEditAccount::LoadSettings( wxString AccNameIn ){
168 // Get the data from the accounts settings file and
169 // fill in the account fields.
173 bool ContinueAcc = TRUE;
174 wxString AccountName;
175 wxString AccountData;
177 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
181 if (AccountName == AccName){
183 cfgfile->SetPath(AccountName);
184 txtAccountName->SetValue(AccountName);
186 cfgfile->Read("address", &AccountData);
187 txtAddress->SetValue(AccountData);
189 cfgfile->Read("port", &AccountData);
190 txtPort->SetValue(AccountData);
192 cfgfile->Read("username", &AccountData);
193 txtUsername->SetValue(AccountData);
195 cfgfile->Read("password", &AccountData);
196 txtPassword->SetValue(AccountData);
198 cfgfile->Read("prefix", &AccountData);
199 txtPrefix->SetValue(AccountData);
201 cfgfile->Read("ssl", &AccountData);
202 if (AccountData == wxT("true")){
204 chkSSL->SetValue(TRUE);
208 cfgfile->Read("refresh", &AccountData);
209 txtRefresh->SetValue(AccountData);
215 cfgfile->SetPath(wxT("/"));
216 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
222 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
225 // Check if server address matches against the blacklist.
226 // Bring up warning message if it does.
228 if (CheckBlacklist(txtAddress->GetValue())){
230 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);
232 if (MessageBoxResult == wxNO){
238 // Update the settings for the account.
241 bool ContinueAcc = TRUE;
242 wxString AccountName;
244 // Look for the account
248 if (AccountName == AccName){
250 // Update the settings for the account.
252 cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
254 cfgfile->SetPath(txtAccountName->GetValue());
255 //txtAccountName->SetValue(AccountName);
257 cfgfile->DeleteEntry(wxT("address"), FALSE);
258 cfgfile->Write(wxT("address"), txtAddress->GetValue());
260 cfgfile->DeleteEntry(wxT("port"), FALSE);
261 cfgfile->Write(wxT("port"), txtPort->GetValue());
263 cfgfile->DeleteEntry(wxT("username"), FALSE);
264 cfgfile->Write(wxT("username"), txtUsername->GetValue());
266 cfgfile->DeleteEntry(wxT("password"), FALSE);
267 cfgfile->Write(wxT("password"), txtPassword->GetValue());
269 cfgfile->DeleteEntry(wxT("prefix"), FALSE);
270 cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
272 cfgfile->DeleteEntry(wxT("ssl"), FALSE);
274 if (chkSSL->GetValue() == TRUE){
276 cfgfile->Write(wxT("ssl"), wxT("true"));
280 cfgfile->Write(wxT("ssl"), wxT("false"));
284 cfgfile->DeleteEntry(wxT("refresh"), FALSE);
285 cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
291 cfgfile->SetPath(wxT("/"));
292 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
296 // Set the dialog result to true and close the window.
303 void frmEditAccount::CloseWindow( wxCommandEvent& event )
306 // Set the dialog result to false and close the window.
308 DialogResult = false;
313 bool frmEditAccount::GetDialogResult(){
315 // Get the result of the dialog.