Changeset 93

Show
Ignore:
Timestamp:
04/20/06 05:48:51 (2 years ago)
Author:
jordi
Message:

It's possible to set the configuration values and to load the configuration from a file. The test suite for the configuration doesn't test loading from a file.

Location:
trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/Config.cxx

    r92 r93  
    1616// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
    1717 
     18#include <config.h> 
     19#include <glib/gstdio.h> 
    1820#include "epdfview.h" 
    1921 
    2022using namespace ePDFView; 
    2123 
    22 /// Constants 
     24// Constants 
    2325static gint DEFAULT_WINDOW_HEIGHT = 650; 
    2426static gint DEFAULT_WINDOW_WIDTH = 600; 
     
    2628static gint DEFAULT_WINDOW_Y = 0; 
    2729 
    28 /// Static member attributes. 
     30// Static member attributes. 
    2931Config *Config::m_Config = NULL; 
    3032gboolean Config::m_LoadFile = TRUE; 
    3133 
     34// Forward declarations. 
     35gchar *getConfigFileName (void); 
     36void makeDirWithParents (const gchar *); 
     37 
    3238/// 
    3339/// @brief Constructs a new Config object. 
    3440/// 
     41/// It creates and load from a file the configuration, unless the 
     42/// Config::loadFile() was called with FALSE. 
     43/// 
    3544Config::Config () 
    3645{ 
     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    } 
    3760} 
    3861 
     
    4265Config::~Config () 
    4366{ 
     67    g_key_file_free (m_Values); 
    4468} 
    4569 
     
    80104 
    81105/// 
     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/// 
     115gint 
     116Config::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/// 
    82140/// @brief Gets the main window's height. 
    83141/// 
     
    87145Config::getWindowHeight () 
    88146{ 
    89     gint windowHeight = DEFAULT_WINDOW_HEIGHT; 
    90      
    91     return windowHeight; 
     147    return getInteger ("main window", "height", DEFAULT_WINDOW_HEIGHT); 
    92148} 
    93149 
     
    100156Config::getWindowWidth () 
    101157{ 
    102     gint windowWidth = DEFAULT_WINDOW_WIDTH; 
    103      
    104     return windowWidth; 
     158    return getInteger ("main window", "width", DEFAULT_WINDOW_WIDTH); 
    105159} 
    106160 
     
    112166gint 
    113167Config::getWindowX () 
    114 { 
    115     gint windowX = DEFAULT_WINDOW_X; 
    116      
    117     return windowX; 
     168{     
     169    return getInteger ("main window", "x", DEFAULT_WINDOW_X); 
    118170} 
    119171 
     
    126178Config::getWindowY () 
    127179{ 
    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/// 
    133192void 
    134193Config::loadFile (gboolean load) 
     
    136195    m_LoadFile = load; 
    137196} 
     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/// 
     204void 
     205Config::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/// 
     217void 
     218Config::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/// 
     232gchar * 
     233getConfigFileName () 
     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/// 
     251void 
     252makeDirWithParents (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  
    4343            gint getWindowX (void); 
    4444            gint getWindowY (void); 
     45            void setWindowSize (gint width, gint height); 
     46            void setWindowPos (gint x, gint y); 
    4547             
    4648        protected: 
     49            GKeyFile *m_Values; 
     50             
    4751            Config (void); 
    4852            Config (Config &config) { } 
     
    5054            static Config *m_Config; 
    5155            static gboolean m_LoadFile; 
     56 
     57            gint getInteger (const gchar *group, const char *key,  
     58                             gint defaultValue); 
    5259    }; 
    5360} 
  • trunk/tests/ConfigTest.cxx

    r92 r93  
    5858    CPPUNIT_ASSERT_EQUAL (650, config.getWindowHeight ()); 
    5959} 
     60 
     61/// 
     62/// @brief Check setting and retrieving values related to the main window. 
     63/// 
     64void 
     65ConfigTest::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  
    2727        CPPUNIT_TEST_SUITE (ConfigTest); 
    2828        CPPUNIT_TEST (defaultValues); 
     29        CPPUNIT_TEST (windowValues); 
    2930        CPPUNIT_TEST_SUITE_END (); 
    3031 
     
    3435 
    3536            void defaultValues (void); 
     37            void windowValues (void); 
    3638    }; 
    3739}