[gnome-logs/wip/async-journal: 1/2] Add asynchronous query interface to GlJournal



commit 4fa5ff002e8bae89a51bae9840ee10146add6664
Author: David King <davidk gnome org>
Date:   Thu Jan 15 11:27:15 2015 +0000

    Add asynchronous query interface to GlJournal

 src/gl-journal.c |   82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gl-journal.h |    4 ++-
 2 files changed, 85 insertions(+), 1 deletions(-)
---
diff --git a/src/gl-journal.c b/src/gl-journal.c
index 04fe4f1..6b64e60 100644
--- a/src/gl-journal.c
+++ b/src/gl-journal.c
@@ -310,6 +310,88 @@ out:
     return NULL;
 }
 
+static GlJournalQuery *
+gl_journal_query_copy (const GlJournalQuery *source)
+{
+    GlJournalQuery *result;
+    guint n_matches = 0;
+    gchar **matches;
+    guint i;
+
+    result = g_slice_new (GlJournalQuery);
+
+    if (source->matches)
+    {
+        n_matches = g_strv_length (source->matches);
+    }
+
+    /* Remember the trailing NULL. */
+    matches = g_new (gchar *, n_matches + 1);
+
+    for (i = 0; i < n_matches; i++)
+    {
+        matches[i] = g_strdup (source->matches[i]);
+    }
+
+    matches[n_matches] = NULL;
+
+    result->matches = matches;
+    result->n_results = source->n_results;
+
+    return result;
+}
+
+static void
+gl_journal_query_free (GlJournalQuery *query)
+{
+    g_strfreev (query->matches);
+    g_slice_free (GlJournalQuery, query);
+}
+
+static void
+gl_journal_query_async_thread (GTask *task,
+                               gpointer source_object,
+                               gpointer task_data,
+                               GCancellable *cancellable)
+{
+    GList *results;
+
+    /* TODO: gl_journal_query() should accept a cancellable. */
+    results = gl_journal_query (GL_JOURNAL (source_object),
+                                (GlJournalQuery *)task_data);
+    g_task_return_pointer (task, results,
+                           (GDestroyNotify)gl_journal_results_free);
+    /* TODO: Return error if necessary (if cancelled?). */
+}
+
+void
+gl_journal_query_async (GlJournal *self,
+                        const GlJournalQuery *query,
+                        GCancellable *cancellable,
+                        GAsyncReadyCallback callback,
+                        gpointer user_data)
+{
+    GTask *task;
+    GlJournalQuery *data;
+
+    data = gl_journal_query_copy (query);
+
+    task = g_task_new (self, cancellable, callback, user_data);
+    g_task_set_task_data (task, data, (GDestroyNotify)gl_journal_query_free);
+    g_task_run_in_thread (task, gl_journal_query_async_thread);
+    g_object_unref (task);
+}
+
+GList *
+gl_journal_query_finish (GlJournal *self,
+                         GAsyncResult *res,
+                         GError **error)
+{
+    g_return_val_if_fail (g_task_is_valid (res, self), NULL);
+
+    return g_task_propagate_pointer (G_TASK (res), error);
+}
+
 GList *
 gl_journal_query (GlJournal *self, const GlJournalQuery *query)
 {
diff --git a/src/gl-journal.h b/src/gl-journal.h
index c210074..f4210b5 100644
--- a/src/gl-journal.h
+++ b/src/gl-journal.h
@@ -19,7 +19,7 @@
 #ifndef GL_JOURNAL_H_
 #define GL_JOURNAL_H_
 
-#include <glib-object.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -82,6 +82,8 @@ typedef struct
 
 GType gl_journal_result_get_type (void);
 GType gl_journal_get_type (void);
+void gl_journal_query_async (GlJournal *self, const GlJournalQuery *query, GCancellable *cancellable, 
GAsyncReadyCallback callback, gpointer user_data);
+GList * gl_journal_query_finish (GlJournal *self, GAsyncResult *res, GError **error);
 GList * gl_journal_query (GlJournal *self, const GlJournalQuery *query);
 GlJournalResult * gl_journal_result_ref (GlJournalResult *result);
 void gl_journal_result_unref (GlJournalResult *result);


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