File indexing completed on 2024-04-28 16:08:34
0001 /*************************************************************************** 0002 * Copyright (C) 2005-2017 by Linuxstopmotion contributors; * 0003 * see the AUTHORS file for details. * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #ifndef PREFERENCESTOOL_H 0021 #define PREFERENCESTOOL_H 0022 0023 #include <libxml/tree.h> 0024 0025 /** 0026 * Represents one string from the preferences file. 0027 */ 0028 class Preference { 0029 const char* val; 0030 bool owns; 0031 public: 0032 /** 0033 * Retrieves the value associated with {@a key}. {@ref get} will return 0034 * this value, or {@c 0} if no such value exists. 0035 */ 0036 Preference(const char* key); 0037 /** 0038 * Retrieves the value associated with {@a key}. {@ref get} will return 0039 * this value, or {@a defaultValue} if no such value exists. 0040 * @note No copy of the string pointed to by {@a defaultValue} will be 0041 * taken; it must remain valid until any caller of {@ref get} has finished 0042 * with it. 0043 */ 0044 Preference(const char* key, const char* defaultValue); 0045 ~Preference(); 0046 /** 0047 * Retrieves the value associated with the key passed in the constructor. 0048 * If there was no such value, the pointer passed as the default value in 0049 * the constructor is returned. Otherwise, null is returned. 0050 * @return The associated value (which becomes invalid after this object 0051 * goes out of scope), the default value (which is not a copy), or null. 0052 * Ownership is not returned. 0053 */ 0054 const char* get() const; 0055 /** 0056 * Tests the retrieved (or default) value against the argument. 0057 * @param str The string to compare against. 0058 * @return {@c true} if both {@a str} and the value is null, or if both 0059 * compare equal. {@c false} otherwise. 0060 */ 0061 bool equals(const char* str); 0062 }; 0063 0064 /** 0065 * A xml based tool for adding, changing and removing of 0066 * various preferences. Everything is saved to a xml organized file 0067 * and can be read by the tool for later usage. 0068 * 0069 * @author Bjoern Erik Nilsen & Fredrik Berg Kjoelstad 0070 */ 0071 class PreferencesTool { 0072 public: 0073 /** 0074 * Retrieves the instance of the PreferencesTool class. 0075 * @return the PreferencesTool singleton instance. 0076 */ 0077 static PreferencesTool* get(); 0078 0079 /** 0080 * Loads preferences from the file. 0081 * @param filePath the path to the file where the preferences are 0082 * stored. 0083 * @return true if the file is loaded correctly, otherwise false. 0084 */ 0085 bool load(const char* filePath); 0086 0087 /** 0088 * Sets the preferences all to default values. 0089 * @param version The version number to write into the preferences. 0090 * @param filePath The path to save the preferences to when flushed. 0091 */ 0092 void setDefaultPreferences(const char* version); 0093 0094 /** 0095 * Checks if the version matches the string given. 0096 * @param version The version to check. 0097 * @return true if the version matches the version parameter, false if it does not. 0098 */ 0099 bool isVersion(const char* version); 0100 0101 /** 0102 * Sets the version of the preference file. 0103 * @param version the version number of the preference file 0104 */ 0105 void setVersion(const char *version); 0106 0107 /** 0108 * Adds a string preference. 0109 * @param key the key for retrieving the preference. 0110 * @param attribute the attribute for the preference. 0111 * flushPreferences() should be called at once after the all preferences 0112 * are set to store them to disk. 0113 */ 0114 void setPreference(const char* key, const char* attribute); 0115 0116 /** 0117 * Adds an int preference. 0118 * @param key the key for retrieving the preference. 0119 * @param attribute the attribute for the preference. 0120 * flushPreferences() should be called at once after the all preferences 0121 * are set to store them to disk. 0122 */ 0123 void setPreference(const char* key, const int attribute); 0124 0125 /** 0126 * Retrieves a string preference. 0127 * @param key the key of the preference to retrieve. 0128 * @return the attribute for the given key or null if the key wasn't found. 0129 */ 0130 const char* getPreference(const char* key); 0131 0132 /** 0133 * Retrieves an int preference. 0134 * @param key the key of the preference to retrieve. 0135 * @param defaultValue a default value for preferences which aren't set 0136 * by the user yet. 0137 * @return the attribute for the given key or "defaultValue" if the key 0138 * wasn't found. 0139 */ 0140 int getPreference(const char* key, const int defaultValue); 0141 0142 /** 0143 * Removes the preference with the key "key". (Which, in practice, means 0144 * setting it to default). 0145 * @param key the key of the preference to remove. 0146 */ 0147 void removePreference(const char* key); 0148 0149 /** 0150 * Sets the path that the preferences will be saved to. 0151 * @param path The path to save to. 0152 * @param wantSave true if the preferences should be saved to this file at 0153 * the earliest opportunity. Should be true if the path set is different to 0154 * the path loaded from. 0155 */ 0156 void setSavePath(const char* path, bool wantSave); 0157 0158 /** 0159 * Flushes the preferences to the file specified with setPreferencesFile(..). 0160 * @throw UiException if there is an error. 0161 */ 0162 void flush(); 0163 0164 0165 protected: 0166 /** 0167 * Protected constructor to deny external instantiation of the singleton. 0168 */ 0169 PreferencesTool(); 0170 0171 /** 0172 * Cleans up after the preferencestool. 0173 */ 0174 ~PreferencesTool(); 0175 0176 private: 0177 /** The singleton instance of this class.*/ 0178 static PreferencesTool *preferencesTool; 0179 0180 xmlDocPtr doc; 0181 xmlDtdPtr dtd; 0182 xmlNodePtr rootNode; 0183 xmlNodePtr preferences; 0184 xmlNodePtr versionNode; 0185 bool dirty; 0186 0187 char *preferencesFile; 0188 0189 /** 0190 * Retrieves the node with key "key". 0191 * @param key the key of the node to retrieve. 0192 * @return the node with the given key. 0193 */ 0194 xmlNodePtr findNode(const char* key); 0195 0196 /** 0197 * Checks if the preferences tool has been initialized and exits with an error 0198 * if it hasn't. 0199 */ 0200 void checkInitialized(); 0201 0202 /** 0203 * Cleans the xml tree. 0204 */ 0205 void cleanTree(); 0206 }; 0207 0208 #endif