2
* Copyright (C) 2012 Red Hat, Inc.
4
* Written by: Rui Matos <rmatos@redhat.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2, or (at your option)
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
#include <glib/gi18n.h>
24
#define GNOME_DESKTOP_USE_UNSTABLE_API
25
#include <libgnome-desktop/gnome-xkb-info.h>
27
#include "cc-keyboard-option.h"
29
#define CC_TYPE_KEYBOARD_OPTION (cc_keyboard_option_get_type ())
30
#define CC_KEYBOARD_OPTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CC_TYPE_KEYBOARD_OPTION, CcKeyboardOption))
31
#define CC_KEYBOARD_OPTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CC_TYPE_KEYBOARD_OPTION, CcKeyboardOptionClass))
32
#define CC_IS_KEYBOARD_OPTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CC_TYPE_KEYBOARD_OPTION))
33
#define CC_IS_KEYBOARD_OPTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CC_TYPE_KEYBOARD_OPTION))
34
#define CC_KEYBOARD_OPTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CC_TYPE_KEYBOARD_OPTION, CcKeyboardOptionClass))
36
#define INPUT_SOURCES_SCHEMA "org.gnome.desktop.input-sources"
37
#define XKB_OPTIONS_KEY "xkb-options"
39
#define XKB_OPTION_GROUP_LVL3 "lv3"
40
#define XKB_OPTION_GROUP_COMP "Compose key"
55
struct _CcKeyboardOption
57
GObject parent_object;
64
const gchar * const *whitelist;
67
typedef struct _CcKeyboardOptionClass CcKeyboardOptionClass;
68
struct _CcKeyboardOptionClass
70
GObjectClass parent_class;
73
static guint keyboard_option_signals[LAST_SIGNAL] = { 0 };
75
static GnomeXkbInfo *xkb_info = NULL;
76
static GSettings *input_sources_settings = NULL;
77
static gchar **current_xkb_options = NULL;
79
static const gchar *xkb_option_lvl3_whitelist[] = {
89
static const gchar *xkb_option_comp_whitelist[] = {
99
static GList *objects_list = NULL;
101
GType cc_keyboard_option_get_type (void);
103
G_DEFINE_TYPE (CcKeyboardOption, cc_keyboard_option, G_TYPE_OBJECT);
106
strv_contains (const gchar * const *strv,
109
const gchar * const *p = strv;
110
for (p = strv; *p; p++)
111
if (g_strcmp0 (*p, str) == 0)
118
reload_setting (CcKeyboardOption *self)
122
for (iter = current_xkb_options; *iter; ++iter)
123
if (strv_contains (self->whitelist, *iter))
125
if (g_strcmp0 (self->current_value, *iter) != 0)
127
g_free (self->current_value);
128
self->current_value = g_strdup (*iter);
129
g_signal_emit (self, keyboard_option_signals[CHANGED_SIGNAL], 0);
134
if (*iter == NULL && self->current_value != NULL)
136
g_clear_pointer (&self->current_value, g_free);
137
g_signal_emit (self, keyboard_option_signals[CHANGED_SIGNAL], 0);
142
xkb_options_changed (GSettings *settings,
148
g_strfreev (current_xkb_options);
149
current_xkb_options = g_settings_get_strv (settings, key);
151
for (l = objects_list; l; l = l->next)
152
reload_setting (CC_KEYBOARD_OPTION (l->data));
156
cc_keyboard_option_get_property (GObject *object,
161
CcKeyboardOption *self;
163
self = CC_KEYBOARD_OPTION (object);
168
g_value_set_string (value, self->group);
170
case PROP_DESCRIPTION:
171
g_value_set_string (value, self->description);
174
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180
cc_keyboard_option_set_property (GObject *object,
185
CcKeyboardOption *self;
187
self = CC_KEYBOARD_OPTION (object);
192
self->group = g_value_dup_string (value);
194
case PROP_DESCRIPTION:
195
self->description = g_value_dup_string (value);
198
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204
cc_keyboard_option_init (CcKeyboardOption *self)
209
cc_keyboard_option_finalize (GObject *object)
211
CcKeyboardOption *self = CC_KEYBOARD_OPTION (object);
213
g_clear_pointer (&self->group, g_free);
214
g_clear_pointer (&self->description, g_free);
215
g_clear_pointer (&self->current_value, g_free);
216
g_clear_object (&self->store);
218
G_OBJECT_CLASS (cc_keyboard_option_parent_class)->finalize (object);
222
cc_keyboard_option_constructed (GObject *object)
227
CcKeyboardOption *self = CC_KEYBOARD_OPTION (object);
229
G_OBJECT_CLASS (cc_keyboard_option_parent_class)->constructed (object);
231
if (g_str_equal (self->group, XKB_OPTION_GROUP_LVL3))
232
self->whitelist = xkb_option_lvl3_whitelist;
233
else if (g_str_equal (self->group, XKB_OPTION_GROUP_COMP))
234
self->whitelist = xkb_option_comp_whitelist;
236
g_assert_not_reached ();
238
self->store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
239
gtk_list_store_append (self->store, &iter);
240
gtk_list_store_set (self->store, &iter,
241
XKB_OPTION_DESCRIPTION_COLUMN, _("Disabled"),
242
XKB_OPTION_ID_COLUMN, NULL,
244
options = gnome_xkb_info_get_options_for_group (xkb_info, self->group);
245
for (l = options; l; l = l->next)
248
if (strv_contains (self->whitelist, option_id))
250
gtk_list_store_append (self->store, &iter);
251
gtk_list_store_set (self->store, &iter,
252
XKB_OPTION_DESCRIPTION_COLUMN,
253
gnome_xkb_info_description_for_option (xkb_info, self->group, option_id),
254
XKB_OPTION_ID_COLUMN,
259
g_list_free (options);
261
reload_setting (self);
265
cc_keyboard_option_class_init (CcKeyboardOptionClass *klass)
267
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
269
gobject_class->get_property = cc_keyboard_option_get_property;
270
gobject_class->set_property = cc_keyboard_option_set_property;
271
gobject_class->finalize = cc_keyboard_option_finalize;
272
gobject_class->constructed = cc_keyboard_option_constructed;
274
g_object_class_install_property (gobject_class,
276
g_param_spec_string ("group",
278
"xkb option group identifier",
280
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
281
g_object_class_install_property (gobject_class,
283
g_param_spec_string ("description",
285
"translated option description",
287
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
289
keyboard_option_signals[CHANGED_SIGNAL] = g_signal_new ("changed",
290
CC_TYPE_KEYBOARD_OPTION,
298
cc_keyboard_option_get_all (void)
303
xkb_info = gnome_xkb_info_new ();
305
input_sources_settings = g_settings_new (INPUT_SOURCES_SCHEMA);
307
g_signal_connect (input_sources_settings, "changed::" XKB_OPTIONS_KEY,
308
G_CALLBACK (xkb_options_changed), NULL);
310
xkb_options_changed (input_sources_settings, XKB_OPTIONS_KEY, NULL);
312
objects_list = g_list_prepend (objects_list,
313
g_object_new (CC_TYPE_KEYBOARD_OPTION,
314
"group", XKB_OPTION_GROUP_LVL3,
315
"description", _("Alternative Characters Key"),
317
objects_list = g_list_prepend (objects_list,
318
g_object_new (CC_TYPE_KEYBOARD_OPTION,
319
"group", XKB_OPTION_GROUP_COMP,
320
"description", _("Compose Key"),
326
cc_keyboard_option_get_description (CcKeyboardOption *self)
328
g_return_val_if_fail (CC_IS_KEYBOARD_OPTION (self), NULL);
330
return self->description;
334
cc_keyboard_option_get_store (CcKeyboardOption *self)
336
g_return_val_if_fail (CC_IS_KEYBOARD_OPTION (self), NULL);
342
cc_keyboard_option_get_current_value_description (CcKeyboardOption *self)
344
g_return_val_if_fail (CC_IS_KEYBOARD_OPTION (self), NULL);
346
if (!self->current_value)
347
return _("Disabled");
349
return gnome_xkb_info_description_for_option (xkb_info, self->group, self->current_value);
353
remove_value (const gchar *value)
357
for (p = current_xkb_options; *p; ++p)
358
if (g_str_equal (*p, value))
371
add_value (const gchar *value)
373
gchar **new_xkb_options;
376
new_xkb_options = g_new0 (gchar *, g_strv_length (current_xkb_options) + 2);
379
for (b = current_xkb_options; *b; ++a, ++b)
382
*a = g_strdup (value);
384
g_strfreev (current_xkb_options);
385
current_xkb_options = new_xkb_options;
389
replace_value (const gchar *old,
394
if (g_str_equal (old, new))
397
for (iter = current_xkb_options; *iter; ++iter)
398
if (g_str_equal (*iter, old))
401
*iter = g_strdup (new);
407
cc_keyboard_option_set_selection (CcKeyboardOption *self,
410
gchar *new_value = NULL;
412
g_return_if_fail (CC_IS_KEYBOARD_OPTION (self));
414
gtk_tree_model_get (GTK_TREE_MODEL (self->store), iter,
415
XKB_OPTION_ID_COLUMN, &new_value,
420
if (self->current_value)
421
remove_value (self->current_value);
425
if (self->current_value)
426
replace_value (self->current_value, new_value);
428
add_value (new_value);
431
g_settings_set_strv (input_sources_settings, XKB_OPTIONS_KEY,
432
(const gchar * const *) current_xkb_options);
438
cc_keyboard_option_clear_all (void)
442
for (l = objects_list; l; l = l->next)
443
g_object_unref (l->data);
445
g_clear_pointer (&objects_list, g_list_free);
446
g_clear_pointer (¤t_xkb_options, g_strfreev);
447
g_clear_object (&input_sources_settings);
448
g_clear_object (&xkb_info);