1 // preferences.cpp - Preferences subroutines.
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/>
23 #include <wx/fileconf.h>
25 #include "preferences.h"
29 void SavePreferences();
30 void LoadPreferences();
32 XABPreferences::XABPreferences(wxString PreferencesFilename){
34 // Load the settings into the XABPreferences object using the
35 // settings file received in PreferencesFilename.
37 wxString fullprefpath;
39 bool accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
40 accountsslvalid, accountusernamevalid, accountpasswordvalid,
41 accountdirprefixvalid, accountdirvalid, accountrefreshvalid = FALSE;
43 bool preaccountstage = FALSE;
44 bool isvalidaccount = FALSE;
45 bool isvalidsection = FALSE;
47 wxString accountaddress;
49 long accountport = 8080;
52 wxString accountusername;
53 wxString accountpassword;
54 wxString accountdirprefix;
61 wxString segmentvalue;
65 fullprefpath.Append(PreferencesFilename);
66 fullprefpath.Append(wxT("settings"));
68 wxFileConfig *settingfile = new wxFileConfig("", "", fullprefpath);
71 settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
73 if (ValueInc == wxT("true")){
76 wxRect WindowPosition;
78 long PosX, PosY, PosH, PosW = 0;
80 bool PosXValid, PosYValid, PosHValid, PosWValid = FALSE;
82 PosXValid = settingfile->Read(wxT("WindowPositionX"), &PosX);
83 PosYValid = settingfile->Read(wxT("WindowPositionY"), &PosY);
84 PosHValid = settingfile->Read(wxT("WindowPositionHeight"), &PosH);
85 PosWValid = settingfile->Read(wxT("WindowPositionWidth"), &PosW);
87 if (PosXValid == TRUE && PosYValid == TRUE && PosHValid == TRUE && PosWValid == TRUE){
89 WindowPosition.SetX((int)PosX);
90 WindowPosition.SetY((int)PosY);
91 WindowPosition.SetHeight((int)PosH);
92 WindowPosition.SetWidth((int)PosW);
96 WindowPosition.SetX(-1);
97 WindowPosition.SetY(-1);
98 WindowPosition.SetHeight(500);
99 WindowPosition.SetWidth(300);
103 SetMainWindowData(WindowPosition);
107 settingfile->Read(wxT("HideLocalAddressBooks"), &ValueInc);
109 if (ValueInc == wxT("true")){
116 // Load the accounts.
118 fullprefpath.Clear();
119 fullprefpath.Append(PreferencesFilename);
120 fullprefpath.Append(wxT("accounts"));
122 wxFileConfig *cfgfile = new wxFileConfig("AddressBook", "Xestia", fullprefpath);
124 wxString EntryName, EntryValue;
125 wxString AccAdr, AccUsr, AccPass, AccPrefix, AccDir;
128 int AccRef, AccPort = 0;
130 bool ContinueAcc = TRUE;
132 ContinueAcc = cfgfile->GetFirstGroup(accountname, itemindex);
136 cfgfile->SetPath(accountname);
137 cfgfile->Read(wxT("type"), &EntryValue);
139 if (EntryValue == wxT("Local")){
141 // Get the account directory.
143 cfgfile->Read(wxT("accountdir"), &AccDir);
145 if (AccDir.Len() > 4){
147 accountdirvalid = TRUE;
151 accountdirvalid = FALSE;
155 if (accountdirvalid == TRUE){
157 accounts.AddAccount(accountname, wxT("Local"), wxT(""), 0,
163 } else if (EntryValue == wxT("CardDAV")){
165 cfgfile->Read(wxT("accountdir"), &AccDir);
166 accountaddressvalid = cfgfile->Read(wxT("address"), &AccAdr);
167 accountportvalid = cfgfile->Read(wxT("port"), &AccPort);
168 accountsslvalid = cfgfile->Read(wxT("ssl"), &AccSSLInc);
169 if (AccSSLInc == wxT("true")){
172 accountusernamevalid = cfgfile->Read(wxT("username"), &AccUsr);
173 accountpasswordvalid = cfgfile->Read(wxT("password"), &AccPass);
174 accountdirprefixvalid = cfgfile->Read(wxT("prefix"), &AccPrefix);
175 accountrefreshvalid = cfgfile->Read(wxT("refresh"), &AccRef);
177 if (AccDir.Len() > 4){
179 accountdirvalid = TRUE;
183 // Make sure it is not bigger than 65535 or less than 1.
184 // If so, default to port 8008.
186 if (accountport < 1 || accountport > 65535){
188 accountportvalid = TRUE;
191 accounts.AddAccount(accountname, wxT("CardDAV"), AccAdr, AccPort,
192 AccSSL, AccUsr, AccPass,
193 AccPrefix, AccDir, AccRef);
197 // Clear up for the next account.
199 accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
200 accountsslvalid, accountusernamevalid, accountpasswordvalid,
201 accountdirprefixvalid, accountdirvalid, accountrefreshvalid, accountssl,
202 isvalidaccount, isvalidsection = FALSE;
210 accountaddress.Clear();
214 accountusername.Clear();
215 accountpassword.Clear();
216 accountdirprefix.Clear();
217 preaccountstage = TRUE;
219 cfgfile->SetPath(wxT("/"));
220 ContinueAcc = cfgfile->GetNextGroup(accountname, itemindex);
226 XABPreferences::~XABPreferences(){
230 bool XABPreferences::GetBoolData(wxString SettingName){
232 // GetBoolData from the XABPreferences object.
234 if (SettingName == wxT("SaveWindowPosition")) { return SaveWindowPos; }
235 else if (SettingName == wxT("HideLocalAddressBooks")) { return HideLocalABs; }
241 wxRect XABPreferences::GetMainWindowData(){
243 // Return the main window data as a wxRect object.
245 return MainWindowData;
249 void XABPreferences::SetMainWindowData(wxRect WindowData){
251 // Set the main window data from a wxRect object.
253 MainWindowData = WindowData;
259 XABPrefAccounts::XABPrefAccounts(){
261 // Setup the default values for XABPrefAccounts.
267 int XABPrefAccounts::AddAccount(wxString NewAccName,
269 wxString NewAccAddress,
274 wxString NewAccDirPrefix,
279 // Add an account to the list of accounts in the
280 // XABPrefAccounts object.
282 AccountName.Add(NewAccName, 1);
283 AccountType.Add(NewAccType, 1);
284 AccountAddress.Add(NewAccAddress, 1);
285 AccountPort.Add(NewAccPort, 1);
286 AccountSSL.Add(NewAccSSL, 1);
287 AccountUsername.Add(NewAccUser, 1);
288 AccountPassword.Add(NewAccPass, 1);
289 DirectoryPrefix.Add(NewAccDirPrefix, 1);
290 AccountDirectory.Add(NewAccDir, 1);
291 AccountRefresh.Add(NewAccRefresh, 1);
299 int XABPrefAccounts::GetCount(){
301 // Get the count of accounts in the XABPrefAccounts object.
303 return AccountsCount;
307 wxString XABPrefAccounts::GetAccountName(int AccountNum){
309 // Get the account name.
311 if (AccountNum > AccountsCount){
315 return AccountName[AccountNum];
319 wxString XABPrefAccounts::GetAccountType(int AccountNum){
321 // Get the account type.
323 if (AccountNum > AccountsCount){
327 return AccountType[AccountNum];
331 wxString XABPrefAccounts::GetAccountAddress(int AccountNum){
333 // Get the account server address.
335 if (AccountNum > AccountsCount){
339 return AccountAddress[AccountNum];
343 int XABPrefAccounts::GetAccountPort(int AccountNum){
345 // Get the account server port.
347 if (AccountNum > AccountsCount){
351 return AccountPort[AccountNum];
355 bool XABPrefAccounts::GetAccountSSL(int AccountNum){
357 // Get the account server SSL support.
359 if (AccountNum > AccountsCount){
363 return AccountSSL[AccountNum];
367 wxString XABPrefAccounts::GetAccountUsername(int AccountNum){
369 // Get the account username.
371 if (AccountNum > AccountsCount){
375 return AccountUsername[AccountNum];
379 wxString XABPrefAccounts::GetAccountPassword(int AccountNum){
381 // Get the account password.
383 if (AccountNum > AccountsCount){
387 return AccountPassword[AccountNum];
391 wxString XABPrefAccounts::GetAccountDirectory(int AccountNum){
393 // Get the account directory.
395 if (AccountNum > AccountsCount){
399 return AccountDirectory[AccountNum];
403 wxString XABPrefAccounts::GetAccountDirPrefix(int AccountNum){
405 // Get the account server directory prefix.
407 if (AccountNum > AccountsCount){
411 return DirectoryPrefix[AccountNum];
415 long XABPrefAccounts::GetAccountRefresh(int AccountNum){
417 // Get the account refresh time.
419 if (AccountNum > AccountsCount){
423 return AccountRefresh[AccountNum];