]> www.fi.muni.cz Git - evince.git/blob - backend/comics/comics-document.c
9f657c24e1be667943bef0ce3e49532722267ef1
[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         gchar *quoted_command;
190         ComicBookDecompressType type;
191         
192         type = comics_document->command_usage;
193         quoted_file = g_shell_quote (comics_document->archive);
194         quoted_command = g_shell_quote (comics_document->selected_command);
195
196         comics_document->extract_command =
197                             g_strdup_printf (command_usage_def[type].extract,
198                                              quoted_command);
199         comics_document->list_command =
200                             g_strdup_printf (command_usage_def[type].list,
201                                              quoted_command, quoted_file);
202         comics_document->offset = command_usage_def[type].offset;
203         if (command_usage_def[type].decompress_tmp) {
204                 comics_document->dir = ev_mkdtemp ("evince-comics-XXXXXX", error);
205                 if (comics_document->dir == NULL)
206                         return FALSE;
207
208                 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
209
210                 comics_document->decompress_tmp =
211                         g_strdup_printf (command_usage_def[type].decompress_tmp, 
212                                          quoted_command, quoted_file,
213                                          comics_document->dir);
214                 g_free (quoted_file);
215                 g_free (quoted_command);
216
217                 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
218                     comics_document->selected_command, error))
219                         return FALSE;
220                 else
221                         return TRUE;
222         } else {
223                 g_free (quoted_file);
224                 g_free (quoted_command);
225                 return TRUE;
226         }
227
228 }
229
230 /* This function chooses an external command for decompressing a comic 
231  * book based on its mime tipe. */
232 static gboolean 
233 comics_check_decompress_command (gchar          *mime_type, 
234                                  ComicsDocument *comics_document,
235                                  GError         **error)
236 {
237         gboolean success;
238         gchar *std_out, *std_err;
239         gint retval;
240         GError *err = NULL;
241         
242         /* FIXME, use proper cbr/cbz mime types once they're
243          * included in shared-mime-info */
244         
245         if (!strcmp (mime_type, "application/x-cbr") ||
246             !strcmp (mime_type, "application/x-rar")) {
247                 /* The RARLAB provides a no-charge proprietary (freeware) 
248                 * decompress-only client for Linux called unrar. Another 
249                 * option is a GPLv2-licensed command-line tool developed by 
250                 * the Gna! project. Confusingly enough, the free software RAR 
251                 * decoder is also named unrar. For this reason we need to add 
252                 * some lines for disambiguation. Sorry for the added the 
253                 * complexity but it's life :)
254                 * Finally, some distributions, like Debian, rename this free 
255                 * option as unrar-free. 
256                 * */
257                 comics_document->selected_command = 
258                                         g_find_program_in_path ("unrar");
259                 if (comics_document->selected_command) {
260                         /* We only use std_err to avoid printing useless error 
261                          * messages on the terminal */
262                         success = 
263                                 g_spawn_command_line_sync (
264                                               comics_document->selected_command, 
265                                                            &std_out, &std_err,
266                                                            &retval, &err);
267                         if (!success) {
268                                 g_propagate_error (error, err);
269                                 g_error_free (err);
270                                 return FALSE;
271                         /* I don't check retval status because RARLAB unrar 
272                          * doesn't have a way to return 0 without involving an 
273                          * operation with a file*/
274                         } else if (WIFEXITED (retval)) {
275                                 if (g_strrstr (std_out,"freeware") != NULL)
276                                         /* The RARLAB freeware client */
277                                         comics_document->command_usage = RARLABS;
278                                 else
279                                         /* The Gna! free software client */
280                                         comics_document->command_usage = GNAUNRAR;
281
282                                 g_free (std_out);
283                                 g_free (std_err);
284                                 return TRUE;
285                         }
286                 }
287                 /* The Gna! free software client with Debian naming convention */
288                 comics_document->selected_command = 
289                                 g_find_program_in_path ("unrar-free");
290                 if (comics_document->selected_command) {
291                         comics_document->command_usage = GNAUNRAR;
292                         return TRUE;
293                 }
294
295         } else if (!strcmp (mime_type, "application/x-cbz") ||
296                    !strcmp (mime_type, "application/zip")) {
297                 /* InfoZIP's unzip program */
298                 comics_document->selected_command = 
299                                 g_find_program_in_path ("unzip");
300                 if (comics_document->selected_command) {
301                         comics_document->command_usage = UNZIP;
302                         return TRUE;
303                 }
304
305         } else if (!strcmp (mime_type, "application/x-cb7") ||
306                    !strcmp (mime_type, "application/x-7z-compressed")) {
307                 /* 7zr, 7za and 7z are the commands from the p7zip project able 
308                  * to decompress .7z files */ 
309                         comics_document->selected_command = 
310                                 g_find_program_in_path ("7zr");
311                         if (comics_document->selected_command) {
312                                 comics_document->command_usage = P7ZIP;
313                                 return TRUE;
314                         }
315                         comics_document->selected_command = 
316                                 g_find_program_in_path ("7za");
317                         if (comics_document->selected_command) {
318                                 comics_document->command_usage = P7ZIP;
319                                 return TRUE;
320                         }
321                         comics_document->selected_command = 
322                                 g_find_program_in_path ("7z");
323                         if (comics_document->selected_command) {
324                                 comics_document->command_usage = P7ZIP;
325                                 return TRUE;
326                         }
327         } else if (!strcmp (mime_type, "application/x-cbt") ||
328                    !strcmp (mime_type, "application/x-tar")) {
329                 /* tar utility (Tape ARchive) */
330                 comics_document->selected_command =
331                                 g_find_program_in_path ("tar");
332                 if (comics_document->selected_command) {
333                         comics_document->command_usage = TAR;
334                         return TRUE;
335                 }
336         } else {
337                 g_set_error (error,
338                              EV_DOCUMENT_ERROR,
339                              EV_DOCUMENT_ERROR_INVALID,
340                              _("Not a comic book MIME type: %s"),
341                              mime_type);
342                              return FALSE;
343         }
344         g_set_error_literal (error,
345                              EV_DOCUMENT_ERROR,
346                              EV_DOCUMENT_ERROR_INVALID,
347                              _("Can't find an appropriate command to "
348                              "decompress this type of comic book"));
349         return FALSE;
350 }
351
352 static int
353 sort_page_names (gconstpointer a,
354                  gconstpointer b)
355 {
356   return strcmp (* (const char **) a, * (const char **) b);
357 }
358
359 static gboolean
360 comics_document_load (EvDocument *document,
361                       const char *uri,
362                       GError    **error)
363 {
364         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
365         GSList *supported_extensions;
366         gchar *std_out;
367         gchar *mime_type;
368         gchar **cb_files, *cb_file;
369         gboolean success;
370         int i, retval;
371         GError *err = NULL;
372
373         comics_document->archive = g_filename_from_uri (uri, NULL, error);
374         if (!comics_document->archive)
375                 return FALSE;
376
377         mime_type = ev_file_get_mime_type (uri, FALSE, &err);
378         if (!mime_type) {
379                 if (err) {
380                         g_propagate_error (error, err);
381                 } else {
382                         g_set_error_literal (error,
383                                              EV_DOCUMENT_ERROR,
384                                              EV_DOCUMENT_ERROR_INVALID,
385                                              _("Unknown MIME Type"));
386                 }
387
388                 return FALSE;
389         }
390         
391         if (!comics_check_decompress_command (mime_type, comics_document, 
392         error)) {       
393                 g_free (mime_type);
394                 return FALSE;
395         } else if (!comics_generate_command_lines (comics_document, error)) {
396                    g_free (mime_type);
397                 return FALSE;
398         }
399
400         g_free (mime_type);
401
402         /* Get list of files in archive */
403         success = g_spawn_command_line_sync (comics_document->list_command,
404                                              &std_out, NULL, &retval, error);
405
406         if (!success) {
407                 return FALSE;
408         } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
409                 g_set_error_literal (error,
410                                      EV_DOCUMENT_ERROR,
411                                      EV_DOCUMENT_ERROR_INVALID,
412                                      _("File corrupted"));
413                 return FALSE;
414         }
415
416         /* FIXME: is this safe against filenames containing \n in the archive ? */
417         cb_files = g_strsplit (std_out, "\n", 0);
418         g_free (std_out);
419
420         if (!cb_files) {
421                 g_set_error_literal (error,
422                                      EV_DOCUMENT_ERROR,
423                                      EV_DOCUMENT_ERROR_INVALID,
424                                      _("No files in archive"));
425                 return FALSE;
426         }
427
428         comics_document->page_names = g_ptr_array_sized_new (64);
429
430         supported_extensions = get_supported_image_extensions ();
431         for (i = 0; cb_files[i] != NULL; i++) {
432                 if (comics_document->offset != NO_OFFSET) {
433                         if (g_utf8_strlen (cb_files[i],-1) > 
434                             comics_document->offset) {
435                                 cb_file = 
436                                         g_utf8_offset_to_pointer (cb_files[i], 
437                                                        comics_document->offset);
438                         } else {
439                                 continue;
440                         }
441                 } else {
442                         cb_file = cb_files[i];
443                 }
444                 gchar *suffix = g_strrstr (cb_file, ".");
445                 if (!suffix)
446                         continue;
447                 suffix = g_ascii_strdown (suffix + 1, -1);
448                 if (g_slist_find_custom (supported_extensions, suffix,
449                                          (GCompareFunc) strcmp) != NULL) {
450                         g_ptr_array_add (comics_document->page_names,
451                                          g_strstrip (g_strdup (cb_file)));
452                 }
453                 g_free (suffix);
454         }
455         g_strfreev (cb_files);
456         g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
457         g_slist_free (supported_extensions);
458
459         if (comics_document->page_names->len == 0) {
460                 g_set_error (error,
461                              EV_DOCUMENT_ERROR,
462                              EV_DOCUMENT_ERROR_INVALID,
463                              _("No images found in archive %s"),
464                              uri);
465                 return FALSE;
466         }
467
468         /* Now sort the pages */
469         g_ptr_array_sort (comics_document->page_names, sort_page_names);
470
471         return TRUE;
472 }
473
474
475 static gboolean
476 comics_document_save (EvDocument *document,
477                       const char *uri,
478                       GError    **error)
479 {
480         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
481
482         return ev_xfer_uri_simple (comics_document->archive, uri, error);
483 }
484
485 static int
486 comics_document_get_n_pages (EvDocument *document)
487 {
488         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
489
490         if (comics_document->page_names == NULL)
491                 return 0;
492
493         return comics_document->page_names->len;
494 }
495
496 static void
497 comics_document_get_page_size (EvDocument *document,
498                                EvPage     *page,
499                                double     *width,
500                                double     *height)
501 {
502         GdkPixbufLoader *loader;
503         char **argv;
504         guchar buf[1024];
505         gboolean success, got_size = FALSE;
506         gint outpipe = -1;
507         GPid child_pid;
508         gssize bytes;
509         GdkPixbuf *pixbuf;
510         gchar *filename;
511         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
512         
513         if (!comics_document->decompress_tmp) {
514                 argv = extract_argv (document, page->index);
515                 success = g_spawn_async_with_pipes (NULL, argv, NULL,
516                                                     G_SPAWN_SEARCH_PATH | 
517                                                     G_SPAWN_STDERR_TO_DEV_NULL,
518                                                     NULL, NULL,
519                                                     &child_pid,
520                                                     NULL, &outpipe, NULL, NULL);
521                 g_strfreev (argv);
522                 g_return_if_fail (success == TRUE);
523
524                 loader = gdk_pixbuf_loader_new ();
525                 g_signal_connect (loader, "area-prepared",
526                                   G_CALLBACK (get_page_size_area_prepared_cb),
527                                   &got_size);
528
529                 while (outpipe >= 0) {
530                         bytes = read (outpipe, buf, 1024);
531                 
532                         if (bytes > 0)
533                         gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
534                         if (bytes <= 0 || got_size) {
535                                 close (outpipe);
536                                 outpipe = -1;
537                                 gdk_pixbuf_loader_close (loader, NULL);
538                         }
539                 }
540
541                 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
542                         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
543                         if (width)
544                                 *width = gdk_pixbuf_get_width (pixbuf);
545                         if (height)
546                                 *height = gdk_pixbuf_get_height (pixbuf);
547                 }
548
549                 g_spawn_close_pid (child_pid);
550                 g_object_unref (loader);
551         } else {
552                 filename = g_build_filename (comics_document->dir,
553                                              (char *) comics_document->page_names->pdata[page->index],
554                                              NULL);
555                 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
556                 g_free (filename);
557                 if (width)
558                         *width = gdk_pixbuf_get_width (pixbuf);
559                 if (height)
560                         *height = gdk_pixbuf_get_height (pixbuf);
561         }
562 }
563
564 static void
565 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
566                                 gpointer         data)
567 {
568         gboolean *got_size = data;
569         *got_size = TRUE;
570 }
571
572 static GdkPixbuf *
573 comics_document_render_pixbuf (EvDocument      *document,
574                                EvRenderContext *rc)
575 {
576         GdkPixbufLoader *loader;
577         GdkPixbuf *rotated_pixbuf;
578         char **argv;
579         guchar buf[4096];
580         gboolean success;
581         gint outpipe = -1;
582         GPid child_pid;
583         gssize bytes;
584         gint width, height;
585         gchar *filename;
586         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
587         
588         if (!comics_document->decompress_tmp) {
589                 argv = extract_argv (document, rc->page->index);
590                 success = g_spawn_async_with_pipes (NULL, argv, NULL,
591                                                     G_SPAWN_SEARCH_PATH | 
592                                                     G_SPAWN_STDERR_TO_DEV_NULL,
593                                                     NULL, NULL,
594                                                     &child_pid,
595                                                     NULL, &outpipe, NULL, NULL);
596                 g_strfreev (argv);
597                 g_return_val_if_fail (success == TRUE, NULL);
598
599                 loader = gdk_pixbuf_loader_new ();
600                 g_signal_connect (loader, "size-prepared",
601                                   G_CALLBACK (render_pixbuf_size_prepared_cb), 
602                                   &rc->scale);
603
604                 while (outpipe >= 0) {
605                         bytes = read (outpipe, buf, 4096);
606
607                         if (bytes > 0) {
608                                 gdk_pixbuf_loader_write (loader, buf, bytes, 
609                                 NULL);
610                         } else if (bytes <= 0) {
611                                 close (outpipe);
612                                 gdk_pixbuf_loader_close (loader, NULL);
613                                 outpipe = -1;
614                         }
615                 }
616
617                 rotated_pixbuf = gdk_pixbuf_rotate_simple (
618                                         gdk_pixbuf_loader_get_pixbuf (loader),
619                                         360 - rc->rotation);
620                 g_spawn_close_pid (child_pid);
621                 g_object_unref (loader);
622         } else {
623                 filename = 
624                         g_build_filename (comics_document->dir,
625                                           (char *) comics_document->page_names->pdata[rc->page->index],
626                                           NULL);
627            
628                 gdk_pixbuf_get_file_info (filename, &width, &height);
629                 
630                 rotated_pixbuf = 
631                   gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
632                                             filename, width * (rc->scale) + 0.5,
633                                             height * (rc->scale) + 0.5, NULL),
634                                             360 - rc->rotation);
635                 g_free (filename);
636         
637         }
638         return rotated_pixbuf;
639 }
640
641 static cairo_surface_t *
642 comics_document_render (EvDocument      *document,
643                         EvRenderContext *rc)
644 {
645         GdkPixbuf       *pixbuf;
646         cairo_surface_t *surface;
647
648         pixbuf = comics_document_render_pixbuf (document, rc);
649         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
650         g_object_unref (pixbuf);
651         
652         return surface;
653 }
654
655 static void
656 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
657                                 gint             width,
658                                 gint             height,
659                                 gpointer         data)
660 {
661         double *scale = data;
662         int w = (width  * (*scale) + 0.5);
663         int h = (height * (*scale) + 0.5);
664
665         gdk_pixbuf_loader_set_size (loader, w, h);
666 }
667
668 /**
669  * comics_remove_dir: Removes a directory recursively. 
670  * Returns:
671  *      0 if it was successfully deleted,
672  *      -1 if an error occurred                 
673  */
674 static int 
675 comics_remove_dir (gchar *path_name) 
676 {
677         GDir  *content_dir;
678         const gchar *filename;
679         gchar *filename_with_path;
680         
681         if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
682                 content_dir = g_dir_open  (path_name, 0, NULL);
683                 filename  = g_dir_read_name (content_dir);
684                 while (filename) {
685                         filename_with_path = 
686                                 g_build_filename (path_name, 
687                                                   filename, NULL);
688                         comics_remove_dir (filename_with_path);
689                         g_free (filename_with_path);
690                         filename = g_dir_read_name (content_dir);
691                 }
692                 g_dir_close (content_dir);
693         }
694         /* Note from g_remove() documentation: on Windows, it is in general not 
695          * possible to remove a file that is open to some process, or mapped 
696          * into memory.*/
697         return (g_remove (path_name));
698 }
699
700 static void
701 comics_document_finalize (GObject *object)
702 {
703         ComicsDocument *comics_document = COMICS_DOCUMENT (object);
704         
705         if (comics_document->decompress_tmp) {
706                 if (comics_remove_dir (comics_document->dir) == -1)
707                         g_warning (_("There was an error deleting “%s”."),
708                                    comics_document->dir);
709                 g_free (comics_document->dir);
710         }
711         
712         if (comics_document->page_names) {
713                 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
714                 g_ptr_array_free (comics_document->page_names, TRUE);
715         }
716
717         g_free (comics_document->archive);
718         g_free (comics_document->selected_command);
719         g_free (comics_document->extract_command);
720         g_free (comics_document->list_command);
721
722         G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
723 }
724
725 static void
726 comics_document_class_init (ComicsDocumentClass *klass)
727 {
728         GObjectClass    *gobject_class = G_OBJECT_CLASS (klass);
729         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
730
731         gobject_class->finalize = comics_document_finalize;
732
733         ev_document_class->load = comics_document_load;
734         ev_document_class->save = comics_document_save;
735         ev_document_class->get_n_pages = comics_document_get_n_pages;
736         ev_document_class->get_page_size = comics_document_get_page_size;
737         ev_document_class->render = comics_document_render;
738 }
739
740 static void
741 comics_document_init (ComicsDocument *comics_document)
742 {
743         comics_document->archive = NULL;
744         comics_document->page_names = NULL;
745         comics_document->extract_command = NULL;
746 }
747
748 /* Returns a list of file extensions supported by gdk-pixbuf */
749 static GSList*
750 get_supported_image_extensions()
751 {
752         GSList *extensions = NULL;
753         GSList *formats = gdk_pixbuf_get_formats ();
754         GSList *l;
755
756         for (l = formats; l != NULL; l = l->next) {
757                 int i;
758                 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
759
760                 for (i = 0; ext[i] != NULL; i++) {
761                         extensions = g_slist_append (extensions,
762                                                      g_strdup (ext[i]));
763                 }
764
765                 g_strfreev (ext);
766         }
767
768         g_slist_free (formats);
769         return extensions;
770 }
771
772 static GdkPixbuf *
773 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
774                                           EvRenderContext      *rc,
775                                           gboolean              border)
776 {
777         GdkPixbuf *thumbnail;
778
779         thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
780
781         if (border) {
782               GdkPixbuf *tmp_pixbuf = thumbnail;
783               
784               thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
785               g_object_unref (tmp_pixbuf);
786         }
787
788         return thumbnail;
789 }
790
791 static void
792 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
793                                            EvRenderContext      *rc,
794                                            gint                 *width,
795                                            gint                 *height)
796 {
797         gdouble page_width, page_height;
798         
799         comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
800                                        &page_width, &page_height);
801
802         if (rc->rotation == 90 || rc->rotation == 270) {
803                 *width = (gint) (page_height * rc->scale);
804                 *height = (gint) (page_width * rc->scale);
805         } else {
806                 *width = (gint) (page_width * rc->scale);
807                 *height = (gint) (page_height * rc->scale);
808         }
809 }
810
811 static void
812 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
813 {
814         iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
815         iface->get_dimensions = comics_document_thumbnails_get_dimensions;
816 }
817
818 static char**
819 extract_argv (EvDocument *document, gint page)
820 {
821         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
822         char **argv;
823         char *command_line, *quoted_archive, *quoted_filename;
824         GError *err = NULL;
825
826         if (page >= comics_document->page_names->len)
827                 return NULL;
828
829         quoted_archive = g_shell_quote (comics_document->archive);
830         quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
831
832         command_line = g_strdup_printf ("%s %s %s",
833                                         comics_document->extract_command,
834                                         quoted_archive,
835                                         quoted_filename);
836         g_shell_parse_argv (command_line, NULL, &argv, &err);
837
838         if (err) {
839                 g_warning (_("Error %s"), err->message);
840                 g_error_free (err);
841                 return NULL;
842         }
843
844         g_free (command_line);
845         g_free (quoted_archive);
846         g_free (quoted_filename);
847         return argv;
848 }