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"
23 frmEditAccount::frmEditAccount( wxWindow* parent )
25 frmEditAccountADT( parent )
30 void frmEditAccount::DetectAddressBook( wxCommandEvent& event )
33 // Check data before connecting.
38 ValueData = txtPort->GetValue();
39 ValueData.ToLong(&PortNum, 10);
41 ValueData = txtRefresh->GetValue();
42 ValueData.ToLong(&RefreshNum, 10);
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 // Setup a CardDAV object.
85 CDavObj.SetupConnection(txtAddress->GetValue(), (int)PortNum, txtUsername->GetValue(), txtPassword->GetValue(), UseSSL);
87 // Verify SSL trust first before doing anything.
91 CURLcode sslcode = CDavObj.SSLVerifyTest();
93 if (sslcode == CURLE_OK){
95 // Certificate is okay. Do nothing.
97 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
99 // Certificate is more than likely a self-signed or
100 // expired certificate so display the invalid
101 // SSL certificate message.
103 // Setup the data to be sent in the wxPostEvent command.
107 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
109 frmICPtr->LoadDataNew(CDavObj.GetSSLVerifyResults(), txtAddress->GetValue());
110 frmICPtr->ShowModal();
112 SSLResult = frmICPtr->GetResult();
114 // Clean up before processing response.
119 // Process the response from the user.
123 // Accept the Certificate.
125 CDavObj.AllowSelfSignTest(TRUE);
127 } else if (SSLResult == 2){
129 // Reject the certificate, abort the task and mark as failed.
131 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
139 // Something else happened. Stop the process and
140 // display an error message instead.
142 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
149 // Attempt to extract the CardDAV address.
151 wxString ABURL = CDavObj.GetDefaultAddressBookURL();
153 txtPrefix->SetValue(ABURL);
157 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
159 // Setup the account configuration file pointer.
165 void frmEditAccount::LoadSettings( wxString AccNameIn ){
167 // Get the data from the accounts settings file and
168 // fill in the account fields.
172 bool ContinueAcc = TRUE;
173 wxString AccountName;
174 wxString AccountData;
176 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
180 if (AccountName == AccName){
182 cfgfile->SetPath(AccountName);
183 txtAccountName->SetValue(AccountName);
185 cfgfile->Read("address", &AccountData);
186 txtAddress->SetValue(AccountData);
188 cfgfile->Read("port", &AccountData);
189 txtPort->SetValue(AccountData);
191 cfgfile->Read("username", &AccountData);
192 txtUsername->SetValue(AccountData);
194 cfgfile->Read("password", &AccountData);
195 txtPassword->SetValue(AccountData);
197 cfgfile->Read("prefix", &AccountData);
198 txtPrefix->SetValue(AccountData);
200 cfgfile->Read("ssl", &AccountData);
201 if (AccountData == wxT("true")){
203 chkSSL->SetValue(TRUE);
207 cfgfile->Read("refresh", &AccountData);
208 txtRefresh->SetValue(AccountData);
214 cfgfile->SetPath(wxT("/"));
215 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
221 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
224 // Update the settings for the account.
227 bool ContinueAcc = TRUE;
228 wxString AccountName;
230 // Look for the account
234 if (AccountName == AccName){
236 // Update the settings for the account.
238 cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
240 cfgfile->SetPath(txtAccountName->GetValue());
241 //txtAccountName->SetValue(AccountName);
243 cfgfile->DeleteEntry(wxT("address"), FALSE);
244 cfgfile->Write(wxT("address"), txtAddress->GetValue());
246 cfgfile->DeleteEntry(wxT("port"), FALSE);
247 cfgfile->Write(wxT("port"), txtPort->GetValue());
249 cfgfile->DeleteEntry(wxT("username"), FALSE);
250 cfgfile->Write(wxT("username"), txtUsername->GetValue());
252 cfgfile->DeleteEntry(wxT("password"), FALSE);
253 cfgfile->Write(wxT("password"), txtPassword->GetValue());
255 cfgfile->DeleteEntry(wxT("prefix"), FALSE);
256 cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
258 cfgfile->DeleteEntry(wxT("ssl"), FALSE);
260 if (chkSSL->GetValue() == TRUE){
262 cfgfile->Write(wxT("ssl"), wxT("true"));
266 cfgfile->Write(wxT("ssl"), wxT("false"));
270 cfgfile->DeleteEntry(wxT("refresh"), FALSE);
271 cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
277 cfgfile->SetPath(wxT("/"));
278 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
282 // Set the dialog result to true and close the window.
289 void frmEditAccount::CloseWindow( wxCommandEvent& event )
292 // Set the dialog result to false and close the window.
294 DialogResult = false;
299 bool frmEditAccount::GetDialogResult(){
301 // Get the result of the dialog.