Changeset 96

Show
Ignore:
Timestamp:
04/20/06 09:41:29 (2 years ago)
Author:
jordi
Message:

The configuration class can now hold the last used directory to open a file.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/Config.cxx

    r94 r96  
    2323 
    2424// Constants 
     25static gchar *DEFAULT_OPEN_FILE_FOLDER = NULL; 
    2526static gint DEFAULT_WINDOW_HEIGHT = 650; 
    2627static gint DEFAULT_WINDOW_WIDTH = 600; 
     
    138139 
    139140/// 
     141/// @brief Gets an string configuration option. 
     142/// 
     143/// @param group Configuration group where the key belongs. 
     144/// @param key The key name to retrieve its value. 
     145/// @param defaultValue The default value to return if @a key doesn't exists. 
     146/// 
     147/// @return The value of the @a key in @a group or @a defaultValue if @a key 
     148///         doesn't exists. 
     149/// 
     150gchar * 
     151Config::getString (const gchar *group, const gchar *key,  
     152                   const gchar *defaultValue) 
     153{ 
     154    gchar *value = g_strdup (defaultValue); 
     155    if ( g_key_file_has_key (m_Values, group, key, NULL) ) 
     156    { 
     157        GError *error = NULL;  
     158        gchar *savedValue =  
     159            g_key_file_get_string (m_Values, group, key, &error); 
     160        if ( NULL == error ) 
     161        { 
     162            g_free (value); 
     163            value = savedValue; 
     164        } 
     165        else 
     166        { 
     167            g_warning ("Error reading key '%s' from group '%s': %s\n",  
     168                       key, group, error->message); 
     169            g_error_free (error); 
     170        } 
     171    } 
     172 
     173    return value; 
     174} 
     175 
     176/// 
     177/// @brief Gets the last folder used to open a file. 
     178/// 
     179/// @return The path to the last folder that was used to open a file. 
     180///         This string must be freed using g_free(). NULL if no folder 
     181///         has been used yet, which means to use the current working directory. 
     182/// 
     183gchar * 
     184Config::getOpenFileFolder () 
     185{ 
     186    return getString ("open dialog", "folder", DEFAULT_OPEN_FILE_FOLDER); 
     187} 
     188 
     189/// 
    140190/// @brief Gets the main window's height. 
    141191/// 
     
    212262    g_free (contents); 
    213263    g_free (configFile); 
     264} 
     265 
     266/// 
     267/// @brief Saves the last folder used to open a file. 
     268/// 
     269/// @param folder The path to the last folder used. 
     270/// 
     271void 
     272Config::setOpenFileFolder (const gchar *folder) 
     273{ 
     274    g_key_file_set_string (m_Values, "open dialog", "folder", folder); 
    214275} 
    215276 
  • trunk/src/Config.h

    r94 r96  
    3939            ~Config (void); 
    4040 
     41            gchar *getOpenFileFolder (void); 
    4142            gint getWindowHeight (void); 
    4243            gint getWindowWidth (void); 
     
    4445            gint getWindowY (void); 
    4546            void save(void); 
     47            void setOpenFileFolder (const gchar *folder); 
    4648            void setWindowSize (gint width, gint height); 
    4749            void setWindowPos (gint x, gint y); 
     
    5860            gint getInteger (const gchar *group, const char *key,  
    5961                             gint defaultValue); 
     62            gchar *getString (const gchar *group, const char *key,  
     63                              const gchar *defaultValue); 
    6064    }; 
    6165} 
  • trunk/tests/ConfigTest.cxx

    r93 r96  
    3333ConfigTest::setUp () 
    3434{ 
     35    Config::loadFile (FALSE); 
    3536} 
    3637 
     
    5051ConfigTest::defaultValues () 
    5152{ 
    52     Config::loadFile (FALSE); 
    5353    Config &config = Config::getConfig (); 
    5454 
     
    5757    CPPUNIT_ASSERT_EQUAL (600, config.getWindowWidth ()); 
    5858    CPPUNIT_ASSERT_EQUAL (650, config.getWindowHeight ()); 
     59    CPPUNIT_ASSERT_EQUAL ((gchar *)NULL, config.getOpenFileFolder ()); 
    5960} 
    6061 
     
    6566ConfigTest::windowValues () 
    6667{ 
    67     Config::loadFile (FALSE); 
    6868    Config &config = Config::getConfig (); 
    6969 
     
    7676    CPPUNIT_ASSERT_EQUAL (90, config.getWindowHeight ());     
    7777} 
     78 
     79/// 
     80/// @brief Check setting the current folder for opening files. 
     81/// 
     82void 
     83ConfigTest::currentFolder () 
     84{ 
     85    Config &config = Config::getConfig (); 
     86 
     87    config.setOpenFileFolder ("/tmp"); 
     88    gchar *openFolder = config.getOpenFileFolder (); 
     89    CPPUNIT_ASSERT ( 0 == g_ascii_strcasecmp("/tmp", openFolder)); 
     90    g_free (openFolder); 
     91} 
  • trunk/tests/ConfigTest.h

    r93 r96  
    2828        CPPUNIT_TEST (defaultValues); 
    2929        CPPUNIT_TEST (windowValues); 
     30        CPPUNIT_TEST (currentFolder); 
    3031        CPPUNIT_TEST_SUITE_END (); 
    3132 
     
    3637            void defaultValues (void); 
    3738            void windowValues (void); 
     39            void currentFolder (void); 
    3840    }; 
    3941}