1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2009-2010 Juanjo Marín <juanj.marin@juntadeandalucia.es>
4 * Copyright (C) 2005, Teemu Tervo <teemu.tervo@gmx.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <glib/gi18n-lib.h>
30 #include <glib/gstdio.h>
34 # define WIFEXITED(x) ((x) != 3)
35 # define WEXITSTATUS(x) (x)
37 # include <sys/wait.h>
40 #include "comics-document.h"
41 #include "ev-document-misc.h"
42 #include "ev-file-helpers.h"
45 /* On windows g_spawn_command_line_sync reads stdout in O_BINARY mode, not in O_TEXT mode.
46 * As a consequence, newlines are in a platform dependent representation (\r\n). This
47 * might be considered a bug in glib.
61 } ComicBookDecompressType;
63 typedef struct _ComicsDocumentClass ComicsDocumentClass;
65 struct _ComicsDocumentClass
67 EvDocumentClass parent_class;
70 struct _ComicsDocument
72 EvDocument parent_instance;
75 GPtrArray *page_names;
76 gchar *selected_command, *alternative_command;
77 gchar *extract_command, *list_command, *decompress_tmp;
80 ComicBookDecompressType command_usage;
87 /* For perfomance reasons of 7z* we've choosen to decompress on the temporary
88 * directory instead of decompressing on the stdout */
91 * @extract: command line arguments to pass to extract a file from the archive
93 * @list: command line arguments to list the archive contents
94 * @decompress_tmp: command line arguments to pass to extract the archive
96 * @regex_arg: whether the command can accept regex expressions
97 * @offset: the position offset of the filename on each line in the output of
98 * running the @list command
103 char *decompress_tmp;
106 } ComicBookDecompressCommand;
108 static const ComicBookDecompressCommand command_usage_def[] = {
110 {"%s p -c- -ierr --", "%s vb -c- -- %s", NULL , FALSE, NO_OFFSET},
113 {NULL , "%s t %s" , "%s -xf %s %s" , FALSE, NO_OFFSET},
116 {"%s -p -C --" , "%s %s" , NULL , TRUE , OFFSET_ZIP},
119 {NULL , "%s l -- %s" , "%s x -y %s -o%s", FALSE, OFFSET_7Z},
122 {"%s -xOf" , "%s -tf %s" , NULL , FALSE, NO_OFFSET}
125 static GSList* get_supported_image_extensions (void);
126 static void get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
128 static void render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
132 static char** extract_argv (EvDocument *document,
136 EV_BACKEND_REGISTER (ComicsDocument, comics_document)
139 * comics_regex_quote:
140 * @unquoted_string: a literal string
142 * Quotes a string so unzip will not interpret the regex expressions of
143 * @unquoted_string. Basically, this functions uses [] to disable regex
144 * expressions. The return value must be freed with * g_free()
146 * Return value: quoted and disabled-regex string
149 comics_regex_quote (const gchar *unquoted_string)
154 dest = g_string_new ("'");
160 /* * matches a sequence of 0 or more characters */
162 /* ? matches exactly 1 charactere */
164 /* [...] matches any single character found inside
165 * the brackets. Disabling the first bracket is enough.
168 g_string_append (dest, "[");
169 g_string_append_c (dest, *p);
170 g_string_append (dest, "]");
172 /* Because \ escapes regex expressions that we are
173 * disabling for unzip, we need to disable \ too */
175 g_string_append (dest, "[\\\\]");
177 /* Escape single quote inside the string */
179 g_string_append (dest, "'\\''");
182 g_string_append_c (dest, *p);
187 g_string_append_c (dest, '\'');
188 return g_string_free (dest, FALSE);
192 /* This function manages the command for decompressing a comic book */
194 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
195 const gchar *command,
199 gchar *std_out, *basename;
203 success = g_spawn_command_line_sync (command_decompress_tmp, &std_out,
204 NULL, &retval, &err);
205 basename = g_path_get_basename (command);
209 EV_DOCUMENT_ERROR_INVALID,
210 _("Error launching the command “%s” in order to "
211 "decompress the comic book: %s"),
215 } else if (WIFEXITED (retval)) {
216 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
223 EV_DOCUMENT_ERROR_INVALID,
224 _("The command “%s” failed at "
225 "decompressing the comic book."),
232 EV_DOCUMENT_ERROR_INVALID,
233 _("The command “%s” did not end normally."),
241 /* This function shows how to use the choosen command for decompressing a
242 * comic book file. It modifies fields of the ComicsDocument struct with
243 * this information */
245 comics_generate_command_lines (ComicsDocument *comics_document,
248 gchar *quoted_file, *quoted_file_aux;
249 gchar *quoted_command;
250 ComicBookDecompressType type;
252 type = comics_document->command_usage;
253 comics_document->regex_arg = command_usage_def[type].regex_arg;
254 quoted_command = g_shell_quote (comics_document->selected_command);
255 if (comics_document->regex_arg) {
256 quoted_file = comics_regex_quote (comics_document->archive);
257 quoted_file_aux = g_shell_quote (comics_document->archive);
258 comics_document->list_command =
259 g_strdup_printf (command_usage_def[type].list,
260 comics_document->alternative_command,
262 g_free (quoted_file_aux);
264 quoted_file = g_shell_quote (comics_document->archive);
265 comics_document->list_command =
266 g_strdup_printf (command_usage_def[type].list,
267 quoted_command, quoted_file);
269 comics_document->extract_command =
270 g_strdup_printf (command_usage_def[type].extract,
272 comics_document->offset = command_usage_def[type].offset;
273 if (command_usage_def[type].decompress_tmp) {
274 comics_document->dir = ev_mkdtemp ("evince-comics-XXXXXX", error);
275 if (comics_document->dir == NULL)
278 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
280 comics_document->decompress_tmp =
281 g_strdup_printf (command_usage_def[type].decompress_tmp,
282 quoted_command, quoted_file,
283 comics_document->dir);
284 g_free (quoted_file);
285 g_free (quoted_command);
287 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
288 comics_document->selected_command, error))
293 g_free (quoted_file);
294 g_free (quoted_command);
300 /* This function chooses an external command for decompressing a comic
301 * book based on its mime tipe. */
303 comics_check_decompress_command (gchar *mime_type,
304 ComicsDocument *comics_document,
308 gchar *std_out, *std_err;
312 /* FIXME, use proper cbr/cbz mime types once they're
313 * included in shared-mime-info */
315 if (!strcmp (mime_type, "application/x-cbr") ||
316 !strcmp (mime_type, "application/x-rar")) {
317 /* The RARLAB provides a no-charge proprietary (freeware)
318 * decompress-only client for Linux called unrar. Another
319 * option is a GPLv2-licensed command-line tool developed by
320 * the Gna! project. Confusingly enough, the free software RAR
321 * decoder is also named unrar. For this reason we need to add
322 * some lines for disambiguation. Sorry for the added the
323 * complexity but it's life :)
324 * Finally, some distributions, like Debian, rename this free
325 * option as unrar-free.
327 comics_document->selected_command =
328 g_find_program_in_path ("unrar");
329 if (comics_document->selected_command) {
330 /* We only use std_err to avoid printing useless error
331 * messages on the terminal */
333 g_spawn_command_line_sync (
334 comics_document->selected_command,
338 g_propagate_error (error, err);
341 /* I don't check retval status because RARLAB unrar
342 * doesn't have a way to return 0 without involving an
343 * operation with a file*/
344 } else if (WIFEXITED (retval)) {
345 if (g_strrstr (std_out,"freeware") != NULL)
346 /* The RARLAB freeware client */
347 comics_document->command_usage = RARLABS;
349 /* The Gna! free software client */
350 comics_document->command_usage = GNAUNRAR;
357 /* The Gna! free software client with Debian naming convention */
358 comics_document->selected_command =
359 g_find_program_in_path ("unrar-free");
360 if (comics_document->selected_command) {
361 comics_document->command_usage = GNAUNRAR;
365 } else if (!strcmp (mime_type, "application/x-cbz") ||
366 !strcmp (mime_type, "application/zip")) {
367 /* InfoZIP's unzip program */
368 comics_document->selected_command =
369 g_find_program_in_path ("unzip");
370 comics_document->alternative_command =
371 g_find_program_in_path ("zipnote");
372 if (comics_document->selected_command &&
373 comics_document->alternative_command) {
374 comics_document->command_usage = UNZIP;
378 } else if (!strcmp (mime_type, "application/x-cb7") ||
379 !strcmp (mime_type, "application/x-7z-compressed")) {
380 /* 7zr, 7za and 7z are the commands from the p7zip project able
381 * to decompress .7z files */
382 comics_document->selected_command =
383 g_find_program_in_path ("7zr");
384 if (comics_document->selected_command) {
385 comics_document->command_usage = P7ZIP;
388 comics_document->selected_command =
389 g_find_program_in_path ("7za");
390 if (comics_document->selected_command) {
391 comics_document->command_usage = P7ZIP;
394 comics_document->selected_command =
395 g_find_program_in_path ("7z");
396 if (comics_document->selected_command) {
397 comics_document->command_usage = P7ZIP;
400 } else if (!strcmp (mime_type, "application/x-cbt") ||
401 !strcmp (mime_type, "application/x-tar")) {
402 /* tar utility (Tape ARchive) */
403 comics_document->selected_command =
404 g_find_program_in_path ("tar");
405 if (comics_document->selected_command) {
406 comics_document->command_usage = TAR;
412 EV_DOCUMENT_ERROR_INVALID,
413 _("Not a comic book MIME type: %s"),
417 g_set_error_literal (error,
419 EV_DOCUMENT_ERROR_INVALID,
420 _("Can't find an appropriate command to "
421 "decompress this type of comic book"));
426 sort_page_names (gconstpointer a,
429 return strcmp (* (const char **) a, * (const char **) b);
433 comics_document_load (EvDocument *document,
437 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
438 GSList *supported_extensions;
441 gchar **cb_files, *cb_file;
446 comics_document->archive = g_filename_from_uri (uri, NULL, error);
447 if (!comics_document->archive)
450 mime_type = ev_file_get_mime_type (uri, FALSE, &err);
453 g_propagate_error (error, err);
455 g_set_error_literal (error,
457 EV_DOCUMENT_ERROR_INVALID,
458 _("Unknown MIME Type"));
464 if (!comics_check_decompress_command (mime_type, comics_document,
468 } else if (!comics_generate_command_lines (comics_document, error)) {
475 /* Get list of files in archive */
476 success = g_spawn_command_line_sync (comics_document->list_command,
477 &std_out, NULL, &retval, error);
481 } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
482 g_set_error_literal (error,
484 EV_DOCUMENT_ERROR_INVALID,
485 _("File corrupted"));
489 /* FIXME: is this safe against filenames containing \n in the archive ? */
490 cb_files = g_strsplit (std_out, EV_EOL, 0);
495 g_set_error_literal (error,
497 EV_DOCUMENT_ERROR_INVALID,
498 _("No files in archive"));
502 comics_document->page_names = g_ptr_array_sized_new (64);
504 supported_extensions = get_supported_image_extensions ();
505 for (i = 0; cb_files[i] != NULL; i++) {
506 if (comics_document->offset != NO_OFFSET) {
507 if (g_utf8_strlen (cb_files[i],-1) >
508 comics_document->offset) {
510 g_utf8_offset_to_pointer (cb_files[i],
511 comics_document->offset);
516 cb_file = cb_files[i];
518 gchar *suffix = g_strrstr (cb_file, ".");
521 suffix = g_ascii_strdown (suffix + 1, -1);
522 if (g_slist_find_custom (supported_extensions, suffix,
523 (GCompareFunc) strcmp) != NULL) {
524 g_ptr_array_add (comics_document->page_names,
525 g_strstrip (g_strdup (cb_file)));
529 g_strfreev (cb_files);
530 g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
531 g_slist_free (supported_extensions);
533 if (comics_document->page_names->len == 0) {
536 EV_DOCUMENT_ERROR_INVALID,
537 _("No images found in archive %s"),
542 /* Now sort the pages */
543 g_ptr_array_sort (comics_document->page_names, sort_page_names);
550 comics_document_save (EvDocument *document,
554 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
556 return ev_xfer_uri_simple (comics_document->archive, uri, error);
560 comics_document_get_n_pages (EvDocument *document)
562 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
564 if (comics_document->page_names == NULL)
567 return comics_document->page_names->len;
571 comics_document_get_page_size (EvDocument *document,
576 GdkPixbufLoader *loader;
579 gboolean success, got_size = FALSE;
585 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
587 if (!comics_document->decompress_tmp) {
588 argv = extract_argv (document, page->index);
589 success = g_spawn_async_with_pipes (NULL, argv, NULL,
590 G_SPAWN_SEARCH_PATH |
591 G_SPAWN_STDERR_TO_DEV_NULL,
594 NULL, &outpipe, NULL, NULL);
596 g_return_if_fail (success == TRUE);
598 loader = gdk_pixbuf_loader_new ();
599 g_signal_connect (loader, "area-prepared",
600 G_CALLBACK (get_page_size_area_prepared_cb),
603 while (outpipe >= 0) {
604 bytes = read (outpipe, buf, 1024);
607 gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
608 if (bytes <= 0 || got_size) {
611 gdk_pixbuf_loader_close (loader, NULL);
615 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
616 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
618 *width = gdk_pixbuf_get_width (pixbuf);
620 *height = gdk_pixbuf_get_height (pixbuf);
623 g_spawn_close_pid (child_pid);
624 g_object_unref (loader);
626 filename = g_build_filename (comics_document->dir,
627 (char *) comics_document->page_names->pdata[page->index],
629 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
632 *width = gdk_pixbuf_get_width (pixbuf);
634 *height = gdk_pixbuf_get_height (pixbuf);
639 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
642 gboolean *got_size = data;
647 comics_document_render_pixbuf (EvDocument *document,
650 GdkPixbufLoader *loader;
651 GdkPixbuf *rotated_pixbuf;
660 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
662 if (!comics_document->decompress_tmp) {
663 argv = extract_argv (document, rc->page->index);
664 success = g_spawn_async_with_pipes (NULL, argv, NULL,
665 G_SPAWN_SEARCH_PATH |
666 G_SPAWN_STDERR_TO_DEV_NULL,
669 NULL, &outpipe, NULL, NULL);
671 g_return_val_if_fail (success == TRUE, NULL);
673 loader = gdk_pixbuf_loader_new ();
674 g_signal_connect (loader, "size-prepared",
675 G_CALLBACK (render_pixbuf_size_prepared_cb),
678 while (outpipe >= 0) {
679 bytes = read (outpipe, buf, 4096);
682 gdk_pixbuf_loader_write (loader, buf, bytes,
684 } else if (bytes <= 0) {
686 gdk_pixbuf_loader_close (loader, NULL);
691 rotated_pixbuf = gdk_pixbuf_rotate_simple (
692 gdk_pixbuf_loader_get_pixbuf (loader),
694 g_spawn_close_pid (child_pid);
695 g_object_unref (loader);
698 g_build_filename (comics_document->dir,
699 (char *) comics_document->page_names->pdata[rc->page->index],
702 gdk_pixbuf_get_file_info (filename, &width, &height);
705 gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
706 filename, width * (rc->scale) + 0.5,
707 height * (rc->scale) + 0.5, NULL),
712 return rotated_pixbuf;
715 static cairo_surface_t *
716 comics_document_render (EvDocument *document,
720 cairo_surface_t *surface;
722 pixbuf = comics_document_render_pixbuf (document, rc);
723 surface = ev_document_misc_surface_from_pixbuf (pixbuf);
724 g_object_unref (pixbuf);
730 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
735 double *scale = data;
736 int w = (width * (*scale) + 0.5);
737 int h = (height * (*scale) + 0.5);
739 gdk_pixbuf_loader_set_size (loader, w, h);
743 * comics_remove_dir: Removes a directory recursively.
745 * 0 if it was successfully deleted,
746 * -1 if an error occurred
749 comics_remove_dir (gchar *path_name)
752 const gchar *filename;
753 gchar *filename_with_path;
755 if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
756 content_dir = g_dir_open (path_name, 0, NULL);
757 filename = g_dir_read_name (content_dir);
760 g_build_filename (path_name,
762 comics_remove_dir (filename_with_path);
763 g_free (filename_with_path);
764 filename = g_dir_read_name (content_dir);
766 g_dir_close (content_dir);
768 /* Note from g_remove() documentation: on Windows, it is in general not
769 * possible to remove a file that is open to some process, or mapped
771 return (g_remove (path_name));
775 comics_document_finalize (GObject *object)
777 ComicsDocument *comics_document = COMICS_DOCUMENT (object);
779 if (comics_document->decompress_tmp) {
780 if (comics_remove_dir (comics_document->dir) == -1)
781 g_warning (_("There was an error deleting “%s”."),
782 comics_document->dir);
783 g_free (comics_document->dir);
786 if (comics_document->page_names) {
787 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
788 g_ptr_array_free (comics_document->page_names, TRUE);
791 g_free (comics_document->archive);
792 g_free (comics_document->selected_command);
793 g_free (comics_document->alternative_command);
794 g_free (comics_document->extract_command);
795 g_free (comics_document->list_command);
797 G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
801 comics_document_class_init (ComicsDocumentClass *klass)
803 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
804 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
806 gobject_class->finalize = comics_document_finalize;
808 ev_document_class->load = comics_document_load;
809 ev_document_class->save = comics_document_save;
810 ev_document_class->get_n_pages = comics_document_get_n_pages;
811 ev_document_class->get_page_size = comics_document_get_page_size;
812 ev_document_class->render = comics_document_render;
816 comics_document_init (ComicsDocument *comics_document)
818 comics_document->archive = NULL;
819 comics_document->page_names = NULL;
820 comics_document->extract_command = NULL;
823 /* Returns a list of file extensions supported by gdk-pixbuf */
825 get_supported_image_extensions()
827 GSList *extensions = NULL;
828 GSList *formats = gdk_pixbuf_get_formats ();
831 for (l = formats; l != NULL; l = l->next) {
833 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
835 for (i = 0; ext[i] != NULL; i++) {
836 extensions = g_slist_append (extensions,
843 g_slist_free (formats);
848 extract_argv (EvDocument *document, gint page)
850 ComicsDocument *comics_document = COMICS_DOCUMENT (document);
852 char *command_line, *quoted_archive, *quoted_filename;
855 if (page >= comics_document->page_names->len)
858 if (comics_document->regex_arg) {
859 quoted_archive = comics_regex_quote (comics_document->archive);
861 comics_regex_quote (comics_document->page_names->pdata[page]);
863 quoted_archive = g_shell_quote (comics_document->archive);
864 quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
867 command_line = g_strdup_printf ("%s %s %s",
868 comics_document->extract_command,
871 g_shell_parse_argv (command_line, NULL, &argv, &err);
874 g_warning (_("Error %s"), err->message);
879 g_free (command_line);
880 g_free (quoted_archive);
881 g_free (quoted_filename);