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 wxString fullprefpath;
36 bool accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
37 accountsslvalid, accountusernamevalid, accountpasswordvalid,
38 accountdirprefixvalid, accountdirvalid, accountrefreshvalid = FALSE;
40 bool preaccountstage = FALSE;
41 bool isvalidaccount = FALSE;
42 bool isvalidsection = FALSE;
44 wxString accountaddress;
46 long accountport = 8080;
49 wxString accountusername;
50 wxString accountpassword;
51 wxString accountdirprefix;
58 wxString segmentvalue;
62 fullprefpath.Append(PreferencesFilename);
63 fullprefpath.Append(wxT("settings"));
65 wxFileConfig *settingfile = new wxFileConfig("", "", fullprefpath);
68 settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
70 if (ValueInc == wxT("true")){
73 wxRect WindowPosition;
75 long PosX, PosY, PosH, PosW = 0;
77 bool PosXValid, PosYValid, PosHValid, PosWValid = FALSE;
79 PosXValid = settingfile->Read(wxT("WindowPositionX"), &PosX);
80 PosYValid = settingfile->Read(wxT("WindowPositionY"), &PosY);
81 PosHValid = settingfile->Read(wxT("WindowPositionHeight"), &PosH);
82 PosWValid = settingfile->Read(wxT("WindowPositionWidth"), &PosW);
84 if (PosXValid == TRUE && PosYValid == TRUE && PosHValid == TRUE && PosWValid == TRUE){
86 WindowPosition.SetX((int)PosX);
87 WindowPosition.SetY((int)PosY);
88 WindowPosition.SetHeight((int)PosH);
89 WindowPosition.SetWidth((int)PosW);
93 WindowPosition.SetX(-1);
94 WindowPosition.SetY(-1);
95 WindowPosition.SetHeight(500);
96 WindowPosition.SetWidth(300);
100 SetMainWindowData(WindowPosition);
104 settingfile->Read(wxT("HideLocalAddressBooks"), &ValueInc);
106 if (ValueInc == wxT("true")){
113 // Load the accounts.
115 fullprefpath.Clear();
116 fullprefpath.Append(PreferencesFilename);
117 fullprefpath.Append(wxT("accounts"));
119 wxFileConfig *cfgfile = new wxFileConfig("AddressBook", "Xestia", fullprefpath);
121 wxString EntryName, EntryValue;
122 wxString AccAdr, AccUsr, AccPass, AccPrefix, AccDir;
125 int AccRef, AccPort = 0;
127 bool ContinueAcc = TRUE;
129 ContinueAcc = cfgfile->GetFirstGroup(accountname, itemindex);
133 cfgfile->SetPath(accountname);
134 cfgfile->Read(wxT("type"), &EntryValue);
136 if (EntryValue == wxT("Local")){
138 // Get the account directory.
140 cfgfile->Read(wxT("accountdir"), &AccDir);
142 if (AccDir.Len() > 4){
144 accountdirvalid = TRUE;
148 accountdirvalid = FALSE;
152 if (accountdirvalid == TRUE){
154 accounts.AddAccount(accountname, wxT("Local"), wxT(""), 0,
160 } else if (EntryValue == wxT("CardDAV")){
162 cfgfile->Read(wxT("accountdir"), &AccDir);
163 accountaddressvalid = cfgfile->Read(wxT("address"), &AccAdr);
164 accountportvalid = cfgfile->Read(wxT("port"), &AccPort);
165 accountsslvalid = cfgfile->Read(wxT("ssl"), &AccSSLInc);
166 if (AccSSLInc == wxT("true")){
169 accountusernamevalid = cfgfile->Read(wxT("username"), &AccUsr);
170 accountpasswordvalid = cfgfile->Read(wxT("password"), &AccPass);
171 accountdirprefixvalid = cfgfile->Read(wxT("prefix"), &AccPrefix);
172 accountrefreshvalid = cfgfile->Read(wxT("refresh"), &AccRef);
174 if (AccDir.Len() > 4){
176 accountdirvalid = TRUE;
180 // Make sure it is not bigger than 65535 or less than 1.
181 // If so, default to port 8008.
183 if (accountport < 1 || accountport > 65535){
185 accountportvalid = TRUE;
188 accounts.AddAccount(accountname, wxT("CardDAV"), AccAdr, AccPort,
189 AccSSL, AccUsr, AccPass,
190 AccPrefix, AccDir, AccRef);
194 // Clear up for the next account.
196 accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
197 accountsslvalid, accountusernamevalid, accountpasswordvalid,
198 accountdirprefixvalid, accountdirvalid, accountrefreshvalid, accountssl,
199 isvalidaccount, isvalidsection = FALSE;
207 accountaddress.Clear();
211 accountusername.Clear();
212 accountpassword.Clear();
213 accountdirprefix.Clear();
214 preaccountstage = TRUE;
216 cfgfile->SetPath(wxT("/"));
217 ContinueAcc = cfgfile->GetNextGroup(accountname, itemindex);
223 XABPreferences::~XABPreferences(){
227 bool XABPreferences::GetBoolData(wxString SettingName){
229 if (SettingName == wxT("SaveWindowPosition")) { return SaveWindowPos; }
230 else if (SettingName == wxT("HideLocalAddressBooks")) { return HideLocalABs; }
236 wxRect XABPreferences::GetMainWindowData(){
238 return MainWindowData;
242 void XABPreferences::SetMainWindowData(wxRect WindowData){
244 MainWindowData = WindowData;
250 XABPrefAccounts::XABPrefAccounts(){
254 int XABPrefAccounts::AddAccount(wxString NewAccName,
256 wxString NewAccAddress,
261 wxString NewAccDirPrefix,
266 AccountName.Add(NewAccName, 1);
267 AccountType.Add(NewAccType, 1);
268 AccountAddress.Add(NewAccAddress, 1);
269 AccountPort.Add(NewAccPort, 1);
270 AccountSSL.Add(NewAccSSL, 1);
271 AccountUsername.Add(NewAccUser, 1);
272 AccountPassword.Add(NewAccPass, 1);
273 DirectoryPrefix.Add(NewAccDirPrefix, 1);
274 AccountDirectory.Add(NewAccDir, 1);
275 AccountRefresh.Add(NewAccRefresh, 1);
283 int XABPrefAccounts::GetCount(){
285 return AccountsCount;
289 wxString XABPrefAccounts::GetAccountName(int AccountNum){
291 if (AccountNum > AccountsCount){
295 return AccountName[AccountNum];
299 wxString XABPrefAccounts::GetAccountType(int AccountNum){
301 if (AccountNum > AccountsCount){
305 return AccountType[AccountNum];
309 wxString XABPrefAccounts::GetAccountAddress(int AccountNum){
311 if (AccountNum > AccountsCount){
315 return AccountAddress[AccountNum];
319 int XABPrefAccounts::GetAccountPort(int AccountNum){
321 if (AccountNum > AccountsCount){
325 return AccountPort[AccountNum];
329 bool XABPrefAccounts::GetAccountSSL(int AccountNum){
331 if (AccountNum > AccountsCount){
335 return AccountSSL[AccountNum];
339 wxString XABPrefAccounts::GetAccountUsername(int AccountNum){
341 if (AccountNum > AccountsCount){
345 return AccountUsername[AccountNum];
349 wxString XABPrefAccounts::GetAccountPassword(int AccountNum){
351 if (AccountNum > AccountsCount){
355 return AccountPassword[AccountNum];
359 wxString XABPrefAccounts::GetAccountDirectory(int AccountNum){
361 if (AccountNum > AccountsCount){
365 return AccountDirectory[AccountNum];
369 wxString XABPrefAccounts::GetAccountDirPrefix(int AccountNum){
371 if (AccountNum > AccountsCount){
375 return DirectoryPrefix[AccountNum];
379 long XABPrefAccounts::GetAccountRefresh(int AccountNum){
381 if (AccountNum > AccountsCount){
385 return AccountRefresh[AccountNum];