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