summaryrefslogtreecommitdiff
path: root/gdk/gdkcontentprovider.c
blob: e4c6afda451361c9d6ccb0385fdd3ee34ac8c201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* 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"

/**
 * SECTION:gdkcontentprovider
 * @Short_description: Provides content for data transfer between applications
 * @Title: GdkContentProvider
 * @See_also: #GdkContentSerializer, #GdkContentDeserializer
 *
 * A GdkContentProvider is used to provide content for the clipboard in
 * a number of formats.
 *
 * To create a GdkContentProvider, use gdk_content_provider_new_for_value() or
 * gdk_content_provider_new_for_bytes().
 *
 * GDK knows how to handle common text and image formats out-of-the-box. See
 * #GdkContentSerializer and #GdkContentDeserializer if you want to add support
 * for application-specific data formats.
 */

typedef struct _GdkContentProviderPrivate GdkContentProviderPrivate;

struct _GdkContentProviderPrivate
{
  GdkContentFormats *formats;
};

enum {
  PROP_0,
  PROP_FORMATS,
  PROP_STORABLE_FORMATS,
  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);
}

static GdkContentFormats *
gdk_content_provider_real_ref_storable_formats (GdkContentProvider *provider)
{
  return gdk_content_provider_ref_formats (provider);
}

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,
                           _("Cannot provide contents as ā€œ%sā€"), mime_type);
  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;

    case PROP_STORABLE_FORMATS:
      g_value_take_boxed (value, gdk_content_provider_ref_storable_formats (provider));
      break;

    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;
  class->ref_storable_formats = gdk_content_provider_real_ref_storable_formats;
  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;

  /**
   * GdkContentProvider:formats:
   *
   * The possible formats that the provider can provide its data in.
   */
  properties[PROP_FORMATS] =
    g_param_spec_boxed ("formats",
                        "Formats",
                        "The possible formats for data",
                        GDK_TYPE_CONTENT_FORMATS,
                        G_PARAM_READABLE |
                        G_PARAM_STATIC_STRINGS |
                        G_PARAM_EXPLICIT_NOTIFY);

  /**
   * GdkContentProvider:storable-formats:
   *
   * The subset of formats that clipboard managers should store this provider's data in.
   */
  properties[PROP_STORABLE_FORMATS] =
    g_param_spec_boxed ("storable-formats",
                        "Storable formats",
                        "The formats that data should be stored in",
                        GDK_TYPE_CONTENT_FORMATS,
                        G_PARAM_READABLE |
                        G_PARAM_STATIC_STRINGS |
                        G_PARAM_EXPLICIT_NOTIFY);

  /**
   * GdkContentProvider::content-changed:
   *
   * Emitted whenever the content provided by this provider has changed.
   */
  signals[CONTENT_CHANGED] =
    g_signal_new ("content-changed",
                  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)
{
}

/**
 * gdk_content_provider_ref_formats:
 * @provider: a #GdkContentProvider
 *
 * Gets the formats that the provider can provide its current contents in.
 *
 * Returns: (transfer full): The formats of the provider
 **/
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);
}

/**
 * gdk_content_provider_ref_storable_formats:
 * @provider: a #GdkContentProvider
 *
 * Gets the formats that the provider suggests other applications to store
 * the data in.  
 * An example of such an application would be a clipboard manager.
 *
 * This can be assumed to be a subset of gdk_content_provider_ref_formats().
 *
 * Returns: (transfer full): The storable formats of the provider
 **/
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);
}

/**
 * gdk_content_provider_content_changed:
 * @provider: a #GdkContentProvider
 *
 * Emits the #GdkContentProvider::contents-changed signal.
 */
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:
 * @provider: a #GdkContentProvider
 * @mime_type: the mime type to provide the data in
 * @stream: the #GOutputStream to write to
 * @io_priority: the [I/O priority][io-priority]
 * of the request. 
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
 * @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
 * @mime_type. When the operation is finished @callback will be called. You
 * can then call gdk_content_provider_write_mime_type_finish() to get the 
 * result of the operation.
 *
 * The given mime type does not need to be listed in the formats returned by
 * gdk_content_provider_ref_formats(). However, if the given #GType is not
 * supported, #G_IO_ERROR_NOT_SUPPORTED will be reported.
 *
 * The given @stream will not be closed.
 **/
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:
 * @provider: a #GdkContentProvider
 * @result: a #GAsyncResult
 * @error: a #GError location to store the error occurring, or %NULL to 
 *     ignore.
 *
 * Finishes an asynchronous write operation started with
 * gdk_content_provider_write_mime_type_async().
 *
 * Returns: %TRUE if the operation was completed successfully. Otherwise
 *     @error will be set to describe the failure.
 **/
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:
 * @provider: a #GdkContentProvider
 * @value: the #GValue to fill
 * @error: a #GError location to store the error occurring, or %NULL to 
 *     ignore.
 *
 * Gets the contents of @provider stored in @value.
 *
 * 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 gdk_content_provider_ref_formats(). However, if the given
 * #GType is not supported, this operation can fail and
 * #G_IO_ERROR_NOT_SUPPORTED will be reported.
 *
 * Returns: %TRUE if the value was set successfully. Otherwise
 *     @error will be set to describe the failure.
 **/
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));

  return GDK_CONTENT_PROVIDER_GET_CLASS (provider)->attach_clipboard (provider, clipboard);
}

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));

  return GDK_CONTENT_PROVIDER_GET_CLASS (provider)->detach_clipboard (provider, clipboard);
}