[gnome-dictionary] Use XDG directory layout for config



commit 63c5af12646a00cedbce0fe35e0f936822593325
Author: Antono Vasiljev <self antono info>
Date:   Wed Jan 4 15:23:08 2012 +0300

    Use XDG directory layout for config
    
      - implemented gdict_get_config_dir
      - implemented gdict_migrate_configs
      - some whitespace cleanup
        (mixed tabs and spaces -> spaces)

 src/gdict-app.c           |    7 +++-
 src/gdict-common.c        |  103 +++++++++++++++++++++++++++++++++++++++++----
 src/gdict-common.h        |    6 ++-
 src/gdict-source-dialog.c |   22 ++++-----
 4 files changed, 115 insertions(+), 23 deletions(-)
---
diff --git a/src/gdict-app.c b/src/gdict-app.c
index c744920..4b44067 100644
--- a/src/gdict-app.c
+++ b/src/gdict-app.c
@@ -224,6 +224,11 @@ gdict_main (int    *argc,
 {
   gchar *loader_path;
 
+  g_set_prgname ("gnome-dictionary");
+
+  if (!gdict_create_config_dir ())
+    exit (1);
+
   if (!gdict_create_data_dir ())
     exit (1);
 
@@ -238,7 +243,7 @@ gdict_main (int    *argc,
 
   /* add user's path for fetching dictionary sources */  
   singleton->loader = gdict_source_loader_new ();
-  loader_path = gdict_get_data_dir (); 
+  loader_path = gdict_get_config_dir ();
   gdict_source_loader_add_search_path (singleton->loader, loader_path);
   g_free (loader_path);
 
diff --git a/src/gdict-common.c b/src/gdict-common.c
index 35ec8d1..a53aeb8 100644
--- a/src/gdict-common.c
+++ b/src/gdict-common.c
@@ -44,6 +44,18 @@ gdict_get_data_dir (void)
 {
   gchar *retval;
 
+  retval = g_build_filename (g_get_user_data_dir (),
+                             g_get_prgname (),
+                             NULL);
+  return retval;
+}
+
+/* legacy data dir. pre 3.3.4 */
+gchar *
+gdict_get_old_data_dir (void)
+{
+  gchar *retval;
+
   retval = g_build_filename (g_get_home_dir (),
 		  	     ".gnome2",
 			     "gnome-dictionary",
@@ -52,6 +64,79 @@ gdict_get_data_dir (void)
   return retval;
 }
 
+gchar *
+gdict_get_config_dir (void)
+{
+  gchar *retval;
+
+  retval = g_build_filename (g_get_user_config_dir (),
+                             g_get_prgname (),
+                             NULL);
+  return retval;
+}
+
+gboolean
+gdict_migrate_configs (void)
+{
+  gchar *old_data_dir_name; // this one was used for configs only
+  gchar *config_dir_name;
+  gboolean res = TRUE;
+
+  old_data_dir_name = gdict_get_old_data_dir ();
+  config_dir_name = gdict_get_config_dir ();
+
+  /* move configs from pre-XDG directory to right place */
+  if (g_file_test (old_data_dir_name, G_FILE_TEST_IS_DIR))
+    {
+      g_message ("Migrating old configs to XDG directory layout...");
+
+      if (g_rename (old_data_dir_name, config_dir_name) == -1)
+        {
+          g_critical ("Unable to rename file '%s' to '%s': %s",
+                      old_data_dir_name,
+                      config_dir_name,
+                      g_strerror (errno));
+
+          res = FALSE;
+        }
+    }
+
+  g_free (config_dir_name);
+  g_free (old_data_dir_name);
+
+  return res;
+}
+
+gboolean
+gdict_create_config_dir (void)
+{
+  gchar *config_dir_name;
+  gboolean res = TRUE;
+
+  config_dir_name = gdict_get_config_dir ();
+
+  gdict_migrate_configs ();
+
+  if (!g_file_test (config_dir_name, G_FILE_TEST_IS_DIR))
+    {
+      if (!g_file_test (config_dir_name, G_FILE_TEST_IS_DIR)) {
+        g_message ("Creating XDG config direcotry: %s", config_dir_name);
+
+        if (g_mkdir (config_dir_name, 0700) == -1)
+          {
+            g_critical ("Unable to create directory '%s': %s",
+                        config_dir_name,
+                        g_strerror (errno));
+
+            res = FALSE;
+          }
+      }
+    }
+
+  g_free (config_dir_name);
+  return res;
+}
+
 /* create the data directory inside $HOME, if it doesn't exist yet */
 gboolean
 gdict_create_data_dir (void)
@@ -59,6 +144,7 @@ gdict_create_data_dir (void)
   gchar *data_dir_name;
   
   data_dir_name = gdict_get_data_dir ();
+
   if (g_mkdir (data_dir_name, 0700) == -1)
     {
       /* this is weird, but sometimes there's a "gnome-dictionary" file
@@ -77,9 +163,8 @@ gdict_create_data_dir (void)
                           g_strerror (errno));
 
 	      g_free (backup);
-	      g_free (data_dir_name);
 
-	      return FALSE;
+              goto error;
             }
 
 	  g_free (backup);
@@ -90,9 +175,7 @@ gdict_create_data_dir (void)
                           data_dir_name,
                           g_strerror (errno));
 
-              g_free (data_dir_name);
-
-	      return FALSE;
+              goto error;
             }
 
 	  goto success;
@@ -103,10 +186,7 @@ gdict_create_data_dir (void)
           g_critical ("Unable to create the data directory '%s': %s",
                       data_dir_name,
                       g_strerror (errno));
-
-	  g_free (data_dir_name);
-
-	  return FALSE;
+          goto error;
 	}
     }
 
@@ -114,6 +194,11 @@ success:
   g_free (data_dir_name);
 
   return TRUE;
+
+error:
+  g_free (data_dir_name);
+
+  return FALSE;
 }
 
 /* shows an error dialog making it transient for @parent */
diff --git a/src/gdict-common.h b/src/gdict-common.h
index b3fa9da..69d0163 100644
--- a/src/gdict-common.h
+++ b/src/gdict-common.h
@@ -27,8 +27,12 @@
 
 G_BEGIN_DECLS
 
-gboolean gdict_create_data_dir (void);
 gchar *  gdict_get_data_dir    (void) G_GNUC_MALLOC;
+gchar *  gdict_get_old_data_dir   (void) G_GNUC_MALLOC;
+gchar *  gdict_get_config_dir     (void) G_GNUC_MALLOC;
+
+gboolean gdict_create_data_dir    (void);
+gboolean gdict_create_config_dir  (void);
 
 void     gdict_show_error_dialog  (GtkWindow   *parent,
 				   const gchar *message,
diff --git a/src/gdict-source-dialog.c b/src/gdict-source-dialog.c
index 6d070d8..7bc7152 100644
--- a/src/gdict-source-dialog.c
+++ b/src/gdict-source-dialog.c
@@ -285,6 +285,7 @@ build_new_source (GdictSourceDialog *dialog)
   gsize length;
   GError *error;
   gchar *filename;
+  gchar *config_dir;
   GdictDatabaseChooser *db_chooser;
   GdictStrategyChooser *strat_chooser;
   
@@ -345,12 +346,10 @@ build_new_source (GdictSourceDialog *dialog)
       return;
     }
       
-  name = g_strdup_printf ("%s.desktop", gdict_source_get_name (source));
-  filename = g_build_filename (g_get_home_dir (),
-  			       ".gnome2",
-      			       "gnome-dictionary",
-      			       name,
-      			       NULL);
+  config_dir = gdict_get_config_dir();
+  name = g_strconcat (gdict_source_get_name (source), ".desktop", NULL);
+  filename = g_build_filename (config_dir, name, NULL);
+  g_free (config_dir);
   g_free (name);
       
   g_file_set_contents (filename, data, length, &error);
@@ -377,6 +376,7 @@ save_source (GdictSourceDialog *dialog)
   gsize length;
   GError *error;
   gchar *filename;
+  gchar *config_dir;
   
   source = gdict_source_loader_get_source (dialog->loader,
 		  			   dialog->source_name);
@@ -438,12 +438,10 @@ save_source (GdictSourceDialog *dialog)
       return;
     }
       
-  name = g_strdup_printf ("%s.desktop", gdict_source_get_name (source));
-  filename = g_build_filename (g_get_home_dir (),
-      			       ".gnome2",
-			       "gnome-dictionary",
-			       name,
-			       NULL);
+  config_dir = gdict_get_config_dir();
+  name = g_strconcat (gdict_source_get_name (source), ".desktop", NULL);
+  filename = g_build_filename (config_dir, name, NULL);
+  g_free (config_dir);
   g_free (name);
       
   g_file_set_contents (filename, data, length, &error);



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