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>
26 #include <glib/gstdio.h>
32 #include "comics-document.h"
33 #include "ev-document-misc.h"
34 #include "ev-document-thumbnails.h"
35 #include "ev-file-helpers.h"
37 struct _ComicsDocumentClass
39 GObjectClass parent_class;
48 } ComicBookDecompressType;
50 struct _ComicsDocument
52 GObject parent_instance;
56 gchar *selected_command;
57 gchar *extract_command, *list_command, *decompress_tmp;
59 ComicBookDecompressType command_usage;
64 char *extract, *list, *decompress_tmp;
66 } command_usage_def[] = {
67 {"%s p -c- -ierr", "%s vb -c- -- %s", NULL , FALSE},
68 {NULL , "%s t %s" , "%s -xf %s %s", TRUE },
69 {"%s -p -C" , "%s -Z -1 -- %s" , NULL , TRUE },
70 {"%s x -so" , "%s l -- %s" , NULL , TRUE }
74 typedef struct _ComicsDocumentClass ComicsDocumentClass;
76 static void comics_document_document_iface_init (EvDocumentIface *iface);
77 static void comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
79 static GSList* get_supported_image_extensions (void);
80 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
82 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
86 static char** extract_argv (EvDocument *document,
90 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
92 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
93 comics_document_document_thumbnails_iface_init);
97 comics_regex_quote (const char *s)
101 d = ret = g_malloc (strlen (s) * 4 + 3);
105 for (; *s; s++, d++) {
130 /* This function manages the command for decompressing a comic book */
132 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
133 const gchar *command,
137 gchar *std_out, *basename;
141 success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
142 NULL, &retval, &err);
143 basename = g_path_get_basename (command);
147 EV_DOCUMENT_ERROR_INVALID,
148 _("Error launching the command “%s” in order to "
149 "decompress the comic book: %s"),
153 } else if (WIFEXITED (retval)) {
154 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
161 EV_DOCUMENT_ERROR_INVALID,
162 _("The command “%s” failed at "
163 "decompressing the comic book."),
170 EV_DOCUMENT_ERROR_INVALID,
171 _("The command “%s” did not end normally."),
179 /* This function shows how to use the choosen command for decompressing a
180 * comic book file. It modifies fields of the ComicsDocument struct with
181 * this information */
183 comics_generate_command_lines (ComicsDocument *comics_document,
187 ComicBookDecompressType type;
189 type = comics_document->command_usage;
190 quoted_file = g_shell_quote (comics_document->archive);
192 comics_document->extract_command =
193 g_strdup_printf (command_usage_def[type].extract,
194 comics_document->selected_command);
195 comics_document->list_command =
196 g_strdup_printf (command_usage_def[type].list,
197 comics_document->selected_command,
199 comics_document->regex_arg = command_usage_def[type].regex_arg;
200 if (command_usage_def[type].decompress_tmp) {
201 comics_document->dir = ev_tmp_directory (NULL);
202 comics_document->decompress_tmp =
203 g_strdup_printf (command_usage_def[type].decompress_tmp,
204 comics_document->selected_command,
206 comics_document->dir);
207 g_free (quoted_file);
208 /* unrar-free can't create directories so we do it on its
210 if (type == GNAUNRAR) {
211 if (g_mkdir_with_parents (comics_document->dir, 0700) !=
216 EV_DOCUMENT_ERROR_INVALID,
217 _("Failed to create a temporary "
219 g_warning ("Failed to create directory %s: %s",
220 comics_document->dir,
226 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
227 comics_document->selected_command, error))
232 g_free (quoted_file);
238 /* This function chooses an external command for decompressing a comic
239 * book based on its mime tipe. */
241 comics_check_decompress_command (gchar *mime_type,
242 ComicsDocument *comics_document,
246 gchar *std_out, *std_err;
250 /* FIXME, use proper cbr/cbz mime types once they're
251 * included in shared-mime-info */
253 if (!strcmp (mime_type, "application/x-cbr") ||
254 !strcmp (mime_type, "application/x-rar")) {
255 /* The RARLAB provides a no-charge proprietary (freeware)
256 * decompress-only client for Linux called unrar. Another
257 * option is a GPLv2-licensed command-line tool developed by
258 * the Gna! project. Confusingly enough, the free software RAR
259 * decoder is also named unrar. For this reason we need to add
260 * some lines for disambiguation. Sorry for the added the
261 * complexity but it's life :)
262 * Finally, some distributions, like Debian, rename this free
263 * option as unrar-free.
265 comics_document->selected_command =
266 g_find_program_in_path ("unrar");
267 if (comics_document->selected_command) {
268 /* We only use std_err to avoid printing useless error
269 * messages on the terminal */
271 g_spawn_command_line_sync (
272 comics_document->selected_command,
276 g_propagate_error (error, err);
279 /* I don't check retval status because RARLAB unrar
280 * doesn't have a way to return 0 without involving an
281 * operation with a file*/
282 } else if (WIFEXITED (retval)) {
283 if (g_strrstr (std_out,"freeware") != NULL)
284 /* The RARLAB freeware client */
285 comics_document->command_usage = RARLABS;
287 /* The Gna! free software client */
288 comics_document->command_usage = GNAUNRAR;
295 /* The Gna! free software client with Debian naming convention */
296 comics_document->selected_command =
297 g_find_program_in_path ("unrar-free");
298 if (comics_document->selected_command) {
299 comics_document->command_usage = GNAUNRAR;
303 } else if (!strcmp (mime_type, "application/x-cbz") ||
304 !strcmp (mime_type, "application/zip")) {
305 /* InfoZIP's unzip program */
306 comics_document->selected_command =
307 g_find_program_in_path ("unzip");
308 if (comics_document->selected_command) {
309 comics_document->command_usage = UNZIP;
313 } else if (!strcmp (mime_type, "application/x-cb7") ||
314 !strcmp (mime_type, "application/x-7z-compressed")) {
315 /* 7zr is a light stand-alone executable that supports only
317 comics_document->selected_command =
318 g_find_program_in_path ("7zr");
319 if (comics_document->selected_command) {
320 comics_document->command_usage = P7ZIP;
326 EV_DOCUMENT_ERROR_INVALID,
327 _("Not a comic book MIME type: %s"),
331 g_set_error_literal (error,
333 EV_DOCUMENT_ERROR_INVALID,
334 _("Can't find an appropiate command to "
335 "decompress this type of comic book"));
340 comics_document_load (EvDocument *document,
344 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
345 GSList *supported_extensions;
353 comics_document->archive = g_filename_from_uri (uri, NULL, error);
354 if (!comics_document->archive)
357 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
360 g_propagate_error (error, err);
362 g_set_error_literal (error,
364 EV_DOCUMENT_ERROR_INVALID,
365 _("Unknown MIME Type"));
371 if (!comics_check_decompress_command (mime_type, comics_document,
375 } else if (!comics_generate_command_lines (comics_document, error)) {
382 /* Get list of files in archive */
383 success = g_spawn_command_line_sync (comics_document->list_command,
384 &std_out, NULL, &retval, error);
388 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
389 g_set_error_literal (error,
391 EV_DOCUMENT_ERROR_INVALID,
392 _("File corrupted"));
396 /* FIXME: is this safe against filenames containing \n in the archive ? */
397 cbr_files = g_strsplit (std_out, "\n", 0);
401 g_set_error_literal (error,
403 EV_DOCUMENT_ERROR_INVALID,
404 _("No files in archive"));
408 supported_extensions = get_supported_image_extensions ();
409 for (i = 0; cbr_files[i] != NULL; i++) {
410 gchar *suffix = g_strrstr (cbr_files[i], ".");
413 suffix = g_ascii_strdown (suffix + 1, -1);
415 if (g_slist_find_custom (supported_extensions, suffix,
416 (GCompareFunc) strcmp) != NULL) {
417 comics_document->page_names =
418 g_slist_insert_sorted (
419 comics_document->page_names,
420 g_strdup (g_strstrip (cbr_files[i])),
421 (GCompareFunc) strcmp);
422 comics_document->n_pages++;
428 g_strfreev (cbr_files);
429 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
430 g_slist_free (supported_extensions);
432 if (comics_document->n_pages == 0) {
435 EV_DOCUMENT_ERROR_INVALID,
436 _("No images found in archive %s"),
446 comics_document_save (EvDocument *document,
450 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
452 return ev_xfer_uri_simple (comics_document->archive, uri, error);
456 comics_document_get_n_pages (EvDocument *document)
458 return COMICS_DOCUMENT (document)->n_pages;
462 comics_document_get_page_size (EvDocument *document,
467 GdkPixbufLoader *loader;
470 gboolean success, got_size = FALSE;
476 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
478 if (!comics_document->decompress_tmp) {
479 argv = extract_argv (document, page->index);
480 success = g_spawn_async_with_pipes (NULL, argv, NULL,
481 G_SPAWN_SEARCH_PATH |
482 G_SPAWN_STDERR_TO_DEV_NULL,
485 NULL, &outpipe, NULL, NULL);
487 g_return_if_fail (success == TRUE);
489 loader = gdk_pixbuf_loader_new ();
490 g_signal_connect (loader, "area-prepared",
491 G_CALLBACK (get_page_size_area_prepared_cb),
494 while (outpipe >= 0) {
495 bytes = read (outpipe, buf, 1024);
498 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
499 if (bytes <= 0 || got_size) {
502 gdk_pixbuf_loader_close (loader, NULL);
506 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
507 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
509 *width = gdk_pixbuf_get_width (pixbuf);
511 *height = gdk_pixbuf_get_height (pixbuf);
514 g_spawn_close_pid (child_pid);
515 g_object_unref (loader);
517 filename = g_build_filename (comics_document->dir,
518 (char*) g_slist_nth_data (
519 comics_document->page_names,
522 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
525 *width = gdk_pixbuf_get_width (pixbuf);
527 *height = gdk_pixbuf_get_height (pixbuf);
532 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
535 gboolean *got_size = data;
540 comics_document_render_pixbuf (EvDocument *document,
543 GdkPixbufLoader *loader;
544 GdkPixbuf *rotated_pixbuf;
553 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
555 if (!comics_document->decompress_tmp) {
556 argv = extract_argv (document, rc->page->index);
557 success = g_spawn_async_with_pipes (NULL, argv, NULL,
558 G_SPAWN_SEARCH_PATH |
559 G_SPAWN_STDERR_TO_DEV_NULL,
562 NULL, &outpipe, NULL, NULL);
564 g_return_val_if_fail (success == TRUE, NULL);
566 loader = gdk_pixbuf_loader_new ();
567 g_signal_connect (loader, "size-prepared",
568 G_CALLBACK (render_pixbuf_size_prepared_cb),
571 while (outpipe >= 0) {
572 bytes = read (outpipe, buf, 4096);
575 gdk_pixbuf_loader_write (loader, buf, bytes,
577 } else if (bytes <= 0) {
579 gdk_pixbuf_loader_close (loader, NULL);
584 rotated_pixbuf = gdk_pixbuf_rotate_simple (
585 gdk_pixbuf_loader_get_pixbuf (loader),
587 g_spawn_close_pid (child_pid);
588 g_object_unref (loader);
591 g_build_filename (comics_document->dir,
592 (char*) g_slist_nth_data (
593 comics_document->page_names,
597 gdk_pixbuf_get_file_info (filename, &width, &height);
600 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
601 filename, width * (rc->scale) + 0.5,
602 height * (rc->scale) + 0.5, NULL),
607 return rotated_pixbuf;
610 static cairo_surface_t *
611 comics_document_render (EvDocument *document,
615 cairo_surface_t *surface;
617 pixbuf = comics_document_render_pixbuf (document, rc);
618 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
619 g_object_unref (pixbuf);
625 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
630 double *scale = data;
631 int w = (width * (*scale) + 0.5);
632 int h = (height * (*scale) + 0.5);
634 gdk_pixbuf_loader_set_size (loader, w, h);
638 * comics_remove_dir: Removes a directory recursively.
640 * 0 if it was successfully deleted,
641 * -1 if an error occurred
644 comics_remove_dir (gchar *path_name)
647 const gchar *filename;
648 gchar *filename_with_path;
650 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
651 content_dir = g_dir_open (path_name, 0, NULL);
652 filename = g_dir_read_name (content_dir);
655 g_build_filename (path_name,
657 comics_remove_dir (filename_with_path);
658 g_free (filename_with_path);
659 filename = g_dir_read_name (content_dir);
661 g_dir_close (content_dir);
663 /* Note from g_remove() documentation: on Windows, it is in general not
664 * possible to remove a file that is open to some process, or mapped
666 return (g_remove (path_name));
670 comics_document_finalize (GObject *object)
672 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
674 if (comics_document->decompress_tmp) {
675 if (comics_remove_dir (comics_document->dir) == -1)
676 g_warning (_("There was an error deleting “%s”."),
677 comics_document->dir);
678 g_free (comics_document->dir);
679 g_remove (ev_tmp_dir ());
682 if (comics_document->page_names) {
683 g_slist_foreach (comics_document->page_names,
684 (GFunc) g_free, NULL);
685 g_slist_free (comics_document->page_names);
688 g_free (comics_document->archive);
689 g_free (comics_document->selected_command);
690 g_free (comics_document->extract_command);
691 g_free (comics_document->list_command);
693 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
697 comics_document_class_init (ComicsDocumentClass *klass)
699 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
700 gobject_class->finalize = comics_document_finalize;
703 static EvDocumentInfo *
704 comics_document_get_info (EvDocument *document)
706 EvDocumentInfo *info;
707 info = g_new0 (EvDocumentInfo, 1);
712 comics_document_document_iface_init (EvDocumentIface *iface)
714 iface->load = comics_document_load;
715 iface->save = comics_document_save;
716 iface->get_n_pages = comics_document_get_n_pages;
717 iface->get_page_size = comics_document_get_page_size;
718 iface->render = comics_document_render;
719 iface->get_info = comics_document_get_info;
723 comics_document_init (ComicsDocument *comics_document)
725 comics_document->archive = NULL;
726 comics_document->page_names = NULL;
727 comics_document->extract_command = NULL;
728 comics_document->n_pages = 0;
731 /* Returns a list of file extensions supported by gdk-pixbuf */
733 get_supported_image_extensions()
735 GSList *extensions = NULL;
736 GSList *formats = gdk_pixbuf_get_formats ();
739 for (l = formats; l != NULL; l = l->next) {
741 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
743 for (i = 0; ext[i] != NULL; i++) {
744 extensions = g_slist_append (extensions,
751 g_slist_free (formats);
756 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
760 GdkPixbuf *thumbnail;
762 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
765 GdkPixbuf *tmp_pixbuf = thumbnail;
767 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
768 g_object_unref (tmp_pixbuf);
775 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
780 gdouble page_width, page_height;
782 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
783 &page_width, &page_height);
785 if (rc->rotation == 90 || rc->rotation == 270) {
786 *width = (gint) (page_height * rc->scale);
787 *height = (gint) (page_width * rc->scale);
789 *width = (gint) (page_width * rc->scale);
790 *height = (gint) (page_height * rc->scale);
795 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
797 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
798 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
802 extract_argv (EvDocument *document, gint page)
804 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
806 char *command_line, *quoted_archive, *quoted_filename;
809 quoted_archive = g_shell_quote (comics_document->archive);
810 if (comics_document->regex_arg) {
811 quoted_filename = comics_regex_quote (
812 g_slist_nth_data (comics_document->page_names, page));
814 quoted_filename = g_shell_quote (
815 g_slist_nth_data (comics_document->page_names, page));
818 command_line = g_strdup_printf ("%s -- %s %s",
819 comics_document->extract_command,
823 g_shell_parse_argv (command_line, NULL, &argv, &err);
826 g_warning (_("Error %s"), err->message);
831 g_free (command_line);
832 g_free (quoted_archive);
833 g_free (quoted_filename);