]> www.fi.muni.cz Git - evince.git/blob - backend/comics/comics-document.c
[windows] Make comics backend also compile on Windows
[evince.git] / backend / comics / comics-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Copyright (C) 2009, Juanjo Marín <juanj.marin@juntadeandalucia.es>
4  * Copyright (C) 2005, Teemu Tervo <teemu.tervo@gmx.net>
5  *
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)
9  * any later version.
10  *
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.
15  *
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <errno.h>
27
28 #include <glib.h>
29 #include <glib/gi18n-lib.h>
30 #include <glib/gstdio.h>
31 #include <gio/gio.h>
32
33 #ifdef G_OS_WIN32
34 # define WIFEXITED(x) ((x) != 3)
35 # define WEXITSTATUS(x) (x)
36 #else
37 # include <sys/wait.h>
38 #endif
39
40 #include "comics-document.h"
41 #include "ev-document-misc.h"
42 #include "ev-document-thumbnails.h"
43 #include "ev-file-helpers.h"
44
45 typedef enum
46 {
47         RARLABS,
48         GNAUNRAR,
49         UNZIP,
50         P7ZIP,
51         TAR
52 } ComicBookDecompressType;
53
54 typedef struct _ComicsDocumentClass ComicsDocumentClass;
55
56 struct _ComicsDocumentClass
57 {
58         EvDocumentClass parent_class;
59 };
60
61 struct _ComicsDocument
62 {
63         EvDocument parent_instance;
64
65         gchar    *archive, *dir;
66         GPtrArray *page_names;
67         gchar    *selected_command;
68         gchar    *extract_command, *list_command, *decompress_tmp;
69         gint     offset;
70         ComicBookDecompressType command_usage;
71 };
72
73 #define OFFSET_7Z 53
74 #define NO_OFFSET 0
75
76 /* For perfomance reasons of 7z* we've choosen to decompress on the temporary 
77  * directory instead of decompressing on the stdout */
78
79 /**
80  * @extract: command line arguments to pass to extract a file from the archive
81  *   to stdout.
82  * @list: command line arguments to list the archive contents
83  * @decompress_tmp: command line arguments to pass to extract the archive
84  *   into a directory.
85  * @offset: the position offset of the filename on each line in the output of
86  *   running the @list command
87  */
88 typedef struct {
89         char *extract;
90         char *list;
91         char *decompress_tmp;
92         gint offset;
93 } ComicBookDecompressCommand;
94
95 static const ComicBookDecompressCommand command_usage_def[] = {
96         /* RARLABS unrar */
97         {"%s p -c- -ierr --", "%s vb -c- -- %s", NULL             , NO_OFFSET},
98
99         /* GNA! unrar */
100         {NULL               , "%s t %s"        , "%s -xf %s %s"   , NO_OFFSET},
101
102         /* unzip */
103         {"%s -p -C --"      , "%s -Z -1 -- %s" , NULL             , NO_OFFSET},
104
105         /* 7zip */
106         {NULL               , "%s l -- %s"     , "%s x -y %s -o%s", OFFSET_7Z},
107
108         /* tar */
109         {"%s -xOf"          , "%s -tf %s"      , NULL             , NO_OFFSET}
110 };
111
112 static void       comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
113
114 static GSList*    get_supported_image_extensions (void);
115 static void       get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
116                                                   gpointer data);
117 static void       render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
118                                                   gint width,
119                                                   gint height,
120                                                   gpointer data);
121 static char**     extract_argv                   (EvDocument *document,
122                                                   gint page);
123
124
125 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
126         {
127                 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
128                                                 comics_document_document_thumbnails_iface_init);
129         } );
130
131
132 /* This function manages the command for decompressing a comic book */
133 static gboolean 
134 comics_decompress_temp_dir (const gchar *command_decompress_tmp,
135                             const gchar *command, 
136                             GError      **error)
137 {
138         gboolean success;
139         gchar *std_out, *basename;
140         GError *err = NULL;
141         gint retval;
142         
143         success = g_spawn_command_line_sync (command_decompress_tmp, &std_out, 
144                                              NULL, &retval, &err);
145         basename = g_path_get_basename (command);
146         if (!success) {
147                 g_set_error (error,
148                              EV_DOCUMENT_ERROR, 
149                              EV_DOCUMENT_ERROR_INVALID,
150                              _("Error launching the command “%s” in order to "
151                              "decompress the comic book: %s"),
152                              basename,
153                              err->message);
154                 g_error_free (err);
155         } else if (WIFEXITED (retval)) {
156                 if (WEXITSTATUS (retval) == EXIT_SUCCESS) {
157                         g_free (std_out);
158                         g_free (basename);
159                         return TRUE;
160                 } else {
161                         g_set_error (error,
162                                      EV_DOCUMENT_ERROR,
163                                      EV_DOCUMENT_ERROR_INVALID,
164                                      _("The command “%s” failed at "
165                                      "decompressing the comic book."),
166                                      basename);
167                         g_free (std_out);
168                 }
169         } else {
170                 g_set_error (error,
171                              EV_DOCUMENT_ERROR,
172                              EV_DOCUMENT_ERROR_INVALID,
173                              _("The command “%s” did not end normally."),
174                              basename);
175                 g_free (std_out);
176         }
177         g_free (basename);
178         return FALSE;
179 }
180
181 /* This function shows how to use the choosen command for decompressing a
182  * comic book file. It modifies fields of the ComicsDocument struct with 
183  * this information */
184 static gboolean 
185 comics_generate_command_lines (ComicsDocument *comics_document, 
186                                GError         **error)
187 {
188         gchar *quoted_file;
189         ComicBookDecompressType type;
190         
191         type = comics_document->command_usage;
192         quoted_file = g_shell_quote (comics_document->archive);
193         
194         comics_document->extract_command = 
195                             g_strdup_printf (command_usage_def[type].extract, 
196                                              comics_document->selected_command);
197         comics_document->list_command =
198                             g_strdup_printf (command_usage_def[type].list, 
199                                              comics_document->selected_command, 
200                                              quoted_file);
201         comics_document->offset = command_usage_def[type].offset;
202         if (command_usage_def[type].decompress_tmp) {
203                 comics_document->dir = ev_mkdtemp ("evince-comics-XXXXXX", error);
204                 if (comics_document->dir == NULL)
205                         return FALSE;
206
207                 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
208
209                 comics_document->decompress_tmp =
210                         g_strdup_printf (command_usage_def[type].decompress_tmp, 
211                                          comics_document->selected_command, 
212                                          quoted_file, 
213                                          comics_document->dir);
214                 g_free (quoted_file);
215
216                 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
217                     comics_document->selected_command, error))
218                         return FALSE;
219                 else
220                         return TRUE;
221         } else {
222                 g_free (quoted_file);
223                 return TRUE;
224         }
225
226 }
227
228 /* This function chooses an external command for decompressing a comic 
229  * book based on its mime tipe. */
230 static gboolean 
231 comics_check_decompress_command (gchar          *mime_type, 
232                                  ComicsDocument *comics_document,
233                                  GError         **error)
234 {
235         gboolean success;
236         gchar *std_out, *std_err;
237         gint retval;
238         GError *err = NULL;
239         
240         /* FIXME, use proper cbr/cbz mime types once they're
241          * included in shared-mime-info */
242         
243         if (!strcmp (mime_type, "application/x-cbr") ||
244             !strcmp (mime_type, "application/x-rar")) {
245                 /* The RARLAB provides a no-charge proprietary (freeware) 
246                 * decompress-only client for Linux called unrar. Another 
247                 * option is a GPLv2-licensed command-line tool developed by 
248                 * the Gna! project. Confusingly enough, the free software RAR 
249                 * decoder is also named unrar. For this reason we need to add 
250                 * some lines for disambiguation. Sorry for the added the 
251                 * complexity but it's life :)
252                 * Finally, some distributions, like Debian, rename this free 
253                 * option as unrar-free. 
254                 * */
255                 comics_document->selected_command = 
256                                         g_find_program_in_path ("unrar");
257                 if (comics_document->selected_command) {
258                         /* We only use std_err to avoid printing useless error 
259                          * messages on the terminal */
260                         success = 
261                                 g_spawn_command_line_sync (
262                                               comics_document->selected_command, 
263                                                            &std_out, &std_err,
264                                                            &retval, &err);
265                         if (!success) {
266                                 g_propagate_error (error, err);
267                                 g_error_free (err);
268                                 return FALSE;
269                         /* I don't check retval status because RARLAB unrar 
270                          * doesn't have a way to return 0 without involving an 
271                          * operation with a file*/
272                         } else if (WIFEXITED (retval)) {
273                                 if (g_strrstr (std_out,"freeware") != NULL)
274                                         /* The RARLAB freeware client */
275                                         comics_document->command_usage = RARLABS;
276                                 else
277                                         /* The Gna! free software client */
278                                         comics_document->command_usage = GNAUNRAR;
279
280                                 g_free (std_out);
281                                 g_free (std_err);
282                                 return TRUE;
283                         }
284                 }
285                 /* The Gna! free software client with Debian naming convention */
286                 comics_document->selected_command = 
287                                 g_find_program_in_path ("unrar-free");
288                 if (comics_document->selected_command) {
289                         comics_document->command_usage = GNAUNRAR;
290                         return TRUE;
291                 }
292
293         } else if (!strcmp (mime_type, "application/x-cbz") ||
294                    !strcmp (mime_type, "application/zip")) {
295                 /* InfoZIP's unzip program */
296                 comics_document->selected_command = 
297                                 g_find_program_in_path ("unzip");
298                 if (comics_document->selected_command) {
299                         comics_document->command_usage = UNZIP;
300                         return TRUE;
301                 }
302
303         } else if (!strcmp (mime_type, "application/x-cb7") ||
304                    !strcmp (mime_type, "application/x-7z-compressed")) {
305                 /* 7zr, 7za and 7z are the commands from the p7zip project able 
306                  * to decompress .7z files */ 
307                         comics_document->selected_command = 
308                                 g_find_program_in_path ("7zr");
309                         if (comics_document->selected_command) {
310                                 comics_document->command_usage = P7ZIP;
311                                 return TRUE;
312                         }
313                         comics_document->selected_command = 
314                                 g_find_program_in_path ("7za");
315                         if (comics_document->selected_command) {
316                                 comics_document->command_usage = P7ZIP;
317                                 return TRUE;
318                         }
319                         comics_document->selected_command = 
320                                 g_find_program_in_path ("7z");
321                         if (comics_document->selected_command) {
322                                 comics_document->command_usage = P7ZIP;
323                                 return TRUE;
324                         }
325         } else if (!strcmp (mime_type, "application/x-cbt") ||
326                    !strcmp (mime_type, "application/x-tar")) {
327                 /* tar utility (Tape ARchive) */
328                 comics_document->selected_command =
329                                 g_find_program_in_path ("tar");
330                 if (comics_document->selected_command) {
331                         comics_document->command_usage = TAR;
332                         return TRUE;
333                 }
334         } else {
335                 g_set_error (error,
336                              EV_DOCUMENT_ERROR,
337                              EV_DOCUMENT_ERROR_INVALID,
338                              _("Not a comic book MIME type: %s"),
339                              mime_type);
340                              return FALSE;
341         }
342         g_set_error_literal (error,
343                              EV_DOCUMENT_ERROR,
344                              EV_DOCUMENT_ERROR_INVALID,
345                              _("Can't find an appropriate command to "
346                              "decompress this type of comic book"));
347         return FALSE;
348 }
349
350 static int
351 sort_page_names (gconstpointer a,
352                  gconstpointer b)
353 {
354   return strcmp (* (const char **) a, * (const char **) b);
355 }
356
357 static gboolean
358 comics_document_load (EvDocument *document,
359                       const char *uri,
360                       GError    **error)
361 {
362         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
363         GSList *supported_extensions;
364         gchar *std_out;
365         gchar *mime_type;
366         gchar **cb_files, *cb_file;
367         gboolean success;
368         int i, retval;
369         GError *err = NULL;
370
371         comics_document->archive = g_filename_from_uri (uri, NULL, error);
372         if (!comics_document->archive)
373                 return FALSE;
374
375         mime_type = ev_file_get_mime_type (uri, FALSE, &err);
376         if (!mime_type) {
377                 if (err) {
378                         g_propagate_error (error, err);
379                 } else {
380                         g_set_error_literal (error,
381                                              EV_DOCUMENT_ERROR,
382                                              EV_DOCUMENT_ERROR_INVALID,
383                                              _("Unknown MIME Type"));
384                 }
385
386                 return FALSE;
387         }
388         
389         if (!comics_check_decompress_command (mime_type, comics_document, 
390         error)) {       
391                 g_free (mime_type);
392                 return FALSE;
393         } else if (!comics_generate_command_lines (comics_document, error)) {
394                    g_free (mime_type);
395                 return FALSE;
396         }
397
398         g_free (mime_type);
399
400         /* Get list of files in archive */
401         success = g_spawn_command_line_sync (comics_document->list_command,
402                                              &std_out, NULL, &retval, error);
403
404         if (!success) {
405                 return FALSE;
406         } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
407                 g_set_error_literal (error,
408                                      EV_DOCUMENT_ERROR,
409                                      EV_DOCUMENT_ERROR_INVALID,
410                                      _("File corrupted"));
411                 return FALSE;
412         }
413
414         /* FIXME: is this safe against filenames containing \n in the archive ? */
415         cb_files = g_strsplit (std_out, "\n", 0);
416         g_free (std_out);
417
418         if (!cb_files) {
419                 g_set_error_literal (error,
420                                      EV_DOCUMENT_ERROR,
421                                      EV_DOCUMENT_ERROR_INVALID,
422                                      _("No files in archive"));
423                 return FALSE;
424         }
425
426         comics_document->page_names = g_ptr_array_sized_new (64);
427
428         supported_extensions = get_supported_image_extensions ();
429         for (i = 0; cb_files[i] != NULL; i++) {
430                 if (comics_document->offset != NO_OFFSET) {
431                         if (g_utf8_strlen (cb_files[i],-1) > 
432                             comics_document->offset) {
433                                 cb_file = 
434                                         g_utf8_offset_to_pointer (cb_files[i], 
435                                                        comics_document->offset);
436                         } else {
437                                 continue;
438                         }
439                 } else {
440                         cb_file = cb_files[i];
441                 }
442                 gchar *suffix = g_strrstr (cb_file, ".");
443                 if (!suffix)
444                         continue;
445                 suffix = g_ascii_strdown (suffix + 1, -1);
446                 if (g_slist_find_custom (supported_extensions, suffix,
447                                          (GCompareFunc) strcmp) != NULL) {
448                         g_ptr_array_add (comics_document->page_names,
449                                          g_strstrip (g_strdup (cb_file)));
450                 }
451                 g_free (suffix);
452         }
453         g_strfreev (cb_files);
454         g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
455         g_slist_free (supported_extensions);
456
457         if (comics_document->page_names->len == 0) {
458                 g_set_error (error,
459                              EV_DOCUMENT_ERROR,
460                              EV_DOCUMENT_ERROR_INVALID,
461                              _("No images found in archive %s"),
462                              uri);
463                 return FALSE;
464         }
465
466         /* Now sort the pages */
467         g_ptr_array_sort (comics_document->page_names, sort_page_names);
468
469         return TRUE;
470 }
471
472
473 static gboolean
474 comics_document_save (EvDocument *document,
475                       const char *uri,
476                       GError    **error)
477 {
478         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
479
480         return ev_xfer_uri_simple (comics_document->archive, uri, error);
481 }
482
483 static int
484 comics_document_get_n_pages (EvDocument *document)
485 {
486         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
487
488         if (comics_document->page_names == NULL)
489                 return 0;
490
491         return comics_document->page_names->len;
492 }
493
494 static void
495 comics_document_get_page_size (EvDocument *document,
496                                EvPage     *page,
497                                double     *width,
498                                double     *height)
499 {
500         GdkPixbufLoader *loader;
501         char **argv;
502         guchar buf[1024];
503         gboolean success, got_size = FALSE;
504         gint outpipe = -1;
505         GPid child_pid = -1;
506         gssize bytes;
507         GdkPixbuf *pixbuf;
508         gchar *filename;
509         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
510         
511         if (!comics_document->decompress_tmp) {
512                 argv = extract_argv (document, page->index);
513                 success = g_spawn_async_with_pipes (NULL, argv, NULL,
514                                                     G_SPAWN_SEARCH_PATH | 
515                                                     G_SPAWN_STDERR_TO_DEV_NULL,
516                                                     NULL, NULL,
517                                                     &child_pid,
518                                                     NULL, &outpipe, NULL, NULL);
519                 g_strfreev (argv);
520                 g_return_if_fail (success == TRUE);
521
522                 loader = gdk_pixbuf_loader_new ();
523                 g_signal_connect (loader, "area-prepared",
524                                   G_CALLBACK (get_page_size_area_prepared_cb),
525                                   &got_size);
526
527                 while (outpipe >= 0) {
528                         bytes = read (outpipe, buf, 1024);
529                 
530                         if (bytes > 0)
531                         gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
532                         if (bytes <= 0 || got_size) {
533                                 close (outpipe);
534                                 outpipe = -1;
535                                 gdk_pixbuf_loader_close (loader, NULL);
536                         }
537                 }
538
539                 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
540                         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
541                         if (width)
542                                 *width = gdk_pixbuf_get_width (pixbuf);
543                         if (height)
544                                 *height = gdk_pixbuf_get_height (pixbuf);
545                 }
546
547                 g_spawn_close_pid (child_pid);
548                 g_object_unref (loader);
549         } else {
550                 filename = g_build_filename (comics_document->dir,
551                                              (char *) comics_document->page_names->pdata[page->index],
552                                              NULL);
553                 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
554                 g_free (filename);
555                 if (width)
556                         *width = gdk_pixbuf_get_width (pixbuf);
557                 if (height)
558                         *height = gdk_pixbuf_get_height (pixbuf);
559         }
560 }
561
562 static void
563 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
564                                 gpointer         data)
565 {
566         gboolean *got_size = data;
567         *got_size = TRUE;
568 }
569
570 static GdkPixbuf *
571 comics_document_render_pixbuf (EvDocument      *document,
572                                EvRenderContext *rc)
573 {
574         GdkPixbufLoader *loader;
575         GdkPixbuf *rotated_pixbuf;
576         char **argv;
577         guchar buf[4096];
578         gboolean success;
579         gint outpipe = -1;
580         GPid child_pid = -1;
581         gssize bytes;
582         gint width, height;
583         gchar *filename;
584         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
585         
586         if (!comics_document->decompress_tmp) {
587                 argv = extract_argv (document, rc->page->index);
588                 success = g_spawn_async_with_pipes (NULL, argv, NULL,
589                                                     G_SPAWN_SEARCH_PATH | 
590                                                     G_SPAWN_STDERR_TO_DEV_NULL,
591                                                     NULL, NULL,
592                                                     &child_pid,
593                                                     NULL, &outpipe, NULL, NULL);
594                 g_strfreev (argv);
595                 g_return_val_if_fail (success == TRUE, NULL);
596
597                 loader = gdk_pixbuf_loader_new ();
598                 g_signal_connect (loader, "size-prepared",
599                                   G_CALLBACK (render_pixbuf_size_prepared_cb), 
600                                   &rc->scale);
601
602                 while (outpipe >= 0) {
603                         bytes = read (outpipe, buf, 4096);
604
605                         if (bytes > 0) {
606                                 gdk_pixbuf_loader_write (loader, buf, bytes, 
607                                 NULL);
608                         } else if (bytes <= 0) {
609                                 close (outpipe);
610                                 gdk_pixbuf_loader_close (loader, NULL);
611                                 outpipe = -1;
612                         }
613                 }
614
615                 rotated_pixbuf = gdk_pixbuf_rotate_simple (
616                                         gdk_pixbuf_loader_get_pixbuf (loader),
617                                         360 - rc->rotation);
618                 g_spawn_close_pid (child_pid);
619                 g_object_unref (loader);
620         } else {
621                 filename = 
622                         g_build_filename (comics_document->dir,
623                                           (char *) comics_document->page_names->pdata[rc->page->index],
624                                           NULL);
625            
626                 gdk_pixbuf_get_file_info (filename, &width, &height);
627                 
628                 rotated_pixbuf = 
629                   gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
630                                             filename, width * (rc->scale) + 0.5,
631                                             height * (rc->scale) + 0.5, NULL),
632                                             360 - rc->rotation);
633                 g_free (filename);
634         
635         }
636         return rotated_pixbuf;
637 }
638
639 static cairo_surface_t *
640 comics_document_render (EvDocument      *document,
641                         EvRenderContext *rc)
642 {
643         GdkPixbuf       *pixbuf;
644         cairo_surface_t *surface;
645
646         pixbuf = comics_document_render_pixbuf (document, rc);
647         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
648         g_object_unref (pixbuf);
649         
650         return surface;
651 }
652
653 static void
654 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
655                                 gint             width,
656                                 gint             height,
657                                 gpointer         data)
658 {
659         double *scale = data;
660         int w = (width  * (*scale) + 0.5);
661         int h = (height * (*scale) + 0.5);
662
663         gdk_pixbuf_loader_set_size (loader, w, h);
664 }
665
666 /**
667  * comics_remove_dir: Removes a directory recursively. 
668  * Returns:
669  *      0 if it was successfully deleted,
670  *      -1 if an error occurred                 
671  */
672 static int 
673 comics_remove_dir (gchar *path_name) 
674 {
675         GDir  *content_dir;
676         const gchar *filename;
677         gchar *filename_with_path;
678         
679         if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
680                 content_dir = g_dir_open  (path_name, 0, NULL);
681                 filename  = g_dir_read_name (content_dir);
682                 while (filename) {
683                         filename_with_path = 
684                                 g_build_filename (path_name, 
685                                                   filename, NULL);
686                         comics_remove_dir (filename_with_path);
687                         g_free (filename_with_path);
688                         filename = g_dir_read_name (content_dir);
689                 }
690                 g_dir_close (content_dir);
691         }
692         /* Note from g_remove() documentation: on Windows, it is in general not 
693          * possible to remove a file that is open to some process, or mapped 
694          * into memory.*/
695         return (g_remove (path_name));
696 }
697
698 static void
699 comics_document_finalize (GObject *object)
700 {
701         ComicsDocument *comics_document = COMICS_DOCUMENT (object);
702         
703         if (comics_document->decompress_tmp) {
704                 if (comics_remove_dir (comics_document->dir) == -1)
705                         g_warning (_("There was an error deleting “%s”."),
706                                    comics_document->dir);
707                 g_free (comics_document->dir);
708         }
709         
710         if (comics_document->page_names) {
711                 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
712                 g_ptr_array_free (comics_document->page_names, TRUE);
713         }
714
715         g_free (comics_document->archive);
716         g_free (comics_document->selected_command);
717         g_free (comics_document->extract_command);
718         g_free (comics_document->list_command);
719
720         G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
721 }
722
723 static void
724 comics_document_class_init (ComicsDocumentClass *klass)
725 {
726         GObjectClass    *gobject_class = G_OBJECT_CLASS (klass);
727         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
728
729         gobject_class->finalize = comics_document_finalize;
730
731         ev_document_class->load = comics_document_load;
732         ev_document_class->save = comics_document_save;
733         ev_document_class->get_n_pages = comics_document_get_n_pages;
734         ev_document_class->get_page_size = comics_document_get_page_size;
735         ev_document_class->render = comics_document_render;
736 }
737
738 static void
739 comics_document_init (ComicsDocument *comics_document)
740 {
741         comics_document->archive = NULL;
742         comics_document->page_names = NULL;
743         comics_document->extract_command = NULL;
744 }
745
746 /* Returns a list of file extensions supported by gdk-pixbuf */
747 static GSList*
748 get_supported_image_extensions()
749 {
750         GSList *extensions = NULL;
751         GSList *formats = gdk_pixbuf_get_formats ();
752         GSList *l;
753
754         for (l = formats; l != NULL; l = l->next) {
755                 int i;
756                 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
757
758                 for (i = 0; ext[i] != NULL; i++) {
759                         extensions = g_slist_append (extensions,
760                                                      g_strdup (ext[i]));
761                 }
762
763                 g_strfreev (ext);
764         }
765
766         g_slist_free (formats);
767         return extensions;
768 }
769
770 static GdkPixbuf *
771 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
772                                           EvRenderContext      *rc,
773                                           gboolean              border)
774 {
775         GdkPixbuf *thumbnail;
776
777         thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
778
779         if (border) {
780               GdkPixbuf *tmp_pixbuf = thumbnail;
781               
782               thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
783               g_object_unref (tmp_pixbuf);
784         }
785
786         return thumbnail;
787 }
788
789 static void
790 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
791                                            EvRenderContext      *rc,
792                                            gint                 *width,
793                                            gint                 *height)
794 {
795         gdouble page_width, page_height;
796         
797         comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
798                                        &page_width, &page_height);
799
800         if (rc->rotation == 90 || rc->rotation == 270) {
801                 *width = (gint) (page_height * rc->scale);
802                 *height = (gint) (page_width * rc->scale);
803         } else {
804                 *width = (gint) (page_width * rc->scale);
805                 *height = (gint) (page_height * rc->scale);
806         }
807 }
808
809 static void
810 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
811 {
812         iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
813         iface->get_dimensions = comics_document_thumbnails_get_dimensions;
814 }
815
816 static char**
817 extract_argv (EvDocument *document, gint page)
818 {
819         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
820         char **argv;
821         char *command_line, *quoted_archive, *quoted_filename;
822         GError *err = NULL;
823
824         if (page >= comics_document->page_names->len)
825                 return NULL;
826
827         quoted_archive = g_shell_quote (comics_document->archive);
828         quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
829
830         command_line = g_strdup_printf ("%s %s %s",
831                                         comics_document->extract_command,
832                                         quoted_archive,
833                                         quoted_filename);
834         g_shell_parse_argv (command_line, NULL, &argv, &err);
835
836         if (err) {
837                 g_warning (_("Error %s"), err->message);
838                 g_error_free (err);
839                 return NULL;
840         }
841
842         g_free (command_line);
843         g_free (quoted_archive);
844         g_free (quoted_filename);
845         return argv;
846 }