[gnote] Local pref storage for addins.



commit 442cdc4a19e681573a678b404f346ef83dd3579b
Author: Hubert Figuiere <hub figuiere net>
Date:   Tue Jun 16 19:43:17 2009 -0400

    Local pref storage for addins.

 src/Makefile.am                                    |    1 +
 src/addinmanager.cpp                               |   14 +++-
 src/addinmanager.hpp                               |    6 +
 .../stickynoteimport/stickynoteimportnoteaddin.cpp |   31 +++++-
 .../stickynoteimport/stickynoteimportnoteaddin.hpp |    2 +-
 src/addins/tomboyimport/tomboyimportaddin.cpp      |    2 +-
 src/addins/tomboyimport/tomboyimportaddin.hpp      |    2 +-
 src/base/inifile.cpp                               |  107 ++++++++++++++++++++
 src/base/inifile.hpp                               |   63 ++++++++++++
 src/importaddin.hpp                                |    4 +-
 src/notemanager.cpp                                |    2 +-
 src/preferences.cpp                                |    2 -
 src/preferences.hpp                                |    2 -
 13 files changed, 222 insertions(+), 16 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index b2a353a..37f6e6f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -76,6 +76,7 @@ endif
 libgnote_a_SOURCES = \
 	base/singleton.hpp \
 	base/macros.hpp \
+	base/inifile.hpp base/inifile.cpp \
 	sharp/addinstreemodel.hpp sharp/addinstreemodel.cpp \
 	sharp/datetime.hpp sharp/datetime.cpp \
 	sharp/dynamicmodule.hpp sharp/dynamicmodule.cpp \
diff --git a/src/addinmanager.cpp b/src/addinmanager.cpp
index 0d42084..4392345 100644
--- a/src/addinmanager.cpp
+++ b/src/addinmanager.cpp
@@ -24,6 +24,7 @@
 #include <boost/checked_delete.hpp>
 
 #include "sharp/map.hpp"
+#include "sharp/directory.hpp"
 #include "sharp/dynamicmodule.hpp"
 
 #include "addinmanager.hpp"
@@ -37,13 +38,14 @@
 #if 1
 
 // HACK to make sure we link the modules needed only for addins
-
+#include "base/inifile.hpp"
 #include "sharp/fileinfo.hpp"
 #include "sharp/streamwriter.hpp"
 #include "sharp/xsltransform.hpp"
 #include "sharp/xsltargumentlist.hpp"
 
 bool links[] = {
+  base::IniFileLink_,
   sharp::FileInfoLink_,
   sharp::StreamWriterLink_,
   sharp::XslTransformLink_,
@@ -69,6 +71,7 @@ namespace gnote {
   AddinManager::AddinManager(const std::string & conf_dir)
     : m_gnote_conf_dir(conf_dir)
   {
+    m_addins_prefs_dir = Glib::build_filename(conf_dir, "addins");
     initialize_sharp_addins();
   }
 
@@ -89,6 +92,15 @@ namespace gnote {
 
   void AddinManager::initialize_sharp_addins()
   {
+
+    try {
+      if(!sharp::directory_exists(m_addins_prefs_dir)) {
+        sharp::directory_create(m_addins_prefs_dir);
+      }
+    }
+    catch(...) {
+
+    }
     // get the factory
 
     REGISTER_BUILTIN_NOTE_ADDIN(NoteRenameWatcher);
diff --git a/src/addinmanager.hpp b/src/addinmanager.hpp
index 2286a8e..ad7ffe2 100644
--- a/src/addinmanager.hpp
+++ b/src/addinmanager.hpp
@@ -46,6 +46,11 @@ public:
   AddinManager(const std::string & conf_dir);
   ~AddinManager();
 
+  std::string & get_prefs_dir()
+    {
+      return m_addins_prefs_dir;
+    }
+
   void load_addins_for_note(const Note::Ptr &);
   void get_application_addins(std::list<ApplicationAddin*> &) const;
   void get_preference_tab_addins(std::list<PreferenceTabAddin *> &) const;
@@ -64,6 +69,7 @@ private:
   void initialize_sharp_addins();
     
   const std::string m_gnote_conf_dir;
+  std::string m_addins_prefs_dir;
   sharp::ModuleManager m_module_manager;
   std::list<sharp::IfaceFactoryBase*> m_builtin_ifaces;
   /// Key = TypeExtensionNode.Id
diff --git a/src/addins/stickynoteimport/stickynoteimportnoteaddin.cpp b/src/addins/stickynoteimport/stickynoteimportnoteaddin.cpp
index 66cb3b7..dfd9110 100644
--- a/src/addins/stickynoteimport/stickynoteimportnoteaddin.cpp
+++ b/src/addins/stickynoteimport/stickynoteimportnoteaddin.cpp
@@ -20,13 +20,16 @@
 #include <boost/format.hpp>
 
 #include <glibmm/i18n.h>
+#include <glibmm/keyfile.h>
 #include <gtkmm/image.h>
 #include <gtkmm/stock.h>
 
+#include "base/inifile.hpp"
 #include "stickynoteimportnoteaddin.hpp"
 #include "sharp/files.hpp"
 #include "sharp/string.hpp"
 #include "sharp/xml.hpp"
+#include "addinmanager.hpp"
 #include "debug.hpp"
 #include "note.hpp"
 #include "notemanager.hpp"
@@ -76,6 +79,8 @@ static const char * DEBUG_CREATE_ERROR_BASE = "StickyNoteImporter: Error while t
 static const char * DEBUG_FIRST_RUN_DETECTED = "StickyNoteImporter: Detecting that importer has never been run...";
 //static const char * DEBUG_GCONF_SET_ERROR_BASE = "StickyNoteImporter: Error setting initial GConf first run key value: %s";
 
+static const char * PREFS_FILE = "stickynoteimport.ini";
+
 const char * TB_STICKYNOTEIMPORTER_FIRST_RUN =
   "/apps/tomboy/sticky_note_importer/sticky_importer_first_run";
 
@@ -125,12 +130,18 @@ void StickyNoteImportNoteAddin::shutdown()
 
 
 
-bool StickyNoteImportNoteAddin::want_to_run()
+bool StickyNoteImportNoteAddin::want_to_run(gnote::NoteManager & manager)
 {
   bool want_run;
+  std::string prefs_file =
+    Glib::build_filename(manager.get_addin_manager().get_prefs_dir(),
+                         PREFS_FILE);
+  base::IniFile ini_file(prefs_file);
+  ini_file.load();
 
   if(s_sticky_file_might_exist) {
-    want_run = Preferences::obj().get<bool>(Preferences::STICKYNOTEIMPORTER_FIRST_RUN);
+    want_run = !ini_file.get_bool("status", "first_run");
+
     if(want_run) {
       // we think we want to run
       // so we check for Tomboy. If Tomboy wants to run then we want
@@ -152,7 +163,8 @@ bool StickyNoteImportNoteAddin::want_to_run()
       // probably already imported.
       if(!tb_must_run) {
         // Mark like we already ran.
-        Preferences::obj().set<bool>(Preferences::STICKYNOTEIMPORTER_FIRST_RUN, false);
+        ini_file.set_bool("status", "first_run", true);
+//        Preferences::obj().set<bool>(Preferences::STICKYNOTEIMPORTER_FIRST_RUN, false);
         want_run = false;
       }
     }
@@ -163,10 +175,19 @@ bool StickyNoteImportNoteAddin::want_to_run()
 
 bool StickyNoteImportNoteAddin::first_run(gnote::NoteManager & manager)
 {
-  bool firstRun = Preferences::obj().get<bool> (Preferences::STICKYNOTEIMPORTER_FIRST_RUN);
+  base::IniFile ini_file(Glib::build_filename(
+                           manager.get_addin_manager().get_prefs_dir(), 
+                           PREFS_FILE));
+  
+  ini_file.load();
+
+  bool firstRun = !ini_file.get_bool("status", "first_run");
+//Preferences::obj().get<bool> (Preferences::STICKYNOTEIMPORTER_FIRST_RUN);
 
   if (firstRun) {
-    Preferences::obj().set<bool> (Preferences::STICKYNOTEIMPORTER_FIRST_RUN, false);
+    ini_file.set_bool("status", "first_run", true);
+
+//    Preferences::obj().set<bool> (Preferences::STICKYNOTEIMPORTER_FIRST_RUN, false);
 
     DBG_OUT(DEBUG_FIRST_RUN_DETECTED);
 
diff --git a/src/addins/stickynoteimport/stickynoteimportnoteaddin.hpp b/src/addins/stickynoteimport/stickynoteimportnoteaddin.hpp
index f0ce271..66c8bfc 100644
--- a/src/addins/stickynoteimport/stickynoteimportnoteaddin.hpp
+++ b/src/addins/stickynoteimport/stickynoteimportnoteaddin.hpp
@@ -66,7 +66,7 @@ public:
   virtual void initialize();
   virtual void shutdown();
 
-  virtual bool want_to_run();
+  virtual bool want_to_run(gnote::NoteManager & manager);
   virtual bool first_run(gnote::NoteManager & manager);
 
 private:
diff --git a/src/addins/tomboyimport/tomboyimportaddin.cpp b/src/addins/tomboyimport/tomboyimportaddin.cpp
index 091b3b0..c03a583 100644
--- a/src/addins/tomboyimport/tomboyimportaddin.cpp
+++ b/src/addins/tomboyimport/tomboyimportaddin.cpp
@@ -80,7 +80,7 @@ void TomboyImportAddin::shutdown()
 }
 
 
-bool TomboyImportAddin::want_to_run()
+bool TomboyImportAddin::want_to_run(gnote::NoteManager & )
 {
   return sharp::directory_exists(m_tomboy_path);
 }
diff --git a/src/addins/tomboyimport/tomboyimportaddin.hpp b/src/addins/tomboyimport/tomboyimportaddin.hpp
index 9a6783a..494d909 100644
--- a/src/addins/tomboyimport/tomboyimportaddin.hpp
+++ b/src/addins/tomboyimport/tomboyimportaddin.hpp
@@ -58,7 +58,7 @@ public:
     }
   virtual void initialize();
   virtual void shutdown();
-  virtual bool want_to_run();
+  virtual bool want_to_run(gnote::NoteManager & manager);
   virtual bool first_run(gnote::NoteManager & manager);
 
 private:
diff --git a/src/base/inifile.cpp b/src/base/inifile.cpp
new file mode 100644
index 0000000..9f544c1
--- /dev/null
+++ b/src/base/inifile.cpp
@@ -0,0 +1,107 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "base/inifile.hpp"
+#include "debug.hpp"
+
+namespace base {
+
+const bool IniFileLink_ = true;
+
+
+bool IniFile::load()
+{
+  GError *error = NULL;
+  if(!g_key_file_load_from_file(m_keyfile, m_filename.c_str(), 
+                                G_KEY_FILE_NONE, &error)) {
+    DBG_OUT("can't load keyfile '%s': %s", m_filename.c_str(),
+            error->message);
+    g_error_free(error);
+    return false;
+  }
+  return true;
+}
+
+
+bool IniFile::save()
+{
+  bool success = true;
+  GError *error = NULL;
+  gsize length;
+  size_t written;
+  gchar * data;
+
+  data = g_key_file_to_data(m_keyfile, &length, &error);
+  if(!data) {
+    ERR_OUT("couldn't get keyfile data: %s", error->message);
+    g_error_free(error);
+    return false;
+  }
+
+  FILE * f = fopen(m_filename.c_str(), "w");
+  if(f) {
+    written = fwrite(data, 1, length, f);
+    if(written != length) {
+      ERR_OUT("short write: %zd of %zd", written, length);
+      success = false;
+    }
+    fclose(f);
+  }
+  else {
+    ERR_OUT("couldn't open file '%s': %s", m_filename.c_str(), strerror(errno));
+    success = false;
+  }
+  g_free(data);
+
+  return success;
+}
+
+
+bool IniFile::get_bool(const char * group, const char * key, bool dflt)
+{
+  GError *error = NULL;
+  gboolean value = g_key_file_get_boolean(m_keyfile, group, key, &error);
+  if(error) {
+    if(error->code == G_KEY_FILE_ERROR_INVALID_VALUE) {
+      DBG_OUT("key %s:%s is not bool", group, key);
+    }
+    value = dflt;
+    g_error_free(error);
+  }
+
+  return value;
+}
+
+
+void IniFile::set_bool(const char * group, const char * key, bool value)
+{
+  g_key_file_set_boolean(m_keyfile, group, key, value);
+}
+
+
+}
diff --git a/src/base/inifile.hpp b/src/base/inifile.hpp
new file mode 100644
index 0000000..ee831c2
--- /dev/null
+++ b/src/base/inifile.hpp
@@ -0,0 +1,63 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __BASE_INIFILE_HPP_
+#define __BASE_INIFILE_HPP_
+
+#include <string>
+#include <glib.h>
+
+namespace base {
+
+
+extern const bool IniFileLink_;
+
+class IniFile
+{
+public:
+  
+  IniFile(const std::string & filename)
+    : m_filename(filename)
+    , m_keyfile(g_key_file_new())
+    {
+    }
+  ~IniFile()
+    {
+      g_key_file_free(m_keyfile);
+    }
+
+  bool load();
+  bool save();
+  bool get_bool(const char * group, const char * key, bool dflt = false);
+  void set_bool(const char * group, const char * key, bool value);
+
+private:
+  std::string m_filename;
+  GKeyFile    *m_keyfile;
+};
+
+
+}
+
+#endif
diff --git a/src/importaddin.hpp b/src/importaddin.hpp
index 282e3d3..82dc6ba 100644
--- a/src/importaddin.hpp
+++ b/src/importaddin.hpp
@@ -40,12 +40,12 @@ public:
 
   /** Return whether the importer want to run at startup or not 
    */
-  virtual bool want_to_run() = 0;
+  virtual bool want_to_run(NoteManager & manager) = 0;
   /** Run import during first run.
    *  @param manager the NoteManager to import into.
    *  @return true in case of success.
    */
-  virtual bool first_run(gnote::NoteManager & manager) = 0;
+  virtual bool first_run(NoteManager & manager) = 0;
 
 protected:
   bool m_initialized;
diff --git a/src/notemanager.cpp b/src/notemanager.cpp
index 4519f01..7f837c2 100644
--- a/src/notemanager.cpp
+++ b/src/notemanager.cpp
@@ -132,7 +132,7 @@ namespace gnote {
 
         DBG_OUT("importing");
         (*iter)->initialize();
-        if((*iter)->want_to_run()) {
+        if((*iter)->want_to_run(*this)) {
           has_imported |= (*iter)->first_run(*this);
         }
       }
diff --git a/src/preferences.cpp b/src/preferences.cpp
index 075fcc1..e3f6d47 100644
--- a/src/preferences.cpp
+++ b/src/preferences.cpp
@@ -50,8 +50,6 @@ namespace gnote {
   const char * Preferences::EXPORTHTML_EXPORT_LINKED = "/apps/gnote/export_html/export_linked";
   const char * Preferences::EXPORTHTML_EXPORT_LINKED_ALL = "/apps/gnote/export_html/export_linked_all";
 
-  const char * Preferences::STICKYNOTEIMPORTER_FIRST_RUN = "/apps/gnote/sticky_note_importer/sticky_importer_first_run";
-
   const char * Preferences::SYNC_CLIENT_ID = "/apps/gnote/sync/sync_guid";
   const char * Preferences::SYNC_LOCAL_PATH = "/apps/gnote/sync/sync_local_path";
   const char * Preferences::SYNC_SELECTED_SERVICE_ADDIN = "/apps/gnote/sync/sync_selected_service_addin";
diff --git a/src/preferences.hpp b/src/preferences.hpp
index 2048b63..5741b8e 100644
--- a/src/preferences.hpp
+++ b/src/preferences.hpp
@@ -60,8 +60,6 @@ namespace gnote {
     static const char *EXPORTHTML_EXPORT_LINKED;
     static const char *EXPORTHTML_EXPORT_LINKED_ALL;
 
-    static const char *STICKYNOTEIMPORTER_FIRST_RUN;
-
     static const char *SYNC_CLIENT_ID;
     static const char *SYNC_LOCAL_PATH;
     static const char *SYNC_SELECTED_SERVICE_ADDIN;



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