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;
42 struct _ComicsDocument
44 GObject parent_instance;
49 gchar *selected_command;
50 gchar *extract_command, *list_command;
51 gboolean regex_arg, flag_temp, flag_FLOSS;
54 typedef struct _ComicsDocumentClass ComicsDocumentClass;
56 static void comics_document_document_iface_init (EvDocumentIface *iface);
57 static void comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
59 static GSList* get_supported_image_extensions (void);
60 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
62 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
66 static char** extract_argv (EvDocument *document,
70 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
72 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
73 comics_document_document_thumbnails_iface_init);
77 comics_regex_quote (const char *s)
81 d = ret = g_malloc (strlen (s) * 4 + 3);
85 for (; *s; s++, d++) {
110 /* This function manages the command for decompressing a comic book */
112 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
113 const gchar *command,
117 gchar *std_out, *basename;
121 success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
122 NULL, &retval, &err);
123 basename = g_path_get_basename (command);
127 EV_DOCUMENT_ERROR_INVALID,
128 _("Error launching the command “%s” in order to "
129 "decompress the comic book: %s"),
133 } else if (WIFEXITED (retval)) {
134 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
141 EV_DOCUMENT_ERROR_INVALID,
142 _("The command “%s” failed at "
143 "decompressing the comic book."),
150 EV_DOCUMENT_ERROR_INVALID,
151 _("The command “%s” did not end normally."),
159 /* This function shows how to use the choosen command for decompressing a
160 * comic book file. It modifies fields of the ComicsDocument struct with
161 * this information */
163 comics_generate_command_lines (ComicsDocument *comics_document,
167 gchar *command_decompress_tmp, *checksum;
169 quoted_file = g_shell_quote (comics_document->archive);
171 /* RARLabs software */
172 if (g_str_has_suffix (comics_document->selected_command, "unrar") &&
173 !comics_document->flag_FLOSS) {
174 comics_document->extract_command =
175 g_strdup_printf ("%s p -c- -ierr",
176 comics_document->selected_command);
177 comics_document->list_command =
178 g_strdup_printf ("%s vb -c- -- %s",
179 comics_document->selected_command,
181 comics_document->flag_temp = FALSE;
182 comics_document->regex_arg = FALSE;
183 g_free (quoted_file);
188 if (g_str_has_suffix (comics_document->selected_command, "unrar-free")||
189 (g_str_has_suffix (comics_document->selected_command, "unrar") &&
190 comics_document->flag_FLOSS)) {
192 comics_document->flag_temp = TRUE;
195 g_compute_checksum_for_string (G_CHECKSUM_MD5,
196 comics_document->archive,
198 comics_document->dir = g_build_filename (ev_tmp_dir (),
201 /* unrar-free can't create directories so we do it on its
203 if (g_mkdir_with_parents (comics_document->dir, 0700) != 0) {
207 EV_DOCUMENT_ERROR_INVALID,
208 _("Failed to create a temporary "
209 "directory on “%s”: %s"),
212 g_free (quoted_file);
213 comics_document->flag_temp = FALSE;
214 g_free (comics_document->dir);
218 command_decompress_tmp =
219 g_strdup_printf ("%s -xf %s %s",
220 comics_document->selected_command,
222 comics_document->dir);
224 if (!comics_decompress_temp_dir (command_decompress_tmp,
225 comics_document->selected_command, error)){
226 g_free (command_decompress_tmp);
227 g_free (quoted_file);
230 comics_document->list_command =
231 g_strdup_printf ("%s t %s",
232 comics_document->selected_command,
234 comics_document->regex_arg = TRUE;
235 g_free (command_decompress_tmp);
236 g_free (quoted_file);
241 /* InfoZIP's unzip */
242 if (g_str_has_suffix (comics_document->selected_command,"unzip")) {
243 comics_document->extract_command =
244 g_strdup_printf ("%s -p -C",
245 comics_document->selected_command);
246 comics_document->list_command =
247 g_strdup_printf ("%s -Z -1 -- %s",
248 comics_document->selected_command,
250 comics_document->flag_temp = FALSE;
251 comics_document->regex_arg = TRUE;
252 g_free (quoted_file);
257 if (g_str_has_suffix (comics_document->selected_command,"7zr")) {
258 comics_document->extract_command =
259 g_strdup_printf ("%s x -so",
260 comics_document->selected_command);
261 comics_document->list_command =
262 g_strdup_printf ("%s l -- %s",
263 comics_document->selected_command,
265 comics_document->flag_temp = FALSE;
266 comics_document->regex_arg = TRUE;
267 g_free (quoted_file);
271 /* You are not supposed to get here ! */
272 g_set_error_literal (error,
274 EV_DOCUMENT_ERROR_INVALID,
275 _("Internal error configuring the command for "
276 "decompressing the comic book file"));
277 g_free (quoted_file);
281 /* This function chooses an external command for decompressing a comic
282 * book based on its mime tipe. */
284 comics_check_decompress_command (gchar *mime_type,
285 ComicsDocument *comics_document,
289 gchar *std_out, *std_err;
293 /* FIXME, use proper cbr/cbz mime types once they're
294 * included in shared-mime-info */
296 if (!strcmp (mime_type, "application/x-cbr") ||
297 !strcmp (mime_type, "application/x-rar")) {
298 /* The RARLAB provides a no-charge proprietary (freeware)
299 * decompress-only client for Linux called unrar. Another
300 * option is a GPLv2-licensed command-line tool developed by
301 * the Gna! project. Confusingly enough, the free software RAR
302 * decoder is also named unrar. For this reason we need to add
303 * some lines for disambiguation. Sorry for the added the
304 * complexity but it's life :)
305 * Finally, some distributions, like Debian, rename this free
306 * option as unrar-free.
308 comics_document->selected_command =
309 g_find_program_in_path ("unrar");
310 if (comics_document->selected_command) {
311 /* We only use std_err to avoid printing useless error
312 * messages on the terminal */
314 g_spawn_command_line_sync (
315 comics_document->selected_command,
319 g_propagate_error (error, err);
322 /* I don't check retval status because RARLAB unrar
323 * doesn't have a way to return 0 without involving an
324 * operation with a file*/
325 } else if (WIFEXITED (retval)) {
326 if (g_strrstr (std_out,"freeware") != NULL)
327 /* The RARLAB freeware client */
328 comics_document->flag_FLOSS = FALSE;
330 /* The Gna! free software client */
331 comics_document->flag_FLOSS = TRUE;
337 /* The Gna! free software client with Debian naming convention */
338 comics_document->selected_command =
339 g_find_program_in_path ("unrar-free");
340 if (comics_document->selected_command)
342 } else if (!strcmp (mime_type, "application/x-cbz") ||
343 !strcmp (mime_type, "application/zip")) {
344 /* InfoZIP's unzip program */
345 comics_document->selected_command =
346 g_find_program_in_path ("unzip");
347 if (comics_document->selected_command)
349 } else if (!strcmp (mime_type, "application/x-cb7") ||
350 !strcmp (mime_type, "application/x-7z-compressed")) {
351 /* 7zr is a light stand-alone executable that supports only
353 comics_document->selected_command =
354 g_find_program_in_path ("7zr");
355 if (comics_document->selected_command)
360 EV_DOCUMENT_ERROR_INVALID,
361 _("Not a comic book MIME type: %s"),
365 g_set_error_literal (error,
367 EV_DOCUMENT_ERROR_INVALID,
368 _("Can't find an appropiate command to "
369 "decompress this type of comic book"));
374 comics_document_load (EvDocument *document,
378 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
379 GSList *supported_extensions;
387 comics_document->archive = g_filename_from_uri (uri, NULL, error);
388 if (!comics_document->archive)
391 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
394 g_propagate_error (error, err);
396 g_set_error_literal (error,
398 EV_DOCUMENT_ERROR_INVALID,
399 _("Unknown MIME Type"));
405 if (!comics_check_decompress_command (mime_type, comics_document,
409 } else if (!comics_generate_command_lines (comics_document, error)) {
416 /* Get list of files in archive */
417 success = g_spawn_command_line_sync (comics_document->list_command,
418 &std_out, NULL, &retval, error);
422 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
423 g_set_error_literal (error,
425 EV_DOCUMENT_ERROR_INVALID,
426 _("File corrupted"));
430 /* FIXME: is this safe against filenames containing \n in the archive ? */
431 cbr_files = g_strsplit (std_out, "\n", 0);
435 g_set_error_literal (error,
437 EV_DOCUMENT_ERROR_INVALID,
438 _("No files in archive"));
442 supported_extensions = get_supported_image_extensions ();
443 for (i = 0; cbr_files[i] != NULL; i++) {
444 gchar *suffix = g_strrstr (cbr_files[i], ".");
447 suffix = g_ascii_strdown (suffix + 1, -1);
449 if (g_slist_find_custom (supported_extensions, suffix,
450 (GCompareFunc) strcmp) != NULL) {
451 comics_document->page_names =
452 g_slist_insert_sorted (
453 comics_document->page_names,
454 g_strdup (g_strstrip (cbr_files[i])),
455 (GCompareFunc) strcmp);
456 comics_document->n_pages++;
462 g_strfreev (cbr_files);
463 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
464 g_slist_free (supported_extensions);
466 if (comics_document->n_pages == 0) {
469 EV_DOCUMENT_ERROR_INVALID,
470 _("No images found in archive %s"),
480 comics_document_save (EvDocument *document,
484 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
486 return ev_xfer_uri_simple (comics_document->archive, uri, error);
490 comics_document_get_n_pages (EvDocument *document)
492 return COMICS_DOCUMENT (document)->n_pages;
496 comics_document_get_page_size (EvDocument *document,
501 GdkPixbufLoader *loader;
504 gboolean success, got_size = FALSE;
510 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
512 if (!comics_document->flag_temp) {
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*) g_slist_nth_data (
554 comics_document->page_names,
557 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
560 *width = gdk_pixbuf_get_width (pixbuf);
562 *height = gdk_pixbuf_get_height (pixbuf);
567 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
570 gboolean *got_size = data;
575 comics_document_render_pixbuf (EvDocument *document,
578 GdkPixbufLoader *loader;
579 GdkPixbuf *rotated_pixbuf;
588 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
590 if (!comics_document->flag_temp) {
591 argv = extract_argv (document, rc->page->index);
592 success = g_spawn_async_with_pipes (NULL, argv, NULL,
593 G_SPAWN_SEARCH_PATH |
594 G_SPAWN_STDERR_TO_DEV_NULL,
597 NULL, &outpipe, NULL, NULL);
599 g_return_val_if_fail (success == TRUE, NULL);
601 loader = gdk_pixbuf_loader_new ();
602 g_signal_connect (loader, "size-prepared",
603 G_CALLBACK (render_pixbuf_size_prepared_cb),
606 while (outpipe >= 0) {
607 bytes = read (outpipe, buf, 4096);
610 gdk_pixbuf_loader_write (loader, buf, bytes,
612 } else if (bytes <= 0) {
614 gdk_pixbuf_loader_close (loader, NULL);
619 rotated_pixbuf = gdk_pixbuf_rotate_simple (
620 gdk_pixbuf_loader_get_pixbuf (loader),
622 g_spawn_close_pid (child_pid);
623 g_object_unref (loader);
626 g_build_filename (comics_document->dir,
627 (char*) g_slist_nth_data (
628 comics_document->page_names,
632 gdk_pixbuf_get_file_info (filename, &width, &height);
635 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
636 filename, width * (rc->scale) + 0.5,
637 height * (rc->scale) + 0.5, NULL),
642 return rotated_pixbuf;
645 static cairo_surface_t *
646 comics_document_render (EvDocument *document,
650 cairo_surface_t *surface;
652 pixbuf = comics_document_render_pixbuf (document, rc);
653 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
654 g_object_unref (pixbuf);
660 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
665 double *scale = data;
666 int w = (width * (*scale) + 0.5);
667 int h = (height * (*scale) + 0.5);
669 gdk_pixbuf_loader_set_size (loader, w, h);
673 * comics_remove_dir: Removes a directory recursively.
675 * 0 if it was successfully deleted,
676 * -1 if an error occurred
679 comics_remove_dir (gchar *path_name)
682 const gchar *filename;
683 gchar *filename_with_path;
685 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
686 content_dir = g_dir_open (path_name, 0, NULL);
687 filename = g_dir_read_name (content_dir);
690 g_build_filename (path_name,
692 comics_remove_dir (filename_with_path);
693 g_free (filename_with_path);
694 filename = g_dir_read_name (content_dir);
696 g_dir_close (content_dir);
698 /* Note from g_remove() documentation: on Windows, it is in general not
699 * possible to remove a file that is open to some process, or mapped
701 return (g_remove (path_name));
705 comics_document_finalize (GObject *object)
707 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
709 if (comics_document->flag_temp) {
710 if (comics_remove_dir (comics_document->dir) == -1)
711 g_warning (_("There was an error deleting “%s”."),
712 comics_document->dir);
713 g_free (comics_document->dir);
716 if (comics_document->archive)
717 g_free (comics_document->archive);
719 if (comics_document->page_names) {
720 g_slist_foreach (comics_document->page_names,
721 (GFunc) g_free, NULL);
722 g_slist_free (comics_document->page_names);
725 if (comics_document->selected_command)
726 g_free (comics_document->selected_command);
728 if (comics_document->extract_command)
729 g_free (comics_document->extract_command);
731 if (comics_document->list_command)
732 g_free (comics_document->list_command);
734 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
738 comics_document_class_init (ComicsDocumentClass *klass)
740 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
741 gobject_class->finalize = comics_document_finalize;
744 static EvDocumentInfo *
745 comics_document_get_info (EvDocument *document)
747 EvDocumentInfo *info;
748 info = g_new0 (EvDocumentInfo, 1);
753 comics_document_document_iface_init (EvDocumentIface *iface)
755 iface->load = comics_document_load;
756 iface->save = comics_document_save;
757 iface->get_n_pages = comics_document_get_n_pages;
758 iface->get_page_size = comics_document_get_page_size;
759 iface->render = comics_document_render;
760 iface->get_info = comics_document_get_info;
764 comics_document_init (ComicsDocument *comics_document)
766 comics_document->archive = NULL;
767 comics_document->page_names = NULL;
768 comics_document->extract_command = NULL;
769 comics_document->n_pages = 0;
772 /* Returns a list of file extensions supported by gdk-pixbuf */
774 get_supported_image_extensions()
776 GSList *extensions = NULL;
777 GSList *formats = gdk_pixbuf_get_formats ();
780 for (l = formats; l != NULL; l = l->next) {
782 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
784 for (i = 0; ext[i] != NULL; i++) {
785 extensions = g_slist_append (extensions,
792 g_slist_free (formats);
797 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
801 GdkPixbuf *thumbnail;
803 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
806 GdkPixbuf *tmp_pixbuf = thumbnail;
808 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
809 g_object_unref (tmp_pixbuf);
816 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
821 gdouble page_width, page_height;
823 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
824 &page_width, &page_height);
826 if (rc->rotation == 90 || rc->rotation == 270) {
827 *width = (gint) (page_height * rc->scale);
828 *height = (gint) (page_width * rc->scale);
830 *width = (gint) (page_width * rc->scale);
831 *height = (gint) (page_height * rc->scale);
836 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
838 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
839 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
843 extract_argv (EvDocument *document, gint page)
845 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
847 char *command_line, *quoted_archive, *quoted_filename;
850 quoted_archive = g_shell_quote (comics_document->archive);
851 if (comics_document->regex_arg) {
852 quoted_filename = comics_regex_quote (
853 g_slist_nth_data (comics_document->page_names, page));
855 quoted_filename = g_shell_quote (
856 g_slist_nth_data (comics_document->page_names, page));
859 command_line = g_strdup_printf ("%s -- %s %s",
860 comics_document->extract_command,
864 g_shell_parse_argv (command_line, NULL, &argv, &err);
867 g_warning (_("Error %s"), err->message);
872 g_free (command_line);
873 g_free (quoted_archive);
874 g_free (quoted_filename);