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 for decompressing "
129 "the comic book to the temporary directory: %s"), basename, err->message);
131 } else if (WIFEXITED (retval)) {
132 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
139 EV_DOCUMENT_ERROR_INVALID,
140 _("The command %s failed to decompress the "
141 "comic book to the temporary directory."), basename);
147 EV_DOCUMENT_ERROR_INVALID,
148 _("The command %s does not end normally."), basename);
155 /* This function shows how to use the choosen command for decompressing a
156 * comic book file. It modifies fields of the ComicsDocument struct with
157 * this information */
159 comics_generate_command_lines (ComicsDocument *comics_document,
163 gchar *command_decompress_tmp, *checksum;
165 quoted_file = g_shell_quote (comics_document->archive);
167 /* RARLabs software */
168 if (g_str_has_suffix (comics_document->selected_command, "unrar") &&
169 !comics_document->flag_FLOSS) {
170 comics_document->extract_command =
171 g_strdup_printf ("%s p -c- -ierr",
172 comics_document->selected_command);
173 comics_document->list_command =
174 g_strdup_printf ("%s vb -c- -- %s",
175 comics_document->selected_command, quoted_file);
176 comics_document->flag_temp = FALSE;
177 comics_document->regex_arg = FALSE;
178 g_free (quoted_file);
183 if (g_str_has_suffix (comics_document->selected_command, "unrar-free")
184 || (g_str_has_suffix (comics_document->selected_command,
185 "unrar") && comics_document->flag_FLOSS)) {
187 comics_document->flag_temp = TRUE;
189 checksum = g_compute_checksum_for_string (G_CHECKSUM_MD5,
190 comics_document->archive, -1);
191 comics_document->dir = g_build_filename (ev_tmp_dir (),
194 /* unrar-free can't create directories so we do it on its
196 if (g_mkdir_with_parents (comics_document->dir, 0700) != 0) {
199 EV_DOCUMENT_ERROR_INVALID,
200 _("Failed to create a directory %s in the "
201 "temporary directory: %s"),
202 comics_document->dir,
204 g_free (quoted_file);
208 command_decompress_tmp = g_strdup_printf ( "%s -xf %s %s",
209 comics_document->selected_command,
211 comics_document->dir);
213 if (!comics_decompress_temp_dir (command_decompress_tmp,
214 comics_document->selected_command,
216 g_free (command_decompress_tmp);
217 g_free (quoted_file);
220 comics_document->list_command =
221 g_strdup_printf ("%s t %s",
222 comics_document->selected_command,
224 comics_document->regex_arg = TRUE;
225 g_free (command_decompress_tmp);
226 g_free (quoted_file);
231 /* InfoZIP's unzip */
232 if (g_str_has_suffix (comics_document->selected_command,"unzip")) {
233 comics_document->extract_command =
234 g_strdup_printf ("%s -p -C",
235 comics_document->selected_command);
236 comics_document->list_command =
237 g_strdup_printf ("%s -Z -1 -- %s",
238 comics_document->selected_command,
240 comics_document->flag_temp = FALSE;
241 comics_document->regex_arg = TRUE;
242 g_free (quoted_file);
247 if (g_str_has_suffix (comics_document->selected_command,"7zr")) {
248 comics_document->extract_command =
249 g_strdup_printf ("%s x -so",
250 comics_document->selected_command);
251 comics_document->list_command =
252 g_strdup_printf ("%s l -- %s",
253 comics_document->selected_command,
255 comics_document->flag_temp = FALSE;
256 comics_document->regex_arg = TRUE;
257 g_free (quoted_file);
261 /* You are not supposed to get here ! */
262 g_set_error_literal (error,
264 EV_DOCUMENT_ERROR_INVALID,
265 _("Internal error configuring the command for decompressing "
266 "the comic book file"));
267 g_free (quoted_file);
271 /* This function chooses an external command for decompressing a comic
272 * book based on its mime tipe. */
274 comics_check_decompress_command (gchar *mime_type,
275 ComicsDocument *comics_document,
279 gchar *std_out, *std_err;
283 /* FIXME, use proper cbr/cbz mime types once they're
284 * included in shared-mime-info */
286 if (!strcmp (mime_type, "application/x-cbr") ||
287 !strcmp (mime_type, "application/x-rar")) {
288 /* The RARLAB provides a no-charge proprietary (freeware)
289 * decompress-only client for Linux called unrar. Another
290 * option is a GPLv2-licensed command-line tool developed by
291 * the Gna! project. Confusingly enough, the free software RAR
292 * decoder is also named unrar. For this reason we need to add
293 * some lines for disambiguation. Sorry for the added the
294 * complexity but it's life :)
295 * Finally, some distributions, like Debian, rename this free
296 * option as unrar-free.
298 comics_document->selected_command =
299 g_find_program_in_path ("unrar");
300 if (comics_document->selected_command) {
301 /* We only use std_err to avoid printing useless error
302 * messages on the terminal */
303 success = g_spawn_command_line_sync (
304 comics_document->selected_command,
305 &std_out, &std_err, &retval, &err);
307 g_propagate_error (error, err);
310 /* I don't check retval status because RARLAB unrar
311 * doesn't have a way to return 0 without involving an
312 * operation with a file*/
313 } else if (WIFEXITED (retval)) {
314 if (g_strrstr (std_out,"freeware") != NULL)
315 /* The RARLAB freeware client */
316 comics_document->flag_FLOSS = FALSE;
318 /* The Gna! free software client */
319 comics_document->flag_FLOSS = TRUE;
325 /* The Gna! free software client with Debian naming convention */
326 comics_document->selected_command =
327 g_find_program_in_path ("unrar-free");
328 if (comics_document->selected_command)
330 } else if (!strcmp (mime_type, "application/x-cbz") ||
331 !strcmp (mime_type, "application/zip")) {
332 /* InfoZIP's unzip program */
333 comics_document->selected_command =
334 g_find_program_in_path ("unzip");
335 if (comics_document->selected_command)
337 } else if (!strcmp (mime_type, "application/x-cb7") ||
338 !strcmp (mime_type, "application/x-7z-compressed")) {
339 /* 7zr is a light stand-alone executable that supports only
341 comics_document->selected_command =
342 g_find_program_in_path ("7zr");
343 if (comics_document->selected_command)
348 EV_DOCUMENT_ERROR_INVALID,
349 _("Not a comic book MIME type: %s"),
353 g_set_error_literal (error,
355 EV_DOCUMENT_ERROR_INVALID,
356 _("Can't find an appropiate command to "
357 "decompress this type of comic book"));
362 comics_document_load (EvDocument *document,
366 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
367 GSList *supported_extensions;
375 comics_document->archive = g_filename_from_uri (uri, NULL, error);
376 if (!comics_document->archive)
379 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
382 g_propagate_error (error, err);
384 g_set_error_literal (error,
386 EV_DOCUMENT_ERROR_INVALID,
387 _("Unknown MIME Type"));
393 if (!comics_check_decompress_command (mime_type, comics_document,
397 } else if (!comics_generate_command_lines (comics_document, error)) {
404 /* Get list of files in archive */
405 success = g_spawn_command_line_sync (comics_document->list_command,
406 &std_out, NULL, &retval, error);
410 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
411 g_set_error_literal (error,
413 EV_DOCUMENT_ERROR_INVALID,
414 _("File corrupted"));
418 /* FIXME: is this safe against filenames containing \n in the archive ? */
419 cbr_files = g_strsplit (std_out, "\n", 0);
423 g_set_error_literal (error,
425 EV_DOCUMENT_ERROR_INVALID,
426 _("No files in archive"));
430 supported_extensions = get_supported_image_extensions ();
431 for (i = 0; cbr_files[i] != NULL; i++) {
432 gchar *suffix = g_strrstr (cbr_files[i], ".");
435 suffix = g_ascii_strdown (suffix + 1, -1);
437 if (g_slist_find_custom (supported_extensions, suffix,
438 (GCompareFunc) strcmp) != NULL) {
439 comics_document->page_names =
440 g_slist_insert_sorted (
441 comics_document->page_names,
442 g_strdup (g_strstrip (cbr_files[i])),
443 (GCompareFunc) strcmp);
444 comics_document->n_pages++;
450 g_strfreev (cbr_files);
451 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
452 g_slist_free (supported_extensions);
454 if (comics_document->n_pages == 0) {
457 EV_DOCUMENT_ERROR_INVALID,
458 _("No images found in archive %s"),
468 comics_document_save (EvDocument *document,
472 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
474 return ev_xfer_uri_simple (comics_document->archive, uri, error);
478 comics_document_get_n_pages (EvDocument *document)
480 return COMICS_DOCUMENT (document)->n_pages;
484 comics_document_get_page_size (EvDocument *document,
489 GdkPixbufLoader *loader;
492 gboolean success, got_size = FALSE;
498 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
500 if (!comics_document->flag_temp) {
502 argv = extract_argv (document, page->index);
503 success = g_spawn_async_with_pipes (NULL, argv, NULL,
504 G_SPAWN_SEARCH_PATH |
505 G_SPAWN_STDERR_TO_DEV_NULL,
508 NULL, &outpipe, NULL, NULL);
510 g_return_if_fail (success == TRUE);
512 loader = gdk_pixbuf_loader_new ();
513 g_signal_connect (loader, "area-prepared",
514 G_CALLBACK (get_page_size_area_prepared_cb),
517 while (outpipe >= 0) {
518 bytes = read (outpipe, buf, 1024);
521 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
522 if (bytes <= 0 || got_size) {
525 gdk_pixbuf_loader_close (loader, NULL);
529 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
530 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
532 *width = gdk_pixbuf_get_width (pixbuf);
534 *height = gdk_pixbuf_get_height (pixbuf);
537 g_spawn_close_pid (child_pid);
538 g_object_unref (loader);
540 filename = g_build_filename (comics_document->dir,
541 (char*) g_slist_nth_data (
542 comics_document->page_names,
545 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
548 *width = gdk_pixbuf_get_width (pixbuf);
550 *height = gdk_pixbuf_get_height (pixbuf);
555 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
558 gboolean *got_size = data;
563 comics_document_render_pixbuf (EvDocument *document,
566 GdkPixbufLoader *loader;
567 GdkPixbuf *rotated_pixbuf;
576 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
578 if (!comics_document->flag_temp) {
579 argv = extract_argv (document, rc->page->index);
580 success = g_spawn_async_with_pipes (NULL, argv, NULL,
581 G_SPAWN_SEARCH_PATH |
582 G_SPAWN_STDERR_TO_DEV_NULL,
585 NULL, &outpipe, NULL, NULL);
587 g_return_val_if_fail (success == TRUE, NULL);
589 loader = gdk_pixbuf_loader_new ();
590 g_signal_connect (loader, "size-prepared",
591 G_CALLBACK (render_pixbuf_size_prepared_cb),
594 while (outpipe >= 0) {
595 bytes = read (outpipe, buf, 4096);
598 gdk_pixbuf_loader_write (loader, buf, bytes,
600 } else if (bytes <= 0) {
602 gdk_pixbuf_loader_close (loader, NULL);
607 rotated_pixbuf = gdk_pixbuf_rotate_simple (
608 gdk_pixbuf_loader_get_pixbuf (loader),
610 g_spawn_close_pid (child_pid);
611 g_object_unref (loader);
613 filename = g_build_filename (comics_document->dir,
614 (char*) g_slist_nth_data (
615 comics_document->page_names,
619 gdk_pixbuf_get_file_info (filename, &width, &height);
621 rotated_pixbuf = gdk_pixbuf_rotate_simple (
622 gdk_pixbuf_new_from_file_at_size (
624 width * (rc->scale) + 0.5,
625 height * (rc->scale) + 0.5,
631 return rotated_pixbuf;
634 static cairo_surface_t *
635 comics_document_render (EvDocument *document,
639 cairo_surface_t *surface;
641 pixbuf = comics_document_render_pixbuf (document, rc);
642 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
643 g_object_unref (pixbuf);
649 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
654 double *scale = data;
655 int w = (width * (*scale) + 0.5);
656 int h = (height * (*scale) + 0.5);
658 gdk_pixbuf_loader_set_size (loader, w, h);
662 * comics_remove_dir: Removes a directory recursively.
664 * 0 if it was successfully deleted,
665 * -1 if an error occurred
668 comics_remove_dir (gchar *path_name)
671 const gchar *filename;
672 gchar *filename_with_path;
674 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
675 content_dir = g_dir_open (path_name, 0, NULL);
676 filename = g_dir_read_name (content_dir);
678 filename_with_path = g_build_filename (path_name,
680 comics_remove_dir (filename_with_path);
681 g_free (filename_with_path);
682 filename = g_dir_read_name (content_dir);
684 g_dir_close (content_dir);
686 /* Note from g_remove() documentation: on Windows, it is in general not
687 * possible to remove a file that is open to some process, or mapped
689 return (g_remove (path_name));
693 comics_document_finalize (GObject *object)
695 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
697 if (comics_document->flag_temp) {
698 if (comics_remove_dir (comics_document->dir) == -1)
699 g_warning (_("There was an error deleting %s"),
700 comics_document->dir);
701 g_free (comics_document->dir);
704 if (comics_document->archive)
705 g_free (comics_document->archive);
707 if (comics_document->page_names) {
708 g_slist_foreach (comics_document->page_names,
709 (GFunc) g_free, NULL);
710 g_slist_free (comics_document->page_names);
713 if (comics_document->selected_command)
714 g_free (comics_document->selected_command);
716 if (comics_document->extract_command)
717 g_free (comics_document->extract_command);
719 if (comics_document->list_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 gobject_class->finalize = comics_document_finalize;
732 static EvDocumentInfo *
733 comics_document_get_info (EvDocument *document)
735 EvDocumentInfo *info;
736 info = g_new0 (EvDocumentInfo, 1);
741 comics_document_document_iface_init (EvDocumentIface *iface)
743 iface->load = comics_document_load;
744 iface->save = comics_document_save;
745 iface->get_n_pages = comics_document_get_n_pages;
746 iface->get_page_size = comics_document_get_page_size;
747 iface->render = comics_document_render;
748 iface->get_info = comics_document_get_info;
752 comics_document_init (ComicsDocument *comics_document)
754 comics_document->archive = NULL;
755 comics_document->page_names = NULL;
756 comics_document->extract_command = NULL;
757 comics_document->n_pages = 0;
760 /* Returns a list of file extensions supported by gdk-pixbuf */
762 get_supported_image_extensions()
764 GSList *extensions = NULL;
765 GSList *formats = gdk_pixbuf_get_formats ();
768 for (l = formats; l != NULL; l = l->next) {
770 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
772 for (i = 0; ext[i] != NULL; i++) {
773 extensions = g_slist_append (extensions,
780 g_slist_free (formats);
785 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
789 GdkPixbuf *thumbnail;
791 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
794 GdkPixbuf *tmp_pixbuf = thumbnail;
796 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
797 g_object_unref (tmp_pixbuf);
804 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
809 gdouble page_width, page_height;
811 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
812 &page_width, &page_height);
814 if (rc->rotation == 90 || rc->rotation == 270) {
815 *width = (gint) (page_height * rc->scale);
816 *height = (gint) (page_width * rc->scale);
818 *width = (gint) (page_width * rc->scale);
819 *height = (gint) (page_height * rc->scale);
824 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
826 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
827 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
831 extract_argv (EvDocument *document, gint page)
833 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
835 char *command_line, *quoted_archive, *quoted_filename;
838 quoted_archive = g_shell_quote (comics_document->archive);
839 if (comics_document->regex_arg) {
840 quoted_filename = comics_regex_quote (
841 g_slist_nth_data (comics_document->page_names, page));
843 quoted_filename = g_shell_quote (
844 g_slist_nth_data (comics_document->page_names, page));
847 command_line = g_strdup_printf ("%s -- %s %s",
848 comics_document->extract_command,
852 g_shell_parse_argv (command_line, NULL, &argv, &err);
855 g_warning (_("Error %s"), err->message);
860 g_free (command_line);
861 g_free (quoted_archive);
862 g_free (quoted_filename);