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
This commit is contained in:
Carlos Garnacho 2020-06-19 13:27:04 +02:00
parent aa70020bc8
commit ec36762309
2 changed files with 83 additions and 3 deletions

View File

@ -40,7 +40,7 @@ typedef struct _TransferData TransferData;
struct _TransferData struct _TransferData
{ {
StClipboard *clipboard; StClipboard *clipboard;
StClipboardCallbackFunc callback; GCallback callback;
gpointer user_data; gpointer user_data;
GOutputStream *stream; 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); 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_object_unref (data->stream);
g_free (data); g_free (data);
g_free (text); 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: * st_clipboard_get_mimetypes:
* @clipboard: a #StClipboard * @clipboard: a #StClipboard
@ -205,7 +222,7 @@ st_clipboard_get_text (StClipboard *clipboard,
data = g_new0 (TransferData, 1); data = g_new0 (TransferData, 1);
data->clipboard = clipboard; data->clipboard = clipboard;
data->callback = callback; data->callback = G_CALLBACK (callback);
data->user_data = user_data; data->user_data = user_data;
data->stream = g_memory_output_stream_new_resizable (); data->stream = g_memory_output_stream_new_resizable ();
@ -217,6 +234,52 @@ st_clipboard_get_text (StClipboard *clipboard,
data); 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: * st_clipboard_set_content:
* @clipboard: A #StClipboard * @clipboard: A #StClipboard

View File

@ -63,6 +63,18 @@ typedef void (*StClipboardCallbackFunc) (StClipboard *clipboard,
const gchar *text, const gchar *text,
gpointer user_data); 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); StClipboard* st_clipboard_get_default (void);
GList * st_clipboard_get_mimetypes (StClipboard *clipboard, GList * st_clipboard_get_mimetypes (StClipboard *clipboard,
@ -80,6 +92,11 @@ void st_clipboard_set_content (StClipboard *clipboard,
StClipboardType type, StClipboardType type,
const gchar *mimetype, const gchar *mimetype,
GBytes *bytes); 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); void st_clipboard_set_selection (MetaSelection *selection);