~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/keyboard/cc-keyboard-item.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2011 Red Hat, Inc.
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
 
 
26
#include <gtk/gtk.h>
 
27
#include <gio/gio.h>
 
28
#include <glib/gi18n-lib.h>
 
29
 
 
30
#include "cc-keyboard-item.h"
 
31
 
 
32
#define CC_KEYBOARD_ITEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_KEYBOARD_ITEM, CcKeyboardItemPrivate))
 
33
 
 
34
#define CUSTOM_KEYS_SCHEMA "org.gnome.settings-daemon.plugins.media-keys.custom-keybinding"
 
35
 
 
36
struct CcKeyboardItemPrivate
 
37
{
 
38
  /* properties */
 
39
  int foo;
 
40
 
 
41
  /* internal */
 
42
};
 
43
 
 
44
enum {
 
45
  PROP_0,
 
46
  PROP_DESCRIPTION,
 
47
  PROP_BINDING,
 
48
  PROP_EDITABLE,
 
49
  PROP_TYPE,
 
50
  PROP_COMMAND
 
51
};
 
52
 
 
53
static void     cc_keyboard_item_class_init     (CcKeyboardItemClass *klass);
 
54
static void     cc_keyboard_item_init           (CcKeyboardItem      *keyboard_item);
 
55
static void     cc_keyboard_item_finalize       (GObject               *object);
 
56
 
 
57
G_DEFINE_TYPE (CcKeyboardItem, cc_keyboard_item, G_TYPE_OBJECT)
 
58
 
 
59
static gboolean
 
60
binding_from_string (const char             *str,
 
61
                     guint                  *accelerator_key,
 
62
                     guint                  *keycode,
 
63
                     GdkModifierType        *accelerator_mods)
 
64
{
 
65
  g_return_val_if_fail (accelerator_key != NULL, FALSE);
 
66
  guint *keycodes;
 
67
 
 
68
  if (str == NULL || strcmp (str, "disabled") == 0)
 
69
    {
 
70
      *accelerator_key = 0;
 
71
      *keycode = 0;
 
72
      *accelerator_mods = 0;
 
73
      return TRUE;
 
74
    }
 
75
 
 
76
  gtk_accelerator_parse_with_keycode (str, accelerator_key, &keycodes, accelerator_mods);
 
77
 
 
78
  if (keycode != NULL)
 
79
    *keycode = (keycodes ? keycodes[0] : 0);
 
80
  g_free (keycodes);
 
81
 
 
82
  if (*accelerator_key == 0)
 
83
    return FALSE;
 
84
  else
 
85
    return TRUE;
 
86
}
 
87
 
 
88
static void
 
89
_set_description (CcKeyboardItem *item,
 
90
                  const char       *value)
 
91
{
 
92
  g_free (item->description);
 
93
  item->description = g_strdup (value);
 
94
}
 
95
 
 
96
const char *
 
97
cc_keyboard_item_get_description (CcKeyboardItem *item)
 
98
{
 
99
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
 
100
 
 
101
  return item->description;
 
102
}
 
103
 
 
104
/* wrapper around g_settings_set_str[ing|v] */
 
105
static void
 
106
settings_set_binding (GSettings  *settings,
 
107
                      const char *key,
 
108
                      const char *value)
 
109
{
 
110
  GVariant *variant;
 
111
 
 
112
  variant = g_settings_get_value (settings, key);
 
113
 
 
114
  if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
 
115
    g_settings_set_string (settings, key, value);
 
116
  else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING_ARRAY))
 
117
    {
 
118
      char **str_array;
 
119
 
 
120
      str_array = g_variant_dup_strv (variant, NULL);
 
121
 
 
122
      /* create a space for the new binding if empty */
 
123
      if (*str_array == NULL)
 
124
        {
 
125
          g_free (str_array);
 
126
          str_array = g_new0 (char *, 2);
 
127
        }
 
128
 
 
129
      /* replace the first binding */
 
130
      g_free (*str_array);
 
131
      *str_array = g_strdup (value);
 
132
 
 
133
      g_settings_set_strv (settings, key, (const char * const *)str_array);
 
134
      g_strfreev (str_array);
 
135
    }
 
136
 
 
137
  g_variant_unref (variant);
 
138
}
 
139
 
 
140
 
 
141
static void
 
142
_set_binding (CcKeyboardItem *item,
 
143
              const char     *value,
 
144
              gboolean        set_backend)
 
145
{
 
146
  /* don't reassign <Alt_L> or <Alt> key in the callback to the binding itself (as it's invalid for the cell renderer) */
 
147
  if ((g_strcmp0 (value, "<Alt_L>") == 0) || (g_strcmp0 (value, "<Alt>") == 0))
 
148
    return;
 
149
 
 
150
  g_free (item->binding);
 
151
  item->binding = g_strdup (value);
 
152
  binding_from_string (item->binding, &item->keyval, &item->keycode, &item->mask);
 
153
 
 
154
  const char *key;
 
155
  char *cheated_modifier = NULL;
 
156
  if (g_strcmp0 (item->binding, "Alt_L") == 0)
 
157
    cheated_modifier = g_strdup_printf ("<%s>", item->binding);
 
158
 
 
159
  if (set_backend == FALSE)
 
160
    return;
 
161
 
 
162
  settings_set_binding (item->settings, item->key, cheated_modifier ? cheated_modifier: item->binding);
 
163
 
 
164
  g_free (cheated_modifier);
 
165
}
 
166
 
 
167
const char *
 
168
cc_keyboard_item_get_binding (CcKeyboardItem *item)
 
169
{
 
170
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
 
171
 
 
172
  return item->binding;
 
173
}
 
174
 
 
175
static void
 
176
_set_type (CcKeyboardItem *item,
 
177
           gint            value)
 
178
{
 
179
  item->type = value;
 
180
}
 
181
 
 
182
static void
 
183
_set_command (CcKeyboardItem *item,
 
184
              const char       *value)
 
185
{
 
186
  g_free (item->command);
 
187
  item->command = g_strdup (value);
 
188
}
 
189
 
 
190
const char *
 
191
cc_keyboard_item_get_command (CcKeyboardItem *item)
 
192
{
 
193
  g_return_val_if_fail (CC_IS_KEYBOARD_ITEM (item), NULL);
 
194
 
 
195
  return item->command;
 
196
}
 
197
 
 
198
static void
 
199
cc_keyboard_item_set_property (GObject      *object,
 
200
                               guint         prop_id,
 
201
                               const GValue *value,
 
202
                               GParamSpec   *pspec)
 
203
{
 
204
  CcKeyboardItem *self;
 
205
 
 
206
  self = CC_KEYBOARD_ITEM (object);
 
207
 
 
208
  switch (prop_id) {
 
209
  case PROP_DESCRIPTION:
 
210
    _set_description (self, g_value_get_string (value));
 
211
    break;
 
212
  case PROP_BINDING:
 
213
    _set_binding (self, g_value_get_string (value), TRUE);
 
214
    break;
 
215
  case PROP_COMMAND:
 
216
    _set_command (self, g_value_get_string (value));
 
217
    break;
 
218
  case PROP_TYPE:
 
219
    _set_type (self, g_value_get_int (value));
 
220
    break;
 
221
  default:
 
222
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
223
    break;
 
224
  }
 
225
}
 
226
 
 
227
static void
 
228
cc_keyboard_item_get_property (GObject    *object,
 
229
                               guint       prop_id,
 
230
                               GValue     *value,
 
231
                               GParamSpec *pspec)
 
232
{
 
233
  CcKeyboardItem *self;
 
234
 
 
235
  self = CC_KEYBOARD_ITEM (object);
 
236
 
 
237
  switch (prop_id) {
 
238
  case PROP_DESCRIPTION:
 
239
    g_value_set_string (value, self->description);
 
240
    break;
 
241
  case PROP_BINDING:
 
242
    g_value_set_string (value, self->binding);
 
243
    break;
 
244
  case PROP_EDITABLE:
 
245
    g_value_set_boolean (value, self->editable);
 
246
    break;
 
247
  case PROP_COMMAND:
 
248
    g_value_set_string (value, self->command);
 
249
    break;
 
250
  default:
 
251
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
252
    break;
 
253
  }
 
254
}
 
255
 
 
256
static GObject *
 
257
cc_keyboard_item_constructor (GType                  type,
 
258
                              guint                  n_construct_properties,
 
259
                              GObjectConstructParam *construct_properties)
 
260
{
 
261
  CcKeyboardItem      *keyboard_item;
 
262
 
 
263
  keyboard_item = CC_KEYBOARD_ITEM (G_OBJECT_CLASS (cc_keyboard_item_parent_class)->constructor (type,
 
264
                                                                                                 n_construct_properties,
 
265
                                                                                                 construct_properties));
 
266
 
 
267
  return G_OBJECT (keyboard_item);
 
268
}
 
269
 
 
270
static void
 
271
cc_keyboard_item_class_init (CcKeyboardItemClass *klass)
 
272
{
 
273
  GObjectClass  *object_class = G_OBJECT_CLASS (klass);
 
274
 
 
275
  object_class->get_property = cc_keyboard_item_get_property;
 
276
  object_class->set_property = cc_keyboard_item_set_property;
 
277
  object_class->constructor = cc_keyboard_item_constructor;
 
278
  object_class->finalize = cc_keyboard_item_finalize;
 
279
 
 
280
  g_object_class_install_property (object_class,
 
281
                                   PROP_DESCRIPTION,
 
282
                                   g_param_spec_string ("description",
 
283
                                                        "description",
 
284
                                                        "description",
 
285
                                                        NULL,
 
286
                                                        G_PARAM_READWRITE));
 
287
 
 
288
  g_object_class_install_property (object_class,
 
289
                                   PROP_BINDING,
 
290
                                   g_param_spec_string ("binding",
 
291
                                                        "binding",
 
292
                                                        "binding",
 
293
                                                        NULL,
 
294
                                                        G_PARAM_READWRITE));
 
295
 
 
296
  g_object_class_install_property (object_class,
 
297
                                   PROP_EDITABLE,
 
298
                                   g_param_spec_boolean ("editable",
 
299
                                                         NULL,
 
300
                                                         NULL,
 
301
                                                         FALSE,
 
302
                                                         G_PARAM_READABLE));
 
303
 
 
304
  g_object_class_install_property (object_class,
 
305
                                   PROP_TYPE,
 
306
                                   g_param_spec_int ("type",
 
307
                                                     NULL,
 
308
                                                     NULL,
 
309
                                                     CC_KEYBOARD_ITEM_TYPE_NONE,
 
310
                                                     CC_KEYBOARD_ITEM_TYPE_GSETTINGS,
 
311
                                                     CC_KEYBOARD_ITEM_TYPE_NONE,
 
312
                                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
 
313
 
 
314
  g_object_class_install_property (object_class,
 
315
                                   PROP_COMMAND,
 
316
                                   g_param_spec_string ("command",
 
317
                                                        "command",
 
318
                                                        "command",
 
319
                                                        NULL,
 
320
                                                        G_PARAM_READWRITE));
 
321
 
 
322
  g_type_class_add_private (klass, sizeof (CcKeyboardItemPrivate));
 
323
}
 
324
 
 
325
static void
 
326
cc_keyboard_item_init (CcKeyboardItem *item)
 
327
{
 
328
  item->priv = CC_KEYBOARD_ITEM_GET_PRIVATE (item);
 
329
}
 
330
 
 
331
static void
 
332
cc_keyboard_item_finalize (GObject *object)
 
333
{
 
334
  CcKeyboardItem *item;
 
335
 
 
336
  g_return_if_fail (object != NULL);
 
337
  g_return_if_fail (CC_IS_KEYBOARD_ITEM (object));
 
338
 
 
339
  item = CC_KEYBOARD_ITEM (object);
 
340
 
 
341
  g_return_if_fail (item->priv != NULL);
 
342
 
 
343
  if (item->settings != NULL)
 
344
    g_object_unref (item->settings);
 
345
 
 
346
  /* Free memory */
 
347
  g_free (item->binding);
 
348
  g_free (item->gettext_package);
 
349
  g_free (item->gsettings_path);
 
350
  g_free (item->description);
 
351
  g_free (item->command);
 
352
  g_free (item->schema);
 
353
  g_free (item->key);
 
354
 
 
355
  G_OBJECT_CLASS (cc_keyboard_item_parent_class)->finalize (object);
 
356
}
 
357
 
 
358
CcKeyboardItem *
 
359
cc_keyboard_item_new (CcKeyboardItemType type)
 
360
{
 
361
  GObject *object;
 
362
 
 
363
  object = g_object_new (CC_TYPE_KEYBOARD_ITEM,
 
364
                         "type", type,
 
365
                         NULL);
 
366
 
 
367
  return CC_KEYBOARD_ITEM (object);
 
368
}
 
369
 
 
370
/* wrapper around g_settings_get_str[ing|v] */
 
371
static char *
 
372
settings_get_binding (GSettings  *settings,
 
373
                      const char *key)
 
374
{
 
375
  GVariant *variant;
 
376
  char *value = NULL;
 
377
 
 
378
  variant = g_settings_get_value (settings, key);
 
379
  if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING))
 
380
    value = g_variant_dup_string (variant, NULL);
 
381
  else if (g_variant_is_of_type (variant, G_VARIANT_TYPE_STRING_ARRAY))
 
382
    {
 
383
      const char **str_array;
 
384
 
 
385
      str_array = g_variant_get_strv (variant, NULL);
 
386
      value = g_strdup (str_array[0]);
 
387
    }
 
388
  g_variant_unref (variant);
 
389
 
 
390
  return value;
 
391
}
 
392
 
 
393
static void
 
394
binding_changed (GSettings *settings,
 
395
                 const char *key,
 
396
                 CcKeyboardItem *item)
 
397
{
 
398
  char *value;
 
399
 
 
400
  value = settings_get_binding (item->settings, item->key);
 
401
  item->editable = g_settings_is_writable (item->settings, item->key);
 
402
  _set_binding (item, value, FALSE);
 
403
  g_free (value);
 
404
  g_object_notify (G_OBJECT (item), "binding");
 
405
}
 
406
 
 
407
gboolean
 
408
cc_keyboard_item_load_from_gsettings_path (CcKeyboardItem *item,
 
409
                                           const char     *path,
 
410
                                           gboolean        reset)
 
411
{
 
412
  item->schema = g_strdup (CUSTOM_KEYS_SCHEMA);
 
413
  item->gsettings_path = g_strdup (path);
 
414
  item->key = g_strdup ("binding");
 
415
  item->settings = g_settings_new_with_path (item->schema, path);
 
416
  item->editable = g_settings_is_writable (item->settings, item->key);
 
417
  item->desc_editable = g_settings_is_writable (item->settings, "name");
 
418
  item->cmd_editable = g_settings_is_writable (item->settings, "command");
 
419
 
 
420
  if (reset)
 
421
    {
 
422
      g_settings_reset (item->settings, "name");
 
423
      g_settings_reset (item->settings, "command");
 
424
      g_settings_reset (item->settings, "binding");
 
425
    }
 
426
 
 
427
  g_settings_bind (item->settings, "name",
 
428
                   G_OBJECT (item), "description", G_SETTINGS_BIND_DEFAULT);
 
429
  g_settings_bind (item->settings, "command",
 
430
                   G_OBJECT (item), "command", G_SETTINGS_BIND_DEFAULT);
 
431
 
 
432
  item->binding = settings_get_binding (item->settings, item->key);
 
433
  binding_from_string (item->binding, &item->keyval, &item->keycode, &item->mask);
 
434
  g_signal_connect (G_OBJECT (item->settings), "changed::binding",
 
435
                    G_CALLBACK (binding_changed), item);
 
436
 
 
437
  return TRUE;
 
438
}
 
439
 
 
440
gboolean
 
441
cc_keyboard_item_load_from_gsettings (CcKeyboardItem *item,
 
442
                                      const char *description,
 
443
                                      const char *schema,
 
444
                                      const char *key)
 
445
{
 
446
  char *signal_name;
 
447
 
 
448
  item->schema = g_strdup (schema);
 
449
  item->key = g_strdup (key);
 
450
  item->description = g_strdup (description);
 
451
 
 
452
  item->settings = g_settings_new (item->schema);
 
453
  item->binding = settings_get_binding (item->settings, item->key);
 
454
  item->editable = g_settings_is_writable (item->settings, item->key);
 
455
 
 
456
  if ((g_strcmp0 (item->binding, "<Alt>") == 0) || (g_strcmp0 (item->binding, "<Alt_L>") == 0))
 
457
    {
 
458
      g_free (item->binding);
 
459
      item->binding = g_strdup ("Alt_L");
 
460
    }
 
461
 
 
462
  binding_from_string (item->binding, &item->keyval, &item->keycode, &item->mask);
 
463
 
 
464
  signal_name = g_strdup_printf ("changed::%s", item->key);
 
465
  g_signal_connect (G_OBJECT (item->settings), signal_name,
 
466
                    G_CALLBACK (binding_changed), item);
 
467
  g_free (signal_name);
 
468
 
 
469
  return TRUE;
 
470
}
 
471
 
 
472
gboolean
 
473
cc_keyboard_item_equal (CcKeyboardItem *a,
 
474
                        CcKeyboardItem *b)
 
475
{
 
476
  if (a->type != b->type)
 
477
    return FALSE;
 
478
  switch (a->type)
 
479
    {
 
480
      case CC_KEYBOARD_ITEM_TYPE_GSETTINGS_PATH:
 
481
        return g_str_equal (a->gsettings_path, b->gsettings_path);
 
482
      case CC_KEYBOARD_ITEM_TYPE_GSETTINGS:
 
483
        return (g_str_equal (a->schema, b->schema) &&
 
484
                g_str_equal (a->key, b->key));
 
485
      default:
 
486
        g_assert_not_reached ();
 
487
    }
 
488
 
 
489
}
 
490
 
 
491
/*
 
492
 * vim: sw=2 ts=8 cindent noai bs=2
 
493
 */