1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2005, Teemu Tervo <teemu.tervo@gmx.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <glib/gi18n-lib.h>
28 #include "comics-document.h"
29 #include "ev-document-misc.h"
30 #include "ev-document-thumbnails.h"
31 #include "ev-file-helpers.h"
33 struct _ComicsDocumentClass
35 GObjectClass parent_class;
38 struct _ComicsDocument
40 GObject parent_instance;
45 char *extract_command;
49 typedef struct _ComicsDocumentClass ComicsDocumentClass;
51 static void comics_document_document_iface_init (EvDocumentIface *iface);
52 static void comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
54 static GSList* get_supported_image_extensions (void);
55 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
57 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
61 static char** extract_argv (EvDocument *document,
65 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
67 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
68 comics_document_document_thumbnails_iface_init);
72 comics_regex_quote (const char *s)
76 d = ret = g_malloc (strlen (s) * 4 + 3);
80 for (; *s; s++, d++) {
106 comics_document_load (EvDocument *document,
110 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
111 GSList *supported_extensions;
112 gchar *list_files_command = NULL, *std_out, *quoted_file;
118 comics_document->archive = g_filename_from_uri (uri, NULL, error);
119 g_return_val_if_fail (comics_document->archive != NULL, FALSE);
121 quoted_file = g_shell_quote (comics_document->archive);
122 mime_type = ev_file_get_mime_type (uri, FALSE, NULL);
124 /* FIXME, use proper cbr/cbz mime types once they're
125 * included in shared-mime-info */
126 if (!strcmp (mime_type, "application/x-cbr") ||
127 !strcmp (mime_type, "application/x-rar")) {
128 comics_document->extract_command =
129 g_strdup ("unrar p -c- -ierr");
131 g_strdup_printf ("unrar vb -c- -- %s", quoted_file);
132 comics_document->regex_arg = FALSE;
133 } else if (!strcmp (mime_type, "application/x-cbz") ||
134 !strcmp (mime_type, "application/zip")) {
135 comics_document->extract_command =
136 g_strdup ("unzip -p -C");
138 g_strdup_printf ("zipinfo -1 -- %s", quoted_file);
139 comics_document->regex_arg = TRUE;
140 } else if (!strcmp (mime_type, "application/x-cb7")) {
141 comics_document->extract_command =
142 g_strdup ("7zr x -so");
144 g_strdup_printf ("7zr l -- %s", quoted_file);
145 comics_document->regex_arg = TRUE;
149 g_free (quoted_file);
151 /* Get list of files in archive */
152 success = g_spawn_command_line_sync (list_files_command,
153 &std_out, NULL, &retval, error);
154 g_free (list_files_command);
158 } else if (retval != 0) {
161 EV_DOCUMENT_ERROR_INVALID,
162 _("File corrupted."));
166 cbr_files = g_strsplit (std_out, "\n", 0);
167 supported_extensions = get_supported_image_extensions ();
168 for (i = 0; cbr_files[i] != NULL; i++) {
169 gchar *suffix = g_strrstr (cbr_files[i], ".");
172 suffix = g_ascii_strdown (suffix + 1, -1);
174 if (g_slist_find_custom (supported_extensions, suffix,
175 (GCompareFunc) strcmp) != NULL) {
176 comics_document->page_names =
177 g_slist_insert_sorted (
178 comics_document->page_names,
179 g_strdup (g_strstrip (cbr_files[i])),
180 (GCompareFunc) strcmp);
181 comics_document->n_pages++;
188 g_strfreev (cbr_files);
189 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
190 g_slist_free (supported_extensions);
192 if (comics_document->n_pages == 0) {
195 EV_DOCUMENT_ERROR_INVALID,
196 _("No images found in archive %s"),
206 comics_document_save (EvDocument *document,
210 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
212 return ev_xfer_uri_simple (comics_document->archive, uri, error);
216 comics_document_get_n_pages (EvDocument *document)
218 return COMICS_DOCUMENT (document)->n_pages;
222 comics_document_get_page_size (EvDocument *document,
227 GdkPixbufLoader *loader;
230 gboolean success, got_size = FALSE;
234 argv = extract_argv (document, page->index);
235 success = g_spawn_async_with_pipes (NULL, argv, NULL,
236 G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
239 NULL, &outpipe, NULL, NULL);
241 g_return_if_fail (success == TRUE);
243 loader = gdk_pixbuf_loader_new ();
244 g_signal_connect (loader, "area-prepared",
245 G_CALLBACK (get_page_size_area_prepared_cb),
248 while (outpipe >= 0) {
249 gssize bytes = read (outpipe, buf, 1024);
252 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
253 if (bytes <= 0 || got_size) {
256 gdk_pixbuf_loader_close (loader, NULL);
260 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
261 GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
263 *width = gdk_pixbuf_get_width (pixbuf);
265 *height = gdk_pixbuf_get_height (pixbuf);
268 g_spawn_close_pid (child_pid);
269 g_object_unref (loader);
273 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
276 gboolean *got_size = data;
281 comics_document_render_pixbuf (EvDocument *document,
284 GdkPixbufLoader *loader;
285 GdkPixbuf *rotated_pixbuf;
292 argv = extract_argv (document, rc->page->index);
293 success = g_spawn_async_with_pipes (NULL, argv, NULL,
295 | G_SPAWN_STDERR_TO_DEV_NULL,
298 NULL, &outpipe, NULL, NULL);
300 g_return_val_if_fail (success == TRUE, NULL);
302 loader = gdk_pixbuf_loader_new ();
303 g_signal_connect (loader, "size-prepared",
304 G_CALLBACK (render_pixbuf_size_prepared_cb), &rc->scale);
306 while (outpipe >= 0) {
307 gssize bytes = read (outpipe, buf, 4096);
310 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
311 } else if (bytes <= 0) {
313 gdk_pixbuf_loader_close (loader, NULL);
318 rotated_pixbuf = gdk_pixbuf_rotate_simple (gdk_pixbuf_loader_get_pixbuf (loader),
320 g_spawn_close_pid (child_pid);
321 g_object_unref (loader);
323 return rotated_pixbuf;
326 static cairo_surface_t *
327 comics_document_render (EvDocument *document,
331 cairo_surface_t *surface;
333 pixbuf = comics_document_render_pixbuf (document, rc);
334 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
335 g_object_unref (pixbuf);
341 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
346 double *scale = data;
347 int w = (width * (*scale) + 0.5);
348 int h = (height * (*scale) + 0.5);
350 gdk_pixbuf_loader_set_size (loader, w, h);
354 comics_document_finalize (GObject *object)
356 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
358 if (comics_document->archive)
359 g_free (comics_document->archive);
361 if (comics_document->page_names) {
362 g_slist_foreach (comics_document->page_names,
363 (GFunc) g_free, NULL);
364 g_slist_free (comics_document->page_names);
367 if (comics_document->extract_command)
368 g_free (comics_document->extract_command);
370 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
374 comics_document_class_init (ComicsDocumentClass *klass)
376 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
377 gobject_class->finalize = comics_document_finalize;
380 static EvDocumentInfo *
381 comics_document_get_info (EvDocument *document)
383 EvDocumentInfo *info;
384 info = g_new0 (EvDocumentInfo, 1);
389 comics_document_document_iface_init (EvDocumentIface *iface)
391 iface->load = comics_document_load;
392 iface->save = comics_document_save;
393 iface->get_n_pages = comics_document_get_n_pages;
394 iface->get_page_size = comics_document_get_page_size;
395 iface->render = comics_document_render;
396 iface->get_info = comics_document_get_info;
400 comics_document_init (ComicsDocument *comics_document)
402 comics_document->archive = NULL;
403 comics_document->page_names = NULL;
404 comics_document->extract_command = NULL;
405 comics_document->n_pages = 0;
408 /* Returns a list of file extensions supported by gdk-pixbuf */
410 get_supported_image_extensions()
412 GSList *extensions = NULL;
413 GSList *formats = gdk_pixbuf_get_formats ();
416 for (l = formats; l != NULL; l = l->next) {
418 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
420 for (i = 0; ext[i] != NULL; i++) {
421 extensions = g_slist_append (extensions,
428 g_slist_free (formats);
433 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
437 GdkPixbuf *thumbnail;
439 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
442 GdkPixbuf *tmp_pixbuf = thumbnail;
444 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
445 g_object_unref (tmp_pixbuf);
452 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
457 gdouble page_width, page_height;
459 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
460 &page_width, &page_height);
462 if (rc->rotation == 90 || rc->rotation == 270) {
463 *width = (gint) (page_height * rc->scale);
464 *height = (gint) (page_width * rc->scale);
466 *width = (gint) (page_width * rc->scale);
467 *height = (gint) (page_height * rc->scale);
472 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
474 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
475 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
479 extract_argv (EvDocument *document, gint page)
481 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
483 char *command_line, *quoted_archive, *quoted_filename;
486 quoted_archive = g_shell_quote (comics_document->archive);
487 if (comics_document->regex_arg) {
488 quoted_filename = comics_regex_quote (
489 g_slist_nth_data (comics_document->page_names, page));
491 quoted_filename = g_shell_quote (
492 g_slist_nth_data (comics_document->page_names, page));
495 command_line = g_strdup_printf ("%s -- %s %s",
496 comics_document->extract_command,
500 g_shell_parse_argv (command_line, NULL, &argv, &err);
503 g_warning ("Error %s", err->message);
507 g_free (command_line);
508 g_free (quoted_archive);
509 g_free (quoted_filename);