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 EvDocumentClass parent_class;
48 } ComicBookDecompressType;
50 struct _ComicsDocument
52 EvDocument parent_instance;
55 GPtrArray *page_names;
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_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
85 static GSList* get_supported_image_extensions (void);
86 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
88 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
92 static char** extract_argv (EvDocument *document,
96 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
98 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
99 comics_document_document_thumbnails_iface_init);
103 comics_regex_quote (const char *s)
107 d = ret = g_malloc (strlen (s) * 4 + 3);
111 for (; *s; s++, d++) {
136 /* This function manages the command for decompressing a comic book */
138 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
139 const gchar *command,
143 gchar *std_out, *basename;
147 success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
148 NULL, &retval, &err);
149 basename = g_path_get_basename (command);
153 EV_DOCUMENT_ERROR_INVALID,
154 _("Error launching the command “%s” in order to "
155 "decompress the comic book: %s"),
159 } else if (WIFEXITED (retval)) {
160 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
167 EV_DOCUMENT_ERROR_INVALID,
168 _("The command “%s” failed at "
169 "decompressing the comic book."),
176 EV_DOCUMENT_ERROR_INVALID,
177 _("The command “%s” did not end normally."),
185 /* This function shows how to use the choosen command for decompressing a
186 * comic book file. It modifies fields of the ComicsDocument struct with
187 * this information */
189 comics_generate_command_lines (ComicsDocument *comics_document,
193 ComicBookDecompressType type;
195 type = comics_document->command_usage;
196 quoted_file = g_shell_quote (comics_document->archive);
198 comics_document->extract_command =
199 g_strdup_printf (command_usage_def[type].extract,
200 comics_document->selected_command);
201 comics_document->list_command =
202 g_strdup_printf (command_usage_def[type].list,
203 comics_document->selected_command,
205 comics_document->regex_arg = command_usage_def[type].regex_arg;
206 comics_document->offset = command_usage_def[type].offset;
207 if (command_usage_def[type].decompress_tmp) {
208 comics_document->dir = ev_mkdtemp ("comics.XXXXXX", error);
209 if (comics_document->dir == NULL)
212 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
214 comics_document->decompress_tmp =
215 g_strdup_printf (command_usage_def[type].decompress_tmp,
216 comics_document->selected_command,
218 comics_document->dir);
219 g_free (quoted_file);
221 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
222 comics_document->selected_command, error))
227 g_free (quoted_file);
233 /* This function chooses an external command for decompressing a comic
234 * book based on its mime tipe. */
236 comics_check_decompress_command (gchar *mime_type,
237 ComicsDocument *comics_document,
241 gchar *std_out, *std_err;
245 /* FIXME, use proper cbr/cbz mime types once they're
246 * included in shared-mime-info */
248 if (!strcmp (mime_type, "application/x-cbr") ||
249 !strcmp (mime_type, "application/x-rar")) {
250 /* The RARLAB provides a no-charge proprietary (freeware)
251 * decompress-only client for Linux called unrar. Another
252 * option is a GPLv2-licensed command-line tool developed by
253 * the Gna! project. Confusingly enough, the free software RAR
254 * decoder is also named unrar. For this reason we need to add
255 * some lines for disambiguation. Sorry for the added the
256 * complexity but it's life :)
257 * Finally, some distributions, like Debian, rename this free
258 * option as unrar-free.
260 comics_document->selected_command =
261 g_find_program_in_path ("unrar");
262 if (comics_document->selected_command) {
263 /* We only use std_err to avoid printing useless error
264 * messages on the terminal */
266 g_spawn_command_line_sync (
267 comics_document->selected_command,
271 g_propagate_error (error, err);
274 /* I don't check retval status because RARLAB unrar
275 * doesn't have a way to return 0 without involving an
276 * operation with a file*/
277 } else if (WIFEXITED (retval)) {
278 if (g_strrstr (std_out,"freeware") != NULL)
279 /* The RARLAB freeware client */
280 comics_document->command_usage = RARLABS;
282 /* The Gna! free software client */
283 comics_document->command_usage = GNAUNRAR;
290 /* The Gna! free software client with Debian naming convention */
291 comics_document->selected_command =
292 g_find_program_in_path ("unrar-free");
293 if (comics_document->selected_command) {
294 comics_document->command_usage = GNAUNRAR;
298 } else if (!strcmp (mime_type, "application/x-cbz") ||
299 !strcmp (mime_type, "application/zip")) {
300 /* InfoZIP's unzip program */
301 comics_document->selected_command =
302 g_find_program_in_path ("unzip");
303 if (comics_document->selected_command) {
304 comics_document->command_usage = UNZIP;
308 } else if (!strcmp (mime_type, "application/x-cb7") ||
309 !strcmp (mime_type, "application/x-7z-compressed")) {
310 /* 7zr, 7za and 7z are the commands from the p7zip project able
311 * to decompress .7z files */
312 comics_document->selected_command =
313 g_find_program_in_path ("7zr");
314 if (comics_document->selected_command) {
315 comics_document->command_usage = P7ZIP;
318 comics_document->selected_command =
319 g_find_program_in_path ("7za");
320 if (comics_document->selected_command) {
321 comics_document->command_usage = P7ZIP;
324 comics_document->selected_command =
325 g_find_program_in_path ("7z");
326 if (comics_document->selected_command) {
327 comics_document->command_usage = P7ZIP;
333 EV_DOCUMENT_ERROR_INVALID,
334 _("Not a comic book MIME type: %s"),
338 g_set_error_literal (error,
340 EV_DOCUMENT_ERROR_INVALID,
341 _("Can't find an appropriate command to "
342 "decompress this type of comic book"));
347 sort_page_names (gconstpointer a,
350 return strcmp (* (const char **) a, * (const char **) b);
354 comics_document_load (EvDocument *document,
358 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
359 GSList *supported_extensions;
362 gchar **cb_files, *cb_file;
367 comics_document->archive = g_filename_from_uri (uri, NULL, error);
368 if (!comics_document->archive)
371 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
374 g_propagate_error (error, err);
376 g_set_error_literal (error,
378 EV_DOCUMENT_ERROR_INVALID,
379 _("Unknown MIME Type"));
385 if (!comics_check_decompress_command (mime_type, comics_document,
389 } else if (!comics_generate_command_lines (comics_document, error)) {
396 /* Get list of files in archive */
397 success = g_spawn_command_line_sync (comics_document->list_command,
398 &std_out, NULL, &retval, error);
402 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
403 g_set_error_literal (error,
405 EV_DOCUMENT_ERROR_INVALID,
406 _("File corrupted"));
410 /* FIXME: is this safe against filenames containing \n in the archive ? */
411 cb_files = g_strsplit (std_out, "\n", 0);
415 g_set_error_literal (error,
417 EV_DOCUMENT_ERROR_INVALID,
418 _("No files in archive"));
422 comics_document->page_names = g_ptr_array_sized_new (64);
424 supported_extensions = get_supported_image_extensions ();
425 for (i = 0; cb_files[i] != NULL; i++) {
426 if (comics_document->offset != NO_OFFSET) {
427 if (g_utf8_strlen (cb_files[i],-1) >
428 comics_document->offset) {
430 g_utf8_offset_to_pointer (cb_files[i],
431 comics_document->offset);
436 cb_file = cb_files[i];
438 gchar *suffix = g_strrstr (cb_file, ".");
441 suffix = g_ascii_strdown (suffix + 1, -1);
442 if (g_slist_find_custom (supported_extensions, suffix,
443 (GCompareFunc) strcmp) != NULL) {
444 g_ptr_array_add (comics_document->page_names,
445 g_strstrip (g_strdup (cb_file)));
449 g_strfreev (cb_files);
450 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
451 g_slist_free (supported_extensions);
453 if (comics_document->page_names->len == 0) {
456 EV_DOCUMENT_ERROR_INVALID,
457 _("No images found in archive %s"),
462 /* Now sort the pages */
463 g_ptr_array_sort (comics_document->page_names, sort_page_names);
470 comics_document_save (EvDocument *document,
474 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
476 return ev_xfer_uri_simple (comics_document->archive, uri, error);
480 comics_document_get_n_pages (EvDocument *document)
482 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
484 if (comics_document->page_names == NULL)
487 return comics_document->page_names->len;
491 comics_document_get_page_size (EvDocument *document,
496 GdkPixbufLoader *loader;
499 gboolean success, got_size = FALSE;
505 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
507 if (!comics_document->decompress_tmp) {
508 argv = extract_argv (document, page->index);
509 success = g_spawn_async_with_pipes (NULL, argv, NULL,
510 G_SPAWN_SEARCH_PATH |
511 G_SPAWN_STDERR_TO_DEV_NULL,
514 NULL, &outpipe, NULL, NULL);
516 g_return_if_fail (success == TRUE);
518 loader = gdk_pixbuf_loader_new ();
519 g_signal_connect (loader, "area-prepared",
520 G_CALLBACK (get_page_size_area_prepared_cb),
523 while (outpipe >= 0) {
524 bytes = read (outpipe, buf, 1024);
527 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
528 if (bytes <= 0 || got_size) {
531 gdk_pixbuf_loader_close (loader, NULL);
535 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
536 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
538 *width = gdk_pixbuf_get_width (pixbuf);
540 *height = gdk_pixbuf_get_height (pixbuf);
543 g_spawn_close_pid (child_pid);
544 g_object_unref (loader);
546 filename = g_build_filename (comics_document->dir,
547 (char *) comics_document->page_names->pdata[page->index],
549 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
552 *width = gdk_pixbuf_get_width (pixbuf);
554 *height = gdk_pixbuf_get_height (pixbuf);
559 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
562 gboolean *got_size = data;
567 comics_document_render_pixbuf (EvDocument *document,
570 GdkPixbufLoader *loader;
571 GdkPixbuf *rotated_pixbuf;
580 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
582 if (!comics_document->decompress_tmp) {
583 argv = extract_argv (document, rc->page->index);
584 success = g_spawn_async_with_pipes (NULL, argv, NULL,
585 G_SPAWN_SEARCH_PATH |
586 G_SPAWN_STDERR_TO_DEV_NULL,
589 NULL, &outpipe, NULL, NULL);
591 g_return_val_if_fail (success == TRUE, NULL);
593 loader = gdk_pixbuf_loader_new ();
594 g_signal_connect (loader, "size-prepared",
595 G_CALLBACK (render_pixbuf_size_prepared_cb),
598 while (outpipe >= 0) {
599 bytes = read (outpipe, buf, 4096);
602 gdk_pixbuf_loader_write (loader, buf, bytes,
604 } else if (bytes <= 0) {
606 gdk_pixbuf_loader_close (loader, NULL);
611 rotated_pixbuf = gdk_pixbuf_rotate_simple (
612 gdk_pixbuf_loader_get_pixbuf (loader),
614 g_spawn_close_pid (child_pid);
615 g_object_unref (loader);
618 g_build_filename (comics_document->dir,
619 (char *) comics_document->page_names->pdata[rc->page->index],
622 gdk_pixbuf_get_file_info (filename, &width, &height);
625 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
626 filename, width * (rc->scale) + 0.5,
627 height * (rc->scale) + 0.5, NULL),
632 return rotated_pixbuf;
635 static cairo_surface_t *
636 comics_document_render (EvDocument *document,
640 cairo_surface_t *surface;
642 pixbuf = comics_document_render_pixbuf (document, rc);
643 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
644 g_object_unref (pixbuf);
650 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
655 double *scale = data;
656 int w = (width * (*scale) + 0.5);
657 int h = (height * (*scale) + 0.5);
659 gdk_pixbuf_loader_set_size (loader, w, h);
663 * comics_remove_dir: Removes a directory recursively.
665 * 0 if it was successfully deleted,
666 * -1 if an error occurred
669 comics_remove_dir (gchar *path_name)
672 const gchar *filename;
673 gchar *filename_with_path;
675 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
676 content_dir = g_dir_open (path_name, 0, NULL);
677 filename = g_dir_read_name (content_dir);
680 g_build_filename (path_name,
682 comics_remove_dir (filename_with_path);
683 g_free (filename_with_path);
684 filename = g_dir_read_name (content_dir);
686 g_dir_close (content_dir);
688 /* Note from g_remove() documentation: on Windows, it is in general not
689 * possible to remove a file that is open to some process, or mapped
691 return (g_remove (path_name));
695 comics_document_finalize (GObject *object)
697 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
699 if (comics_document->decompress_tmp) {
700 if (comics_remove_dir (comics_document->dir) == -1)
701 g_warning (_("There was an error deleting “%s”."),
702 comics_document->dir);
703 g_free (comics_document->dir);
706 if (comics_document->page_names) {
707 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
708 g_ptr_array_free (comics_document->page_names, TRUE);
711 g_free (comics_document->archive);
712 g_free (comics_document->selected_command);
713 g_free (comics_document->extract_command);
714 g_free (comics_document->list_command);
716 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
720 comics_document_class_init (ComicsDocumentClass *klass)
722 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
723 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
725 gobject_class->finalize = comics_document_finalize;
727 ev_document_class->load = comics_document_load;
728 ev_document_class->save = comics_document_save;
729 ev_document_class->get_n_pages = comics_document_get_n_pages;
730 ev_document_class->get_page_size = comics_document_get_page_size;
731 ev_document_class->render = comics_document_render;
735 comics_document_init (ComicsDocument *comics_document)
737 comics_document->archive = NULL;
738 comics_document->page_names = NULL;
739 comics_document->extract_command = NULL;
742 /* Returns a list of file extensions supported by gdk-pixbuf */
744 get_supported_image_extensions()
746 GSList *extensions = NULL;
747 GSList *formats = gdk_pixbuf_get_formats ();
750 for (l = formats; l != NULL; l = l->next) {
752 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
754 for (i = 0; ext[i] != NULL; i++) {
755 extensions = g_slist_append (extensions,
762 g_slist_free (formats);
767 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
771 GdkPixbuf *thumbnail;
773 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
776 GdkPixbuf *tmp_pixbuf = thumbnail;
778 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
779 g_object_unref (tmp_pixbuf);
786 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
791 gdouble page_width, page_height;
793 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
794 &page_width, &page_height);
796 if (rc->rotation == 90 || rc->rotation == 270) {
797 *width = (gint) (page_height * rc->scale);
798 *height = (gint) (page_width * rc->scale);
800 *width = (gint) (page_width * rc->scale);
801 *height = (gint) (page_height * rc->scale);
806 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
808 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
809 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
813 extract_argv (EvDocument *document, gint page)
815 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
817 char *command_line, *quoted_archive, *quoted_filename;
820 if (page >= comics_document->page_names->len)
823 quoted_archive = g_shell_quote (comics_document->archive);
824 if (comics_document->regex_arg) {
825 quoted_filename = comics_regex_quote (comics_document->page_names->pdata[page]);
827 quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
830 command_line = g_strdup_printf ("%s -- %s %s",
831 comics_document->extract_command,
835 g_shell_parse_argv (command_line, NULL, &argv, &err);
838 g_warning (_("Error %s"), err->message);
843 g_free (command_line);
844 g_free (quoted_archive);
845 g_free (quoted_filename);