]> www.fi.muni.cz Git - evince.git/blob - backend/comics/comics-document.c
dc5a8b0dde79300b24ed5888bb8c0800ab40e00b
[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 <gio/gio.h>
27
28 #include "comics-document.h"
29 #include "ev-document-misc.h"
30 #include "ev-document-thumbnails.h"
31 #include "ev-file-helpers.h"
32
33 struct _ComicsDocumentClass
34 {
35         GObjectClass parent_class;
36 };
37
38 struct _ComicsDocument
39 {
40         GObject parent_instance;
41
42         gchar  *archive;
43         GSList *page_names;
44         int     n_pages;
45         char   *extract_command;
46         gboolean regex_arg;
47 };
48
49 typedef struct _ComicsDocumentClass ComicsDocumentClass;
50
51 static void       comics_document_document_iface_init (EvDocumentIface *iface);
52 static void       comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
53
54 static GSList*    get_supported_image_extensions (void);
55 static void       get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
56                                                   gpointer data);
57 static void       render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
58                                                   gint width,
59                                                   gint height,
60                                                   gpointer data);
61 static char**     extract_argv                   (EvDocument *document,
62                                                   gint page);
63
64
65 EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
66         {
67                 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
68                                                 comics_document_document_thumbnails_iface_init);
69         } );
70
71 static char *
72 comics_regex_quote (const char *s)
73 {
74     char *ret, *d;
75
76     d = ret = g_malloc (strlen (s) * 4 + 3);
77     
78     *d++ = '\'';
79
80     for (; *s; s++, d++) {
81         switch (*s) {
82         case '?':
83         case '|':
84         case '[':
85         case ']':
86         case '*':
87         case '\\':
88             *d++ = '\\';
89             break;
90         case '\'':
91             *d++ = '\'';
92             *d++ = '\\';
93             *d++ = '\'';
94             break;
95         }
96         *d = *s;
97     }
98     
99     *d++ = '\'';
100     *d = '\0';
101
102     return ret;
103 }
104
105 static gboolean
106 comics_document_load (EvDocument *document,
107                       const char *uri,
108                       GError    **error)
109 {
110         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
111         GSList *supported_extensions;
112         gchar *list_files_command = NULL, *std_out, *quoted_file;
113         gchar *mime_type;
114         gchar **cbr_files;
115         gboolean success;
116         int i, retval;
117         GError *err = NULL;
118
119         comics_document->archive = g_filename_from_uri (uri, NULL, error);
120         if (!comics_document->archive)
121                 return FALSE;
122
123         mime_type = ev_file_get_mime_type (uri, FALSE, &err);
124         if (!mime_type) {
125                 if (err) {
126                         g_propagate_error (error, err);
127                 } else {
128                         g_set_error_literal (error,
129                                               EV_DOCUMENT_ERROR,
130                                               EV_DOCUMENT_ERROR_INVALID,
131                                               _("Unknown MIME Type"));
132                 }
133
134                 return FALSE;
135         }
136
137         quoted_file = g_shell_quote (comics_document->archive);
138
139         /* FIXME, use proper cbr/cbz mime types once they're
140          * included in shared-mime-info */
141         if (!strcmp (mime_type, "application/x-cbr") ||
142             !strcmp (mime_type, "application/x-rar")) {
143                 comics_document->extract_command =
144                         g_strdup ("unrar p -c- -ierr");
145                 list_files_command =
146                         g_strdup_printf ("unrar vb -c- -- %s", quoted_file);
147                 comics_document->regex_arg = FALSE;
148         } else if (!strcmp (mime_type, "application/x-cbz") ||
149                    !strcmp (mime_type, "application/zip")) {
150                 comics_document->extract_command =
151                         g_strdup ("unzip -p -C");
152                 list_files_command = 
153                         g_strdup_printf ("zipinfo -1 -- %s", quoted_file);
154                 comics_document->regex_arg = TRUE;
155         } else if (!strcmp (mime_type, "application/x-cb7")) {
156                 comics_document->extract_command =
157                         g_strdup ("7zr x -so");
158                 list_files_command = 
159                         g_strdup_printf ("7zr l -- %s", quoted_file);
160                 comics_document->regex_arg = TRUE;
161         } else {
162                 g_set_error (error,
163                              EV_DOCUMENT_ERROR,
164                              EV_DOCUMENT_ERROR_INVALID,
165                              _("Not a comic book MIME type: %s"),
166                              mime_type);
167                 g_free (mime_type);
168                 g_free (quoted_file);
169                 return FALSE;
170         }
171
172         g_free (mime_type);
173         g_free (quoted_file);
174
175         /* Get list of files in archive */
176         success = g_spawn_command_line_sync (list_files_command,
177                                              &std_out, NULL, &retval, error);
178         g_free (list_files_command);
179
180         if (!success) {
181                 return FALSE;
182         } else if (retval != 0) {
183                 g_set_error_literal (error,
184                                      EV_DOCUMENT_ERROR,
185                                      EV_DOCUMENT_ERROR_INVALID,
186                                      _("File corrupted."));
187                 return FALSE;
188         }
189
190         /* FIXME: is this safe against filenames containing \n in the archive ? */
191         cbr_files = g_strsplit (std_out, "\n", 0);
192         g_free (std_out);
193
194         if (!cbr_files) {
195                 g_set_error_literal (error,
196                                      EV_DOCUMENT_ERROR,
197                                      EV_DOCUMENT_ERROR_INVALID,
198                                      _("No files in archive."));
199                 return FALSE;
200         }
201
202         supported_extensions = get_supported_image_extensions ();
203         for (i = 0; cbr_files[i] != NULL; i++) {
204                 gchar *suffix = g_strrstr (cbr_files[i], ".");
205                 if (!suffix)
206                         continue;
207                 suffix = g_ascii_strdown (suffix + 1, -1);
208
209                 if (g_slist_find_custom (supported_extensions, suffix,
210                                          (GCompareFunc) strcmp) != NULL) {
211                         comics_document->page_names =
212                                 g_slist_insert_sorted (
213                                         comics_document->page_names,
214                                         g_strdup (g_strstrip (cbr_files[i])),
215                                         (GCompareFunc) strcmp);
216                         comics_document->n_pages++;
217                 }
218
219                 g_free (suffix);
220         }
221
222         g_strfreev (cbr_files);
223         g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
224         g_slist_free (supported_extensions);
225
226         if (comics_document->n_pages == 0) {
227                 g_set_error (error,
228                              EV_DOCUMENT_ERROR,
229                              EV_DOCUMENT_ERROR_INVALID,
230                              _("No images found in archive %s"),
231                              uri);
232                 return FALSE;
233         }
234
235         return TRUE;
236 }
237
238
239 static gboolean
240 comics_document_save (EvDocument *document,
241                       const char *uri,
242                       GError    **error)
243 {
244         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
245
246         return ev_xfer_uri_simple (comics_document->archive, uri, error);
247 }
248
249 static int
250 comics_document_get_n_pages (EvDocument *document)
251 {
252         return COMICS_DOCUMENT (document)->n_pages;
253 }
254
255 static void
256 comics_document_get_page_size (EvDocument *document,
257                                EvPage     *page,
258                                double     *width,
259                                double     *height)
260 {
261         GdkPixbufLoader *loader;
262         char **argv;
263         guchar buf[1024];
264         gboolean success, got_size = FALSE;
265         gint outpipe = -1;
266         GPid child_pid = -1;
267
268         argv = extract_argv (document, page->index);
269         success = g_spawn_async_with_pipes (NULL, argv, NULL,
270                                             G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
271                                             NULL, NULL,
272                                             &child_pid,
273                                             NULL, &outpipe, NULL, NULL);
274         g_strfreev (argv);
275         g_return_if_fail (success == TRUE);
276
277         loader = gdk_pixbuf_loader_new ();
278         g_signal_connect (loader, "area-prepared",
279                           G_CALLBACK (get_page_size_area_prepared_cb),
280                           &got_size);
281
282         while (outpipe >= 0) {
283                 gssize bytes = read (outpipe, buf, 1024);
284                 
285                 if (bytes > 0)
286                         gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
287                 if (bytes <= 0 || got_size) {
288                         close (outpipe);
289                         outpipe = -1;
290                         gdk_pixbuf_loader_close (loader, NULL);
291                 }
292         }
293
294         if (gdk_pixbuf_loader_get_pixbuf (loader)) {
295                 GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
296                 if (width)
297                         *width = gdk_pixbuf_get_width (pixbuf);
298                 if (height)
299                         *height = gdk_pixbuf_get_height (pixbuf);
300         }
301
302         g_spawn_close_pid (child_pid);
303         g_object_unref (loader);
304 }
305
306 static void
307 get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
308                                 gpointer         data)
309 {
310         gboolean *got_size = data;
311         *got_size = TRUE;
312 }
313
314 static GdkPixbuf *
315 comics_document_render_pixbuf (EvDocument      *document,
316                                EvRenderContext *rc)
317 {
318         GdkPixbufLoader *loader;
319         GdkPixbuf *rotated_pixbuf;
320         char **argv;
321         guchar buf[4096];
322         gboolean success;
323         gint outpipe = -1;
324         GPid child_pid = -1;
325
326         argv = extract_argv (document, rc->page->index);
327         success = g_spawn_async_with_pipes (NULL, argv, NULL,
328                                             G_SPAWN_SEARCH_PATH
329                                             | G_SPAWN_STDERR_TO_DEV_NULL,
330                                             NULL, NULL,
331                                             &child_pid,
332                                             NULL, &outpipe, NULL, NULL);
333         g_strfreev (argv);
334         g_return_val_if_fail (success == TRUE, NULL);
335
336         loader = gdk_pixbuf_loader_new ();
337         g_signal_connect (loader, "size-prepared",
338                           G_CALLBACK (render_pixbuf_size_prepared_cb), &rc->scale);
339
340         while (outpipe >= 0) {
341                 gssize bytes = read (outpipe, buf, 4096);
342
343                 if (bytes > 0) {
344                         gdk_pixbuf_loader_write (loader, buf, bytes, NULL);
345                 } else if (bytes <= 0) {
346                         close (outpipe);
347                         gdk_pixbuf_loader_close (loader, NULL);
348                         outpipe = -1;
349                 }
350         }
351
352         rotated_pixbuf = gdk_pixbuf_rotate_simple (gdk_pixbuf_loader_get_pixbuf (loader),
353                                                    360 - rc->rotation);
354         g_spawn_close_pid (child_pid);
355         g_object_unref (loader);
356
357         return rotated_pixbuf;
358 }
359
360 static cairo_surface_t *
361 comics_document_render (EvDocument      *document,
362                         EvRenderContext *rc)
363 {
364         GdkPixbuf       *pixbuf;
365         cairo_surface_t *surface;
366
367         pixbuf = comics_document_render_pixbuf (document, rc);
368         surface = ev_document_misc_surface_from_pixbuf (pixbuf);
369         g_object_unref (pixbuf);
370         
371         return surface;
372 }
373
374 static void
375 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
376                                 gint             width,
377                                 gint             height,
378                                 gpointer         data)
379 {
380         double *scale = data;
381         int w = (width  * (*scale) + 0.5);
382         int h = (height * (*scale) + 0.5);
383
384         gdk_pixbuf_loader_set_size (loader, w, h);
385 }
386
387 static void
388 comics_document_finalize (GObject *object)
389 {
390         ComicsDocument *comics_document = COMICS_DOCUMENT (object);
391
392         if (comics_document->archive)
393                 g_free (comics_document->archive);
394
395         if (comics_document->page_names) {
396                 g_slist_foreach (comics_document->page_names,
397                                  (GFunc) g_free, NULL);
398                 g_slist_free (comics_document->page_names);
399         }
400
401         if (comics_document->extract_command)
402                 g_free (comics_document->extract_command);
403
404         G_OBJECT_CLASS (comics_document_parent_class)->finalize (object);
405 }
406
407 static void
408 comics_document_class_init (ComicsDocumentClass *klass)
409 {
410         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
411         gobject_class->finalize = comics_document_finalize;
412 }
413
414 static EvDocumentInfo *
415 comics_document_get_info (EvDocument *document)
416 {
417         EvDocumentInfo *info;
418         info = g_new0 (EvDocumentInfo, 1);
419         return info;
420 }
421
422 static void
423 comics_document_document_iface_init (EvDocumentIface *iface)
424 {
425         iface->load = comics_document_load;
426         iface->save = comics_document_save;
427         iface->get_n_pages = comics_document_get_n_pages;
428         iface->get_page_size = comics_document_get_page_size;
429         iface->render = comics_document_render;
430         iface->get_info = comics_document_get_info;
431 }
432
433 static void
434 comics_document_init (ComicsDocument *comics_document)
435 {
436         comics_document->archive = NULL;
437         comics_document->page_names = NULL;
438         comics_document->extract_command = NULL;
439         comics_document->n_pages = 0;
440 }
441
442 /* Returns a list of file extensions supported by gdk-pixbuf */
443 static GSList*
444 get_supported_image_extensions()
445 {
446         GSList *extensions = NULL;
447         GSList *formats = gdk_pixbuf_get_formats ();
448         GSList *l;
449
450         for (l = formats; l != NULL; l = l->next) {
451                 int i;
452                 gchar **ext = gdk_pixbuf_format_get_extensions (l->data);
453
454                 for (i = 0; ext[i] != NULL; i++) {
455                         extensions = g_slist_append (extensions,
456                                                      g_strdup (ext[i]));
457                 }
458
459                 g_strfreev (ext);
460         }
461
462         g_slist_free (formats);
463         return extensions;
464 }
465
466 static GdkPixbuf *
467 comics_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
468                                           EvRenderContext      *rc,
469                                           gboolean              border)
470 {
471         GdkPixbuf *thumbnail;
472
473         thumbnail = comics_document_render_pixbuf (EV_DOCUMENT (document), rc);
474
475         if (border) {
476               GdkPixbuf *tmp_pixbuf = thumbnail;
477               
478               thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
479               g_object_unref (tmp_pixbuf);
480         }
481
482         return thumbnail;
483 }
484
485 static void
486 comics_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
487                                            EvRenderContext      *rc,
488                                            gint                 *width,
489                                            gint                 *height)
490 {
491         gdouble page_width, page_height;
492         
493         comics_document_get_page_size (EV_DOCUMENT (document), rc->page,
494                                        &page_width, &page_height);
495
496         if (rc->rotation == 90 || rc->rotation == 270) {
497                 *width = (gint) (page_height * rc->scale);
498                 *height = (gint) (page_width * rc->scale);
499         } else {
500                 *width = (gint) (page_width * rc->scale);
501                 *height = (gint) (page_height * rc->scale);
502         }
503 }
504
505 static void
506 comics_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
507 {
508         iface->get_thumbnail = comics_document_thumbnails_get_thumbnail;
509         iface->get_dimensions = comics_document_thumbnails_get_dimensions;
510 }
511
512 static char**
513 extract_argv (EvDocument *document, gint page)
514 {
515         ComicsDocument *comics_document = COMICS_DOCUMENT (document);
516         char **argv;
517         char *command_line, *quoted_archive, *quoted_filename;
518         GError *err = NULL;
519
520         quoted_archive = g_shell_quote (comics_document->archive);
521         if (comics_document->regex_arg) {
522                 quoted_filename = comics_regex_quote (
523                         g_slist_nth_data (comics_document->page_names, page));
524         } else {
525                 quoted_filename = g_shell_quote (
526                         g_slist_nth_data (comics_document->page_names, page));
527         }
528
529         command_line = g_strdup_printf ("%s -- %s %s",
530                                         comics_document->extract_command,
531                                         quoted_archive,
532                                         quoted_filename);
533
534         g_shell_parse_argv (command_line, NULL, &argv, &err);
535         
536         if (err) {
537                 g_warning ("Error %s", err->message);
538                 return NULL;
539         }
540         
541         g_free (command_line);
542         g_free (quoted_archive);
543         g_free (quoted_filename);
544         return argv;
545 }