[evince: 13/13] libview: Add API to load from streams and gfile



commit 302102256c3a2bc95435db90634e524a7416af10
Author: Christian Persch <chpe gnome org>
Date:   Sun May 13 20:49:51 2012 +0200

    libview: Add API to load from streams and gfile
    
    Add EvJobLoadStream and EvJobLoadGFile to load documents from
    GInputStream and GFile.
    
    Part of bug #654832.

 help/reference/libview/libevview-sections.txt |   22 ++
 libview/ev-jobs.c                             |  288 +++++++++++++++++++++++++
 libview/ev-jobs.h                             |   66 ++++++
 3 files changed, 376 insertions(+), 0 deletions(-)
---
diff --git a/help/reference/libview/libevview-sections.txt b/help/reference/libview/libevview-sections.txt
index fe50b2e..4e6794b 100644
--- a/help/reference/libview/libevview-sections.txt
+++ b/help/reference/libview/libevview-sections.txt
@@ -141,6 +141,10 @@ EvJobFonts
 EvJobFontsClass
 EvJobLoad
 EvJobLoadClass
+EvJobLoadStream
+EvJobLoadStreamClass
+EvJobLoadGFile
+EvJobLoadGFileClass
 EvJobSave
 EvJobSaveClass
 EvJobFind
@@ -174,6 +178,14 @@ ev_job_fonts_new
 ev_job_load_new
 ev_job_load_set_uri
 ev_job_load_set_password
+ev_job_load_stream_new
+ev_job_load_stream_set_stream
+ev_job_load_stream_set_load_flags
+ev_job_load_stream_set_password
+ev_job_load_gfile_new
+ev_job_load_gfile_set_gfile
+ev_job_load_gfile_set_load_flags
+ev_job_load_gfile_set_password
 ev_job_save_new
 ev_job_find_new
 ev_job_find_get_n_results
@@ -232,6 +244,16 @@ ev_job_load_get_type
 EV_JOB_LOAD
 EV_JOB_LOAD_CLASS
 EV_IS_JOB_LOAD
+EV_TYPE_JOB_LOAD_STREAM
+ev_job_load_stream_get_type
+EV_JOB_LOAD_STREAM
+EV_JOB_LOAD_STREAM_CLASS
+EV_IS_JOB_LOAD_STREAM
+EV_TYPE_JOB_LOAD_GFILE
+ev_job_load_gfile_get_type
+EV_JOB_LOAD_GFILE
+EV_JOB_LOAD_GFILE_CLASS
+EV_IS_JOB_LOAD_GFILE
 EV_TYPE_JOB_SAVE
 ev_job_save_get_type
 EV_JOB_SAVE
diff --git a/libview/ev-jobs.c b/libview/ev-jobs.c
index 23ee586..e14edff 100644
--- a/libview/ev-jobs.c
+++ b/libview/ev-jobs.c
@@ -99,6 +99,8 @@ G_DEFINE_TYPE (EvJobPageData, ev_job_page_data, EV_TYPE_JOB)
 G_DEFINE_TYPE (EvJobThumbnail, ev_job_thumbnail, EV_TYPE_JOB)
 G_DEFINE_TYPE (EvJobFonts, ev_job_fonts, EV_TYPE_JOB)
 G_DEFINE_TYPE (EvJobLoad, ev_job_load, EV_TYPE_JOB)
+G_DEFINE_TYPE (EvJobLoadStream, ev_job_load_stream, EV_TYPE_JOB)
+G_DEFINE_TYPE (EvJobLoadGFile, ev_job_load_gfile, EV_TYPE_JOB)
 G_DEFINE_TYPE (EvJobSave, ev_job_save, EV_TYPE_JOB)
 G_DEFINE_TYPE (EvJobFind, ev_job_find, EV_TYPE_JOB)
 G_DEFINE_TYPE (EvJobLayers, ev_job_layers, EV_TYPE_JOB)
@@ -1048,6 +1050,292 @@ ev_job_load_set_password (EvJobLoad *job, const gchar *password)
 	job->password = password ? g_strdup (password) : NULL;
 }
 
+/* EvJobLoadStream */
+
+/**
+ * EvJobLoadStream:
+ *
+ * A job class to load a #EvDocument from a #GInputStream.
+ *
+ * Since: 3.6
+ */
+
+static void
+ev_job_load_stream_init (EvJobLoadStream *job)
+{
+        job->flags = EV_DOCUMENT_LOAD_FLAG_NONE;
+
+        EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
+}
+
+static void
+ev_job_load_stream_dispose (GObject *object)
+{
+        EvJobLoadStream *job = EV_JOB_LOAD_STREAM (object);
+
+        if (job->stream) {
+                g_object_unref (job->stream);
+                job->stream = NULL;
+        }
+
+        g_free (job->password);
+        job->password = NULL;
+
+        G_OBJECT_CLASS (ev_job_load_stream_parent_class)->dispose (object);
+}
+
+static gboolean
+ev_job_load_stream_run (EvJob *job)
+{
+        EvJobLoadStream *job_load_stream = EV_JOB_LOAD_STREAM (job);
+        GError *error = NULL;
+
+        ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
+
+        ev_document_fc_mutex_lock ();
+
+        /* This job may already have a document even if the job didn't complete
+           because, e.g., a password is required - if so, just reload_stream rather than
+           creating a new instance */
+        /* FIXME: do we need to rewind the stream here?? */
+        if (job->document) {
+
+                if (job_load_stream->password) {
+                        ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document),
+                                                           job_load_stream->password);
+                }
+
+                job->failed = FALSE;
+                job->finished = FALSE;
+                g_clear_error (&job->error);
+
+                ev_document_load_stream (job->document,
+                                         job_load_stream->stream,
+                                         job_load_stream->flags,
+                                         job->cancellable,
+                                         &error);
+        } else {
+                job->document = ev_document_factory_get_document_for_stream (job_load_stream->stream,
+                                                                             NULL /* mime-type FIXME? */,
+                                                                             job_load_stream->flags,
+                                                                             job->cancellable,
+                                                                             &error);
+        }
+
+        ev_document_fc_mutex_unlock ();
+
+        if (error) {
+                ev_job_failed_from_error (job, error);
+                g_error_free (error);
+        } else {
+                ev_job_succeeded (job);
+        }
+
+        return FALSE;
+}
+
+static void
+ev_job_load_stream_class_init (EvJobLoadStreamClass *class)
+{
+        GObjectClass *oclass = G_OBJECT_CLASS (class);
+        EvJobClass   *job_class = EV_JOB_CLASS (class);
+
+        oclass->dispose = ev_job_load_stream_dispose;
+        job_class->run = ev_job_load_stream_run;
+}
+
+EvJob *
+ev_job_load_stream_new (GInputStream       *stream,
+                        EvDocumentLoadFlags flags)
+{
+        EvJobLoadStream *job;
+
+        job = g_object_new (EV_TYPE_JOB_LOAD_STREAM, NULL);
+        ev_job_load_stream_set_stream (job, stream);
+        ev_job_load_stream_set_load_flags (job, flags);
+
+        return EV_JOB (job);
+}
+
+void
+ev_job_load_stream_set_stream (EvJobLoadStream *job,
+                               GInputStream    *stream)
+{
+        g_return_if_fail (EV_IS_JOB_LOAD_STREAM (job));
+        g_return_if_fail (G_IS_INPUT_STREAM (stream));
+
+        g_object_ref (stream);
+        if (job->stream)
+              g_object_unref (job->stream);
+        job->stream = stream;
+}
+
+void
+ev_job_load_stream_set_load_flags (EvJobLoadStream    *job,
+                                   EvDocumentLoadFlags flags)
+{
+        g_return_if_fail (EV_IS_JOB_LOAD_STREAM (job));
+
+        job->flags = flags;
+}
+
+void
+ev_job_load_stream_set_password (EvJobLoadStream *job,
+                                 const char *password)
+{
+        char *old_password;
+
+        ev_debug_message (DEBUG_JOBS, NULL);
+
+        g_return_if_fail (EV_IS_JOB_LOAD_STREAM (job));
+
+        old_password = job->password;
+        job->password = g_strdup (password);
+        g_free (old_password);
+}
+
+/* EvJobLoadGFile */
+
+/**
+ * EvJobLoadGFile:
+ *
+ * A job class to load a #EvDocument from a #GFile.
+ *
+ * Since: 3.6
+ */
+
+static void
+ev_job_load_gfile_init (EvJobLoadGFile *job)
+{
+        job->flags = EV_DOCUMENT_LOAD_FLAG_NONE;
+
+        EV_JOB (job)->run_mode = EV_JOB_RUN_THREAD;
+}
+
+static void
+ev_job_load_gfile_dispose (GObject *object)
+{
+        EvJobLoadGFile *job = EV_JOB_LOAD_GFILE (object);
+
+        if (job->gfile) {
+                g_object_unref (job->gfile);
+                job->gfile = NULL;
+        }
+
+        g_free (job->password);
+        job->password = NULL;
+
+        G_OBJECT_CLASS (ev_job_load_gfile_parent_class)->dispose (object);
+}
+
+static gboolean
+ev_job_load_gfile_run (EvJob *job)
+{
+        EvJobLoadGFile *job_load_gfile = EV_JOB_LOAD_GFILE (job);
+        GError    *error = NULL;
+
+        ev_profiler_start (EV_PROFILE_JOBS, "%s (%p)", EV_GET_TYPE_NAME (job), job);
+
+        ev_document_fc_mutex_lock ();
+
+        /* This job may already have a document even if the job didn't complete
+           because, e.g., a password is required - if so, just reload_gfile rather than
+           creating a new instance */
+        if (job->document) {
+
+                if (job_load_gfile->password) {
+                        ev_document_security_set_password (EV_DOCUMENT_SECURITY (job->document),
+                                                           job_load_gfile->password);
+                }
+
+                job->failed = FALSE;
+                job->finished = FALSE;
+                g_clear_error (&job->error);
+
+                ev_document_load_gfile (job->document,
+                                        job_load_gfile->gfile,
+                                        job_load_gfile->flags,
+                                        job->cancellable,
+                                        &error);
+        } else {
+                job->document = ev_document_factory_get_document_for_gfile (job_load_gfile->gfile,
+                                                                            job_load_gfile->flags,
+                                                                            job->cancellable,
+                                                                            &error);
+        }
+
+        ev_document_fc_mutex_unlock ();
+
+        if (error) {
+                ev_job_failed_from_error (job, error);
+                g_error_free (error);
+        } else {
+                ev_job_succeeded (job);
+        }
+
+        return FALSE;
+}
+
+static void
+ev_job_load_gfile_class_init (EvJobLoadGFileClass *class)
+{
+        GObjectClass *oclass = G_OBJECT_CLASS (class);
+        EvJobClass   *job_class = EV_JOB_CLASS (class);
+
+        oclass->dispose = ev_job_load_gfile_dispose;
+        job_class->run = ev_job_load_gfile_run;
+}
+
+EvJob *
+ev_job_load_gfile_new (GFile              *gfile,
+                       EvDocumentLoadFlags flags)
+{
+        EvJobLoadGFile *job;
+
+        job = g_object_new (EV_TYPE_JOB_LOAD_GFILE, NULL);
+        ev_job_load_gfile_set_gfile (job, gfile);
+        ev_job_load_gfile_set_load_flags (job, flags);
+
+        return EV_JOB (job);
+}
+
+void
+ev_job_load_gfile_set_gfile (EvJobLoadGFile *job,
+                             GFile          *gfile)
+{
+        g_return_if_fail (EV_IS_JOB_LOAD_GFILE (job));
+        g_return_if_fail (G_IS_FILE (gfile));
+
+        g_object_ref (gfile);
+        if (job->gfile)
+              g_object_unref (job->gfile);
+        job->gfile = gfile;
+}
+
+void
+ev_job_load_gfile_set_load_flags (EvJobLoadGFile     *job,
+                                  EvDocumentLoadFlags flags)
+{
+        g_return_if_fail (EV_IS_JOB_LOAD_GFILE (job));
+
+        job->flags = flags;
+}
+
+void
+ev_job_load_gfile_set_password (EvJobLoadGFile *job,
+                                const char *password)
+{
+        char *old_password;
+
+        ev_debug_message (DEBUG_JOBS, NULL);
+
+        g_return_if_fail (EV_IS_JOB_LOAD_GFILE (job));
+
+        old_password = job->password;
+        job->password = g_strdup (password);
+        g_free (old_password);
+}
+
 /* EvJobSave */
 static void
 ev_job_save_init (EvJobSave *job)
diff --git a/libview/ev-jobs.h b/libview/ev-jobs.h
index 7596d76..2f1107b 100644
--- a/libview/ev-jobs.h
+++ b/libview/ev-jobs.h
@@ -60,6 +60,12 @@ typedef struct _EvJobFontsClass EvJobFontsClass;
 typedef struct _EvJobLoad EvJobLoad;
 typedef struct _EvJobLoadClass EvJobLoadClass;
 
+typedef struct _EvJobLoadStream EvJobLoadStream;
+typedef struct _EvJobLoadStreamClass EvJobLoadStreamClass;
+
+typedef struct _EvJobLoadGFile EvJobLoadGFile;
+typedef struct _EvJobLoadGFileClass EvJobLoadGFileClass;
+
 typedef struct _EvJobSave EvJobSave;
 typedef struct _EvJobSaveClass EvJobSaveClass;
 
@@ -121,6 +127,16 @@ typedef struct _EvJobPrintClass EvJobPrintClass;
 #define EV_JOB_LOAD_CLASS(klass)	     (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_LOAD, EvJobLoadClass))
 #define EV_IS_JOB_LOAD(object)		     (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_LOAD))
 
+#define EV_TYPE_JOB_LOAD_STREAM                     (ev_job_load_stream_get_type())
+#define EV_JOB_LOAD_STREAM(object)                  (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_LOAD_STREAM, EvJobLoadStream))
+#define EV_JOB_LOAD_STREAM_CLASS(klass)             (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_LOAD_STREAM, EvJobLoadStreamClass))
+#define EV_IS_JOB_LOAD_STREAM(object)               (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_LOAD_STREAM))
+
+#define EV_TYPE_JOB_LOAD_GFILE                     (ev_job_load_gfile_get_type())
+#define EV_JOB_LOAD_GFILE(object)                  (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_LOAD_GFILE, EvJobLoadGFile))
+#define EV_JOB_LOAD_GFILE_CLASS(klass)             (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_LOAD_GFILE, EvJobLoadGFileClass))
+#define EV_IS_JOB_LOAD_GFILE(object)               (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_JOB_LOAD_GFILE))
+
 #define EV_TYPE_JOB_SAVE		     (ev_job_save_get_type())
 #define EV_JOB_SAVE(object)	     	     (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_JOB_SAVE, EvJobSave))
 #define EV_JOB_SAVE_CLASS(klass)	     (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_JOB_SAVE, EvJobSaveClass))
@@ -322,6 +338,34 @@ struct _EvJobLoadClass
 	EvJobClass parent_class;
 };
 
+struct _EvJobLoadStream
+{
+        EvJob parent;
+
+        char *password;
+        GInputStream *stream;
+        EvDocumentLoadFlags flags;
+};
+
+struct _EvJobLoadStreamClass
+{
+        EvJobClass parent_class;
+};
+
+struct _EvJobLoadGFile
+{
+        EvJob parent;
+
+        char *password;
+        GFile *gfile;
+        EvDocumentLoadFlags flags;
+};
+
+struct _EvJobLoadGFileClass
+{
+        EvJobClass parent_class;
+};
+
 struct _EvJobSave
 {
 	EvJob parent;
@@ -462,6 +506,28 @@ void            ev_job_load_set_uri       (EvJobLoad       *load,
 void            ev_job_load_set_password  (EvJobLoad       *job,
 					   const gchar     *password);
 
+/* EvJobLoadStream */
+GType           ev_job_load_stream_get_type       (void) G_GNUC_CONST;
+EvJob          *ev_job_load_stream_new            (GInputStream       *stream,
+                                                   EvDocumentLoadFlags flags);
+void            ev_job_load_stream_set_stream     (EvJobLoadStream    *job,
+                                                   GInputStream       *stream);
+void            ev_job_load_stream_set_load_flags (EvJobLoadStream    *job,
+                                                   EvDocumentLoadFlags flags);
+void            ev_job_load_stream_set_password   (EvJobLoadStream    *job,
+                                                   const gchar        *password);
+
+/* EvJobLoadGFile */
+GType           ev_job_load_gfile_get_type        (void) G_GNUC_CONST;
+EvJob          *ev_job_load_gfile_new             (GFile              *gfile,
+                                                   EvDocumentLoadFlags flags);
+void            ev_job_load_gfile_set_gfile       (EvJobLoadGFile     *job,
+                                                   GFile              *gfile);
+void            ev_job_load_gfile_set_load_flags  (EvJobLoadGFile     *job,
+                                                   EvDocumentLoadFlags flags);
+void            ev_job_load_gfile_set_password    (EvJobLoadGFile     *job,
+                                                   const gchar        *password);
+
 /* EvJobSave */
 GType           ev_job_save_get_type      (void) G_GNUC_CONST;
 EvJob          *ev_job_save_new           (EvDocument      *document,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]