[glabels] Use g_get_user_config_dir() to locate custom templates.



commit a4d7958651c3cd53424b8ca74b314b8bd71a055f
Author: Jim Evins <evins snaught com>
Date:   Sat May 15 23:42:38 2010 -0400

    Use g_get_user_config_dir() to locate custom templates.
    
    Use g_get_user_config_dir() to locate custom template.  By default this
    means that custom templates will be stored at ~/.config/libglabels/templates
    instead of ~/.glabels.
    
    Also automatically migrate user custom templates to the new location when
    run for the first time.

 libglabels/Makefile.am          |    4 +-
 libglabels/db.c                 |   94 ++++++++++++++++++++++++++++++++++-----
 libglabels/libglabels-private.h |    4 --
 3 files changed, 85 insertions(+), 17 deletions(-)
---
diff --git a/libglabels/Makefile.am b/libglabels/Makefile.am
index f6e27aa..4d43f5c 100644
--- a/libglabels/Makefile.am
+++ b/libglabels/Makefile.am
@@ -1,8 +1,8 @@
-templatesdir = $(datadir)/$(LIBGLABELS_BRANCH)/templates
+configdir = $(datadir)/$(LIBGLABELS_BRANCH)
 
 INCLUDES = \
 	$(LIBGLABELS_CFLAGS)				\
-	-DLIBGLABELS_TEMPLATE_DIR=\""$(templatesdir)"\" \
+	-DLIBGLABELS_CONFIG_DIR=\""$(configdir)"\" \
 	$(DISABLE_DEPRECATED_CFLAGS)
 
 libglabels_3_0_la_LDFLAGS=\
diff --git a/libglabels/db.c b/libglabels/db.c
index 7f7b9e3..76fd084 100644
--- a/libglabels/db.c
+++ b/libglabels/db.c
@@ -36,6 +36,15 @@
 #include "xml-vendor.h"
 #include "xml-template.h"
 
+/*===========================================*/
+/* Private macros and constants.             */
+/*===========================================*/
+
+/* Data system and user data directories.  (must free w/ g_free()) */
+#define SYSTEM_CONFIG_DIR     g_build_filename (LIBGLABELS_CONFIG_DIR, "templates", NULL)
+#define USER_CONFIG_DIR       g_build_filename (g_get_user_config_dir (), "libglabels", "templates" , NULL)
+#define USER_CONFIG_DIR_OLD22 g_build_filename (g_get_home_dir (), ".glabels", NULL)
+
 
 /*===========================================*/
 /* Private types                             */
@@ -117,6 +126,8 @@ static void   read_template_files_from_dir (const gchar *dirname);
 
 static lglTemplate *template_full_page     (const gchar *page_size);
 
+static void   copy_old_custom_files        (void);
+
 
 /*****************************************************************************/
 /* Object infrastructure.                                                    */
@@ -243,6 +254,8 @@ lgl_db_init (void)
         GList       *page_sizes;
         GList       *p;
 
+        copy_old_custom_files ();
+
         model = lgl_db_model_new ();
 
         /*
@@ -622,11 +635,11 @@ read_papers (void)
 	gchar *data_dir;
 	GList *papers = NULL;
 
-	data_dir = LGL_SYSTEM_DATA_DIR;
+	data_dir = SYSTEM_CONFIG_DIR;
 	papers = read_paper_files_from_dir (papers, data_dir);
 	g_free (data_dir);
 
-	data_dir = LGL_USER_DATA_DIR;
+	data_dir = USER_CONFIG_DIR;
 	papers = read_paper_files_from_dir (papers, data_dir);
 	g_free (data_dir);
 
@@ -1014,11 +1027,11 @@ read_categories (void)
 	gchar *data_dir;
 	GList *categories = NULL;
 
-	data_dir = LGL_SYSTEM_DATA_DIR;
+	data_dir = SYSTEM_CONFIG_DIR;
 	categories = read_category_files_from_dir (categories, data_dir);
 	g_free (data_dir);
 
-	data_dir = LGL_USER_DATA_DIR;
+	data_dir = USER_CONFIG_DIR;
 	categories = read_category_files_from_dir (categories, data_dir);
 	g_free (data_dir);
 
@@ -1253,11 +1266,11 @@ read_vendors (void)
 	gchar *data_dir;
 	GList *vendors = NULL;
 
-	data_dir = LGL_SYSTEM_DATA_DIR;
+	data_dir = SYSTEM_CONFIG_DIR;
 	vendors = read_vendor_files_from_dir (vendors, data_dir);
 	g_free (data_dir);
 
-	data_dir = LGL_USER_DATA_DIR;
+	data_dir = USER_CONFIG_DIR;
 	vendors = read_vendor_files_from_dir (vendors, data_dir);
 	g_free (data_dir);
 
@@ -1478,8 +1491,8 @@ lgl_db_register_template (const lglTemplate *template)
 
 	if (lgl_db_is_paper_id_known (template->paper_id))
         {
-		dir = LGL_USER_DATA_DIR;
-		mkdir (dir, 0775); /* Try to make sure directory exists. */
+		dir = USER_CONFIG_DIR;
+		g_mkdir_with_parents (dir, 0775); /* Try to make sure directory exists. */
 		filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
 		abs_filename = g_build_filename (dir, filename, NULL);
 		bytes_written = lgl_xml_template_write_template_to_file (template, abs_filename);
@@ -1530,7 +1543,7 @@ lgl_db_delete_template_by_name (const gchar *name)
         template = lgl_db_lookup_template_from_name (name);
         if ( lgl_template_does_category_match (template, "user-defined") )
         {
-		dir = LGL_USER_DATA_DIR;
+		dir = USER_CONFIG_DIR;
 		filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
 		abs_filename = g_build_filename (dir, filename, NULL);
 
@@ -2024,7 +2037,7 @@ read_templates (void)
         /*
          * User defined templates.  Add to user-defined category.
          */
-	data_dir = LGL_USER_DATA_DIR;
+	data_dir = USER_CONFIG_DIR;
 	read_template_files_from_dir (data_dir);
 	g_free (data_dir);
         for ( p=model->templates; p != NULL; p=p->next )
@@ -2036,7 +2049,7 @@ read_templates (void)
         /*
          * System templates.
          */
-	data_dir = LGL_SYSTEM_DATA_DIR;
+	data_dir = SYSTEM_CONFIG_DIR;
 	read_template_files_from_dir (data_dir);
 	g_free (data_dir);
 
@@ -2136,6 +2149,65 @@ template_full_page (const gchar *paper_id)
 }
 
 
+/*
+ * Migrate custom files from the old ~/.glabels location to the new
+ * ~/.config/libglabels/templates directory.  Place a timestamp file
+ * in the old directory to prevent future copies.
+ */
+static void
+copy_old_custom_files (void)
+{
+        gchar       *old_dir_name;
+        gchar       *new_dir_name;
+        gchar       *timestamp_file_name;
+        GDir        *dir;
+        const gchar *name;
+        gchar       *old_full_name, *new_full_name;
+        gchar       *contents;
+        gsize        length;
+
+        old_dir_name = USER_CONFIG_DIR_OLD22;
+        timestamp_file_name = g_build_filename (old_dir_name, ".copied_to_30", NULL);
+
+        if ( g_file_test (old_dir_name, G_FILE_TEST_EXISTS) &&
+             !g_file_test (timestamp_file_name, G_FILE_TEST_EXISTS) )
+        {
+
+                new_dir_name = USER_CONFIG_DIR;
+		g_mkdir_with_parents (new_dir_name, 0775); /* Try to make sure directory exists. */
+
+                dir = g_dir_open (old_dir_name, 0, NULL);
+
+                if (dir)
+                {
+                        while (name = g_dir_read_name (dir))
+                        {
+                                old_full_name = g_build_filename (old_dir_name, name, NULL);
+                                new_full_name = g_build_filename (new_dir_name, name, NULL);
+
+                                if ( g_file_get_contents (old_full_name, &contents, &length, NULL) )
+                                {
+                                        g_file_set_contents (new_full_name, contents, length, NULL);
+                                        g_free (contents);
+                                }
+
+                                g_free (old_full_name);
+                                g_free (new_full_name);
+                        }
+
+                        g_dir_close (dir);
+
+                        g_file_set_contents (timestamp_file_name, NULL, 0, NULL);
+                }
+
+                g_free (new_dir_name);
+        }
+
+        g_free (timestamp_file_name);
+        g_free (old_dir_name);
+}
+
+
 /**
  * lgl_db_print_known_templates:
  *
diff --git a/libglabels/libglabels-private.h b/libglabels/libglabels-private.h
index b5f6c21..726c803 100644
--- a/libglabels/libglabels-private.h
+++ b/libglabels/libglabels-private.h
@@ -26,10 +26,6 @@
 #include "str.h"
 #include "template.h"
 
-/* Data system and user data directories.  (must free w/ g_free()) */
-#define LGL_SYSTEM_DATA_DIR g_build_filename (LIBGLABELS_TEMPLATE_DIR, NULL)
-#define LGL_USER_DATA_DIR   g_build_filename (g_get_home_dir (), ".glabels", NULL)
-
 #undef  G_LOG_DOMAIN
 #define G_LOG_DOMAIN "LibGlabels"
 



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