]> www.fi.muni.cz Git - evince.git/blob - backend/comics/comics-document.c
[comics] Includes cleanup
[evince.git] / backend / comics / comics-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Copyright (C) 2005, Teemu Tervo <teemu.tervo@gmx.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/wait.h>
25 #include <stdlib.h>
26 #include <errno.h>
27
28 #include <glib.h>
29 #include <glib/gi18n-lib.h>
30 #include <glib/gstdio.h>
31 #include <gio/gio.h>
32
33 #include "comics-document.h"
34 #include "ev-document-misc.h"
35 #include "ev-document-thumbnails.h"
36 #include "ev-file-helpers.h"
37
38 struct _ComicsDocumentClass
39 {
40         EvDocumentClass parent_class;
41 };
42
43 typedef enum
44 {
45         RARLABS,
46         GNAUNRAR,
47         UNZIP,
48         P7ZIP
49 } ComicBookDecompressType;
50
51 struct _ComicsDocument
52 {
53         EvDocument parent_instance;
54
55         gchar    *archive, *dir;
56         GPtrArray *page_names;
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_mkdtemp ("comics.XXXXXX", error);
210                 if (comics_document->dir == NULL)
211                         return FALSE;
212
213                 /* unrar-free can't create directories, but ev_mkdtemp already created the dir */
214
215                 comics_document->decompress_tmp =
216                         g_strdup_printf (command_usage_def[type].decompress_tmp, 
217                                          comics_document->selected_command, 
218                                          quoted_file, 
219                                          comics_document->dir);
220                 g_free (quoted_file);
221
222                 if (!comics_decompress_temp_dir (comics_document->decompress_tmp,
223                     comics_document->selected_command, error))
224                         return FALSE;
225                 else
226                         return TRUE;
227         } else {
228                 g_free (quoted_file);
229                 return TRUE;
230         }
231
232 }
233
234 /* This function chooses an external command for decompressing a comic 
235  * book based on its mime tipe. */
236 static gboolean 
237 comics_check_decompress_command (gchar          *mime_type, 
238                                  ComicsDocument *comics_document,
239                                  GError         **error)
240 {
241         gboolean success;
242         gchar *std_out, *std_err;
243         gint retval;
244         GError *err = NULL;
245         
246         /* FIXME, use proper cbr/cbz mime types once they're
247          * included in shared-mime-info */
248         
249         if (!strcmp (mime_type, "application/x-cbr") ||
250             !strcmp (mime_type, "application/x-rar")) {
251                 /* The RARLAB provides a no-charge proprietary (freeware) 
252                 * decompress-only client for Linux called unrar. Another 
253                 * option is a GPLv2-licensed command-line tool developed by 
254                 * the Gna! project. Confusingly enough, the free software RAR 
255                 * decoder is also named unrar. For this reason we need to add 
256                 * some lines for disambiguation. Sorry for the added the 
257                 * complexity but it's life :)
258                 * Finally, some distributions, like Debian, rename this free 
259                 * option as unrar-free. 
260                 * */
261                 comics_document->selected_command = 
262                                         g_find_program_in_path ("unrar");
263                 if (comics_document->selected_command) {
264                         /* We only use std_err to avoid printing useless error 
265                          * messages on the terminal */
266                         success = 
267                                 g_spawn_command_line_sync (
268                                               comics_document->selected_command, 
269                                                            &std_out, &std_err,
270                                                            &retval, &err);
271                         if (!success) {
272                                 g_propagate_error (error, err);
273                                 g_error_free (err);
274                                 return FALSE;
275                         /* I don't check retval status because RARLAB unrar 
276                          * doesn't have a way to return 0 without involving an 
277                          * operation with a file*/
278                         } else if (WIFEXITED (retval)) {
279                                 if (g_strrstr (std_out,"freeware") != NULL)
280                                         /* The RARLAB freeware client */
281                                         comics_document->command_usage = RARLABS;
282                                 else
283                                         /* The Gna! free software client */
284                                         comics_document->command_usage = GNAUNRAR;
285
286                                 g_free (std_out);
287                                 g_free (std_err);
288                                 return TRUE;
289                         }
290                 }
291                 /* The Gna! free software client with Debian naming convention */
292                 comics_document->selected_command = 
293                                 g_find_program_in_path ("unrar-free");
294                 if (comics_document->selected_command) {
295                         comics_document->command_usage = GNAUNRAR;
296                         return TRUE;
297                 }
298
299         } else if (!strcmp (mime_type, "application/x-cbz") ||
300                    !strcmp (mime_type, "application/zip")) {
301                 /* InfoZIP's unzip program */
302                 comics_document->selected_command = 
303                                 g_find_program_in_path ("unzip");
304                 if (comics_document->selected_command) {
305                         comics_document->command_usage = UNZIP;
306                         return TRUE;
307                 }
308
309         } else if (!strcmp (mime_type, "application/x-cb7") ||
310                    !strcmp (mime_type, "application/x-7z-compressed")) {
311                 /* 7zr, 7za and 7z are the commands from the p7zip project able 
312                  * to decompress .7z files */ 
313                         comics_document->selected_command = 
314                                 g_find_program_in_path ("7zr");
315                         if (comics_document->selected_command) {
316                                 comics_document->command_usage = P7ZIP;
317                                 return TRUE;
318                         }
319                         comics_document->selected_command = 
320                                 g_find_program_in_path ("7za");
321                         if (comics_document->selected_command) {
322                                 comics_document->command_usage = P7ZIP;
323                                 return TRUE;
324                         }
325                         comics_document->selected_command = 
326                                 g_find_program_in_path ("7z");
327                         if (comics_document->selected_command) {
328                                 comics_document->command_usage = P7ZIP;
329                                 return TRUE;
330                         }
331         } else {
332                 g_set_error (error,
333                              EV_DOCUMENT_ERROR,
334                              EV_DOCUMENT_ERROR_INVALID,
335                              _("Not a comic book MIME type: %s"),
336                              mime_type);
337                              return FALSE;
338         }
339         g_set_error_literal (error,
340                              EV_DOCUMENT_ERROR,
341                              EV_DOCUMENT_ERROR_INVALID,
342                              _("Can't find an appropriate command to "
343                              "decompress this type of comic book"));
344         return FALSE;
345 }
346
347 static int
348 sort_page_names (gconstpointer a,
349                  gconstpointer b)
350 {
351   return strcmp (* (const char **) a, * (const char **) b);
352 }
353
354 static gboolean
355 comics_document_load (EvDocument *document,
356                       const char *uri,
357                       GError    **error)
358 {
359         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
360         GSList *supported_extensions;
361         gchar *std_out;
362         gchar *mime_type;
363         gchar **cb_files, *cb_file;
364         gboolean success;
365         int i, retval;
366         GError *err = NULL;
367
368         comics_document->archive = g_filename_from_uri (uri, NULL, error);
369         if (!comics_document->archive)
370                 return FALSE;
371
372         mime_type = ev_file_get_mime_type (uri, FALSE, &err);
373         if (!mime_type) {
374                 if (err) {
375                         g_propagate_error (error, err);
376                 } else {
377                         g_set_error_literal (error,
378                                              EV_DOCUMENT_ERROR,
379                                              EV_DOCUMENT_ERROR_INVALID,
380                                              _("Unknown MIME Type"));
381                 }
382
383                 return FALSE;
384         }
385         
386         if (!comics_check_decompress_command (mime_type, comics_document, 
387         error)) {       
388                 g_free (mime_type);
389                 return FALSE;
390         } else if (!comics_generate_command_lines (comics_document, error)) {
391                    g_free (mime_type);
392                 return FALSE;
393         }
394
395         g_free (mime_type);
396
397         /* Get list of files in archive */
398         success = g_spawn_command_line_sync (comics_document->list_command,
399                                              &std_out, NULL, &retval, error);
400
401         if (!success) {
402                 return FALSE;
403         } else if (!WIFEXITED(retval) || WEXITSTATUS(retval) != EXIT_SUCCESS) {
404                 g_set_error_literal (error,
405                                      EV_DOCUMENT_ERROR,
406                                      EV_DOCUMENT_ERROR_INVALID,
407                                      _("File corrupted"));
408                 return FALSE;
409         }
410
411         /* FIXME: is this safe against filenames containing \n in the archive ? */
412         cb_files = g_strsplit (std_out, "\n", 0);
413         g_free (std_out);
414
415         if (!cb_files) {
416                 g_set_error_literal (error,
417                                      EV_DOCUMENT_ERROR,
418                                      EV_DOCUMENT_ERROR_INVALID,
419                                      _("No files in archive"));
420                 return FALSE;
421         }
422
423         comics_document->page_names = g_ptr_array_sized_new (64);
424
425         supported_extensions = get_supported_image_extensions ();
426         for (i = 0; cb_files[i] != NULL; i++) {
427                 if (comics_document->offset != NO_OFFSET) {
428                         if (g_utf8_strlen (cb_files[i],-1) > 
429                             comics_document->offset) {
430                                 cb_file = 
431                                         g_utf8_offset_to_pointer (cb_files[i], 
432                                                        comics_document->offset);
433                         } else {
434                                 continue;
435                         }
436                 } else {
437                         cb_file = cb_files[i];
438                 }
439                 gchar *suffix = g_strrstr (cb_file, ".");
440                 if (!suffix)
441                         continue;
442                 suffix = g_ascii_strdown (suffix + 1, -1);
443                 if (g_slist_find_custom (supported_extensions, suffix,
444                                          (GCompareFunc) strcmp) != NULL) {
445                         g_ptr_array_add (comics_document->page_names,
446                                          g_strstrip (g_strdup (cb_file)));
447                 }
448                 g_free (suffix);
449         }
450         g_strfreev (cb_files);
451         g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
452         g_slist_free (supported_extensions);
453
454         if (comics_document->page_names->len == 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         /* Now sort the pages */
464         g_ptr_array_sort (comics_document->page_names, sort_page_names);
465
466         return TRUE;
467 }
468
469
470 static gboolean
471 comics_document_save (EvDocument *document,
472                       const char *uri,
473                       GError    **error)
474 {
475         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
476
477         return ev_xfer_uri_simple (comics_document->archive, uri, error);
478 }
479
480 static int
481 comics_document_get_n_pages (EvDocument *document)
482 {
483         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
484
485         if (comics_document->page_names == NULL)
486                 return 0;
487
488         return comics_document->page_names->len;
489 }
490
491 static void
492 comics_document_get_page_size (EvDocument *document,
493                                EvPage     *page,
494                                double     *width,
495                                double     *height)
496 {
497         GdkPixbufLoader *loader;
498         char **argv;
499         guchar buf[1024];
500         gboolean success, got_size = FALSE;
501         gint outpipe = -1;
502         GPid child_pid = -1;
503         gssize bytes;
504         GdkPixbuf *pixbuf;
505         gchar *filename;
506         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
507         
508         if (!comics_document->decompress_tmp) {
509                 argv = extract_argv (document, page->index);
510                 success = g_spawn_async_with_pipes (NULL, argv, NULL,
511                                                     G_SPAWN_SEARCH_PATH | 
512                                                     G_SPAWN_STDERR_TO_DEV_NULL,
513                                                     NULL, NULL,
514                                                     &child_pid,
515                                                     NULL, &outpipe, NULL, NULL);
516                 g_strfreev (argv);
517                 g_return_if_fail (success == TRUE);
518
519                 loader = gdk_pixbuf_loader_new ();
520                 g_signal_connect (loader, "area-prepared",
521                                   G_CALLBACK (get_page_size_area_prepared_cb),
522                                   &got_size);
523
524                 while (outpipe >= 0) {
525                         bytes = read (outpipe, buf, 1024);
526                 
527                         if (bytes > 0)
528                         gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
529                         if (bytes <= 0 || got_size) {
530                                 close (outpipe);
531                                 outpipe = -1;
532                                 gdk_pixbuf_loader_close (loader, NULL);
533                         }
534                 }
535
536                 if (gdk_pixbuf_loader_get_pixbuf (loader)) {
537                         pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
538                         if (width)
539                                 *width = gdk_pixbuf_get_width (pixbuf);
540                         if (height)
541                                 *height = gdk_pixbuf_get_height (pixbuf);
542                 }
543
544                 g_spawn_close_pid (child_pid);
545                 g_object_unref (loader);
546         } else {
547                 filename = g_build_filename (comics_document->dir,
548                                              (char *) comics_document->page_names->pdata[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 *) comics_document->page_names->pdata[rc->page->index],
621                                           NULL);
622            
623                 gdk_pixbuf_get_file_info (filename, &width, &height);
624                 
625                 rotated_pixbuf = 
626                   gdk_pixbuf_rotate_simple (gdk_pixbuf_new_from_file_at_size (
627                                             filename, width * (rc->scale) + 0.5,
628                                             height * (rc->scale) + 0.5, NULL),
629                                             360 - rc->rotation);
630                 g_free (filename);
631         
632         }
633         return rotated_pixbuf;
634 }
635
636 static cairo_surface_t *
637 comics_document_render (EvDocument      *document,
638                         EvRenderContext *rc)
639 {
640         GdkPixbuf       *pixbuf;
641         cairo_surface_t *surface;
642
643         pixbuf = comics_document_render_pixbuf (document, rc);
644         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
645         g_object_unref (pixbuf);
646         
647         return surface;
648 }
649
650 static void
651 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
652                                 gint             width,
653                                 gint             height,
654                                 gpointer         data)
655 {
656         double *scale = data;
657         int w = (width  * (*scale) + 0.5);
658         int h = (height * (*scale) + 0.5);
659
660         gdk_pixbuf_loader_set_size (loader, w, h);
661 }
662
663 /**
664  * comics_remove_dir: Removes a directory recursively. 
665  * Returns:
666  *      0 if it was successfully deleted,
667  *      -1 if an error occurred                 
668  */
669 static int 
670 comics_remove_dir (gchar *path_name) 
671 {
672         GDir  *content_dir;
673         const gchar *filename;
674         gchar *filename_with_path;
675         
676         if (g_file_test (path_name, G_FILE_TEST_IS_DIR)) {
677                 content_dir = g_dir_open  (path_name, 0, NULL);
678                 filename  = g_dir_read_name (content_dir);
679                 while (filename) {
680                         filename_with_path = 
681                                 g_build_filename (path_name, 
682                                                   filename, NULL);
683                         comics_remove_dir (filename_with_path);
684                         g_free (filename_with_path);
685                         filename = g_dir_read_name (content_dir);
686                 }
687                 g_dir_close (content_dir);
688         }
689         /* Note from g_remove() documentation: on Windows, it is in general not 
690          * possible to remove a file that is open to some process, or mapped 
691          * into memory.*/
692         return (g_remove (path_name));
693 }
694
695 static void
696 comics_document_finalize (GObject *object)
697 {
698         ComicsDocument *comics_document = COMICS_DOCUMENT (object);
699         
700         if (comics_document->decompress_tmp) {
701                 if (comics_remove_dir (comics_document->dir) == -1)
702                         g_warning (_("There was an error deleting “%s”."),
703                                    comics_document->dir);
704                 g_free (comics_document->dir);
705         }
706         
707         if (comics_document->page_names) {
708                 g_ptr_array_foreach (comics_document->page_names, (GFunc) g_free, NULL);
709                 g_ptr_array_free (comics_document->page_names, TRUE);
710         }
711
712         g_free (comics_document->archive);
713         g_free (comics_document->selected_command);
714         g_free (comics_document->extract_command);
715         g_free (comics_document->list_command);
716
717         G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
718 }
719
720 static void
721 comics_document_class_init (ComicsDocumentClass *klass)
722 {
723         GObjectClass    *gobject_class = G_OBJECT_CLASS (klass);
724         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
725
726         gobject_class->finalize = comics_document_finalize;
727
728         ev_document_class->load = comics_document_load;
729         ev_document_class->save = comics_document_save;
730         ev_document_class->get_n_pages = comics_document_get_n_pages;
731         ev_document_class->get_page_size = comics_document_get_page_size;
732         ev_document_class->render = comics_document_render;
733 }
734
735 static void
736 comics_document_init (ComicsDocument *comics_document)
737 {
738         comics_document->archive = NULL;
739         comics_document->page_names = NULL;
740         comics_document->extract_command = NULL;
741 }
742
743 /* Returns a list of file extensions supported by gdk-pixbuf */
744 static GSList*
745 get_supported_image_extensions()
746 {
747         GSList *extensions = NULL;
748         GSList *formats = gdk_pixbuf_get_formats ();
749         GSList *l;
750
751         for (l = formats; l != NULL; l = l->next) {
752                 int i;
753                 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
754
755                 for (i = 0; ext[i] != NULL; i++) {
756                         extensions = g_slist_append (extensions,
757                                                      g_strdup (ext[i]));
758                 }
759
760                 g_strfreev (ext);
761         }
762
763         g_slist_free (formats);
764         return extensions;
765 }
766
767 static GdkPixbuf *
768 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
769                                           EvRenderContext      *rc,
770                                           gboolean              border)
771 {
772         GdkPixbuf *thumbnail;
773
774         thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
775
776         if (border) {
777               GdkPixbuf *tmp_pixbuf = thumbnail;
778               
779               thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
780               g_object_unref (tmp_pixbuf);
781         }
782
783         return thumbnail;
784 }
785
786 static void
787 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
788                                            EvRenderContext      *rc,
789                                            gint                 *width,
790                                            gint                 *height)
791 {
792         gdouble page_width, page_height;
793         
794         comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
795                                        &page_width, &page_height);
796
797         if (rc->rotation == 90 || rc->rotation == 270) {
798                 *width = (gint) (page_height * rc->scale);
799                 *height = (gint) (page_width * rc->scale);
800         } else {
801                 *width = (gint) (page_width * rc->scale);
802                 *height = (gint) (page_height * rc->scale);
803         }
804 }
805
806 static void
807 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
808 {
809         iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
810         iface->get_dimensions = comics_document_thumbnails_get_dimensions;
811 }
812
813 static char**
814 extract_argv (EvDocument *document, gint page)
815 {
816         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
817         char **argv;
818         char *command_line, *quoted_archive, *quoted_filename;
819         GError *err = NULL;
820
821         if (page >= comics_document->page_names->len)
822                 return NULL;
823
824         quoted_archive = g_shell_quote (comics_document->archive);
825         if (comics_document->regex_arg) {
826                 quoted_filename = comics_regex_quote (comics_document->page_names->pdata[page]);
827         } else {
828                 quoted_filename = g_shell_quote (comics_document->page_names->pdata[page]);
829         }
830
831         command_line = g_strdup_printf ("%s -- %s %s",
832                                         comics_document->extract_command,
833                                         quoted_archive,
834                                         quoted_filename);
835
836         g_shell_parse_argv (command_line, NULL, &argv, &err);
837
838         if (err) {
839                 g_warning (_("Error %s"), err->message);
840                 g_error_free (err);
841                 return NULL;
842         }
843
844         g_free (command_line);
845         g_free (quoted_archive);
846         g_free (quoted_filename);
847         return argv;
848 }