2017-11-24 05:17:37 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Benjamin Otte <otte@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gdkcontentproviderprivate.h"
|
|
|
|
|
|
|
|
#include "gdkclipboard.h"
|
|
|
|
#include "gdkcontentformats.h"
|
|
|
|
#include "gdkintl.h"
|
2022-08-25 18:33:30 +00:00
|
|
|
#include "gdk-private.h"
|
2017-11-24 05:17:37 +00:00
|
|
|
|
2017-12-04 23:58:30 +00:00
|
|
|
/**
|
2021-02-21 05:13:57 +00:00
|
|
|
* GdkContentProvider:
|
2017-12-04 23:58:30 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* A `GdkContentProvider` is used to provide content for the clipboard or
|
|
|
|
* for drag-and-drop operations in a number of formats.
|
2017-12-05 00:18:24 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* To create a `GdkContentProvider`, use [ctor@Gdk.ContentProvider.new_for_value]
|
|
|
|
* or [ctor@Gdk.ContentProvider.new_for_bytes].
|
2017-12-05 00:18:24 +00:00
|
|
|
*
|
|
|
|
* GDK knows how to handle common text and image formats out-of-the-box. See
|
2021-02-21 05:13:57 +00:00
|
|
|
* [class@Gdk.ContentSerializer] and [class@Gdk.ContentDeserializer] if you want
|
|
|
|
* to add support for application-specific data formats.
|
2017-12-04 23:58:30 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-24 05:17:37 +00:00
|
|
|
typedef struct _GdkContentProviderPrivate GdkContentProviderPrivate;
|
|
|
|
|
|
|
|
struct _GdkContentProviderPrivate
|
|
|
|
{
|
|
|
|
GdkContentFormats *formats;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_FORMATS,
|
2017-11-29 09:07:52 +00:00
|
|
|
PROP_STORABLE_FORMATS,
|
2017-11-24 05:17:37 +00:00
|
|
|
N_PROPERTIES
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CONTENT_CHANGED,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *properties[N_PROPERTIES] = { NULL, };
|
|
|
|
static guint signals[N_SIGNALS] = { 0 };
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GdkContentProvider, gdk_content_provider, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_content_provider_real_attach_clipboard (GdkContentProvider *provider,
|
|
|
|
GdkClipboard *clipboard)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_content_provider_real_detach_clipboard (GdkContentProvider *provider,
|
|
|
|
GdkClipboard *clipboard)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static GdkContentFormats *
|
|
|
|
gdk_content_provider_real_ref_formats (GdkContentProvider *provider)
|
|
|
|
{
|
|
|
|
return gdk_content_formats_new (NULL, 0);
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:07:52 +00:00
|
|
|
static GdkContentFormats *
|
|
|
|
gdk_content_provider_real_ref_storable_formats (GdkContentProvider *provider)
|
|
|
|
{
|
|
|
|
return gdk_content_provider_ref_formats (provider);
|
|
|
|
}
|
|
|
|
|
2017-11-24 05:17:37 +00:00
|
|
|
static void
|
|
|
|
gdk_content_provider_real_write_mime_type_async (GdkContentProvider *provider,
|
|
|
|
const char *mime_type,
|
|
|
|
GOutputStream *stream,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GTask *task;
|
|
|
|
|
|
|
|
task = g_task_new (provider, cancellable, callback, user_data);
|
|
|
|
g_task_set_priority (task, io_priority);
|
|
|
|
g_task_set_source_tag (task, gdk_content_provider_real_write_mime_type_async);
|
|
|
|
|
|
|
|
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
2017-12-03 17:38:39 +00:00
|
|
|
_("Cannot provide contents as “%s”"), mime_type);
|
2017-11-24 05:17:37 +00:00
|
|
|
g_object_unref (task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gdk_content_provider_real_write_mime_type_finish (GdkContentProvider *provider,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (g_task_is_valid (result, provider), FALSE);
|
|
|
|
g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) == gdk_content_provider_real_write_mime_type_async, FALSE);
|
|
|
|
|
|
|
|
return g_task_propagate_boolean (G_TASK (result), error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gdk_content_provider_real_get_value (GdkContentProvider *provider,
|
|
|
|
GValue *value,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
|
|
|
|
_("Cannot provide contents as %s"), G_VALUE_TYPE_NAME (value));
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_content_provider_get_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GdkContentProvider *provider = GDK_CONTENT_PROVIDER (gobject);
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_FORMATS:
|
|
|
|
g_value_take_boxed (value, gdk_content_provider_ref_formats (provider));
|
|
|
|
break;
|
|
|
|
|
2017-11-29 09:07:52 +00:00
|
|
|
case PROP_STORABLE_FORMATS:
|
|
|
|
g_value_take_boxed (value, gdk_content_provider_ref_storable_formats (provider));
|
|
|
|
break;
|
|
|
|
|
2017-11-24 05:17:37 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_content_provider_class_init (GdkContentProviderClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
object_class->get_property = gdk_content_provider_get_property;
|
|
|
|
|
|
|
|
class->attach_clipboard = gdk_content_provider_real_attach_clipboard;
|
|
|
|
class->detach_clipboard = gdk_content_provider_real_detach_clipboard;
|
|
|
|
class->ref_formats = gdk_content_provider_real_ref_formats;
|
2017-11-29 09:07:52 +00:00
|
|
|
class->ref_storable_formats = gdk_content_provider_real_ref_storable_formats;
|
2017-11-24 05:17:37 +00:00
|
|
|
class->write_mime_type_async = gdk_content_provider_real_write_mime_type_async;
|
|
|
|
class->write_mime_type_finish = gdk_content_provider_real_write_mime_type_finish;
|
|
|
|
class->get_value = gdk_content_provider_real_get_value;
|
|
|
|
|
|
|
|
/**
|
2021-02-24 14:11:23 +00:00
|
|
|
* GdkContentProvider:formats: (attributes org.gtk.Property.get=gdk_content_provider_ref_formats)
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
|
|
|
* The possible formats that the provider can provide its data in.
|
|
|
|
*/
|
|
|
|
properties[PROP_FORMATS] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_boxed ("formats", NULL, NULL,
|
2017-11-24 05:17:37 +00:00
|
|
|
GDK_TYPE_CONTENT_FORMATS,
|
|
|
|
G_PARAM_READABLE |
|
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
2017-11-29 09:07:52 +00:00
|
|
|
/**
|
2021-02-24 14:11:23 +00:00
|
|
|
* GdkContentProvider:storable-formats: (attributes org.gtk.Property.get=gdk_content_provider_ref_storable_formats)
|
2017-11-29 09:07:52 +00:00
|
|
|
*
|
|
|
|
* The subset of formats that clipboard managers should store this provider's data in.
|
|
|
|
*/
|
|
|
|
properties[PROP_STORABLE_FORMATS] =
|
2022-05-11 12:19:39 +00:00
|
|
|
g_param_spec_boxed ("storable-formats", NULL, NULL,
|
2017-11-29 09:07:52 +00:00
|
|
|
GDK_TYPE_CONTENT_FORMATS,
|
|
|
|
G_PARAM_READABLE |
|
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY);
|
|
|
|
|
2017-11-24 05:17:37 +00:00
|
|
|
/**
|
2020-02-09 00:27:32 +00:00
|
|
|
* GdkContentProvider::content-changed:
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
|
|
|
* Emitted whenever the content provided by this provider has changed.
|
|
|
|
*/
|
|
|
|
signals[CONTENT_CHANGED] =
|
2022-08-25 18:33:30 +00:00
|
|
|
g_signal_new (I_("content-changed"),
|
2017-11-24 05:17:37 +00:00
|
|
|
G_TYPE_FROM_CLASS (class),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GdkContentProviderClass, content_changed),
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
G_TYPE_NONE, 0);
|
|
|
|
|
|
|
|
g_object_class_install_properties (object_class, N_PROPERTIES, properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_content_provider_init (GdkContentProvider *provider)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-24 14:11:23 +00:00
|
|
|
* gdk_content_provider_ref_formats: (attributes org.gtk.Method.get_property=formats)
|
2021-05-20 03:39:18 +00:00
|
|
|
* @provider: a `GdkContentProvider`
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
|
|
|
* Gets the formats that the provider can provide its current contents in.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): The formats of the provider
|
2021-02-21 05:13:57 +00:00
|
|
|
*/
|
2017-11-24 05:17:37 +00:00
|
|
|
GdkContentFormats *
|
|
|
|
gdk_content_provider_ref_formats (GdkContentProvider *provider)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GDK_IS_CONTENT_PROVIDER (provider), NULL);
|
|
|
|
|
|
|
|
return GDK_CONTENT_PROVIDER_GET_CLASS (provider)->ref_formats (provider);
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:07:52 +00:00
|
|
|
/**
|
2021-02-24 14:11:23 +00:00
|
|
|
* gdk_content_provider_ref_storable_formats: (attributes org.gtk.Method.get_property=storable-formats)
|
2021-05-20 03:39:18 +00:00
|
|
|
* @provider: a `GdkContentProvider`
|
2017-11-29 09:07:52 +00:00
|
|
|
*
|
|
|
|
* Gets the formats that the provider suggests other applications to store
|
2021-02-21 05:13:57 +00:00
|
|
|
* the data in.
|
|
|
|
*
|
2017-11-29 09:07:52 +00:00
|
|
|
* An example of such an application would be a clipboard manager.
|
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* This can be assumed to be a subset of [method@Gdk.ContentProvider.ref_formats].
|
2017-11-29 09:07:52 +00:00
|
|
|
*
|
|
|
|
* Returns: (transfer full): The storable formats of the provider
|
2021-02-21 05:13:57 +00:00
|
|
|
*/
|
2017-11-29 09:07:52 +00:00
|
|
|
GdkContentFormats *
|
|
|
|
gdk_content_provider_ref_storable_formats (GdkContentProvider *provider)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GDK_IS_CONTENT_PROVIDER (provider), NULL);
|
|
|
|
|
|
|
|
return GDK_CONTENT_PROVIDER_GET_CLASS (provider)->ref_storable_formats (provider);
|
|
|
|
}
|
|
|
|
|
2017-12-05 00:08:32 +00:00
|
|
|
/**
|
2017-12-05 00:18:24 +00:00
|
|
|
* gdk_content_provider_content_changed:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @provider: a `GdkContentProvider`
|
2017-12-05 00:08:32 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* Emits the ::content-changed signal.
|
2017-12-05 00:08:32 +00:00
|
|
|
*/
|
2017-11-24 05:17:37 +00:00
|
|
|
void
|
|
|
|
gdk_content_provider_content_changed (GdkContentProvider *provider)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GDK_IS_CONTENT_PROVIDER (provider));
|
|
|
|
|
|
|
|
g_signal_emit (provider, signals[CONTENT_CHANGED], 0);
|
|
|
|
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (provider), properties[PROP_FORMATS]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_content_provider_write_mime_type_async:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @provider: a `GdkContentProvider`
|
2017-12-05 07:52:48 +00:00
|
|
|
* @mime_type: the mime type to provide the data in
|
2021-02-21 05:13:57 +00:00
|
|
|
* @stream: the `GOutputStream` to write to
|
|
|
|
* @io_priority: I/O priority of the request.
|
|
|
|
* @cancellable: (nullable): optional `GCancellable` object, %NULL to ignore.
|
2017-11-24 05:17:37 +00:00
|
|
|
* @callback: (scope async): callback to call when the request is satisfied
|
|
|
|
* @user_data: (closure): the data to pass to callback function
|
|
|
|
*
|
|
|
|
* Asynchronously writes the contents of @provider to @stream in the given
|
2021-02-21 05:13:57 +00:00
|
|
|
* @mime_type.
|
|
|
|
*
|
|
|
|
* When the operation is finished @callback will be called. You must then call
|
|
|
|
* [method@Gdk.ContentProvider.write_mime_type_finish] to get the result
|
|
|
|
* of the operation.
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
|
|
|
* The given mime type does not need to be listed in the formats returned by
|
2021-02-21 05:13:57 +00:00
|
|
|
* [method@Gdk.ContentProvider.ref_formats]. However, if the given `GType` is
|
2021-05-20 03:39:18 +00:00
|
|
|
* not supported, `G_IO_ERROR_NOT_SUPPORTED` will be reported.
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
|
|
|
* The given @stream will not be closed.
|
2021-02-21 05:13:57 +00:00
|
|
|
*/
|
2017-11-24 05:17:37 +00:00
|
|
|
void
|
|
|
|
gdk_content_provider_write_mime_type_async (GdkContentProvider *provider,
|
|
|
|
const char *mime_type,
|
|
|
|
GOutputStream *stream,
|
|
|
|
int io_priority,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GDK_IS_CONTENT_PROVIDER (provider));
|
|
|
|
g_return_if_fail (mime_type != NULL);
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
|
|
|
|
|
|
|
|
GDK_CONTENT_PROVIDER_GET_CLASS (provider)->write_mime_type_async (provider,
|
|
|
|
g_intern_string (mime_type),
|
|
|
|
stream,
|
|
|
|
io_priority,
|
|
|
|
cancellable,
|
|
|
|
callback,
|
|
|
|
user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_content_provider_write_mime_type_finish:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @provider: a `GdkContentProvider`
|
|
|
|
* @result: a `GAsyncResult`
|
2021-05-21 00:45:06 +00:00
|
|
|
* @error: a `GError` location to store the error occurring
|
2021-02-21 05:13:57 +00:00
|
|
|
*
|
|
|
|
* Finishes an asynchronous write operation.
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* See [method@Gdk.ContentProvider.write_mime_type_async].
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
|
|
|
* Returns: %TRUE if the operation was completed successfully. Otherwise
|
2021-05-18 21:05:26 +00:00
|
|
|
* @error will be set to describe the failure.
|
2021-02-21 05:13:57 +00:00
|
|
|
*/
|
2017-11-24 05:17:37 +00:00
|
|
|
gboolean
|
|
|
|
gdk_content_provider_write_mime_type_finish (GdkContentProvider *provider,
|
|
|
|
GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GDK_IS_CONTENT_PROVIDER (provider), FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
return GDK_CONTENT_PROVIDER_GET_CLASS (provider)->write_mime_type_finish (provider, result, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_content_provider_get_value:
|
2021-02-21 05:13:57 +00:00
|
|
|
* @provider: a `GdkContentProvider`
|
2021-12-25 13:29:07 +00:00
|
|
|
* @value: (out caller-allocates): the `GValue` to fill
|
2021-05-21 00:45:06 +00:00
|
|
|
* @error: a `GError` location to store the error occurring
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
2020-01-01 17:10:02 +00:00
|
|
|
* Gets the contents of @provider stored in @value.
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
2021-02-21 05:13:57 +00:00
|
|
|
* The @value will have been initialized to the `GType` the value should be
|
|
|
|
* provided in. This given `GType` does not need to be listed in the formats
|
|
|
|
* returned by [method@Gdk.ContentProvider.ref_formats]. However, if the
|
|
|
|
* given `GType` is not supported, this operation can fail and
|
2021-05-20 03:39:18 +00:00
|
|
|
* `G_IO_ERROR_NOT_SUPPORTED` will be reported.
|
2017-11-24 05:17:37 +00:00
|
|
|
*
|
|
|
|
* Returns: %TRUE if the value was set successfully. Otherwise
|
2021-05-18 21:05:26 +00:00
|
|
|
* @error will be set to describe the failure.
|
2021-02-21 05:13:57 +00:00
|
|
|
*/
|
2017-11-24 05:17:37 +00:00
|
|
|
gboolean
|
|
|
|
gdk_content_provider_get_value (GdkContentProvider *provider,
|
|
|
|
GValue *value,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GDK_IS_CONTENT_PROVIDER (provider), FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_VALUE (value), FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
return GDK_CONTENT_PROVIDER_GET_CLASS (provider)->get_value (provider,
|
|
|
|
value,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gdk_content_provider_attach_clipboard (GdkContentProvider *provider,
|
|
|
|
GdkClipboard *clipboard)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GDK_IS_CONTENT_PROVIDER (provider));
|
|
|
|
g_return_if_fail (GDK_IS_CLIPBOARD (clipboard));
|
|
|
|
|
2021-04-16 03:22:27 +00:00
|
|
|
GDK_CONTENT_PROVIDER_GET_CLASS (provider)->attach_clipboard (provider, clipboard);
|
2017-11-24 05:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gdk_content_provider_detach_clipboard (GdkContentProvider *provider,
|
|
|
|
GdkClipboard *clipboard)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GDK_IS_CONTENT_PROVIDER (provider));
|
|
|
|
g_return_if_fail (GDK_IS_CLIPBOARD (clipboard));
|
|
|
|
|
2021-04-16 03:22:27 +00:00
|
|
|
GDK_CONTENT_PROVIDER_GET_CLASS (provider)->detach_clipboard (provider, clipboard);
|
2017-11-24 05:17:37 +00:00
|
|
|
}
|