File: iconfig-gtk.c

package info (click to toggle)
ibus-input-pad 1.4.99.20140916-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 292 kB
  • sloc: ansic: 1,493; makefile: 150; sh: 16; xml: 6
file content (424 lines) | stat: -rw-r--r-- 13,849 bytes parent folder | download | duplicates (2)
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
/* vim:set et sts=4: */
/* ibus-input-pad - Input pad for IBus
 * Copyright (C) 2010-2011 Takao Fujiwara <takao.fujiwara1@gmail.com>
 * Copyright (C) 2010-2011 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <ibus.h>
#include <glib.h>

#include "iconfig-gtk.h"

#define IBUS_INPUT_PAD_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_INPUT_PAD_CONFIG, IBusInputPadConfigPrivate))

typedef struct _SavedTable SavedTable;

typedef enum {
    CONFIG_VALUE_TYPE_NONE = 0,
    CONFIG_VALUE_TYPE_INT,
    CONFIG_VALUE_TYPE_STRING,
} ConfigValueType;

struct _SavedTable {
    gchar                      *path;
    ConfigValueType             type;
    union {
        int                     i;
        gchar                  *str;
    } value;
};

struct _IBusInputPadConfigPrivate {
    IBusConfig                 *config;
    GHashTable                 *table;
    GObject                    *apply_button;
};

G_DEFINE_TYPE (IBusInputPadConfig, ibus_input_pad_config,
               G_TYPE_OBJECT)

static void
destroy_saved_table (SavedTable *saved_table)
{
    if (saved_table->type == CONFIG_VALUE_TYPE_STRING) {
        g_free (saved_table->value.str);
        saved_table->value.str = NULL;
    }
    g_free (saved_table);
}

static void
commit_path (gpointer key, gpointer value, gpointer user_data)
{
    IBusInputPadConfig *config;
    const gchar *path = (const gchar *) key;
    SavedTable *saved_table = (SavedTable *) value;
    gchar *p;
    gchar *section;
    gchar *name;

    g_return_if_fail (key != NULL && value != NULL);
    g_return_if_fail (IBUS_IS_INPUT_PAD_CONFIG (user_data));

    config = IBUS_INPUT_PAD_CONFIG (user_data);
    p = g_strrstr (path, "/");
    g_return_if_fail (p != NULL && p > path);
    section = g_strndup (path, p - path);
    name = g_strdup (p + 1);
    if (saved_table->type == CONFIG_VALUE_TYPE_STRING) {
        ibus_input_pad_config_commit_str (config,
                                          section,
                                          name,
                                          saved_table->value.str);
    } else {
        ibus_input_pad_config_commit_int (config,
                                          section,
                                          name,
                                          saved_table->value.i);
    }
}

static GObject *
ibus_input_pad_config_get_apply_button_real (IBusInputPadConfig      *config)
{
    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), NULL);
    g_return_val_if_fail (config->priv != NULL, NULL);

    return config->priv->apply_button;
}

static void
ibus_input_pad_config_set_apply_button_real (IBusInputPadConfig      *config,
                                             GObject                 *button)
{
    g_return_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config));
    g_return_if_fail (config->priv != NULL);

    config->priv->apply_button = button;
}

static void
ibus_input_pad_config_init (IBusInputPadConfig *config)
{
    IBusInputPadConfigPrivate *priv = IBUS_INPUT_PAD_CONFIG_GET_PRIVATE (config);
    priv->table = g_hash_table_new_full (g_str_hash,
                                         g_str_equal,
                                         (GDestroyNotify) g_free,
                                         (GDestroyNotify) destroy_saved_table);
    config->priv = priv;
}

static void
ibus_input_pad_config_class_init (IBusInputPadConfigClass *klass)
{
#if 0
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
#endif

    g_type_class_add_private (klass, sizeof (IBusInputPadConfigPrivate));

    klass->get_apply_button = ibus_input_pad_config_get_apply_button_real;
    klass->set_apply_button = ibus_input_pad_config_set_apply_button_real;
}

/* currently this is a static function due to ibus_bus_get_config */
static IBusInputPadConfig *
ibus_input_pad_config_new (GDBusConnection *connection)
{
    GObject *obj;

    obj = g_object_new (IBUS_TYPE_INPUT_PAD_CONFIG,
                        NULL);

    return IBUS_INPUT_PAD_CONFIG (obj);
}

IBusInputPadConfig *
ibus_bus_get_input_pad_config (IBusBus *bus)
{
    IBusConfig *ibus_config;
    IBusInputPadConfig *config;

    ibus_config = ibus_bus_get_config (bus);
    if (ibus_config == NULL) {
        g_warning ("IBus connection failed. Maybe no ibus-daemon.");
        return NULL;
    }
    /* No idea to set parent from ibus_bus_get_config () */
    config = ibus_input_pad_config_new (NULL);
    g_return_val_if_fail (config->priv != NULL, NULL);
    config->priv->config = ibus_config;

    return config;
}

gboolean
ibus_input_pad_config_get_int (IBusInputPadConfig      *config,
                               const gchar             *section,
                               const gchar             *name,
                               int                     *value_intp)
{
    IBusConfig *ibus_config;
    GVariant *value = NULL;

    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), FALSE);
    g_return_val_if_fail (config->priv != NULL, FALSE);

    ibus_config = config->priv->config;
    if ((value = ibus_config_get_value (ibus_config, section, name)) != NULL) {
        if (value_intp) {
            *value_intp = g_variant_get_int32 (value);
        }
        g_variant_unref (value);
        return TRUE;
    }
    if (!g_strcmp0 (name, "char_table_combo_box")) {
        if (value_intp) {
            *value_intp = CHAR_TABLE_COMBO_BOX_DEFAULT;
        }
        return TRUE;
    } else if (!g_strcmp0 (name, "layout_table_combo_box")) {
        if (value_intp) {
            *value_intp = LAYOUT_TABLE_COMBO_BOX_DEFAULT;
        }
        return TRUE;
    }
    return FALSE;
}

gboolean
ibus_input_pad_config_get_str (IBusInputPadConfig      *config,
                               const gchar             *section,
                               const gchar             *name,
                               gchar                  **value_strp)
{
    IBusConfig *ibus_config;
    GVariant *value = NULL;

    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), FALSE);
    g_return_val_if_fail (config->priv != NULL, FALSE);

    ibus_config = config->priv->config;
    if ((value = ibus_config_get_value (ibus_config, section, name)) != NULL) {
        if (value_strp) {
            *value_strp = g_variant_dup_string (value, NULL);
        }
        g_variant_unref (value);
        return TRUE;
    }
    if (!g_strcmp0 (name, "keyboard_theme")) {
        if (value_strp) {
            *value_strp = g_strdup (KEYBOARD_THEME_DEFAULT);
        }
        return TRUE;
    }
    return FALSE;
}

#if 0
gboolean
ibus_input_pad_config_get_value (IBusConfig    *config,
                                 const gchar   *section,
                                 const gchar   *name,
                                 GVariant      *value)
{
    if ((value = ibus_config_get_value (config, section, name)) != NULL) {
        return TRUE;
    }
    if (!g_strcmp0 (name, "keyboard_theme")) {
        value = g_variant_new ("s", KEYBOARD_THEME_DEFAULT);
        return TRUE;
    } else if (!g_strcmp0 (name, "char_table_combo_box")) {
        value = g_variant_new ("i", CHAR_TABLE_COMBO_BOX_DEFAULT);
        return TRUE;
    } else if (!g_strcmp0 (name, "layout_table_combo_box")) {
        value = g_variant_new ("i", LAYOUT_TABLE_COMBO_BOX_DEFAULT);
        return TRUE;
    }
    return FALSE;
}
#endif

gboolean
ibus_input_pad_config_set_int (IBusInputPadConfig      *config,
                               const gchar             *section,
                               const gchar             *name,
                               int                      value_int)
{
    IBusConfig *ibus_config;
    GVariant *value = NULL;
    int orig_i = 0;
    gchar *path;
    SavedTable *saved_table;

    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), FALSE);
    g_return_val_if_fail (config->priv != NULL, FALSE);
    g_return_val_if_fail (section != NULL && name != NULL, FALSE);

    ibus_config = config->priv->config;
    if ((value = ibus_config_get_value (ibus_config, section, name)) != NULL) {
        orig_i = g_variant_get_int32 (value);
        g_variant_unref (value);
        if (value_int == orig_i) {
            return FALSE;
        }
    } else if (!g_strcmp0 (name, "char_table_combo_box")) {
        if (value_int == CHAR_TABLE_COMBO_BOX_DEFAULT) {
            return FALSE;
        }
    } else if (!g_strcmp0 (name, "layout_table_combo_box")) {
        if (value_int == LAYOUT_TABLE_COMBO_BOX_DEFAULT) {
            return FALSE;
        }
    }

    path = g_build_path ("/", section, name, NULL);
    saved_table = g_new0 (SavedTable, 1);
    saved_table->path = g_strdup (path);
    saved_table->type = CONFIG_VALUE_TYPE_INT;
    saved_table->value.i = value_int;
    if (g_hash_table_lookup (config->priv->table, path)) {
        g_hash_table_replace (config->priv->table,
                              (gpointer) g_strdup (path),
                              (gpointer) saved_table);
    } else {
        g_hash_table_insert (config->priv->table,
                             (gpointer) g_strdup (path),
                             (gpointer) saved_table);
    }
    g_free (path);

    return TRUE;
}

gboolean
ibus_input_pad_config_set_str (IBusInputPadConfig      *config,
                               const gchar             *section,
                               const gchar             *name,
                               const gchar             *value_str)
{
    IBusConfig *ibus_config;
    GVariant *value = NULL;
    const gchar *orig_str = NULL;
    gchar *path;
    SavedTable *saved_table;

    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), FALSE);
    g_return_val_if_fail (config->priv != NULL, FALSE);
    g_return_val_if_fail (section != NULL && name != NULL, FALSE);

    ibus_config = config->priv->config;
    if ((value = ibus_config_get_value (ibus_config, section, name)) != NULL) {
        orig_str = g_variant_get_string (value, NULL);
        if (!g_strcmp0 (value_str, orig_str)) {
            g_variant_unref (value);
            return FALSE;
        }
        g_variant_unref (value);
    } else if (!g_strcmp0 (name, "keyboard_theme")) {
        if (value_str == NULL ||
            !g_strcmp0 (value_str, KEYBOARD_THEME_DEFAULT)) {
            return FALSE;
        }
    }

    path = g_build_path ("/", section, name, NULL);
    saved_table = g_new0 (SavedTable, 1);
    saved_table->path = g_strdup (path);
    saved_table->type = CONFIG_VALUE_TYPE_STRING;
    if (value_str) {
        saved_table->value.str = g_strdup (value_str);
    }
    if (g_hash_table_lookup (config->priv->table, path)) {
        g_hash_table_replace (config->priv->table,
                              (gpointer) g_strdup (path),
                              (gpointer) saved_table);
    } else {
        g_hash_table_insert (config->priv->table,
                             (gpointer) g_strdup (path),
                             (gpointer) saved_table);
    }
    g_free (path);

    return TRUE;
}

gboolean
ibus_input_pad_config_commit_int (IBusInputPadConfig      *config,
                                  const gchar             *section,
                                  const gchar             *name,
                                  int                      value_int)
{
    IBusConfig *ibus_config;
    GVariant *value = NULL;

    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), FALSE);
    g_return_val_if_fail (config->priv != NULL, FALSE);
    g_return_val_if_fail (section != NULL && name != NULL, FALSE);

    ibus_config = config->priv->config;
    value = g_variant_new ("i", value_int);
    if (!ibus_config_set_value (ibus_config, section, name, value)) {
        return FALSE;
    }
    return TRUE;
}

gboolean
ibus_input_pad_config_commit_str (IBusInputPadConfig      *config,
                                  const gchar             *section,
                                  const gchar             *name,
                                  const gchar             *value_str)
{
    IBusConfig *ibus_config;
    GVariant *value = NULL;

    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), FALSE);
    g_return_val_if_fail (config->priv != NULL, FALSE);
    g_return_val_if_fail (section != NULL && name != NULL, FALSE);

    ibus_config = config->priv->config;
    value = g_variant_new ("s", value_str);
    if (!ibus_config_set_value (ibus_config, section, name, value)) {
        return FALSE;
    }
    return TRUE;
}

gboolean
ibus_input_pad_config_commit_all (IBusInputPadConfig      *config)
{
    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), FALSE);
    g_return_val_if_fail (config->priv != NULL, FALSE);

    g_hash_table_foreach (config->priv->table, commit_path, config);
    g_hash_table_remove_all (config->priv->table);
    return TRUE;
}

int
ibus_input_pad_config_get_changed_size (IBusInputPadConfig      *config)
{
    g_return_val_if_fail (IBUS_IS_INPUT_PAD_CONFIG (config), 0);
    g_return_val_if_fail (config->priv != NULL, 0);

    return g_hash_table_size (config->priv->table);
}