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;
60 ComicBookDecompressType command_usage;
66 /* For perfomance reasons of 7z* we've choosen to decompress on the temporary
67 * directory instead of decompressing on the stdout */
70 char *extract, *list, *decompress_tmp;
73 } command_usage_def[] = {
74 {"%s p -c- -ierr", "%s vb -c- -- %s", NULL , FALSE, NO_OFFSET},
75 {NULL , "%s t %s" , "%s -xf %s %s" , TRUE , NO_OFFSET},
76 {"%s -p -C" , "%s -Z -1 -- %s" , NULL , TRUE , NO_OFFSET},
77 {NULL , "%s l -- %s" , "%s x -y %s -o%s", FALSE, OFFSET_7Z}
81 typedef struct _ComicsDocumentClass ComicsDocumentClass;
83 static void comics_document_document_iface_init (EvDocumentIface *iface);
84 static void comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
86 static GSList* get_supported_image_extensions (void);
87 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
89 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
93 static char** extract_argv (EvDocument *document,
97 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
99 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
100 comics_document_document_thumbnails_iface_init);
104 comics_regex_quote (const char *s)
108 d = ret = g_malloc (strlen (s) * 4 + 3);
112 for (; *s; s++, d++) {
137 /* This function manages the command for decompressing a comic book */
139 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
140 const gchar *command,
144 gchar *std_out, *basename;
148 success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
149 NULL, &retval, &err);
150 basename = g_path_get_basename (command);
154 EV_DOCUMENT_ERROR_INVALID,
155 _("Error launching the command “%s” in order to "
156 "decompress the comic book: %s"),
160 } else if (WIFEXITED (retval)) {
161 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
168 EV_DOCUMENT_ERROR_INVALID,
169 _("The command “%s” failed at "
170 "decompressing the comic book."),
177 EV_DOCUMENT_ERROR_INVALID,
178 _("The command “%s” did not end normally."),
186 /* This function shows how to use the choosen command for decompressing a
187 * comic book file. It modifies fields of the ComicsDocument struct with
188 * this information */
190 comics_generate_command_lines (ComicsDocument *comics_document,
194 ComicBookDecompressType type;
196 type = comics_document->command_usage;
197 quoted_file = g_shell_quote (comics_document->archive);
199 comics_document->extract_command =
200 g_strdup_printf (command_usage_def[type].extract,
201 comics_document->selected_command);
202 comics_document->list_command =
203 g_strdup_printf (command_usage_def[type].list,
204 comics_document->selected_command,
206 comics_document->regex_arg = command_usage_def[type].regex_arg;
207 comics_document->offset = command_usage_def[type].offset;
208 if (command_usage_def[type].decompress_tmp) {
209 comics_document->dir = ev_tmp_directory (NULL);
210 comics_document->decompress_tmp =
211 g_strdup_printf (command_usage_def[type].decompress_tmp,
212 comics_document->selected_command,
214 comics_document->dir);
215 g_free (quoted_file);
216 /* unrar-free can't create directories so we do it on its
218 if (type == GNAUNRAR) {
219 if (g_mkdir_with_parents (comics_document->dir, 0700) !=
224 EV_DOCUMENT_ERROR_INVALID,
225 _("Failed to create a temporary "
227 g_warning ("Failed to create directory %s: %s",
228 comics_document->dir,
234 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
235 comics_document->selected_command, error))
240 g_free (quoted_file);
246 /* This function chooses an external command for decompressing a comic
247 * book based on its mime tipe. */
249 comics_check_decompress_command (gchar *mime_type,
250 ComicsDocument *comics_document,
254 gchar *std_out, *std_err;
258 /* FIXME, use proper cbr/cbz mime types once they're
259 * included in shared-mime-info */
261 if (!strcmp (mime_type, "application/x-cbr") ||
262 !strcmp (mime_type, "application/x-rar")) {
263 /* The RARLAB provides a no-charge proprietary (freeware)
264 * decompress-only client for Linux called unrar. Another
265 * option is a GPLv2-licensed command-line tool developed by
266 * the Gna! project. Confusingly enough, the free software RAR
267 * decoder is also named unrar. For this reason we need to add
268 * some lines for disambiguation. Sorry for the added the
269 * complexity but it's life :)
270 * Finally, some distributions, like Debian, rename this free
271 * option as unrar-free.
273 comics_document->selected_command =
274 g_find_program_in_path ("unrar");
275 if (comics_document->selected_command) {
276 /* We only use std_err to avoid printing useless error
277 * messages on the terminal */
279 g_spawn_command_line_sync (
280 comics_document->selected_command,
284 g_propagate_error (error, err);
287 /* I don't check retval status because RARLAB unrar
288 * doesn't have a way to return 0 without involving an
289 * operation with a file*/
290 } else if (WIFEXITED (retval)) {
291 if (g_strrstr (std_out,"freeware") != NULL)
292 /* The RARLAB freeware client */
293 comics_document->command_usage = RARLABS;
295 /* The Gna! free software client */
296 comics_document->command_usage = GNAUNRAR;
303 /* The Gna! free software client with Debian naming convention */
304 comics_document->selected_command =
305 g_find_program_in_path ("unrar-free");
306 if (comics_document->selected_command) {
307 comics_document->command_usage = GNAUNRAR;
311 } else if (!strcmp (mime_type, "application/x-cbz") ||
312 !strcmp (mime_type, "application/zip")) {
313 /* InfoZIP's unzip program */
314 comics_document->selected_command =
315 g_find_program_in_path ("unzip");
316 if (comics_document->selected_command) {
317 comics_document->command_usage = UNZIP;
321 } else if (!strcmp (mime_type, "application/x-cb7") ||
322 !strcmp (mime_type, "application/x-7z-compressed")) {
323 /* 7zr, 7za and 7z are the commands from the p7zip project able
324 * to decompress .7z files */
325 comics_document->selected_command =
326 g_find_program_in_path ("7zr");
327 if (comics_document->selected_command) {
328 comics_document->command_usage = P7ZIP;
331 comics_document->selected_command =
332 g_find_program_in_path ("7za");
333 if (comics_document->selected_command) {
334 comics_document->command_usage = P7ZIP;
337 comics_document->selected_command =
338 g_find_program_in_path ("7z");
339 if (comics_document->selected_command) {
340 comics_document->command_usage = P7ZIP;
346 EV_DOCUMENT_ERROR_INVALID,
347 _("Not a comic book MIME type: %s"),
351 g_set_error_literal (error,
353 EV_DOCUMENT_ERROR_INVALID,
354 _("Can't find an appropriate command to "
355 "decompress this type of comic book"));
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 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 comics_document->page_names =
449 g_slist_insert_sorted (
450 comics_document->page_names,
451 g_strdup (g_strstrip (cb_file)),
452 (GCompareFunc) strcmp);
453 comics_document->n_pages++;
457 g_strfreev (cb_files);
458 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
459 g_slist_free (supported_extensions);
461 if (comics_document->n_pages == 0) {
464 EV_DOCUMENT_ERROR_INVALID,
465 _("No images found in archive %s"),
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 return COMICS_DOCUMENT (document)->n_pages;
490 comics_document_get_page_size (EvDocument *document,
495 GdkPixbufLoader *loader;
498 gboolean success, got_size = FALSE;
504 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
506 if (!comics_document->decompress_tmp) {
507 argv = extract_argv (document, page->index);
508 success = g_spawn_async_with_pipes (NULL, argv, NULL,
509 G_SPAWN_SEARCH_PATH |
510 G_SPAWN_STDERR_TO_DEV_NULL,
513 NULL, &outpipe, NULL, NULL);
515 g_return_if_fail (success == TRUE);
517 loader = gdk_pixbuf_loader_new ();
518 g_signal_connect (loader, "area-prepared",
519 G_CALLBACK (get_page_size_area_prepared_cb),
522 while (outpipe >= 0) {
523 bytes = read (outpipe, buf, 1024);
526 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
527 if (bytes <= 0 || got_size) {
530 gdk_pixbuf_loader_close (loader, NULL);
534 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
535 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
537 *width = gdk_pixbuf_get_width (pixbuf);
539 *height = gdk_pixbuf_get_height (pixbuf);
542 g_spawn_close_pid (child_pid);
543 g_object_unref (loader);
545 filename = g_build_filename (comics_document->dir,
546 (char*) g_slist_nth_data (
547 comics_document->page_names,
550 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
553 *width = gdk_pixbuf_get_width (pixbuf);
555 *height = gdk_pixbuf_get_height (pixbuf);
560 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
563 gboolean *got_size = data;
568 comics_document_render_pixbuf (EvDocument *document,
571 GdkPixbufLoader *loader;
572 GdkPixbuf *rotated_pixbuf;
581 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
583 if (!comics_document->decompress_tmp) {
584 argv = extract_argv (document, rc->page->index);
585 success = g_spawn_async_with_pipes (NULL, argv, NULL,
586 G_SPAWN_SEARCH_PATH |
587 G_SPAWN_STDERR_TO_DEV_NULL,
590 NULL, &outpipe, NULL, NULL);
592 g_return_val_if_fail (success == TRUE, NULL);
594 loader = gdk_pixbuf_loader_new ();
595 g_signal_connect (loader, "size-prepared",
596 G_CALLBACK (render_pixbuf_size_prepared_cb),
599 while (outpipe >= 0) {
600 bytes = read (outpipe, buf, 4096);
603 gdk_pixbuf_loader_write (loader, buf, bytes,
605 } else if (bytes <= 0) {
607 gdk_pixbuf_loader_close (loader, NULL);
612 rotated_pixbuf = gdk_pixbuf_rotate_simple (
613 gdk_pixbuf_loader_get_pixbuf (loader),
615 g_spawn_close_pid (child_pid);
616 g_object_unref (loader);
619 g_build_filename (comics_document->dir,
620 (char*) g_slist_nth_data (
621 comics_document->page_names,
625 gdk_pixbuf_get_file_info (filename, &width, &height);
628 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
629 filename, width * (rc->scale) + 0.5,
630 height * (rc->scale) + 0.5, NULL),
635 return rotated_pixbuf;
638 static cairo_surface_t *
639 comics_document_render (EvDocument *document,
643 cairo_surface_t *surface;
645 pixbuf = comics_document_render_pixbuf (document, rc);
646 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
647 g_object_unref (pixbuf);
653 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
658 double *scale = data;
659 int w = (width * (*scale) + 0.5);
660 int h = (height * (*scale) + 0.5);
662 gdk_pixbuf_loader_set_size (loader, w, h);
666 * comics_remove_dir: Removes a directory recursively.
668 * 0 if it was successfully deleted,
669 * -1 if an error occurred
672 comics_remove_dir (gchar *path_name)
675 const gchar *filename;
676 gchar *filename_with_path;
678 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
679 content_dir = g_dir_open (path_name, 0, NULL);
680 filename = g_dir_read_name (content_dir);
683 g_build_filename (path_name,
685 comics_remove_dir (filename_with_path);
686 g_free (filename_with_path);
687 filename = g_dir_read_name (content_dir);
689 g_dir_close (content_dir);
691 /* Note from g_remove() documentation: on Windows, it is in general not
692 * possible to remove a file that is open to some process, or mapped
694 return (g_remove (path_name));
698 comics_document_finalize (GObject *object)
700 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
702 if (comics_document->decompress_tmp) {
703 if (comics_remove_dir (comics_document->dir) == -1)
704 g_warning (_("There was an error deleting “%s”."),
705 comics_document->dir);
706 g_free (comics_document->dir);
707 g_remove (ev_tmp_dir ());
710 if (comics_document->page_names) {
711 g_slist_foreach (comics_document->page_names,
712 (GFunc) g_free, NULL);
713 g_slist_free (comics_document->page_names);
716 g_free (comics_document->archive);
717 g_free (comics_document->selected_command);
718 g_free (comics_document->extract_command);
719 g_free (comics_document->list_command);
721 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
725 comics_document_class_init (ComicsDocumentClass *klass)
727 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
728 gobject_class->finalize = comics_document_finalize;
731 static EvDocumentInfo *
732 comics_document_get_info (EvDocument *document)
734 EvDocumentInfo *info;
735 info = g_new0 (EvDocumentInfo, 1);
740 comics_document_document_iface_init (EvDocumentIface *iface)
742 iface->load = comics_document_load;
743 iface->save = comics_document_save;
744 iface->get_n_pages = comics_document_get_n_pages;
745 iface->get_page_size = comics_document_get_page_size;
746 iface->render = comics_document_render;
747 iface->get_info = comics_document_get_info;
751 comics_document_init (ComicsDocument *comics_document)
753 comics_document->archive = NULL;
754 comics_document->page_names = NULL;
755 comics_document->extract_command = NULL;
756 comics_document->n_pages = 0;
759 /* Returns a list of file extensions supported by gdk-pixbuf */
761 get_supported_image_extensions()
763 GSList *extensions = NULL;
764 GSList *formats = gdk_pixbuf_get_formats ();
767 for (l = formats; l != NULL; l = l->next) {
769 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
771 for (i = 0; ext[i] != NULL; i++) {
772 extensions = g_slist_append (extensions,
779 g_slist_free (formats);
784 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
788 GdkPixbuf *thumbnail;
790 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
793 GdkPixbuf *tmp_pixbuf = thumbnail;
795 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
796 g_object_unref (tmp_pixbuf);
803 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
808 gdouble page_width, page_height;
810 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
811 &page_width, &page_height);
813 if (rc->rotation == 90 || rc->rotation == 270) {
814 *width = (gint) (page_height * rc->scale);
815 *height = (gint) (page_width * rc->scale);
817 *width = (gint) (page_width * rc->scale);
818 *height = (gint) (page_height * rc->scale);
823 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
825 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
826 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
830 extract_argv (EvDocument *document, gint page)
832 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
834 char *command_line, *quoted_archive, *quoted_filename;
837 quoted_archive = g_shell_quote (comics_document->archive);
838 if (comics_document->regex_arg) {
839 quoted_filename = comics_regex_quote (
840 g_slist_nth_data (comics_document->page_names, page));
842 quoted_filename = g_shell_quote (
843 g_slist_nth_data (comics_document->page_names, page));
846 command_line = g_strdup_printf ("%s -- %s %s",
847 comics_document->extract_command,
851 g_shell_parse_argv (command_line, NULL, &argv, &err);
854 g_warning (_("Error %s"), err->message);
859 g_free (command_line);
860 g_free (quoted_archive);
861 g_free (quoted_filename);