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