#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 */
static lglTemplate *template_full_page (const gchar *page_size);
+static void copy_old_custom_files (void);
+
/*****************************************************************************/
/* Object infrastructure. */
GList *page_sizes;
GList *p;
+ copy_old_custom_files ();
+
model = lgl_db_model_new ();
/*
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);
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);
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);
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);
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);
/*
* 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 )
/*
* System templates.
*/
- data_dir = LGL_SYSTEM_DATA_DIR;
+ data_dir = SYSTEM_CONFIG_DIR;
read_template_files_from_dir (data_dir);
g_free (data_dir);
}
+/*
+ * 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:
*