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.
36 ValueData = txtPort->GetValue();
37 ValueData.ToLong(&PortNum, 10);
39 ValueData = txtRefresh->GetValue();
40 ValueData.ToLong(&RefreshNum, 10);
43 if (txtAddress->IsEmpty()){
45 wxMessageBox(wxT("The server address cannot be blank."), wxT("Error"), wxICON_ERROR);
50 if (txtPort->IsEmpty() || PortNum < 0 || PortNum > 65535){
52 wxMessageBox(wxT("The server port needs to be between number 1 and 65535."), wxT("Error"), wxICON_ERROR);
57 if (txtUsername->IsEmpty()){
59 wxMessageBox(wxT("The server username cannot be blank."), wxT("Error"), wxICON_ERROR);
64 if (txtPassword->IsEmpty()){
66 wxMessageBox(wxT("The server password cannot be blank."), wxT("Error"), wxICON_ERROR);
71 UseSSL = chkSSL->GetValue();
73 if (txtRefresh->IsEmpty() || RefreshNum < 300 || RefreshNum > 86400){
79 // Setup a CardDAV object.
83 CDavObj.SetupConnection(txtAddress->GetValue(), (int)PortNum, txtUsername->GetValue(), txtPassword->GetValue(), UseSSL);
85 // Verify SSL trust first before doing anything.
89 CURLcode sslcode = CDavObj.SSLVerifyTest();
91 if (sslcode == CURLE_OK){
93 // Certificate is okay. Do nothing.
95 } else if (sslcode == CURLE_SSL_CACERT || sslcode == CURLE_SSL_CONNECT_ERROR){
97 // Certificate is more than likely a self-signed or
98 // expired certificate so display the invalid
99 // SSL certificate message.
101 // Setup the data to be sent in the wxPostEvent command.
105 #if defined(__APPLE__)
107 SSLResult = DisplayTrustPanel(&CDavObj);
109 #elif defined(__WIN32__)
111 BOOL ModifiedCertificateData;
112 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&CDavObj, (HWND)this->GetHandle());
114 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)){
115 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
119 if (ModifiedCertificateData == FALSE){
120 wxMessageBox(_("Unable to get server prefix without a valid SSL certificate."), _("Error getting server prefix"));
126 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
128 frmICPtr->LoadDataNew(CDavObj.GetSSLVerifyResults(), txtAddress->GetValue());
129 frmICPtr->ShowModal();
131 SSLResult = frmICPtr->GetResult();
133 // Clean up before processing response.
140 // Process the response from the user.
144 // Accept the Certificate.
146 CDavObj.AllowSelfSignTest(TRUE);
148 } else if (SSLResult == 2){
150 // Reject the certificate, abort the task and mark as failed.
152 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
159 // Something else happened. Stop the process and
160 // display an error message instead.
162 wxMessageBox(_("An error occured whilst connnecting: ") + CDavObj.GetErrorMessage() + wxString::Format(wxT(" (%i)\n%s"), sslcode, CDavObj.GetErrorBuffer().mb_str()), _("Failed"), wxOK+wxICON_ERROR);
169 // Attempt to extract the CardDAV address.
171 wxString ABURL = CDavObj.GetDefaultAddressBookURL();
173 txtPrefix->SetValue(ABURL);
177 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
179 // Setup the account configuration file pointer.
185 void frmEditAccount::LoadSettings( wxString AccNameIn ){
187 // Get the data from the accounts settings file and
188 // fill in the account fields.
192 bool ContinueAcc = TRUE;
193 wxString AccountName;
194 wxString AccountData;
196 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
200 if (AccountName == AccName){
202 cfgfile->SetPath(AccountName);
203 txtAccountName->SetValue(AccountName);
205 cfgfile->Read("address", &AccountData);
206 txtAddress->SetValue(AccountData);
208 cfgfile->Read("port", &AccountData);
209 txtPort->SetValue(AccountData);
211 cfgfile->Read("username", &AccountData);
212 txtUsername->SetValue(AccountData);
214 cfgfile->Read("password", &AccountData);
215 txtPassword->SetValue(AccountData);
217 cfgfile->Read("prefix", &AccountData);
218 txtPrefix->SetValue(AccountData);
220 cfgfile->Read("ssl", &AccountData);
221 if (AccountData == wxT("true")){
223 chkSSL->SetValue(TRUE);
227 cfgfile->Read("refresh", &AccountData);
228 txtRefresh->SetValue(AccountData);
234 cfgfile->SetPath(wxT("/"));
235 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
241 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
244 // Check if server address matches against the blacklist.
245 // Bring up warning message if it does.
247 if (CheckBlacklist(txtAddress->GetValue())){
249 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);
251 if (MessageBoxResult == wxNO){
257 // Update the settings for the account.
260 bool ContinueAcc = TRUE;
261 wxString AccountName;
263 // Look for the account
267 if (AccountName == AccName){
269 // Update the settings for the account.
271 cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
273 cfgfile->SetPath(txtAccountName->GetValue());
274 //txtAccountName->SetValue(AccountName);
276 cfgfile->DeleteEntry(wxT("address"), FALSE);
277 cfgfile->Write(wxT("address"), txtAddress->GetValue());
279 cfgfile->DeleteEntry(wxT("port"), FALSE);
280 cfgfile->Write(wxT("port"), txtPort->GetValue());
282 cfgfile->DeleteEntry(wxT("username"), FALSE);
283 cfgfile->Write(wxT("username"), txtUsername->GetValue());
285 cfgfile->DeleteEntry(wxT("password"), FALSE);
286 cfgfile->Write(wxT("password"), txtPassword->GetValue());
288 cfgfile->DeleteEntry(wxT("prefix"), FALSE);
289 cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
291 cfgfile->DeleteEntry(wxT("ssl"), FALSE);
293 if (chkSSL->GetValue() == TRUE){
295 cfgfile->Write(wxT("ssl"), wxT("true"));
299 cfgfile->Write(wxT("ssl"), wxT("false"));
303 cfgfile->DeleteEntry(wxT("refresh"), FALSE);
304 cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
310 cfgfile->SetPath(wxT("/"));
311 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
315 // Set the dialog result to true and close the window.
322 void frmEditAccount::CloseWindow( wxCommandEvent& event )
325 // Set the dialog result to false and close the window.
327 DialogResult = false;
332 bool frmEditAccount::GetDialogResult(){
334 // Get the result of the dialog.