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