// preferences.cpp - Preferences subroutines. // // (c) 2012-2015 Xestia Software Development. // // This file is part of Xestia Address Book. // // Xestia Address Book 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 Address Book 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 Address Book. If not, see #include #include #include #include #include #include "preferences.h" using namespace std; void SavePreferences(); void LoadPreferences(); XABPreferences::XABPreferences(wxString PreferencesFilename){ // Load the settings into the XABPreferences 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; } settingfile->Read(wxT("UseBackgroundContactColour"), &ValueInc); if (ValueInc == wxT("true")){ UseBackgroundContactColour = true; wxString BackgroundContactColourRead; settingfile->Read(wxT("BackgroundContactColour"), &BackgroundContactColourRead); if (BackgroundContactColourRead.Mid(0, 1) == "#"){ BackgroundContactColour.Set(BackgroundContactColourRead); } } 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("CardDAV")){ 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("CardDAV"), 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); } } XABPreferences::~XABPreferences(){ } bool XABPreferences::GetBoolData(wxString SettingName){ // GetBoolData from the XABPreferences object. if (SettingName == wxT("SaveWindowPosition")) { return SaveWindowPos; } else if (SettingName == wxT("HideLocalAddressBooks")) { return HideLocalABs; } else if (SettingName == wxT("UseBackgroundContactColour")) { return UseBackgroundContactColour; } return false; } wxRect XABPreferences::GetMainWindowData(){ // Return the main window data as a wxRect object. return MainWindowData; } void XABPreferences::SetMainWindowData(wxRect WindowData){ // Set the main window data from a wxRect object. MainWindowData = WindowData; } wxColour XABPreferences::GetBackgroundContactColourData(){ return BackgroundContactColour; } void XABPreferences::SetBackgroundContactColourData(wxColour ColourData){ BackgroundContactColour = ColourData; } // XABPrefAccounts XABPrefAccounts::XABPrefAccounts(){ // Setup the default values for XABPrefAccounts. AccountsCount = 0; } int XABPrefAccounts::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 // XABPrefAccounts 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 XABPrefAccounts::GetCount(){ // Get the count of accounts in the XABPrefAccounts object. return AccountsCount; } wxString XABPrefAccounts::GetAccountName(int AccountNum){ // Get the account name. if (AccountNum > AccountsCount){ return wxT(""); } return AccountName[AccountNum]; } wxString XABPrefAccounts::GetAccountType(int AccountNum){ // Get the account type. if (AccountNum > AccountsCount){ return wxT(""); } return AccountType[AccountNum]; } wxString XABPrefAccounts::GetAccountAddress(int AccountNum){ // Get the account server address. if (AccountNum > AccountsCount){ return wxT(""); } return AccountAddress[AccountNum]; } int XABPrefAccounts::GetAccountPort(int AccountNum){ // Get the account server port. if (AccountNum > AccountsCount){ return 0; } return AccountPort[AccountNum]; } bool XABPrefAccounts::GetAccountSSL(int AccountNum){ // Get the account server SSL support. if (AccountNum > AccountsCount){ return wxT(""); } return AccountSSL[AccountNum]; } wxString XABPrefAccounts::GetAccountUsername(int AccountNum){ // Get the account username. if (AccountNum > AccountsCount){ return wxT(""); } return AccountUsername[AccountNum]; } wxString XABPrefAccounts::GetAccountPassword(int AccountNum){ // Get the account password. if (AccountNum > AccountsCount){ return wxT(""); } return AccountPassword[AccountNum]; } wxString XABPrefAccounts::GetAccountDirectory(int AccountNum){ // Get the account directory. if (AccountNum > AccountsCount){ return wxT(""); } return AccountDirectory[AccountNum]; } wxString XABPrefAccounts::GetAccountDirPrefix(int AccountNum){ // Get the account server directory prefix. if (AccountNum > AccountsCount){ return wxT(""); } return DirectoryPrefix[AccountNum]; } long XABPrefAccounts::GetAccountRefresh(int AccountNum){ // Get the account refresh time. if (AccountNum > AccountsCount){ return 0; } return AccountRefresh[AccountNum]; }