summaryrefslogtreecommitdiff
path: root/src/data/grl-data.c
blob: 455121ddb3ec0596f3149d86f0ce98211a9d40f0 (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
394
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
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
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
/*
 * Copyright (C) 2010, 2011 Igalia S.L.
 *
 * Contact: Iago Toral Quiroga <itoral@igalia.com>
 *
 * Authors: Juan A. Suarez Romero <jasuarez@igalia.com>
 *
 * 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; version 2.1 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:grl-data
 * @short_description: Low-level class for storing metadata information about
 * media resources provided by #GrlSource objects.
 * @see_also: #GrlMedia
 *
 * This class acts as a generic metadata dictionary where metadata keys and
 * their values can be stored.  Usually, application and plugin developers would
 * interact with specific subclass of #GrlData, #GrlMedia, which provide
 * specific API to manipulate well known attributes of these media types.
 */

#include "grl-data.h"
#include "grl-log.h"
#include "grl-registry-priv.h"

#define GRL_LOG_DOMAIN_DEFAULT data_log_domain
GRL_LOG_DOMAIN(data_log_domain);

struct _GrlDataPrivate {
  GHashTable *data;
};

static void grl_data_finalize (GObject *object);
static void free_list_values (GrlKeyID key, GList *values, gpointer user_data);

static void free_list_values (GrlKeyID key, GList *values, gpointer user_data);

/* ================ GrlData GObject ================ */

G_DEFINE_TYPE_WITH_PRIVATE (GrlData, grl_data, G_TYPE_OBJECT);

static void
grl_data_class_init (GrlDataClass *klass)
{
  GObjectClass *gobject_class = (GObjectClass *)klass;

  gobject_class->finalize = grl_data_finalize;
}

static void
grl_data_init (GrlData *self)
{
  self->priv = grl_data_get_instance_private (self);
  self->priv->data = g_hash_table_new_full (g_direct_hash,
                                            g_direct_equal,
                                            NULL,
                                            NULL);
}

static void
grl_data_finalize (GObject *object)
{
  GrlData *data = GRL_DATA (object);

  g_signal_handlers_destroy (object);
  g_hash_table_foreach (data->priv->data,
                        (GHFunc) free_list_values,
                        NULL);
  g_hash_table_unref (data->priv->data);

  G_OBJECT_CLASS (grl_data_parent_class)->finalize (object);
}

/* ================ Utitilies ================ */

/* Free the list of values, which are of type #GrlRelatedKeys */
static void
free_list_values (GrlKeyID key, GList *values, gpointer user_data)
{
  g_list_free_full (values, g_object_unref);
}

/* Returns the sample key that represents the set of keys related with @key */
static GrlKeyID
get_sample_key (GrlKeyID key)
{
  GrlRegistry *registry;
  const GList *related_keys;

  registry = grl_registry_get_default ();
  related_keys =
    grl_registry_lookup_metadata_key_relation (registry, key);

  if (!related_keys) {
    GRL_WARNING ("Related keys not found for key \"%s\"",
                 grl_metadata_key_get_name (key));
    return GRL_METADATA_KEY_INVALID;
  } else {
    return GRLPOINTER_TO_KEYID (related_keys->data);
  }
}

/* ================ API ================ */

/**
 * grl_data_new:
 *
 * Creates a new data object.
 *
 * Returns: a new data object.
 *
 * Since: 0.1.4
 **/
GrlData *
grl_data_new (void)
{
  return g_object_new (GRL_TYPE_DATA,
		       NULL);
}

/**
 * grl_data_get:
 * @data: data to retrieve value
 * @key: (type GrlKeyID): key to look up.
 *
 * Get the first value from @data associated with @key.
 *
 * Returns: (transfer none): a #GValue. This value should not be modified nor
 * freed by user.
 *
 * Since: 0.1.4
 **/
const GValue *
grl_data_get (GrlData *data, GrlKeyID key)
{
  GrlRelatedKeys *relkeys = NULL;

  g_return_val_if_fail (GRL_IS_DATA (data), NULL);
  g_return_val_if_fail (key, NULL);

  if (grl_data_length (data, key) > 0) {
    relkeys = grl_data_get_related_keys (data, key, 0);
  }

  if (!relkeys) {
    return NULL;
  }

  return grl_related_keys_get (relkeys, key);
}

/**
 * grl_data_set:
 * @data: data to modify
 * @key: (type GrlKeyID): key to change or add
 * @value: the new value
 *
 * Sets the first value associated with @key in @data. If key already has a
 * value old value is freed and the new one is set.
 *
 * Also, checks that @value is compliant with @key specification, modifying it
 * accordingly. For instance, if @key requires a number between 0 and 10, but
 * @value is outside this range, it will be adapted accordingly.
 *
 * Since: 0.1.4
 **/
void
grl_data_set (GrlData *data, GrlKeyID key, const GValue *value)
{
  GrlRelatedKeys *relkeys = NULL;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  if (!value) {
    return;
  }

  /* Get the right set of related keys */
  if (grl_data_length (data, key) > 0) {
    relkeys = grl_data_get_related_keys (data, key, 0);
  }

  if (!relkeys) {
    /* No related keys; add them */
    relkeys = grl_related_keys_new ();
    grl_related_keys_set (relkeys, key, value);
    grl_data_add_related_keys (data, relkeys);
  } else {
    /* Set the new value */
    grl_related_keys_set (relkeys, key, value);
  }
}

/**
 * grl_data_set_string:
 * @data: data to modify
 * @key: (type GrlKeyID): key to change or add
 * @strvalue: the new value
 *
 * Sets the first string value associated with @key in @data. If @key already
 * has a value old value is freed and the new one is set.
 *
 * Since: 0.1.4
 **/
void
grl_data_set_string (GrlData *data,
                     GrlKeyID key,
                     const gchar *strvalue)
{
  GValue value = { 0 };

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  if (strvalue) {
    g_value_init (&value, G_TYPE_STRING);
    g_value_set_string (&value, strvalue);
    grl_data_set (data, key, &value);
    g_value_unset (&value);
  }
}

/**
 * grl_data_get_string:
 * @data: data to inspect
 * @key: (type GrlKeyID): key to use
 *
 * Returns the first string value associated with @key from @data. If @key has
 * no first value, or value is not string, or @key is not in @data, then %NULL
 * is returned.
 *
 * Returns: string associated with @key, or %NULL in other case. Caller should
 * not change nor free the value.
 *
 * Since: 0.1.4
 **/
const gchar *
grl_data_get_string (GrlData *data, GrlKeyID key)
{
  const GValue *value;

  g_return_val_if_fail (GRL_IS_DATA (data), NULL);
  g_return_val_if_fail (key, NULL);

  value = grl_data_get (data, key);

  if (!value || !G_VALUE_HOLDS_STRING (value)) {
    return NULL;
  } else {
    return g_value_get_string (value);
  }
}

/**
 * grl_data_set_int:
 * @data: data to change
 * @key: (type GrlKeyID): key to change or add
 * @intvalue: the new value
 *
 * Sets the first int value associated with @key in @data. If @key already has a
 * first value old value is replaced by the new one.
 *
 * Since: 0.1.4
 **/
void
grl_data_set_int (GrlData *data, GrlKeyID key, gint intvalue)
{
  GValue value = { 0 };

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  g_value_init (&value, G_TYPE_INT);
  g_value_set_int (&value, intvalue);
  grl_data_set (data, key, &value);
}

/**
 * grl_data_get_int:
 * @data: data to inspect
 * @key: (type GrlKeyID): key to use
 *
 * Returns the first int value associated with @key from @data. If @key has no
 * first value, or value is not a gint, or @key is not in data, then 0 is
 * returned.
 *
 * Returns: int value associated with @key, or 0 in other case.
 *
 * Since: 0.1.4
 **/
gint
grl_data_get_int (GrlData *data, GrlKeyID key)
{
  const GValue *value;

  g_return_val_if_fail (GRL_IS_DATA (data), 0);
  g_return_val_if_fail (key, 0);

  value = grl_data_get (data, key);

  if (!value || !G_VALUE_HOLDS_INT (value)) {
    return 0;
  } else {
    return g_value_get_int (value);
  }
}

/**
 * grl_data_set_float:
 * @data: data to change
 * @key: (type GrlKeyID): key to change or add
 * @floatvalue: the new value
 *
 * Sets the first float value associated with @key in @data. If @key already has
 * a first value old value is replaced by the new one.
 *
 * Since: 0.1.5
 **/
void
grl_data_set_float (GrlData *data, GrlKeyID key, float floatvalue)
{
  GValue value = { 0 };

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  g_value_init (&value, G_TYPE_FLOAT);
  g_value_set_float (&value, floatvalue);
  grl_data_set (data, key, &value);
}

/**
 * grl_data_get_float:
 * @data: data to inspect
 * @key: (type GrlKeyID): key to use
 *
 * Returns the first float value associated with @key from @data. If @key has no
 * first value, or value is not a gfloat, or @key is not in data, then 0 is
 * returned.
 *
 * Returns: float value associated with @key, or 0 in other case.
 *
 * Since: 0.1.5
 **/
gfloat
grl_data_get_float (GrlData *data, GrlKeyID key)
{
  const GValue *value;

  g_return_val_if_fail (GRL_IS_DATA (data), 0.0);
  g_return_val_if_fail (key, 0.0);

  value = grl_data_get (data, key);

  if (!value || !G_VALUE_HOLDS_FLOAT (value)) {
    return 0;
  } else {
    return g_value_get_float (value);
  }
}

/**
 * grl_data_set_boolean:
 * @data: data to change
 * @key: (type GrlKeyID): key to change or add
 * @boolvalue: the new value
 *
 * Sets the first boolean value associated with @key in @data. If @key already
 * has a first value, old value is replaced by the new one.
 *
 * Since: 0.2.3
 **/
void
grl_data_set_boolean (GrlData *data, GrlKeyID key, gboolean boolvalue)
{
  GValue value = { 0 };

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  g_value_init (&value, G_TYPE_BOOLEAN);
  g_value_set_boolean (&value, boolvalue);
  grl_data_set (data, key, &value);
}

/**
 * grl_data_get_boolean:
 * @data: data to inspect
 * @key: (type GrlKeyID): key to use
 *
 * Returns: the first boolean value associated with @key from @data. If @key has
 * no first value, or value is not a gboolean, or @key is not in the data, then
 * FALSE is returned
 *
 * Since: 0.2.3
 */
gboolean
grl_data_get_boolean (GrlData *data, GrlKeyID key)
{
  const GValue *value;

  g_return_val_if_fail (GRL_IS_DATA (data), FALSE);
  g_return_val_if_fail (key, FALSE);

  value = grl_data_get (data, key);

  if (!value || !G_VALUE_HOLDS_BOOLEAN (value)) {
    return FALSE;
  } else {
    return g_value_get_boolean (value);
  }
}

/**
 * grl_data_set_binary:
 * @data: data to change
 * @key: (type GrlKeyID): key to change or add
 * @buf: buffer holding the data
 * @size: size of the buffer
 *
 * Sets the first binary value associated with @key in @data. If @key already
 * has a first value old value is replaced by the new one.
 *
 * Since: 0.1.9
 **/
void
grl_data_set_binary (GrlData *data, GrlKeyID key, const guint8 *buf, gsize size)
{
  GValue v = { 0 };
  GByteArray * array;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  if (!buf || !size) {
    return;
  }

  array = g_byte_array_append(g_byte_array_sized_new(size),
		              buf,
		              size);

  g_value_init (&v, g_byte_array_get_type());
  g_value_take_boxed(&v, array);
  grl_data_set(data, key, &v);
  g_value_unset (&v);
}

/**
 * grl_data_get_binary:
 * @data: data to inspect
 * @key: (type GrlKeyID): key to use
 * @size: (out): location to store the buffer size
 *
 * Returns the first binary value associated with @key from @data. If @key has
 * no first value, or value is not a gfloat, or @key is not in data, then %NULL
 * is returned.
 *
 * Returns: buffer location associated with the @key, or %NULL in other case. If
 * successful @size will be set the to the buffer size.
 *
 * Since: 0.1.9
 **/
const guint8 *
grl_data_get_binary(GrlData *data, GrlKeyID key, gsize *size)
{
  const GValue *value;

  g_return_val_if_fail (size, NULL);
  g_return_val_if_fail (GRL_IS_DATA (data), NULL);
  g_return_val_if_fail (key, NULL);

  value = grl_data_get (data, key);

  if (!value || !G_VALUE_HOLDS_BOXED (value)) {
    return NULL;
  } else {
    GByteArray * array;

    array = g_value_get_boxed(value);
    *size = array->len;
    return (const guint8 *) array->data;
  }
}

/**
 * grl_data_set_boxed:
 * @data: data to modify
 * @key: key to change or add
 * @boxed: the new value
 *
 * Sets the first boxed value associated with @key in @data. If @key already
 * has a value, the old value is freed and the new one is set.
 *
 * Since: 0.2.0
 **/
void
grl_data_set_boxed (GrlData *data, GrlKeyID key, gconstpointer boxed)
{
  GValue value = { 0 };

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);
  g_return_if_fail (boxed != NULL);

  g_value_init (&value, GRL_METADATA_KEY_GET_TYPE (key));
  g_value_set_boxed (&value, boxed);
  grl_data_set (data, key, &value);
  g_value_unset (&value);
}

/**
 * grl_data_get_boxed:
 * @data: data to inspect
 * @key: (type GrlKeyID): key to use
 *
 * Returns the first boxed value associated with @key from @data. If @key has
 * no first value, that value is not of a boxed type, or @key is not in @data,
 * then %NULL is returned.
 *
 * Returns: (transfer none): the boxed instance associated with @key if
 * possible, or %NULL in other cases. The caller should not change nor free the
 * value.
 *
 * Since: 0.2.0
 **/
gpointer
grl_data_get_boxed (GrlData *data, GrlKeyID key)
{
  const GValue *value;

  g_return_val_if_fail (GRL_IS_DATA (data), NULL);
  g_return_val_if_fail (key, NULL);

  value = grl_data_get (data, key);

  if (!value || !G_VALUE_HOLDS_BOXED (value)) {
    return NULL;
  } else {
    return g_value_get_boxed (value);
  }
}

/**
 * grl_data_set_int64:
 * @data: data to change
 * @key: (type GrlKeyID): key to change or add
 * @intvalue: the new value
 *
 * Sets the first int64 value associated with @key in @data. If @key already has a
 * first value old value is replaced by the new one.
 *
 * Since: 0.2.12
 **/
void
grl_data_set_int64 (GrlData *data, GrlKeyID key, gint64 intvalue)
{
  GValue value = { 0 };

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  g_value_init (&value, G_TYPE_INT64);
  g_value_set_int64 (&value, intvalue);
  grl_data_set (data, key, &value);
}

/**
 * grl_data_get_int64:
 * @data: data to inspect
 * @key: (type GrlKeyID): key to use
 *
 * Returns the first int64 value associated with @key from @data. If @key has no
 * first value, or value is not a gint, or @key is not in data, then 0 is
 * returned.
 *
 * Returns: int value associated with @key, or 0 in other case.
 *
 * Since: 0.2.12
 **/
gint64
grl_data_get_int64 (GrlData *data, GrlKeyID key)
{
  const GValue *value;

  g_return_val_if_fail (GRL_IS_DATA (data), 0);
  g_return_val_if_fail (key, 0);

  value = grl_data_get (data, key);

  if (!value || !G_VALUE_HOLDS_INT64 (value)) {
    return 0;
  } else {
    return g_value_get_int64 (value);
  }
}

/**
 * grl_data_set_for_id:
 * @data: data to change
 * @key_name: name of the key to change or add
 * @value: the new value
 *
 * Sets the first value associated with @key_name in @data. This @key_name is used to create
 * a new #GParamSpec instance, which is further used to create and register a key using
 * grl_registry_register_metadata_key(). If @key_name already has a first @value, old
 * value is replaced by the new one.
 *
 * A property key_name consists of segments consisting of ASCII letters and
 * digits, separated by either the '-' or '_' character. The first
 * character of a property key_name must be a letter. Key_names which violate these
 * rules lead to undefined behaviour.
 *
 * Returns: TRUE if @value was set to @key_name, FALSE otherwise.
 *
 * Since: 0.3.6
 **/
gboolean
grl_data_set_for_id (GrlData *data, const gchar *key_name, const GValue *value)
{
  GrlRegistry *registry;
  GrlKeyID key_id;

  registry = grl_registry_get_default ();
  key_id = grl_registry_register_or_lookup_metadata_key (registry,
                                                         key_name,
                                                         value,
                                                         GRL_METADATA_KEY_INVALID);
  if (key_id == GRL_METADATA_KEY_INVALID) {
    return FALSE;
  }

  grl_data_set (data, key_id, value);
  return TRUE;
}


/**
 * grl_data_add_for_id:
 * @data: data to change
 * @key_name: name of the key to change or add
 * @value: the new value
 *
 * Appends the value associated with @key_name to @data. This @key_name is used to create
 * a new #GParamSpec instance, which is further used to create and register a key using
 * grl_registry_register_metadata_key().
 *
 * A property key_name consists of segments consisting of ASCII letters and
 * digits, separated by either the '-' or '_' character. The first
 * character of a property key_name must be a letter. Key_names which violate these
 * rules lead to undefined behaviour.
 *
 * Returns: TRUE if @value was added to @key_name, FALSE otherwise.
 *
 * Since: 0.3.6
 **/
gboolean
grl_data_add_for_id (GrlData *data, const gchar *key_name, const GValue *value)
{
  GrlRegistry *registry;
  GrlKeyID key_id;
  GType value_type;

  registry = grl_registry_get_default ();
  key_id = grl_registry_register_or_lookup_metadata_key (registry,
                                                         key_name,
                                                         value,
                                                         GRL_METADATA_KEY_INVALID);
  if (key_id == GRL_METADATA_KEY_INVALID) {
    return FALSE;
  }

  value_type = G_VALUE_TYPE (value);

  switch (value_type) {
  case G_TYPE_INT:
    grl_data_add_int (data, key_id, g_value_get_int (value));
    break;

  case G_TYPE_INT64:
    grl_data_add_int64 (data, key_id, g_value_get_int64 (value));
    break;

  case G_TYPE_FLOAT:
    grl_data_add_float (data, key_id, g_value_get_float (value));
    break;

  case G_TYPE_STRING:
    grl_data_add_string (data, key_id, g_value_get_string (value));
    break;

  default:
    /* FIXME: We should add support to all GType supported by GrlMedia */
    GRL_WARNING ("'%s' is being ignored as %s type is not being handled",
                 key_name, g_type_name (value_type));
    return FALSE;
  }

  return TRUE;
}

/**
 * grl_data_remove:
 * @data: data to change
 * @key: (type GrlKeyID): key to remove
 *
 * Removes the first value for @key from @data. If there are other keys related
 * to @key their values will also be removed from @data.
 *
 * Since: 0.1.4
 **/
void
grl_data_remove (GrlData *data, GrlKeyID key)
{
  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  grl_data_remove_nth (data, key, 0);
}

/**
 * grl_data_has_key:
 * @data: data to inspect
 * @key: (type GrlKeyID): key to search
 *
 * Checks if @key is in @data.
 *
 * Returns: %TRUE if @key is in @data, %FALSE in other case.
 *
 * Since: 0.1.4
 **/
gboolean
grl_data_has_key (GrlData *data, GrlKeyID key)
{
  GList *related_keys;
  GrlKeyID sample_key;
  gboolean found = FALSE;

  g_return_val_if_fail (GRL_IS_DATA (data), FALSE);
  g_return_val_if_fail (key, FALSE);

  sample_key = get_sample_key (key);
  if (!sample_key) {
    return FALSE;
  }

  related_keys = g_hash_table_lookup (data->priv->data, GRLKEYID_TO_POINTER (sample_key));
  while (related_keys && !found) {
    found = grl_related_keys_has_key (related_keys->data, key);
    related_keys = g_list_next (related_keys);
  }

  return found;
}

/**
 * grl_data_get_keys:
 * @data: data to inspect
 *
 * Returns a list with keys contained in @data.
 *
 * Returns: (transfer container) (element-type GrlKeyID): an array with the
 * keys. The content of the list should not be modified or freed. Use
 * g_list_free() when done using the list.
 *
 * Since: 0.1.4
 **/
GList *
grl_data_get_keys (GrlData *data)
{
  GList *allkeys = NULL;
  GList *keylist, *key;
  GrlRegistry *registry;
  const GList *relkeys;

  g_return_val_if_fail (GRL_IS_DATA (data), NULL);

  keylist = g_hash_table_get_keys (data->priv->data);
  registry = grl_registry_get_default ();

  for (key = keylist; key; key = g_list_next (key)) {
    GrlKeyID key_id = GRLPOINTER_TO_KEYID (key->data);
    relkeys =
        grl_registry_lookup_metadata_key_relation (registry, key_id);
    while (relkeys) {
      if (grl_data_has_key (data, GRLPOINTER_TO_KEYID (relkeys->data))) {
        allkeys = g_list_prepend (allkeys, relkeys->data);
      }
      relkeys = g_list_next (relkeys);
    }
  }

  g_list_free (keylist);

  return allkeys;
}

/**
 * grl_data_add_related_keys:
 * @data: data to change
 * @relkeys: (transfer full): a set of related properties with their values
 *
 * Adds a new set of values into @data.
 *
 * All keys in @prop must be related among them.
 *
 * @data will take the ownership of @relkeys, so do not modify it.
 *
 * Since: 0.1.10
 **/
void
grl_data_add_related_keys (GrlData *data,
                           GrlRelatedKeys *relkeys)
{
  GList *keys;
  GList *list_relkeys;
  GrlKeyID sample_key;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (GRL_IS_RELATED_KEYS (relkeys));

  keys = grl_related_keys_get_keys (relkeys);
  if (!keys) {
    /* Ignore empty set of related keys */
    GRL_WARNING ("Trying to add an empty GrlRelatedKeys to GrlData");
    g_object_unref (relkeys);
    return;
  }

  sample_key = get_sample_key (GRLPOINTER_TO_KEYID (keys->data));
  g_list_free (keys);

  if (!sample_key) {
    g_object_unref (relkeys);
    return;
  }

  list_relkeys = g_hash_table_lookup (data->priv->data,
                                      GRLKEYID_TO_POINTER (sample_key));
  list_relkeys = g_list_append (list_relkeys, relkeys);
  g_hash_table_insert (data->priv->data,
                       GRLKEYID_TO_POINTER (sample_key),
                       list_relkeys);
}

/**
 * grl_data_add_string:
 * @data: data to append
 * @key: (type GrlKeyID): key to append
 * @strvalue: the new value
 *
 * Appends a new string value for @key in @data.
 *
 * Since: 0.1.10
 **/
void
grl_data_add_string (GrlData *data,
                     GrlKeyID key,
                     const gchar *strvalue)
{
  GrlRelatedKeys *relkeys;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  if (strvalue) {
    relkeys = grl_related_keys_new ();
    grl_related_keys_set_string (relkeys, key, strvalue);
    grl_data_add_related_keys (data, relkeys);
  }
}

/**
 * grl_data_add_int:
 * @data: data to append
 * @key: (type GrlKeyID): key to append
 * @intvalue: the new value
 *
 * Appends a new int value for @key in @data.
 *
 * Since: 0.1.10
 **/
void
grl_data_add_int (GrlData *data,
                  GrlKeyID key,
                  gint intvalue)
{
  GrlRelatedKeys *relkeys;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  relkeys = grl_related_keys_new ();
  grl_related_keys_set_int (relkeys, key, intvalue);
  grl_data_add_related_keys (data, relkeys);
}

/**
 * grl_data_add_float:
 * @data: data to append
 * @key: (type GrlKeyID): key to append
 * @floatvalue: the new value
 *
 * Appends a new float value for @key in @data.
 *
 * Since: 0.1.10
 **/
void
grl_data_add_float (GrlData *data,
                    GrlKeyID key,
                    gfloat floatvalue)
{
  GrlRelatedKeys *relkeys;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  relkeys = grl_related_keys_new ();
  grl_related_keys_set_float (relkeys, key, floatvalue);
  grl_data_add_related_keys (data, relkeys);
}

/**
 * grl_data_add_binary:
 * @data: data to append
 * @key: (type GrlKeyID): key to append
 * @buf: the buffer containing the new value
 * @size: size of buffer
 *
 * Appends a new binary value for @key in @data.
 *
 * Since: 0.1.10
 **/
void
grl_data_add_binary (GrlData *data,
                     GrlKeyID key,
                     const guint8 *buf,
                     gsize size)
{
  GrlRelatedKeys *relkeys;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  if (!buf || !size) {
    return;
  }

  relkeys = grl_related_keys_new ();
  grl_related_keys_set_binary (relkeys, key, buf, size);
  grl_data_add_related_keys (data, relkeys);
}

/**
 * grl_data_add_boxed:
 * @data: data to append
 * @key: (type GrlKeyID): key to append
 * @boxed: the new value
 *
 * Appends a new boxed value for @key in @data.
 *
 * Since: 0.2.0
 **/
void
grl_data_add_boxed (GrlData *data,
                    GrlKeyID key,
                    gconstpointer boxed)
{
  GrlRelatedKeys *relkeys;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);
  g_return_if_fail (boxed != NULL);

  relkeys = grl_related_keys_new ();
  grl_related_keys_set_boxed (relkeys, key, boxed);
  grl_data_add_related_keys (data, relkeys);
}

/**
 * grl_data_add_int64:
 * @data: data to append
 * @key: (type GrlKeyID): key to append
 * @intvalue: the new value
 *
 * Appends a new int64 value for @key in @data.
 *
 * Since: 0.2.12
 **/
void
grl_data_add_int64 (GrlData *data,
                    GrlKeyID key,
                    gint64 intvalue)
{
  GrlRelatedKeys *relkeys;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  relkeys = grl_related_keys_new ();
  grl_related_keys_set_int64 (relkeys, key, intvalue);
  grl_data_add_related_keys (data, relkeys);
}

/**
 * grl_data_length:
 * @data: a data
 * @key: a metadata key
 *
 * Returns how many values @key or related keys have in @data: if @key has no
 * value, but a related key has, then it is counted as positive.
 *
 * As example, let's think in three related keys, K1, K2 and K3, and then thinks
 * we have added several values for those keys, as:
 *
 *   (V10, V20, V30),, (V11, NULL, V31), (V12, NULL, V32)
 *
 * Therefore, when invoking grl_data_length (data, K2) it will return 3:
 * considering K2 and the related keys (K1 and K3), there are 3 values.
 *
 * Returns: number of values
 *
 * Since: 0.1.10
 **/
guint
grl_data_length (GrlData *data,
                 GrlKeyID key)
{
  GrlKeyID sample_key;

  g_return_val_if_fail (GRL_IS_DATA (data), 0);
  g_return_val_if_fail (key, 0);

  sample_key = get_sample_key (key);
  if (!sample_key) {
    return 0;
  }

  return g_list_length (g_hash_table_lookup (data->priv->data,
                                             GRLKEYID_TO_POINTER (sample_key)));
}

/**
 * grl_data_get_related_keys:
 * @data: a data
 * @key: a metadata key
 * @index: element to retrieve, starting at 0
 *
 * Returns a set containing the values for @key and related keys at position
 * @index from @data.
 *
 * If user changes any of the values in the related keys, the changes will
 * become permanent.
 *
 * Returns: (transfer none): a #GrlRelatedKeys. Do not free it.
 *
 * Since: 0.1.10
 **/
GrlRelatedKeys *
grl_data_get_related_keys (GrlData *data,
                           GrlKeyID key,
                           guint index)
{
  GList *relkeys_list;
  GrlKeyID sample_key;
  GrlRelatedKeys *relkeys;

  g_return_val_if_fail (GRL_IS_DATA (data), NULL);
  g_return_val_if_fail (key, NULL);

  sample_key = get_sample_key (key);
  if (!sample_key) {
    return NULL;
  }

  relkeys_list = g_hash_table_lookup (data->priv->data,
                                      GRLKEYID_TO_POINTER (sample_key));
  relkeys = g_list_nth_data (relkeys_list, index);

  if (!relkeys) {
    GRL_WARNING ("%s: index %u out of range", __FUNCTION__, index);
    return NULL;
  }

  return relkeys;
}

/**
 * grl_data_get_single_values_for_key:
 * @data: a data
 * @key: a metadata key
 *
 * Returns all non-%NULL values for @key from @data. This ignores related keys.
 *
 * Returns: (element-type GObject.Value) (transfer container): a #GList with
 * values. Do not change or free the values. Free the list with #g_list_free.
 *
 * Since: 0.1.13
 */
GList *
grl_data_get_single_values_for_key (GrlData *data,
                                    GrlKeyID key)
{
  GList *related_keys;
  GList *values = NULL;
  GrlKeyID sample_key;
  const GValue *v;

  g_return_val_if_fail (GRL_IS_DATA (data), NULL);
  g_return_val_if_fail (key, NULL);

  sample_key = get_sample_key (key);
  if (!sample_key) {
    return NULL;
  }

  related_keys = g_hash_table_lookup (data->priv->data,
                                      GRLKEYID_TO_POINTER (sample_key));
  while (related_keys) {
    v = grl_related_keys_get (related_keys->data, key);
    if (v) {
      values = g_list_prepend (values, (gpointer) v);
    }
    related_keys = g_list_next (related_keys);
  }

  return g_list_reverse (values);
}

/**
 * grl_data_get_single_values_for_key_string:
 * @data: a data
 * @key: a metadata key
 *
 * Returns all non-%NULL values for @key from @data. @key must have been
 * registered as a string-type key. This ignores related keys.
 *
 * Returns: (element-type utf8) (transfer container): a #GList with values. Do
 * not change or free the strings. Free the list with #g_list_free.
 *
 * Since: 0.1.13
 **/
GList *
grl_data_get_single_values_for_key_string (GrlData *data,
                                           GrlKeyID key)
{
  GList *list_strings = NULL;
  GList *list_values;
  GList *value;
  const gchar *string_value;

  g_return_val_if_fail (GRL_IS_DATA (data), NULL);
  g_return_val_if_fail (key, NULL);

  /* Verify key is of type string */
  if (GRL_METADATA_KEY_GET_TYPE (key) != G_TYPE_STRING) {
    GRL_WARNING ("%s: requested key is not of type string", __FUNCTION__);
    return NULL;
  }

  list_values = grl_data_get_single_values_for_key (data, key);
  for (value = list_values; value; value = g_list_next (value)) {
    string_value = g_value_get_string (value->data);
    if (string_value) {
      list_strings = g_list_prepend (list_strings, (gpointer) string_value);
    }
  }

  g_list_free (list_values);

  return g_list_reverse (list_strings);
}

/**
 * grl_data_remove_nth:
 * @data: a data
 * @key: a metadata key
 * @index: index of key to be removed, starting at 0
 *
 * Removes the value at position @index for @key from @data. If there are other
 * keys related to @key, their values at position @index will also be removed
 * from @data.
 *
 * Since: 0.1.10
 **/
void
grl_data_remove_nth (GrlData *data,
                     GrlKeyID key,
                     guint index)
{
  GList *relkeys_element;
  GList *relkeys_list;
  GrlKeyID sample_key;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (key);

  sample_key = get_sample_key (key);
  if (!sample_key) {
    return;
  }

  relkeys_list = g_hash_table_lookup (data->priv->data,
                                      GRLKEYID_TO_POINTER (sample_key));
  relkeys_element = g_list_nth (relkeys_list, index);
  if (!relkeys_element) {
    GRL_WARNING ("%s: index %u out of range", __FUNCTION__, index);
    return;
  }

  g_object_unref (relkeys_element->data);
  relkeys_list = g_list_delete_link (relkeys_list, relkeys_element);
  g_hash_table_insert (data->priv->data,
                       GRLKEYID_TO_POINTER (sample_key),
                       relkeys_list);
}

/**
 * grl_data_set_related_keys:
 * @data: a data
 * @relkeys: a set of related keys
 * @index: position to be updated, starting at 0
 *
 * Updates the values at position @index in @data with values in @relkeys.
 *
 * @data will take ownership of @relkeys, so do not free it after invoking this
 * function.
 *
 * Since: 0.1.10
 **/
void
grl_data_set_related_keys (GrlData *data,
                           GrlRelatedKeys *relkeys,
                           guint index)
{
  GList *keys;
  GList *relkeys_element;
  GList *relkeys_list;
  GrlKeyID sample_key;

  g_return_if_fail (GRL_IS_DATA (data));
  g_return_if_fail (GRL_IS_RELATED_KEYS (relkeys));

  keys = grl_related_keys_get_keys (relkeys);
  if (!keys) {
    GRL_WARNING ("Trying to set an empty GrlRelatedKeys into GrlData");
    g_object_unref (relkeys);
    return;
  }

  sample_key = get_sample_key (GRLPOINTER_TO_KEYID (keys->data));
  g_list_free (keys);
  if (!sample_key) {
    return;
  }

  relkeys_list = g_hash_table_lookup (data->priv->data,
                                      GRLKEYID_TO_POINTER (sample_key));
  relkeys_element = g_list_nth (relkeys_list, index);
  if (!relkeys_element) {
    GRL_WARNING ("%s: index %u out of range", __FUNCTION__, index);
    return;
  }

  g_object_unref (relkeys_element->data);
  relkeys_element->data = relkeys;
}

/**
 * grl_data_dup:
 * @data: data to duplicate
 *
 * Makes a deep copy of @data and all its contents.
 *
 * Returns: (transfer full): a new #GrlData. Free it with #g_object_unref.
 *
 * Since: 0.1.10
 **/
GrlData *
grl_data_dup (GrlData *data)
{
  GList *dup_relkeys_list;
  GList *key;
  GList *keys;
  GList *relkeys_list;
  GrlData *dup_data;

  g_return_val_if_fail (GRL_IS_DATA (data), NULL);

  dup_data = grl_data_new ();
  keys = g_hash_table_get_keys (data->priv->data);
  for (key = keys; key; key = g_list_next (key)) {
    dup_relkeys_list = NULL;
    relkeys_list = g_hash_table_lookup (data->priv->data, key->data);
    while (relkeys_list) {
      dup_relkeys_list =
        g_list_prepend (dup_relkeys_list,
                        grl_related_keys_dup (relkeys_list->data));
      relkeys_list = g_list_next (relkeys_list);
    }
    g_hash_table_insert (dup_data->priv->data,
                         key->data,
                         g_list_reverse (relkeys_list));
  }

  g_list_free (keys);

  return dup_data;
}