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,
186 gchar *quoted_file, *checksum;
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) {
202 g_compute_checksum_for_string (G_CHECKSUM_MD5,
203 comics_document->archive,
205 comics_document->dir = g_build_filename (ev_tmp_dir (),
207 comics_document->decompress_tmp =
208 g_strdup_printf (command_usage_def[type].decompress_tmp,
209 comics_document->selected_command,
211 comics_document->dir);
212 g_free (quoted_file);
214 /* unrar-free can't create directories so we do it on its
216 if (type == GNAUNRAR) {
217 if (g_mkdir_with_parents (comics_document->dir, 0700) !=
222 EV_DOCUMENT_ERROR_INVALID,
223 _("Failed to create a temporary "
225 g_warning ("Failed to create directory %s: %s",
226 comics_document->dir,
232 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
233 comics_document->selected_command, error))
238 g_free (quoted_file);
244 /* This function chooses an external command for decompressing a comic
245 * book based on its mime tipe. */
247 comics_check_decompress_command (gchar *mime_type,
248 ComicsDocument *comics_document,
252 gchar *std_out, *std_err;
256 /* FIXME, use proper cbr/cbz mime types once they're
257 * included in shared-mime-info */
259 if (!strcmp (mime_type, "application/x-cbr") ||
260 !strcmp (mime_type, "application/x-rar")) {
261 /* The RARLAB provides a no-charge proprietary (freeware)
262 * decompress-only client for Linux called unrar. Another
263 * option is a GPLv2-licensed command-line tool developed by
264 * the Gna! project. Confusingly enough, the free software RAR
265 * decoder is also named unrar. For this reason we need to add
266 * some lines for disambiguation. Sorry for the added the
267 * complexity but it's life :)
268 * Finally, some distributions, like Debian, rename this free
269 * option as unrar-free.
271 comics_document->selected_command =
272 g_find_program_in_path ("unrar");
273 if (comics_document->selected_command) {
274 /* We only use std_err to avoid printing useless error
275 * messages on the terminal */
277 g_spawn_command_line_sync (
278 comics_document->selected_command,
282 g_propagate_error (error, err);
285 /* I don't check retval status because RARLAB unrar
286 * doesn't have a way to return 0 without involving an
287 * operation with a file*/
288 } else if (WIFEXITED (retval)) {
289 if (g_strrstr (std_out,"freeware") != NULL)
290 /* The RARLAB freeware client */
291 comics_document->command_usage = RARLABS;
293 /* The Gna! free software client */
294 comics_document->command_usage = GNAUNRAR;
301 /* The Gna! free software client with Debian naming convention */
302 comics_document->selected_command =
303 g_find_program_in_path ("unrar-free");
304 if (comics_document->selected_command) {
305 comics_document->command_usage = GNAUNRAR;
309 } else if (!strcmp (mime_type, "application/x-cbz") ||
310 !strcmp (mime_type, "application/zip")) {
311 /* InfoZIP's unzip program */
312 comics_document->selected_command =
313 g_find_program_in_path ("unzip");
314 if (comics_document->selected_command) {
315 comics_document->command_usage = UNZIP;
319 } else if (!strcmp (mime_type, "application/x-cb7") ||
320 !strcmp (mime_type, "application/x-7z-compressed")) {
321 /* 7zr is a light stand-alone executable that supports only
323 comics_document->selected_command =
324 g_find_program_in_path ("7zr");
325 if (comics_document->selected_command) {
326 comics_document->command_usage = P7ZIP;
332 EV_DOCUMENT_ERROR_INVALID,
333 _("Not a comic book MIME type: %s"),
337 g_set_error_literal (error,
339 EV_DOCUMENT_ERROR_INVALID,
340 _("Can't find an appropiate command to "
341 "decompress this type of comic book"));
346 comics_document_load (EvDocument *document,
350 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
351 GSList *supported_extensions;
359 comics_document->archive = g_filename_from_uri (uri, NULL, error);
360 if (!comics_document->archive)
363 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
366 g_propagate_error (error, err);
368 g_set_error_literal (error,
370 EV_DOCUMENT_ERROR_INVALID,
371 _("Unknown MIME Type"));
377 if (!comics_check_decompress_command (mime_type, comics_document,
381 } else if (!comics_generate_command_lines (comics_document, error)) {
388 /* Get list of files in archive */
389 success = g_spawn_command_line_sync (comics_document->list_command,
390 &std_out, NULL, &retval, error);
394 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
395 g_set_error_literal (error,
397 EV_DOCUMENT_ERROR_INVALID,
398 _("File corrupted"));
402 /* FIXME: is this safe against filenames containing \n in the archive ? */
403 cbr_files = g_strsplit (std_out, "\n", 0);
407 g_set_error_literal (error,
409 EV_DOCUMENT_ERROR_INVALID,
410 _("No files in archive"));
414 supported_extensions = get_supported_image_extensions ();
415 for (i = 0; cbr_files[i] != NULL; i++) {
416 gchar *suffix = g_strrstr (cbr_files[i], ".");
419 suffix = g_ascii_strdown (suffix + 1, -1);
421 if (g_slist_find_custom (supported_extensions, suffix,
422 (GCompareFunc) strcmp) != NULL) {
423 comics_document->page_names =
424 g_slist_insert_sorted (
425 comics_document->page_names,
426 g_strdup (g_strstrip (cbr_files[i])),
427 (GCompareFunc) strcmp);
428 comics_document->n_pages++;
434 g_strfreev (cbr_files);
435 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
436 g_slist_free (supported_extensions);
438 if (comics_document->n_pages == 0) {
441 EV_DOCUMENT_ERROR_INVALID,
442 _("No images found in archive %s"),
452 comics_document_save (EvDocument *document,
456 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
458 return ev_xfer_uri_simple (comics_document->archive, uri, error);
462 comics_document_get_n_pages (EvDocument *document)
464 return COMICS_DOCUMENT (document)->n_pages;
468 comics_document_get_page_size (EvDocument *document,
473 GdkPixbufLoader *loader;
476 gboolean success, got_size = FALSE;
482 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
484 if (!comics_document->decompress_tmp) {
485 argv = extract_argv (document, page->index);
486 success = g_spawn_async_with_pipes (NULL, argv, NULL,
487 G_SPAWN_SEARCH_PATH |
488 G_SPAWN_STDERR_TO_DEV_NULL,
491 NULL, &outpipe, NULL, NULL);
493 g_return_if_fail (success == TRUE);
495 loader = gdk_pixbuf_loader_new ();
496 g_signal_connect (loader, "area-prepared",
497 G_CALLBACK (get_page_size_area_prepared_cb),
500 while (outpipe >= 0) {
501 bytes = read (outpipe, buf, 1024);
504 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
505 if (bytes <= 0 || got_size) {
508 gdk_pixbuf_loader_close (loader, NULL);
512 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
513 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
515 *width = gdk_pixbuf_get_width (pixbuf);
517 *height = gdk_pixbuf_get_height (pixbuf);
520 g_spawn_close_pid (child_pid);
521 g_object_unref (loader);
523 filename = g_build_filename (comics_document->dir,
524 (char*) g_slist_nth_data (
525 comics_document->page_names,
528 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
531 *width = gdk_pixbuf_get_width (pixbuf);
533 *height = gdk_pixbuf_get_height (pixbuf);
538 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
541 gboolean *got_size = data;
546 comics_document_render_pixbuf (EvDocument *document,
549 GdkPixbufLoader *loader;
550 GdkPixbuf *rotated_pixbuf;
559 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
561 if (!comics_document->decompress_tmp) {
562 argv = extract_argv (document, rc->page->index);
563 success = g_spawn_async_with_pipes (NULL, argv, NULL,
564 G_SPAWN_SEARCH_PATH |
565 G_SPAWN_STDERR_TO_DEV_NULL,
568 NULL, &outpipe, NULL, NULL);
570 g_return_val_if_fail (success == TRUE, NULL);
572 loader = gdk_pixbuf_loader_new ();
573 g_signal_connect (loader, "size-prepared",
574 G_CALLBACK (render_pixbuf_size_prepared_cb),
577 while (outpipe >= 0) {
578 bytes = read (outpipe, buf, 4096);
581 gdk_pixbuf_loader_write (loader, buf, bytes,
583 } else if (bytes <= 0) {
585 gdk_pixbuf_loader_close (loader, NULL);
590 rotated_pixbuf = gdk_pixbuf_rotate_simple (
591 gdk_pixbuf_loader_get_pixbuf (loader),
593 g_spawn_close_pid (child_pid);
594 g_object_unref (loader);
597 g_build_filename (comics_document->dir,
598 (char*) g_slist_nth_data (
599 comics_document->page_names,
603 gdk_pixbuf_get_file_info (filename, &width, &height);
606 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
607 filename, width * (rc->scale) + 0.5,
608 height * (rc->scale) + 0.5, NULL),
613 return rotated_pixbuf;
616 static cairo_surface_t *
617 comics_document_render (EvDocument *document,
621 cairo_surface_t *surface;
623 pixbuf = comics_document_render_pixbuf (document, rc);
624 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
625 g_object_unref (pixbuf);
631 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
636 double *scale = data;
637 int w = (width * (*scale) + 0.5);
638 int h = (height * (*scale) + 0.5);
640 gdk_pixbuf_loader_set_size (loader, w, h);
644 * comics_remove_dir: Removes a directory recursively.
646 * 0 if it was successfully deleted,
647 * -1 if an error occurred
650 comics_remove_dir (gchar *path_name)
653 const gchar *filename;
654 gchar *filename_with_path;
656 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
657 content_dir = g_dir_open (path_name, 0, NULL);
658 filename = g_dir_read_name (content_dir);
661 g_build_filename (path_name,
663 comics_remove_dir (filename_with_path);
664 g_free (filename_with_path);
665 filename = g_dir_read_name (content_dir);
667 g_dir_close (content_dir);
669 /* Note from g_remove() documentation: on Windows, it is in general not
670 * possible to remove a file that is open to some process, or mapped
672 return (g_remove (path_name));
676 comics_document_finalize (GObject *object)
678 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
680 if (comics_document->decompress_tmp) {
681 if (comics_remove_dir (comics_document->dir) == -1)
682 g_warning (_("There was an error deleting “%s”."),
683 comics_document->dir);
684 g_free (comics_document->dir);
685 g_remove (ev_tmp_dir ());
688 if (comics_document->page_names) {
689 g_slist_foreach (comics_document->page_names,
690 (GFunc) g_free, NULL);
691 g_slist_free (comics_document->page_names);
694 g_free (comics_document->archive);
695 g_free (comics_document->selected_command);
696 g_free (comics_document->extract_command);
697 g_free (comics_document->list_command);
699 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
703 comics_document_class_init (ComicsDocumentClass *klass)
705 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
706 gobject_class->finalize = comics_document_finalize;
709 static EvDocumentInfo *
710 comics_document_get_info (EvDocument *document)
712 EvDocumentInfo *info;
713 info = g_new0 (EvDocumentInfo, 1);
718 comics_document_document_iface_init (EvDocumentIface *iface)
720 iface->load = comics_document_load;
721 iface->save = comics_document_save;
722 iface->get_n_pages = comics_document_get_n_pages;
723 iface->get_page_size = comics_document_get_page_size;
724 iface->render = comics_document_render;
725 iface->get_info = comics_document_get_info;
729 comics_document_init (ComicsDocument *comics_document)
731 comics_document->archive = NULL;
732 comics_document->page_names = NULL;
733 comics_document->extract_command = NULL;
734 comics_document->n_pages = 0;
737 /* Returns a list of file extensions supported by gdk-pixbuf */
739 get_supported_image_extensions()
741 GSList *extensions = NULL;
742 GSList *formats = gdk_pixbuf_get_formats ();
745 for (l = formats; l != NULL; l = l->next) {
747 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
749 for (i = 0; ext[i] != NULL; i++) {
750 extensions = g_slist_append (extensions,
757 g_slist_free (formats);
762 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
766 GdkPixbuf *thumbnail;
768 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
771 GdkPixbuf *tmp_pixbuf = thumbnail;
773 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
774 g_object_unref (tmp_pixbuf);
781 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
786 gdouble page_width, page_height;
788 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
789 &page_width, &page_height);
791 if (rc->rotation == 90 || rc->rotation == 270) {
792 *width = (gint) (page_height * rc->scale);
793 *height = (gint) (page_width * rc->scale);
795 *width = (gint) (page_width * rc->scale);
796 *height = (gint) (page_height * rc->scale);
801 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
803 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
804 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
808 extract_argv (EvDocument *document, gint page)
810 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
812 char *command_line, *quoted_archive, *quoted_filename;
815 quoted_archive = g_shell_quote (comics_document->archive);
816 if (comics_document->regex_arg) {
817 quoted_filename = comics_regex_quote (
818 g_slist_nth_data (comics_document->page_names, page));
820 quoted_filename = g_shell_quote (
821 g_slist_nth_data (comics_document->page_names, page));
824 command_line = g_strdup_printf ("%s -- %s %s",
825 comics_document->extract_command,
829 g_shell_parse_argv (command_line, NULL, &argv, &err);
832 g_warning (_("Error %s"), err->message);
837 g_free (command_line);
838 g_free (quoted_archive);
839 g_free (quoted_filename);