]> git.sur5r.net Git - glabels/commitdiff
Search both ~/.glabels and ${XDG_CONFIG_HOME}/libglabels/templates.
authorJim Evins <evins@snaught.com>
Sun, 5 Dec 2010 04:09:19 +0000 (23:09 -0500)
committerJim Evins <evins@snaught.com>
Sun, 5 Dec 2010 04:09:19 +0000 (23:09 -0500)
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
libglabels/template.c
libglabels/xml-template.c

index 3c309d1c5bfa04345ff27130b9d0c797140c2db3..3ef6d232cbe3cab562c53428bb36721646eebd80 100644 (file)
@@ -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);
@@ -1947,6 +1949,13 @@ read_templates (void)
                 lgl_template_add_category (template, "user-defined");
         }
 
+        /*
+         * 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.
          */
@@ -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:
  *
index 716db60e96ed47b4ed49a96670a598c921ea954d..5a1cf815e5a3d71d9f5d79e6aec2122605372201 100644 (file)
@@ -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);
 
index 333ee09261fe0b98e48d6ef187c9cbc82d4f5203..5238138332e334df40fdb2d57a40a624a1b30e02 100644 (file)
@@ -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);
                                }
                        }