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 } else if (SSLResult == 2){
173 // Reject the certificate, abort the task.
175 wxMessageBox(_("Server certficiate rejected. Unable to detect the prefix."), _("Failed"), wxOK+wxICON_ERROR);
184 // Get the server prefix if the connection was successful.
186 if (TestConnectionResult == COCONNECT_OK){
188 COConnectResult TestConnectionResult = TestConnection.Connect(true);
190 if (UsingSSLBypass == true){
191 TestConnection.BypassSSLVerification(true);
194 COServerResponse PrefixRequestResult = TestConnection.GetDefaultPrefix(&ReceivedServerPrefix);
196 if (UsingSSLBypass == true){
197 TestConnection.BypassSSLVerification(true);
202 wxMessageBox(_("An error occured whilst detecting the prefix: ") + TestConnection.GetErrorMessage(), _("Failed"), wxOK+wxICON_ERROR);
206 txtPrefix->SetValue(ReceivedServerPrefix);
210 void frmEditAccount::LoadPointers( wxFileConfig* cfgin ){
212 // Setup the account configuration file pointer.
218 void frmEditAccount::LoadSettings( wxString AccNameIn ){
220 // Get the data from the accounts settings file and
221 // fill in the account fields.
225 bool ContinueAcc = TRUE;
226 wxString AccountName;
227 wxString AccountData;
229 ContinueAcc = cfgfile->GetFirstGroup(AccountName, itemindex);
233 if (AccountName == AccName){
235 cfgfile->SetPath(AccountName);
236 txtAccountName->SetValue(AccountName);
238 cfgfile->Read("address", &AccountData);
239 txtAddress->SetValue(AccountData);
241 cfgfile->Read("port", &AccountData);
242 txtPort->SetValue(AccountData);
244 cfgfile->Read("username", &AccountData);
245 txtUsername->SetValue(AccountData);
247 cfgfile->Read("password", &AccountData);
248 txtPassword->SetValue(AccountData);
250 cfgfile->Read("prefix", &AccountData);
251 txtPrefix->SetValue(AccountData);
253 cfgfile->Read("ssl", &AccountData);
254 if (AccountData == wxT("true")){
256 chkSSL->SetValue(TRUE);
260 cfgfile->Read("refresh", &AccountData);
261 txtRefresh->SetValue(AccountData);
267 cfgfile->SetPath(wxT("/"));
268 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
274 void frmEditAccount::UpdateSettings( wxCommandEvent& event )
277 // Check if server address matches against the blacklist.
278 // Bring up warning message if it does.
280 if (CheckBlacklist(txtAddress->GetValue())){
282 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);
284 if (MessageBoxResult == wxNO){
290 // Update the settings for the account.
293 bool ContinueAcc = TRUE;
294 wxString AccountName;
296 // Look for the account
300 if (AccountName == AccName){
302 // Update the settings for the account.
304 cfgfile->RenameGroup(AccountName, txtAccountName->GetValue());
306 cfgfile->SetPath(txtAccountName->GetValue());
307 //txtAccountName->SetValue(AccountName);
309 cfgfile->DeleteEntry(wxT("address"), FALSE);
310 cfgfile->Write(wxT("address"), txtAddress->GetValue());
312 cfgfile->DeleteEntry(wxT("port"), FALSE);
313 cfgfile->Write(wxT("port"), txtPort->GetValue());
315 cfgfile->DeleteEntry(wxT("username"), FALSE);
316 cfgfile->Write(wxT("username"), txtUsername->GetValue());
318 cfgfile->DeleteEntry(wxT("password"), FALSE);
319 cfgfile->Write(wxT("password"), txtPassword->GetValue());
321 cfgfile->DeleteEntry(wxT("prefix"), FALSE);
322 cfgfile->Write(wxT("prefix"), txtPrefix->GetValue());
324 cfgfile->DeleteEntry(wxT("ssl"), FALSE);
326 if (chkSSL->GetValue() == TRUE){
328 cfgfile->Write(wxT("ssl"), wxT("true"));
332 cfgfile->Write(wxT("ssl"), wxT("false"));
336 cfgfile->DeleteEntry(wxT("refresh"), FALSE);
337 cfgfile->Write(wxT("refresh"), txtRefresh->GetValue());
343 cfgfile->SetPath(wxT("/"));
344 ContinueAcc = cfgfile->GetNextGroup(AccountName, itemindex);
348 // Set the dialog result to true and close the window.
355 void frmEditAccount::CloseWindow( wxCommandEvent& event )
358 // Set the dialog result to false and close the window.
360 DialogResult = false;
365 bool frmEditAccount::GetDialogResult(){
367 // Get the result of the dialog.