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