paperbox r160 - in trunk: . src



Author: markoa
Date: Sat Jun  7 15:58:18 2008
New Revision: 160
URL: http://svn.gnome.org/viewvc/paperbox?rev=160&view=rev

Log:
More configuration settings and checks

Modified:
   trunk/ChangeLog
   trunk/src/   (props changed)
   trunk/src/config.cc
   trunk/src/config.hh
   trunk/src/main-window.cc
   trunk/src/main-window.hh

Modified: trunk/src/config.cc
==============================================================================
--- trunk/src/config.cc	(original)
+++ trunk/src/config.cc	Sat Jun  7 15:58:18 2008
@@ -34,29 +34,8 @@
 
     const ustring UI_GROUP = "UI";
 
-    void
-    write_default_key_file()
-    {
-        Glib::KeyFile kf;
-        kf.set_integer(UI_GROUP, "window_width", DEFAULT_WIDTH);
-        kf.set_integer(UI_GROUP, "window_height", DEFAULT_HEIGHT);
-        kf.set_integer(UI_GROUP, "paned_position", 3 * (DEFAULT_WIDTH / 4));
- 
-        Glib::RefPtr<Gio::File> file =
-            Gio::File::create_for_path(KEY_FILE_CONFIG_PATH);
-        Glib::RefPtr<Gio::FileOutputStream> stream = file->create_file();
-        stream->write(kf.to_data());
-        stream->close();
-    }
-
-    // Creates ~/.config/paperbox/config.ini if it does not exist.
-    void
-    check_config_file()
-    {
-        if (! Glib::file_test(KEY_FILE_CONFIG_PATH, Glib::FILE_TEST_EXISTS))
-            write_default_key_file();
-    }
-
+    // Returns the default implementation of the Config interface to
+    // the rest of the application.
     shared_ptr<Config>
     get_default_config()
     {
@@ -66,7 +45,7 @@
 
     KeyFileConfig::KeyFileConfig()
     {
-        check_config_file();
+        check_file_presence();
 
         try {
             if (! keyfile_.load_from_file(KEY_FILE_CONFIG_PATH)) {
@@ -75,6 +54,14 @@
                 //if false is even possible since there's exception throwing
                 //File a bug or something for glibmm docs.
             }
+
+            if (! has_all_fields()) {
+                write_default(true);
+                // reload, although this will reset previous configuration,
+                // it should be smarter in later versions
+                keyfile_.load_from_file(KEY_FILE_CONFIG_PATH);
+            }
+
         } catch (Glib::FileError& e) {
             LOG_EXCEPTION("Could not load program configuration file: "
                           << e.what());
@@ -86,6 +73,67 @@
     }
 
     void
+    KeyFileConfig::write_default(bool replace)
+    {
+        Glib::KeyFile keyfile_;
+
+        keyfile_.set_integer(UI_GROUP, "window_x", DEFAULT_X);
+        keyfile_.set_integer(UI_GROUP, "window_y", DEFAULT_Y);
+        keyfile_.set_integer(UI_GROUP, "window_width", DEFAULT_WIDTH);
+        keyfile_.set_integer(UI_GROUP, "window_height", DEFAULT_HEIGHT);
+        keyfile_.set_integer(UI_GROUP, "main_paned_position",
+                             3 * (DEFAULT_WIDTH / 4));
+        keyfile_.set_integer(UI_GROUP, "category_paned_position", 
+                             DEFAULT_CATEGORY_PANED_POS);
+
+        Glib::RefPtr<Gio::File> file =
+            Gio::File::create_for_path(KEY_FILE_CONFIG_PATH);
+        Glib::RefPtr<Gio::FileOutputStream> stream;
+        stream = replace ? file->replace() : file->create_file();
+        stream->write(keyfile_.to_data());
+        stream->close();
+    }
+
+    // Creates ~/.config/paperbox/config.ini if it does not exist.
+    void
+    KeyFileConfig::check_file_presence()
+    {
+        if (! Glib::file_test(KEY_FILE_CONFIG_PATH, Glib::FILE_TEST_EXISTS))
+            write_default(false);
+    }
+
+    bool
+    KeyFileConfig::has_all_fields()
+    {
+        try {
+            return keyfile_.has_key(UI_GROUP, "window_x") &&
+                keyfile_.has_key(UI_GROUP, "window_y") &&
+                keyfile_.has_key(UI_GROUP, "window_width") &&
+                keyfile_.has_key(UI_GROUP, "window_height") &&
+                keyfile_.has_key(UI_GROUP, "main_paned_position") &&
+                keyfile_.has_key(UI_GROUP, "category_paned_position");
+        } catch (const Glib::KeyFileError& e) {
+            LOG_EXCEPTION("Could not check configuration keys for presence"
+                    << e.what());
+            return false;
+        }
+    }
+
+    void
+    KeyFileConfig::get_window_position(int& x, int& y)
+    {
+        x = keyfile_.get_integer(UI_GROUP, "window_x");
+        y = keyfile_.get_integer(UI_GROUP, "window_y");
+    }
+
+    void
+    KeyFileConfig::set_window_position(const int x, const int y)
+    {
+        keyfile_.set_integer(UI_GROUP, "window_x", x);
+        keyfile_.set_integer(UI_GROUP, "window_y", y);
+    }
+
+    void
     KeyFileConfig::get_window_size(int& width, int& height)
     {
         width = keyfile_.get_integer(UI_GROUP, "window_width");
@@ -93,9 +141,9 @@
     }
 
     void
-    KeyFileConfig::get_paned_position(int& pos)
+    KeyFileConfig::get_main_paned_position(int& pos)
     {
-        pos = keyfile_.get_integer(UI_GROUP, "paned_position");
+        pos = keyfile_.get_integer(UI_GROUP, "main_paned_position");
     }
 
     void
@@ -106,9 +154,22 @@
     }
 
     void
-    KeyFileConfig::set_paned_position(const int pos)
+    KeyFileConfig::set_main_paned_position(const int pos)
+    {
+        keyfile_.set_integer(UI_GROUP, "main_paned_position", pos);
+    }
+
+
+    void
+    KeyFileConfig::get_category_paned_position(int& pos)
+    {
+        pos = keyfile_.get_integer(UI_GROUP, "category_paned_position");
+    }
+
+    void
+    KeyFileConfig::set_category_paned_position(const int pos)
     {
-        keyfile_.set_integer(UI_GROUP, "paned_position", pos);
+        keyfile_.set_integer(UI_GROUP, "category_paned_position", pos);
     }
 
     void

Modified: trunk/src/config.hh
==============================================================================
--- trunk/src/config.hh	(original)
+++ trunk/src/config.hh	Sat Jun  7 15:58:18 2008
@@ -32,12 +32,20 @@
         Config() {}
         virtual ~Config() {}
 
+        virtual void get_window_position(int& x, int& y) = 0;
+        virtual void set_window_position(const int x, const int y) = 0;
+
         virtual void get_window_size(int& width, int& height) = 0;
         virtual void set_window_size(const int width, const int height) = 0;
  
-        virtual void get_paned_position(int& pos) = 0;
-        virtual void set_paned_position(const int pos) = 0;
+        virtual void get_main_paned_position(int& pos) = 0;
+        virtual void set_main_paned_position(const int pos) = 0;
+
+        virtual void get_category_paned_position(int& pos) = 0;
+        virtual void set_category_paned_position(const int pos) = 0;
 
+        virtual bool has_all_fields() = 0;
+        
         virtual void save() = 0;
     };
 
@@ -53,17 +61,28 @@
         explicit KeyFileConfig();
         virtual ~KeyFileConfig();
 
+        virtual void get_window_position(int& x, int& y);
+        virtual void set_window_position(const int x, const int y);
+
         virtual void get_window_size(int& width, int& height);
         virtual void set_window_size(const int width, const int height);
 
-        virtual void get_paned_position(int& pos);
-        virtual void set_paned_position(const int pos);
+        virtual void get_main_paned_position(int& pos);
+        virtual void set_main_paned_position(const int pos);
 
+        virtual void get_category_paned_position(int& pos);
+        virtual void set_category_paned_position(const int pos);
+
+        virtual bool has_all_fields();
+ 
         virtual void save();
 
         std::string get_key_file_path();
 
     protected:
+        void check_file_presence();
+        void write_default(bool replace);
+
         Glib::KeyFile keyfile_;
     };
 }

Modified: trunk/src/main-window.cc
==============================================================================
--- trunk/src/main-window.cc	(original)
+++ trunk/src/main-window.cc	Sat Jun  7 15:58:18 2008
@@ -116,15 +116,19 @@
         tag_cloud_vbox_(0),
         tag_box_(false, 4)
     {
-        init_gui();
-
         shared_ptr<Config> cfg = get_default_config();
+        init_gui(cfg);
+
+        int x, y;
+        cfg->get_window_position(x, y);
+        move(x, y);
+
         int width, height;
         cfg->get_window_size(width, height);
         set_default_size(width, height);
 
         int pane_pos;
-        cfg->get_paned_position(pane_pos);
+        cfg->get_main_paned_position(pane_pos);
         hpane_->set_position(pane_pos);
 
         browser_ = Browser::instance();
@@ -137,14 +141,23 @@
 
     MainWindow::~MainWindow()
     {
+        // save layout in the config file
         shared_ptr<Config> cfg = get_default_config();
+
+        int x, y;
+        get_position(x, y);
+        g_debug("%d, %d", x, y);
+        cfg->set_window_position(x, y);
         
         int width, height;
         get_size(width, height);
         cfg->set_window_size(width, height);
 
         int pos = hpane_->get_position();
-        cfg->set_paned_position(pos);
+        cfg->set_main_paned_position(pos);
+
+        pos = right_vpane_->get_position();
+        cfg->set_category_paned_position(pos);
 
         cfg->save();
     }
@@ -161,7 +174,7 @@
     }
 
     void
-    MainWindow::init_gui()
+    MainWindow::init_gui(const shared_ptr<Config>& cfg)
     {
         get_widgets_from_ui_file();
         setup_tiles();
@@ -177,7 +190,9 @@
 
         setup_categories();
 
-        right_vpane_->set_position(280);
+        int paned_pos;
+        cfg->get_category_paned_position(paned_pos);
+        right_vpane_->set_position(paned_pos);
 
         set_title(Glib::Util::uprintf("%s %s", PACKAGE_NAME, PACKAGE_VERSION));
 

Modified: trunk/src/main-window.hh
==============================================================================
--- trunk/src/main-window.hh	(original)
+++ trunk/src/main-window.hh	Sat Jun  7 15:58:18 2008
@@ -41,10 +41,15 @@
 
     class Browser;
     class CategoryModel;
+    class Config;
     class Document;
 
+    // These consts are used by configuration code.
+    const int DEFAULT_X = 50;
+    const int DEFAULT_Y = 50;
     const int DEFAULT_WIDTH = 800;
     const int DEFAULT_HEIGHT = 690;
+    const int DEFAULT_CATEGORY_PANED_POS = 280;
 
     class MainWindow : public Gtk::Window
     {
@@ -56,7 +61,7 @@
         static MainWindow* create();
 
     protected:
-        void init_gui();
+        void init_gui(const boost::shared_ptr<Config>& cfg);
         void get_widgets_from_ui_file();
         void setup_tiles();
         void setup_categories();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]