+// preferences.cpp - Preferences subroutines.
+//
+// (c) 2012-2016 Xestia Software Development.
+//
+// This file is part of Xestia Calendar. Originally from Xestia Address Book.
+//
+// Xestia Calendar is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by the
+// Free Software Foundation, version 3 of the license.
+//
+// Xestia Calendar is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
+
+#include <iostream>
+#include <iomanip>
+#include <ios>
+#include <string>
+#include <wx/fileconf.h>
+
+#include "preferences.h"
+
+using namespace std;
+
+void SavePreferences();
+void LoadPreferences();
+
+XCALPreferences::XCALPreferences(wxString PreferencesFilename){
+
+ // Load the settings into the XCALPreferences object using the
+ // settings file received in PreferencesFilename.
+
+ wxString fullprefpath;
+
+ bool accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
+ accountsslvalid, accountusernamevalid, accountpasswordvalid,
+ accountdirprefixvalid, accountdirvalid, accountrefreshvalid = FALSE;
+
+ bool preaccountstage = FALSE;
+ bool isvalidaccount = FALSE;
+ bool isvalidsection = FALSE;
+ wxString accountname;
+ wxString accountaddress;
+ wxString accounttype;
+ long accountport = 8080;
+ long accountrefresh;
+ bool accountssl;
+ wxString accountusername;
+ wxString accountpassword;
+ wxString accountdirprefix;
+ wxString accountdir;
+
+ wxString firstchar;
+ string s;
+
+ wxString segmentname;
+ wxString segmentvalue;
+
+ // Load the settings.
+
+ fullprefpath.Append(PreferencesFilename);
+ fullprefpath.Append(wxT("settings"));
+
+ wxFileConfig *settingfile = new wxFileConfig("", "", fullprefpath);
+
+ wxString ValueInc;
+ settingfile->Read(wxT("SaveWindowPosition"), &ValueInc);
+
+ if (ValueInc == wxT("true")){
+
+ SaveWindowPos = TRUE;
+ wxRect WindowPosition;
+
+ long PosX, PosY, PosH, PosW = 0;
+
+ bool PosXValid, PosYValid, PosHValid, PosWValid = FALSE;
+
+ PosXValid = settingfile->Read(wxT("WindowPositionX"), &PosX);
+ PosYValid = settingfile->Read(wxT("WindowPositionY"), &PosY);
+ PosHValid = settingfile->Read(wxT("WindowPositionHeight"), &PosH);
+ PosWValid = settingfile->Read(wxT("WindowPositionWidth"), &PosW);
+
+ if (PosXValid == TRUE && PosYValid == TRUE && PosHValid == TRUE && PosWValid == TRUE){
+
+ WindowPosition.SetX((int)PosX);
+ WindowPosition.SetY((int)PosY);
+ WindowPosition.SetHeight((int)PosH);
+ WindowPosition.SetWidth((int)PosW);
+
+ } else {
+
+ WindowPosition.SetX(-1);
+ WindowPosition.SetY(-1);
+ WindowPosition.SetHeight(500);
+ WindowPosition.SetWidth(300);
+
+ }
+
+ SetMainWindowData(WindowPosition);
+
+ }
+
+ settingfile->Read(wxT("HideLocalAddressBooks"), &ValueInc);
+
+ if (ValueInc == wxT("true")){
+ HideLocalABs = TRUE;
+ }
+
+ delete settingfile;
+ settingfile = NULL;
+
+ // Load the accounts.
+
+ fullprefpath.Clear();
+ fullprefpath.Append(PreferencesFilename);
+ fullprefpath.Append(wxT("accounts"));
+
+ wxFileConfig *cfgfile = new wxFileConfig("AddressBook", "Xestia", fullprefpath);
+
+ wxString EntryName, EntryValue;
+ wxString AccAdr, AccUsr, AccPass, AccPrefix, AccDir;
+ wxString AccSSLInc;
+ bool AccSSL = FALSE;
+ int AccRef, AccPort = 0;
+ long itemindex = 0;
+ bool ContinueAcc = TRUE;
+
+ ContinueAcc = cfgfile->GetFirstGroup(accountname, itemindex);
+
+ while (ContinueAcc){
+
+ cfgfile->SetPath(accountname);
+ cfgfile->Read(wxT("type"), &EntryValue);
+
+ if (EntryValue == wxT("Local")){
+
+ // Get the account directory.
+
+ cfgfile->Read(wxT("accountdir"), &AccDir);
+
+ if (AccDir.Len() > 4){
+
+ accountdirvalid = TRUE;
+
+ } else {
+
+ accountdirvalid = FALSE;
+
+ }
+
+ if (accountdirvalid == TRUE){
+
+ accounts.AddAccount(accountname, wxT("Local"), wxT(""), 0,
+ 0, wxT(""), wxT(""),
+ wxT(""), AccDir, 0);
+
+ }
+
+ } else if (EntryValue == wxT("CalDAV")){
+
+ cfgfile->Read(wxT("accountdir"), &AccDir);
+ accountaddressvalid = cfgfile->Read(wxT("address"), &AccAdr);
+ accountportvalid = cfgfile->Read(wxT("port"), &AccPort);
+ accountsslvalid = cfgfile->Read(wxT("ssl"), &AccSSLInc);
+ if (AccSSLInc == wxT("true")){
+ AccSSL = TRUE;
+ }
+ else {
+ AccSSL = FALSE;
+ }
+ accountusernamevalid = cfgfile->Read(wxT("username"), &AccUsr);
+ accountpasswordvalid = cfgfile->Read(wxT("password"), &AccPass);
+ accountdirprefixvalid = cfgfile->Read(wxT("prefix"), &AccPrefix);
+ accountrefreshvalid = cfgfile->Read(wxT("refresh"), &AccRef);
+
+ if (AccDir.Len() > 4){
+
+ accountdirvalid = TRUE;
+
+ }
+
+ // Make sure it is not bigger than 65535 or less than 1.
+ // If so, default to port 8008.
+
+ if (accountport < 1 || accountport > 65535){
+ accountport = 8008;
+ accountportvalid = TRUE;
+ }
+
+ accounts.AddAccount(accountname, wxT("CalDAV"), AccAdr, AccPort,
+ AccSSL, AccUsr, AccPass,
+ AccPrefix, AccDir, AccRef);
+
+ } else {
+
+ cfgfile->Read(wxT("accountdir"), &AccDir);
+ accountaddressvalid = cfgfile->Read(wxT("address"), &AccAdr);
+ accountportvalid = cfgfile->Read(wxT("port"), &AccPort);
+ accountsslvalid = cfgfile->Read(wxT("ssl"), &AccSSLInc);
+ if (AccSSLInc == wxT("true")){
+ AccSSL = TRUE;
+ }
+ else {
+ AccSSL = FALSE;
+ }
+ accountusernamevalid = cfgfile->Read(wxT("username"), &AccUsr);
+ accountpasswordvalid = cfgfile->Read(wxT("password"), &AccPass);
+ accountdirprefixvalid = cfgfile->Read(wxT("prefix"), &AccPrefix);
+ accountrefreshvalid = cfgfile->Read(wxT("refresh"), &AccRef);
+
+ if (AccDir.Len() > 4){
+
+ accountdirvalid = TRUE;
+
+ }
+
+ // Make sure it is not bigger than 65535 or less than 1.
+ // If so, default to port 8008.
+
+ if (accountport < 1 || accountport > 65535){
+ accountport = 8008;
+ accountportvalid = TRUE;
+ }
+
+ accounts.AddAccount(accountname, EntryValue, AccAdr, AccPort,
+ AccSSL, AccUsr, AccPass,
+ AccPrefix, AccDir, AccRef);
+
+ }
+
+ // Clear up for the next account.
+
+ accountnamevalid, accountaddressvalid, accounttypevalid, accountportvalid,
+ accountsslvalid, accountusernamevalid, accountpasswordvalid,
+ accountdirprefixvalid, accountdirvalid, accountrefreshvalid, accountssl,
+ isvalidaccount, isvalidsection = FALSE;
+
+ AccAdr.Clear();
+ AccDir.Clear();
+ AccUsr.Clear();
+ AccPass.Clear();
+ AccPrefix.Clear();
+ accountname.Clear();
+ accountaddress.Clear();
+ accounttype.Clear();
+ accountport = 0;
+ accountrefresh = 0;
+ accountusername.Clear();
+ accountpassword.Clear();
+ accountdirprefix.Clear();
+ preaccountstage = TRUE;
+
+ cfgfile->SetPath(wxT("/"));
+ ContinueAcc = cfgfile->GetNextGroup(accountname, itemindex);
+
+ }
+
+}
+
+XCALPreferences::~XCALPreferences(){
+
+}
+
+bool XCALPreferences::GetBoolData(wxString SettingName){
+
+ // GetBoolData from the XCALPreferences object.
+
+ if (SettingName == wxT("SaveWindowPosition")) { return SaveWindowPos; }
+ else if (SettingName == wxT("HideLocalAddressBooks")) { return HideLocalABs; }
+
+ return FALSE;
+
+}
+
+wxRect XCALPreferences::GetMainWindowData(){
+
+ // Return the main window data as a wxRect object.
+
+ return MainWindowData;
+
+}
+
+void XCALPreferences::SetMainWindowData(wxRect WindowData){
+
+ // Set the main window data from a wxRect object.
+
+ MainWindowData = WindowData;
+
+}
+
+// XCALPrefAccounts
+
+XCALPrefAccounts::XCALPrefAccounts(){
+
+ // Setup the default values for XCALPrefAccounts.
+
+ AccountsCount = 0;
+
+}
+
+int XCALPrefAccounts::AddAccount(wxString NewAccName,
+ wxString NewAccType,
+ wxString NewAccAddress,
+ int NewAccPort,
+ int NewAccSSL,
+ wxString NewAccUser,
+ wxString NewAccPass,
+ wxString NewAccDirPrefix,
+ wxString NewAccDir,
+ long NewAccRefresh
+){
+
+ // Add an account to the list of accounts in the
+ // XCALPrefAccounts object.
+
+ AccountName.Add(NewAccName, 1);
+ AccountType.Add(NewAccType, 1);
+ AccountAddress.Add(NewAccAddress, 1);
+ AccountPort.Add(NewAccPort, 1);
+ AccountSSL.Add(NewAccSSL, 1);
+ AccountUsername.Add(NewAccUser, 1);
+ AccountPassword.Add(NewAccPass, 1);
+ DirectoryPrefix.Add(NewAccDirPrefix, 1);
+ AccountDirectory.Add(NewAccDir, 1);
+ AccountRefresh.Add(NewAccRefresh, 1);
+
+ AccountsCount++;
+
+ return 0;
+
+}
+
+int XCALPrefAccounts::GetCount(){
+
+ // Get the count of accounts in the XCALPrefAccounts object.
+
+ return AccountsCount;
+
+}
+
+wxString XCALPrefAccounts::GetAccountName(int AccountNum){
+
+ // Get the account name.
+
+ if (AccountNum > AccountsCount){
+ return wxT("");
+ }
+
+ return AccountName[AccountNum];
+
+}
+
+wxString XCALPrefAccounts::GetAccountType(int AccountNum){
+
+ // Get the account type.
+
+ if (AccountNum > AccountsCount){
+ return wxT("");
+ }
+
+ return AccountType[AccountNum];
+
+}
+
+wxString XCALPrefAccounts::GetAccountAddress(int AccountNum){
+
+ // Get the account server address.
+
+ if (AccountNum > AccountsCount){
+ return wxT("");
+ }
+
+ return AccountAddress[AccountNum];
+
+}
+
+int XCALPrefAccounts::GetAccountPort(int AccountNum){
+
+ // Get the account server port.
+
+ if (AccountNum > AccountsCount){
+ return 0;
+ }
+
+ return AccountPort[AccountNum];
+
+}
+
+bool XCALPrefAccounts::GetAccountSSL(int AccountNum){
+
+ // Get the account server SSL support.
+
+ if (AccountNum > AccountsCount){
+ return wxT("");
+ }
+
+ return AccountSSL[AccountNum];
+
+}
+
+wxString XCALPrefAccounts::GetAccountUsername(int AccountNum){
+
+ // Get the account username.
+
+ if (AccountNum > AccountsCount){
+ return wxT("");
+ }
+
+ return AccountUsername[AccountNum];
+
+}
+
+wxString XCALPrefAccounts::GetAccountPassword(int AccountNum){
+
+ // Get the account password.
+
+ if (AccountNum > AccountsCount){
+ return wxT("");
+ }
+
+ return AccountPassword[AccountNum];
+
+}
+
+wxString XCALPrefAccounts::GetAccountDirectory(int AccountNum){
+
+ // Get the account directory.
+
+ if (AccountNum > AccountsCount){
+ return wxT("");
+ }
+
+ return AccountDirectory[AccountNum];
+
+}
+
+wxString XCALPrefAccounts::GetAccountDirPrefix(int AccountNum){
+
+ // Get the account server directory prefix.
+
+ if (AccountNum > AccountsCount){
+ return wxT("");
+ }
+
+ return DirectoryPrefix[AccountNum];
+
+}
+
+long XCALPrefAccounts::GetAccountRefresh(int AccountNum){
+
+ // Get the account refresh time.
+
+ if (AccountNum > AccountsCount){
+ return 0;
+ }
+
+ return AccountRefresh[AccountNum];
+
+}
\ No newline at end of file