1 // preferences.cpp - Preferences subroutines.
3 // (c) 2012-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar. Originally from Xestia Address Book.
7 // Xestia Calendar 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 Calendar 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 Calendar. If not, see <http://www.gnu.org/licenses/>
23 #include <wx/fileconf.h>
25 #include "preferences.h"
29 void SavePreferences();
30 void LoadPreferences();
32 XCALPreferences::XCALPreferences(wxString preferencesFilename){
34 // Load the settings into the XCALPreferences 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("CalDAV")){
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")){
175 accountUsernameValid = cfgFile->Read(wxT("username"), &accUsr);
176 accountPasswordValid = cfgFile->Read(wxT("password"), &accPass);
177 accountDirPrefixValid = cfgFile->Read(wxT("prefix"), &accPrefix);
178 accountRefreshValid = cfgFile->Read(wxT("refresh"), &accRef);
180 if (accDir.Len() > 4){
182 accountDirValid = TRUE;
186 // Make sure it is not bigger than 65535 or less than 1.
187 // If so, default to port 8008.
189 if (accountPort < 1 || accountPort > 65535){
191 accountPortValid = TRUE;
194 accounts.AddAccount(accountName, wxT("CalDAV"), accAdr, accPort,
195 accSSL, accUsr, accPass,
196 accPrefix, accDir, accRef);
200 cfgFile->Read(wxT("accountdir"), &accDir);
201 accountAddressValid = cfgFile->Read(wxT("address"), &accAdr);
202 accountPortValid = cfgFile->Read(wxT("port"), &accPort);
203 accountSSLValid = cfgFile->Read(wxT("ssl"), &accSSLInc);
204 if (accSSLInc == wxT("true")){
210 accountUsernameValid = cfgFile->Read(wxT("username"), &accUsr);
211 accountPasswordValid = cfgFile->Read(wxT("password"), &accPass);
212 accountDirPrefixValid = cfgFile->Read(wxT("prefix"), &accPrefix);
213 accountRefreshValid = cfgFile->Read(wxT("refresh"), &accRef);
215 if (accDir.Len() > 4){
217 accountDirValid = TRUE;
221 // Make sure it is not bigger than 65535 or less than 1.
222 // If so, default to port 8008.
224 if (accountPort < 1 || accountPort > 65535){
226 accountPortValid = TRUE;
229 accounts.AddAccount(accountName, entryValue, accAdr, accPort,
230 accSSL, accUsr, accPass,
231 accPrefix, accDir, accRef);
235 // Clear up for the next account.
237 accountNameValid, accountAddressValid, accountTypeValid, accountPortValid,
238 accountSSLValid, accountUsernameValid, accountPasswordValid,
239 accountDirPrefixValid, accountDirValid, accountRefreshValid, accountSSL,
240 isValidAccount, isValidSection = FALSE;
248 accountAddress.Clear();
252 accountUsername.Clear();
253 accountPassword.Clear();
254 accountDirPrefix.Clear();
255 preAccountStage = TRUE;
257 cfgFile->SetPath(wxT("/"));
258 continueAcc = cfgFile->GetNextGroup(accountName, itemIndex);
264 XCALPreferences::~XCALPreferences(){
268 bool XCALPreferences::GetBoolData(wxString settingName){
270 // GetBoolData from the XCALPreferences object.
272 if (settingName == wxT("SaveWindowPosition")) { return saveWindowPos; }
273 else if (settingName == wxT("HideLocalAddressBooks")) { return hideLocalABs; }
279 wxRect XCALPreferences::GetMainWindowData(){
281 // Return the main window data as a wxRect object.
283 return mainWindowData;
287 void XCALPreferences::SetMainWindowData(wxRect windowData){
289 // Set the main window data from a wxRect object.
291 mainWindowData = windowData;
297 XCALPrefAccounts::XCALPrefAccounts(){
299 // Setup the default values for XCALPrefAccounts.
305 int XCALPrefAccounts::AddAccount(wxString newAccName,
307 wxString newAccAddress,
312 wxString newAccDirPrefix,
317 // Add an account to the list of accounts in the
318 // XCALPrefAccounts object.
320 accountName.Add(newAccName, 1);
321 accountType.Add(newAccType, 1);
322 accountAddress.Add(newAccAddress, 1);
323 accountPort.Add(newAccPort, 1);
324 accountSSL.Add(newAccSSL, 1);
325 accountUsername.Add(newAccUser, 1);
326 accountPassword.Add(newAccPass, 1);
327 directoryPrefix.Add(newAccDirPrefix, 1);
328 accountDirectory.Add(newAccDir, 1);
329 accountRefresh.Add(newAccRefresh, 1);
337 int XCALPrefAccounts::GetCount(){
339 // Get the count of accounts in the XCALPrefAccounts object.
341 return accountsCount;
345 wxString XCALPrefAccounts::GetAccountName(int accountNum){
347 // Get the account name.
349 if (accountNum > accountsCount){
353 return accountName[accountNum];
357 wxString XCALPrefAccounts::GetAccountType(int accountNum){
359 // Get the account type.
361 if (accountNum > accountsCount){
365 return accountType[accountNum];
369 wxString XCALPrefAccounts::GetAccountAddress(int accountNum){
371 // Get the account server address.
373 if (accountNum > accountsCount){
377 return accountAddress[accountNum];
381 int XCALPrefAccounts::GetAccountPort(int accountNum){
383 // Get the account server port.
385 if (accountNum > accountsCount){
389 return accountPort[accountNum];
393 bool XCALPrefAccounts::GetAccountSSL(int accountNum){
395 // Get the account server SSL support.
397 if (accountNum > accountsCount){
401 return accountSSL[accountNum];
405 wxString XCALPrefAccounts::GetAccountUsername(int accountNum){
407 // Get the account username.
409 if (accountNum > accountsCount){
413 return accountUsername[accountNum];
417 wxString XCALPrefAccounts::GetAccountPassword(int accountNum){
419 // Get the account password.
421 if (accountNum > accountsCount){
425 return accountPassword[accountNum];
429 wxString XCALPrefAccounts::GetAccountDirectory(int accountNum){
431 // Get the account directory.
433 if (accountNum > accountsCount){
437 return accountDirectory[accountNum];
441 wxString XCALPrefAccounts::GetAccountDirPrefix(int accountNum){
443 // Get the account server directory prefix.
445 if (accountNum > accountsCount){
449 return directoryPrefix[accountNum];
453 long XCALPrefAccounts::GetAccountRefresh(int accountNum){
455 // Get the account refresh time.
457 if (accountNum > accountsCount){
461 return accountRefresh[accountNum];