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.
29 #include <glib/gi18n-lib.h>
30 #include <glib/gstdio.h>
33 #include "comics-document.h"
34 #include "ev-document-misc.h"
35 #include "ev-document-thumbnails.h"
36 #include "ev-file-helpers.h"
44 } ComicBookDecompressType;
46 typedef struct _ComicsDocumentClass ComicsDocumentClass;
48 struct _ComicsDocumentClass
50 EvDocumentClass parent_class;
53 struct _ComicsDocument
55 EvDocument parent_instance;
58 GPtrArray *page_names;
59 gchar *selected_command;
60 gchar *extract_command, *list_command, *decompress_tmp;
63 ComicBookDecompressType command_usage;
69 /* For perfomance reasons of 7z* we've choosen to decompress on the temporary
70 * directory instead of decompressing on the stdout */
73 * @extract: command line arguments to pass to extract a file from the archive
74 * to stdout. The archive file and the file to extract will be appended after
76 * @list: command line arguments to list the archive contents
77 * @decompress_tmp: command line arguments to pass to extract the archive
78 * into a directory. The archive file and the directory to extract to will be
79 * appended after a "--".
80 * @regex_arg: whether the command expects one filename or accepts a regex (glob?)
81 * @offset: the byte offset of the filename on each line in the output of
82 * running the @list command
90 } ComicBookDecompressCommand;
92 static const ComicBookDecompressCommand command_usage_def[] = {
94 {"%s p -c- -ierr", "%s vb -c- -- %s", NULL , FALSE, NO_OFFSET},
97 {NULL , "%s t %s" , "%s -xf %s %s" , TRUE , NO_OFFSET},
100 {"%s -p -C" , "%s -Z -1 -- %s" , NULL , TRUE , NO_OFFSET},
103 {NULL , "%s l -- %s" , "%s x -y %s -o%s", FALSE, OFFSET_7Z}
106 static void comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
108 static GSList* get_supported_image_extensions (void);
109 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
111 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
115 static char** extract_argv (EvDocument *document,
119 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
121 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
122 comics_document_document_thumbnails_iface_init);
126 comics_regex_quote (const char *s)
130 d = ret = g_malloc (strlen (s) * 4 + 3);
134 for (; *s; s++, d++) {
159 /* This function manages the command for decompressing a comic book */
161 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
162 const gchar *command,
166 gchar *std_out, *basename;
170 success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
171 NULL, &retval, &err);
172 basename = g_path_get_basename (command);
176 EV_DOCUMENT_ERROR_INVALID,
177 _("Error launching the command “%s” in order to "
178 "decompress the comic book: %s"),
182 } else if (WIFEXITED (retval)) {
183 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
190 EV_DOCUMENT_ERROR_INVALID,
191 _("The command “%s” failed at "
192 "decompressing the comic book."),
199 EV_DOCUMENT_ERROR_INVALID,
200 _("The command “%s” did not end normally."),
208 /* This function shows how to use the choosen command for decompressing a
209 * comic book file. It modifies fields of the ComicsDocument struct with
210 * this information */
212 comics_generate_command_lines (ComicsDocument *comics_document,
216 ComicBookDecompressType type;
218 type = comics_document->command_usage;
219 quoted_file = g_shell_quote (comics_document->archive);
221 comics_document->extract_command =
222 g_strdup_printf (command_usage_def[type].extract,
223 comics_document->selected_command);
224 comics_document->list_command =
225 g_strdup_printf (command_usage_def[type].list,
226 comics_document->selected_command,
228 comics_document->regex_arg = command_usage_def[type].regex_arg;
229 comics_document->offset = command_usage_def[type].offset;
230 if (command_usage_def[type].decompress_tmp) {
231 comics_document->dir = ev_mkdtemp ("evince-comics-XXXXXX", error);
232 if (comics_document->dir == NULL)
235 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
237 comics_document->decompress_tmp =
238 g_strdup_printf (command_usage_def[type].decompress_tmp,
239 comics_document->selected_command,
241 comics_document->dir);
242 g_free (quoted_file);
244 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
245 comics_document->selected_command, error))
250 g_free (quoted_file);
256 /* This function chooses an external command for decompressing a comic
257 * book based on its mime tipe. */
259 comics_check_decompress_command (gchar *mime_type,
260 ComicsDocument *comics_document,
264 gchar *std_out, *std_err;
268 /* FIXME, use proper cbr/cbz mime types once they're
269 * included in shared-mime-info */
271 if (!strcmp (mime_type, "application/x-cbr") ||
272 !strcmp (mime_type, "application/x-rar")) {
273 /* The RARLAB provides a no-charge proprietary (freeware)
274 * decompress-only client for Linux called unrar. Another
275 * option is a GPLv2-licensed command-line tool developed by
276 * the Gna! project. Confusingly enough, the free software RAR
277 * decoder is also named unrar. For this reason we need to add
278 * some lines for disambiguation. Sorry for the added the
279 * complexity but it's life :)
280 * Finally, some distributions, like Debian, rename this free
281 * option as unrar-free.
283 comics_document->selected_command =
284 g_find_program_in_path ("unrar");
285 if (comics_document->selected_command) {
286 /* We only use std_err to avoid printing useless error
287 * messages on the terminal */
289 g_spawn_command_line_sync (
290 comics_document->selected_command,
294 g_propagate_error (error, err);
297 /* I don't check retval status because RARLAB unrar
298 * doesn't have a way to return 0 without involving an
299 * operation with a file*/
300 } else if (WIFEXITED (retval)) {
301 if (g_strrstr (std_out,"freeware") != NULL)
302 /* The RARLAB freeware client */
303 comics_document->command_usage = RARLABS;
305 /* The Gna! free software client */
306 comics_document->command_usage = GNAUNRAR;
313 /* The Gna! free software client with Debian naming convention */
314 comics_document->selected_command =
315 g_find_program_in_path ("unrar-free");
316 if (comics_document->selected_command) {
317 comics_document->command_usage = GNAUNRAR;
321 } else if (!strcmp (mime_type, "application/x-cbz") ||
322 !strcmp (mime_type, "application/zip")) {
323 /* InfoZIP's unzip program */
324 comics_document->selected_command =
325 g_find_program_in_path ("unzip");
326 if (comics_document->selected_command) {
327 comics_document->command_usage = UNZIP;
331 } else if (!strcmp (mime_type, "application/x-cb7") ||
332 !strcmp (mime_type, "application/x-7z-compressed")) {
333 /* 7zr, 7za and 7z are the commands from the p7zip project able
334 * to decompress .7z files */
335 comics_document->selected_command =
336 g_find_program_in_path ("7zr");
337 if (comics_document->selected_command) {
338 comics_document->command_usage = P7ZIP;
341 comics_document->selected_command =
342 g_find_program_in_path ("7za");
343 if (comics_document->selected_command) {
344 comics_document->command_usage = P7ZIP;
347 comics_document->selected_command =
348 g_find_program_in_path ("7z");
349 if (comics_document->selected_command) {
350 comics_document->command_usage = P7ZIP;
356 EV_DOCUMENT_ERROR_INVALID,
357 _("Not a comic book MIME type: %s"),
361 g_set_error_literal (error,
363 EV_DOCUMENT_ERROR_INVALID,
364 _("Can't find an appropriate command to "
365 "decompress this type of comic book"));
370 sort_page_names (gconstpointer a,
373 return strcmp (* (const char **) a, * (const char **) b);
377 comics_document_load (EvDocument *document,
381 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
382 GSList *supported_extensions;
385 gchar **cb_files, *cb_file;
390 comics_document->archive = g_filename_from_uri (uri, NULL, error);
391 if (!comics_document->archive)
394 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
397 g_propagate_error (error, err);
399 g_set_error_literal (error,
401 EV_DOCUMENT_ERROR_INVALID,
402 _("Unknown MIME Type"));
408 if (!comics_check_decompress_command (mime_type, comics_document,
412 } else if (!comics_generate_command_lines (comics_document, error)) {
419 /* Get list of files in archive */
420 success = g_spawn_command_line_sync (comics_document->list_command,
421 &std_out, NULL, &retval, error);
425 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
426 g_set_error_literal (error,
428 EV_DOCUMENT_ERROR_INVALID,
429 _("File corrupted"));
433 /* FIXME: is this safe against filenames containing \n in the archive ? */
434 cb_files = g_strsplit (std_out, "\n", 0);
438 g_set_error_literal (error,
440 EV_DOCUMENT_ERROR_INVALID,
441 _("No files in archive"));
445 comics_document->page_names = g_ptr_array_sized_new (64);
447 supported_extensions = get_supported_image_extensions ();
448 for (i = 0; cb_files[i] != NULL; i++) {
449 if (comics_document->offset != NO_OFFSET) {
450 if (g_utf8_strlen (cb_files[i],-1) >
451 comics_document->offset) {
453 g_utf8_offset_to_pointer (cb_files[i],
454 comics_document->offset);
459 cb_file = cb_files[i];
461 gchar *suffix = g_strrstr (cb_file, ".");
464 suffix = g_ascii_strdown (suffix + 1, -1);
465 if (g_slist_find_custom (supported_extensions, suffix,
466 (GCompareFunc) strcmp) != NULL) {
467 g_ptr_array_add (comics_document->page_names,
468 g_strstrip (g_strdup (cb_file)));
472 g_strfreev (cb_files);
473 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
474 g_slist_free (supported_extensions);
476 if (comics_document->page_names->len == 0) {
479 EV_DOCUMENT_ERROR_INVALID,
480 _("No images found in archive %s"),
485 /* Now sort the pages */
486 g_ptr_array_sort (comics_document->page_names, sort_page_names);
493 comics_document_save (EvDocument *document,
497 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
499 return ev_xfer_uri_simple (comics_document->archive, uri, error);
503 comics_document_get_n_pages (EvDocument *document)
505 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
507 if (comics_document->page_names == NULL)
510 return comics_document->page_names->len;
514 comics_document_get_page_size (EvDocument *document,
519 GdkPixbufLoader *loader;
522 gboolean success, got_size = FALSE;
528 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
530 if (!comics_document->decompress_tmp) {
531 argv = extract_argv (document, page->index);
532 success = g_spawn_async_with_pipes (NULL, argv, NULL,
533 G_SPAWN_SEARCH_PATH |
534 G_SPAWN_STDERR_TO_DEV_NULL,
537 NULL, &outpipe, NULL, NULL);
539 g_return_if_fail (success == TRUE);
541 loader = gdk_pixbuf_loader_new ();
542 g_signal_connect (loader, "area-prepared",
543 G_CALLBACK (get_page_size_area_prepared_cb),
546 while (outpipe >= 0) {
547 bytes = read (outpipe, buf, 1024);
550 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
551 if (bytes <= 0 || got_size) {
554 gdk_pixbuf_loader_close (loader, NULL);
558 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
559 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
561 *width = gdk_pixbuf_get_width (pixbuf);
563 *height = gdk_pixbuf_get_height (pixbuf);
566 g_spawn_close_pid (child_pid);
567 g_object_unref (loader);
569 filename = g_build_filename (comics_document->dir,
570 (char *) comics_document->page_names->pdata[page->index],
572 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
575 *width = gdk_pixbuf_get_width (pixbuf);
577 *height = gdk_pixbuf_get_height (pixbuf);
582 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
585 gboolean *got_size = data;
590 comics_document_render_pixbuf (EvDocument *document,
593 GdkPixbufLoader *loader;
594 GdkPixbuf *rotated_pixbuf;
603 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
605 if (!comics_document->decompress_tmp) {
606 argv = extract_argv (document, rc->page->index);
607 success = g_spawn_async_with_pipes (NULL, argv, NULL,
608 G_SPAWN_SEARCH_PATH |
609 G_SPAWN_STDERR_TO_DEV_NULL,
612 NULL, &outpipe, NULL, NULL);
614 g_return_val_if_fail (success == TRUE, NULL);
616 loader = gdk_pixbuf_loader_new ();
617 g_signal_connect (loader, "size-prepared",
618 G_CALLBACK (render_pixbuf_size_prepared_cb),
621 while (outpipe >= 0) {
622 bytes = read (outpipe, buf, 4096);
625 gdk_pixbuf_loader_write (loader, buf, bytes,
627 } else if (bytes <= 0) {
629 gdk_pixbuf_loader_close (loader, NULL);
634 rotated_pixbuf = gdk_pixbuf_rotate_simple (
635 gdk_pixbuf_loader_get_pixbuf (loader),
637 g_spawn_close_pid (child_pid);
638 g_object_unref (loader);
641 g_build_filename (comics_document->dir,
642 (char *) comics_document->page_names->pdata[rc->page->index],
645 gdk_pixbuf_get_file_info (filename, &width, &height);
648 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
649 filename, width * (rc->scale) + 0.5,
650 height * (rc->scale) + 0.5, NULL),
655 return rotated_pixbuf;
658 static cairo_surface_t *
659 comics_document_render (EvDocument *document,
663 cairo_surface_t *surface;
665 pixbuf = comics_document_render_pixbuf (document, rc);
666 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
667 g_object_unref (pixbuf);
673 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
678 double *scale = data;
679 int w = (width * (*scale) + 0.5);
680 int h = (height * (*scale) + 0.5);
682 gdk_pixbuf_loader_set_size (loader, w, h);
686 * comics_remove_dir: Removes a directory recursively.
688 * 0 if it was successfully deleted,
689 * -1 if an error occurred
692 comics_remove_dir (gchar *path_name)
695 const gchar *filename;
696 gchar *filename_with_path;
698 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
699 content_dir = g_dir_open (path_name, 0, NULL);
700 filename = g_dir_read_name (content_dir);
703 g_build_filename (path_name,
705 comics_remove_dir (filename_with_path);
706 g_free (filename_with_path);
707 filename = g_dir_read_name (content_dir);
709 g_dir_close (content_dir);
711 /* Note from g_remove() documentation: on Windows, it is in general not
712 * possible to remove a file that is open to some process, or mapped
714 return (g_remove (path_name));
718 comics_document_finalize (GObject *object)
720 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
722 if (comics_document->decompress_tmp) {
723 if (comics_remove_dir (comics_document->dir) == -1)
724 g_warning (_("There was an error deleting “%s”."),
725 comics_document->dir);
726 g_free (comics_document->dir);
729 if (comics_document->page_names) {
730 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
731 g_ptr_array_free (comics_document->page_names, TRUE);
734 g_free (comics_document->archive);
735 g_free (comics_document->selected_command);
736 g_free (comics_document->extract_command);
737 g_free (comics_document->list_command);
739 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
743 comics_document_class_init (ComicsDocumentClass *klass)
745 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
746 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
748 gobject_class->finalize = comics_document_finalize;
750 ev_document_class->load = comics_document_load;
751 ev_document_class->save = comics_document_save;
752 ev_document_class->get_n_pages = comics_document_get_n_pages;
753 ev_document_class->get_page_size = comics_document_get_page_size;
754 ev_document_class->render = comics_document_render;
758 comics_document_init (ComicsDocument *comics_document)
760 comics_document->archive = NULL;
761 comics_document->page_names = NULL;
762 comics_document->extract_command = NULL;
765 /* Returns a list of file extensions supported by gdk-pixbuf */
767 get_supported_image_extensions()
769 GSList *extensions = NULL;
770 GSList *formats = gdk_pixbuf_get_formats ();
773 for (l = formats; l != NULL; l = l->next) {
775 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
777 for (i = 0; ext[i] != NULL; i++) {
778 extensions = g_slist_append (extensions,
785 g_slist_free (formats);
790 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
794 GdkPixbuf *thumbnail;
796 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
799 GdkPixbuf *tmp_pixbuf = thumbnail;
801 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
802 g_object_unref (tmp_pixbuf);
809 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
814 gdouble page_width, page_height;
816 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
817 &page_width, &page_height);
819 if (rc->rotation == 90 || rc->rotation == 270) {
820 *width = (gint) (page_height * rc->scale);
821 *height = (gint) (page_width * rc->scale);
823 *width = (gint) (page_width * rc->scale);
824 *height = (gint) (page_height * rc->scale);
829 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
831 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
832 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
836 extract_argv (EvDocument *document, gint page)
838 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
840 char *command_line, *quoted_archive, *quoted_filename;
843 if (page >= comics_document->page_names->len)
846 quoted_archive = g_shell_quote (comics_document->archive);
847 if (comics_document->regex_arg) {
848 quoted_filename = comics_regex_quote (comics_document->page_names->pdata[page]);
850 quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
853 command_line = g_strdup_printf ("%s -- %s %s",
854 comics_document->extract_command,
858 g_shell_parse_argv (command_line, NULL, &argv, &err);
861 g_warning (_("Error %s"), err->message);
866 g_free (command_line);
867 g_free (quoted_archive);
868 g_free (quoted_filename);