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){
35 wxString fullprefpath;
37 bool accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
38 accountsslvalid, accountusernamevalid, accountpasswordvalid,
39 accountdirprefixvalid, accountdirvalid, accountrefreshvalid = FALSE;
41 bool preaccountstage = FALSE;
42 bool isvalidaccount = FALSE;
43 bool isvalidsection = FALSE;
45 wxString accountaddress;
47 long accountport = 8080;
50 wxString accountusername;
51 wxString accountpassword;
52 wxString accountdirprefix;
59 wxString segmentvalue;
63 fullprefpath.Append(PreferencesFilename);
64 fullprefpath.Append(wxT("settings"));
66 wxFileConfig *settingfile = new wxFileConfig("", "", fullprefpath);
69 settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
71 if (ValueInc == wxT("true")){
74 wxRect WindowPosition;
76 long PosX, PosY, PosH, PosW = 0;
78 bool PosXValid, PosYValid, PosHValid, PosWValid = FALSE;
80 PosXValid = settingfile->Read(wxT("WindowPositionX"), &PosX);
81 PosYValid = settingfile->Read(wxT("WindowPositionY"), &PosY);
82 PosHValid = settingfile->Read(wxT("WindowPositionHeight"), &PosH);
83 PosWValid = settingfile->Read(wxT("WindowPositionWidth"), &PosW);
85 if (PosXValid == TRUE && PosYValid == TRUE && PosHValid == TRUE && PosWValid == TRUE){
87 WindowPosition.SetX((int)PosX);
88 WindowPosition.SetY((int)PosY);
89 WindowPosition.SetHeight((int)PosH);
90 WindowPosition.SetWidth((int)PosW);
94 WindowPosition.SetX(-1);
95 WindowPosition.SetY(-1);
96 WindowPosition.SetHeight(500);
97 WindowPosition.SetWidth(300);
101 SetMainWindowData(WindowPosition);
105 settingfile->Read(wxT("HideLocalAddressBooks"), &ValueInc);
107 if (ValueInc == wxT("true")){
114 // Load the accounts.
116 fullprefpath.Clear();
117 fullprefpath.Append(PreferencesFilename);
118 fullprefpath.Append(wxT("accounts"));
120 wxFileConfig *cfgfile = new wxFileConfig("AddressBook", "Xestia", fullprefpath);
122 wxString EntryName, EntryValue;
123 wxString AccAdr, AccUsr, AccPass, AccPrefix, AccDir;
126 int AccRef, AccPort = 0;
128 bool ContinueAcc = TRUE;
130 ContinueAcc = cfgfile->GetFirstGroup(accountname, itemindex);
134 cfgfile->SetPath(accountname);
135 //ContinueProc = cfgfile->GetFirstEntry(EntryName, subitemindex);
136 cfgfile->Read(wxT("type"), &EntryValue);
138 if (EntryValue == wxT("Local")){
140 // Get the account directory.
142 cfgfile->Read(wxT("accountdir"), &AccDir);
144 if (AccDir.Len() > 4){
146 accountdirvalid = TRUE;
150 accountdirvalid = FALSE;
154 if (accountdirvalid == TRUE){
156 accounts.AddAccount(accountname, wxT("Local"), wxT(""), 0,
162 } else if (EntryValue == wxT("CardDAV")){
164 cfgfile->Read(wxT("accountdir"), &AccDir);
165 accountaddressvalid = cfgfile->Read(wxT("address"), &AccAdr);
166 accountportvalid = cfgfile->Read(wxT("port"), &AccPort);
167 accountsslvalid = cfgfile->Read(wxT("ssl"), &AccSSLInc);
168 if (AccSSLInc == wxT("true")){
171 accountusernamevalid = cfgfile->Read(wxT("username"), &AccUsr);
172 accountpasswordvalid = cfgfile->Read(wxT("password"), &AccPass);
173 accountdirprefixvalid = cfgfile->Read(wxT("prefix"), &AccPrefix);
174 accountrefreshvalid = cfgfile->Read(wxT("refresh"), &AccRef);
176 if (AccDir.Len() > 4){
178 accountdirvalid = TRUE;
182 // Make sure it is not bigger than 65535 or less than 1.
183 // If so, default to port 8008.
185 if (accountport < 1 || accountport > 65535){
187 accountportvalid = TRUE;
190 accounts.AddAccount(accountname, wxT("CardDAV"), AccAdr, AccPort,
191 AccSSL, AccUsr, AccPass,
192 AccPrefix, AccDir, AccRef);
196 // Clear up for the next account.
198 accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
199 accountsslvalid, accountusernamevalid, accountpasswordvalid,
200 accountdirprefixvalid, accountdirvalid, accountrefreshvalid, accountssl,
201 isvalidaccount, isvalidsection = FALSE;
209 accountaddress.Clear();
213 accountusername.Clear();
214 accountpassword.Clear();
215 accountdirprefix.Clear();
216 preaccountstage = TRUE;
218 cfgfile->SetPath(wxT("/"));
219 ContinueAcc = cfgfile->GetNextGroup(accountname, itemindex);
225 XABPreferences::~XABPreferences(){
229 bool XABPreferences::GetBoolData(wxString SettingName){
231 if (SettingName == wxT("SaveWindowPosition")) { return SaveWindowPos; }
232 else if (SettingName == wxT("HideLocalAddressBooks")) { return HideLocalABs; }
238 wxRect XABPreferences::GetMainWindowData(){
240 return MainWindowData;
244 void XABPreferences::SetMainWindowData(wxRect WindowData){
246 MainWindowData = WindowData;
252 XABPrefAccounts::XABPrefAccounts(){
256 int XABPrefAccounts::AddAccount(wxString NewAccName,
258 wxString NewAccAddress,
263 wxString NewAccDirPrefix,
268 AccountName.Add(NewAccName, 1);
269 AccountType.Add(NewAccType, 1);
270 AccountAddress.Add(NewAccAddress, 1);
271 AccountPort.Add(NewAccPort, 1);
272 AccountSSL.Add(NewAccSSL, 1);
273 AccountUsername.Add(NewAccUser, 1);
274 AccountPassword.Add(NewAccPass, 1);
275 DirectoryPrefix.Add(NewAccDirPrefix, 1);
276 AccountDirectory.Add(NewAccDir, 1);
277 AccountRefresh.Add(NewAccRefresh, 1);
285 int XABPrefAccounts::GetCount(){
287 return AccountsCount;
291 wxString XABPrefAccounts::GetAccountName(int AccountNum){
293 if (AccountNum > AccountsCount){
297 return AccountName[AccountNum];
301 wxString XABPrefAccounts::GetAccountType(int AccountNum){
303 if (AccountNum > AccountsCount){
307 return AccountType[AccountNum];
311 wxString XABPrefAccounts::GetAccountAddress(int AccountNum){
313 if (AccountNum > AccountsCount){
317 return AccountAddress[AccountNum];
321 int XABPrefAccounts::GetAccountPort(int AccountNum){
323 if (AccountNum > AccountsCount){
327 return AccountPort[AccountNum];
331 bool XABPrefAccounts::GetAccountSSL(int AccountNum){
333 if (AccountNum > AccountsCount){
337 return AccountSSL[AccountNum];
341 wxString XABPrefAccounts::GetAccountUsername(int AccountNum){
343 if (AccountNum > AccountsCount){
347 return AccountUsername[AccountNum];
351 wxString XABPrefAccounts::GetAccountPassword(int AccountNum){
353 if (AccountNum > AccountsCount){
357 return AccountPassword[AccountNum];
361 wxString XABPrefAccounts::GetAccountDirectory(int AccountNum){
363 if (AccountNum > AccountsCount){
367 return AccountDirectory[AccountNum];
371 wxString XABPrefAccounts::GetAccountDirPrefix(int AccountNum){
373 if (AccountNum > AccountsCount){
377 return DirectoryPrefix[AccountNum];
381 long XABPrefAccounts::GetAccountRefresh(int AccountNum){
383 if (AccountNum > AccountsCount){
387 return AccountRefresh[AccountNum];