[gnote] Conform to the XDG Base Directory Specification



commit 227809d37eaa8553b4d0e091de9411ce73489815
Author: Debarshi Ray <debarshir src gnome org>
Date:   Sat Nov 28 03:42:13 2009 +0200

    Conform to the XDG Base Directory Specification
    
    Use the directories mentioned in the specification:
    http://standards.freedesktop.org/basedir-spec/latest/
    http://live.gnome.org/GnomeGoals/XDGConfigFolders
    
    By default, these are:
    Notes: ~/.local/share/gnote/
    Configuration and add-ins: ~/.config/gnote/
    Caches: ~/.cache/gnote/
    
    Configuration and notes from ~/.gnote should be copied to the new
    location without modifying the old location.
    
    Fixes: https://bugzilla.gnome.org/579359

 src/addinmanager.cpp                        |   32 ++++++++++++++++
 src/addinmanager.hpp                        |    1 +
 src/addins/bugzilla/bugzillalink.cpp        |    3 +-
 src/addins/bugzilla/bugzillanoteaddin.cpp   |   46 +++++++++++++++++++++++
 src/addins/bugzilla/bugzillanoteaddin.hpp   |    6 ++-
 src/addins/bugzilla/bugzillapreferences.cpp |    3 +-
 src/gnote.cpp                               |   39 +++++++++++++-------
 src/gnote.hpp                               |    6 +++-
 src/notemanager.cpp                         |   52 ++++++++++++++++++++++++++-
 src/notemanager.hpp                         |    1 +
 src/sharp/directory.cpp                     |   38 +++++++++++++++++++
 src/sharp/directory.hpp                     |   11 ++++++
 12 files changed, 218 insertions(+), 20 deletions(-)
---
diff --git a/src/addinmanager.cpp b/src/addinmanager.cpp
index 4fa8378..062b8a5 100644
--- a/src/addinmanager.cpp
+++ b/src/addinmanager.cpp
@@ -30,6 +30,7 @@
 #include "addinmanager.hpp"
 #include "addinpreferencefactory.hpp"
 #include "debug.hpp"
+#include "gnote.hpp"
 #include "watchers.hpp"
 #include "notebooks/notebookapplicationaddin.hpp"
 #include "notebooks/notebooknoteaddin.hpp"
@@ -72,6 +73,22 @@ namespace gnote {
     : m_gnote_conf_dir(conf_dir)
   {
     m_addins_prefs_dir = Glib::build_filename(conf_dir, "addins");
+
+    const bool is_first_run
+                 = !sharp::directory_exists(m_addins_prefs_dir);
+    const std::string old_addins_dir
+                        = Glib::build_filename(Gnote::old_note_dir(),
+                                               "addins");
+    const bool migration_needed
+                 = is_first_run
+                   && sharp::directory_exists(old_addins_dir);
+
+    if (is_first_run)
+      sharp::directory_create(m_addins_prefs_dir);
+
+    if (migration_needed)
+      migrate_addins(old_addins_dir);
+
     initialize_sharp_addins();
   }
 
@@ -202,4 +219,19 @@ namespace gnote {
     }
     return NULL;
   }
+
+  void AddinManager::migrate_addins(const std::string & old_addins_dir)
+  {
+    const Glib::RefPtr<Gio::File> src
+      = Gio::File::create_for_path(old_addins_dir);
+    const Glib::RefPtr<Gio::File> dest
+      = Gio::File::create_for_path(m_gnote_conf_dir);
+
+    try {
+      sharp::directory_copy(src, dest);
+    }
+    catch (const Gio::Error & e) {
+      DBG_OUT("AddinManager: migrating addins: %s", e.what().c_str());
+    }
+  }
 }
diff --git a/src/addinmanager.hpp b/src/addinmanager.hpp
index ad7ffe2..991938e 100644
--- a/src/addinmanager.hpp
+++ b/src/addinmanager.hpp
@@ -67,6 +67,7 @@ public:
 private:
 
   void initialize_sharp_addins();
+  void migrate_addins(const std::string & old_addins_dir);
     
   const std::string m_gnote_conf_dir;
   std::string m_addins_prefs_dir;
diff --git a/src/addins/bugzilla/bugzillalink.cpp b/src/addins/bugzilla/bugzillalink.cpp
index d08a06c..844a06b 100644
--- a/src/addins/bugzilla/bugzillalink.cpp
+++ b/src/addins/bugzilla/bugzillalink.cpp
@@ -22,6 +22,7 @@
 #include "debug.hpp"
 #include "utils.hpp"
 #include "bugzillalink.hpp"
+#include "bugzillanoteaddin.hpp"
 
 namespace bugzilla {
 
@@ -78,7 +79,7 @@ namespace bugzilla {
 
     std::string host = uri.get_host();
 
-    std::string imageDir = Glib::get_home_dir() + "/.gnote/BugzillaIcons/";
+    std::string imageDir = BugzillaNoteAddin::images_dir();
     std::string imagePath = imageDir + host + ".png";
     Glib::RefPtr<Gdk::Pixbuf> image;
     try {
diff --git a/src/addins/bugzilla/bugzillanoteaddin.cpp b/src/addins/bugzilla/bugzillanoteaddin.cpp
index bee1155..60f03d7 100644
--- a/src/addins/bugzilla/bugzillanoteaddin.cpp
+++ b/src/addins/bugzilla/bugzillanoteaddin.cpp
@@ -26,7 +26,10 @@
 #include <glibmm/i18n.h>
 
 
+#include "sharp/directory.hpp"
+
 #include "debug.hpp"
+#include "gnote.hpp"
 #include "notebuffer.hpp"
 #include "notewindow.hpp"
 
@@ -37,6 +40,8 @@
 
 namespace bugzilla {
 
+  DECLARE_MODULE(BugzillaModule);
+
   BugzillaModule::BugzillaModule()
   {
     ADD_INTERFACE_IMPL(BugzillaNoteAddin);
@@ -70,6 +75,31 @@ namespace bugzilla {
 
   const char * BugzillaNoteAddin::TAG_NAME = "link:bugzilla";
 
+  BugzillaNoteAddin::BugzillaNoteAddin()
+    : gnote::NoteAddin()
+  {
+    const bool is_first_run
+                 = !sharp::directory_exists(images_dir());
+    const std::string old_images_dir
+      = Glib::build_filename(gnote::Gnote::old_note_dir(),
+                             "BugzillaIcons");
+    const bool migration_needed
+                 = is_first_run
+                   && sharp::directory_exists(old_images_dir);
+
+    if (is_first_run)
+      sharp::directory_create(images_dir());
+
+    if (migration_needed)
+      migrate_images(old_images_dir);
+  }
+
+  std::string BugzillaNoteAddin::images_dir()
+  {
+    return Glib::build_filename(gnote::Gnote::conf_dir(),
+                                "BugzillaIcons");
+  }
+
   void BugzillaNoteAddin::initialize()
   {
     if(!get_note()->get_tag_table()->is_dynamic_tag_registered(TAG_NAME)) {
@@ -78,6 +108,22 @@ namespace bugzilla {
     }
   }
 
+  void BugzillaNoteAddin::migrate_images(
+                            const std::string & old_images_dir)
+  {
+    const Glib::RefPtr<Gio::File> src
+      = Gio::File::create_for_path(old_images_dir);
+    const Glib::RefPtr<Gio::File> dest
+      = Gio::File::create_for_path(gnote::Gnote::conf_dir());
+
+    try {
+      sharp::directory_copy(src, dest);
+    }
+    catch (const Gio::Error & e) {
+      DBG_OUT("BugzillaNoteAddin: migrating images: %s",
+              e.what().c_str());
+    }
+  }
 
 
   void BugzillaNoteAddin::shutdown()
diff --git a/src/addins/bugzilla/bugzillanoteaddin.hpp b/src/addins/bugzilla/bugzillanoteaddin.hpp
index 6af49b6..226f2c5 100644
--- a/src/addins/bugzilla/bugzillanoteaddin.hpp
+++ b/src/addins/bugzilla/bugzillanoteaddin.hpp
@@ -43,8 +43,6 @@ public:
   virtual const char * version() const;
 };
 
-DECLARE_MODULE(BugzillaModule);
-
 class BugzillaNoteAddin
   : public gnote::NoteAddin
 {
@@ -53,10 +51,14 @@ public:
     {
       return new BugzillaNoteAddin;
     }
+  static std::string images_dir();
   virtual void initialize();
   virtual void shutdown();
   virtual void on_note_opened();
 private:
+  BugzillaNoteAddin();
+  void migrate_images(const std::string & old_images_dir);
+
   static const char * TAG_NAME;
 
   void on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>&, int, int, const Gtk::SelectionData &,
diff --git a/src/addins/bugzilla/bugzillapreferences.cpp b/src/addins/bugzilla/bugzillapreferences.cpp
index 2312f70..b41de17 100644
--- a/src/addins/bugzilla/bugzillapreferences.cpp
+++ b/src/addins/bugzilla/bugzillapreferences.cpp
@@ -33,6 +33,7 @@
 #include "debug.hpp"
 #include "utils.hpp"
 
+#include "bugzillanoteaddin.hpp"
 #include "bugzillapreferences.hpp"
 
 
@@ -44,7 +45,7 @@ namespace bugzilla {
   void BugzillaPreferences::_init_static()
   {
     if(!s_static_inited) {
-      s_image_dir = Glib::get_home_dir() + "/.gnote/BugzillaIcons";
+      s_image_dir = BugzillaNoteAddin::images_dir();
       s_static_inited = true;
     }
   }
diff --git a/src/gnote.cpp b/src/gnote.cpp
index 66e6d3b..529b47c 100644
--- a/src/gnote.cpp
+++ b/src/gnote.cpp
@@ -169,13 +169,7 @@ namespace gnote {
       note_path = override_path;
     }
     if(note_path.empty()) {
-      note_path = Gnote::conf_dir();
-    }
-    std::string home_dir;
-    const char *s = getenv("HOME");
-    if(s) {
-      home_dir = s;
-      note_path = sharp::string_replace_first(note_path, "~", home_dir);
+      note_path = Gnote::data_dir();
     }
 
     return note_path;
@@ -388,15 +382,32 @@ namespace gnote {
   }
 
 
+  std::string Gnote::cache_dir()
+  {
+    return Glib::get_user_cache_dir() + "/gnote";
+  }
+
+
   std::string Gnote::conf_dir()
   {
-    std::string dir;
-    const char * home = getenv("HOME");
-    if(!home) {
-      home = ".";
-    }
-    dir = std::string(home) + "/.gnote";
-    return dir;
+    return Glib::get_user_config_dir() + "/gnote";
+  }
+
+
+  std::string Gnote::data_dir()
+  {
+    return Glib::get_user_data_dir() + "/gnote";
+  }
+
+
+  std::string Gnote::old_note_dir()
+  {
+    std::string home_dir = Glib::get_home_dir();
+
+    if (home_dir.empty())
+      home_dir = Glib::get_current_dir();
+
+    return home_dir + "/.gnote";
   }
 
 
diff --git a/src/gnote.hpp b/src/gnote.hpp
index 5edc902..ba14e72 100644
--- a/src/gnote.hpp
+++ b/src/gnote.hpp
@@ -70,6 +70,11 @@ public:
   void open_search_all();
   void open_note_sync_window();
 
+  static std::string cache_dir();
+  static std::string conf_dir();
+  static std::string data_dir();
+  static std::string old_note_dir();
+
   static bool tray_icon_showing()
     {
       return s_tray_icon_showing;
@@ -84,7 +89,6 @@ public:
     }
 private:
   void start_note_created(const Note::Ptr & start_note);
-  static std::string conf_dir();
   std::string get_note_path(const std::string & override_path);
   void register_remote_control(NoteManager & manager);
 
diff --git a/src/notemanager.cpp b/src/notemanager.cpp
index c1019cc..0608009 100644
--- a/src/notemanager.cpp
+++ b/src/notemanager.cpp
@@ -115,6 +115,16 @@ namespace gnote {
     bool is_first_run = first_run ();
     create_notes_dir ();
 
+    const std::string old_note_dir = Gnote::old_note_dir();
+    const bool migration_needed
+                 = is_first_run
+                   && sharp::directory_exists(old_note_dir);
+
+    if (migration_needed) {
+      migrate_notes(old_note_dir);
+      is_first_run = false;
+    }
+
     m_trie_controller = create_trie_controller ();
     m_addin_mgr = create_addin_manager ();
 
@@ -176,7 +186,7 @@ namespace gnote {
 
   AddinManager *NoteManager::create_addin_manager() const
   {
-    return new AddinManager(m_notes_dir);
+    return new AddinManager(Gnote::conf_dir());
   }
 
   // For overriding in test methods.
@@ -205,6 +215,9 @@ namespace gnote {
       // First run. Create storage directory.
       create_directory(m_notes_dir);
     }
+    if (!directory_exists(m_backup_dir)) {
+      create_directory(m_backup_dir);
+    }
   }
   
 
@@ -359,6 +372,43 @@ namespace gnote {
     }
   }
 
+  void NoteManager::migrate_notes(const std::string & old_note_dir)
+  {
+    std::list<std::string> files;
+    sharp::directory_get_files_with_ext(old_note_dir, ".note", files);
+
+    for (std::list<std::string>::const_iterator iter = files.begin();
+         files.end() != iter; ++iter) {
+      const Glib::RefPtr<Gio::File> src = Gio::File::create_for_path(
+                                                       *iter);
+      const std::string dest_path
+          = Glib::build_filename(m_notes_dir,
+                                 Glib::path_get_basename(*iter));
+      const Glib::RefPtr<Gio::File> dest = Gio::File::create_for_path(
+                                                        dest_path);
+      src->copy(dest, Gio::FILE_COPY_NONE);
+    }
+
+    files.clear();
+    const std::string old_backup_dir = Glib::build_filename(
+                                         old_note_dir,
+                                         "Backup");
+    sharp::directory_get_files_with_ext(old_backup_dir,
+                                        ".note", files);
+
+    for (std::list<std::string>::const_iterator iter = files.begin();
+         files.end() != iter; ++iter) {
+      const Glib::RefPtr<Gio::File> src = Gio::File::create_for_path(
+                                            *iter);
+      const std::string dest_path
+          = Glib::build_filename(m_backup_dir,
+                                 Glib::path_get_basename(*iter));
+      const Glib::RefPtr<Gio::File> dest = Gio::File::create_for_path(
+                                                        dest_path);
+      src->copy(dest, Gio::FILE_COPY_NONE);
+    }
+  }
+
   bool NoteManager::on_exiting_event()
   {
     // Call ApplicationAddin.Shutdown () on all the known ApplicationAddins
diff --git a/src/notemanager.hpp b/src/notemanager.hpp
index bcc1bee..dc8a801 100644
--- a/src/notemanager.hpp
+++ b/src/notemanager.hpp
@@ -108,6 +108,7 @@ namespace gnote {
     void create_start_notes();
     void on_note_save(const Note::Ptr & note);
     void load_notes();
+    void migrate_notes(const std::string & old_note_dir);
     void post_load();
     bool first_run() const;
     void create_notes_dir() const;
diff --git a/src/sharp/directory.cpp b/src/sharp/directory.cpp
index b4f5ceb..8a38f43 100644
--- a/src/sharp/directory.cpp
+++ b/src/sharp/directory.cpp
@@ -67,6 +67,44 @@ namespace sharp {
     return (exists(p) && is_directory(p));
   }
 
+  void directory_copy(const Glib::RefPtr<Gio::File> & src,
+                      const Glib::RefPtr<Gio::File> & dest)
+                      throw(Gio::Error)
+  {
+    if (false == dest->query_exists()
+        || Gio::FILE_TYPE_DIRECTORY
+             != dest->query_file_type(Gio::FILE_QUERY_INFO_NONE))
+        return;
+
+    if (Gio::FILE_TYPE_REGULAR
+          == src->query_file_type(Gio::FILE_QUERY_INFO_NONE)) {
+      src->copy(dest->get_child(src->get_basename()),
+                Gio::FILE_COPY_OVERWRITE);
+    }
+    else if (Gio::FILE_TYPE_DIRECTORY
+                 == src->query_file_type(Gio::FILE_QUERY_INFO_NONE)) {
+      const Glib::RefPtr<Gio::File> dest_dir
+        = dest->get_child(src->get_basename());
+
+      if (false == dest_dir->query_exists())
+        dest_dir->make_directory_with_parents();
+
+      Glib::Dir src_dir(src->get_path());
+
+      for (Glib::Dir::iterator it = src_dir.begin();
+           src_dir.end() != it; it++) {
+        const Glib::RefPtr<Gio::File> file = src->get_child(*it);
+
+        if (Gio::FILE_TYPE_DIRECTORY == file->query_file_type(
+                                          Gio::FILE_QUERY_INFO_NONE))
+          directory_copy(file, dest_dir);
+        else
+          file->copy(dest_dir->get_child(file->get_basename()),
+                                         Gio::FILE_COPY_OVERWRITE);
+      }
+    }
+  }
+
   bool directory_create(const std::string & dir)
   {
     try {
diff --git a/src/sharp/directory.hpp b/src/sharp/directory.hpp
index 220295e..5ab1b3a 100644
--- a/src/sharp/directory.hpp
+++ b/src/sharp/directory.hpp
@@ -31,6 +31,8 @@
 #include <list>
 #include <string>
 
+#include <glibmm.h>
+#include <giomm.h>
 
 namespace sharp {
 
@@ -47,6 +49,15 @@ namespace sharp {
                            std::list<std::string>  & files);
 
   bool directory_exists(const std::string & dir);
+
+  /**
+   * @param src The source directory (or file)
+   * @param dest The destination directory (should exist)
+   */
+  void directory_copy(const Glib::RefPtr<Gio::File> & src,
+                      const Glib::RefPtr<Gio::File> & dest)
+                      throw(Gio::Error);
+
   bool directory_create(const std::string & dir);
 
 }



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