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 gchar *quoted_command;
190 ComicBookDecompressType type;
192 type = comics_document->command_usage;
193 quoted_file = g_shell_quote (comics_document->archive);
194 quoted_command = g_shell_quote (comics_document->selected_command);
196 comics_document->extract_command =
197 g_strdup_printf (command_usage_def[type].extract,
199 comics_document->list_command =
200 g_strdup_printf (command_usage_def[type].list,
201 quoted_command, quoted_file);
202 comics_document->offset = command_usage_def[type].offset;
203 if (command_usage_def[type].decompress_tmp) {
204 comics_document->dir = ev_mkdtemp ("evince-comics-XXXXXX", error);
205 if (comics_document->dir == NULL)
208 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
210 comics_document->decompress_tmp =
211 g_strdup_printf (command_usage_def[type].decompress_tmp,
212 quoted_command, quoted_file,
213 comics_document->dir);
214 g_free (quoted_file);
215 g_free (quoted_command);
217 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
218 comics_document->selected_command, error))
223 g_free (quoted_file);
224 g_free (quoted_command);
230 /* This function chooses an external command for decompressing a comic
231 * book based on its mime tipe. */
233 comics_check_decompress_command (gchar *mime_type,
234 ComicsDocument *comics_document,
238 gchar *std_out, *std_err;
242 /* FIXME, use proper cbr/cbz mime types once they're
243 * included in shared-mime-info */
245 if (!strcmp (mime_type, "application/x-cbr") ||
246 !strcmp (mime_type, "application/x-rar")) {
247 /* The RARLAB provides a no-charge proprietary (freeware)
248 * decompress-only client for Linux called unrar. Another
249 * option is a GPLv2-licensed command-line tool developed by
250 * the Gna! project. Confusingly enough, the free software RAR
251 * decoder is also named unrar. For this reason we need to add
252 * some lines for disambiguation. Sorry for the added the
253 * complexity but it's life :)
254 * Finally, some distributions, like Debian, rename this free
255 * option as unrar-free.
257 comics_document->selected_command =
258 g_find_program_in_path ("unrar");
259 if (comics_document->selected_command) {
260 /* We only use std_err to avoid printing useless error
261 * messages on the terminal */
263 g_spawn_command_line_sync (
264 comics_document->selected_command,
268 g_propagate_error (error, err);
271 /* I don't check retval status because RARLAB unrar
272 * doesn't have a way to return 0 without involving an
273 * operation with a file*/
274 } else if (WIFEXITED (retval)) {
275 if (g_strrstr (std_out,"freeware") != NULL)
276 /* The RARLAB freeware client */
277 comics_document->command_usage = RARLABS;
279 /* The Gna! free software client */
280 comics_document->command_usage = GNAUNRAR;
287 /* The Gna! free software client with Debian naming convention */
288 comics_document->selected_command =
289 g_find_program_in_path ("unrar-free");
290 if (comics_document->selected_command) {
291 comics_document->command_usage = GNAUNRAR;
295 } else if (!strcmp (mime_type, "application/x-cbz") ||
296 !strcmp (mime_type, "application/zip")) {
297 /* InfoZIP's unzip program */
298 comics_document->selected_command =
299 g_find_program_in_path ("unzip");
300 if (comics_document->selected_command) {
301 comics_document->command_usage = UNZIP;
305 } else if (!strcmp (mime_type, "application/x-cb7") ||
306 !strcmp (mime_type, "application/x-7z-compressed")) {
307 /* 7zr, 7za and 7z are the commands from the p7zip project able
308 * to decompress .7z files */
309 comics_document->selected_command =
310 g_find_program_in_path ("7zr");
311 if (comics_document->selected_command) {
312 comics_document->command_usage = P7ZIP;
315 comics_document->selected_command =
316 g_find_program_in_path ("7za");
317 if (comics_document->selected_command) {
318 comics_document->command_usage = P7ZIP;
321 comics_document->selected_command =
322 g_find_program_in_path ("7z");
323 if (comics_document->selected_command) {
324 comics_document->command_usage = P7ZIP;
327 } else if (!strcmp (mime_type, "application/x-cbt") ||
328 !strcmp (mime_type, "application/x-tar")) {
329 /* tar utility (Tape ARchive) */
330 comics_document->selected_command =
331 g_find_program_in_path ("tar");
332 if (comics_document->selected_command) {
333 comics_document->command_usage = TAR;
339 EV_DOCUMENT_ERROR_INVALID,
340 _("Not a comic book MIME type: %s"),
344 g_set_error_literal (error,
346 EV_DOCUMENT_ERROR_INVALID,
347 _("Can't find an appropriate command to "
348 "decompress this type of comic book"));
353 sort_page_names (gconstpointer a,
356 return strcmp (* (const char **) a, * (const char **) b);
360 comics_document_load (EvDocument *document,
364 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
365 GSList *supported_extensions;
368 gchar **cb_files, *cb_file;
373 comics_document->archive = g_filename_from_uri (uri, NULL, error);
374 if (!comics_document->archive)
377 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
380 g_propagate_error (error, err);
382 g_set_error_literal (error,
384 EV_DOCUMENT_ERROR_INVALID,
385 _("Unknown MIME Type"));
391 if (!comics_check_decompress_command (mime_type, comics_document,
395 } else if (!comics_generate_command_lines (comics_document, error)) {
402 /* Get list of files in archive */
403 success = g_spawn_command_line_sync (comics_document->list_command,
404 &std_out, NULL, &retval, error);
408 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
409 g_set_error_literal (error,
411 EV_DOCUMENT_ERROR_INVALID,
412 _("File corrupted"));
416 /* FIXME: is this safe against filenames containing \n in the archive ? */
417 cb_files = g_strsplit (std_out, "\n", 0);
421 g_set_error_literal (error,
423 EV_DOCUMENT_ERROR_INVALID,
424 _("No files in archive"));
428 comics_document->page_names = g_ptr_array_sized_new (64);
430 supported_extensions = get_supported_image_extensions ();
431 for (i = 0; cb_files[i] != NULL; i++) {
432 if (comics_document->offset != NO_OFFSET) {
433 if (g_utf8_strlen (cb_files[i],-1) >
434 comics_document->offset) {
436 g_utf8_offset_to_pointer (cb_files[i],
437 comics_document->offset);
442 cb_file = cb_files[i];
444 gchar *suffix = g_strrstr (cb_file, ".");
447 suffix = g_ascii_strdown (suffix + 1, -1);
448 if (g_slist_find_custom (supported_extensions, suffix,
449 (GCompareFunc) strcmp) != NULL) {
450 g_ptr_array_add (comics_document->page_names,
451 g_strstrip (g_strdup (cb_file)));
455 g_strfreev (cb_files);
456 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
457 g_slist_free (supported_extensions);
459 if (comics_document->page_names->len == 0) {
462 EV_DOCUMENT_ERROR_INVALID,
463 _("No images found in archive %s"),
468 /* Now sort the pages */
469 g_ptr_array_sort (comics_document->page_names, sort_page_names);
476 comics_document_save (EvDocument *document,
480 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
482 return ev_xfer_uri_simple (comics_document->archive, uri, error);
486 comics_document_get_n_pages (EvDocument *document)
488 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
490 if (comics_document->page_names == NULL)
493 return comics_document->page_names->len;
497 comics_document_get_page_size (EvDocument *document,
502 GdkPixbufLoader *loader;
505 gboolean success, got_size = FALSE;
511 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
513 if (!comics_document->decompress_tmp) {
514 argv = extract_argv (document, page->index);
515 success = g_spawn_async_with_pipes (NULL, argv, NULL,
516 G_SPAWN_SEARCH_PATH |
517 G_SPAWN_STDERR_TO_DEV_NULL,
520 NULL, &outpipe, NULL, NULL);
522 g_return_if_fail (success == TRUE);
524 loader = gdk_pixbuf_loader_new ();
525 g_signal_connect (loader, "area-prepared",
526 G_CALLBACK (get_page_size_area_prepared_cb),
529 while (outpipe >= 0) {
530 bytes = read (outpipe, buf, 1024);
533 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
534 if (bytes <= 0 || got_size) {
537 gdk_pixbuf_loader_close (loader, NULL);
541 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
542 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
544 *width = gdk_pixbuf_get_width (pixbuf);
546 *height = gdk_pixbuf_get_height (pixbuf);
549 g_spawn_close_pid (child_pid);
550 g_object_unref (loader);
552 filename = g_build_filename (comics_document->dir,
553 (char *) comics_document->page_names->pdata[page->index],
555 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
558 *width = gdk_pixbuf_get_width (pixbuf);
560 *height = gdk_pixbuf_get_height (pixbuf);
565 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
568 gboolean *got_size = data;
573 comics_document_render_pixbuf (EvDocument *document,
576 GdkPixbufLoader *loader;
577 GdkPixbuf *rotated_pixbuf;
586 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
588 if (!comics_document->decompress_tmp) {
589 argv = extract_argv (document, rc->page->index);
590 success = g_spawn_async_with_pipes (NULL, argv, NULL,
591 G_SPAWN_SEARCH_PATH |
592 G_SPAWN_STDERR_TO_DEV_NULL,
595 NULL, &outpipe, NULL, NULL);
597 g_return_val_if_fail (success == TRUE, NULL);
599 loader = gdk_pixbuf_loader_new ();
600 g_signal_connect (loader, "size-prepared",
601 G_CALLBACK (render_pixbuf_size_prepared_cb),
604 while (outpipe >= 0) {
605 bytes = read (outpipe, buf, 4096);
608 gdk_pixbuf_loader_write (loader, buf, bytes,
610 } else if (bytes <= 0) {
612 gdk_pixbuf_loader_close (loader, NULL);
617 rotated_pixbuf = gdk_pixbuf_rotate_simple (
618 gdk_pixbuf_loader_get_pixbuf (loader),
620 g_spawn_close_pid (child_pid);
621 g_object_unref (loader);
624 g_build_filename (comics_document->dir,
625 (char *) comics_document->page_names->pdata[rc->page->index],
628 gdk_pixbuf_get_file_info (filename, &width, &height);
631 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
632 filename, width * (rc->scale) + 0.5,
633 height * (rc->scale) + 0.5, NULL),
638 return rotated_pixbuf;
641 static cairo_surface_t *
642 comics_document_render (EvDocument *document,
646 cairo_surface_t *surface;
648 pixbuf = comics_document_render_pixbuf (document, rc);
649 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
650 g_object_unref (pixbuf);
656 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
661 double *scale = data;
662 int w = (width * (*scale) + 0.5);
663 int h = (height * (*scale) + 0.5);
665 gdk_pixbuf_loader_set_size (loader, w, h);
669 * comics_remove_dir: Removes a directory recursively.
671 * 0 if it was successfully deleted,
672 * -1 if an error occurred
675 comics_remove_dir (gchar *path_name)
678 const gchar *filename;
679 gchar *filename_with_path;
681 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
682 content_dir = g_dir_open (path_name, 0, NULL);
683 filename = g_dir_read_name (content_dir);
686 g_build_filename (path_name,
688 comics_remove_dir (filename_with_path);
689 g_free (filename_with_path);
690 filename = g_dir_read_name (content_dir);
692 g_dir_close (content_dir);
694 /* Note from g_remove() documentation: on Windows, it is in general not
695 * possible to remove a file that is open to some process, or mapped
697 return (g_remove (path_name));
701 comics_document_finalize (GObject *object)
703 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
705 if (comics_document->decompress_tmp) {
706 if (comics_remove_dir (comics_document->dir) == -1)
707 g_warning (_("There was an error deleting “%s”."),
708 comics_document->dir);
709 g_free (comics_document->dir);
712 if (comics_document->page_names) {
713 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
714 g_ptr_array_free (comics_document->page_names, TRUE);
717 g_free (comics_document->archive);
718 g_free (comics_document->selected_command);
719 g_free (comics_document->extract_command);
720 g_free (comics_document->list_command);
722 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
726 comics_document_class_init (ComicsDocumentClass *klass)
728 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
729 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
731 gobject_class->finalize = comics_document_finalize;
733 ev_document_class->load = comics_document_load;
734 ev_document_class->save = comics_document_save;
735 ev_document_class->get_n_pages = comics_document_get_n_pages;
736 ev_document_class->get_page_size = comics_document_get_page_size;
737 ev_document_class->render = comics_document_render;
741 comics_document_init (ComicsDocument *comics_document)
743 comics_document->archive = NULL;
744 comics_document->page_names = NULL;
745 comics_document->extract_command = NULL;
748 /* Returns a list of file extensions supported by gdk-pixbuf */
750 get_supported_image_extensions()
752 GSList *extensions = NULL;
753 GSList *formats = gdk_pixbuf_get_formats ();
756 for (l = formats; l != NULL; l = l->next) {
758 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
760 for (i = 0; ext[i] != NULL; i++) {
761 extensions = g_slist_append (extensions,
768 g_slist_free (formats);
773 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
777 GdkPixbuf *thumbnail;
779 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
782 GdkPixbuf *tmp_pixbuf = thumbnail;
784 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
785 g_object_unref (tmp_pixbuf);
792 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
797 gdouble page_width, page_height;
799 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
800 &page_width, &page_height);
802 if (rc->rotation == 90 || rc->rotation == 270) {
803 *width = (gint) (page_height * rc->scale);
804 *height = (gint) (page_width * rc->scale);
806 *width = (gint) (page_width * rc->scale);
807 *height = (gint) (page_height * rc->scale);
812 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
814 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
815 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
819 extract_argv (EvDocument *document, gint page)
821 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
823 char *command_line, *quoted_archive, *quoted_filename;
826 if (page >= comics_document->page_names->len)
829 quoted_archive = g_shell_quote (comics_document->archive);
830 quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
832 command_line = g_strdup_printf ("%s %s %s",
833 comics_document->extract_command,
836 g_shell_parse_argv (command_line, NULL, &argv, &err);
839 g_warning (_("Error %s"), err->message);
844 g_free (command_line);
845 g_free (quoted_archive);
846 g_free (quoted_filename);