// preferences.cpp - Preferences subroutines. // // (c) 2012-2017 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 #include #include #include #include #include #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]; }