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__)
99 // Connect again and fetch SSL certificate information.
101 TestConnection.BypassSSLVerification(true);
103 COConnectResult TestConnectionResult = TestConnection.Connect(false);
105 TestConnection.BypassSSLVerification(false);
107 SSLCertCollectionString CertData = TestConnection.BuildSSLCollection();
108 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
110 frmICPtr->LoadDataNew(CertData, txtAddress->GetValue().ToStdString());
111 frmICPtr->ShowModal();
113 int SSLResult = frmICPtr->GetResult();
115 // Clean up before processing response.
120 // Process the response from the user.
124 // Accept the Certificate.
126 UsingSSLBypass = true;
127 TestConnection.BypassSSLVerification(true);
129 COConnectResult TestConnectionResult = TestConnection.Connect(true);
131 TestConnection.BypassSSLVerification(false);
133 } else if (SSLResult == 2){
135 // Reject the certificate, abort the task.
137 wxMessageBox(_("Server certficiate rejected. Unable to detect the prefix."), _("Failed"), wxOK+wxICON_ERROR);
146 // Get the server prefix if the connection was successful.
148 if (TestConnectionResult == COCONNECT_OK){
150 if (UsingSSLBypass == true){
151 TestConnection.BypassSSLVerification(true);
154 COServerResponse PrefixRequestResult = TestConnection.GetDefaultPrefix(&ReceivedServerPrefix);
156 if (UsingSSLBypass == true){
157 TestConnection.BypassSSLVerification(true);
162 wxMessageBox(_("An error occured whilst detecting the prefix: ") + TestConnection.GetErrorMessage(), _("Failed"), wxOK+wxICON_ERROR);
166 txtPrefix->SetValue(ReceivedServerPrefix);
170 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
172 // Setup the account configuration file pointer.
178 void frmEditAccount::LoadSettings( wxString AccNameIn ){
180 // Get the data from the accounts settings file and
181 // fill in the account fields.
185 bool ContinueAcc = TRUE;
186 wxString AccountName;
187 wxString AccountData;
189 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
193 if (AccountName == AccName){
195 cfgfile->SetPath(AccountName);
196 txtAccountName->SetValue(AccountName);
198 cfgfile->Read("address", &AccountData);
199 txtAddress->SetValue(AccountData);
201 cfgfile->Read("port", &AccountData);
202 txtPort->SetValue(AccountData);
204 cfgfile->Read("username", &AccountData);
205 txtUsername->SetValue(AccountData);
207 cfgfile->Read("password", &AccountData);
208 txtPassword->SetValue(AccountData);
210 cfgfile->Read("prefix", &AccountData);
211 txtPrefix->SetValue(AccountData);
213 cfgfile->Read("ssl", &AccountData);
214 if (AccountData == wxT("true")){
216 chkSSL->SetValue(TRUE);
220 cfgfile->Read("refresh", &AccountData);
221 txtRefresh->SetValue(AccountData);
227 cfgfile->SetPath(wxT("/"));
228 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
234 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
237 // Check if server address matches against the blacklist.
238 // Bring up warning message if it does.
240 if (CheckBlacklist(txtAddress->GetValue())){
242 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);
244 if (MessageBoxResult == wxNO){
250 // Update the settings for the account.
253 bool ContinueAcc = TRUE;
254 wxString AccountName;
256 // Look for the account
260 if (AccountName == AccName){
262 // Update the settings for the account.
264 cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
266 cfgfile->SetPath(txtAccountName->GetValue());
267 //txtAccountName->SetValue(AccountName);
269 cfgfile->DeleteEntry(wxT("address"), FALSE);
270 cfgfile->Write(wxT("address"), txtAddress->GetValue());
272 cfgfile->DeleteEntry(wxT("port"), FALSE);
273 cfgfile->Write(wxT("port"), txtPort->GetValue());
275 cfgfile->DeleteEntry(wxT("username"), FALSE);
276 cfgfile->Write(wxT("username"), txtUsername->GetValue());
278 cfgfile->DeleteEntry(wxT("password"), FALSE);
279 cfgfile->Write(wxT("password"), txtPassword->GetValue());
281 cfgfile->DeleteEntry(wxT("prefix"), FALSE);
282 cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
284 cfgfile->DeleteEntry(wxT("ssl"), FALSE);
286 if (chkSSL->GetValue() == TRUE){
288 cfgfile->Write(wxT("ssl"), wxT("true"));
292 cfgfile->Write(wxT("ssl"), wxT("false"));
296 cfgfile->DeleteEntry(wxT("refresh"), FALSE);
297 cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
303 cfgfile->SetPath(wxT("/"));
304 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
308 // Set the dialog result to true and close the window.
315 void frmEditAccount::CloseWindow( wxCommandEvent& event )
318 // Set the dialog result to false and close the window.
320 DialogResult = false;
325 bool frmEditAccount::GetDialogResult(){
327 // Get the result of the dialog.