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