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