]> www.fi.muni.cz Git - evince.git/blobdiff - backend/comics/comics-document.c
Do not trust file extensions when getting the command needed to uncompress
[evince.git] / backend / comics / comics-document.c
index e0fdd31ef781b0223b05afa746cbf11001efede9..6ea6a7a1cdc741a165834c8eac6e1d22217ed1e3 100644 (file)
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+#include "config.h"
+
+#include <config.h>
 #include <unistd.h>
 #include <string.h>
 #include <glib/gi18n.h>
-#include <libgnomevfs/gnome-vfs-mime-utils.h>
+#include <gio/gio.h>
 
 #include "comics-document.h"
 #include "ev-document-misc.h"
 #include "ev-document-thumbnails.h"
+#include "ev-file-helpers.h"
 
 struct _ComicsDocumentClass
 {
@@ -58,13 +62,10 @@ static char**     extract_argv                   (EvDocument *document,
                                                  gint page);
 
 
-G_DEFINE_TYPE_WITH_CODE (
-       ComicsDocument, comics_document, G_TYPE_OBJECT,
+EV_BACKEND_REGISTER_WITH_CODE (ComicsDocument, comics_document,
        {
-               G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
-                                      comics_document_document_iface_init);
-               G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
-                                      comics_document_document_thumbnails_iface_init);
+               EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
+                                               comics_document_document_thumbnails_iface_init);
        } );
 
 static char *
@@ -72,7 +73,7 @@ comics_regex_quote (const char *s)
 {
     char *ret, *d;
 
-    d = ret = g_malloc (strlen (s) * 2 + 3);
+    d = ret = g_malloc (strlen (s) * 4 + 3);
     
     *d++ = '\'';
 
@@ -84,8 +85,12 @@ comics_regex_quote (const char *s)
        case ']':
        case '*':
        case '\\':
+           *d++ = '\\';
+           break;
        case '\'':
+           *d++ = '\'';
            *d++ = '\\';
+           *d++ = '\'';
            break;
        }
        *d = *s;
@@ -104,7 +109,8 @@ comics_document_load (EvDocument *document,
 {
        ComicsDocument *comics_document = COMICS_DOCUMENT (document);
        GSList *supported_extensions;
-       gchar *list_files_command = NULL, *stdout, *quoted_file, *mime_type;
+       gchar *list_files_command = NULL, *std_out, *quoted_file;
+       gchar *mime_type;
        gchar **cbr_files;
        gboolean success;
        int i, retval;
@@ -113,44 +119,51 @@ comics_document_load (EvDocument *document,
        g_return_val_if_fail (comics_document->archive != NULL, FALSE);
 
        quoted_file = g_shell_quote (comics_document->archive);
-       mime_type = gnome_vfs_get_mime_type (uri);
+       mime_type = ev_file_get_mime_type (uri, FALSE, NULL);
 
        /* FIXME, use proper cbr/cbz mime types once they're
         * included in shared-mime-info */
-       if (!strcmp (mime_type, "application/x-cbr")) {
+       if (!strcmp (mime_type, "application/x-cbr") ||
+           !strcmp (mime_type, "application/x-rar")) {
                comics_document->extract_command =
                        g_strdup ("unrar p -c- -ierr");
                list_files_command =
                        g_strdup_printf ("unrar vb -c- -- %s", quoted_file);
                comics_document->regex_arg = FALSE;
-       } else if (!strcmp (mime_type, "application/x-cbz")) {
+       } else if (!strcmp (mime_type, "application/x-cbz") ||
+                  !strcmp (mime_type, "application/x-zip")) {
                comics_document->extract_command =
                        g_strdup ("unzip -p -C");
                list_files_command = 
                        g_strdup_printf ("zipinfo -1 -- %s", quoted_file);
                comics_document->regex_arg = TRUE;
+       } else if (!strcmp (mime_type, "application/x-cb7")) {
+               comics_document->extract_command =
+                       g_strdup ("7zr x -so");
+               list_files_command = 
+                       g_strdup_printf ("7zr l -- %s", quoted_file);
+               comics_document->regex_arg = TRUE;
        }
 
+       g_free (mime_type);
        g_free (quoted_file);
 
        /* Get list of files in archive */
        success = g_spawn_command_line_sync (list_files_command,
-                                            &stdout, NULL, &retval, error);
+                                            &std_out, NULL, &retval, error);
        g_free (list_files_command);
 
        if (!success) {
-               g_free (mime_type);
                return FALSE;
        } else if (retval != 0) {
                g_set_error (error,
                             EV_DOCUMENT_ERROR,
                             EV_DOCUMENT_ERROR_INVALID,
                             _("File corrupted."));
-               g_free (mime_type);
                return FALSE;
        }
 
-       cbr_files = g_strsplit (stdout, "\n", 0);
+       cbr_files = g_strsplit (std_out, "\n", 0);
        supported_extensions = get_supported_image_extensions ();
        for (i = 0; cbr_files[i] != NULL; i++) {
                gchar *suffix = g_strrstr (cbr_files[i], ".");
@@ -171,8 +184,7 @@ comics_document_load (EvDocument *document,
                g_free (suffix);
        }
 
-       g_free (stdout);
-       g_free (mime_type);
+       g_free (std_out);
        g_strfreev (cbr_files);
        g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
        g_slist_free (supported_extensions);
@@ -208,7 +220,7 @@ comics_document_get_n_pages (EvDocument *document)
 
 static void
 comics_document_get_page_size (EvDocument *document,
-                              int         page,
+                              EvPage     *page,
                               double     *width,
                               double     *height)
 {
@@ -219,7 +231,7 @@ comics_document_get_page_size (EvDocument *document,
        gint outpipe = -1;
        GPid child_pid = -1;
 
-       argv = extract_argv (document, page);
+       argv = extract_argv (document, page->index);
        success = g_spawn_async_with_pipes (NULL, argv, NULL,
                                            G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
                                            NULL, NULL,
@@ -266,7 +278,7 @@ get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
 }
 
 static GdkPixbuf *
-comics_document_render_pixbuf (EvDocument  *document,
+comics_document_render_pixbuf (EvDocument      *document,
                               EvRenderContext *rc)
 {
        GdkPixbufLoader *loader;
@@ -277,7 +289,7 @@ comics_document_render_pixbuf (EvDocument  *document,
        gint outpipe = -1;
        GPid child_pid = -1;
 
-       argv = extract_argv (document, rc->page);
+       argv = extract_argv (document, rc->page->index);
        success = g_spawn_async_with_pipes (NULL, argv, NULL,
                                            G_SPAWN_SEARCH_PATH
                                            | G_SPAWN_STDERR_TO_DEV_NULL,
@@ -307,9 +319,24 @@ comics_document_render_pixbuf (EvDocument  *document,
                                                   360 - rc->rotation);
        g_spawn_close_pid (child_pid);
        g_object_unref (loader);
+
        return rotated_pixbuf;
 }
 
+static cairo_surface_t *
+comics_document_render (EvDocument      *document,
+                       EvRenderContext *rc)
+{
+       GdkPixbuf       *pixbuf;
+       cairo_surface_t *surface;
+
+       pixbuf = comics_document_render_pixbuf (document, rc);
+       surface = ev_document_misc_surface_from_pixbuf (pixbuf);
+       g_object_unref (pixbuf);
+       
+       return surface;
+}
+
 static void
 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
                                gint             width,
@@ -317,8 +344,8 @@ render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
                                gpointer         data)
 {
        double *scale = data;
-       int w = width  * (*scale);
-       int h = height * (*scale);
+       int w = (width  * (*scale) + 0.5);
+       int h = (height * (*scale) + 0.5);
 
        gdk_pixbuf_loader_set_size (loader, w, h);
 }
@@ -350,12 +377,6 @@ comics_document_class_init (ComicsDocumentClass *klass)
        gobject_class->finalize = comics_document_finalize;
 }
 
-static gboolean
-comics_document_can_get_text (EvDocument *document)
-{
-       return FALSE;
-}
-
 static EvDocumentInfo *
 comics_document_get_info (EvDocument *document)
 {
@@ -369,11 +390,10 @@ comics_document_document_iface_init (EvDocumentIface *iface)
 {
        iface->load = comics_document_load;
        iface->save = comics_document_save;
-       iface->can_get_text  = comics_document_can_get_text;
-       iface->get_n_pages   = comics_document_get_n_pages;
+       iface->get_n_pages = comics_document_get_n_pages;
        iface->get_page_size = comics_document_get_page_size;
-       iface->render_pixbuf = comics_document_render_pixbuf;
-       iface->get_info      = comics_document_get_info;
+       iface->render = comics_document_render;
+       iface->get_info = comics_document_get_info;
 }
 
 static void
@@ -461,6 +481,7 @@ extract_argv (EvDocument *document, gint page)
        ComicsDocument *comics_document = COMICS_DOCUMENT (document);
        char **argv;
        char *command_line, *quoted_archive, *quoted_filename;
+       GError *err = NULL;
 
        quoted_archive = g_shell_quote (comics_document->archive);
        if (comics_document->regex_arg) {
@@ -475,8 +496,14 @@ extract_argv (EvDocument *document, gint page)
                                        comics_document->extract_command,
                                        quoted_archive,
                                        quoted_filename);
-       g_shell_parse_argv (command_line, NULL, &argv, NULL);
 
+       g_shell_parse_argv (command_line, NULL, &argv, &err);
+       
+       if (err) {
+               g_warning ("Error %s", err->message);
+               return NULL;
+       }
+       
        g_free (command_line);
        g_free (quoted_archive);
        g_free (quoted_filename);