1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2009, Juanjo Marín <juanj.marin@juntadeandalucia.es>
4 * Copyright (C) 2005, Teemu Tervo <teemu.tervo@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <glib/gi18n-lib.h>
30 #include <glib/gstdio.h>
34 # define WIFEXITED(x) ((x) != 3)
35 # define WEXITSTATUS(x) (x)
37 # include <sys/wait.h>
40 #include "comics-document.h"
41 #include "ev-document-misc.h"
42 #include "ev-document-thumbnails.h"
43 #include "ev-file-helpers.h"
52 } ComicBookDecompressType;
54 typedef struct _ComicsDocumentClass ComicsDocumentClass;
56 struct _ComicsDocumentClass
58 EvDocumentClass parent_class;
61 struct _ComicsDocument
63 EvDocument parent_instance;
66 GPtrArray *page_names;
67 gchar *selected_command;
68 gchar *extract_command, *list_command, *decompress_tmp;
70 ComicBookDecompressType command_usage;
76 /* For perfomance reasons of 7z* we've choosen to decompress on the temporary
77 * directory instead of decompressing on the stdout */
80 * @extract: command line arguments to pass to extract a file from the archive
82 * @list: command line arguments to list the archive contents
83 * @decompress_tmp: command line arguments to pass to extract the archive
85 * @offset: the position offset of the filename on each line in the output of
86 * running the @list command
93 } ComicBookDecompressCommand;
95 static const ComicBookDecompressCommand command_usage_def[] = {
97 {"%s p -c- -ierr --", "%s vb -c- -- %s", NULL , NO_OFFSET},
100 {NULL , "%s t %s" , "%s -xf %s %s" , NO_OFFSET},
103 {"%s -p -C --" , "%s -Z -1 -- %s" , NULL , NO_OFFSET},
106 {NULL , "%s l -- %s" , "%s x -y %s -o%s", OFFSET_7Z},
109 {"%s -xOf" , "%s -tf %s" , NULL , NO_OFFSET}
112 static void comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
114 static GSList* get_supported_image_extensions (void);
115 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
117 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
121 static char** extract_argv (EvDocument *document,
125 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
127 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
128 comics_document_document_thumbnails_iface_init);
132 /* This function manages the command for decompressing a comic book */
134 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
135 const gchar *command,
139 gchar *std_out, *basename;
143 success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
144 NULL, &retval, &err);
145 basename = g_path_get_basename (command);
149 EV_DOCUMENT_ERROR_INVALID,
150 _("Error launching the command “%s” in order to "
151 "decompress the comic book: %s"),
155 } else if (WIFEXITED (retval)) {
156 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
163 EV_DOCUMENT_ERROR_INVALID,
164 _("The command “%s” failed at "
165 "decompressing the comic book."),
172 EV_DOCUMENT_ERROR_INVALID,
173 _("The command “%s” did not end normally."),
181 /* This function shows how to use the choosen command for decompressing a
182 * comic book file. It modifies fields of the ComicsDocument struct with
183 * this information */
185 comics_generate_command_lines (ComicsDocument *comics_document,
189 ComicBookDecompressType type;
191 type = comics_document->command_usage;
192 quoted_file = g_shell_quote (comics_document->archive);
194 comics_document->extract_command =
195 g_strdup_printf (command_usage_def[type].extract,
196 comics_document->selected_command);
197 comics_document->list_command =
198 g_strdup_printf (command_usage_def[type].list,
199 comics_document->selected_command,
201 comics_document->offset = command_usage_def[type].offset;
202 if (command_usage_def[type].decompress_tmp) {
203 comics_document->dir = ev_mkdtemp ("evince-comics-XXXXXX", error);
204 if (comics_document->dir == NULL)
207 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
209 comics_document->decompress_tmp =
210 g_strdup_printf (command_usage_def[type].decompress_tmp,
211 comics_document->selected_command,
213 comics_document->dir);
214 g_free (quoted_file);
216 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
217 comics_document->selected_command, error))
222 g_free (quoted_file);
228 /* This function chooses an external command for decompressing a comic
229 * book based on its mime tipe. */
231 comics_check_decompress_command (gchar *mime_type,
232 ComicsDocument *comics_document,
236 gchar *std_out, *std_err;
240 /* FIXME, use proper cbr/cbz mime types once they're
241 * included in shared-mime-info */
243 if (!strcmp (mime_type, "application/x-cbr") ||
244 !strcmp (mime_type, "application/x-rar")) {
245 /* The RARLAB provides a no-charge proprietary (freeware)
246 * decompress-only client for Linux called unrar. Another
247 * option is a GPLv2-licensed command-line tool developed by
248 * the Gna! project. Confusingly enough, the free software RAR
249 * decoder is also named unrar. For this reason we need to add
250 * some lines for disambiguation. Sorry for the added the
251 * complexity but it's life :)
252 * Finally, some distributions, like Debian, rename this free
253 * option as unrar-free.
255 comics_document->selected_command =
256 g_find_program_in_path ("unrar");
257 if (comics_document->selected_command) {
258 /* We only use std_err to avoid printing useless error
259 * messages on the terminal */
261 g_spawn_command_line_sync (
262 comics_document->selected_command,
266 g_propagate_error (error, err);
269 /* I don't check retval status because RARLAB unrar
270 * doesn't have a way to return 0 without involving an
271 * operation with a file*/
272 } else if (WIFEXITED (retval)) {
273 if (g_strrstr (std_out,"freeware") != NULL)
274 /* The RARLAB freeware client */
275 comics_document->command_usage = RARLABS;
277 /* The Gna! free software client */
278 comics_document->command_usage = GNAUNRAR;
285 /* The Gna! free software client with Debian naming convention */
286 comics_document->selected_command =
287 g_find_program_in_path ("unrar-free");
288 if (comics_document->selected_command) {
289 comics_document->command_usage = GNAUNRAR;
293 } else if (!strcmp (mime_type, "application/x-cbz") ||
294 !strcmp (mime_type, "application/zip")) {
295 /* InfoZIP's unzip program */
296 comics_document->selected_command =
297 g_find_program_in_path ("unzip");
298 if (comics_document->selected_command) {
299 comics_document->command_usage = UNZIP;
303 } else if (!strcmp (mime_type, "application/x-cb7") ||
304 !strcmp (mime_type, "application/x-7z-compressed")) {
305 /* 7zr, 7za and 7z are the commands from the p7zip project able
306 * to decompress .7z files */
307 comics_document->selected_command =
308 g_find_program_in_path ("7zr");
309 if (comics_document->selected_command) {
310 comics_document->command_usage = P7ZIP;
313 comics_document->selected_command =
314 g_find_program_in_path ("7za");
315 if (comics_document->selected_command) {
316 comics_document->command_usage = P7ZIP;
319 comics_document->selected_command =
320 g_find_program_in_path ("7z");
321 if (comics_document->selected_command) {
322 comics_document->command_usage = P7ZIP;
325 } else if (!strcmp (mime_type, "application/x-cbt") ||
326 !strcmp (mime_type, "application/x-tar")) {
327 /* tar utility (Tape ARchive) */
328 comics_document->selected_command =
329 g_find_program_in_path ("tar");
330 if (comics_document->selected_command) {
331 comics_document->command_usage = TAR;
337 EV_DOCUMENT_ERROR_INVALID,
338 _("Not a comic book MIME type: %s"),
342 g_set_error_literal (error,
344 EV_DOCUMENT_ERROR_INVALID,
345 _("Can't find an appropriate command to "
346 "decompress this type of comic book"));
351 sort_page_names (gconstpointer a,
354 return strcmp (* (const char **) a, * (const char **) b);
358 comics_document_load (EvDocument *document,
362 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
363 GSList *supported_extensions;
366 gchar **cb_files, *cb_file;
371 comics_document->archive = g_filename_from_uri (uri, NULL, error);
372 if (!comics_document->archive)
375 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
378 g_propagate_error (error, err);
380 g_set_error_literal (error,
382 EV_DOCUMENT_ERROR_INVALID,
383 _("Unknown MIME Type"));
389 if (!comics_check_decompress_command (mime_type, comics_document,
393 } else if (!comics_generate_command_lines (comics_document, error)) {
400 /* Get list of files in archive */
401 success = g_spawn_command_line_sync (comics_document->list_command,
402 &std_out, NULL, &retval, error);
406 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
407 g_set_error_literal (error,
409 EV_DOCUMENT_ERROR_INVALID,
410 _("File corrupted"));
414 /* FIXME: is this safe against filenames containing \n in the archive ? */
415 cb_files = g_strsplit (std_out, "\n", 0);
419 g_set_error_literal (error,
421 EV_DOCUMENT_ERROR_INVALID,
422 _("No files in archive"));
426 comics_document->page_names = g_ptr_array_sized_new (64);
428 supported_extensions = get_supported_image_extensions ();
429 for (i = 0; cb_files[i] != NULL; i++) {
430 if (comics_document->offset != NO_OFFSET) {
431 if (g_utf8_strlen (cb_files[i],-1) >
432 comics_document->offset) {
434 g_utf8_offset_to_pointer (cb_files[i],
435 comics_document->offset);
440 cb_file = cb_files[i];
442 gchar *suffix = g_strrstr (cb_file, ".");
445 suffix = g_ascii_strdown (suffix + 1, -1);
446 if (g_slist_find_custom (supported_extensions, suffix,
447 (GCompareFunc) strcmp) != NULL) {
448 g_ptr_array_add (comics_document->page_names,
449 g_strstrip (g_strdup (cb_file)));
453 g_strfreev (cb_files);
454 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
455 g_slist_free (supported_extensions);
457 if (comics_document->page_names->len == 0) {
460 EV_DOCUMENT_ERROR_INVALID,
461 _("No images found in archive %s"),
466 /* Now sort the pages */
467 g_ptr_array_sort (comics_document->page_names, sort_page_names);
474 comics_document_save (EvDocument *document,
478 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
480 return ev_xfer_uri_simple (comics_document->archive, uri, error);
484 comics_document_get_n_pages (EvDocument *document)
486 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
488 if (comics_document->page_names == NULL)
491 return comics_document->page_names->len;
495 comics_document_get_page_size (EvDocument *document,
500 GdkPixbufLoader *loader;
503 gboolean success, got_size = FALSE;
509 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
511 if (!comics_document->decompress_tmp) {
512 argv = extract_argv (document, page->index);
513 success = g_spawn_async_with_pipes (NULL, argv, NULL,
514 G_SPAWN_SEARCH_PATH |
515 G_SPAWN_STDERR_TO_DEV_NULL,
518 NULL, &outpipe, NULL, NULL);
520 g_return_if_fail (success == TRUE);
522 loader = gdk_pixbuf_loader_new ();
523 g_signal_connect (loader, "area-prepared",
524 G_CALLBACK (get_page_size_area_prepared_cb),
527 while (outpipe >= 0) {
528 bytes = read (outpipe, buf, 1024);
531 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
532 if (bytes <= 0 || got_size) {
535 gdk_pixbuf_loader_close (loader, NULL);
539 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
540 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
542 *width = gdk_pixbuf_get_width (pixbuf);
544 *height = gdk_pixbuf_get_height (pixbuf);
547 g_spawn_close_pid (child_pid);
548 g_object_unref (loader);
550 filename = g_build_filename (comics_document->dir,
551 (char *) comics_document->page_names->pdata[page->index],
553 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
556 *width = gdk_pixbuf_get_width (pixbuf);
558 *height = gdk_pixbuf_get_height (pixbuf);
563 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
566 gboolean *got_size = data;
571 comics_document_render_pixbuf (EvDocument *document,
574 GdkPixbufLoader *loader;
575 GdkPixbuf *rotated_pixbuf;
584 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
586 if (!comics_document->decompress_tmp) {
587 argv = extract_argv (document, rc->page->index);
588 success = g_spawn_async_with_pipes (NULL, argv, NULL,
589 G_SPAWN_SEARCH_PATH |
590 G_SPAWN_STDERR_TO_DEV_NULL,
593 NULL, &outpipe, NULL, NULL);
595 g_return_val_if_fail (success == TRUE, NULL);
597 loader = gdk_pixbuf_loader_new ();
598 g_signal_connect (loader, "size-prepared",
599 G_CALLBACK (render_pixbuf_size_prepared_cb),
602 while (outpipe >= 0) {
603 bytes = read (outpipe, buf, 4096);
606 gdk_pixbuf_loader_write (loader, buf, bytes,
608 } else if (bytes <= 0) {
610 gdk_pixbuf_loader_close (loader, NULL);
615 rotated_pixbuf = gdk_pixbuf_rotate_simple (
616 gdk_pixbuf_loader_get_pixbuf (loader),
618 g_spawn_close_pid (child_pid);
619 g_object_unref (loader);
622 g_build_filename (comics_document->dir,
623 (char *) comics_document->page_names->pdata[rc->page->index],
626 gdk_pixbuf_get_file_info (filename, &width, &height);
629 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
630 filename, width * (rc->scale) + 0.5,
631 height * (rc->scale) + 0.5, NULL),
636 return rotated_pixbuf;
639 static cairo_surface_t *
640 comics_document_render (EvDocument *document,
644 cairo_surface_t *surface;
646 pixbuf = comics_document_render_pixbuf (document, rc);
647 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
648 g_object_unref (pixbuf);
654 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
659 double *scale = data;
660 int w = (width * (*scale) + 0.5);
661 int h = (height * (*scale) + 0.5);
663 gdk_pixbuf_loader_set_size (loader, w, h);
667 * comics_remove_dir: Removes a directory recursively.
669 * 0 if it was successfully deleted,
670 * -1 if an error occurred
673 comics_remove_dir (gchar *path_name)
676 const gchar *filename;
677 gchar *filename_with_path;
679 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
680 content_dir = g_dir_open (path_name, 0, NULL);
681 filename = g_dir_read_name (content_dir);
684 g_build_filename (path_name,
686 comics_remove_dir (filename_with_path);
687 g_free (filename_with_path);
688 filename = g_dir_read_name (content_dir);
690 g_dir_close (content_dir);
692 /* Note from g_remove() documentation: on Windows, it is in general not
693 * possible to remove a file that is open to some process, or mapped
695 return (g_remove (path_name));
699 comics_document_finalize (GObject *object)
701 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
703 if (comics_document->decompress_tmp) {
704 if (comics_remove_dir (comics_document->dir) == -1)
705 g_warning (_("There was an error deleting “%s”."),
706 comics_document->dir);
707 g_free (comics_document->dir);
710 if (comics_document->page_names) {
711 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
712 g_ptr_array_free (comics_document->page_names, TRUE);
715 g_free (comics_document->archive);
716 g_free (comics_document->selected_command);
717 g_free (comics_document->extract_command);
718 g_free (comics_document->list_command);
720 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
724 comics_document_class_init (ComicsDocumentClass *klass)
726 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
727 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
729 gobject_class->finalize = comics_document_finalize;
731 ev_document_class->load = comics_document_load;
732 ev_document_class->save = comics_document_save;
733 ev_document_class->get_n_pages = comics_document_get_n_pages;
734 ev_document_class->get_page_size = comics_document_get_page_size;
735 ev_document_class->render = comics_document_render;
739 comics_document_init (ComicsDocument *comics_document)
741 comics_document->archive = NULL;
742 comics_document->page_names = NULL;
743 comics_document->extract_command = NULL;
746 /* Returns a list of file extensions supported by gdk-pixbuf */
748 get_supported_image_extensions()
750 GSList *extensions = NULL;
751 GSList *formats = gdk_pixbuf_get_formats ();
754 for (l = formats; l != NULL; l = l->next) {
756 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
758 for (i = 0; ext[i] != NULL; i++) {
759 extensions = g_slist_append (extensions,
766 g_slist_free (formats);
771 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
775 GdkPixbuf *thumbnail;
777 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
780 GdkPixbuf *tmp_pixbuf = thumbnail;
782 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
783 g_object_unref (tmp_pixbuf);
790 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
795 gdouble page_width, page_height;
797 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
798 &page_width, &page_height);
800 if (rc->rotation == 90 || rc->rotation == 270) {
801 *width = (gint) (page_height * rc->scale);
802 *height = (gint) (page_width * rc->scale);
804 *width = (gint) (page_width * rc->scale);
805 *height = (gint) (page_height * rc->scale);
810 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
812 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
813 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
817 extract_argv (EvDocument *document, gint page)
819 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
821 char *command_line, *quoted_archive, *quoted_filename;
824 if (page >= comics_document->page_names->len)
827 quoted_archive = g_shell_quote (comics_document->archive);
828 quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
830 command_line = g_strdup_printf ("%s %s %s",
831 comics_document->extract_command,
834 g_shell_parse_argv (command_line, NULL, &argv, &err);
837 g_warning (_("Error %s"), err->message);
842 g_free (command_line);
843 g_free (quoted_archive);
844 g_free (quoted_filename);