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__)
97 TestConnection.BypassSSLVerification(true);
99 COConnectResult TestConnectionResult = TestConnection.Connect(false);
101 TestConnection.BypassSSLVerification(false);
103 int SSLResult = DisplayTrustPanel(&TestConnection);
105 if (SSLResult != NSOKButton){
107 wxMessageBox(_("An error occured whilst connnecting: ") + TestConnection.GetErrorMessage(), _("Failed"), wxOK+wxICON_ERROR);
112 #elif defined(__WIN32__)
114 TestConnection.BypassSSLVerification(true);
116 COConnectResult TestConnectionResult = TestConnection.Connect(false);
118 TestConnection.BypassSSLVerification(false);
120 BOOL ModifiedCertificateData = false;
121 CRYPTUI_VIEWCERTIFICATE_STRUCTW CertificateData = BuildCertificateData(&TestConnection, (HWND)this->GetHandle());
123 if (!CryptUIDlgViewCertificate(&CertificateData, &ModifiedCertificateData)) {
124 wxMessageBox(_("An error occured while trying to open the certificate dialog."), _("Error opening Certificate Information dialog"));
128 if (ModifiedCertificateData == false) {
129 wxMessageBox(_("An invalid certificate was received from the server."), _("Invalid certificate"));
132 TestConnection.BypassSSLVerification(true);
133 COConnectResult TestConnectionResult = TestConnection.Connect(true);
134 TestConnection.BypassSSLVerification(false);
139 // Connect again and fetch SSL certificate information.
141 TestConnection.BypassSSLVerification(true);
143 COConnectResult TestConnectionResult = TestConnection.Connect(false);
145 TestConnection.BypassSSLVerification(false);
147 SSLCertCollectionString CertData = TestConnection.BuildSSLCollection();
148 frmInvalidSSLCertificate *frmICPtr = new frmInvalidSSLCertificate(this);
150 frmICPtr->LoadDataNew(CertData, txtAddress->GetValue().ToStdString());
151 frmICPtr->ShowModal();
153 int SSLResult = frmICPtr->GetResult();
155 // Clean up before processing response.
160 // Process the response from the user.
164 // Accept the Certificate.
166 UsingSSLBypass = true;
167 TestConnection.BypassSSLVerification(true);
169 COConnectResult TestConnectionResult = TestConnection.Connect(true);
171 TestConnection.BypassSSLVerification(false);
173 } else if (SSLResult == 2){
175 // Reject the certificate, abort the task.
177 wxMessageBox(_("Server certficiate rejected. Unable to detect the prefix."), _("Failed"), wxOK+wxICON_ERROR);
186 // Get the server prefix if the connection was successful.
188 if (TestConnectionResult == COCONNECT_OK){
190 COConnectResult TestConnectionResult = TestConnection.Connect(true);
192 if (UsingSSLBypass == true){
193 TestConnection.BypassSSLVerification(true);
196 COServerResponse PrefixRequestResult = TestConnection.GetDefaultPrefix(&ReceivedServerPrefix);
198 if (UsingSSLBypass == true){
199 TestConnection.BypassSSLVerification(true);
204 wxMessageBox(_("An error occured whilst detecting the prefix: ") + TestConnection.GetErrorMessage(), _("Failed"), wxOK+wxICON_ERROR);
208 txtPrefix->SetValue(ReceivedServerPrefix);
212 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
214 // Setup the account configuration file pointer.
220 void frmEditAccount::LoadSettings( wxString AccNameIn ){
222 // Get the data from the accounts settings file and
223 // fill in the account fields.
227 bool ContinueAcc = TRUE;
228 wxString AccountName;
229 wxString AccountData;
231 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
235 if (AccountName == AccName){
237 cfgfile->SetPath(AccountName);
238 txtAccountName->SetValue(AccountName);
240 cfgfile->Read("address", &AccountData);
241 txtAddress->SetValue(AccountData);
243 cfgfile->Read("port", &AccountData);
244 txtPort->SetValue(AccountData);
246 cfgfile->Read("username", &AccountData);
247 txtUsername->SetValue(AccountData);
249 cfgfile->Read("password", &AccountData);
250 txtPassword->SetValue(AccountData);
252 cfgfile->Read("prefix", &AccountData);
253 txtPrefix->SetValue(AccountData);
255 cfgfile->Read("ssl", &AccountData);
256 if (AccountData == wxT("true")){
258 chkSSL->SetValue(TRUE);
262 cfgfile->Read("refresh", &AccountData);
263 txtRefresh->SetValue(AccountData);
269 cfgfile->SetPath(wxT("/"));
270 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
276 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
279 // Check if server address matches against the blacklist.
280 // Bring up warning message if it does.
282 if (CheckBlacklist(txtAddress->GetValue())){
284 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);
286 if (MessageBoxResult == wxNO){
292 // Update the settings for the account.
295 bool ContinueAcc = TRUE;
296 wxString AccountName;
298 // Look for the account
302 if (AccountName == AccName){
304 // Update the settings for the account.
306 cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
308 cfgfile->SetPath(txtAccountName->GetValue());
309 //txtAccountName->SetValue(AccountName);
311 cfgfile->DeleteEntry(wxT("address"), FALSE);
312 cfgfile->Write(wxT("address"), txtAddress->GetValue());
314 cfgfile->DeleteEntry(wxT("port"), FALSE);
315 cfgfile->Write(wxT("port"), txtPort->GetValue());
317 cfgfile->DeleteEntry(wxT("username"), FALSE);
318 cfgfile->Write(wxT("username"), txtUsername->GetValue());
320 cfgfile->DeleteEntry(wxT("password"), FALSE);
321 cfgfile->Write(wxT("password"), txtPassword->GetValue());
323 cfgfile->DeleteEntry(wxT("prefix"), FALSE);
324 cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
326 cfgfile->DeleteEntry(wxT("ssl"), FALSE);
328 if (chkSSL->GetValue() == TRUE){
330 cfgfile->Write(wxT("ssl"), wxT("true"));
334 cfgfile->Write(wxT("ssl"), wxT("false"));
338 cfgfile->DeleteEntry(wxT("refresh"), FALSE);
339 cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
345 cfgfile->SetPath(wxT("/"));
346 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
350 // Set the dialog result to true and close the window.
357 void frmEditAccount::CloseWindow( wxCommandEvent& event )
360 // Set the dialog result to false and close the window.
362 DialogResult = false;
367 bool frmEditAccount::GetDialogResult(){
369 // Get the result of the dialog.