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