gdkcontentdeserializer.c 28.8 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
30

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

31
32

/**
Matthias Clasen's avatar
Matthias Clasen committed
33
 * GdkContentDeserializer:
34
 *
Matthias Clasen's avatar
Matthias Clasen committed
35
 * A `GdkContentDeserializer` is used to deserialize content received via
36
 * inter-application data transfers.
Matthias Clasen's avatar
Matthias Clasen committed
37
38
39
40
41
42
43
44
45
 *
 * 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].
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
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;
77
  gboolean returned;
78
79
80
81
82
  GCancellable *cancellable;
  gpointer user_data;
  GAsyncReadyCallback callback;
  gpointer callback_data;

83
84
85
  gpointer task_data;
  GDestroyNotify task_notify;

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

126
127
128
  if (deserializer->task_notify)
    deserializer->task_notify (deserializer->task_data);

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

173
174
/**
 * gdk_content_deserializer_get_mime_type:
Matthias Clasen's avatar
Matthias Clasen committed
175
 * @deserializer: a `GdkContentDeserializer`
176
177
178
179
180
 *
 * Gets the mime type to deserialize from.
 *
 * Returns: (transfer none): the mime type for the current operation
 */
181
182
183
184
185
186
187
188
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;
}

189
190
/**
 * gdk_content_deserializer_get_gtype:
Matthias Clasen's avatar
Matthias Clasen committed
191
 * @deserializer: a `GdkContentDeserializer`
192
 *
Matthias Clasen's avatar
Matthias Clasen committed
193
 * Gets the `GType` to create an instance of.
194
 *
Matthias Clasen's avatar
Matthias Clasen committed
195
 * Returns: the `GType` for the current operation
196
 */
197
198
199
200
201
202
203
204
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);
}

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

  return &deserializer->value;
}

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

  return deserializer->stream;
}

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

  return deserializer->priority;
}

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

  return deserializer->cancellable;
}

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

  return deserializer->user_data;
}

291
292
/**
 * gdk_content_deserializer_set_task_data:
Matthias Clasen's avatar
Matthias Clasen committed
293
 * @deserializer: a `GdkContentDeserializer`
294
295
296
297
298
 * @data: data to associate with this operation
 * @notify: destroy notify for @data
 *
 * Associate data with the current deserialization operation.
 */
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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;
}

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

  return deserializer->task_data;
}

331
332
333
334
335
336
337
338
339
340
341
342
343
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;
}

344
345
/**
 * gdk_content_deserializer_return_success:
Matthias Clasen's avatar
Matthias Clasen committed
346
 * @deserializer: a `GdkContentDeserializer`
347
348
349
 *
 * Indicate that the deserialization has been successfully completed.
 */
350
351
352
353
354
355
356
357
358
359
360
361
362
363
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 */
}

364
365
/**
 * gdk_content_deserializer_return_error:
Matthias Clasen's avatar
Matthias Clasen committed
366
 * @deserializer: a `GdkContentDeserializer`
367
 * @error: (transfer full): a `GError`
368
369
 *
 * Indicate that the deserialization has ended with an error.
Matthias Clasen's avatar
Matthias Clasen committed
370
 *
371
372
 * This function consumes @error.
 */
373
374
375
376
377
378
379
380
381
382
383
384
385
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);
}

386
387
388
389
390
391
392
393
/**
 * 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
394
 * Registers a function to deserialize object of a given type.
395
 */
396
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
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
441

442
443
444
  return NULL;
}

445
446
/**
 * gdk_content_formats_union_deserialize_gtypes:
Matthias Clasen's avatar
Matthias Clasen committed
447
 * @formats: (transfer full): a `GdkContentFormats`
448
449
450
451
 *
 * Add GTypes for mime types in @formats for which deserializers are
 * registered.
 *
Matthias Clasen's avatar
Matthias Clasen committed
452
 * Return: a new `GdkContentFormats`
453
 */
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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);

477
  return gdk_content_formats_builder_free_to_formats (builder);
478
479
}

480
481
/**
 * gdk_content_formats_union_deserialize_mime_types:
Matthias Clasen's avatar
Matthias Clasen committed
482
 * @formats: (transfer full): a `GdkContentFormats`
483
484
485
486
 *
 * Add mime types for GTypes in @formats for which deserializers are
 * registered.
 *
Matthias Clasen's avatar
Matthias Clasen committed
487
 * Return: a new `GdkContentFormats`
488
 */
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
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);

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

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

526
527
/**
 * gdk_content_deserialize_async:
Matthias Clasen's avatar
Matthias Clasen committed
528
 * @stream: a `GInputStream` to read the serialized content from
529
530
 * @mime_type: the mime type to deserialize from
 * @type: the GType to deserialize from
Matthias Clasen's avatar
Matthias Clasen committed
531
532
 * @io_priority: the I/O priority of the operation
 * @cancellable: (nullable): optional `GCancellable` object
533
534
535
536
 * @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
537
538
539
540
541
 *
 * 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
542
 * call [func@Gdk.content_deserialize_finish] to get the result of the operation.
543
 */
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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));
  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);
}

572
573
/**
 * gdk_content_deserialize_finish:
Matthias Clasen's avatar
Matthias Clasen committed
574
 * @result: the `GAsyncResult`
575
576
577
578
579
 * @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
580
581
582
 * Returns: %TRUE if the operation was successful. In this case,
 *   @value is set. %FALSE if an error occurred. In this case,
 *   @error is set
583
 */
584
gboolean
585
gdk_content_deserialize_finish (GAsyncResult  *result,
586
                                GValue        *value,
587
588
589
590
                                GError       **error)
{
  GdkContentDeserializer *deserializer;

591
592
  g_return_val_if_fail (GDK_IS_CONTENT_DESERIALIZER (result), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
593
  deserializer = GDK_CONTENT_DESERIALIZER (result);
594
  g_return_val_if_fail (G_VALUE_HOLDS (value, G_VALUE_TYPE (&deserializer->value)), FALSE);
595
596
597
598
599

  if (deserializer->error)
    {
      if (error)
        *error = g_error_copy (deserializer->error);
600
      return FALSE;
601
602
    }

603
604
  g_value_copy (&deserializer->value, value);
  return TRUE;
605
606
607
608
609
610
611
612
613
614
}

/*** DESERIALIZERS ***/

static void
pixbuf_deserializer_finish (GObject      *source,
                            GAsyncResult *res,
                            gpointer      deserializer)
{
  GdkPixbuf *pixbuf;
615
  GValue *value;
616
617
618
619
620
621
622
623
624
  GError *error = NULL;

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

625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
  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 ();
    }
641
642
643
644
645
646
647
648
649
650
651
652
  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);
}

653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
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
    {
675
676
677
678
679
680
681
682
683
684
      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;
        }

685
      g_value_take_string (gdk_content_deserializer_get_value (deserializer),
686
                           g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (mem_stream)));
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
    }
  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),
715
                                G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
716
717
718
719
720
721
722
                                gdk_content_deserializer_get_priority (deserializer),
                                gdk_content_deserializer_get_cancellable (deserializer),
                                string_deserializer_finish,
                                deserializer);
  g_object_unref (filter);
}

723
724
725
726
727
728
729
730
static void
file_uri_deserializer_finish (GObject      *source,
                              GAsyncResult *result,
                              gpointer      deserializer)
{
  GOutputStream *stream = G_OUTPUT_STREAM (source);
  GError *error = NULL;
  gssize written;
731
  GValue *value;
732
733
734
735
736
737
738
739
740
741
742
  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 */
743
744
  if (g_output_stream_write (stream, "", 1, NULL, &error) < 0 ||
      !g_output_stream_close (stream, NULL, &error))
745
746
747
748
749
    {
      gdk_content_deserializer_return_error (deserializer, error);
      return;
    }

750
  str = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (stream));
751
752
753
  uris = g_uri_list_extract_uris (str);
  g_free (str);

754
755
  value = gdk_content_deserializer_get_value (deserializer);
  if (G_VALUE_HOLDS (value, G_TYPE_FILE))
756
    {
757
758
      if (uris[0] != NULL)
        g_value_take_object (value, g_file_new_for_uri (uris[0]));
759
760
761
    }
  else
    {
762
763
764
765
766
      GSList *l = NULL;
      gsize i;

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

768
      g_value_take_boxed (value, g_slist_reverse (l));
769
770
    }
  g_strfreev (uris);
771
772

  gdk_content_deserializer_return_success (deserializer);
773
774
775
776
777
778
779
780
781
782
783
}

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),
784
                                G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE,
785
786
787
788
789
790
791
                                gdk_content_deserializer_get_priority (deserializer),
                                gdk_content_deserializer_get_cancellable (deserializer),
                                file_uri_deserializer_finish,
                                deserializer);
  g_object_unref (output);
}

792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
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);
}

849
850
851
852
853
static void
init (void)
{
  static gboolean initialized = FALSE;
  GSList *formats, *f;
854
  const char *charset;
855
856
857
858
859
860
861
862
863
864
865
866

  if (initialized)
    return;

  initialized = TRUE;

  formats = gdk_pixbuf_get_formats ();

  /* Make sure png comes first */
  for (f = formats; f; f = f->next)
    {
      GdkPixbufFormat *fmt = f->data;
Benjamin Otte's avatar
Benjamin Otte committed
867
      char *name; 
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
 
      name = gdk_pixbuf_format_get_name (fmt);
      if (g_str_equal (name, "png"))
	{
	  formats = g_slist_delete_link (formats, f);
	  formats = g_slist_prepend (formats, fmt);

	  g_free (name);

	  break;
	}

      g_free (name);
    }  

  for (f = formats; f; f = f->next)
    {
      GdkPixbufFormat *fmt = f->data;
Benjamin Otte's avatar
Benjamin Otte committed
886
      char **mimes, **m;
887
888
889
890

      mimes = gdk_pixbuf_format_get_mime_types (fmt);
      for (m = mimes; *m; m++)
	{
891
892
893
894
895
          gdk_content_register_deserializer (*m,
                                             GDK_TYPE_TEXTURE,
                                             pixbuf_deserializer,
                                             NULL,
                                             NULL);
896
897
898
899
900
901
902
903
904
905
          gdk_content_register_deserializer (*m,
                                             GDK_TYPE_PIXBUF,
                                             pixbuf_deserializer,
                                             NULL,
                                             NULL);
	}
      g_strfreev (mimes);
    }

  g_slist_free (formats);
906

907
#if defined(G_OS_UNIX) && !defined(__APPLE__)
908
  file_transfer_portal_register ();
909
910
#endif

911
912
913
914
915
  gdk_content_register_deserializer ("text/uri-list",
                                     GDK_TYPE_FILE_LIST,
                                     file_uri_deserializer,
                                     NULL,
                                     NULL);
916

917
918
919
920
921
922
  gdk_content_register_deserializer ("text/uri-list",
                                     G_TYPE_FILE,
                                     file_uri_deserializer,
                                     NULL,
                                     NULL);

923
924
925
926
927
928
929
930
931
932
933
  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,
934
                                         (gpointer) charset,
935
936
937
938
939
940
941
                                         g_free);
    }
  gdk_content_register_deserializer ("text/plain",
                                     G_TYPE_STRING,
                                     string_deserializer,
                                     (gpointer) "ASCII",
                                     NULL);
942
943
944
945
946
947

  gdk_content_register_deserializer ("application/x-color",
                                     GDK_TYPE_RGBA,
                                     color_deserializer,
                                     NULL,
                                     NULL);
948
949
}