[latexila/wip/templates-revamp] Templates revamp (wip)



commit 3551be8523c4658882d2608c76a4df93b023ac9b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Apr 8 17:41:00 2015 +0200

    Templates revamp (wip)

 src/liblatexila/Makefile.am          |    2 +
 src/liblatexila/latexila-templates.c |  347 ++++++++++++++++++++++++++++++++++
 src/liblatexila/latexila-templates.h |   34 ++++
 3 files changed, 383 insertions(+), 0 deletions(-)
---
diff --git a/src/liblatexila/Makefile.am b/src/liblatexila/Makefile.am
index 7bd41ab..adb4c05 100644
--- a/src/liblatexila/Makefile.am
+++ b/src/liblatexila/Makefile.am
@@ -23,6 +23,7 @@ liblatexila_headers =                         \
        latexila-post-processor-latex.h         \
        latexila-post-processor-latexmk.h       \
        latexila-synctex.h                      \
+       latexila-templates.h                    \
        latexila-types.h                        \
        latexila-utils.h
 
@@ -38,6 +39,7 @@ liblatexila_sources =                         \
        latexila-post-processor-latex.c         \
        latexila-post-processor-latexmk.c       \
        latexila-synctex.c                      \
+       latexila-templates.c                    \
        latexila-utils.c
 
 liblatexila_built_sources =    \
diff --git a/src/liblatexila/latexila-templates.c b/src/liblatexila/latexila-templates.c
new file mode 100644
index 0000000..1bfcfa6
--- /dev/null
+++ b/src/liblatexila/latexila-templates.c
@@ -0,0 +1,347 @@
+/*
+ * 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.h"
+#include <gtk/gtk.h>
+#include <glib/gi18n.h>
+#include <string.h>
+
+typedef struct _LatexilaTemplatesPrivate LatexilaTemplatesPrivate;
+
+struct _LatexilaTemplates
+{
+  GObject parent;
+};
+
+struct _LatexilaTemplatesPrivate
+{
+  /* Contains the default templates (empty, article, report, ...). */
+  GtkListStore *default_store;
+
+  /* Contains the personal templates (created by the user). */
+  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, 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");
+}
+
+#if 0
+static GFile *
+get_data_dir (void)
+{
+  gchar *path;
+  GFile *data_dir;
+
+  path = g_build_filename (g_get_user_data_dir (), "latexila", NULL);
+  data_dir = g_file_new_for_path (path);
+
+  g_free (path);
+  return data_dir;
+}
+#endif
+
+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;
+}
+
+static GFile *
+get_personal_template_file (gint template_num)
+{
+  gchar *filename;
+  gchar *path;
+  GFile *template_file;
+
+  filename = g_strdup_printf ("%d.tex", template_num);
+  path = g_build_filename (g_get_user_data_dir (), "latexila", filename, NULL);
+  template_file = g_file_new_for_path (path);
+
+  g_free (filename);
+  g_free (path);
+  return template_file;
+}
+
+static void
+rc_file_contents_loaded_cb (GFile             *rc_file,
+                            GAsyncResult      *result,
+                            LatexilaTemplates *templates)
+{
+  LatexilaTemplatesPrivate *priv = GET_PRIV (templates);
+  gchar *contents = NULL;
+  gsize length;
+  GKeyFile *key_file = NULL;
+  gchar **names = NULL;
+  gchar **icons = NULL;
+  gsize n_names;
+  gsize n_icons;
+  gint i;
+  GError *error = NULL;
+
+  g_file_load_contents_finish (rc_file, result, &contents, &length, NULL, &error);
+
+  if (error != NULL)
+    {
+      /* If the rc file doesn't exist, it means that there is no personal
+       * templates.
+       */
+      if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          g_error_free (error);
+          error = NULL;
+        }
+
+      goto out;
+    }
+
+  key_file = g_key_file_new ();
+  g_key_file_load_from_data (key_file, contents, length, G_KEY_FILE_NONE, &error);
+
+  if (error != NULL)
+    goto out;
+
+  names = g_key_file_get_string_list (key_file, PACKAGE_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);
+
+  if (error != NULL)
+    goto out;
+
+  g_return_if_fail (n_names == n_icons);
+
+  for (i = 0; i < n_names; i++)
+    {
+      GFile *template_file;
+
+      template_file = get_personal_template_file (i);
+      add_template (priv->personal_store, names[i], icons[i], template_file);
+
+      g_object_unref (template_file);
+    }
+
+out:
+
+  if (error != NULL)
+    {
+      g_warning ("The loading of personal templates failed: %s", error->message);
+      g_error_free (error);
+    }
+
+  g_free (contents);
+  g_strfreev (names);
+  g_strfreev (icons);
+
+  if (key_file != NULL)
+    g_key_file_unref (key_file);
+
+  /* Async operation finished. */
+  g_object_unref (templates);
+}
+
+static void
+init_personal_templates (LatexilaTemplates *templates)
+{
+  LatexilaTemplatesPrivate *priv = GET_PRIV (templates);
+  GFile *rc_file;
+
+  priv->personal_store = create_new_store ();
+
+  rc_file = get_rc_file ();
+
+  /* Prevent @templates from being destroyed during the async operation. */
+  g_object_ref (templates);
+  g_file_load_contents_async (rc_file,
+                              NULL,
+                              (GAsyncReadyCallback) rc_file_contents_loaded_cb,
+                              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);
+}
+
+#if 0
+static void
+latexila_templates_finalize (GObject *object)
+{
+
+  G_OBJECT_CLASS (latexila_templates_parent_class)->finalize (object);
+}
+#endif
+
+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;
+}
diff --git a/src/liblatexila/latexila-templates.h b/src/liblatexila/latexila-templates.h
new file mode 100644
index 0000000..52c4f10
--- /dev/null
+++ b/src/liblatexila/latexila-templates.h
@@ -0,0 +1,34 @@
+/*
+ * 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_H__
+#define __LATEXILA_TEMPLATES_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define LATEXILA_TYPE_TEMPLATES latexila_templates_get_type ()
+G_DECLARE_FINAL_TYPE (LatexilaTemplates, latexila_templates, LATEXILA, TEMPLATES, GObject)
+
+LatexilaTemplates *   latexila_templates_get_instance             (void);
+
+G_END_DECLS
+
+#endif /* __LATEXILA_TEMPLATES_H__ */


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