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