Changeset 93
- Timestamp:
- 04/20/06 05:48:51 (2 years ago)
- Location:
- trunk
- Files:
-
- 4 modified
-
src/Config.cxx (modified) (9 diffs)
-
src/Config.h (modified) (2 diffs)
-
tests/ConfigTest.cxx (modified) (1 diff)
-
tests/ConfigTest.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/Config.cxx
r92 r93 16 16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 17 18 #include <config.h> 19 #include <glib/gstdio.h> 18 20 #include "epdfview.h" 19 21 20 22 using namespace ePDFView; 21 23 22 // /Constants24 // Constants 23 25 static gint DEFAULT_WINDOW_HEIGHT = 650; 24 26 static gint DEFAULT_WINDOW_WIDTH = 600; … … 26 28 static gint DEFAULT_WINDOW_Y = 0; 27 29 28 // /Static member attributes.30 // Static member attributes. 29 31 Config *Config::m_Config = NULL; 30 32 gboolean Config::m_LoadFile = TRUE; 31 33 34 // Forward declarations. 35 gchar *getConfigFileName (void); 36 void makeDirWithParents (const gchar *); 37 32 38 /// 33 39 /// @brief Constructs a new Config object. 34 40 /// 41 /// It creates and load from a file the configuration, unless the 42 /// Config::loadFile() was called with FALSE. 43 /// 35 44 Config::Config () 36 45 { 46 m_Values = g_key_file_new (); 47 if ( m_LoadFile ) 48 { 49 gchar *configFile = getConfigFileName (); 50 GError *error = NULL; 51 if ( !g_key_file_load_from_file (m_Values, configFile, 52 G_KEY_FILE_NONE, &error ) ) 53 { 54 g_warning ("Couldn't load config file '%s': %s\n", configFile, 55 error->message); 56 g_error_free (error); 57 } 58 g_free (configFile); 59 } 37 60 } 38 61 … … 42 65 Config::~Config () 43 66 { 67 g_key_file_free (m_Values); 44 68 } 45 69 … … 80 104 81 105 /// 106 /// @brief Gets an integer configuration option. 107 /// 108 /// @param group Configuration group where the key belongs. 109 /// @param key The key name to retrieve its value. 110 /// @param defaultValue The default value to return if @a key doesn't exists. 111 /// 112 /// @return The value of the @a key in @a group or @a defaultValue if @a key 113 /// doesn't exists. 114 /// 115 gint 116 Config::getInteger (const gchar *group, const gchar *key, gint defaultValue) 117 { 118 gint value = defaultValue; 119 120 if ( g_key_file_has_key (m_Values, group, key, NULL) ) 121 { 122 GError *error = NULL; 123 gint savedValue = g_key_file_get_integer (m_Values, group, key, &error); 124 if ( NULL == error ) 125 { 126 value = savedValue; 127 } 128 else 129 { 130 g_warning ("Error reading key '%s' from group '%s': %s\n", 131 key, group, error->message); 132 g_error_free (error); 133 } 134 } 135 136 return value; 137 } 138 139 /// 82 140 /// @brief Gets the main window's height. 83 141 /// … … 87 145 Config::getWindowHeight () 88 146 { 89 gint windowHeight = DEFAULT_WINDOW_HEIGHT; 90 91 return windowHeight; 147 return getInteger ("main window", "height", DEFAULT_WINDOW_HEIGHT); 92 148 } 93 149 … … 100 156 Config::getWindowWidth () 101 157 { 102 gint windowWidth = DEFAULT_WINDOW_WIDTH; 103 104 return windowWidth; 158 return getInteger ("main window", "width", DEFAULT_WINDOW_WIDTH); 105 159 } 106 160 … … 112 166 gint 113 167 Config::getWindowX () 114 { 115 gint windowX = DEFAULT_WINDOW_X; 116 117 return windowX; 168 { 169 return getInteger ("main window", "x", DEFAULT_WINDOW_X); 118 170 } 119 171 … … 126 178 Config::getWindowY () 127 179 { 128 gint windowY = DEFAULT_WINDOW_Y; 129 130 return windowY; 131 } 132 180 return getInteger ("main window", "y", DEFAULT_WINDOW_Y); 181 } 182 183 /// 184 /// @brief Sets if load the configuration value. 185 /// 186 /// This is mainly for testing only purposes. If it's called before 187 /// the first call to getConfig(), then the configuration file is not 188 /// loaded. 189 /// 190 /// @param load Set to TRUE to load the file, to FALSE to prevent loading it. 191 /// 133 192 void 134 193 Config::loadFile (gboolean load) … … 136 195 m_LoadFile = load; 137 196 } 197 198 /// 199 /// @brief Saves the current window's size. 200 /// 201 /// @param width The window's width. 202 /// @param height The window's height. 203 /// 204 void 205 Config::setWindowSize (gint width, gint height) 206 { 207 g_key_file_set_integer (m_Values, "main window", "width", width); 208 g_key_file_set_integer (m_Values, "main window", "height", height); 209 } 210 211 /// 212 /// @brief Saves the current window's position. 213 /// 214 /// @param x The X position of the window. 215 /// @param y The Y position of the window. 216 /// 217 void 218 Config::setWindowPos (gint x, gint y) 219 { 220 g_key_file_set_integer (m_Values, "main window", "x", x); 221 g_key_file_set_integer (m_Values, "main window", "y", y); 222 } 223 224 /// 225 /// @brief Gets the configuration file name. 226 /// 227 /// This function creates the directory where the configuration file should 228 /// go, if it doesn't exist yet. 229 /// 230 /// @return The full path to the configuration file. 231 /// 232 gchar * 233 getConfigFileName () 234 { 235 gchar *configDir = 236 g_build_filename (g_get_user_config_dir (), PACKAGE, NULL); 237 makeDirWithParents (configDir); 238 gchar *configFile = g_build_filename (configDir, "main.conf", NULL); 239 g_free (configDir); 240 241 return configFile; 242 } 243 244 /// 245 /// @brief Creates the directory and all its parent directories. 246 /// 247 /// If the directory already exists it does nothing. 248 /// 249 /// @param path The directory to create. 250 /// 251 void 252 makeDirWithParents (const gchar *path) 253 { 254 // Get the list of directories to create. 255 GList *dirs = NULL; 256 gchar *dirName = g_strdup (path); 257 while ( !g_file_test (dirName, G_FILE_TEST_EXISTS) ); 258 { 259 dirs = g_list_prepend (dirs, dirName); 260 dirName = g_path_get_dirname (dirName); 261 } 262 263 // Now create all of them. 264 GList *dir = g_list_first (dirs); 265 while ( NULL != dir ); 266 { 267 if ( -1 == g_mkdir ((gchar *)dir->data, 0700) ) 268 { 269 g_warning ("Couldn't make directory '%s'\n", (gchar *)dir->data); 270 } 271 g_free (dir->data); 272 dir = g_list_next (dir); 273 } 274 g_list_free (dirs); 275 } -
trunk/src/Config.h
r92 r93 43 43 gint getWindowX (void); 44 44 gint getWindowY (void); 45 void setWindowSize (gint width, gint height); 46 void setWindowPos (gint x, gint y); 45 47 46 48 protected: 49 GKeyFile *m_Values; 50 47 51 Config (void); 48 52 Config (Config &config) { } … … 50 54 static Config *m_Config; 51 55 static gboolean m_LoadFile; 56 57 gint getInteger (const gchar *group, const char *key, 58 gint defaultValue); 52 59 }; 53 60 } -
trunk/tests/ConfigTest.cxx
r92 r93 58 58 CPPUNIT_ASSERT_EQUAL (650, config.getWindowHeight ()); 59 59 } 60 61 /// 62 /// @brief Check setting and retrieving values related to the main window. 63 /// 64 void 65 ConfigTest::windowValues () 66 { 67 Config::loadFile (FALSE); 68 Config &config = Config::getConfig (); 69 70 config.setWindowPos (30, 40); 71 config.setWindowSize (100, 90); 72 73 CPPUNIT_ASSERT_EQUAL (30, config.getWindowX ()); 74 CPPUNIT_ASSERT_EQUAL (40, config.getWindowY ()); 75 CPPUNIT_ASSERT_EQUAL (100, config.getWindowWidth ()); 76 CPPUNIT_ASSERT_EQUAL (90, config.getWindowHeight ()); 77 } -
trunk/tests/ConfigTest.h
r92 r93 27 27 CPPUNIT_TEST_SUITE (ConfigTest); 28 28 CPPUNIT_TEST (defaultValues); 29 CPPUNIT_TEST (windowValues); 29 30 CPPUNIT_TEST_SUITE_END (); 30 31 … … 34 35 35 36 void defaultValues (void); 37 void windowValues (void); 36 38 }; 37 39 }
