[latexila/wip/build-tools-revamp: 3/3] More utilities functions



commit 58a49466314a2729ccbdb5ea2994b48308d07251
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu May 8 15:10:20 2014 +0200

    More utilities functions

 docs/reference/latexila-sections.txt |    2 +
 src/liblatexila/latexila-utils.c     |  115 ++++++++++++++++++++++++++++++++++
 src/liblatexila/latexila-utils.h     |    4 +
 3 files changed, 121 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index d0d729b..8d1703a 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -130,5 +130,7 @@ latexila_build_view_get_type
 <SECTION>
 <FILE>utils</FILE>
 <TITLE>LatexilaUtils</TITLE>
+latexila_utils_get_shortname
 latexila_utils_replace_home_dir_with_tilde
+latexila_utils_register_icons
 </SECTION>
diff --git a/src/liblatexila/latexila-utils.c b/src/liblatexila/latexila-utils.c
index a16d984..3f6d93d 100644
--- a/src/liblatexila/latexila-utils.c
+++ b/src/liblatexila/latexila-utils.c
@@ -31,8 +31,54 @@
  */
 
 #include "latexila-utils.h"
+#include <gtk/gtk.h>
 #include <string.h>
 
+static gint
+get_extension_position (const gchar *filename)
+{
+  const gchar *pos;
+  gint length;
+
+  if (filename == NULL)
+    {
+      return 0;
+    }
+
+  length = strlen (filename);
+  pos = filename + length;
+  g_assert (pos[0] == '\0');
+
+  while (TRUE)
+    {
+      pos = g_utf8_find_prev_char (filename, pos);
+
+      if (pos == NULL || pos[0] == '/')
+        {
+          break;
+        }
+
+      if (pos[0] == '.')
+        {
+          return pos - filename;
+        }
+    }
+
+  return length;
+}
+
+/**
+ * latexila_utils_get_shortname:
+ * @filename: a filename.
+ *
+ * Returns: the @filename without its extension. Free with g_free().
+ */
+gchar *
+latexila_utils_get_shortname (const gchar *filename)
+{
+  return g_strndup (filename, get_extension_position (filename));
+}
+
 /**
  * latexila_utils_replace_home_dir_with_tilde:
  * @filename: the filename.
@@ -86,3 +132,72 @@ latexila_utils_replace_home_dir_with_tilde (const gchar *filename)
   g_free (home);
   return g_strdup (filename);
 }
+
+/**
+ * latexila_utils_register_icons:
+ *
+ * Register the LaTeXila icons to the #GtkIconTheme as built-in icons prefixed
+ * with "latexila-". For example the icon located at
+ * data/images/stock-icons/badbox.png in the LaTeXila git repository will be
+ * available with the icon name "latexila-badbox". The "stock-icons" directory
+ * name is for historical reasons and should be changed when stock icons are no
+ * longer used in LaTeXila.
+ */
+void
+latexila_utils_register_icons (void)
+{
+  gchar *resource_path = "/org/gnome/latexila/stock-icons/";
+  gchar **icon_files;
+  gchar **icon_file;
+  GError *error = NULL;
+
+  icon_files = g_resources_enumerate_children (resource_path,
+                                               G_RESOURCE_LOOKUP_FLAGS_NONE,
+                                               &error);
+
+  if (error != NULL)
+    {
+      g_warning ("Failed to register new icons: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  for (icon_file = icon_files; icon_file != NULL && *icon_file != NULL; icon_file++)
+    {
+      gchar *icon_path;
+      GdkPixbuf *pixbuf;
+
+      icon_path = g_strdup_printf ("%s%s", resource_path, *icon_file);
+      pixbuf = gdk_pixbuf_new_from_resource (icon_path, &error);
+
+      if (error == NULL)
+        {
+          gint width = gdk_pixbuf_get_width (pixbuf);
+          gint height = gdk_pixbuf_get_height (pixbuf);
+          gint size = MAX (width, height);
+          gchar *short_name = latexila_utils_get_shortname (*icon_file);
+          gchar *icon_name = g_strdup_printf ("latexila-%s", short_name);
+
+          if (width != height)
+            {
+              g_warning ("Icon with different width and height: %s", *icon_file);
+            }
+
+          gtk_icon_theme_add_builtin_icon (icon_name, size, pixbuf);
+
+          g_free (short_name);
+          g_free (icon_name);
+        }
+      else
+        {
+          g_warning ("Failed to register icon: %s", error->message);
+          g_error_free (error);
+          error = NULL;
+        }
+
+      g_free (icon_path);
+      g_object_unref (pixbuf);
+    }
+
+  g_strfreev (icon_files);
+}
diff --git a/src/liblatexila/latexila-utils.h b/src/liblatexila/latexila-utils.h
index f96abe1..cf88567 100644
--- a/src/liblatexila/latexila-utils.h
+++ b/src/liblatexila/latexila-utils.h
@@ -24,8 +24,12 @@
 
 G_BEGIN_DECLS
 
+gchar *         latexila_utils_get_shortname                    (const gchar *filename);
+
 gchar *         latexila_utils_replace_home_dir_with_tilde      (const gchar *filename);
 
+void            latexila_utils_register_icons                   (void);
+
 G_END_DECLS
 
 #endif /* __LATEXILA_UTILS_H__ */


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