[latexila/wip/latexila-next: 36/50] More utilities functions
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [latexila/wip/latexila-next: 36/50] More utilities functions
- Date: Mon, 18 Aug 2014 20:04:38 +0000 (UTC)
commit 2da7491084df92eca1cb3eabd90a79b3e3975ede
Author: Sébastien Wilmet <swilmet gnome org>
Date: Thu May 8 15:10:20 2014 +0200
More utilities functions
With unit tests and documentation of course.
docs/reference/latexila-sections.txt | 2 +
src/liblatexila/latexila-utils.c | 115 ++++++++++++++++++++++++++++++++++
src/liblatexila/latexila-utils.h | 4 +
tests/Makefile.am | 3 +
tests/test-utils.c | 76 ++++++++++++++++++++++
5 files changed, 200 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__ */
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a9d0ac7..b690129 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -11,6 +11,9 @@ LDADD = $(top_builddir)/src/liblatexila/liblatexila.la
UNIT_TEST_PROGS = test-build-tools
test_build_tools_SOURCES = test-build-tools.c
+UNIT_TEST_PROGS += test-utils
+test_utils_SOURCES = test-utils.c
+
noinst_PROGRAMS = $(UNIT_TEST_PROGS)
TESTS = $(UNIT_TEST_PROGS)
diff --git a/tests/test-utils.c b/tests/test-utils.c
new file mode 100644
index 0000000..9072428
--- /dev/null
+++ b/tests/test-utils.c
@@ -0,0 +1,76 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2014 - 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.h"
+
+static void
+test_get_shortname (void)
+{
+ gchar *shortname;
+
+ shortname = latexila_utils_get_shortname ("file.txt");
+ g_assert_cmpstr (shortname, ==, "file");
+ g_free (shortname);
+
+ shortname = latexila_utils_get_shortname ("file.tar.gz");
+ g_assert_cmpstr (shortname, ==, "file.tar");
+ g_free (shortname);
+
+ shortname = latexila_utils_get_shortname ("file");
+ g_assert_cmpstr (shortname, ==, "file");
+ g_free (shortname);
+
+ shortname = latexila_utils_get_shortname ("dir.ext/blah");
+ g_assert_cmpstr (shortname, ==, "dir.ext/blah");
+ g_free (shortname);
+}
+
+static void
+test_replace_home_dir_with_tilde (void)
+{
+ const gchar *homedir = g_get_home_dir ();
+ gchar *before;
+ gchar *after;
+
+ before = g_build_filename (homedir, "blah", NULL);
+ after = latexila_utils_replace_home_dir_with_tilde (before);
+ g_assert_cmpstr (after, ==, "~/blah");
+ g_free (before);
+ g_free (after);
+
+ after = latexila_utils_replace_home_dir_with_tilde (homedir);
+ g_assert_cmpstr (after, ==, "~");
+ g_free (after);
+
+ after = latexila_utils_replace_home_dir_with_tilde ("/blah");
+ g_assert_cmpstr (after, ==, "/blah");
+ g_free (after);
+}
+
+gint
+main (gint argc,
+ gchar **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/utils/get-shortname", test_get_shortname);
+ g_test_add_func ("/utils/replace-home-dir-with-tilde", test_replace_home_dir_with_tilde);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]