paperbox r157 - in trunk: . src



Author: markoa
Date: Wed Jun  4 20:25:40 2008
New Revision: 157
URL: http://svn.gnome.org/viewvc/paperbox?rev=157&view=rev

Log:
Beginning a config system

Added:
   trunk/src/config.cc
   trunk/src/config.hh
Modified:
   trunk/ChangeLog
   trunk/src/Makefile.am
   trunk/src/main-window.cc
   trunk/src/main-window.hh

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	(original)
+++ trunk/src/Makefile.am	Wed Jun  4 20:25:40 2008
@@ -19,6 +19,8 @@
 	category-factory.hh \
 	category-view.cc \
 	category-view.hh \
+	config.cc \
+	config.hh \
 	dialog-entry.cc \
 	dialog-entry.hh \
 	dialog-tag-entry.cc \

Added: trunk/src/config.cc
==============================================================================
--- (empty file)
+++ trunk/src/config.cc	Wed Jun  4 20:25:40 2008
@@ -0,0 +1,118 @@
+// -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+
+/*
+ *  PaperBox - config.cc
+ *
+ *  Copyright (C) 2008 Marko Anastasov
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <glibmm/fileutils.h>
+#include <glibmm-utils/log-stream-utils.h>
+#include <giomm/error.h>
+#include <giomm/file.h>
+#include "config.hh"
+#include "main-window.hh"
+
+namespace paperbox {
+
+    using boost::shared_ptr;
+    using Glib::ustring;
+
+    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);
+ 
+        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();
+    }
+
+    shared_ptr<Config>
+    get_default_config()
+    {
+        shared_ptr<Config> cfg(new KeyFileConfig());
+        return cfg;
+    }
+
+    KeyFileConfig::KeyFileConfig()
+    {
+        check_config_file();
+
+        try {
+            if (! keyfile_.load_from_file(KEY_FILE_CONFIG_PATH)) {
+                g_warning("Could not find configuration file!");
+                //TODO: handle this better, although I don't know
+                //if false is even possible since there's exception throwing
+                //File a bug or something for glibmm docs.
+            }
+        } catch (Glib::FileError& e) {
+            LOG_EXCEPTION("Could not load program configuration file: "
+                          << e.what());
+        }
+    }
+
+    KeyFileConfig::~KeyFileConfig()
+    {
+    }
+
+    void
+    KeyFileConfig::get_window_size(int& width, int& height)
+    {
+        width = keyfile_.get_integer(UI_GROUP, "window_width");
+        height = keyfile_.get_integer(UI_GROUP, "window_height");
+    }
+
+    void
+    KeyFileConfig::set_window_size(const int width, const int height)
+    {
+        keyfile_.set_integer(UI_GROUP, "window_width", width);
+        keyfile_.set_integer(UI_GROUP, "window_height", height);
+        save();
+    }
+
+    void
+    KeyFileConfig::save()
+    {
+        Glib::RefPtr<Gio::File> file =
+            Gio::File::create_for_path(KEY_FILE_CONFIG_PATH);
+
+        try {
+            Glib::RefPtr<Gio::FileOutputStream> stream = file->replace();
+            stream->write(keyfile_.to_data());
+            stream->close();
+        } catch (Gio::Error& e) {
+            LOG_EXCEPTION("Failed to save key file configuration: "
+                          << e.what());
+        }
+    }
+
+}

Added: trunk/src/config.hh
==============================================================================
--- (empty file)
+++ trunk/src/config.hh	Wed Jun  4 20:25:40 2008
@@ -0,0 +1,63 @@
+// -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+
+/*
+ *  PaperBox - config.hh
+ *
+ *  Copyright (C) 2008 Marko Anastasov
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <boost/shared_ptr.hpp>
+#include <glibmm/miscutils.h>
+#include <glibmm/keyfile.h>
+
+namespace paperbox {
+
+    class Config
+    {
+    public:
+        Config() {}
+        virtual ~Config() {}
+
+        virtual void get_window_size(int& width, int& height) = 0;
+        virtual void set_window_size(const int width, const int height) = 0;
+
+        virtual void save() = 0;
+    };
+
+    boost::shared_ptr<Config> get_default_config();
+
+    const std::string KEY_FILE_CONFIG_PATH = Glib::get_home_dir() +
+        G_DIR_SEPARATOR_S + ".config" + G_DIR_SEPARATOR + "paperbox" +
+        G_DIR_SEPARATOR + "config.ini";
+
+    class KeyFileConfig : public Config
+    {
+    public:
+        explicit KeyFileConfig();
+        virtual ~KeyFileConfig();
+
+        virtual void get_window_size(int& width, int& height);
+        virtual void set_window_size(const int width, const int height);
+
+        virtual void save();
+
+        std::string get_key_file_path();
+
+    protected:
+        Glib::KeyFile keyfile_;
+    };
+}

Modified: trunk/src/main-window.cc
==============================================================================
--- trunk/src/main-window.cc	(original)
+++ trunk/src/main-window.cc	Wed Jun  4 20:25:40 2008
@@ -32,6 +32,7 @@
 #include "browser.hh"
 #include "category-editor.hh"
 #include "category-factory.hh"
+#include "config.hh"
 #include "file-utils.hh"
 #include "document-tag-cloud-model.hh"
 #include "document-tile.hh"
@@ -116,7 +117,12 @@
         tag_box_(false, 4)
     {
         init_gui();
-        set_default_size(800, 690);
+
+        shared_ptr<Config> cfg = get_default_config();
+        int width, height;
+        cfg->get_window_size(width, height);
+        set_default_size(width, height);
+
         setup_pane_pos();
 
         browser_ = Browser::instance();
@@ -129,6 +135,10 @@
 
     MainWindow::~MainWindow()
     {
+        shared_ptr<Config> cfg = get_default_config();
+        int width, height;
+        get_size(width, height);
+        cfg->set_window_size(width, height);
     }
 
     MainWindow*

Modified: trunk/src/main-window.hh
==============================================================================
--- trunk/src/main-window.hh	(original)
+++ trunk/src/main-window.hh	Wed Jun  4 20:25:40 2008
@@ -43,6 +43,9 @@
     class CategoryModel;
     class Document;
 
+    const int DEFAULT_WIDTH = 800;
+    const int DEFAULT_HEIGHT = 690;
+
     class MainWindow : public Gtk::Window
     {
     public:



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