[latexila/wip/templates-revamp: 5/5] TemplatesDefault and TemplatesPersonal (wip)



commit fda25acc41ad19be1f6decf3bbb93ab3448b57d7
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Apr 18 19:02:10 2015 +0200

    TemplatesDefault and TemplatesPersonal (wip)

 po/POTFILES.in                                |    4 +-
 src/liblatexila/Makefile.am                   |    8 +-
 src/liblatexila/latexila-templates-common.c   |  121 ++++++++
 src/liblatexila/latexila-templates-common.h   |   58 ++++
 src/liblatexila/latexila-templates-default.c  |  222 +++++++++++++++
 src/liblatexila/latexila-templates-default.h  |   38 +++
 src/liblatexila/latexila-templates-personal.c |   37 +++
 src/liblatexila/latexila-templates-personal.h |   32 +++
 src/liblatexila/latexila-templates.c          |  364 -------------------------
 9 files changed, 517 insertions(+), 367 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index bb6c33a..5dfe577 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -36,8 +36,10 @@ src/liblatexila/latexila-post-processor.c
 src/liblatexila/latexila-post-processor-latex.c
 src/liblatexila/latexila-post-processor-latexmk.c
 src/liblatexila/latexila-synctex.c
+src/liblatexila/latexila-templates-common.c
+src/liblatexila/latexila-templates-default.c
 src/liblatexila/latexila-templates-dialogs.c
-src/liblatexila/latexila-templates.c
+src/liblatexila/latexila-templates-personal.c
 src/liblatexila/latexila-utils.c
 src/main.vala
 src/main_window_build_tools.vala
diff --git a/src/liblatexila/Makefile.am b/src/liblatexila/Makefile.am
index aef1085..7a85fa4 100644
--- a/src/liblatexila/Makefile.am
+++ b/src/liblatexila/Makefile.am
@@ -23,8 +23,10 @@ liblatexila_headers =                                \
        latexila-post-processor-latex.h         \
        latexila-post-processor-latexmk.h       \
        latexila-synctex.h                      \
-       latexila-templates.h                    \
+       latexila-templates-common.h             \
+       latexila-templates-default.h            \
        latexila-templates-dialogs.h            \
+       latexila-templates-personal.h           \
        latexila-types.h                        \
        latexila-utils.h
 
@@ -40,8 +42,10 @@ liblatexila_sources =                                \
        latexila-post-processor-latex.c         \
        latexila-post-processor-latexmk.c       \
        latexila-synctex.c                      \
-       latexila-templates.c                    \
+       latexila-templates-common.c             \
+       latexila-templates-default.c            \
        latexila-templates-dialogs.c            \
+       latexila-templates-personal.c           \
        latexila-utils.c
 
 liblatexila_built_sources =    \
diff --git a/src/liblatexila/latexila-templates-common.c b/src/liblatexila/latexila-templates-common.c
new file mode 100644
index 0000000..99bb887
--- /dev/null
+++ b/src/liblatexila/latexila-templates-common.c
@@ -0,0 +1,121 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2015 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Common functions between default and personal templates. */
+
+#include "latexila-templates-common.h"
+
+void
+latexila_templates_init_store (GtkListStore *store)
+{
+  GType types[] = {G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_FILE};
+
+  gtk_list_store_set_column_types (store,
+                                   LATEXILA_TEMPLATES_N_COLUMNS,
+                                   types);
+}
+
+/* For compatibility reasons. @config_icon_name is the string stored in the rc
+ * file, and the return value is the theme icon name used for the pixbuf. If we
+ * store directly the theme icon names in the rc file, old rc files must be
+ * modified via a script for example, but it's simpler like that.
+ * The config_icon_name can also be seen as the template _type_.
+ */
+static const gchar *
+get_pixbuf_icon_name (const gchar *config_icon_name)
+{
+  g_return_val_if_fail (config_icon_name != NULL, NULL);
+
+  if (g_str_equal (config_icon_name, "empty"))
+    return "text-x-preview";
+
+  if (g_str_equal (config_icon_name, "article"))
+    return "text-x-generic";
+
+  if (g_str_equal (config_icon_name, "report"))
+    return "x-office-document";
+
+  if (g_str_equal (config_icon_name, "book"))
+    return "accessories-dictionary";
+
+  if (g_str_equal (config_icon_name, "letter"))
+    return "emblem-mail";
+
+  if (g_str_equal (config_icon_name, "beamer"))
+    return "x-office-presentation";
+
+  g_return_val_if_reached (NULL);
+}
+
+void
+latexila_templates_add_template (GtkListStore *store,
+                                 const gchar  *name,
+                                 const gchar  *config_icon_name,
+                                 GFile        *file)
+{
+  GtkTreeIter iter;
+
+  gtk_list_store_append (store, &iter);
+  gtk_list_store_set (store, &iter,
+                      COLUMN_PIXBUF_ICON_NAME, get_pixbuf_icon_name (config_icon_name),
+                      COLUMN_CONFIG_ICON_NAME, config_icon_name,
+                      COLUMN_NAME, name,
+                      COLUMN_FILE, file,
+                      -1);
+}
+
+GtkTreeView *
+latexila_templates_get_view (GtkListStore *store)
+{
+  GtkTreeView *view;
+  GtkTreeSelection *selection;
+  GtkCellRenderer *renderer;
+  GtkTreeViewColumn *column;
+
+  view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)));
+  gtk_tree_view_set_headers_visible (view, FALSE);
+  gtk_widget_set_hexpand (GTK_WIDGET (view), TRUE);
+  gtk_widget_set_vexpand (GTK_WIDGET (view), TRUE);
+
+  selection = gtk_tree_view_get_selection (view);
+  gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
+
+  /* Icon */
+  renderer = gtk_cell_renderer_pixbuf_new ();
+  g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
+
+  column = gtk_tree_view_column_new_with_attributes (NULL,
+                                                     renderer,
+                                                     "icon-name", COLUMN_PIXBUF_ICON_NAME,
+                                                     NULL);
+
+  gtk_tree_view_append_column (view, column);
+
+  /* Name */
+  renderer = gtk_cell_renderer_text_new ();
+
+  column = gtk_tree_view_column_new_with_attributes (NULL,
+                                                     renderer,
+                                                     "text", COLUMN_NAME,
+                                                     NULL);
+
+  gtk_tree_view_append_column (view, column);
+
+  return view;
+}
diff --git a/src/liblatexila/latexila-templates-common.h b/src/liblatexila/latexila-templates-common.h
new file mode 100644
index 0000000..e046cf9
--- /dev/null
+++ b/src/liblatexila/latexila-templates-common.h
@@ -0,0 +1,58 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2015 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LATEXILA_TEMPLATES_COMMON_H__
+#define __LATEXILA_TEMPLATES_COMMON_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+enum _LatexilaTemplatesColumn
+{
+  LATEXILA_TEMPLATES_COLUMN_PIXBUF_ICON_NAME,
+
+  /* The string stored in the rc file (article, report, ...). For
+   * backward-compatibility reasons, this is not the same as PIXBUF_ICON_NAME.
+   */
+  LATEXILA_TEMPLATES_COLUMN_CONFIG_ICON_NAME,
+
+  LATEXILA_TEMPLATES_COLUMN_NAME,
+
+  /* The file where is stored the contents. For a default template this is an
+   * XML file, for a personal template this is a .tex file. A NULL file is
+   * valid for a default template, it means an empty template.
+   */
+  LATEXILA_TEMPLATES_COLUMN_FILE,
+
+  LATEXILA_TEMPLATES_N_COLUMNS
+};
+
+void            latexila_templates_init_store       (GtkListStore *store);
+
+void            latexila_templates_add_template     (GtkListStore *store,
+                                                     const gchar  *name,
+                                                     const gchar  *config_icon_name,
+                                                     GFile        *file);
+
+GtkTreeView *   latexila_templates_get_view         (GtkListStore *store);
+
+G_END_DECLS
+
+#endif /* __LATEXILA_TEMPLATES_COMMON_H__ */
diff --git a/src/liblatexila/latexila-templates-default.c b/src/liblatexila/latexila-templates-default.c
new file mode 100644
index 0000000..ecf184c
--- /dev/null
+++ b/src/liblatexila/latexila-templates-default.c
@@ -0,0 +1,222 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2015 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include "latexila-templates-default.h"
+#include <glib/gi18n.h>
+#include "latexila-templates-common.h"
+
+struct _LatexilaTemplatesDefault
+{
+  GtkListStore parent;
+};
+
+G_DEFINE_TYPE (LatexilaTemplatesDefault, latexila_templates_default, GTK_TYPE_LIST_STORE)
+
+static void
+latexila_templates_default_class_init (LatexilaTemplatesDefaultClass *klass)
+{
+}
+
+static void
+add_default_template (LatexilaTemplatesDefault *templates,
+                      const gchar              *name,
+                      const gchar              *config_icon_name,
+                      const gchar              *filename)
+{
+  gchar *path;
+  GFile *file;
+
+  path = g_build_filename (DATA_DIR, "templates", filename, NULL);
+  file = g_file_new_for_path (path);
+
+  latexila_templates_add_template (GTK_LIST_STORE (templates),
+                                   name,
+                                   config_icon_name,
+                                   file);
+
+  g_free (path);
+  g_object_unref (file);
+}
+
+static void
+latexila_templates_default_init (LatexilaTemplatesDefault *templates)
+{
+  latexila_templates_init_store (GTK_LIST_STORE (templates));
+
+  latexila_templates_add_template (GTK_LIST_STORE (templates),
+                                   _("Empty"),
+                                   "empty",
+                                   NULL);
+
+  add_default_template (templates, _("Article"), "article", "article.xml");
+  add_default_template (templates, _("Report"), "report", "report.xml");
+  add_default_template (templates, _("Book"), "book", "book.xml");
+  add_default_template (templates, _("Letter"), "letter", "letter.xml");
+  add_default_template (templates, _("Presentation"), "beamer", "beamer.xml");
+}
+
+/**
+ * latexila_templates_default_get_instance:
+ *
+ * Gets the instance of the #LatexilaTemplatesDefault singleton.
+ *
+ * Returns: (transfer none): the instance of #LatexilaTemplatesDefault.
+ */
+LatexilaTemplatesDefault *
+latexila_templates_default_get_instance (void)
+{
+  static LatexilaTemplatesDefault *instance = NULL;
+
+  if (instance == NULL)
+    instance = g_object_new (LATEXILA_TYPE_TEMPLATES_DEFAULT, NULL);
+
+  return instance;
+}
+
+static void
+parser_add_chunk (GString     *string,
+                  const gchar *chunk,
+                  gint         chunk_len)
+{
+  if (chunk == NULL)
+    return;
+
+  /* Remove the first '\n'. Without this, the XML files would be less well
+   * presented.
+   */
+  if (chunk[0] == '\n')
+    {
+      chunk = chunk + 1;
+
+      if (chunk_len != -1)
+        chunk_len--;
+    }
+
+  if (chunk_len != -1)
+    g_string_append_len (string, chunk, chunk_len);
+  else
+    g_string_append (string, chunk);
+}
+
+static void
+parser_text (GMarkupParseContext  *context,
+             const gchar          *text,
+             gsize                 text_len,
+             gpointer              user_data,
+             GError              **error)
+{
+  GString *template_contents = user_data;
+  const gchar *element;
+  gchar *text_nul_terminated = NULL;
+
+  element = g_markup_parse_context_get_element (context);
+
+  if (g_strcmp0 (element, "chunk") == 0)
+    {
+      parser_add_chunk (template_contents, text, text_len);
+    }
+
+  else if (g_strcmp0 (element, "translatableChunk") == 0)
+    {
+      const gchar *chunk;
+
+      text_nul_terminated = g_strndup (text, text_len);
+      chunk = _(text_nul_terminated);
+
+      parser_add_chunk (template_contents, chunk, -1);
+    }
+
+  else if (g_strcmp0 (element, "babel") == 0)
+    {
+      const gchar *translated_text;
+
+      text_nul_terminated = g_strndup (text, text_len);
+      translated_text = _(text_nul_terminated);
+
+      if (translated_text != text_nul_terminated)
+        parser_add_chunk (template_contents, translated_text, -1);
+    }
+
+  g_free (text_nul_terminated);
+}
+
+/**
+ * latexila_templates_default_get_contents:
+ * @templates: the #LatexilaTemplatesDefault instance.
+ * @path: the #GtkTreePath of a default template.
+ *
+ * Gets the contents of a default template.
+ *
+ * TODO load contents asynchronously.
+ *
+ * Returns: the default template's contents. Free with g_free().
+ */
+gchar *
+latexila_templates_default_get_contents (LatexilaTemplatesDefault *templates,
+                                         GtkTreePath              *path)
+{
+  GtkTreeIter iter;
+  GFile *xml_file;
+  gchar *xml_contents = NULL;
+  gsize xml_length;
+  GString *template_contents = NULL;
+  GMarkupParser parser = { NULL, NULL, parser_text, NULL, NULL };
+  GMarkupParseContext *context = NULL;
+  GError *error = NULL;
+
+  g_return_val_if_fail (LATEXILA_IS_TEMPLATES_DEFAULT (templates), NULL);
+
+  gtk_tree_model_get_iter (GTK_TREE_MODEL (templates),
+                           &iter,
+                           path);
+
+  gtk_tree_model_get (GTK_TREE_MODEL (templates),
+                      &iter,
+                      LATEXILA_TEMPLATES_COLUMN_FILE, &xml_file,
+                      -1);
+
+  if (xml_file == NULL)
+    return g_strdup ("");
+
+  g_file_load_contents (xml_file, NULL, &xml_contents, &xml_length, NULL, &error);
+
+  template_contents = g_string_new (NULL);
+
+  if (error != NULL)
+    goto out;
+
+  context = g_markup_parse_context_new (&parser, 0, template_contents, NULL);
+  g_markup_parse_context_parse (context, xml_contents, xml_length, &error);
+
+out:
+  g_object_unref (xml_file);
+  g_free (xml_contents);
+
+  if (context != NULL)
+    g_markup_parse_context_unref (context);
+
+  if (error != NULL)
+    {
+      g_warning ("Error when loading default template contents: %s", error->message);
+      g_error_free (error);
+    }
+
+  return g_string_free (template_contents, FALSE);
+}
diff --git a/src/liblatexila/latexila-templates-default.h b/src/liblatexila/latexila-templates-default.h
new file mode 100644
index 0000000..a85e11e
--- /dev/null
+++ b/src/liblatexila/latexila-templates-default.h
@@ -0,0 +1,38 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2015 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LATEXILA_TEMPLATES_DEFAULT_H__
+#define __LATEXILA_TEMPLATES_DEFAULT_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define LATEXILA_TYPE_TEMPLATES_DEFAULT latexila_templates_default_get_type ()
+G_DECLARE_FINAL_TYPE (LatexilaTemplatesDefault, latexila_templates_default, LATEXILA, TEMPLATES_DEFAULT, 
GtkListStore)
+
+LatexilaTemplatesDefault *
+              latexila_templates_default_get_instance           (void);
+
+gchar *       latexila_templates_default_get_contents           (LatexilaTemplatesDefault *templates,
+                                                                 GtkTreePath              *path);
+
+G_END_DECLS
+
+#endif /* __LATEXILA_TEMPLATES_DEFAULT_H__ */
diff --git a/src/liblatexila/latexila-templates-personal.c b/src/liblatexila/latexila-templates-personal.c
new file mode 100644
index 0000000..dfa4831
--- /dev/null
+++ b/src/liblatexila/latexila-templates-personal.c
@@ -0,0 +1,37 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2015 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "latexila-templates-personal.h"
+
+struct _LatexilaTemplatesPersonal
+{
+  GtkListStore parent;
+};
+
+G_DEFINE_TYPE (LatexilaTemplatesPersonal, latexila_templates_personal, GTK_TYPE_LIST_STORE)
+
+static void
+latexila_templates_personal_class_init (LatexilaTemplatesPersonalClass *klass)
+{
+}
+
+static void
+latexila_templates_personal_init (LatexilaTemplatesPersonal *templates)
+{
+}
diff --git a/src/liblatexila/latexila-templates-personal.h b/src/liblatexila/latexila-templates-personal.h
new file mode 100644
index 0000000..53107ed
--- /dev/null
+++ b/src/liblatexila/latexila-templates-personal.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2015 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LATEXILA_TEMPLATES_PERSONAL_H__
+#define __LATEXILA_TEMPLATES_PERSONAL_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define LATEXILA_TYPE_TEMPLATES_PERSONAL latexila_templates_personal_get_type ()
+G_DECLARE_FINAL_TYPE (LatexilaTemplatesPersonal, latexila_templates_personal, LATEXILA, TEMPLATES_PERSONAL, 
GtkListStore)
+
+G_END_DECLS
+
+#endif /* __LATEXILA_TEMPLATES_PERSONAL_H__ */
diff --git a/src/liblatexila/latexila-templates.c b/src/liblatexila/latexila-templates.c
index fa5f767..33d6687 100644
--- a/src/liblatexila/latexila-templates.c
+++ b/src/liblatexila/latexila-templates.c
@@ -68,119 +68,10 @@ struct _LatexilaTemplatesPrivate
   GtkListStore *personal_store;
 };
 
-enum
-{
-  COLUMN_PIXBUF_ICON_NAME,
-
-  /* The string stored in the rc file (article, report, ...). */
-  COLUMN_CONFIG_ICON_NAME,
-
-  COLUMN_NAME,
-
-  /* The file where is stored the contents. For a default template this is an
-   * XML file, for a personal template this is a .tex file. A NULL file is
-   * valid for a default template, it means an empty template.
-   */
-  COLUMN_FILE,
-
-  N_COLUMNS
-};
-
 #define GET_PRIV(self) (latexila_templates_get_instance_private (self))
 
 G_DEFINE_TYPE_WITH_PRIVATE (LatexilaTemplates, latexila_templates, G_TYPE_OBJECT)
 
-static GtkListStore *
-create_new_store (void)
-{
-  return gtk_list_store_new (N_COLUMNS,
-                             G_TYPE_STRING,
-                             G_TYPE_STRING,
-                             G_TYPE_STRING,
-                             G_TYPE_FILE);
-}
-
-/* For compatibility reasons. @config_icon_name is the string stored in the rc
- * file, and the return value is the theme icon name used for the pixbuf. If we
- * store directly the theme icon names in the rc file, old rc files must be
- * modified via a script for example, but it's simpler like that.
- */
-static const gchar *
-get_pixbuf_icon_name (const gchar *config_icon_name)
-{
-  g_return_val_if_fail (config_icon_name != NULL, NULL);
-
-  if (g_str_equal (config_icon_name, "empty"))
-    return "text-x-preview";
-
-  if (g_str_equal (config_icon_name, "article"))
-    return "text-x-generic";
-
-  if (g_str_equal (config_icon_name, "report"))
-    return "x-office-document";
-
-  if (g_str_equal (config_icon_name, "book"))
-    return "accessories-dictionary";
-
-  if (g_str_equal (config_icon_name, "letter"))
-    return "emblem-mail";
-
-  if (g_str_equal (config_icon_name, "beamer"))
-    return "x-office-presentation";
-
-  g_return_val_if_reached (NULL);
-}
-
-static void
-add_template (GtkListStore *store,
-              const gchar  *name,
-              const gchar  *config_icon_name,
-              GFile        *file)
-{
-  GtkTreeIter iter;
-
-  gtk_list_store_append (store, &iter);
-  gtk_list_store_set (store, &iter,
-                      COLUMN_PIXBUF_ICON_NAME, get_pixbuf_icon_name (config_icon_name),
-                      COLUMN_CONFIG_ICON_NAME, config_icon_name,
-                      COLUMN_NAME, name,
-                      COLUMN_FILE, file,
-                      -1);
-}
-
-static void
-add_default_template (GtkListStore *store,
-                      const gchar  *name,
-                      const gchar  *config_icon_name,
-                      const gchar  *filename)
-{
-  gchar *path;
-  GFile *file;
-
-  path = g_build_filename (DATA_DIR, "templates", filename, NULL);
-  file = g_file_new_for_path (path);
-
-  add_template (store, name, config_icon_name, file);
-
-  g_free (path);
-  g_object_unref (file);
-}
-
-static void
-init_default_templates (LatexilaTemplates *templates)
-{
-  LatexilaTemplatesPrivate *priv = GET_PRIV (templates);
-
-  priv->default_store = create_new_store ();
-
-  add_template (priv->default_store, _("Empty"), "empty", NULL);
-  add_default_template (priv->default_store, _("Article"), "article", "article.xml");
-  add_default_template (priv->default_store, _("Report"), "report", "report.xml");
-  add_default_template (priv->default_store, _("Book"), "book", "book.xml");
-  add_default_template (priv->default_store, _("Letter"), "letter", "letter.xml");
-  add_default_template (priv->default_store, _("Presentation"), "beamer", "beamer.xml");
-}
-
 static GFile *
 get_rc_file (void)
 {
@@ -309,267 +200,12 @@ init_personal_templates (LatexilaTemplates *templates)
 }
 
 static void
-latexila_templates_dispose (GObject *object)
-{
-  LatexilaTemplatesPrivate *priv = GET_PRIV (LATEXILA_TEMPLATES (object));
-
-  g_clear_object (&priv->default_store);
-  g_clear_object (&priv->personal_store);
-
-  G_OBJECT_CLASS (latexila_templates_parent_class)->dispose (object);
-}
-
-static void
-latexila_templates_class_init (LatexilaTemplatesClass *klass)
-{
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-  object_class->dispose = latexila_templates_dispose;
-}
-
-static void
 latexila_templates_init (LatexilaTemplates *templates)
 {
-  init_default_templates (templates);
   init_personal_templates (templates);
 }
 
 /**
- * latexila_templates_get_instance:
- *
- * Gets the instance of the #LatexilaTemplates singleton.
- *
- * Returns: (transfer none): the instance of #LatexilaTemplates.
- */
-LatexilaTemplates *
-latexila_templates_get_instance (void)
-{
-  static LatexilaTemplates *instance = NULL;
-
-  if (instance == NULL)
-    instance = g_object_new (LATEXILA_TYPE_TEMPLATES, NULL);
-
-  return instance;
-}
-
-static GtkTreeView *
-get_view (GtkListStore *store)
-{
-  GtkTreeView *view;
-  GtkTreeSelection *selection;
-  GtkCellRenderer *renderer;
-  GtkTreeViewColumn *column;
-
-  view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)));
-  gtk_tree_view_set_headers_visible (view, FALSE);
-  gtk_widget_set_hexpand (GTK_WIDGET (view), TRUE);
-  gtk_widget_set_vexpand (GTK_WIDGET (view), TRUE);
-
-  selection = gtk_tree_view_get_selection (view);
-  gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
-
-  /* Icon */
-  renderer = gtk_cell_renderer_pixbuf_new ();
-  g_object_set (renderer, "stock-size", GTK_ICON_SIZE_BUTTON, NULL);
-
-  column = gtk_tree_view_column_new_with_attributes (NULL,
-                                                     renderer,
-                                                     "icon-name", COLUMN_PIXBUF_ICON_NAME,
-                                                     NULL);
-
-  gtk_tree_view_append_column (view, column);
-
-  /* Name */
-  renderer = gtk_cell_renderer_text_new ();
-
-  column = gtk_tree_view_column_new_with_attributes (NULL,
-                                                     renderer,
-                                                     "text", COLUMN_NAME,
-                                                     NULL);
-
-  gtk_tree_view_append_column (view, column);
-
-  return view;
-}
-
-/**
- * latexila_templates_get_default_templates_view:
- * @templates: the #LatexilaTemplates instance.
- *
- * Gets the view for default templates.
- *
- * Returns: (transfer floating): the #GtkTreeView containing the default
- * templates.
- */
-GtkTreeView *
-latexila_templates_get_default_templates_view (LatexilaTemplates *templates)
-{
-  LatexilaTemplatesPrivate *priv;
-
-  g_return_val_if_fail (LATEXILA_IS_TEMPLATES (templates), NULL);
-
-  priv = GET_PRIV (templates);
-
-  return get_view (priv->default_store);
-}
-
-/**
- * latexila_templates_get_personal_templates_view:
- * @templates: the #LatexilaTemplates instance.
- *
- * Gets the view for personal templates.
- *
- * Returns: (transfer floating): the #GtkTreeView containing the personal
- * templates.
- */
-GtkTreeView *
-latexila_templates_get_personal_templates_view (LatexilaTemplates *templates)
-{
-  LatexilaTemplatesPrivate *priv;
-
-  g_return_val_if_fail (LATEXILA_IS_TEMPLATES (templates), NULL);
-
-  priv = GET_PRIV (templates);
-
-  return get_view (priv->personal_store);
-}
-
-static void
-parser_add_chunk (GString     *string,
-                  const gchar *chunk,
-                  gint         chunk_len)
-{
-  if (chunk == NULL)
-    return;
-
-  /* Remove the first '\n'. Without this, the XML files would be less well
-   * presented.
-   */
-  if (chunk[0] == '\n')
-    {
-      chunk = chunk + 1;
-
-      if (chunk_len != -1)
-        chunk_len--;
-    }
-
-  if (chunk_len != -1)
-    g_string_append_len (string, chunk, chunk_len);
-  else
-    g_string_append (string, chunk);
-}
-
-static void
-parser_text (GMarkupParseContext  *context,
-             const gchar          *text,
-             gsize                 text_len,
-             gpointer              user_data,
-             GError              **error)
-{
-  GString *template_contents = user_data;
-  const gchar *element;
-  gchar *text_nul_terminated = NULL;
-
-  element = g_markup_parse_context_get_element (context);
-
-  if (g_strcmp0 (element, "chunk") == 0)
-    {
-      parser_add_chunk (template_contents, text, text_len);
-    }
-
-  else if (g_strcmp0 (element, "translatableChunk") == 0)
-    {
-      const gchar *chunk;
-
-      text_nul_terminated = g_strndup (text, text_len);
-      chunk = _(text_nul_terminated);
-
-      parser_add_chunk (template_contents, chunk, -1);
-    }
-
-  else if (g_strcmp0 (element, "babel") == 0)
-    {
-      const gchar *translated_text;
-
-      text_nul_terminated = g_strndup (text, text_len);
-      translated_text = _(text_nul_terminated);
-
-      if (translated_text != text_nul_terminated)
-        parser_add_chunk (template_contents, translated_text, -1);
-    }
-
-  g_free (text_nul_terminated);
-}
-
-/**
- * latexila_templates_get_default_template_contents:
- * @templates: the #LatexilaTemplates instance.
- * @path: the #GtkTreePath of a default template.
- *
- * Gets the contents of a default template. The @path must be obtained via the
- * #GtkTreeView returned by latexila_templates_get_default_templates_view().
- *
- * TODO load contents asynchronously.
- *
- * Returns: the default template contents. Free with g_free().
- */
-gchar *
-latexila_templates_get_default_template_contents (LatexilaTemplates *templates,
-                                                  GtkTreePath       *path)
-{
-  LatexilaTemplatesPrivate *priv;
-  GtkTreeIter iter;
-  GFile *xml_file;
-  gchar *xml_contents = NULL;
-  gsize xml_length;
-  GString *template_contents = NULL;
-  GMarkupParser parser = { NULL, NULL, parser_text, NULL, NULL };
-  GMarkupParseContext *context = NULL;
-  GError *error = NULL;
-
-  g_return_val_if_fail (LATEXILA_IS_TEMPLATES (templates), NULL);
-
-  priv = GET_PRIV (templates);
-
-  gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->default_store),
-                           &iter,
-                           path);
-
-  gtk_tree_model_get (GTK_TREE_MODEL (priv->default_store),
-                      &iter,
-                      COLUMN_FILE, &xml_file,
-                      -1);
-
-  if (xml_file == NULL)
-    return g_strdup ("");
-
-  g_file_load_contents (xml_file, NULL, &xml_contents, &xml_length, NULL, &error);
-
-  template_contents = g_string_new (NULL);
-
-  if (error != NULL)
-    goto out;
-
-  context = g_markup_parse_context_new (&parser, 0, template_contents, NULL);
-  g_markup_parse_context_parse (context, xml_contents, xml_length, &error);
-
-out:
-  g_object_unref (xml_file);
-  g_free (xml_contents);
-
-  if (context != NULL)
-    g_markup_parse_context_unref (context);
-
-  if (error != NULL)
-    {
-      g_warning ("Error when loading default template contents: %s", error->message);
-      g_error_free (error);
-    }
-
-  return g_string_free (template_contents, FALSE);
-}
-
-/**
  * latexila_templates_get_personal_template_contents:
  * @templates: the #LatexilaTemplates instance.
  * @path: the #GtkTreePath of a personal template.


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