summaryrefslogtreecommitdiffstats
path: root/panels/keyboard/cc-keyboard-item.c
blob: 853c5986b57327fa8d8dca76383466f7f842ccec (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2011, 2014 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>

#include <gtk/gtk.h>
#include <gio/gio.h>
#include <glib/gi18n-lib.h>

#include "cc-keyboard-item.h"

#define CUSTOM_KEYS_SCHEMA "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding"

struct _CcKeyboardItem
{
  GObject parent_instance;

  char *binding;

  CcKeyboardItem *reverse_item;
  gboolean is_reversed;
  gboolean hidden;

  CcKeyboardItemType type;

  CcKeyCombo *primary_combo;
  BindingGroupType group;
  char *description;
  gboolean editable;
  GList *key_combos;
  GList *default_combos;

  /* GSettings path */
  char *gsettings_path;
  gboolean desc_editable;
  char *command;
  gboolean cmd_editable;

  /* GSettings */
  char *schema;
  char *key;
  GSettings *settings;
};

enum
{
  PROP_0,
  PROP_DESCRIPTION,
  PROP_BINDING,
  PROP_EDITABLE,
  PROP_TYPE,
  PROP_IS_VALUE_DEFAULT,
  PROP_COMMAND
};

static void     cc_keyboard_item_class_init     (CcKeyboardItemClass *klass);
static void     cc_keyboard_item_init           (CcKeyboardItem      *keyboard_item);
static void     cc_keyboard_item_finalize       (GObject               *object);

G_DEFINE_TYPE (CcKeyboardItem, cc_keyboard_item, G_TYPE_OBJECT)

static const gchar *
get_binding_from_variant (GVariant *variant)
{
  const char *str, **strv;

  if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
    return g_variant_get_string (variant, NULL);
  else if (!g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING_ARRAY))
    return "";

  strv = g_variant_get_strv (variant, NULL);
  str = strv[0];
  g_free (strv);

  return str;
}

static gboolean
binding_from_string (const char *str,
                     CcKeyCombo *combo)
{
  g_return_val_if_fail (combo != NULL, FALSE);
  guint *keycodes;

  if (str == NULL || strcmp (str, "disabled") == 0)
    {
      memset (combo, 0, sizeof(CcKeyCombo));
      return TRUE;
    }

  gtk_accelerator_parse_with_keycode (str, &combo->keyval, &keycodes, &combo->mask);

  combo->keycode = (keycodes ? keycodes[0] : 0);
  g_free (keycodes);

  if (combo->keyval == 0)
    return FALSE;
  else
    return TRUE;
}

static void
_set_description (CcKeyboardItem *item,
                  const char       *value)
{
  g_free (item->description);
  item->description = g_strdup (value);
}

const char *
cc_keyboard_item_get_description (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);

  return item->description;
}

gboolean
cc_keyboard_item_get_desc_editable (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), FALSE);

  return item->desc_editable;
}

/* wrapper around g_settings_set_str[ing|v] */
static void
settings_set_binding (GSettings  *settings,
                      const char *key,
		      const char *value)
{
  GVariant *variant;

  variant = g_settings_get_value (settings, key);

  if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
    g_settings_set_string (settings, key, value ? value : "");
  else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING_ARRAY))
    {
      if (value == NULL || *value == '\0')
        g_settings_set_strv (settings, key, NULL);
      else
        {
          char **str_array = g_new0 (char *, 2);

          /* clear any additional bindings by only setting the first one */
          *str_array = g_strdup (value);

          g_settings_set_strv (settings, key, (const char * const *)str_array);
          g_strfreev (str_array);
        }
    }

  g_variant_unref (variant);
}


static void
_set_binding (CcKeyboardItem *item,
              const char     *value,
	      gboolean        set_backend)
{
  CcKeyboardItem *reverse;
  gboolean enabled;

  reverse = item->reverse_item;
  enabled = value && strlen (value) > 0;

  g_clear_pointer (&item->binding, g_free);
  item->binding = enabled ? g_strdup (value) : g_strdup ("");

  binding_from_string (item->binding, item->primary_combo);

  /*
   * Always treat the pair (item, reverse) as a unit: setting one also
   * disables the other, disabling one up also sets the other.
   */
  if (reverse)
    {
      GdkModifierType reverse_mask;

      reverse_mask = enabled ? item->primary_combo->mask ^ GDK_SHIFT_MASK
                             : item->primary_combo->mask;

      g_clear_pointer (&reverse->binding, g_free);
      if (enabled)
        reverse->binding = gtk_accelerator_name_with_keycode (NULL,
                                                                    item->primary_combo->keyval,
                                                                    item->primary_combo->keycode,
                                                                    reverse_mask);

      binding_from_string (reverse->binding, reverse->primary_combo);
    }

  if (set_backend == FALSE)
    return;

  settings_set_binding (item->settings, item->key, item->binding);

  g_object_notify (G_OBJECT (item), "is-value-default");

  if (reverse)
    {
      settings_set_binding (reverse->settings, reverse->key, reverse->binding);
      g_object_notify (G_OBJECT (reverse), "is-value-default");
    }
}

static void
_set_type (CcKeyboardItem *item,
           gint            value)
{
  item->type = value;
}

static void
_set_command (CcKeyboardItem *item,
              const char       *value)
{
  g_free (item->command);
  item->command = g_strdup (value);
}

const char *
cc_keyboard_item_get_command (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);

  return item->command;
}

gboolean
cc_keyboard_item_get_cmd_editable (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), FALSE);

  return item->cmd_editable;
}

static void
cc_keyboard_item_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  CcKeyboardItem *self;

  self = CC_KEYBOARD_ITEM (object);

  switch (prop_id) {
  case PROP_DESCRIPTION:
    _set_description (self, g_value_get_string (value));
    break;
  case PROP_BINDING:
    _set_binding (self, g_value_get_string (value), TRUE);
    break;
  case PROP_COMMAND:
    _set_command (self, g_value_get_string (value));
    break;
  case PROP_TYPE:
    _set_type (self, g_value_get_int (value));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    break;
  }
}

static void
cc_keyboard_item_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  CcKeyboardItem *self;

  self = CC_KEYBOARD_ITEM (object);

  switch (prop_id) {
  case PROP_DESCRIPTION:
    g_value_set_string (value, self->description);
    break;
  case PROP_BINDING:
    g_value_set_string (value, self->binding);
    break;
  case PROP_EDITABLE:
    g_value_set_boolean (value, self->editable);
    break;
  case PROP_COMMAND:
    g_value_set_string (value, self->command);
    break;
  case PROP_IS_VALUE_DEFAULT:
    g_value_set_boolean (value, cc_keyboard_item_is_value_default (self));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    break;
  }
}

static void
cc_keyboard_item_class_init (CcKeyboardItemClass *klass)
{
  GObjectClass  *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = cc_keyboard_item_get_property;
  object_class->set_property = cc_keyboard_item_set_property;
  object_class->finalize = cc_keyboard_item_finalize;

  g_object_class_install_property (object_class,
                                   PROP_DESCRIPTION,
                                   g_param_spec_string ("description",
                                                        "description",
                                                        "description",
                                                        NULL,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_BINDING,
                                   g_param_spec_string ("binding",
                                                        "binding",
                                                        "binding",
                                                        NULL,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_EDITABLE,
                                   g_param_spec_boolean ("editable",
                                                         NULL,
                                                         NULL,
                                                         FALSE,
                                                         G_PARAM_READABLE));

  g_object_class_install_property (object_class,
                                   PROP_TYPE,
                                   g_param_spec_int ("type",
                                                     NULL,
                                                     NULL,
                                                     CC_KEYBOARD_ITEM_TYPE_NONE,
                                                     CC_KEYBOARD_ITEM_TYPE_GSETTINGS,
                                                     CC_KEYBOARD_ITEM_TYPE_NONE,
                                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_COMMAND,
                                   g_param_spec_string ("command",
                                                        "command",
                                                        "command",
                                                        NULL,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (object_class,
                                   PROP_IS_VALUE_DEFAULT,
                                   g_param_spec_boolean ("is-value-default",
                                                         "is value default",
                                                         "is value default",
                                                         TRUE,
                                                         G_PARAM_READABLE));
}

static void
cc_keyboard_item_init (CcKeyboardItem *item)
{
  item->primary_combo = g_new0 (CcKeyCombo, 1);
}

static void
cc_keyboard_item_finalize (GObject *object)
{
  CcKeyboardItem *item;

  g_return_if_fail (object != NULL);
  g_return_if_fail (CC_IS_KEYBOARD_ITEM (object));

  item = CC_KEYBOARD_ITEM (object);

  if (item->settings != NULL)
    g_object_unref (item->settings);

  /* Free memory */
  g_free (item->binding);
  g_free (item->primary_combo);
  g_free (item->gsettings_path);
  g_free (item->description);
  g_free (item->command);
  g_free (item->schema);
  g_free (item->key);
  g_list_free_full (item->key_combos, g_free);
  g_list_free_full (item->default_combos, g_free);

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

CcKeyboardItem *
cc_keyboard_item_new (CcKeyboardItemType type)
{
  GObject *object;

  object = g_object_new (CC_TYPE_KEYBOARD_ITEM,
                         "type", type,
                         NULL);

  return CC_KEYBOARD_ITEM (object);
}

static guint *
get_above_tab_keysyms (void)
{
  GdkKeymap *keymap = gdk_keymap_get_for_display (gdk_display_get_default ());
  guint keycode = 0x29 /* KEY_GRAVE */ + 8;
  g_autofree guint *keyvals = NULL;
  GArray *keysyms;
  int n_entries, i, j;

  keysyms = g_array_new (TRUE, FALSE, sizeof (guint));

  if (!gdk_keymap_get_entries_for_keycode (keymap, keycode, NULL, &keyvals, &n_entries))
    goto out;

  for (i = 0; i < n_entries; i++)
    {
      gboolean found = FALSE;

      for (j = 0; j < keysyms->len; j++)
        if (g_array_index (keysyms, guint, j) == keyvals[i])
          {
            found = TRUE;
            break;
          }

      if (!found)
        g_array_append_val (keysyms, keyvals[i]);
    }

out:
  return (guint *)g_array_free (keysyms, FALSE);
}

/*
 * translate_above_tab:
 *
 * @original_bindings: A list of accelerator strings
 * @new_bindings: (out): Translated bindings if translation is needed
 *
 * Translate accelerator strings that contain the Above_Tab fake keysym
 * used by mutter to strings that use the real keysyms that correspond
 * to the key that is located physically above the tab key.
 *
 * Returns: %TRUE if strings were translated, %FALSE if @original_bindings
 *   can be used unmodified
 */
static gboolean
translate_above_tab (char **original_bindings,
                     char ***new_bindings)
{
  GPtrArray *replaced_bindings;
  g_autofree guint *above_tab_keysyms = NULL;
  gboolean needs_translation = FALSE;
  char **str;

  for (str = original_bindings; *str && !needs_translation; str++)
    needs_translation = strstr (*str, "Above_Tab") != NULL;

  if (!needs_translation)
    return FALSE;

  above_tab_keysyms = get_above_tab_keysyms ();

  replaced_bindings = g_ptr_array_new ();

  for (str = original_bindings; *str; str++)
    {
      if (strstr (*str, "Above_Tab") == NULL)
        {
          g_ptr_array_add (replaced_bindings, g_strdup (*str));
        }
      else
        {
          g_auto (GStrv) split_str = g_strsplit (*str, "Above_Tab", -1);
          int i;

          for (i = 0; above_tab_keysyms[i]; i++)
            {
              g_autofree char *sym = NULL;

              sym = gtk_accelerator_name (above_tab_keysyms[i], 0);
              g_ptr_array_add (replaced_bindings, g_strjoinv (sym, split_str));
            }
        }
      g_ptr_array_add (replaced_bindings, NULL);
    }

  *new_bindings = (char **)g_ptr_array_free (replaced_bindings, FALSE);
  return TRUE;
}

static char *
translate_binding_string (const char *str)
{
  g_autofree guint *above_tab_keysyms = NULL;
  g_autofree char *symname = NULL;
  g_auto (GStrv) split_str = NULL;

  if (str == NULL || strstr (str, "Above_Tab") == NULL)
    return g_strdup (str);

  above_tab_keysyms = get_above_tab_keysyms ();
  symname = gtk_accelerator_name (above_tab_keysyms[0], 0);

  split_str = g_strsplit (str, "Above_Tab", -1);
  return g_strjoinv (symname, split_str);
}

static GList *
variant_get_key_combos (GVariant *variant)
{
  GList *combos = NULL;
  char **bindings = NULL, **translated_bindings, **str;

  if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
    {
      bindings = g_malloc0_n (2, sizeof(char *));
      bindings[0] = g_variant_dup_string (variant, NULL);
    }
  else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING_ARRAY))
    {
      bindings = g_variant_dup_strv (variant, NULL);
    }

  if (translate_above_tab (bindings, &translated_bindings))
    {
      g_strfreev (bindings);
      bindings = translated_bindings;
    }

  for (str = bindings; *str; str++)
    {
      CcKeyCombo *combo = g_new (CcKeyCombo, 1);

      binding_from_string (*str, combo);
      combos = g_list_prepend (combos, combo);
    }
  g_strfreev (bindings);

  return g_list_reverse (combos);
}

static GList *
settings_get_key_combos (GSettings  *settings,
                         const char *key,
                         gboolean    use_default)
{
  GList *key_combos;
  GVariant *variant;

  if (use_default)
    variant = g_settings_get_default_value (settings, key);
  else
    variant = g_settings_get_value (settings, key);
  key_combos = variant_get_key_combos (variant);
  g_variant_unref (variant);

  return key_combos;
}

/* wrapper around g_settings_get_str[ing|v] */
static char *
settings_get_binding (GSettings  *settings,
                      const char *key)
{
  GVariant *variant;
  char *value = NULL;

  variant = g_settings_get_value (settings, key);
  if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
    value = translate_binding_string (g_variant_get_string (variant, NULL));
  else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING_ARRAY))
    {
      const char **str_array;

      str_array = g_variant_get_strv (variant, NULL);
      value = translate_binding_string (str_array[0]);
      g_free (str_array);
    }
  g_variant_unref (variant);

  return value;
}

static void
binding_changed (CcKeyboardItem *item,
		 const char *key)
{
  char *value;

  g_list_free_full (item->key_combos, g_free);
  item->key_combos = settings_get_key_combos (item->settings, item->key, FALSE);

  value = settings_get_binding (item->settings, item->key);
  item->editable = g_settings_is_writable (item->settings, item->key);
  _set_binding (item, value, FALSE);
  g_free (value);
  g_object_notify (G_OBJECT (item), "binding");
}

gboolean
cc_keyboard_item_load_from_gsettings_path (CcKeyboardItem *item,
                                           const char     *path,
                                           gboolean        reset)
{
  item->schema = g_strdup (CUSTOM_KEYS_SCHEMA);
  item->gsettings_path = g_strdup (path);
  item->key = g_strdup ("binding");
  item->settings = g_settings_new_with_path (item->schema, path);
  item->editable = g_settings_is_writable (item->settings, item->key);
  item->desc_editable = g_settings_is_writable (item->settings, "name");
  item->cmd_editable = g_settings_is_writable (item->settings, "command");

  if (reset)
    {
      g_settings_reset (item->settings, "name");
      g_settings_reset (item->settings, "command");
      g_settings_reset (item->settings, "binding");
    }

  g_settings_bind (item->settings, "name",
                   G_OBJECT (item), "description", G_SETTINGS_BIND_DEFAULT);
  g_settings_bind (item->settings, "command",
                   G_OBJECT (item), "command", G_SETTINGS_BIND_DEFAULT);

  g_list_free_full (item->key_combos, g_free);
  item->key_combos = settings_get_key_combos (item->settings, item->key, FALSE);

  g_free (item->binding);
  item->binding = settings_get_binding (item->settings, item->key);
  binding_from_string (item->binding, item->primary_combo);
  g_signal_connect_object (G_OBJECT (item->settings), "changed::binding",
                           G_CALLBACK (binding_changed), item, G_CONNECT_SWAPPED);

  return TRUE;
}

gboolean
cc_keyboard_item_load_from_gsettings (CcKeyboardItem *item,
				      const char *description,
				      const char *schema,
				      const char *key)
{
  char *signal_name;

  item->schema = g_strdup (schema);
  item->key = g_strdup (key);
  item->description = g_strdup (description);

  item->settings = g_settings_new (item->schema);
  g_free (item->binding);
  item->binding = settings_get_binding (item->settings, item->key);
  item->editable = g_settings_is_writable (item->settings, item->key);
  binding_from_string (item->binding, item->primary_combo);

  g_list_free_full (item->key_combos, g_free);
  item->key_combos = settings_get_key_combos (item->settings, item->key, FALSE);

  g_list_free_full (item->default_combos, g_free);
  item->default_combos = settings_get_key_combos (item->settings, item->key, TRUE);

  signal_name = g_strdup_printf ("changed::%s", item->key);
  g_signal_connect_object (G_OBJECT (item->settings), signal_name,
                           G_CALLBACK (binding_changed), item, G_CONNECT_SWAPPED);
  g_free (signal_name);

  return TRUE;
}

gboolean
cc_keyboard_item_equal (CcKeyboardItem *a,
			CcKeyboardItem *b)
{
  if (a->type != b->type)
    return FALSE;
  switch (a->type)
    {
      case CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH:
	return g_str_equal (a->gsettings_path, b->gsettings_path);
      case CC_KEYBOARD_ITEM_TYPE_GSETTINGS:
	return (g_str_equal (a->schema, b->schema) &&
		g_str_equal (a->key, b->key));
      default:
	g_assert_not_reached ();
    }

}

void
cc_keyboard_item_add_reverse_item (CcKeyboardItem *item,
				   CcKeyboardItem *reverse_item,
				   gboolean is_reversed)
{
  g_return_if_fail (item->key != NULL);

  item->reverse_item = reverse_item;
  if (reverse_item->reverse_item == NULL)
    {
      reverse_item->reverse_item = item;
      reverse_item->is_reversed = !is_reversed;
    }
  else
    g_warn_if_fail (reverse_item->is_reversed == !!is_reversed);

  item->is_reversed = !!is_reversed;
}

CcKeyboardItem *
cc_keyboard_item_get_reverse_item (CcKeyboardItem *item)
{
  return item->reverse_item;
}


void
cc_keyboard_item_set_hidden (CcKeyboardItem *item, gboolean hidden)
{
  item->hidden = !!hidden;
}


gboolean
cc_keyboard_item_is_hidden (CcKeyboardItem *item)
{
  return item->hidden;
}

/**
 * cc_keyboard_item_is_value_default:
 * @self: a #CcKeyboardItem
 *
 * Retrieves whether the shortcut is the default value or not.
 *
 * Returns: %TRUE if the shortcut is the default value, %FALSE otherwise.
 */
gboolean
cc_keyboard_item_is_value_default (CcKeyboardItem *self)
{
  GVariant *user_value;
  gboolean is_value_default;

  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (self), FALSE);

  /*
   * When the shortcut is custom, we don't treat it as modified
   * since we don't know what would be its default value.
   */
  if (self->type == CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH)
    return TRUE;

  user_value = g_settings_get_user_value (self->settings, self->key);

  is_value_default = TRUE;

  if (user_value)
    {
      GVariant *default_value;
      const gchar *default_binding, *user_binding;

      default_value = g_settings_get_default_value (self->settings, self->key);

      default_binding = get_binding_from_variant (default_value);
      user_binding = get_binding_from_variant (user_value);

      is_value_default = (g_strcmp0 (default_binding, user_binding) == 0);

      g_clear_pointer (&default_value, g_variant_unref);
    }

  g_clear_pointer (&user_value, g_variant_unref);

  return is_value_default;
}

/**
 * cc_keyboard_item_reset:
 * @self: a #CcKeyboardItem
 *
 * Reset the keyboard binding to the default value.
 */
void
cc_keyboard_item_reset (CcKeyboardItem *self)
{
  CcKeyboardItem *reverse;

  g_return_if_fail (CC_IS_KEYBOARD_ITEM (self));

  reverse = self->reverse_item;

  g_settings_reset (self->settings, self->key);
  g_object_notify (G_OBJECT (self), "is-value-default");

  /* Also reset the reverse item */
  if (reverse)
    {
      g_settings_reset (reverse->settings, reverse->key);
      g_object_notify (G_OBJECT (reverse), "is-value-default");
    }
}

GList *
cc_keyboard_item_get_key_combos (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
  return item->key_combos;
}

GList *
cc_keyboard_item_get_default_combos (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
  return item->default_combos;
}

CcKeyCombo *
cc_keyboard_item_get_primary_combo (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
  return item->primary_combo;
}

const gchar *
cc_keyboard_item_get_key (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
  return item->key;
}

CcKeyboardItemType
cc_keyboard_item_get_item_type (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), CC_KEYBOARD_ITEM_TYPE_NONE);
  return item->type;
}

const gchar *
cc_keyboard_item_get_gsettings_path (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
  return item->gsettings_path;
}

GSettings *
cc_keyboard_item_get_settings (CcKeyboardItem *item)
{
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
  return item->settings;
}