| | 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 | /// |
| | 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 | } |