[glabels] Search both ~/.glabels and ${XDG_CONFIG_HOME}/libglabels/templates.



commit 350bb1773bd34fb92c111e231d774ea173615594
Author: Jim Evins <evins snaught com>
Date:   Sat Dec 4 23:09:19 2010 -0500

    Search both ~/.glabels and ${XDG_CONFIG_HOME}/libglabels/templates.
    
    Keep ~/.glabels as an alternate location for user defined templates.  This
    directory would hold manually created templates as well as template designer
    templates from previous versions.  The new template designer will manage
    its templates in ${XDG_CONFIG_HOME}/libglabels/templates.

 libglabels/db.c           |   78 ++++++++-------------------------------------
 libglabels/template.c     |    7 ++--
 libglabels/xml-template.c |   29 +++++++++++-----
 3 files changed, 38 insertions(+), 76 deletions(-)
---
diff --git a/libglabels/db.c b/libglabels/db.c
index 3c309d1..3ef6d23 100644
--- a/libglabels/db.c
+++ b/libglabels/db.c
@@ -43,7 +43,7 @@
 /* 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)
+#define ALT_USER_CONFIG_DIR   g_build_filename (g_get_home_dir (), ".glabels", NULL)
 
 
 /*===========================================*/
@@ -126,8 +126,6 @@ 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.                                                    */
@@ -254,8 +252,6 @@ lgl_db_init (void)
         GList       *page_sizes;
         GList       *p;
 
-        copy_old_custom_files ();
-
         model = lgl_db_model_new ();
 
         /*
@@ -1569,6 +1565,12 @@ lgl_db_delete_template_by_name (const gchar *name)
 		filename = g_strdup_printf ("%s_%s.template", template->brand, template->part);
 		abs_filename = g_build_filename (dir, filename, NULL);
 
+                if (!g_file_test (abs_filename, G_FILE_TEST_EXISTS))
+                {
+                        g_message ("File \"%s\" does not exist.  Cannot delete it.");
+                        return LGL_DB_DELETE_DOES_NOT_EXIST;
+                }
+
                 g_unlink (abs_filename);
 
 		g_free (dir);
@@ -1948,6 +1950,13 @@ read_templates (void)
         }
 
         /*
+         * Alternate user defined templates.  (Used for manually created templates).
+         */
+	data_dir = ALT_USER_CONFIG_DIR;
+	read_template_files_from_dir (data_dir);
+	g_free (data_dir);
+
+        /*
          * System templates.
          */
 	data_dir = SYSTEM_CONFIG_DIR;
@@ -2050,65 +2059,6 @@ 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)) != NULL)
-                        {
-                                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/template.c b/libglabels/template.c
index 716db60..5a1cf81 100644
--- a/libglabels/template.c
+++ b/libglabels/template.c
@@ -121,11 +121,12 @@ lgl_template_new_from_equiv (const gchar          *brand,
                              const gchar          *part,
                              const gchar          *equiv_part)
 {
-        lglTemplate      *template;
+        lglTemplate      *template = NULL;
 
-        template = lgl_db_lookup_template_from_brand_part (brand, equiv_part);
-        if (template)
+        if ( lgl_db_does_template_exist (brand, equiv_part) )
         {
+                template = lgl_db_lookup_template_from_brand_part (brand, equiv_part);
+
                 g_free (template->part);
                 g_free (template->equiv_part);
 
diff --git a/libglabels/xml-template.c b/libglabels/xml-template.c
index 333ee09..5238138 100644
--- a/libglabels/xml-template.c
+++ b/libglabels/xml-template.c
@@ -147,26 +147,37 @@ lgl_xml_template_parse_templates_doc (const xmlDocPtr templates_doc)
 	LIBXML_TEST_VERSION;
 
 	root = xmlDocGetRootElement (templates_doc);
-	if (!root || !root->name) {
+	if (!root || !root->name)
+        {
 		g_message ("\"%s\" is not a glabels template file (no root node)",
 			   templates_doc->URL);
 		return;
 	}
-	if (!lgl_xml_is_node (root, "Glabels-templates")) {
+	if (!lgl_xml_is_node (root, "Glabels-templates"))
+        {
 		g_message ("\"%s\" is not a glabels template file (wrong root node)",
 		      templates_doc->URL);
 		return;
 	}
 
-	for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
+	for (node = root->xmlChildrenNode; node != NULL; node = node->next)
+        {
 
-		if (lgl_xml_is_node (node, "Template")) {
+		if (lgl_xml_is_node (node, "Template"))
+                {
 			template = lgl_xml_template_parse_template_node (node);
-                        _lgl_db_register_template_internal (template);
-                        lgl_template_free (template);
-		} else {
-			if ( !xmlNodeIsText(node) ) {
-				if (!lgl_xml_is_node (node,"comment")) {
+                        if (template)
+                        {
+                                _lgl_db_register_template_internal (template);
+                                lgl_template_free (template);
+                        }
+		}
+                else
+                {
+			if ( !xmlNodeIsText(node) )
+                        {
+				if (!lgl_xml_is_node (node,"comment"))
+                                {
 					g_message ("bad node =  \"%s\"",node->name);
 				}
 			}



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