[gnome-epub-thumbnailer] Use new thumbnailer skeleton



commit 6970d3a918a39fc05a4878107ac384778d8f666b
Author: Bastien Nocera <hadess hadess net>
Date:   Sat Jul 20 19:25:57 2013 +0200

    Use new thumbnailer skeleton

 Makefile.am                  |    8 ++-
 gnome-epub-thumbnailer.c     |   99 +++++---------------------------
 gnome-thumbnailer-skeleton.c |  126 ++++++++++++++++++++++++++++++++++++++++++
 gnome-thumbnailer-skeleton.h |   37 ++++++++++++
 4 files changed, 185 insertions(+), 85 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 5a7bb5c..b18655a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,8 +1,12 @@
 
 bin_PROGRAMS = gnome-epub-thumbnailer
 
-gnome_epub_thumbnailer_SOURCES = gnome-epub-thumbnailer.c
-gnome_epub_thumbnailer_CPPFLAGS = $(THUMBNAILER_CFLAGS) $(WARN_CFLAGS)
+gnome_epub_thumbnailer_SOURCES = gnome-epub-thumbnailer.c gnome-thumbnailer-skeleton.c 
gnome-thumbnailer-skeleton.h
+gnome_epub_thumbnailer_CPPFLAGS =                      \
+       $(THUMBNAILER_CFLAGS)                           \
+       -DTHUMBNAILER_RETURNS_DATA                      \
+       -DTHUMBNAILER_USAGE="\"Thumbnail EPub books\""  \
+       $(WARN_CFLAGS)
 gnome_epub_thumbnailer_LDADD = $(THUMBNAILER_LIBS)
 
 thumbnailerdir = $(datadir)/thumbnailers/
diff --git a/gnome-epub-thumbnailer.c b/gnome-epub-thumbnailer.c
index 1b10057..921fb15 100644
--- a/gnome-epub-thumbnailer.c
+++ b/gnome-epub-thumbnailer.c
@@ -21,8 +21,8 @@
 
 #include <string.h>
 #include <glib.h>
-#include <gio/gio.h>
-#include <gdk-pixbuf/gdk-pixbuf.h>
+
+#include "gnome-thumbnailer-skeleton.h"
 
 #include <archive.h>
 #include <archive_entry.h>
@@ -35,17 +35,6 @@
 #define METAFILE_NAMESPACE "urn:oasis:names:tc:opendocument:xmlns:container"
 #define OPF_NAMESPACE "http://www.idpf.org/2007/opf";
 
-static int output_size = 256;
-static gboolean g_fatal_warnings = FALSE;
-static char **filenames = NULL;
-
-static const GOptionEntry entries[] = {
-       { "size", 's', 0, G_OPTION_ARG_INT, &output_size, "Size of the thumbnail in pixels", NULL },
-       {"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL},
-       { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, "[FILE...]" },
-       { NULL }
-};
-
 static int
 regex_matches (gconstpointer a,
               gconstpointer b)
@@ -75,7 +64,7 @@ file_get_zipped_contents (const char   *filename,
        archive_read_support_format_zip (a);
        r = archive_read_open_filename (a, filename, 10240);
        if (r != ARCHIVE_OK) {
-               g_print ("Failed to open archive %s\n", filenames[0]);
+               g_print ("Failed to open archive %s\n", filename);
                return NULL;
        }
 
@@ -254,62 +243,25 @@ bail:
        return cover_path;
 }
 
-int main (int argc, char **argv)
+char *
+file_to_data (const char  *path,
+             gsize       *ret_length,
+             GError     **error)
 {
-       char *input_filename;
        char *metafile;
        gsize length;
        char *cover_path;
        char *cover_data = NULL;
-       GInputStream *mem_stream;
-
-       GdkPixbuf *pixbuf;
-       GError *error = NULL;
-       GOptionContext *context;
-       GFile *input;
-       const char *output;
-
-       g_type_init ();
-
-       /* Options parsing */
-       context = g_option_context_new ("Thumbnail EPub books");
-       g_option_context_add_main_entries (context, entries, NULL);
-
-       if (g_option_context_parse (context, &argc, &argv, &error) == FALSE) {
-               g_warning ("Couldn't parse command-line options: %s", error->message);
-               g_error_free (error);
-               return 1;
-       }
-
-       /* Set fatal warnings if required */
-       if (g_fatal_warnings) {
-               GLogLevelFlags fatal_mask;
-
-               fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
-               fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
-               g_log_set_always_fatal (fatal_mask);
-       }
-
-       if (filenames == NULL || g_strv_length (filenames) != 2) {
-               g_print ("Expects an input and an output file\n");
-               return 1;
-       }
-
-       input = g_file_new_for_commandline_arg (filenames[0]);
-       input_filename = g_file_get_path (input);
-       g_object_unref (input);
-
-       output = filenames[1];
 
        /* Look for the cover in the metafile */
-       metafile = file_get_zipped_contents (input_filename, (GCompareFunc) g_strcmp0, 
"META-INF/container.xml", &length);
-       cover_path = get_cover_path_from_root_file (metafile, length, input_filename);
+       metafile = file_get_zipped_contents (path, (GCompareFunc) g_strcmp0, "META-INF/container.xml", 
&length);
+       cover_path = get_cover_path_from_root_file (metafile, length, path);
        g_free (metafile);
        if (cover_path != NULL) {
-               cover_data = file_get_zipped_contents (input_filename, (GCompareFunc) g_strcmp0, cover_path, 
&length);
+               cover_data = file_get_zipped_contents (path, (GCompareFunc) g_strcmp0, cover_path, &length);
                if (cover_data == NULL)
                        g_warning ("Could not open cover file '%s' in '%s'",
-                                  cover_path, filenames[0]);
+                                  cover_path, path);
                g_free (cover_path);
        }
 
@@ -319,34 +271,15 @@ int main (int argc, char **argv)
 
                regex = g_regex_new (".*cover.*\\.(jpg|jpeg|png)",
                                     G_REGEX_CASELESS, 0, NULL);
-               cover_data = file_get_zipped_contents (input_filename, regex_matches, regex, &length);
+               cover_data = file_get_zipped_contents (path, regex_matches, regex, &length);
        }
 
-       g_free (input_filename);
-
        if (cover_data == NULL) {
-               g_warning ("Could not find cover file in '%s'", filenames[0]);
-               return 1;
-       }
-
-       mem_stream = g_memory_input_stream_new_from_data (cover_data, length, g_free);
-       pixbuf = gdk_pixbuf_new_from_stream_at_scale (mem_stream, output_size, -1, TRUE, NULL, &error);
-       g_object_unref (mem_stream);
-
-       if (!pixbuf) {
-               g_warning ("Couldn't open embedded cover image for file '%s': %s",
-                          filenames[0], error->message);
-               g_error_free (error);
-               return 1;
-       }
-
-       if (gdk_pixbuf_save (pixbuf, output, "png", &error, NULL) == FALSE) {
-               g_warning ("Couldn't save the thumbnail '%s' for file '%s': %s", output, filenames[0], 
error->message);
-               g_error_free (error);
-               return 1;
+               g_warning ("Could not find cover file in '%s'", path);
+               return NULL;
        }
 
-       g_object_unref (pixbuf);
+       *ret_length = length;
 
-       return 0;
+       return cover_data;
 }
diff --git a/gnome-thumbnailer-skeleton.c b/gnome-thumbnailer-skeleton.c
new file mode 100644
index 0000000..b7ec3ec
--- /dev/null
+++ b/gnome-thumbnailer-skeleton.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2013 Bastien Nocera <hadess hadess net>
+ *
+ * Authors: Bastien Nocera <hadess hadess net>
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <string.h>
+#include <glib.h>
+#include <gio/gio.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
+#include "gnome-thumbnailer-skeleton.h"
+
+#ifndef THUMBNAILER_USAGE
+#error "THUMBNAILER_USAGE must be set"
+#endif
+
+static int output_size = 256;
+static gboolean g_fatal_warnings = FALSE;
+static char **filenames = NULL;
+
+static const GOptionEntry entries[] = {
+       { "size", 's', 0, G_OPTION_ARG_INT, &output_size, "Size of the thumbnail in pixels", NULL },
+       {"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL},
+       { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, "[INPUT FILE] [OUTPUT 
FILE]" },
+       { NULL }
+};
+
+int main (int argc, char **argv)
+{
+       char *input_filename;
+       GdkPixbuf *pixbuf;
+       GError *error = NULL;
+       GOptionContext *context;
+       GFile *input;
+       const char *output;
+
+#ifdef THUMBNAILER_RETURNS_PIXBUF
+       /* Nothing */
+#elif THUMBNAILER_RETURNS_DATA
+       char *data = NULL;
+       gsize length;
+#endif
+
+       g_type_init ();
+
+       /* Options parsing */
+       context = g_option_context_new (THUMBNAILER_USAGE);
+       g_option_context_add_main_entries (context, entries, NULL);
+
+       if (g_option_context_parse (context, &argc, &argv, &error) == FALSE) {
+               g_warning ("Couldn't parse command-line options: %s", error->message);
+               g_error_free (error);
+               return 1;
+       }
+
+       /* Set fatal warnings if required */
+       if (g_fatal_warnings) {
+               GLogLevelFlags fatal_mask;
+
+               fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
+               fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
+               g_log_set_always_fatal (fatal_mask);
+       }
+
+       if (filenames == NULL || g_strv_length (filenames) != 2) {
+               g_print ("Expects an input and an output file\n");
+               return 1;
+       }
+
+       input = g_file_new_for_commandline_arg (filenames[0]);
+       input_filename = g_file_get_path (input);
+       g_object_unref (input);
+
+       output = filenames[1];
+
+#ifdef THUMBNAILER_RETURNS_PIXBUF
+       pixbuf = file_to_pixbuf (input_filename, &error);
+#elif THUMBNAILER_RETURNS_DATA
+       data = file_to_data (input_filename, &length, &error);
+       if (data) {
+               GInputStream *mem_stream;
+
+               mem_stream = g_memory_input_stream_new_from_data (data, length, g_free);
+               pixbuf = gdk_pixbuf_new_from_stream_at_scale (mem_stream, output_size, -1, TRUE, NULL, 
&error);
+               g_object_unref (mem_stream);
+       } else {
+               pixbuf = NULL;
+       }
+#else
+#error "One of THUMBNAILER_RETURNS_PIXBUF or THUMBNAILER_RETURNS_DATA must be set"
+#endif
+       g_free (input_filename);
+
+       if (!pixbuf) {
+               g_warning ("Could not thumbnail '%s': %s", filenames[0], error->message);
+               g_error_free (error);
+               g_strfreev (filenames);
+               return 1;
+       }
+
+       if (gdk_pixbuf_save (pixbuf, output, "png", &error, NULL) == FALSE) {
+               g_warning ("Couldn't save the thumbnail '%s' for file '%s': %s", output, filenames[0], 
error->message);
+               g_error_free (error);
+               return 1;
+       }
+
+       g_object_unref (pixbuf);
+
+       return 0;
+}
diff --git a/gnome-thumbnailer-skeleton.h b/gnome-thumbnailer-skeleton.h
new file mode 100644
index 0000000..0c2e687
--- /dev/null
+++ b/gnome-thumbnailer-skeleton.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2013 Bastien Nocera <hadess hadess net>
+ *
+ * Authors: Bastien Nocera <hadess hadess net>
+ *
+ * This program 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 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
+#ifdef THUMBNAILER_RETURNS_PIXBUF
+#ifdef THUMBNAILER_RETURNS_DATA
+#error "Only one of THUMBNAILER_RETURNS_PIXBUF or THUMBNAILER_RETURNS_DATA must be set"
+#else
+GdkPixbuf * file_to_pixbuf (const char  *path,
+                           GError     **error);
+#endif
+#elif THUMBNAILER_RETURNS_DATA
+char * file_to_data (const char  *path,
+                    gsize       *ret_length,
+                    GError     **error);
+#else
+#error "One of THUMBNAILER_RETURNS_PIXBUF or THUMBNAILER_RETURNS_DATA must be set"
+#endif


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