[gnome-latex] latexila -> gnome-latex migration: migrate personal templates



commit 9538491ed07e09d69e1984bf3c3ca85892c9156a
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Mar 25 15:41:39 2018 +0200

    latexila -> gnome-latex migration: migrate personal templates
    
    The group name for the RC file was "LaTeXila", so it needs to be
    changed. But change it to a name independent of the application.

 src/liblatexila/latexila-templates-personal.c |  45 +++-----
 src/liblatexila/latexila-utils.c              | 160 ++++++++++++++++++++++++++
 2 files changed, 179 insertions(+), 26 deletions(-)
---
diff --git a/src/liblatexila/latexila-templates-personal.c b/src/liblatexila/latexila-templates-personal.c
index 6d77e27..6f61dd4 100644
--- a/src/liblatexila/latexila-templates-personal.c
+++ b/src/liblatexila/latexila-templates-personal.c
@@ -26,9 +26,8 @@
  * #LatexilaTemplatesPersonal is a singleton class that stores information about
  * pesonal templates.
  *
- * Personal templates are stored in the ~/.local/share/latexila/ directory.
- * There is a templatesrc file that stores the list of names, icons and
- * files.
+ * Personal templates are stored in the `~/.local/share/gnome-latex/` directory.
+ * There is a templatesrc file that stores the list of names, icons and files.
  */
 
 #include "config.h"
@@ -43,6 +42,8 @@ struct _LatexilaTemplatesPersonal
        GtkListStore parent;
 };
 
+#define RC_FILE_GROUP_NAME "Personal templates"
+
 G_DEFINE_TYPE (LatexilaTemplatesPersonal, latexila_templates_personal, GTK_TYPE_LIST_STORE)
 
 static void
@@ -53,27 +54,19 @@ latexila_templates_personal_class_init (LatexilaTemplatesPersonalClass *klass)
 static GFile *
 get_rc_file (void)
 {
-       gchar *path;
-       GFile *rc_file;
-
-       path = g_build_filename (g_get_user_data_dir (), "latexila", "templatesrc", NULL);
-       rc_file = g_file_new_for_path (path);
-
-       g_free (path);
-       return rc_file;
+       return g_file_new_build_filename (g_get_user_data_dir (),
+                                         "gnome-latex",
+                                         "templatesrc",
+                                         NULL);
 }
 
 static GFile *
 get_personal_template_file_by_filename (const gchar *filename)
 {
-       gchar *path;
-       GFile *template_file;
-
-       path = g_build_filename (g_get_user_data_dir (), "latexila", filename, NULL);
-       template_file = g_file_new_for_path (path);
-
-       g_free (path);
-       return template_file;
+       return g_file_new_build_filename (g_get_user_data_dir (),
+                                         "gnome-latex",
+                                         filename,
+                                         NULL);
 }
 
 static GFile *
@@ -132,14 +125,14 @@ load_rc_file (LatexilaTemplatesPersonal *templates)
                goto out;
        }
 
-       names = g_key_file_get_string_list (key_file, PACKAGE_NAME, "names", &n_names, &error);
+       names = g_key_file_get_string_list (key_file, RC_FILE_GROUP_NAME, "names", &n_names, &error);
 
        if (error != NULL)
        {
                goto out;
        }
 
-       icons = g_key_file_get_string_list (key_file, PACKAGE_NAME, "icons", &n_icons, &error);
+       icons = g_key_file_get_string_list (key_file, RC_FILE_GROUP_NAME, "icons", &n_icons, &error);
 
        if (error != NULL)
        {
@@ -148,7 +141,7 @@ load_rc_file (LatexilaTemplatesPersonal *templates)
 
        g_return_if_fail (n_names == n_icons);
 
-       has_files = g_key_file_has_key (key_file, PACKAGE_NAME, "files", &error);
+       has_files = g_key_file_has_key (key_file, RC_FILE_GROUP_NAME, "files", &error);
 
        if (error != NULL)
        {
@@ -157,7 +150,7 @@ load_rc_file (LatexilaTemplatesPersonal *templates)
 
        if (has_files)
        {
-               files = g_key_file_get_string_list (key_file, PACKAGE_NAME, "files", &n_files, &error);
+               files = g_key_file_get_string_list (key_file, RC_FILE_GROUP_NAME, "files", &n_files, &error);
 
                if (error != NULL)
                {
@@ -355,19 +348,19 @@ save_rc_file (LatexilaTemplatesPersonal  *templates,
        key_file = g_key_file_new ();
 
        g_key_file_set_string_list (key_file,
-                                   PACKAGE_NAME,
+                                   RC_FILE_GROUP_NAME,
                                    "names",
                                    (const gchar * const *) names,
                                    personal_templates_count);
 
        g_key_file_set_string_list (key_file,
-                                   PACKAGE_NAME,
+                                   RC_FILE_GROUP_NAME,
                                    "icons",
                                    (const gchar * const *) icons,
                                    personal_templates_count);
 
        g_key_file_set_string_list (key_file,
-                                   PACKAGE_NAME,
+                                   RC_FILE_GROUP_NAME,
                                    "files",
                                    (const gchar * const *) files,
                                    personal_templates_count);
diff --git a/src/liblatexila/latexila-utils.c b/src/liblatexila/latexila-utils.c
index 06edd55..4f980ea 100644
--- a/src/liblatexila/latexila-utils.c
+++ b/src/liblatexila/latexila-utils.c
@@ -663,6 +663,165 @@ migrate_latexila_to_gnome_latex_personal_build_tools (void)
        g_object_unref (glatex_file);
 }
 
+static void
+migrate_latexila_to_gnome_latex_personal_templates_tex_files (void)
+{
+       GFile *latexila_dir;
+       GFile *glatex_dir;
+       GFileEnumerator *enumerator;
+       GError *error = NULL;
+
+       latexila_dir = g_file_new_build_filename (g_get_user_data_dir (),
+                                                 "latexila",
+                                                 NULL);
+       glatex_dir = g_file_new_build_filename (g_get_user_data_dir (),
+                                               "gnome-latex",
+                                               NULL);
+
+       enumerator = g_file_enumerate_children (latexila_dir,
+                                               G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
+                                               G_FILE_QUERY_INFO_NONE,
+                                               NULL,
+                                               &error);
+
+       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+       {
+               g_clear_error (&error);
+               goto out;
+       }
+
+       if (enumerator == NULL || error != NULL)
+       {
+               goto out;
+       }
+
+       while (TRUE)
+       {
+               GFileInfo *child_file_info;
+               GFile *child_file;
+               const gchar *child_name;
+               GFile *glatex_child_file;
+
+               g_file_enumerator_iterate (enumerator, &child_file_info, &child_file, NULL, &error);
+               if (child_file == NULL || error != NULL)
+               {
+                       break;
+               }
+
+               child_name = g_file_info_get_display_name (child_file_info);
+               if (child_name == NULL || !g_str_has_suffix (child_name, ".tex"))
+               {
+                       continue;
+               }
+
+               glatex_child_file = g_file_get_child (glatex_dir, child_name);
+               migrate_latexila_to_gnome_latex_copy_file (child_file, glatex_child_file);
+               g_object_unref (glatex_child_file);
+       }
+
+out:
+       if (error != NULL)
+       {
+               g_warning ("Error when migrating LaTeXila to GNOME LaTeX personal templates: %s",
+                          error->message);
+               g_clear_error (&error);
+       }
+
+       g_object_unref (latexila_dir);
+       g_object_unref (glatex_dir);
+       g_clear_object (&enumerator);
+}
+
+#define TEMPLATES_RC_FILE_OLD_GROUP_NAME "[LaTeXila]\n"
+#define TEMPLATES_RC_FILE_NEW_GROUP_NAME "[Personal templates]\n"
+
+static void
+migrate_latexila_to_gnome_latex_personal_templates_rc_file (void)
+{
+       GFile *latexila_file;
+       gchar *content = NULL;
+       GFile *glatex_file = NULL;
+       GFileOutputStream *output_stream = NULL;
+       GError *error = NULL;
+
+       /* Load old RC file. */
+       latexila_file = g_file_new_build_filename (g_get_user_data_dir (),
+                                                  "latexila",
+                                                  "templatesrc",
+                                                  NULL);
+
+       g_file_load_contents (latexila_file, NULL, &content, NULL, NULL, &error);
+
+       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+       {
+               g_clear_error (&error);
+               goto out;
+       }
+
+       if (error != NULL || content == NULL)
+       {
+               goto out;
+       }
+
+       /* Modify group name. */
+       if (g_str_has_prefix (content, TEMPLATES_RC_FILE_OLD_GROUP_NAME))
+       {
+               gchar *modified_content;
+
+               modified_content = g_strconcat (TEMPLATES_RC_FILE_NEW_GROUP_NAME,
+                                               content + strlen (TEMPLATES_RC_FILE_OLD_GROUP_NAME),
+                                               NULL);
+               g_free (content);
+               content = modified_content;
+       }
+
+       /* Save to new location. */
+       glatex_file = g_file_new_build_filename (g_get_user_data_dir (),
+                                                "gnome-latex",
+                                                "templatesrc",
+                                                NULL);
+
+       output_stream = g_file_create (glatex_file, G_FILE_CREATE_NONE, NULL, &error);
+
+       if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+       {
+               g_clear_error (&error);
+               goto out;
+       }
+
+       if (error != NULL || output_stream == NULL)
+       {
+               goto out;
+       }
+
+       g_output_stream_write_all (G_OUTPUT_STREAM (output_stream),
+                                  content,
+                                  strlen (content),
+                                  NULL,
+                                  NULL,
+                                  &error);
+
+out:
+       if (error != NULL)
+       {
+               g_warning ("Error when migrating LaTeXila to GNOME LaTeX personal templates: %s",
+                          error->message);
+               g_clear_error (&error);
+       }
+
+       g_object_unref (latexila_file);
+       g_free (content);
+       g_clear_object (&glatex_file);
+       g_clear_object (&output_stream);
+}
+
+static void
+migrate_latexila_to_gnome_latex_personal_templates (void)
+{
+       migrate_latexila_to_gnome_latex_personal_templates_tex_files ();
+       migrate_latexila_to_gnome_latex_personal_templates_rc_file ();
+}
+
 /**
  * latexila_utils_migrate_latexila_to_gnome_latex:
  *
@@ -682,6 +841,7 @@ latexila_utils_migrate_latexila_to_gnome_latex (void)
                migrate_latexila_to_gnome_latex_most_used_symbols ();
                migrate_latexila_to_gnome_latex_projects ();
                migrate_latexila_to_gnome_latex_personal_build_tools ();
+               migrate_latexila_to_gnome_latex_personal_templates ();
 
                g_settings_set_boolean (settings, "latexila-to-gnome-latex-migration-done", TRUE);
        }


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