gdkcontentdeserializer.c 31.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* GTK - The GIMP Toolkit
 * Copyright (C) 2017 Benjamin Otte
 *
 * 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 <gio/gio.h>

#include "gdkcontentdeserializer.h"

#include "gdkcontentformats.h"
25
#include "filetransferportalprivate.h"
26
#include "gdktexture.h"
27
#include "gdkrgbaprivate.h"
28
29
#include "loaders/gdkpngprivate.h"
#include "loaders/gdktiffprivate.h"
30
31
32

#include <gdk-pixbuf/gdk-pixbuf.h>

33
34

/**
Matthias Clasen's avatar
Matthias Clasen committed
35
 * GdkContentDeserializer:
36
 *
Matthias Clasen's avatar
Matthias Clasen committed
37
 * A `GdkContentDeserializer` is used to deserialize content received via
38
 * inter-application data transfers.
Matthias Clasen's avatar
Matthias Clasen committed
39
40
41
42
43
44
45
46
47
 *
 * The `GdkContentDeserializer` transforms serialized content that is
 * identified by a mime type into an object identified by a GType.
 *
 * GTK provides serializers and deserializers for common data types
 * such as text, colors, images or file lists. To register your own
 * deserialization functions, use [func@content_register_deserializer].
 *
 * Also see [class@Gdk.ContentSerializer].
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
typedef struct _Deserializer Deserializer;

struct _Deserializer 
{
  const char *                    mime_type; /* interned */
  GType                           type;
  GdkContentDeserializeFunc       deserialize;
  gpointer                        data;
  GDestroyNotify                  notify;
};

GQueue deserializers = G_QUEUE_INIT;

static void init (void);

#define GDK_CONTENT_DESERIALIZER_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_CONTENT_DESERIALIZER, GdkContentDeserializerClass))
#define GDK_IS_CONTENT_DESERIALIZER_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_CONTENT_DESERIALIZER))
#define GDK_CONTENT_DESERIALIZER_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_CONTENT_DESERIALIZER, GdkContentDeserializerClass))

typedef struct _GdkContentDeserializerClass GdkContentDeserializerClass;

struct _GdkContentDeserializer
{
  GObject parent_instance;

  const char *mime_type; /* interned */
  GValue value;
  GInputStream *stream;
  int priority;
79
  gboolean returned;
80
81
82
83
84
  GCancellable *cancellable;
  gpointer user_data;
  GAsyncReadyCallback callback;
  gpointer callback_data;

85
86
87
  gpointer task_data;
  GDestroyNotify task_notify;

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
  GError *error;
};

struct _GdkContentDeserializerClass
{
  GObjectClass parent_class;
};

static gpointer
gdk_content_deserializer_async_result_get_user_data (GAsyncResult *res)
{
  return GDK_CONTENT_DESERIALIZER (res)->callback_data;
}

static GObject *
gdk_content_deserializer_async_result_get_source_object (GAsyncResult *res)
{
  return NULL;
}

static void
gdk_content_deserializer_async_result_iface_init (GAsyncResultIface *iface)
{
  iface->get_user_data = gdk_content_deserializer_async_result_get_user_data;
  iface->get_source_object = gdk_content_deserializer_async_result_get_source_object;
}

G_DEFINE_TYPE_WITH_CODE (GdkContentDeserializer, gdk_content_deserializer, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_RESULT, gdk_content_deserializer_async_result_iface_init))

static void
gdk_content_deserializer_finalize (GObject *object)
{
  GdkContentDeserializer *deserializer = GDK_CONTENT_DESERIALIZER (object);

  g_value_unset (&deserializer->value);
  g_clear_object (&deserializer->stream);
  g_clear_object (&deserializer->cancellable);
  g_clear_error (&deserializer->error);

128
129
130
  if (deserializer->task_notify)
    deserializer->task_notify (deserializer->task_data);

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
  G_OBJECT_CLASS (gdk_content_deserializer_parent_class)->finalize (object);
}

static void
gdk_content_deserializer_class_init (GdkContentDeserializerClass *content_deserializer_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (content_deserializer_class);

  object_class->finalize = gdk_content_deserializer_finalize;
}

static void
gdk_content_deserializer_init (GdkContentDeserializer *content_deserializer)
{
}

static void
gdk_content_deserializer_run (const char                *mime_type,
                              GType                      type,
                              GInputStream              *stream,
                              int                        priority,
                              GCancellable              *cancellable,
                              GdkContentDeserializeFunc  deserialize_func,
                              gpointer                   user_data,
                              GAsyncReadyCallback        callback,
                              gpointer                   callback_data)
{
  GdkContentDeserializer *deserializer;

  deserializer = g_object_new (GDK_TYPE_CONTENT_DESERIALIZER, NULL);

  deserializer->mime_type = mime_type;
  g_value_init (&deserializer->value, type);
  deserializer->stream = g_object_ref (stream);
  deserializer->priority = priority;
  if (cancellable)
    deserializer->cancellable = g_object_ref (cancellable);
  deserializer->user_data = user_data;
  deserializer->callback = callback;
  deserializer->callback_data = callback_data;

  deserialize_func (deserializer);
}

175
176
/**
 * gdk_content_deserializer_get_mime_type:
Matthias Clasen's avatar
Matthias Clasen committed
177
 * @deserializer: a `GdkContentDeserializer`
178
179
180
181
182
 *
 * Gets the mime type to deserialize from.
 *
 * Returns: (transfer none): the mime type for the current operation
 */
183
184
185
186
187
188
189
190
const char *
gdk_content_deserializer_get_mime_type (GdkContentDeserializer *deserializer)
{
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer), NULL);

  return deserializer->mime_type;
}

191
192
/**
 * gdk_content_deserializer_get_gtype:
Matthias Clasen's avatar
Matthias Clasen committed
193
 * @deserializer: a `GdkContentDeserializer`
194
 *
Matthias Clasen's avatar
Matthias Clasen committed
195
 * Gets the `GType` to create an instance of.
196
 *
Matthias Clasen's avatar
Matthias Clasen committed
197
 * Returns: the `GType` for the current operation
198
 */
199
200
201
202
203
204
205
206
GType
gdk_content_deserializer_get_gtype (GdkContentDeserializer *deserializer)
{
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer), G_TYPE_INVALID);

  return G_VALUE_TYPE (&deserializer->value);
}

207
208
/**
 * gdk_content_deserializer_get_value:
Matthias Clasen's avatar
Matthias Clasen committed
209
 * @deserializer: a `GdkContentDeserializer`
210
 *
Matthias Clasen's avatar
Matthias Clasen committed
211
 * Gets the `GValue` to store the deserialized object in.
212
 *
Matthias Clasen's avatar
Matthias Clasen committed
213
 * Returns: (transfer none): the `GValue` for the current operation
214
 */
215
216
217
218
219
220
221
222
GValue *
gdk_content_deserializer_get_value (GdkContentDeserializer *deserializer)
{
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer), NULL);

  return &deserializer->value;
}

223
224
/**
 * gdk_content_deserializer_get_input_stream:
Matthias Clasen's avatar
Matthias Clasen committed
225
 * @deserializer: a `GdkContentDeserializer`
226
 *
Matthias Clasen's avatar
Matthias Clasen committed
227
228
 * Gets the input stream for the current operation.
 *
Matthias Clasen's avatar
Matthias Clasen committed
229
 * This is the stream that was passed to [func@Gdk.content_deserialize_async].
230
231
232
 *
 * Returns: (transfer none): the input stream for the current operation
 */
233
234
235
236
237
238
239
240
GInputStream *
gdk_content_deserializer_get_input_stream (GdkContentDeserializer *deserializer)
{
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer), NULL);

  return deserializer->stream;
}

241
242
/**
 * gdk_content_deserializer_get_priority:
Matthias Clasen's avatar
Matthias Clasen committed
243
244
245
 * @deserializer: a `GdkContentDeserializer`
 *
 * Gets the I/O priority for the current operation.
246
 *
Matthias Clasen's avatar
Matthias Clasen committed
247
 * This is the priority that was passed to [func@Gdk.content_deserialize_async].
248
 *
Matthias Clasen's avatar
Matthias Clasen committed
249
 * Returns: the I/O priority for the current operation
250
 */
251
252
253
254
255
256
257
258
int
gdk_content_deserializer_get_priority (GdkContentDeserializer *deserializer)
{
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer), G_PRIORITY_DEFAULT);

  return deserializer->priority;
}

259
260
/**
 * gdk_content_deserializer_get_cancellable:
Matthias Clasen's avatar
Matthias Clasen committed
261
262
263
 * @deserializer: a `GdkContentDeserializer`
 *
 * Gets the cancellable for the current operation.
264
 *
Matthias Clasen's avatar
Matthias Clasen committed
265
 * This is the `GCancellable` that was passed to [func@Gdk.content_deserialize_async].
266
267
268
 *
 * Returns: (transfer none): the cancellable for the current operation
 */
269
270
271
272
273
274
275
276
GCancellable *
gdk_content_deserializer_get_cancellable (GdkContentDeserializer *deserializer)
{
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer), NULL);

  return deserializer->cancellable;
}

277
278
/**
 * gdk_content_deserializer_get_user_data:
Matthias Clasen's avatar
Matthias Clasen committed
279
 * @deserializer: a `GdkContentDeserializer`
280
281
282
283
284
 *
 * Gets the user data that was passed when the deserializer was registered.
 *
 * Returns: (transfer none): the user data for this deserializer
 */
285
286
287
288
289
290
291
292
gpointer
gdk_content_deserializer_get_user_data (GdkContentDeserializer *deserializer)
{
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer), NULL);

  return deserializer->user_data;
}

293
294
/**
 * gdk_content_deserializer_set_task_data:
Matthias Clasen's avatar
Matthias Clasen committed
295
 * @deserializer: a `GdkContentDeserializer`
296
297
298
299
300
 * @data: data to associate with this operation
 * @notify: destroy notify for @data
 *
 * Associate data with the current deserialization operation.
 */
301
302
303
304
305
306
307
308
309
310
311
312
313
314
void
gdk_content_deserializer_set_task_data (GdkContentDeserializer *deserializer,
                                        gpointer                data,
                                        GDestroyNotify          notify)
{
  g_return_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer));

  if (deserializer->task_notify)
    deserializer->task_notify (deserializer->task_data);

  deserializer->task_data = data;
  deserializer->task_notify = notify;
}

315
316
/**
 * gdk_content_deserializer_get_task_data:
Matthias Clasen's avatar
Matthias Clasen committed
317
 * @deserializer: a `GdkContentDeserializer`
318
 *
Matthias Clasen's avatar
Matthias Clasen committed
319
320
321
 * Gets the data that was associated with the current operation.
 *
 * See [method@Gdk.ContentDeserializer.set_task_data].
322
323
324
 *
 * Returns: (transfer none): the task data for @deserializer
 */
325
326
327
328
329
330
331
332
gpointer
gdk_content_deserializer_get_task_data (GdkContentDeserializer *deserializer)
{
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer), NULL);

  return deserializer->task_data;
}

333
334
335
336
337
338
339
340
341
342
343
344
345
static gboolean
gdk_content_deserializer_emit_callback (gpointer data)
{
  GdkContentDeserializer *deserializer = data;

  if (deserializer->callback)
    {
      deserializer->callback (NULL, G_ASYNC_RESULT (deserializer), deserializer->callback_data);
    }

  return G_SOURCE_REMOVE;
}

346
347
/**
 * gdk_content_deserializer_return_success:
Matthias Clasen's avatar
Matthias Clasen committed
348
 * @deserializer: a `GdkContentDeserializer`
349
350
351
 *
 * Indicate that the deserialization has been successfully completed.
 */
352
353
354
355
356
357
358
359
360
361
362
363
364
365
void
gdk_content_deserializer_return_success (GdkContentDeserializer *deserializer)
{
  g_return_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer));
  g_return_if_fail (!deserializer->returned);

  deserializer->returned = TRUE;
  g_idle_add_full (deserializer->priority,
                   gdk_content_deserializer_emit_callback,
                   deserializer,
                   g_object_unref);
  /* NB: the idle will destroy our reference */
}

366
367
/**
 * gdk_content_deserializer_return_error:
Matthias Clasen's avatar
Matthias Clasen committed
368
 * @deserializer: a `GdkContentDeserializer`
369
 * @error: (transfer full): a `GError`
370
371
 *
 * Indicate that the deserialization has ended with an error.
Matthias Clasen's avatar
Matthias Clasen committed
372
 *
373
374
 * This function consumes @error.
 */
375
376
377
378
379
380
381
382
383
384
385
386
387
void
gdk_content_deserializer_return_error (GdkContentDeserializer *deserializer,
                                       GError                 *error)
{
  g_return_if_fail (GDK_IS_CONTENT_DESERIALIZER (deserializer));
  g_return_if_fail (!deserializer->returned);
  g_return_if_fail (error != NULL);

  deserializer->error = error;
  /* FIXME: naming */
  gdk_content_deserializer_return_success (deserializer);
}

388
389
390
391
392
393
394
395
/**
 * gdk_content_register_deserializer:
 * @mime_type: the mime type which the function can deserialize from
 * @type: the type of objects that the function creates
 * @deserialize: the callback
 * @data: data that @deserialize can access
 * @notify: destroy notify for @data
 *
Matthias Clasen's avatar
Matthias Clasen committed
396
 * Registers a function to deserialize object of a given type.
397
 */
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
void
gdk_content_register_deserializer (const char                *mime_type,
                                   GType                      type,
                                   GdkContentDeserializeFunc  deserialize,
                                   gpointer                   data,
                                   GDestroyNotify             notify)
{
  Deserializer *deserializer;

  g_return_if_fail (mime_type != NULL);
  g_return_if_fail (deserialize != NULL);

  init ();

  deserializer = g_slice_new0 (Deserializer);

  deserializer->mime_type = g_intern_string (mime_type);
  deserializer->type = type;
  deserializer->deserialize = deserialize;
  deserializer->data = data;
  deserializer->notify = notify;

  g_queue_push_tail (&deserializers, deserializer);
}

static Deserializer *
lookup_deserializer (const char *mime_type,
                     GType       type)
{
  GList *l;

  g_return_val_if_fail (mime_type != NULL, NULL);

  init ();

  mime_type = g_intern_string (mime_type);

  for (l = g_queue_peek_head_link (&deserializers); l; l = l->next)
    {
      Deserializer *deserializer = l->data;

      if (deserializer->mime_type == mime_type &&
          deserializer->type == type)
        return deserializer;
    }
Matthias Clasen's avatar
Matthias Clasen committed
443

444
445
446
  return NULL;
}

447
448
/**
 * gdk_content_formats_union_deserialize_gtypes:
Matthias Clasen's avatar
Matthias Clasen committed
449
 * @formats: (transfer full): a `GdkContentFormats`
450
451
452
453
 *
 * Add GTypes for mime types in @formats for which deserializers are
 * registered.
 *
Matthias Clasen's avatar
Matthias Clasen committed
454
 * Return: a new `GdkContentFormats`
455
 */
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
GdkContentFormats *
gdk_content_formats_union_deserialize_gtypes (GdkContentFormats *formats)
{
  GdkContentFormatsBuilder *builder;
  GList *l;

  g_return_val_if_fail (formats != NULL, NULL);

  init ();

  builder = gdk_content_formats_builder_new ();
  gdk_content_formats_builder_add_formats (builder, formats);

  for (l = g_queue_peek_head_link (&deserializers); l; l = l->next)
    {
      Deserializer *deserializer = l->data;

      if (gdk_content_formats_contain_mime_type (formats, deserializer->mime_type))
        gdk_content_formats_builder_add_gtype (builder, deserializer->type);
    }

  gdk_content_formats_unref (formats);

479
  return gdk_content_formats_builder_free_to_formats (builder);
480
481
}

482
483
/**
 * gdk_content_formats_union_deserialize_mime_types:
Matthias Clasen's avatar
Matthias Clasen committed
484
 * @formats: (transfer full): a `GdkContentFormats`
485
486
487
488
 *
 * Add mime types for GTypes in @formats for which deserializers are
 * registered.
 *
Matthias Clasen's avatar
Matthias Clasen committed
489
 * Return: a new `GdkContentFormats`
490
 */
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
GdkContentFormats *
gdk_content_formats_union_deserialize_mime_types (GdkContentFormats *formats)
{
  GdkContentFormatsBuilder *builder;
  GList *l;

  g_return_val_if_fail (formats != NULL, NULL);

  init ();

  builder = gdk_content_formats_builder_new ();
  gdk_content_formats_builder_add_formats (builder, formats);

  for (l = g_queue_peek_head_link (&deserializers); l; l = l->next)
    {
      Deserializer *deserializer = l->data;

      if (gdk_content_formats_contain_gtype (formats, deserializer->type))
        gdk_content_formats_builder_add_mime_type (builder, deserializer->mime_type);
    }

  gdk_content_formats_unref (formats);

514
  return gdk_content_formats_builder_free_to_formats (builder);
515
516
517
518
519
520
521
522
523
524
525
526
527
}

static void
deserialize_not_found (GdkContentDeserializer *deserializer)
{
  GError *error = g_error_new (G_IO_ERROR,
                               G_IO_ERROR_NOT_FOUND,
                               "Could not convert data from %s to %s",
                               gdk_content_deserializer_get_mime_type (deserializer),
                               g_type_name (gdk_content_deserializer_get_gtype (deserializer)));
  gdk_content_deserializer_return_error (deserializer, error);
}

528
529
/**
 * gdk_content_deserialize_async:
Matthias Clasen's avatar
Matthias Clasen committed
530
 * @stream: a `GInputStream` to read the serialized content from
531
532
 * @mime_type: the mime type to deserialize from
 * @type: the GType to deserialize from
Matthias Clasen's avatar
Matthias Clasen committed
533
534
 * @io_priority: the I/O priority of the operation
 * @cancellable: (nullable): optional `GCancellable` object
535
536
537
538
 * @callback: (scope async): callback to call when the operation is done
 * @user_data: (closure): data to pass to the callback function
 *
 * Read content from the given input stream and deserialize it, asynchronously.
Matthias Clasen's avatar
Matthias Clasen committed
539
540
541
542
543
 *
 * The default I/O priority is %G_PRIORITY_DEFAULT (i.e. 0), and lower numbers
 * indicate a higher priority.
 *
 * When the operation is finished, @callback will be called. You must then
Matthias Clasen's avatar
Matthias Clasen committed
544
 * call [func@Gdk.content_deserialize_finish] to get the result of the operation.
545
 */
546
547
548
549
550
551
552
553
554
555
556
557
void
gdk_content_deserialize_async (GInputStream        *stream,
                               const char          *mime_type,
                               GType                type,
                               int                  io_priority,
                               GCancellable        *cancellable,
                               GAsyncReadyCallback  callback,
                               gpointer             user_data)
{
  Deserializer *deserializer;

  g_return_if_fail (G_IS_INPUT_STREAM (stream));
558
559
  g_return_if_fail (mime_type != NULL);
  g_return_if_fail (G_TYPE_IS_VALUE_TYPE (type));
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
  g_return_if_fail (callback != NULL);

  deserializer = lookup_deserializer (mime_type, type);

  gdk_content_deserializer_run (mime_type,
                                type,
                                stream,
                                io_priority,
                                cancellable,
                                deserializer ? deserializer->deserialize : deserialize_not_found,
                                deserializer ? deserializer->data : NULL,
                                callback,
                                user_data);
}

576
577
/**
 * gdk_content_deserialize_finish:
Matthias Clasen's avatar
Matthias Clasen committed
578
 * @result: the `GAsyncResult`
579
580
581
582
583
 * @value: return location for the result of the operation
 * @error: return location for an error
 *
 * Finishes a content deserialization operation.
 *
Matthias Clasen's avatar
Matthias Clasen committed
584
585
586
 * Returns: %TRUE if the operation was successful. In this case,
 *   @value is set. %FALSE if an error occurred. In this case,
 *   @error is set
587
 */
588
gboolean
589
gdk_content_deserialize_finish (GAsyncResult  *result,
590
                                GValue        *value,
591
592
593
594
                                GError       **error)
{
  GdkContentDeserializer *deserializer;

595
596
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (result), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
597
  deserializer = GDK_CONTENT_DESERIALIZER (result);
598
599
600
601
  if (G_VALUE_TYPE (value) == 0)
    g_value_init (value, G_VALUE_TYPE (&deserializer->value));
  else
    g_return_val_if_fail (G_VALUE_HOLDS (value, G_VALUE_TYPE (&deserializer->value)), FALSE);
602
603
604
605
606

  if (deserializer->error)
    {
      if (error)
        *error = g_error_copy (deserializer->error);
607
      return FALSE;
608
609
    }

610
611
  g_value_copy (&deserializer->value, value);
  return TRUE;
612
613
614
615
616
617
618
619
620
621
}

/*** DESERIALIZERS ***/

static void
pixbuf_deserializer_finish (GObject      *source,
                            GAsyncResult *res,
                            gpointer      deserializer)
{
  GdkPixbuf *pixbuf;
622
  GValue *value;
623
624
625
626
627
628
629
630
631
  GError *error = NULL;

  pixbuf = gdk_pixbuf_new_from_stream_finish (res, &error);
  if (pixbuf == NULL)
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }

632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
  value = gdk_content_deserializer_get_value (deserializer);
  if (G_VALUE_HOLDS (value, GDK_TYPE_PIXBUF))
    {
      g_value_take_object (value, pixbuf);
    }
  else if (G_VALUE_HOLDS (value, GDK_TYPE_TEXTURE))
    {
      GdkTexture *texture;
      texture = gdk_texture_new_for_pixbuf (pixbuf);
      g_object_unref (pixbuf);
      g_value_take_object (value, texture);
    }
  else
    {
      g_assert_not_reached ();
    }
648
649
650
651
652
653
654
655
656
657
658
659
  gdk_content_deserializer_return_success (deserializer);
}

static void
pixbuf_deserializer (GdkContentDeserializer *deserializer)
{
  gdk_pixbuf_new_from_stream_async (gdk_content_deserializer_get_input_stream (deserializer),
				    gdk_content_deserializer_get_cancellable (deserializer),
                                    pixbuf_deserializer_finish,
                                    deserializer);
}

660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
static void
texture_deserializer_finish (GObject      *source,
                             GAsyncResult *result,
                             gpointer      deserializer)
{
  GOutputStream *stream = G_OUTPUT_STREAM (source);
  GBytes *bytes;
  GError *error = NULL;
  GdkTexture *texture = NULL;
  gssize written;

  written = g_output_stream_splice_finish (stream, result, &error);
  if (written < 0)
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }

  bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (stream));

  texture = gdk_texture_new_from_bytes (bytes, &error);
  g_bytes_unref (bytes);
  if (texture == NULL)
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }

  g_value_take_object (gdk_content_deserializer_get_value (deserializer), texture);
  gdk_content_deserializer_return_success (deserializer);
}

static void
texture_deserializer (GdkContentDeserializer *deserializer)
{
  GOutputStream *output;

  output = g_memory_output_stream_new_resizable ();

  g_output_stream_splice_async (output,
                                gdk_content_deserializer_get_input_stream (deserializer),
                                G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE
                                | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
                                gdk_content_deserializer_get_priority (deserializer),
                                gdk_content_deserializer_get_cancellable (deserializer),
                                texture_deserializer_finish,
                                deserializer);
  g_object_unref (output);
}

710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
static void
string_deserializer_finish (GObject      *source,
                            GAsyncResult *result,
                            gpointer      deserializer)
{
  GOutputStream *stream = G_OUTPUT_STREAM (source);
  GError *error = NULL;
  gssize written;

  written = g_output_stream_splice_finish (stream, result, &error);
  if (written < 0)
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }
  else if (written == 0)
    {
      /* Never return NULL, we only return that on error */
      g_value_set_string (gdk_content_deserializer_get_value (deserializer), "");
    }
  else
    {
732
733
734
735
736
737
738
739
740
741
      GOutputStream *mem_stream = g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (stream));

      /* write a terminating NULL byte */
      if (g_output_stream_write (mem_stream, "", 1, NULL, &error) < 0 ||
          !g_output_stream_close (mem_stream, NULL, &error))
        {
          gdk_content_deserializer_return_error (deserializer, error);
          return;
        }

742
      g_value_take_string (gdk_content_deserializer_get_value (deserializer),
743
                           g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (mem_stream)));
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
    }
  gdk_content_deserializer_return_success (deserializer);
}

static void
string_deserializer (GdkContentDeserializer *deserializer)
{
  GOutputStream *output, *filter;
  GCharsetConverter *converter;
  GError *error = NULL;

  converter = g_charset_converter_new ("utf-8",
                                       gdk_content_deserializer_get_user_data (deserializer),
                                       &error);
  if (converter == NULL)
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }
  g_charset_converter_set_use_fallback (converter, TRUE);

  output = g_memory_output_stream_new_resizable ();
  filter = g_converter_output_stream_new (output, G_CONVERTER (converter));
  g_object_unref (output);
  g_object_unref (converter);

  g_output_stream_splice_async (filter,
                                gdk_content_deserializer_get_input_stream (deserializer),
772
                                G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
773
774
775
776
777
778
779
                                gdk_content_deserializer_get_priority (deserializer),
                                gdk_content_deserializer_get_cancellable (deserializer),
                                string_deserializer_finish,
                                deserializer);
  g_object_unref (filter);
}

780
781
782
783
784
785
786
787
static void
file_uri_deserializer_finish (GObject      *source,
                              GAsyncResult *result,
                              gpointer      deserializer)
{
  GOutputStream *stream = G_OUTPUT_STREAM (source);
  GError *error = NULL;
  gssize written;
788
  GValue *value;
789
790
791
792
793
794
795
796
797
798
799
  char *str;
  char **uris;

  written = g_output_stream_splice_finish (stream, result, &error);
  if (written < 0)
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }

  /* write terminating NULL */
800
801
  if (g_output_stream_write (stream, "", 1, NULL, &error) < 0 ||
      !g_output_stream_close (stream, NULL, &error))
802
803
804
805
806
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }

807
  str = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (stream));
808
809
810
  uris = g_uri_list_extract_uris (str);
  g_free (str);

811
812
  value = gdk_content_deserializer_get_value (deserializer);
  if (G_VALUE_HOLDS (value, G_TYPE_FILE))
813
    {
814
815
      if (uris[0] != NULL)
        g_value_take_object (value, g_file_new_for_uri (uris[0]));
816
817
818
    }
  else
    {
819
820
821
822
823
      GSList *l = NULL;
      gsize i;

      for (i = 0; uris[i] != NULL; i++)
        l = g_slist_prepend (l, g_file_new_for_uri (uris[i]));
824

825
      g_value_take_boxed (value, g_slist_reverse (l));
826
827
    }
  g_strfreev (uris);
828
829

  gdk_content_deserializer_return_success (deserializer);
830
831
832
833
834
835
836
837
838
839
840
}

static void
file_uri_deserializer (GdkContentDeserializer *deserializer)
{
  GOutputStream *output;

  output = g_memory_output_stream_new_resizable ();

  g_output_stream_splice_async (output,
                                gdk_content_deserializer_get_input_stream (deserializer),
841
                                G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
842
843
844
845
846
847
848
                                gdk_content_deserializer_get_priority (deserializer),
                                gdk_content_deserializer_get_cancellable (deserializer),
                                file_uri_deserializer_finish,
                                deserializer);
  g_object_unref (output);
}

849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
static void
color_deserializer_finish (GObject      *source,
                           GAsyncResult *result,
                           gpointer      deserializer)
{
  GOutputStream *stream = G_OUTPUT_STREAM (source);
  GError *error = NULL;
  gssize written;

  written = g_output_stream_splice_finish (stream, result, &error);
  if (written < 0)
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }
  else if (written == 0)
    {
      GdkRGBA black = GDK_RGBA ("000");

      /* Never return NULL, we only return that on error */
      g_value_set_boxed (gdk_content_deserializer_get_value (deserializer), &black);
    }
  else
    {
      guint16 *data;
      GdkRGBA rgba;

      data = (guint16 *)g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (stream));
      rgba.red = data[0] / 65535.0;
      rgba.green = data[1] / 65535.0;
      rgba.blue = data[2] / 65535.0;
      rgba.alpha = data[3] / 65535.0;

      g_value_set_boxed (gdk_content_deserializer_get_value (deserializer), &rgba);
    }
  gdk_content_deserializer_return_success (deserializer);
}

static void
color_deserializer (GdkContentDeserializer *deserializer)
{
  GOutputStream *output;
  guint16 *data;

  data = g_new0 (guint16, 4);
  output = g_memory_output_stream_new (data, 4 * sizeof (guint16), NULL, g_free);

  g_output_stream_splice_async (output,
                                gdk_content_deserializer_get_input_stream (deserializer),
                                G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
                                gdk_content_deserializer_get_priority (deserializer),
                                gdk_content_deserializer_get_cancellable (deserializer),
                                color_deserializer_finish,
                                deserializer);
  g_object_unref (output);
}

906
907
908
909
910
static void
init (void)
{
  static gboolean initialized = FALSE;
  GSList *formats, *f;
911
  const char *charset;
912
913
914
915
916
917

  if (initialized)
    return;

  initialized = TRUE;

918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
  gdk_content_register_deserializer ("image/png",
                                     GDK_TYPE_TEXTURE,
                                     texture_deserializer,
                                     NULL,
                                     NULL);
  gdk_content_register_deserializer ("image/tiff",
                                     GDK_TYPE_TEXTURE,
                                     texture_deserializer,
                                     NULL,
                                     NULL);
  gdk_content_register_deserializer ("image/jpeg",
                                     GDK_TYPE_TEXTURE,
                                     texture_deserializer,
                                     NULL,
                                     NULL);


935
936
937
938
939
940
  formats = gdk_pixbuf_get_formats ();

  /* Make sure png comes first */
  for (f = formats; f; f = f->next)
    {
      GdkPixbufFormat *fmt = f->data;
941
942
      char *name;

943
944
      name = gdk_pixbuf_format_get_name (fmt);
      if (g_str_equal (name, "png"))
945
946
947
        {
          formats = g_slist_delete_link (formats, f);
          formats = g_slist_prepend (formats, fmt);
948

949
950
951
          g_free (name);
          break;
        }
952
953

      g_free (name);
954
    }
955
956
957
958

  for (f = formats; f; f = f->next)
    {
      GdkPixbufFormat *fmt = f->data;
Benjamin Otte's avatar
Benjamin Otte committed
959
      char **mimes, **m;
960
      char *name;
961

962
      name = gdk_pixbuf_format_get_name (fmt);
963
964
      mimes = gdk_pixbuf_format_get_mime_types (fmt);
      for (m = mimes; *m; m++)
965
966
967
968
969
970
971
972
973
974
        {
          /* Turning pngs, jpegs and tiffs into textures is handled above */
          if (!g_str_equal (name, "png") &&
              !g_str_equal (name, "jpeg") &&
              !g_str_equal (name, "tiff"))
            gdk_content_register_deserializer (*m,
                                               GDK_TYPE_TEXTURE,
                                               pixbuf_deserializer,
                                               NULL,
                                               NULL);
975
976
977
978
979
          gdk_content_register_deserializer (*m,
                                             GDK_TYPE_PIXBUF,
                                             pixbuf_deserializer,
                                             NULL,
                                             NULL);
980
        }
981
      g_strfreev (mimes);
982
      g_free (name);
983
984
985
    }

  g_slist_free (formats);
986

987
#if defined(G_OS_UNIX) && !defined(__APPLE__)
988
  file_transfer_portal_register ();
989
990
#endif

991
992
993
994
995
  gdk_content_register_deserializer ("text/uri-list",
                                     GDK_TYPE_FILE_LIST,
                                     file_uri_deserializer,
                                     NULL,
                                     NULL);
996

997
998
999
1000
1001
1002
  gdk_content_register_deserializer ("text/uri-list",
                                     G_TYPE_FILE,
                                     file_uri_deserializer,
                                     NULL,
                                     NULL);

1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
  gdk_content_register_deserializer ("text/plain;charset=utf-8",
                                     G_TYPE_STRING,
                                     string_deserializer,
                                     (gpointer) "utf-8",
                                     NULL);
  if (!g_get_charset (&charset))
    {
      char *mime = g_strdup_printf ("text/plain;charset=%s", charset);
      gdk_content_register_deserializer (mime,
                                         G_TYPE_STRING,
                                         string_deserializer,
1014
                                         (gpointer) charset,
1015
1016
1017
1018
1019
1020
1021
                                         g_free);
    }
  gdk_content_register_deserializer ("text/plain",
                                     G_TYPE_STRING,
                                     string_deserializer,
                                     (gpointer) "ASCII",
                                     NULL);
1022
1023
1024
1025
1026
1027

  gdk_content_register_deserializer ("application/x-color",
                                     GDK_TYPE_RGBA,
                                     color_deserializer,
                                     NULL,
                                     NULL);
1028
1029
}