[gnome-photos/wip/rishi/thumbnailer: 9/10] thumbnailer: Implement cancellation



commit 895e890bb649ffb37f5b3fdd91fcbe94285a38ae
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Feb 22 16:39:27 2017 +0100

    thumbnailer: Implement cancellation

 src/photos-thumbnailer.c |   64 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 63 insertions(+), 1 deletions(-)
---
diff --git a/src/photos-thumbnailer.c b/src/photos-thumbnailer.c
index 94dd606..f0090bf 100644
--- a/src/photos-thumbnailer.c
+++ b/src/photos-thumbnailer.c
@@ -28,6 +28,7 @@
 #include <glib/gi18n.h>
 
 #include "photos-debug.h"
+#include "photos-error.h"
 #include "photos-gegl.h"
 #include "photos-pipeline.h"
 #include "photos-pixbuf.h"
@@ -39,6 +40,7 @@ struct _PhotosThumbnailer
 {
   GApplication parent_instance;
   GDBusConnection *connection;
+  GHashTable *cancellables;
   PhotosThumbnailerDBus *skeleton;
   gchar *address;
 };
@@ -532,6 +534,52 @@ photos_thumbnailer_generate_thumbnail_finish (PhotosThumbnailer *self, GAsyncRes
 }
 
 
+static gboolean
+photos_thumbnailer_handle_cancel (PhotosThumbnailer *self,
+                                  GDBusMethodInvocation *invocation,
+                                  guint serial)
+{
+  GCancellable *cancellable;
+  GDBusConnection *connection;
+  GDBusMethodInvocation *invocation_ongoing;
+  GHashTableIter iter;
+
+  g_return_val_if_fail (PHOTOS_IS_THUMBNAILER (self), FALSE);
+  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE);
+
+  photos_debug (PHOTOS_DEBUG_THUMBNAILER, "Handling Cancel for %u", serial);
+  g_application_hold (G_APPLICATION (self));
+
+  connection = g_dbus_method_invocation_get_connection (invocation);
+
+  g_hash_table_iter_init (&iter, self->cancellables);
+  while (g_hash_table_iter_next (&iter, (gpointer *) &invocation_ongoing, (gpointer *) &cancellable))
+    {
+      GDBusConnection *connection_ongoing;
+      GDBusMessage *message_ongoing;
+      guint32 serial_ongoing;
+
+      connection_ongoing = g_dbus_method_invocation_get_connection (invocation_ongoing);
+      message_ongoing = g_dbus_method_invocation_get_message (invocation_ongoing);
+      serial_ongoing = g_dbus_message_get_serial (message_ongoing);
+
+      if (connection == connection_ongoing && serial == serial_ongoing)
+        {
+          g_cancellable_cancel (cancellable);
+          photos_thumbnailer_dbus_complete_generate_thumbnail (self->skeleton, invocation);
+          goto out;
+        }
+    }
+
+  g_dbus_method_invocation_return_error_literal (invocation, PHOTOS_ERROR, 0, "Invalid serial");
+
+ out:
+  photos_debug (PHOTOS_DEBUG_THUMBNAILER, "Completed Cancel");
+  g_application_release (G_APPLICATION (self));
+  return TRUE;
+}
+
+
 static void
 photos_thumbnailer_handle_generate_thumbnail_generate_thumbnail (GObject *source_object,
                                                                  GAsyncResult *res,
@@ -553,6 +601,7 @@ photos_thumbnailer_handle_generate_thumbnail_generate_thumbnail (GObject *source
  out:
   photos_debug (PHOTOS_DEBUG_THUMBNAILER, "Completed GenerateThumbnail");
   g_application_release (G_APPLICATION (self));
+  g_hash_table_remove (self->cancellables, invocation);
   g_object_unref (invocation);
 }
 
@@ -569,6 +618,8 @@ photos_thumbnailer_handle_generate_thumbnail (PhotosThumbnailer *self,
                                               const gchar *thumbnail_path,
                                               gint thumbnail_size)
 {
+  GCancellable *cancellable = NULL;
+
   g_return_val_if_fail (PHOTOS_IS_THUMBNAILER (self), FALSE);
   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE);
   g_return_val_if_fail (uri != NULL && uri[0] != '\0', FALSE);
@@ -582,6 +633,9 @@ photos_thumbnailer_handle_generate_thumbnail (PhotosThumbnailer *self,
   if (pipeline_uri[0] == '\0')
     pipeline_uri = NULL;
 
+  cancellable = g_cancellable_new ();
+  g_hash_table_insert (self->cancellables, g_object_ref (invocation), g_object_ref (cancellable));
+
   g_application_hold (G_APPLICATION (self));
   photos_thumbnailer_generate_thumbnail_async (self,
                                                uri,
@@ -592,10 +646,11 @@ photos_thumbnailer_handle_generate_thumbnail (PhotosThumbnailer *self,
                                                pipeline_uri,
                                                thumbnail_path,
                                                thumbnail_size,
-                                               NULL,
+                                               cancellable,
                                                
photos_thumbnailer_handle_generate_thumbnail_generate_thumbnail,
                                                g_object_ref (invocation));
 
+  g_object_unref (cancellable);
   return TRUE;
 }
 
@@ -704,6 +759,7 @@ photos_thumbnailer_dispose (GObject *object)
 
   g_clear_object (&self->connection);
   g_clear_object (&self->skeleton);
+  g_clear_pointer (&self->cancellables, (GDestroyNotify) g_hash_table_unref);
 
   G_OBJECT_CLASS (photos_thumbnailer_parent_class)->dispose (object);
 }
@@ -728,8 +784,14 @@ photos_thumbnailer_init (PhotosThumbnailer *self)
 {
   photos_gegl_ensure_builtins ();
 
+  self->cancellables = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, g_object_unref);
+
   self->skeleton = photos_thumbnailer_dbus_skeleton_new ();
   g_signal_connect_swapped (self->skeleton,
+                            "handle-cancel",
+                            G_CALLBACK (photos_thumbnailer_handle_cancel),
+                            self);
+  g_signal_connect_swapped (self->skeleton,
                             "handle-generate-thumbnail",
                             G_CALLBACK (photos_thumbnailer_handle_generate_thumbnail),
                             self);


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