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"
38 struct _ComicsDocumentClass
40 EvDocumentClass parent_class;
49 } ComicBookDecompressType;
51 struct _ComicsDocument
53 EvDocument parent_instance;
56 GPtrArray *page_names;
57 gchar *selected_command;
58 gchar *extract_command, *list_command, *decompress_tmp;
61 ComicBookDecompressType command_usage;
67 /* For perfomance reasons of 7z* we've choosen to decompress on the temporary
68 * directory instead of decompressing on the stdout */
71 char *extract, *list, *decompress_tmp;
74 } command_usage_def[] = {
75 {"%s p -c- -ierr", "%s vb -c- -- %s", NULL , FALSE, NO_OFFSET},
76 {NULL , "%s t %s" , "%s -xf %s %s" , TRUE , NO_OFFSET},
77 {"%s -p -C" , "%s -Z -1 -- %s" , NULL , TRUE , NO_OFFSET},
78 {NULL , "%s l -- %s" , "%s x -y %s -o%s", FALSE, OFFSET_7Z}
82 typedef struct _ComicsDocumentClass ComicsDocumentClass;
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_mkdtemp ("comics.XXXXXX", error);
210 if (comics_document->dir == NULL)
213 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
215 comics_document->decompress_tmp =
216 g_strdup_printf (command_usage_def[type].decompress_tmp,
217 comics_document->selected_command,
219 comics_document->dir);
220 g_free (quoted_file);
222 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
223 comics_document->selected_command, error))
228 g_free (quoted_file);
234 /* This function chooses an external command for decompressing a comic
235 * book based on its mime tipe. */
237 comics_check_decompress_command (gchar *mime_type,
238 ComicsDocument *comics_document,
242 gchar *std_out, *std_err;
246 /* FIXME, use proper cbr/cbz mime types once they're
247 * included in shared-mime-info */
249 if (!strcmp (mime_type, "application/x-cbr") ||
250 !strcmp (mime_type, "application/x-rar")) {
251 /* The RARLAB provides a no-charge proprietary (freeware)
252 * decompress-only client for Linux called unrar. Another
253 * option is a GPLv2-licensed command-line tool developed by
254 * the Gna! project. Confusingly enough, the free software RAR
255 * decoder is also named unrar. For this reason we need to add
256 * some lines for disambiguation. Sorry for the added the
257 * complexity but it's life :)
258 * Finally, some distributions, like Debian, rename this free
259 * option as unrar-free.
261 comics_document->selected_command =
262 g_find_program_in_path ("unrar");
263 if (comics_document->selected_command) {
264 /* We only use std_err to avoid printing useless error
265 * messages on the terminal */
267 g_spawn_command_line_sync (
268 comics_document->selected_command,
272 g_propagate_error (error, err);
275 /* I don't check retval status because RARLAB unrar
276 * doesn't have a way to return 0 without involving an
277 * operation with a file*/
278 } else if (WIFEXITED (retval)) {
279 if (g_strrstr (std_out,"freeware") != NULL)
280 /* The RARLAB freeware client */
281 comics_document->command_usage = RARLABS;
283 /* The Gna! free software client */
284 comics_document->command_usage = GNAUNRAR;
291 /* The Gna! free software client with Debian naming convention */
292 comics_document->selected_command =
293 g_find_program_in_path ("unrar-free");
294 if (comics_document->selected_command) {
295 comics_document->command_usage = GNAUNRAR;
299 } else if (!strcmp (mime_type, "application/x-cbz") ||
300 !strcmp (mime_type, "application/zip")) {
301 /* InfoZIP's unzip program */
302 comics_document->selected_command =
303 g_find_program_in_path ("unzip");
304 if (comics_document->selected_command) {
305 comics_document->command_usage = UNZIP;
309 } else if (!strcmp (mime_type, "application/x-cb7") ||
310 !strcmp (mime_type, "application/x-7z-compressed")) {
311 /* 7zr, 7za and 7z are the commands from the p7zip project able
312 * to decompress .7z files */
313 comics_document->selected_command =
314 g_find_program_in_path ("7zr");
315 if (comics_document->selected_command) {
316 comics_document->command_usage = P7ZIP;
319 comics_document->selected_command =
320 g_find_program_in_path ("7za");
321 if (comics_document->selected_command) {
322 comics_document->command_usage = P7ZIP;
325 comics_document->selected_command =
326 g_find_program_in_path ("7z");
327 if (comics_document->selected_command) {
328 comics_document->command_usage = P7ZIP;
334 EV_DOCUMENT_ERROR_INVALID,
335 _("Not a comic book MIME type: %s"),
339 g_set_error_literal (error,
341 EV_DOCUMENT_ERROR_INVALID,
342 _("Can't find an appropriate command to "
343 "decompress this type of comic book"));
348 sort_page_names (gconstpointer a,
351 return strcmp (* (const char **) a, * (const char **) b);
355 comics_document_load (EvDocument *document,
359 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
360 GSList *supported_extensions;
363 gchar **cb_files, *cb_file;
368 comics_document->archive = g_filename_from_uri (uri, NULL, error);
369 if (!comics_document->archive)
372 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
375 g_propagate_error (error, err);
377 g_set_error_literal (error,
379 EV_DOCUMENT_ERROR_INVALID,
380 _("Unknown MIME Type"));
386 if (!comics_check_decompress_command (mime_type, comics_document,
390 } else if (!comics_generate_command_lines (comics_document, error)) {
397 /* Get list of files in archive */
398 success = g_spawn_command_line_sync (comics_document->list_command,
399 &std_out, NULL, &retval, error);
403 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
404 g_set_error_literal (error,
406 EV_DOCUMENT_ERROR_INVALID,
407 _("File corrupted"));
411 /* FIXME: is this safe against filenames containing \n in the archive ? */
412 cb_files = g_strsplit (std_out, "\n", 0);
416 g_set_error_literal (error,
418 EV_DOCUMENT_ERROR_INVALID,
419 _("No files in archive"));
423 comics_document->page_names = g_ptr_array_sized_new (64);
425 supported_extensions = get_supported_image_extensions ();
426 for (i = 0; cb_files[i] != NULL; i++) {
427 if (comics_document->offset != NO_OFFSET) {
428 if (g_utf8_strlen (cb_files[i],-1) >
429 comics_document->offset) {
431 g_utf8_offset_to_pointer (cb_files[i],
432 comics_document->offset);
437 cb_file = cb_files[i];
439 gchar *suffix = g_strrstr (cb_file, ".");
442 suffix = g_ascii_strdown (suffix + 1, -1);
443 if (g_slist_find_custom (supported_extensions, suffix,
444 (GCompareFunc) strcmp) != NULL) {
445 g_ptr_array_add (comics_document->page_names,
446 g_strstrip (g_strdup (cb_file)));
450 g_strfreev (cb_files);
451 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
452 g_slist_free (supported_extensions);
454 if (comics_document->page_names->len == 0) {
457 EV_DOCUMENT_ERROR_INVALID,
458 _("No images found in archive %s"),
463 /* Now sort the pages */
464 g_ptr_array_sort (comics_document->page_names, sort_page_names);
471 comics_document_save (EvDocument *document,
475 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
477 return ev_xfer_uri_simple (comics_document->archive, uri, error);
481 comics_document_get_n_pages (EvDocument *document)
483 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
485 if (comics_document->page_names == NULL)
488 return comics_document->page_names->len;
492 comics_document_get_page_size (EvDocument *document,
497 GdkPixbufLoader *loader;
500 gboolean success, got_size = FALSE;
506 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
508 if (!comics_document->decompress_tmp) {
509 argv = extract_argv (document, page->index);
510 success = g_spawn_async_with_pipes (NULL, argv, NULL,
511 G_SPAWN_SEARCH_PATH |
512 G_SPAWN_STDERR_TO_DEV_NULL,
515 NULL, &outpipe, NULL, NULL);
517 g_return_if_fail (success == TRUE);
519 loader = gdk_pixbuf_loader_new ();
520 g_signal_connect (loader, "area-prepared",
521 G_CALLBACK (get_page_size_area_prepared_cb),
524 while (outpipe >= 0) {
525 bytes = read (outpipe, buf, 1024);
528 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
529 if (bytes <= 0 || got_size) {
532 gdk_pixbuf_loader_close (loader, NULL);
536 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
537 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
539 *width = gdk_pixbuf_get_width (pixbuf);
541 *height = gdk_pixbuf_get_height (pixbuf);
544 g_spawn_close_pid (child_pid);
545 g_object_unref (loader);
547 filename = g_build_filename (comics_document->dir,
548 (char *) comics_document->page_names->pdata[page->index],
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 *) comics_document->page_names->pdata[rc->page->index],
623 gdk_pixbuf_get_file_info (filename, &width, &height);
626 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
627 filename, width * (rc->scale) + 0.5,
628 height * (rc->scale) + 0.5, NULL),
633 return rotated_pixbuf;
636 static cairo_surface_t *
637 comics_document_render (EvDocument *document,
641 cairo_surface_t *surface;
643 pixbuf = comics_document_render_pixbuf (document, rc);
644 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
645 g_object_unref (pixbuf);
651 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
656 double *scale = data;
657 int w = (width * (*scale) + 0.5);
658 int h = (height * (*scale) + 0.5);
660 gdk_pixbuf_loader_set_size (loader, w, h);
664 * comics_remove_dir: Removes a directory recursively.
666 * 0 if it was successfully deleted,
667 * -1 if an error occurred
670 comics_remove_dir (gchar *path_name)
673 const gchar *filename;
674 gchar *filename_with_path;
676 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
677 content_dir = g_dir_open (path_name, 0, NULL);
678 filename = g_dir_read_name (content_dir);
681 g_build_filename (path_name,
683 comics_remove_dir (filename_with_path);
684 g_free (filename_with_path);
685 filename = g_dir_read_name (content_dir);
687 g_dir_close (content_dir);
689 /* Note from g_remove() documentation: on Windows, it is in general not
690 * possible to remove a file that is open to some process, or mapped
692 return (g_remove (path_name));
696 comics_document_finalize (GObject *object)
698 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
700 if (comics_document->decompress_tmp) {
701 if (comics_remove_dir (comics_document->dir) == -1)
702 g_warning (_("There was an error deleting “%s”."),
703 comics_document->dir);
704 g_free (comics_document->dir);
707 if (comics_document->page_names) {
708 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
709 g_ptr_array_free (comics_document->page_names, TRUE);
712 g_free (comics_document->archive);
713 g_free (comics_document->selected_command);
714 g_free (comics_document->extract_command);
715 g_free (comics_document->list_command);
717 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
721 comics_document_class_init (ComicsDocumentClass *klass)
723 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
724 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
726 gobject_class->finalize = comics_document_finalize;
728 ev_document_class->load = comics_document_load;
729 ev_document_class->save = comics_document_save;
730 ev_document_class->get_n_pages = comics_document_get_n_pages;
731 ev_document_class->get_page_size = comics_document_get_page_size;
732 ev_document_class->render = comics_document_render;
736 comics_document_init (ComicsDocument *comics_document)
738 comics_document->archive = NULL;
739 comics_document->page_names = NULL;
740 comics_document->extract_command = NULL;
743 /* Returns a list of file extensions supported by gdk-pixbuf */
745 get_supported_image_extensions()
747 GSList *extensions = NULL;
748 GSList *formats = gdk_pixbuf_get_formats ();
751 for (l = formats; l != NULL; l = l->next) {
753 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
755 for (i = 0; ext[i] != NULL; i++) {
756 extensions = g_slist_append (extensions,
763 g_slist_free (formats);
768 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
772 GdkPixbuf *thumbnail;
774 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
777 GdkPixbuf *tmp_pixbuf = thumbnail;
779 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
780 g_object_unref (tmp_pixbuf);
787 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
792 gdouble page_width, page_height;
794 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
795 &page_width, &page_height);
797 if (rc->rotation == 90 || rc->rotation == 270) {
798 *width = (gint) (page_height * rc->scale);
799 *height = (gint) (page_width * rc->scale);
801 *width = (gint) (page_width * rc->scale);
802 *height = (gint) (page_height * rc->scale);
807 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
809 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
810 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
814 extract_argv (EvDocument *document, gint page)
816 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
818 char *command_line, *quoted_archive, *quoted_filename;
821 if (page >= comics_document->page_names->len)
824 quoted_archive = g_shell_quote (comics_document->archive);
825 if (comics_document->regex_arg) {
826 quoted_filename = comics_regex_quote (comics_document->page_names->pdata[page]);
828 quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
831 command_line = g_strdup_printf ("%s -- %s %s",
832 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);