]> www.fi.muni.cz Git - evince.git/blobdiff - backend/comics/comics-document.c
Update for release 2.25.91
[evince.git] / backend / comics / comics-document.c
index 64bd2b75eabaaa9f301e7db13b5a03321c1d5d75..dc5a8b0dde79300b24ed5888bb8c0800ab40e00b 100644 (file)
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
  * 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 <unistd.h>
 #include <string.h>
-#include <glib/gi18n.h>
-#include <libgnomevfs/gnome-vfs-mime-utils.h>
+#include <glib/gi18n-lib.h>
+#include <gio/gio.h>
 
 #include "comics-document.h"
 #include "ev-document-misc.h"
 #include "ev-document-thumbnails.h"
 
 #include "comics-document.h"
 #include "ev-document-misc.h"
 #include "ev-document-thumbnails.h"
+#include "ev-file-helpers.h"
 
 struct _ComicsDocumentClass
 {
 
 struct _ComicsDocumentClass
 {
@@ -58,13 +62,10 @@ static char**     extract_argv                   (EvDocument *document,
                                                  gint page);
 
 
                                                  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 *
        } );
 
 static char *
@@ -84,6 +85,8 @@ comics_regex_quote (const char *s)
        case ']':
        case '*':
        case '\\':
        case ']':
        case '*':
        case '\\':
+           *d++ = '\\';
+           break;
        case '\'':
            *d++ = '\'';
            *d++ = '\\';
        case '\'':
            *d++ = '\'';
            *d++ = '\\';
@@ -106,53 +109,96 @@ comics_document_load (EvDocument *document,
 {
        ComicsDocument *comics_document = COMICS_DOCUMENT (document);
        GSList *supported_extensions;
 {
        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;
        gchar **cbr_files;
        gboolean success;
        int i, retval;
+       GError *err = NULL;
 
        comics_document->archive = g_filename_from_uri (uri, NULL, error);
 
        comics_document->archive = g_filename_from_uri (uri, NULL, error);
-       g_return_val_if_fail (comics_document->archive != NULL, FALSE);
+       if (!comics_document->archive)
+               return FALSE;
+
+       mime_type = ev_file_get_mime_type (uri, FALSE, &err);
+       if (!mime_type) {
+               if (err) {
+                       g_propagate_error (error, err);
+               } else {
+                       g_set_error_literal (error,
+                                             EV_DOCUMENT_ERROR,
+                                             EV_DOCUMENT_ERROR_INVALID,
+                                             _("Unknown MIME Type"));
+               }
+
+               return FALSE;
+       }
 
        quoted_file = g_shell_quote (comics_document->archive);
 
        quoted_file = g_shell_quote (comics_document->archive);
-       mime_type = gnome_vfs_get_mime_type (uri);
 
        /* FIXME, use proper cbr/cbz mime types once they're
         * included in shared-mime-info */
 
        /* 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;
                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/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;
                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;
+       } else {
+               g_set_error (error,
+                            EV_DOCUMENT_ERROR,
+                            EV_DOCUMENT_ERROR_INVALID,
+                            _("Not a comic book MIME type: %s"),
+                            mime_type);
+               g_free (mime_type);
+               g_free (quoted_file);
+               return FALSE;
        }
 
        }
 
+       g_free (mime_type);
        g_free (quoted_file);
 
        /* Get list of files in archive */
        success = g_spawn_command_line_sync (list_files_command,
        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 (list_files_command);
 
        if (!success) {
-               g_free (mime_type);
                return FALSE;
        } else if (retval != 0) {
                return FALSE;
        } else if (retval != 0) {
-               g_set_error (error,
-                            EV_DOCUMENT_ERROR,
-                            EV_DOCUMENT_ERROR_INVALID,
-                            _("File corrupted."));
-               g_free (mime_type);
+               g_set_error_literal (error,
+                                     EV_DOCUMENT_ERROR,
+                                     EV_DOCUMENT_ERROR_INVALID,
+                                     _("File corrupted."));
+               return FALSE;
+       }
+
+       /* FIXME: is this safe against filenames containing \n in the archive ? */
+       cbr_files = g_strsplit (std_out, "\n", 0);
+       g_free (std_out);
+
+       if (!cbr_files) {
+               g_set_error_literal (error,
+                                    EV_DOCUMENT_ERROR,
+                                    EV_DOCUMENT_ERROR_INVALID,
+                                    _("No files in archive."));
                return FALSE;
        }
 
                return FALSE;
        }
 
-       cbr_files = g_strsplit (stdout, "\n", 0);
        supported_extensions = get_supported_image_extensions ();
        for (i = 0; cbr_files[i] != NULL; i++) {
                gchar *suffix = g_strrstr (cbr_files[i], ".");
        supported_extensions = get_supported_image_extensions ();
        for (i = 0; cbr_files[i] != NULL; i++) {
                gchar *suffix = g_strrstr (cbr_files[i], ".");
@@ -173,8 +219,6 @@ comics_document_load (EvDocument *document,
                g_free (suffix);
        }
 
                g_free (suffix);
        }
 
-       g_free (stdout);
-       g_free (mime_type);
        g_strfreev (cbr_files);
        g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
        g_slist_free (supported_extensions);
        g_strfreev (cbr_files);
        g_slist_foreach (supported_extensions, (GFunc) g_free, NULL);
        g_slist_free (supported_extensions);
@@ -210,7 +254,7 @@ comics_document_get_n_pages (EvDocument *document)
 
 static void
 comics_document_get_page_size (EvDocument *document,
 
 static void
 comics_document_get_page_size (EvDocument *document,
-                              int         page,
+                              EvPage     *page,
                               double     *width,
                               double     *height)
 {
                               double     *width,
                               double     *height)
 {
@@ -221,7 +265,7 @@ comics_document_get_page_size (EvDocument *document,
        gint outpipe = -1;
        GPid child_pid = -1;
 
        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,
        success = g_spawn_async_with_pipes (NULL, argv, NULL,
                                            G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
                                            NULL, NULL,
@@ -268,7 +312,7 @@ get_page_size_area_prepared_cb (GdkPixbufLoader *loader,
 }
 
 static GdkPixbuf *
 }
 
 static GdkPixbuf *
-comics_document_render_pixbuf (EvDocument  *document,
+comics_document_render_pixbuf (EvDocument      *document,
                               EvRenderContext *rc)
 {
        GdkPixbufLoader *loader;
                               EvRenderContext *rc)
 {
        GdkPixbufLoader *loader;
@@ -279,7 +323,7 @@ comics_document_render_pixbuf (EvDocument  *document,
        gint outpipe = -1;
        GPid child_pid = -1;
 
        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,
        success = g_spawn_async_with_pipes (NULL, argv, NULL,
                                            G_SPAWN_SEARCH_PATH
                                            | G_SPAWN_STDERR_TO_DEV_NULL,
@@ -309,9 +353,24 @@ comics_document_render_pixbuf (EvDocument  *document,
                                                   360 - rc->rotation);
        g_spawn_close_pid (child_pid);
        g_object_unref (loader);
                                                   360 - rc->rotation);
        g_spawn_close_pid (child_pid);
        g_object_unref (loader);
+
        return rotated_pixbuf;
 }
 
        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,
 static void
 render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
                                gint             width,
@@ -319,8 +378,8 @@ render_pixbuf_size_prepared_cb (GdkPixbufLoader *loader,
                                gpointer         data)
 {
        double *scale = data;
                                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);
 }
 
        gdk_pixbuf_loader_set_size (loader, w, h);
 }
@@ -352,12 +411,6 @@ comics_document_class_init (ComicsDocumentClass *klass)
        gobject_class->finalize = comics_document_finalize;
 }
 
        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)
 {
 static EvDocumentInfo *
 comics_document_get_info (EvDocument *document)
 {
@@ -371,11 +424,10 @@ comics_document_document_iface_init (EvDocumentIface *iface)
 {
        iface->load = comics_document_load;
        iface->save = comics_document_save;
 {
        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->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
 }
 
 static void