diff options
author | Nickolay V. Shmyrev <nshmyrev@yandex.ru> | 2005-11-14 13:21:13 (GMT) |
---|---|---|
committer | Nickolay V. Shmyrev <nshmyrev@src.gnome.org> | 2005-11-14 13:21:13 (GMT) |
commit | 30582eef6d8c325d026e7db0301921ddba21dfec (patch) | |
tree | bad060c2e88f760d095a403156f615dfd6e7e205 /comics | |
parent | ae3f65853c9a38b242143559ad15b6df1f5244a6 (diff) |
New CBR/CBZ backend for comic books.
2005-11-14 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
* Makefile.am:
* backend/Makefile.am:
* backend/ev-document-factory.c:
* backend/ev-document-factory.h:
* comics/Makefile.am:
* comics/comics-document.c: (G_DEFINE_TYPE_WITH_CODE),
(comics_document_load), (comics_document_save),
(comics_document_get_n_pages), (comics_document_get_page_size),
(get_page_size_area_prepared_cb), (comics_document_render_pixbuf) (render_pixbuf_size_prepared_cb), (comics_document_finalize),
(comics_document_class_init), (comics_document_can_get_text),
(comics_document_get_info), (comics_document_document_iface_init),
(comics_document_init), (get_supported_image_extensions),
(comics_document_thumbnails_get_geometry),
(comics_document_thumbnails_get_thumbnail),
(comics_document_thumbnails_get_dimensions),
(comics_document_document_thumbnails_iface_init), (extract_argv):
* comics/comics-document.h:
* comics/ev-cbr-mime.xml:
* configure.ac:
* po/POTFILES.in:
* shell/ev-application.c: (ev_application_register_service):
* shell/ev-document-types.c: (ev_document_types_add_filters):
* thumbnailer/Makefile.am:
* thumbnailer/evince-thumbnailer-comics.schemas.in:
New CBR/CBZ backend for comic books.
Diffstat (limited to 'comics')
-rw-r--r-- | comics/Makefile.am | 25 | ||||
-rw-r--r-- | comics/comics-document.c | 486 | ||||
-rw-r--r-- | comics/comics-document.h | 38 | ||||
-rw-r--r-- | comics/ev-cbr-mime.xml | 18 |
4 files changed, 567 insertions, 0 deletions
diff --git a/comics/Makefile.am b/comics/Makefile.am new file mode 100644 index 0000000..22c9290 --- /dev/null +++ b/comics/Makefile.am @@ -0,0 +1,25 @@ +INCLUDES = \ + -I$(top_srcdir) \ + -I$(top_srcdir)/backend \ + -I$(top_srcdir)/lib \ + $(LIB_CFLAGS) + +noinst_LTLIBRARIES = libcomicsdocument.la + +libcomicsdocument_la_SOURCES = \ + comics-document.c \ + comics-document.h + +# need to respect $XDG_DATA_DIR probably +xdgmimedir = $(gnome_data)/mime/packages +xdgmime_DATA = ev-cbr-mime.xml + +EXTRA_DIST = $(xdgmime_DATA) + +install-data-hook: + update-mime-database $(gnome_data)/mime + +uninstall-hook: + update-mime-database $(gnome_data)/mime + + diff --git a/comics/comics-document.c b/comics/comics-document.c new file mode 100644 index 0000000..aa7236a --- /dev/null +++ b/comics/comics-document.c @@ -0,0 +1,486 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ +/* + * Copyright (C) 2005, Teemu Tervo <teemu.tervo@gmx.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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include <unistd.h> +#include <string.h> +#include <glib/gi18n.h> +#include <libgnomevfs/gnome-vfs-mime-utils.h> +#include <libgnomevfs/gnome-vfs-xfer.h> + +#include "comics-document.h" +#include "ev-document-misc.h" +#include "ev-document-thumbnails.h" + +struct _ComicsDocumentClass +{ + GObjectClass parent_class; +}; + +struct _ComicsDocument +{ + GObject parent_instance; + + gchar *archive; + GSList *page_names; + int n_pages; + char *extract_command; +}; + +typedef struct _ComicsDocumentClass ComicsDocumentClass; + +static void comics_document_document_iface_init (EvDocumentIface *iface); +static void comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface); + +static GSList* get_supported_image_extensions (void); +static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader, + gpointer data); +static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader, + gint width, + gint height, + gpointer data); +static char** extract_argv (EvDocument *document, + gint page); + + +G_DEFINE_TYPE_WITH_CODE ( + ComicsDocument, comics_document, G_TYPE_OBJECT, + { + G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, + comics_document_document_iface_init); + G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, + comics_document_document_thumbnails_iface_init); + } ); + +static gboolean +comics_document_load (EvDocument *document, + const char *uri, + GError **error) +{ + ComicsDocument *comics_document = COMICS_DOCUMENT (document); + GSList *supported_extensions; + gchar *list_files_command = NULL, *stdout, *quoted_file, *mime_type; + gchar **cbr_files; + gboolean success; + int i, retval; + + comics_document->archive = g_filename_from_uri (uri, NULL, error); + g_return_val_if_fail (comics_document->archive != NULL, FALSE); + + quoted_file = g_shell_quote (comics_document->archive); + mime_type = gnome_vfs_get_mime_type (uri); + + /* FIXME, use proper cbr/cbz mime types once they're + * included in shared-mime-info */ + if (!strcmp (mime_type, "application/cbr")) { + comics_document->extract_command = + g_strdup ("unrar p -c- -ierr"); + list_files_command = + g_strdup_printf ("unrar vb -c- -- %s", quoted_file); + } else if (!strcmp (mime_type, "application/cbz")) { + comics_document->extract_command = + g_strdup ("unzip -p -C"); + list_files_command = + g_strdup_printf ("zipinfo -1 -- %s", quoted_file); + } + + g_free (quoted_file); + + /* Get list of files in archive */ + success = g_spawn_command_line_sync (list_files_command, + &stdout, NULL, &retval, error); + g_free (list_files_command); + + if (!success) { + g_free (mime_type); + return FALSE; + } else if (retval != 0) { + g_set_error (error, + EV_DOCUMENT_ERROR, + EV_DOCUMENT_ERROR_INVALID, + _("File corrupted.")); + g_free (mime_type); + return FALSE; + } + + cbr_files = g_strsplit (stdout, "\n", 0); + supported_extensions = get_supported_image_extensions (); + for (i = 0; cbr_files[i] != NULL; i++) { + gchar *suffix = g_strrstr (cbr_files[i], "."); + if (!suffix) + continue; + suffix = g_ascii_strdown (suffix + 1, -1); + + if (g_slist_find_custom (supported_extensions, suffix, + (GCompareFunc) strcmp) != NULL) { + comics_document->page_names = + g_slist_insert_sorted ( + comics_document->page_names, + g_strdup (g_strstrip (cbr_files[i])), + (GCompareFunc) strcmp); + comics_document->n_pages++; + } + + g_free (suffix); + } + + g_free (stdout); + g_free (mime_type); + g_strfreev (cbr_files); + g_slist_foreach (supported_extensions, (GFunc) g_free, NULL); + g_slist_free (supported_extensions); + + if (comics_document->n_pages == 0) { + g_set_error (error, + EV_DOCUMENT_ERROR, + EV_DOCUMENT_ERROR_INVALID, + _("No images found in archive %s"), + uri); + return FALSE; + } + + return TRUE; +} + + +static gboolean +comics_document_save (EvDocument *document, + const char *uri, + GError **error) +{ + ComicsDocument *comics_document = COMICS_DOCUMENT (document); + GnomeVFSURI *source, *target; + GnomeVFSResult ret; + + source = gnome_vfs_uri_new (comics_document->archive); + target = gnome_vfs_uri_new (uri); + + ret = gnome_vfs_xfer_uri (source, target, + GNOME_VFS_XFER_DEFAULT, + GNOME_VFS_XFER_ERROR_MODE_ABORT, + GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE, + NULL, NULL); + + gnome_vfs_uri_unref (source); + gnome_vfs_uri_unref (target); + + return ret == GNOME_VFS_OK; +} + +static int +comics_document_get_n_pages (EvDocument *document) +{ + return COMICS_DOCUMENT (document)->n_pages; +} + +static void +comics_document_get_page_size (EvDocument *document, + int page, + double *width, + double *height) +{ + GdkPixbufLoader *loader; + char **argv; + guchar buf[1024]; + gboolean success, got_size = FALSE; + gint outpipe = -1; + GPid child_pid = -1; + + argv = extract_argv (document, page); + success = g_spawn_async_with_pipes (NULL, argv, NULL, + G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, + &child_pid, + NULL, &outpipe, NULL, NULL); + g_strfreev (argv); + g_return_if_fail (success == TRUE); + + loader = gdk_pixbuf_loader_new (); + g_signal_connect (loader, "area-prepared", + G_CALLBACK (get_page_size_area_prepared_cb), + &got_size); + + while (outpipe >= 0) { + gssize bytes = read (outpipe, buf, 1024); + + if (bytes > 0) + gdk_pixbuf_loader_write (loader, buf, bytes, NULL); + if (bytes <= 0 || got_size) { + close (outpipe); + outpipe = -1; + gdk_pixbuf_loader_close (loader, NULL); + } + } + + if (gdk_pixbuf_loader_get_pixbuf (loader)) { + GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); + if (width) + *width = gdk_pixbuf_get_width (pixbuf); + if (height) + *height = gdk_pixbuf_get_height (pixbuf); + } + + g_spawn_close_pid (child_pid); + g_object_unref (loader); +} + +static void +get_page_size_area_prepared_cb (GdkPixbufLoader *loader, + gpointer data) +{ + gboolean *got_size = data; + *got_size = TRUE; +} + +static GdkPixbuf * +comics_document_render_pixbuf (EvDocument *document, + EvRenderContext *rc) +{ + GdkPixbufLoader *loader; + GdkPixbuf *rotated_pixbuf; + char **argv; + guchar buf[4096]; + gboolean success; + gint outpipe = -1; + GPid child_pid = -1; + + argv = extract_argv (document, rc->page); + success = g_spawn_async_with_pipes (NULL, argv, NULL, + G_SPAWN_SEARCH_PATH + | G_SPAWN_STDERR_TO_DEV_NULL, + NULL, NULL, + &child_pid, + NULL, &outpipe, NULL, NULL); + g_strfreev (argv); + g_return_val_if_fail (success == TRUE, NULL); + + loader = gdk_pixbuf_loader_new (); + g_signal_connect (loader, "size-prepared", + G_CALLBACK (render_pixbuf_size_prepared_cb), &rc->scale); + + while (outpipe >= 0) { + gssize bytes = read (outpipe, buf, 4096); + + if (bytes > 0) { + gdk_pixbuf_loader_write (loader, buf, bytes, NULL); + } else if (bytes <= 0) { + close (outpipe); + gdk_pixbuf_loader_close (loader, NULL); + outpipe = -1; + } + } + + rotated_pixbuf = gdk_pixbuf_rotate_simple (gdk_pixbuf_loader_get_pixbuf (loader), + 360 - rc->rotation); + g_spawn_close_pid (child_pid); + g_object_unref (loader); + return rotated_pixbuf; +} + +static void +render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader, + gint width, + gint height, + gpointer data) +{ + double *scale = data; + int w = width * (*scale); + int h = height * (*scale); + + gdk_pixbuf_loader_set_size (loader, w, h); +} + +static void +comics_document_finalize (GObject *object) +{ + ComicsDocument *comics_document = COMICS_DOCUMENT (object); + + if (comics_document->archive) + g_free (comics_document->archive); + + if (comics_document->page_names) { + g_slist_foreach (comics_document->page_names, + (GFunc) g_free, NULL); + g_slist_free (comics_document->page_names); + } + + if (comics_document->extract_command) + g_free (comics_document->extract_command); + + G_OBJECT_CLASS (comics_document_parent_class)->finalize (object); +} + +static void +comics_document_class_init (ComicsDocumentClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + gobject_class->finalize = comics_document_finalize; +} + +static gboolean +comics_document_can_get_text (EvDocument *document) +{ + return FALSE; +} + +static EvDocumentInfo * +comics_document_get_info (EvDocument *document) +{ + EvDocumentInfo *info; + info = g_new0 (EvDocumentInfo, 1); + return info; +} + +static void +comics_document_document_iface_init (EvDocumentIface *iface) +{ + iface->load = comics_document_load; + iface->save = comics_document_save; + iface->can_get_text = comics_document_can_get_text; + iface->get_n_pages = comics_document_get_n_pages; + iface->get_page_size = comics_document_get_page_size; + iface->render_pixbuf = comics_document_render_pixbuf; + iface->get_info = comics_document_get_info; +} + +static void +comics_document_init (ComicsDocument *comics_document) +{ + comics_document->archive = NULL; + comics_document->page_names = NULL; + comics_document->extract_command = NULL; + comics_document->n_pages = 0; +} + +/* Returns a list of file extensions supported by gdk-pixbuf */ +static GSList* +get_supported_image_extensions() +{ + GSList *extensions = NULL; + GSList *formats = gdk_pixbuf_get_formats (); + GSList *l; + + for (l = formats; l != NULL; l = l->next) { + int i; + gchar **ext = gdk_pixbuf_format_get_extensions (l->data); + + for (i = 0; ext[i] != NULL; i++) { + extensions = g_slist_append (extensions, + g_strdup (ext[i])); + } + + g_strfreev (ext); + } + + g_slist_free (formats); + return extensions; +} + +static void +comics_document_thumbnails_get_geometry (EvDocumentThumbnails *document, + gint page, + gint suggested_width, + gint *width, + gint *height, + gdouble *scale_factor) +{ + gdouble orig_width, orig_height, scale; + + comics_document_get_page_size (EV_DOCUMENT (document), page, + &orig_width, &orig_height); + scale = suggested_width / orig_width; + + if (width) + *width = suggested_width; + if (height) + *height = orig_height * scale; + if (scale_factor) + *scale_factor = scale; +} + +static GdkPixbuf * +comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document, + gint page, + gint rotation, + gint size, + gboolean border) +{ + GdkPixbuf *thumbnail, *framed; + gint thumb_width, thumb_height; + gdouble scale; + EvRenderContext *rc; + + comics_document_thumbnails_get_geometry (document, page, size, + &thumb_width, &thumb_height, + &scale); + + rc = ev_render_context_new (rotation, page, scale); + thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), + rc); + g_object_unref (G_OBJECT (rc)); + + if (border) { + GdkPixbuf *tmp_pixbuf = thumbnail; + thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf); + g_object_unref (tmp_pixbuf); + } + + return thumbnail; +} + +static void +comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document, + gint page, + gint suggested_width, + gint *width, + gint *height) +{ + comics_document_thumbnails_get_geometry (document, page, + suggested_width, + width, height, NULL); +} + +static void +comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface) +{ + iface->get_thumbnail = comics_document_thumbnails_get_thumbnail; + iface->get_dimensions = comics_document_thumbnails_get_dimensions; +} + +static char** +extract_argv (EvDocument *document, gint page) +{ + ComicsDocument *comics_document = COMICS_DOCUMENT (document); + char **argv; + char *command_line, *quoted_archive, *quoted_filename; + + quoted_archive = g_shell_quote (comics_document->archive); + quoted_filename = g_shell_quote ( + g_slist_nth_data (comics_document->page_names, page)); + + command_line = g_strdup_printf ("%s -- %s %s", + comics_document->extract_command, + quoted_archive, + quoted_filename); + g_shell_parse_argv (command_line, NULL, &argv, NULL); + + g_free (command_line); + g_free (quoted_archive); + g_free (quoted_filename); + return argv; +} diff --git a/comics/comics-document.h b/comics/comics-document.h new file mode 100644 index 0000000..cd5b17b --- /dev/null +++ b/comics/comics-document.h @@ -0,0 +1,38 @@ +/* comics-document.h: Implementation of EvDocument for comic book archives + * Copyright (C) 2005, Teemu Tervo <teemu.tervo@gmx.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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __COMICS_DOCUMENT_H__ +#define __COMICS_DOCUMENT_H__ + +#include "ev-document.h" + +G_BEGIN_DECLS + +#define COMICS_TYPE_DOCUMENT (comics_document_get_type ()) +#define COMICS_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), COMICS_TYPE_DOCUMENT, ComicsDocument)) +#define COMICS_IS_DOCUMENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), COMICS_TYPE_DOCUMENT)) + +typedef struct _ComicsDocument ComicsDocument; + +ComicsDocument *comics_document_new (void); + +GType comics_document_get_type (void) G_GNUC_CONST; + +G_END_DECLS + +#endif /* __COMICS_DOCUMENT_H__ */ diff --git a/comics/ev-cbr-mime.xml b/comics/ev-cbr-mime.xml new file mode 100644 index 0000000..8b1a54a --- /dev/null +++ b/comics/ev-cbr-mime.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> + <mime-type type="application/cbr"> + <comment>CBR document</comment> + <magic priority="50"> + <match value="Rar!" type="string" offset="0"/> + </magic> + <glob pattern="*.cbr"/> + </mime-type> + <mime-type type="application/cbz"> + <comment>CBZ document</comment> + <magic priority="50"> + <match value="PK\003\004" type="string" offset="0"/> + </magic> + <glob pattern="*.cbz"/> + </mime-type> +</mime-info> + |