summaryrefslogtreecommitdiff
path: root/src/grl-operation-options.c
blob: c1c9791f7aa3301e46588aefae90c44f570bd9a1 (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
/*
 * Copyright (C) 2011 Igalia S.L.
 *
 * Contact: Iago Toral Quiroga <itoral@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-operation-options
 * @short_description: Describes the options to be passed to an operation
 * @see_also: #GrlCaps, grl_source_resolve(),
 * grl_source_search(), grl_source_browse(),
 * grl_source_query()
 *
 */
#include <grl-operation-options.h>
#include <grl-value-helper.h>
#include <grl-range-value.h>
#include <grl-log.h>
#include <grl-registry.h>

#include "grl-operation-options-priv.h"
#include "grl-type-builtins.h"

struct _GrlOperationOptionsPrivate {
  GHashTable *data;
  GHashTable *key_filter;
  GHashTable *key_range_filter;
  GrlCaps *caps;
};

G_DEFINE_TYPE_WITH_PRIVATE (GrlOperationOptions, grl_operation_options, G_TYPE_OBJECT);

#define SKIP_DEFAULT 0;
#define COUNT_DEFAULT GRL_COUNT_INFINITY;
#define RESOLUTION_FLAGS_DEFAULT GRL_RESOLVE_NORMAL;
#define REMOVE_FLAGS_DEFAULT GRL_REMOVE_FLAG_UNKNOWN
#define TYPE_FILTER_DEFAULT GRL_TYPE_FILTER_ALL;

static void
grl_operation_options_dispose (GrlOperationOptions *self)
{
}

static void
grl_operation_options_finalize (GrlOperationOptions *self)
{
  g_hash_table_unref (self->priv->data);
  g_hash_table_unref (self->priv->key_filter);
  g_hash_table_unref (self->priv->key_range_filter);
  g_clear_object (&self->priv->caps);
  G_OBJECT_CLASS (grl_operation_options_parent_class)->finalize ((GObject *) self);
}

static void
grl_operation_options_init (GrlOperationOptions *self)
{
  self->priv = grl_operation_options_get_instance_private (self);

  self->priv->data = grl_g_value_hashtable_new ();
  self->priv->key_filter = grl_g_value_hashtable_new_direct ();
  self->priv->key_range_filter = grl_range_value_hashtable_new ();
  self->priv->caps = NULL;
}

static void
grl_operation_options_class_init (GrlOperationOptionsClass *self_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (self_class);

  object_class->dispose = (void (*) (GObject *object)) grl_operation_options_dispose;
  object_class->finalize = (void (*) (GObject *object)) grl_operation_options_finalize;
}

static void
set_value (GrlOperationOptions *options,
                const gchar *key,
                const GValue *value)
{
  g_hash_table_insert (options->priv->data,
                       g_strdup (key), grl_g_value_dup (value));
}

static void
copy_option (GrlOperationOptions *source,
             GrlOperationOptions *destination,
             const gchar *key)
{
  const GValue *value;

  value = g_hash_table_lookup (source->priv->data, key);

  if (value != NULL)
    set_value (destination, key, value);
}

static gboolean
check_and_copy_option (GrlOperationOptions *options,
                       GrlCaps *caps,
                       const gchar *key,
                       GrlOperationOptions **supported_options,
                       GrlOperationOptions **unsupported_options)
{
  if (grl_operation_options_key_is_set (options, key)) {
    GValue *value;
    gboolean filter_is_supported;

    value = g_hash_table_lookup (options->priv->data, key);
    filter_is_supported = grl_caps_test_option (caps, key, value);

    if (filter_is_supported && supported_options)
      set_value (*supported_options, key, value);
    else if (!filter_is_supported && unsupported_options)
      set_value (*unsupported_options, key, value);

    return filter_is_supported;
  }

  return TRUE;
}

static void
key_filter_dup (gpointer key_p, GValue *value, GHashTable *destination)
{
  g_hash_table_insert (destination, key_p, grl_g_value_dup (value));
}

static void
key_range_filter_dup (GrlKeyID key, GrlRangeValue *value, GHashTable *destination)
{
  grl_range_value_hashtable_insert (destination, GRLKEYID_TO_POINTER (key), value->min, value->max);
}

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

/**
 * grl_operation_options_new:
 * @caps: (allow-none): caps that options will have to match. If %NULL, all
 * options will be accepted.
 *
 * Creates a new GrlOperationOptions object.
 *
 * Returns: a new GrlOperationOptions instance.
 *
 * Since: 0.2.0
 */
GrlOperationOptions *
grl_operation_options_new (GrlCaps *caps)
{
  GrlOperationOptions *options = g_object_new (GRL_TYPE_OPERATION_OPTIONS, NULL);
  if (caps != NULL)
    options->priv->caps = g_object_ref (caps);

  return options;
}

/**
 * grl_operation_options_obey_caps:
 * @options: a #GrlOperationOptions instance
 * @caps: capabilities against which we want to test @options
 * @supported_options: (out callee-allocates): if not %NULL, will contain a
 * newly-allocated #GrlOperationOptions instance containing all the values of
 * @options that match @caps.
 * @unsupported_options: (out callee-allocates): if not %NULL, will contain a
 * newly-allocated #GrlOperationOptions instance containing all the values of
 * @options that do not match @caps.
 *
 * Check whether @options obey to @caps.
 * Optionally provide the options that match (respectively don't match) @caps
 * in @supported_options (respectively @unsupported_options).
 * This would typically (but not necessarily) be used with a
 * #GrlOperationOptions instance that was created with %NULL caps.
 *
 * Returns: %TRUE if @options obey to @caps, %FALSE otherwise.
 *
 * Since: 0.2.0
 */
gboolean
grl_operation_options_obey_caps (GrlOperationOptions *options,
                                 GrlCaps *caps,
                                 GrlOperationOptions **supported_options,
                                 GrlOperationOptions **unsupported_options)
{
  gboolean ret = TRUE;
  GHashTableIter table_iter;
  gpointer key_ptr;
  GValue *value;
  GrlRangeValue *range_value;

  if (supported_options) {
    *supported_options = grl_operation_options_new (caps);

    /* these options are always supported */
    copy_option (options, *supported_options, GRL_OPERATION_OPTION_SKIP);
    copy_option (options, *supported_options, GRL_OPERATION_OPTION_COUNT);
    copy_option (options, *supported_options, GRL_OPERATION_OPTION_RESOLUTION_FLAGS);
    copy_option (options, *supported_options, GRL_OPERATION_OPTION_REMOVE_FLAGS);
  }

  if (unsupported_options)
    *unsupported_options = grl_operation_options_new (NULL);

  ret &= check_and_copy_option (options,
                                caps,
                                GRL_OPERATION_OPTION_TYPE_FILTER,
                                supported_options,
                                unsupported_options);

  /* Check filter-by-equal-key */
  g_hash_table_iter_init (&table_iter, options->priv->key_filter);
  while (g_hash_table_iter_next (&table_iter, &key_ptr, (gpointer *)&value)) {
    GrlKeyID key_id = GRLPOINTER_TO_KEYID (key_ptr);
    if (grl_caps_is_key_filter (caps, key_id)) {
      if (supported_options) {
        g_hash_table_insert ((*supported_options)->priv->key_filter,
                             key_ptr,
                             grl_g_value_dup (value));
      }
    } else {
      ret = FALSE;
      if (unsupported_options) {
        g_hash_table_insert ((*unsupported_options)->priv->key_filter,
                             key_ptr,
                             grl_g_value_dup (value));
      }
    }
  }

  /* Check filter-by-range-key */
  g_hash_table_iter_init (&table_iter, options->priv->key_range_filter);
  while (g_hash_table_iter_next (&table_iter, &key_ptr, (gpointer *)&range_value)) {
    GrlKeyID key_id = GRLPOINTER_TO_KEYID (key_ptr);
    if (grl_caps_is_key_range_filter (caps, key_id)) {
      if (supported_options) {
        g_hash_table_insert ((*supported_options)->priv->key_range_filter,
                             key_ptr,
                             grl_range_value_dup (range_value));
      }
    } else {
      ret = FALSE;
      if (unsupported_options) {
        g_hash_table_insert ((*unsupported_options)->priv->key_range_filter,
                             key_ptr,
                             grl_range_value_dup (range_value));
      }
    }
  }

  return ret;
}

/**
 * grl_operation_options_copy:
 * @options: a #GrlOperationOptions instance
 *
 * Returns: (transfer full): a new #GrlOperationOptions instance with its values being copies of
 * the values of @options.
 *
 * Since: 0.2.0
 */
GrlOperationOptions *
grl_operation_options_copy (GrlOperationOptions *options)
{
  GrlOperationOptions *copy = grl_operation_options_new (options->priv->caps);

  copy_option (options, copy, GRL_OPERATION_OPTION_SKIP);
  copy_option (options, copy, GRL_OPERATION_OPTION_COUNT);
  copy_option (options, copy, GRL_OPERATION_OPTION_RESOLUTION_FLAGS);
  copy_option (options, copy, GRL_OPERATION_OPTION_REMOVE_FLAGS);
  copy_option (options, copy, GRL_OPERATION_OPTION_TYPE_FILTER);

  g_hash_table_foreach (options->priv->key_filter,
                        (GHFunc) key_filter_dup,
                        copy->priv->key_filter);

  g_hash_table_foreach (options->priv->key_range_filter,
                        (GHFunc) key_range_filter_dup,
                        copy->priv->key_range_filter);

  return copy;
}

/**
 * grl_operation_options_key_is_set:
 * @options: a #GrlOperationOptions instance
 * @key: an operation option key
 *
 * This is an internal method that shouldn't be used outside of Grilo.
 *
 * Returns: whether @key is set in @options.
 *
 * Since: 0.2.0
 */
gboolean
grl_operation_options_key_is_set (GrlOperationOptions *options,
                                  const gchar *key)
{
  return g_hash_table_lookup_extended (options->priv->data, key, NULL, NULL);
}

/**
 * grl_operation_options_set_skip:
 * @options: a #GrlOperationOptions instance
 * @skip: number of elements to skip in an operation
 *
 * Set the skip option for an operation. Will only succeed if @skip obeys to the
 * inherent capabilities of @options.
 *
 * Returns: %TRUE if @skip could be set, %FALSE otherwise.
 *
 * Since: 0.2.0
 */
gboolean
grl_operation_options_set_skip (GrlOperationOptions *options,
                                guint skip)
{
  GValue skip_val = { 0, };

  g_value_init (&skip_val, G_TYPE_UINT);
  g_value_set_uint (&skip_val, skip);

  set_value (options, GRL_OPERATION_OPTION_SKIP, &skip_val);

  g_value_unset (&skip_val);

  return TRUE;
}

/**
 * grl_operation_options_get_skip:
 * @options: a #GrlOperationOptions instance
 *
 * Get the skip option, that is, the number of elements to skip before
 * retrieving media items in an operation done with @options.
 *
 * Returns: the value of the skip option, or a default value if it is not set.
 *
 * Since: 0.2.0
 */
guint
grl_operation_options_get_skip (GrlOperationOptions *options)
{
  const GValue *value  =
      g_hash_table_lookup (options->priv->data, GRL_OPERATION_OPTION_SKIP);

  if (value)
    return g_value_get_uint (value);

  return SKIP_DEFAULT;
}

/**
 * grl_operation_options_set_count:
 * @options: a #GrlOperationOptions instance
 * @count: number of elements to retrieve in an operation
 *
 * Set the count option for an operation. Will only succeed if @count obey to
 * the inherent capabilities of @options.
 *
 * Returns: %TRUE if @count could be set, %FALSE otherwise.
 *
 * Since: 0.2.0
 */
gboolean
grl_operation_options_set_count (GrlOperationOptions *options, gint count)
{
  GValue count_val = { 0, };

  g_value_init (&count_val, G_TYPE_INT);
  g_value_set_int (&count_val, count);

  set_value (options, GRL_OPERATION_OPTION_COUNT, &count_val);

  g_value_unset (&count_val);

  return TRUE;
}

/**
 * grl_operation_options_get_count:
 * @options: a #GrlOperationOptions instance
 *
 * Get the count option, that is, the number of elements to retrieve in an
 * operation done with @options.
 *
 * Returns: the value of the count option, or a default value if it is not set.
 *
 * Since: 0.2.0
 */
gint
grl_operation_options_get_count (GrlOperationOptions *options)
{
  const GValue *value  =
      g_hash_table_lookup (options->priv->data, GRL_OPERATION_OPTION_COUNT);

  if (value)
    return g_value_get_int (value);

  return COUNT_DEFAULT;
}

/**
 * grl_operation_options_set_resolution_flags:
 * @options: a #GrlOperationOptions instance
 * @flags: the resolution flags to be set for an operation. See
 * #GrlResolutionFlags for possible values.
 *
 * Set the resolution flags for an operation. Will only succeed if @flags obey
 * to the inherent capabilities of @options.
 *
 * Returns: %TRUE if @flags could be set, %FALSE otherwise.
 *
 * Since: 0.2.12
 */
gboolean
grl_operation_options_set_resolution_flags (GrlOperationOptions *options,
                                            GrlResolutionFlags flags)
{
  GValue value = { 0, };

  /* FIXME: I think we should use mk_enum to have a GType for
   * GrlResolutionFlags */
  g_value_init (&value, G_TYPE_UINT);
  g_value_set_uint (&value, flags);
  set_value (options, GRL_OPERATION_OPTION_RESOLUTION_FLAGS, &value);
  g_value_unset (&value);

  return TRUE;
}

/**
 * grl_operation_options_get_resolution_flags:
 * @options: a #GrlOperationOptions instance
 *
 * Returns: resolution flags of @options.
 *
 * Since: 0.2.12
 */
GrlResolutionFlags
grl_operation_options_get_resolution_flags (GrlOperationOptions *options)
{
  const GValue *value;

  if (options)
    value = g_hash_table_lookup (options->priv->data,
                                 GRL_OPERATION_OPTION_RESOLUTION_FLAGS);
  else
    value = NULL;

  if (value)
    return g_value_get_uint (value);

  return RESOLUTION_FLAGS_DEFAULT;
}

/**
 * grl_operation_options_set_remove_flags:
 * @options: a #GrlOperationOptions instance
 * @flags: the remove flags to be set for an operation. See
 * #GrlRemoveFlags for possible values.
 *
 * Set the remove flags for a remove operation. Will only succeed if @flags obey
 * to the inherent capabilities of @options.
 *
 * Returns: %TRUE if @flags could be set, %FALSE otherwise.
 *
 * Since: 0.2.0
 */
gboolean
grl_operation_options_set_remove_flags (GrlOperationOptions *options,
                                        GrlRemoveFlags flags)
{
  GValue value = { 0, };

  /* FIXME: I think we should use mk_enum to have a GType for
   * GrlRemoveFlags */
  g_value_init (&value, G_TYPE_UINT);
  g_value_set_uint (&value, flags);
  set_value (options, GRL_OPERATION_OPTION_REMOVE_FLAGS, &value);
  g_value_unset (&value);

  return TRUE;
}

/**
 * grl_operation_options_get_remove_flags:
 * @options: a #GrlOperationOptions instance
 *
 * Returns: remove flags of @options.
 *
 * Since: 0.2.0
 */
GrlRemoveFlags
grl_operation_options_get_remove_flags (GrlOperationOptions *options)
{
  const GValue *value;

  if (options)
    value = g_hash_table_lookup (options->priv->data,
                                 GRL_OPERATION_OPTION_REMOVE_FLAGS);
  else
    value = NULL;

  if (value)
    return g_value_get_uint (value);

  return REMOVE_FLAGS_DEFAULT;
}

/**
 * grl_operation_options_set_type_filter:
 * @options: a #GrlOperationOptions instance
 * @filter: the type of media to get
 *
 * Set the type of media filter for an operation. Only those media elements that
 * match the @filter will be returned. Will only succeed if @filter obey to the
 * inherent capabilities of @options.
 *
 * Returns: %TRUE if @flags could be set, %FALSE otherwise
 *
 * Since: 0.2.0
 **/
gboolean
grl_operation_options_set_type_filter (GrlOperationOptions *options,
                                       GrlTypeFilter filter)
{
  GValue value = { 0 };
  gboolean ret;

  g_value_init (&value, GRL_TYPE_TYPE_FILTER);
  g_value_set_flags (&value, filter);

  ret = (options->priv->caps == NULL) ||
      grl_caps_test_option (options->priv->caps,
                            GRL_OPERATION_OPTION_TYPE_FILTER, &value);

  if (ret)
    set_value (options, GRL_OPERATION_OPTION_TYPE_FILTER, &value);

  g_value_unset (&value);

  return ret;
}

/**
 * grl_operation_options_get_type_filter:
 * @options: a #GrlOperationOptions instance
 *
 *
 * Returns: resolution flags of @options
 *
 * Since: 0.2.0
 **/
GrlTypeFilter
grl_operation_options_get_type_filter (GrlOperationOptions *options)
{
  const GValue *value = g_hash_table_lookup (options->priv->data,
                                             GRL_OPERATION_OPTION_TYPE_FILTER);

  if (value) {
    return g_value_get_flags (value);
  }

  return TYPE_FILTER_DEFAULT;
}

/**
 * grl_operation_options_set_key_filter_value:
 * @options: a #GrlOperationOptions instance
 * @key: a #GrlKeyID
 * @value: a #GValue
 *
 * Set filter as "@key == @value".
 *
 * Returns: %TRUE on success
 *
 * Since: 0.2.0
 **/
gboolean
grl_operation_options_set_key_filter_value (GrlOperationOptions *options,
                                            GrlKeyID key,
                                            GValue *value)
{
  gboolean ret;
  GrlRegistry *registry;
  GType key_type;

  registry = grl_registry_get_default ();
  key_type = grl_registry_lookup_metadata_key_type (registry, key);

  if (G_VALUE_TYPE (value) != key_type)
    return FALSE;

  ret = (options->priv->caps == NULL) ||
    grl_caps_is_key_filter (options->priv->caps, key);

  if (ret) {
    if (value) {
      g_hash_table_insert (options->priv->key_filter,
                           GRLKEYID_TO_POINTER (key),
                           grl_g_value_dup (value));
    } else {
      g_hash_table_remove (options->priv->key_filter,
                           GRLKEYID_TO_POINTER (key));
    }
  }

  return ret;
}

/**
 * grl_operation_options_set_key_filters:
 * @options: a #GrlOperationOptions instance
 * @...: pairs of #GrlKeyID, value
 *
 * Set filter as "k1 == v1 AND k2 == v2 AND ..."
 *
 * <example>
 *  Elements from album "Frozen" with a bitrate of 256kbs.
 *  <programlisting>
 *   grl_operation_options_set_key_filters (my_options,
 *                                          GRL_METADATA_KEY_ALBUM, "Frozen",
 *                                          GRL_METADATA_KEY_BITRATE, 256,
 *                                          NULL);
 *  </programlisting>
 * </example>
 *
 * Returns: %TRUE on success
 *
 * Since: 0.2.0
 **/
gboolean
grl_operation_options_set_key_filters (GrlOperationOptions *options,
                                       ...)
{
  GType key_type;
  GValue value = { 0 };
  GrlKeyID next_key;
  gboolean skip;
  gboolean success = TRUE;
  va_list args;

  va_start (args, options);
  next_key = va_arg (args, GrlKeyID);
  while (next_key) {
    key_type = GRL_METADATA_KEY_GET_TYPE (next_key);
    g_value_init (&value, key_type);
    skip = FALSE;
    if (key_type == G_TYPE_STRING) {
      g_value_set_string (&value, va_arg (args, gchar *));
    } else if (key_type == G_TYPE_INT) {
      g_value_set_int (&value, va_arg (args, gint));
    } else if (key_type == G_TYPE_BOOLEAN) {
      g_value_set_boolean (&value, va_arg (args, gboolean));
    } else if (key_type == G_TYPE_DATE_TIME) {
      g_value_set_boxed (&value, va_arg (args, gconstpointer));
    } else {
      GRL_WARNING ("Unexpected key type when setting up the filter");
      success = FALSE;
      skip = TRUE;
    }

    if (!skip) {
      success &= grl_operation_options_set_key_filter_value (options,
                                                             next_key,
                                                             &value);
    }

    g_value_unset (&value);
    next_key = va_arg (args, GrlKeyID);
  }

  va_end (args);

  return success;
}

/**
 * grl_operation_options_set_key_filter_dictionary: (rename-to grl_operation_options_set_key_filters)
 * @options: a #GrlOperationOptions instance
 * @filters: (transfer none) (element-type GrlKeyID GValue):
 *
 * Returns: %TRUE on success
 *
 * Since: 0.2.0
 */
gboolean
grl_operation_options_set_key_filter_dictionary (GrlOperationOptions *options,
                                                 GHashTable *filters)
{
  GHashTableIter iter;
  gpointer _key, _value;
  gboolean ret = TRUE;

  g_hash_table_iter_init (&iter, filters);
  while (g_hash_table_iter_next (&iter, &_key, &_value)) {
    GrlKeyID key = GRLPOINTER_TO_KEYID (_key);
    GValue *value = (GValue *)_value;
    ret &=
        grl_operation_options_set_key_filter_value (options, key, value);
  }

  return ret;
}

/**
 * grl_operation_options_get_key_filter:
 * @options: a #GrlOperationOptions instance
 * @key:
 *
 * Returns: (transfer none): the filter
 *
 * Since: 0.2.0
 */
GValue *
grl_operation_options_get_key_filter (GrlOperationOptions *options,
                                      GrlKeyID key)
{
  return g_hash_table_lookup (options->priv->key_filter,
                              GRLKEYID_TO_POINTER (key));
}

/**
 * grl_operation_options_get_key_filter_list:
 * @options: a #GrlOperationOptions instance
 *
 * Returns: (transfer container) (element-type GrlKeyID):
 *
 * Since: 0.2.0
 */
GList *
grl_operation_options_get_key_filter_list (GrlOperationOptions *options)
{
  return g_hash_table_get_keys (options->priv->key_filter);
}

/**
 * grl_operation_options_set_key_range_filter_value:
 * @options: a #GrlOperationOptions instance
 * @key: a #GrlKeyID
 * @min_value: (in) (allow-none): minimum value for range
 * @max_value: (in) (allow-none): maximum value for range
 *
 * Set filter as "@min_value <= @key <= @max_value".
 *
 * If @min_value is %NULL, then filter is "@key <= @max_value".
 *
 * If @max_value is %NULL, then filter is "@key >= @min_value".
 *
 * Returns: %TRUE on success
 *
 * Since: 0.2.0
 **/
gboolean
grl_operation_options_set_key_range_filter_value (GrlOperationOptions *options,
                                                  GrlKeyID key,
                                                  GValue *min_value,
                                                  GValue *max_value)
{
  gboolean ret;

  ret = (options->priv->caps == NULL) ||
    grl_caps_is_key_range_filter (options->priv->caps, key);

  if (ret) {
    if (min_value || max_value) {
      grl_range_value_hashtable_insert (options->priv->key_range_filter,
                                        GRLKEYID_TO_POINTER (key),
                                        min_value, max_value);
    } else {
      g_hash_table_remove (options->priv->key_range_filter,
                           GRLKEYID_TO_POINTER (key));
    }
  }

  return ret;
}

/**
 * grl_operation_options_set_key_range_filter:
 * @options: a #GrlOperationOptions instance
 * @...: triplets of #GrlKeyID, minvalue, maxvalue
 *
 * Set filter as "min1 <= k1 <= max1 AND min2 <= k2 <= max2 AND ..."
 *
 * The range can be open if some of the minX, maxX values are %NULL.

 * <example>
 *  Album must start with "T" and the bitrate should be 256kbs or greater.
 *  <programlisting>
 *   grl_operation_options_set_key_range_filters (my_options,
 *                                                GRL_METADATA_KEY_ALBUM, "Ta", "Tz",
 *                                                GRL_METADATA_KEY_BITRATE, 256, NULL,
 *                                                NULL);
 *  </programlisting>
 * </example>
 *
 * Returns: %TRUE on success
 *
 * Since: 0.2.0
 **/
gboolean
grl_operation_options_set_key_range_filter (GrlOperationOptions *options,
                                            ...)
{
  GType key_type;
  GValue min_value = { 0 };
  GValue *min_p_value;
  gint min_int_value;
  gchar *min_str_value;
  gfloat min_float_value;
  gconstpointer min_date_value;
  GValue max_value = { 0 };
  GValue *max_p_value;
  gint max_int_value;
  gchar *max_str_value;
  gfloat max_float_value;
  gconstpointer max_date_value;
  GrlKeyID next_key;
  gboolean skip;
  gboolean success = TRUE;
  va_list args;

  va_start (args, options);
  next_key = va_arg (args, GrlKeyID);
  while (next_key) {
    key_type = GRL_METADATA_KEY_GET_TYPE (next_key);
    g_value_init (&min_value, key_type);
    g_value_init (&max_value, key_type);
    min_p_value = NULL;
    max_p_value = NULL;
    skip = FALSE;
    if (key_type == G_TYPE_STRING) {
      min_str_value = va_arg (args, gchar *);
      max_str_value = va_arg (args, gchar *);
      if (min_str_value) {
        g_value_set_string (&min_value, min_str_value);
        min_p_value = &min_value;
      }
      if (max_str_value) {
        g_value_set_string (&max_value, max_str_value);
        max_p_value = &max_value;
      }
    } else if (key_type == G_TYPE_INT) {
      min_int_value = va_arg (args, gint);
      max_int_value = va_arg (args, gint);
      if (min_int_value > G_MININT) {
        g_value_set_int (&min_value, min_int_value);
        min_p_value = &min_value;
      }
      if (max_int_value < G_MAXINT) {
        g_value_set_int (&max_value, max_int_value);
        max_p_value = &max_value;
      }
    } else if (key_type == G_TYPE_FLOAT) {
      min_float_value = va_arg (args, gdouble);
      max_float_value = va_arg (args, gdouble);
      if (min_float_value > G_MINFLOAT) {
        g_value_set_float (&min_value, min_float_value);
        min_p_value = &min_value;
      }
      if (max_float_value < G_MAXFLOAT) {
        g_value_set_float (&max_value, max_float_value);
        max_p_value = &max_value;
      }
    } else if (key_type == G_TYPE_DATE_TIME) {
      min_date_value = va_arg (args, gconstpointer);
      max_date_value = va_arg (args, gconstpointer);
      if (min_date_value) {
        g_value_set_boxed (&min_value, min_date_value);
        min_p_value = &min_value;
      }
      if (max_date_value) {
        g_value_set_boxed (&max_value, max_date_value);
        max_p_value = &max_value;
      }
    } else {
      GRL_WARNING ("Unexpected key type when setting up the filter");
      success = FALSE;
      skip = TRUE;
    }

    if (!skip) {
      success &= grl_operation_options_set_key_range_filter_value (options,
                                                                   next_key,
                                                                   min_p_value,
                                                                   max_p_value);
    }

    g_value_unset (&min_value);
    g_value_unset (&max_value);
    next_key = va_arg (args, GrlKeyID);
  }

  va_end (args);

  return success;
}

/**
 * grl_operation_options_get_key_range_filter:
 * @options: a #GrlOperationOptions instance
 * @key: a #GrlKeyID
 * @min_value: (out) (allow-none) (transfer none): the minimum value for the range
 * @max_value: (out) (allow-none) (transfer none): the maximum value for the range
 *
 * Stores the limits of the range in the filter for @key in @min_value and
 * @max_value. If some of the values has no limit, it will set a %NULL.
 *
 * Since: 0.2.0
 **/
void
grl_operation_options_get_key_range_filter (GrlOperationOptions *options,
                                            GrlKeyID key,
                                            GValue **min_value,
                                            GValue **max_value)
{
  GrlRangeValue *range =
    (GrlRangeValue *) g_hash_table_lookup (options->priv->key_range_filter,
                                           GRLKEYID_TO_POINTER (key));

  if (min_value) {
    if (range && range->min) {
      *min_value = range->min;
    } else {
      *min_value = NULL;
    }
  }

  if (max_value) {
    if (range && range->max) {
      *max_value = range->max;
    } else {
      *max_value = NULL;
    }
  }
}

/**
 * grl_operation_options_get_key_range_filter_list:
 * @options: a #GrlOperationOptions instance
 *
 * Returns: (transfer container) (element-type GrlKeyID):
 *
 * Since: 0.2.0
 */
GList *
grl_operation_options_get_key_range_filter_list (GrlOperationOptions *options)
{
  return g_hash_table_get_keys (options->priv->key_range_filter);
}