[gnome-shell/wip/carlosg/selection-get-bytes] st: Add st_clipboard_get_content()



commit ec36762309ab12f215608f7ec9c94eea1e7ea2a3
Author: Carlos Garnacho <carlosg gnome org>
Date:   Fri Jun 19 13:27:04 2020 +0200

    st: Add st_clipboard_get_content()
    
    Complementing st_clipboard_set_content(), this function allows retrieving
    specific mimetypes from the selection as GBytes.
    
    Related: https://gitlab.gnome.org/GNOME/nautilus/-/issues/634
    
    https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1321

 src/st/st-clipboard.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++---
 src/st/st-clipboard.h | 17 +++++++++++++
 2 files changed, 83 insertions(+), 3 deletions(-)
---
diff --git a/src/st/st-clipboard.c b/src/st/st-clipboard.c
index 52ad4941cb..82ea802350 100644
--- a/src/st/st-clipboard.c
+++ b/src/st/st-clipboard.c
@@ -40,7 +40,7 @@ typedef struct _TransferData TransferData;
 struct _TransferData
 {
   StClipboard            *clipboard;
-  StClipboardCallbackFunc callback;
+  GCallback               callback;
   gpointer                user_data;
   GOutputStream          *stream;
 };
@@ -140,12 +140,29 @@ transfer_cb (MetaSelection *selection,
       memcpy (text, g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->stream)), data_size);
     }
 
-  data->callback (data->clipboard, text, data->user_data);
+  ((StClipboardCallbackFunc) data->callback) (data->clipboard, text,
+                                              data->user_data);
   g_object_unref (data->stream);
   g_free (data);
   g_free (text);
 }
 
+static void
+transfer_bytes_cb (MetaSelection *selection,
+                   GAsyncResult  *res,
+                   TransferData  *data)
+{
+  GBytes *bytes = NULL;
+
+  if (meta_selection_transfer_finish (selection, res, NULL))
+    bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (data->stream));
+
+  ((StClipboardContentCallbackFunc) data->callback) (data->clipboard, bytes,
+                                                     data->user_data);
+  g_object_unref (data->stream);
+  g_clear_pointer (&bytes, g_bytes_unref);
+}
+
 /**
  * st_clipboard_get_mimetypes:
  * @clipboard: a #StClipboard
@@ -205,7 +222,7 @@ st_clipboard_get_text (StClipboard            *clipboard,
 
   data = g_new0 (TransferData, 1);
   data->clipboard = clipboard;
-  data->callback = callback;
+  data->callback = G_CALLBACK (callback);
   data->user_data = user_data;
   data->stream = g_memory_output_stream_new_resizable ();
 
@@ -217,6 +234,52 @@ st_clipboard_get_text (StClipboard            *clipboard,
                                  data);
 }
 
+/**
+ * st_clipboard_get_content:
+ * @clipboard: A #StCliboard
+ * @type: The type of clipboard data you want
+ * @mimetype: The mimetype to get content for
+ * @callback: (scope async): function to be called when the type is retrieved
+ * @user_data: data to be passed to the callback
+ *
+ * Request the data from the clipboard in #GBytes form. @callback is executed
+ * when the data is retrieved.
+ *
+ */
+void
+st_clipboard_get_content (StClipboard                    *clipboard,
+                          StClipboardType                 type,
+                          const gchar                    *mimetype,
+                          StClipboardContentCallbackFunc  callback,
+                          gpointer                        user_data)
+{
+  MetaSelectionType selection_type;
+  TransferData *data;
+
+  g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
+  g_return_if_fail (meta_selection != NULL);
+  g_return_if_fail (callback != NULL);
+
+  if (!mimetype || !convert_type (type, &selection_type))
+    {
+      callback (clipboard, NULL, user_data);
+      return;
+    }
+
+  data = g_new0 (TransferData, 1);
+  data->clipboard = clipboard;
+  data->callback = G_CALLBACK (callback);
+  data->user_data = user_data;
+  data->stream = g_memory_output_stream_new_resizable ();
+
+  meta_selection_transfer_async (meta_selection,
+                                 selection_type,
+                                 mimetype, -1,
+                                 data->stream, NULL,
+                                 (GAsyncReadyCallback) transfer_bytes_cb,
+                                 data);
+}
+
 /**
  * st_clipboard_set_content:
  * @clipboard: A #StClipboard
diff --git a/src/st/st-clipboard.h b/src/st/st-clipboard.h
index 22ef63f7fd..022b8323d3 100644
--- a/src/st/st-clipboard.h
+++ b/src/st/st-clipboard.h
@@ -63,6 +63,18 @@ typedef void (*StClipboardCallbackFunc) (StClipboard *clipboard,
                                          const gchar *text,
                                          gpointer     user_data);
 
+/**
+ * StClipboardContentCallbackFunc:
+ * @clipboard: A #StClipboard
+ * @bytes: content from the clipboard
+ * @user_data: user data
+ *
+ * Callback function called when content is retrieved from the clipboard.
+ */
+typedef void (*StClipboardContentCallbackFunc) (StClipboard *clipboard,
+                                                GBytes      *bytes,
+                                                gpointer     user_data);
+
 StClipboard* st_clipboard_get_default (void);
 
 GList * st_clipboard_get_mimetypes (StClipboard             *clipboard,
@@ -80,6 +92,11 @@ void st_clipboard_set_content (StClipboard          *clipboard,
                                StClipboardType       type,
                                const gchar          *mimetype,
                                GBytes               *bytes);
+void st_clipboard_get_content (StClipboard                    *clipboard,
+                               StClipboardType                 type,
+                               const gchar                    *mimetype,
+                               StClipboardContentCallbackFunc  callback,
+                               gpointer                        user_data);
 
 void st_clipboard_set_selection (MetaSelection *selection);
 


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