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"
45 } ComicBookDecompressType;
47 typedef struct _ComicsDocumentClass ComicsDocumentClass;
49 struct _ComicsDocumentClass
51 EvDocumentClass parent_class;
54 struct _ComicsDocument
56 EvDocument parent_instance;
59 GPtrArray *page_names;
60 gchar *selected_command;
61 gchar *extract_command, *list_command, *decompress_tmp;
64 ComicBookDecompressType command_usage;
70 /* For perfomance reasons of 7z* we've choosen to decompress on the temporary
71 * directory instead of decompressing on the stdout */
74 * @extract: command line arguments to pass to extract a file from the archive
75 * to stdout. The archive file and the file to extract will be appended after
77 * @list: command line arguments to list the archive contents
78 * @decompress_tmp: command line arguments to pass to extract the archive
79 * into a directory. The archive file and the directory to extract to will be
80 * appended after a "--".
81 * @regex_arg: whether the command expects one filename or accepts a regex (glob?)
82 * @offset: the byte offset of the filename on each line in the output of
83 * running the @list command
91 } ComicBookDecompressCommand;
93 static const ComicBookDecompressCommand command_usage_def[] = {
95 {"%s p -c- -ierr --", "%s vb -c- -- %s", NULL , FALSE, NO_OFFSET},
98 {NULL , "%s t %s" , "%s -xf %s %s" , TRUE , NO_OFFSET},
101 {"%s -p -C --" , "%s -Z -1 -- %s" , NULL , TRUE , NO_OFFSET},
104 {NULL , "%s l -- %s" , "%s x -y %s -o%s", FALSE, OFFSET_7Z},
107 {"%s -xOf" , "%s -tf %s" , NULL , FALSE, NO_OFFSET}
110 static void comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
112 static GSList* get_supported_image_extensions (void);
113 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
115 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
119 static char** extract_argv (EvDocument *document,
123 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
125 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
126 comics_document_document_thumbnails_iface_init);
130 comics_regex_quote (const char *s)
134 d = ret = g_malloc (strlen (s) * 4 + 3);
138 for (; *s; s++, d++) {
163 /* This function manages the command for decompressing a comic book */
165 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
166 const gchar *command,
170 gchar *std_out, *basename;
174 success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
175 NULL, &retval, &err);
176 basename = g_path_get_basename (command);
180 EV_DOCUMENT_ERROR_INVALID,
181 _("Error launching the command “%s” in order to "
182 "decompress the comic book: %s"),
186 } else if (WIFEXITED (retval)) {
187 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
194 EV_DOCUMENT_ERROR_INVALID,
195 _("The command “%s” failed at "
196 "decompressing the comic book."),
203 EV_DOCUMENT_ERROR_INVALID,
204 _("The command “%s” did not end normally."),
212 /* This function shows how to use the choosen command for decompressing a
213 * comic book file. It modifies fields of the ComicsDocument struct with
214 * this information */
216 comics_generate_command_lines (ComicsDocument *comics_document,
220 ComicBookDecompressType type;
222 type = comics_document->command_usage;
223 quoted_file = g_shell_quote (comics_document->archive);
225 comics_document->extract_command =
226 g_strdup_printf (command_usage_def[type].extract,
227 comics_document->selected_command);
228 comics_document->list_command =
229 g_strdup_printf (command_usage_def[type].list,
230 comics_document->selected_command,
232 comics_document->regex_arg = command_usage_def[type].regex_arg;
233 comics_document->offset = command_usage_def[type].offset;
234 if (command_usage_def[type].decompress_tmp) {
235 comics_document->dir = ev_mkdtemp ("evince-comics-XXXXXX", error);
236 if (comics_document->dir == NULL)
239 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
241 comics_document->decompress_tmp =
242 g_strdup_printf (command_usage_def[type].decompress_tmp,
243 comics_document->selected_command,
245 comics_document->dir);
246 g_free (quoted_file);
248 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
249 comics_document->selected_command, error))
254 g_free (quoted_file);
260 /* This function chooses an external command for decompressing a comic
261 * book based on its mime tipe. */
263 comics_check_decompress_command (gchar *mime_type,
264 ComicsDocument *comics_document,
268 gchar *std_out, *std_err;
272 /* FIXME, use proper cbr/cbz mime types once they're
273 * included in shared-mime-info */
275 if (!strcmp (mime_type, "application/x-cbr") ||
276 !strcmp (mime_type, "application/x-rar")) {
277 /* The RARLAB provides a no-charge proprietary (freeware)
278 * decompress-only client for Linux called unrar. Another
279 * option is a GPLv2-licensed command-line tool developed by
280 * the Gna! project. Confusingly enough, the free software RAR
281 * decoder is also named unrar. For this reason we need to add
282 * some lines for disambiguation. Sorry for the added the
283 * complexity but it's life :)
284 * Finally, some distributions, like Debian, rename this free
285 * option as unrar-free.
287 comics_document->selected_command =
288 g_find_program_in_path ("unrar");
289 if (comics_document->selected_command) {
290 /* We only use std_err to avoid printing useless error
291 * messages on the terminal */
293 g_spawn_command_line_sync (
294 comics_document->selected_command,
298 g_propagate_error (error, err);
301 /* I don't check retval status because RARLAB unrar
302 * doesn't have a way to return 0 without involving an
303 * operation with a file*/
304 } else if (WIFEXITED (retval)) {
305 if (g_strrstr (std_out,"freeware") != NULL)
306 /* The RARLAB freeware client */
307 comics_document->command_usage = RARLABS;
309 /* The Gna! free software client */
310 comics_document->command_usage = GNAUNRAR;
317 /* The Gna! free software client with Debian naming convention */
318 comics_document->selected_command =
319 g_find_program_in_path ("unrar-free");
320 if (comics_document->selected_command) {
321 comics_document->command_usage = GNAUNRAR;
325 } else if (!strcmp (mime_type, "application/x-cbz") ||
326 !strcmp (mime_type, "application/zip")) {
327 /* InfoZIP's unzip program */
328 comics_document->selected_command =
329 g_find_program_in_path ("unzip");
330 if (comics_document->selected_command) {
331 comics_document->command_usage = UNZIP;
335 } else if (!strcmp (mime_type, "application/x-cb7") ||
336 !strcmp (mime_type, "application/x-7z-compressed")) {
337 /* 7zr, 7za and 7z are the commands from the p7zip project able
338 * to decompress .7z files */
339 comics_document->selected_command =
340 g_find_program_in_path ("7zr");
341 if (comics_document->selected_command) {
342 comics_document->command_usage = P7ZIP;
345 comics_document->selected_command =
346 g_find_program_in_path ("7za");
347 if (comics_document->selected_command) {
348 comics_document->command_usage = P7ZIP;
351 comics_document->selected_command =
352 g_find_program_in_path ("7z");
353 if (comics_document->selected_command) {
354 comics_document->command_usage = P7ZIP;
357 } else if (!strcmp (mime_type, "application/x-cbt") ||
358 !strcmp (mime_type, "application/x-tar")) {
359 /* tar utility (Tape ARchive) */
360 comics_document->selected_command =
361 g_find_program_in_path ("tar");
362 if (comics_document->selected_command) {
363 comics_document->command_usage = TAR;
369 EV_DOCUMENT_ERROR_INVALID,
370 _("Not a comic book MIME type: %s"),
374 g_set_error_literal (error,
376 EV_DOCUMENT_ERROR_INVALID,
377 _("Can't find an appropriate command to "
378 "decompress this type of comic book"));
383 sort_page_names (gconstpointer a,
386 return strcmp (* (const char **) a, * (const char **) b);
390 comics_document_load (EvDocument *document,
394 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
395 GSList *supported_extensions;
398 gchar **cb_files, *cb_file;
403 comics_document->archive = g_filename_from_uri (uri, NULL, error);
404 if (!comics_document->archive)
407 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
410 g_propagate_error (error, err);
412 g_set_error_literal (error,
414 EV_DOCUMENT_ERROR_INVALID,
415 _("Unknown MIME Type"));
421 if (!comics_check_decompress_command (mime_type, comics_document,
425 } else if (!comics_generate_command_lines (comics_document, error)) {
432 /* Get list of files in archive */
433 success = g_spawn_command_line_sync (comics_document->list_command,
434 &std_out, NULL, &retval, error);
438 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
439 g_set_error_literal (error,
441 EV_DOCUMENT_ERROR_INVALID,
442 _("File corrupted"));
446 /* FIXME: is this safe against filenames containing \n in the archive ? */
447 cb_files = g_strsplit (std_out, "\n", 0);
451 g_set_error_literal (error,
453 EV_DOCUMENT_ERROR_INVALID,
454 _("No files in archive"));
458 comics_document->page_names = g_ptr_array_sized_new (64);
460 supported_extensions = get_supported_image_extensions ();
461 for (i = 0; cb_files[i] != NULL; i++) {
462 if (comics_document->offset != NO_OFFSET) {
463 if (g_utf8_strlen (cb_files[i],-1) >
464 comics_document->offset) {
466 g_utf8_offset_to_pointer (cb_files[i],
467 comics_document->offset);
472 cb_file = cb_files[i];
474 gchar *suffix = g_strrstr (cb_file, ".");
477 suffix = g_ascii_strdown (suffix + 1, -1);
478 if (g_slist_find_custom (supported_extensions, suffix,
479 (GCompareFunc) strcmp) != NULL) {
480 g_ptr_array_add (comics_document->page_names,
481 g_strstrip (g_strdup (cb_file)));
485 g_strfreev (cb_files);
486 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
487 g_slist_free (supported_extensions);
489 if (comics_document->page_names->len == 0) {
492 EV_DOCUMENT_ERROR_INVALID,
493 _("No images found in archive %s"),
498 /* Now sort the pages */
499 g_ptr_array_sort (comics_document->page_names, sort_page_names);
506 comics_document_save (EvDocument *document,
510 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
512 return ev_xfer_uri_simple (comics_document->archive, uri, error);
516 comics_document_get_n_pages (EvDocument *document)
518 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
520 if (comics_document->page_names == NULL)
523 return comics_document->page_names->len;
527 comics_document_get_page_size (EvDocument *document,
532 GdkPixbufLoader *loader;
535 gboolean success, got_size = FALSE;
541 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
543 if (!comics_document->decompress_tmp) {
544 argv = extract_argv (document, page->index);
545 success = g_spawn_async_with_pipes (NULL, argv, NULL,
546 G_SPAWN_SEARCH_PATH |
547 G_SPAWN_STDERR_TO_DEV_NULL,
550 NULL, &outpipe, NULL, NULL);
552 g_return_if_fail (success == TRUE);
554 loader = gdk_pixbuf_loader_new ();
555 g_signal_connect (loader, "area-prepared",
556 G_CALLBACK (get_page_size_area_prepared_cb),
559 while (outpipe >= 0) {
560 bytes = read (outpipe, buf, 1024);
563 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
564 if (bytes <= 0 || got_size) {
567 gdk_pixbuf_loader_close (loader, NULL);
571 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
572 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
574 *width = gdk_pixbuf_get_width (pixbuf);
576 *height = gdk_pixbuf_get_height (pixbuf);
579 g_spawn_close_pid (child_pid);
580 g_object_unref (loader);
582 filename = g_build_filename (comics_document->dir,
583 (char *) comics_document->page_names->pdata[page->index],
585 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
588 *width = gdk_pixbuf_get_width (pixbuf);
590 *height = gdk_pixbuf_get_height (pixbuf);
595 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
598 gboolean *got_size = data;
603 comics_document_render_pixbuf (EvDocument *document,
606 GdkPixbufLoader *loader;
607 GdkPixbuf *rotated_pixbuf;
616 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
618 if (!comics_document->decompress_tmp) {
619 argv = extract_argv (document, rc->page->index);
620 success = g_spawn_async_with_pipes (NULL, argv, NULL,
621 G_SPAWN_SEARCH_PATH |
622 G_SPAWN_STDERR_TO_DEV_NULL,
625 NULL, &outpipe, NULL, NULL);
627 g_return_val_if_fail (success == TRUE, NULL);
629 loader = gdk_pixbuf_loader_new ();
630 g_signal_connect (loader, "size-prepared",
631 G_CALLBACK (render_pixbuf_size_prepared_cb),
634 while (outpipe >= 0) {
635 bytes = read (outpipe, buf, 4096);
638 gdk_pixbuf_loader_write (loader, buf, bytes,
640 } else if (bytes <= 0) {
642 gdk_pixbuf_loader_close (loader, NULL);
647 rotated_pixbuf = gdk_pixbuf_rotate_simple (
648 gdk_pixbuf_loader_get_pixbuf (loader),
650 g_spawn_close_pid (child_pid);
651 g_object_unref (loader);
654 g_build_filename (comics_document->dir,
655 (char *) comics_document->page_names->pdata[rc->page->index],
658 gdk_pixbuf_get_file_info (filename, &width, &height);
661 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
662 filename, width * (rc->scale) + 0.5,
663 height * (rc->scale) + 0.5, NULL),
668 return rotated_pixbuf;
671 static cairo_surface_t *
672 comics_document_render (EvDocument *document,
676 cairo_surface_t *surface;
678 pixbuf = comics_document_render_pixbuf (document, rc);
679 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
680 g_object_unref (pixbuf);
686 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
691 double *scale = data;
692 int w = (width * (*scale) + 0.5);
693 int h = (height * (*scale) + 0.5);
695 gdk_pixbuf_loader_set_size (loader, w, h);
699 * comics_remove_dir: Removes a directory recursively.
701 * 0 if it was successfully deleted,
702 * -1 if an error occurred
705 comics_remove_dir (gchar *path_name)
708 const gchar *filename;
709 gchar *filename_with_path;
711 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
712 content_dir = g_dir_open (path_name, 0, NULL);
713 filename = g_dir_read_name (content_dir);
716 g_build_filename (path_name,
718 comics_remove_dir (filename_with_path);
719 g_free (filename_with_path);
720 filename = g_dir_read_name (content_dir);
722 g_dir_close (content_dir);
724 /* Note from g_remove() documentation: on Windows, it is in general not
725 * possible to remove a file that is open to some process, or mapped
727 return (g_remove (path_name));
731 comics_document_finalize (GObject *object)
733 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
735 if (comics_document->decompress_tmp) {
736 if (comics_remove_dir (comics_document->dir) == -1)
737 g_warning (_("There was an error deleting “%s”."),
738 comics_document->dir);
739 g_free (comics_document->dir);
742 if (comics_document->page_names) {
743 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
744 g_ptr_array_free (comics_document->page_names, TRUE);
747 g_free (comics_document->archive);
748 g_free (comics_document->selected_command);
749 g_free (comics_document->extract_command);
750 g_free (comics_document->list_command);
752 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
756 comics_document_class_init (ComicsDocumentClass *klass)
758 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
759 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
761 gobject_class->finalize = comics_document_finalize;
763 ev_document_class->load = comics_document_load;
764 ev_document_class->save = comics_document_save;
765 ev_document_class->get_n_pages = comics_document_get_n_pages;
766 ev_document_class->get_page_size = comics_document_get_page_size;
767 ev_document_class->render = comics_document_render;
771 comics_document_init (ComicsDocument *comics_document)
773 comics_document->archive = NULL;
774 comics_document->page_names = NULL;
775 comics_document->extract_command = NULL;
778 /* Returns a list of file extensions supported by gdk-pixbuf */
780 get_supported_image_extensions()
782 GSList *extensions = NULL;
783 GSList *formats = gdk_pixbuf_get_formats ();
786 for (l = formats; l != NULL; l = l->next) {
788 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
790 for (i = 0; ext[i] != NULL; i++) {
791 extensions = g_slist_append (extensions,
798 g_slist_free (formats);
803 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
807 GdkPixbuf *thumbnail;
809 thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
812 GdkPixbuf *tmp_pixbuf = thumbnail;
814 thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
815 g_object_unref (tmp_pixbuf);
822 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
827 gdouble page_width, page_height;
829 comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
830 &page_width, &page_height);
832 if (rc->rotation == 90 || rc->rotation == 270) {
833 *width = (gint) (page_height * rc->scale);
834 *height = (gint) (page_width * rc->scale);
836 *width = (gint) (page_width * rc->scale);
837 *height = (gint) (page_height * rc->scale);
842 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
844 iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
845 iface->get_dimensions = comics_document_thumbnails_get_dimensions;
849 extract_argv (EvDocument *document, gint page)
851 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
853 char *command_line, *quoted_archive, *quoted_filename;
856 if (page >= comics_document->page_names->len)
859 quoted_archive = g_shell_quote (comics_document->archive);
860 if (comics_document->regex_arg) {
861 quoted_filename = comics_regex_quote (comics_document->page_names->pdata[page]);
863 quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
866 command_line = g_strdup_printf ("%s %s %s",
867 comics_document->extract_command,
870 g_shell_parse_argv (command_line, NULL, &argv, &err);
873 g_warning (_("Error %s"), err->message);
878 g_free (command_line);
879 g_free (quoted_archive);
880 g_free (quoted_filename);