cc-region-panel.c 62.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * Copyright (C) 2010 Intel, 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
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16
 *
Sergey V. Udaltsov's avatar
Sergey V. Udaltsov committed
17
 * Author: Sergey Udaltsov <svu@gnome.org>
18
19
20
 *
 */

Matthias Clasen's avatar
Matthias Clasen committed
21
#include <config.h>
22
#include <locale.h>
Matthias Clasen's avatar
Matthias Clasen committed
23
#include <glib/gi18n.h>
Matthias Clasen's avatar
Matthias Clasen committed
24
#include <gio/gio.h>
Matthias Clasen's avatar
Matthias Clasen committed
25
#include <gio/gdesktopappinfo.h>
Matthias Clasen's avatar
Matthias Clasen committed
26
27
#include <gtk/gtk.h>
#include <polkit/polkit.h>
Matthias Clasen's avatar
Matthias Clasen committed
28

29
#include "shell/list-box-helper.h"
30
#include "cc-region-panel.h"
31
#include "cc-region-resources.h"
Matthias Clasen's avatar
Matthias Clasen committed
32
33
34
35
#include "cc-language-chooser.h"
#include "cc-format-chooser.h"
#include "cc-input-chooser.h"
#include "cc-input-options.h"
36

Matthias Clasen's avatar
Matthias Clasen committed
37
38
39
40
41
42
43
44
45
46
#include "cc-common-language.h"

#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnome-desktop/gnome-languages.h>
#include <libgnome-desktop/gnome-xkb-info.h>

#ifdef HAVE_IBUS
#include <ibus.h>
#include "cc-ibus-utils.h"
#endif
47

Matthias Clasen's avatar
Matthias Clasen committed
48
49
#include <act/act.h>

Matthias Clasen's avatar
Matthias Clasen committed
50
51
52
53
54
55
56
57
58
#define GNOME_DESKTOP_INPUT_SOURCES_DIR "org.gnome.desktop.input-sources"
#define KEY_INPUT_SOURCES        "sources"

#define GNOME_SYSTEM_LOCALE_DIR "org.gnome.system.locale"
#define KEY_REGION "region"

#define INPUT_SOURCE_TYPE_XKB "xkb"
#define INPUT_SOURCE_TYPE_IBUS "ibus"

59
60
#define DEFAULT_LOCALE "en_US.utf-8"

Ray Strode's avatar
Ray Strode committed
61
CC_PANEL_REGISTER (CcRegionPanel, cc_region_panel)
62

Matthias Clasen's avatar
Matthias Clasen committed
63
64
#define WID(s) GTK_WIDGET (gtk_builder_get_object (self->priv->builder, s))

65
66
#define REGION_PANEL_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_REGION_PANEL, CcRegionPanelPrivate))

Matthias Clasen's avatar
Matthias Clasen committed
67
68
typedef enum {
        CHOOSE_LANGUAGE,
69
        CHOOSE_REGION,
Matthias Clasen's avatar
Matthias Clasen committed
70
        ADD_INPUT,
71
72
73
        REMOVE_INPUT,
        MOVE_UP_INPUT,
        MOVE_DOWN_INPUT,
Matthias Clasen's avatar
Matthias Clasen committed
74
75
} SystemOp;

76
struct _CcRegionPanelPrivate {
77
78
	GtkBuilder *builder;

Matthias Clasen's avatar
Matthias Clasen committed
79
80
81
        GtkWidget   *login_button;
        GtkWidget   *login_label;
        gboolean     login;
82
        gboolean     login_auto_apply;
Matthias Clasen's avatar
Matthias Clasen committed
83
84
85
        GPermission *permission;
        SystemOp     op;
        GDBusProxy  *localed;
86
        GDBusProxy  *session;
87
        GCancellable *cancellable;
Matthias Clasen's avatar
Matthias Clasen committed
88

89
        GtkWidget *restart_notification;
90
        gchar *needs_restart_file_path;
Matthias Clasen's avatar
Matthias Clasen committed
91

92
93
94
95
96
        GtkWidget     *language_section;
        GtkListBoxRow *language_row;
        GtkWidget     *language_label;
        GtkListBoxRow *formats_row;
        GtkWidget     *formats_label;
Matthias Clasen's avatar
Matthias Clasen committed
97

Matthias Clasen's avatar
Matthias Clasen committed
98
99
100
        ActUserManager *user_manager;
        ActUser        *user;
        GSettings      *locale_settings;
Matthias Clasen's avatar
Matthias Clasen committed
101
102
103

        gchar *language;
        gchar *region;
Matthias Clasen's avatar
Matthias Clasen committed
104
105
        gchar *system_language;
        gchar *system_region;
Matthias Clasen's avatar
Matthias Clasen committed
106

Matthias Clasen's avatar
Matthias Clasen committed
107
        GtkWidget *input_section;
Matthias Clasen's avatar
Matthias Clasen committed
108
        GtkWidget *options_button;
Matthias Clasen's avatar
Matthias Clasen committed
109
110
111
        GtkWidget *input_list;
        GtkWidget *add_input;
        GtkWidget *remove_input;
112
113
        GtkWidget *move_up_input;
        GtkWidget *move_down_input;
Matthias Clasen's avatar
Matthias Clasen committed
114
115
116
117
118
119
120
121
122
123
        GtkWidget *show_config;
        GtkWidget *show_layout;

        GSettings *input_settings;
        GnomeXkbInfo *xkb_info;
#ifdef HAVE_IBUS
        IBusBus *ibus;
        GHashTable *ibus_engines;
        GCancellable *ibus_cancellable;
#endif
124
125
};

126
static void
Matthias Clasen's avatar
Matthias Clasen committed
127
cc_region_panel_finalize (GObject *object)
128
{
Matthias Clasen's avatar
Matthias Clasen committed
129
130
	CcRegionPanel *self = CC_REGION_PANEL (object);
	CcRegionPanelPrivate *priv = self->priv;
131
	GtkWidget *chooser;
132

133
134
135
        g_cancellable_cancel (priv->cancellable);
        g_clear_object (&priv->cancellable);

136
137
138
139
140
141
142
143
144
145
        if (priv->user_manager) {
                g_signal_handlers_disconnect_by_data (priv->user_manager, self);
                priv->user_manager = NULL;
        }

        if (priv->user) {
                g_signal_handlers_disconnect_by_data (priv->user, self);
                priv->user = NULL;
        }

Matthias Clasen's avatar
Matthias Clasen committed
146
147
        g_clear_object (&priv->permission);
        g_clear_object (&priv->localed);
148
        g_clear_object (&priv->session);
Matthias Clasen's avatar
Matthias Clasen committed
149
150
151
152
        g_clear_object (&priv->builder);
        g_clear_object (&priv->locale_settings);
        g_clear_object (&priv->input_settings);
        g_clear_object (&priv->xkb_info);
153
#ifdef HAVE_IBUS
Matthias Clasen's avatar
Matthias Clasen committed
154
155
156
157
158
        g_clear_object (&priv->ibus);
        if (priv->ibus_cancellable)
                g_cancellable_cancel (priv->ibus_cancellable);
        g_clear_object (&priv->ibus_cancellable);
        g_clear_pointer (&priv->ibus_engines, g_hash_table_destroy);
159
#endif
Matthias Clasen's avatar
Matthias Clasen committed
160
161
162
163
        g_free (priv->language);
        g_free (priv->region);
        g_free (priv->system_language);
        g_free (priv->system_region);
164

165
166
        g_clear_pointer (&priv->needs_restart_file_path, g_free);

167
        chooser = g_object_get_data (G_OBJECT (self), "input-chooser");
Debarshi Ray's avatar
Debarshi Ray committed
168
169
        if (chooser)
                gtk_widget_destroy (chooser);
170

Matthias Clasen's avatar
Matthias Clasen committed
171
	G_OBJECT_CLASS (cc_region_panel_parent_class)->finalize (object);
172
173
174
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
175
cc_region_panel_constructed (GObject *object)
176
{
Matthias Clasen's avatar
Matthias Clasen committed
177
        CcRegionPanel *self = CC_REGION_PANEL (object);
Matthias Clasen's avatar
Matthias Clasen committed
178
	CcRegionPanelPrivate *priv = self->priv;
179

Matthias Clasen's avatar
Matthias Clasen committed
180
        G_OBJECT_CLASS (cc_region_panel_parent_class)->constructed (object);
181

182
183
184
        if (priv->permission)
                cc_shell_embed_widget_in_header (cc_panel_get_shell (CC_PANEL (object)),
                                                 priv->login_button);
185
186
}

187
188
189
static const char *
cc_region_panel_get_help_uri (CcPanel *panel)
{
Matthias Clasen's avatar
Matthias Clasen committed
190
        return "help:gnome-help/prefs-language";
191
192
}

193
static void
194
cc_region_panel_class_init (CcRegionPanelClass * klass)
195
196
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
197
	CcPanelClass * panel_class = CC_PANEL_CLASS (klass);
198

199
	g_type_class_add_private (klass, sizeof (CcRegionPanelPrivate));
200

201
202
	panel_class->get_help_uri = cc_region_panel_get_help_uri;

Matthias Clasen's avatar
Matthias Clasen committed
203
        object_class->constructed = cc_region_panel_constructed;
204
	object_class->finalize = cc_region_panel_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
205
}
206

Matthias Clasen's avatar
Matthias Clasen committed
207
208
209
static void
restart_now (CcRegionPanel *self)
{
210
        CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
211

212
213
214
        g_file_delete (g_file_new_for_path (priv->needs_restart_file_path),
                                            NULL, NULL);

215
216
217
218
219
        g_dbus_proxy_call (priv->session,
                           "Logout",
                           g_variant_new ("(u)", 0),
                           G_DBUS_CALL_FLAGS_NONE,
                           -1, NULL, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
220
221
222
}

static void
223
224
225
set_restart_notification_visible (CcRegionPanel *self,
                                  const gchar   *locale,
                                  gboolean       visible)
Matthias Clasen's avatar
Matthias Clasen committed
226
227
{
	CcRegionPanelPrivate *priv = self->priv;
228
        gchar *current_locale = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
229

230
231
232
233
234
        if (locale) {
                current_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
                setlocale (LC_MESSAGES, locale);
        }

235
        gtk_revealer_set_reveal_child (GTK_REVEALER (priv->restart_notification), visible);
236
237
238
239
240

        if (locale) {
                setlocale (LC_MESSAGES, current_locale);
                g_free (current_locale);
        }
241
242
243
244
245
246
247
248
249
250

        if (!visible) {
                g_file_delete (g_file_new_for_path (priv->needs_restart_file_path),
                                                    NULL, NULL);

                return;
        }

        if (!g_file_set_contents (priv->needs_restart_file_path, "", -1, NULL))
                g_warning ("Unable to create %s", priv->needs_restart_file_path);
Matthias Clasen's avatar
Matthias Clasen committed
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
typedef struct {
        CcRegionPanel *self;
        int category;
        gchar *target_locale;
} MaybeNotifyData;

static void
maybe_notify_finish (GObject      *source,
                     GAsyncResult *res,
                     gpointer      data)
{
        MaybeNotifyData *mnd = data;
        CcRegionPanel *self = mnd->self;
        GError *error = NULL;
        GVariant *retval = NULL;
        gchar *current_lang_code = NULL;
        gchar *current_country_code = NULL;
        gchar *target_lang_code = NULL;
        gchar *target_country_code = NULL;
        const gchar *current_locale = NULL;

        retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error);
        if (!retval) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("Failed to get locale: %s\n", error->message);
                goto out;
        }

        g_variant_get (retval, "(&s)", &current_locale);

        if (!gnome_parse_locale (current_locale,
                                 &current_lang_code,
                                 &current_country_code,
                                 NULL,
                                 NULL))
                goto out;

        if (!gnome_parse_locale (mnd->target_locale,
                                 &target_lang_code,
                                 &target_country_code,
                                 NULL,
                                 NULL))
                goto out;

        if (g_str_equal (current_lang_code, target_lang_code) == FALSE ||
            g_str_equal (current_country_code, target_country_code) == FALSE)
299
300
301
302
303
304
305
                set_restart_notification_visible (self,
                                                  mnd->category == LC_MESSAGES ? mnd->target_locale : NULL,
                                                  TRUE);
        else
                set_restart_notification_visible (self,
                                                  mnd->category == LC_MESSAGES ? mnd->target_locale : NULL,
                                                  FALSE);
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
out:
        g_free (target_country_code);
        g_free (target_lang_code);
        g_free (current_country_code);
        g_free (current_lang_code);
        g_clear_pointer (&retval, g_variant_unref);
        g_clear_error (&error);
        g_free (mnd->target_locale);
        g_free (mnd);
}

static void
maybe_notify (CcRegionPanel *self,
              int            category,
              const gchar   *target_locale)
{
        CcRegionPanelPrivate *priv = self->priv;
        MaybeNotifyData *mnd;

        mnd = g_new0 (MaybeNotifyData, 1);
        mnd->self = self;
        mnd->category = category;
        mnd->target_locale = g_strdup (target_locale);

        g_dbus_proxy_call (priv->session,
                           "GetLocale",
                           g_variant_new ("(i)", category),
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
                           priv->cancellable,
                           maybe_notify_finish,
                           mnd);
}

Rui Matos's avatar
Rui Matos committed
340
static void set_localed_locale (CcRegionPanel *self);
Matthias Clasen's avatar
Matthias Clasen committed
341

342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
static void
set_system_language (CcRegionPanel *self,
                     const gchar   *language)
{
        CcRegionPanelPrivate *priv = self->priv;

        if (g_strcmp0 (language, priv->system_language) == 0)
                return;

        g_free (priv->system_language);
        priv->system_language = g_strdup (language);

        set_localed_locale (self);
}

357
static void
Matthias Clasen's avatar
Matthias Clasen committed
358
359
360
361
362
update_language (CcRegionPanel *self,
                 const gchar   *language)
{
	CcRegionPanelPrivate *priv = self->priv;

Matthias Clasen's avatar
Matthias Clasen committed
363
        if (priv->login) {
364
                set_system_language (self, language);
Matthias Clasen's avatar
Matthias Clasen committed
365
366
        } else {
                if (g_strcmp0 (language, priv->language) == 0)
367
                        return;
Matthias Clasen's avatar
Matthias Clasen committed
368
                act_user_set_language (priv->user, language);
369
                if (priv->login_auto_apply)
370
                        set_system_language (self, language);
371
                maybe_notify (self, LC_MESSAGES, language);
Matthias Clasen's avatar
Matthias Clasen committed
372
        }
373
374
375
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
376
377
378
379
380
language_response (GtkDialog     *chooser,
                   gint           response_id,
                   CcRegionPanel *self)
{
        const gchar *language;
Matthias Clasen's avatar
Matthias Clasen committed
381
382
383

        if (response_id == GTK_RESPONSE_OK) {
                language = cc_language_chooser_get_language (GTK_WIDGET (chooser));
384
                update_language (self, language);
Matthias Clasen's avatar
Matthias Clasen committed
385
        }
Matthias Clasen's avatar
Matthias Clasen committed
386
387

        gtk_widget_destroy (GTK_WIDGET (chooser));
Matthias Clasen's avatar
Matthias Clasen committed
388
389
}

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
static void
set_system_region (CcRegionPanel *self,
                   const gchar   *region)
{
        CcRegionPanelPrivate *priv = self->priv;

        if (g_strcmp0 (region, priv->system_region) == 0)
                return;

        g_free (priv->system_region);
        priv->system_region = g_strdup (region);

        set_localed_locale (self);
}

405
static void
Matthias Clasen's avatar
Matthias Clasen committed
406
407
408
409
update_region (CcRegionPanel *self,
               const gchar   *region)
{
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
410

411
412
413
414
415
416
417
418
419
420
        if (priv->login) {
                set_system_region (self, region);
        } else {
                if (g_strcmp0 (region, priv->region) == 0)
                        return;
                g_settings_set_string (priv->locale_settings, KEY_REGION, region);
                if (priv->login_auto_apply)
                        set_system_region (self, region);
                maybe_notify (self, LC_TIME, region);
        }
Matthias Clasen's avatar
Matthias Clasen committed
421
422
423
424
425
426
}

static void
format_response (GtkDialog *chooser,
                 gint       response_id,
                 CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
427
{
Matthias Clasen's avatar
Matthias Clasen committed
428
        const gchar *region;
Matthias Clasen's avatar
Matthias Clasen committed
429
430
431

        if (response_id == GTK_RESPONSE_OK) {
                region = cc_format_chooser_get_region (GTK_WIDGET (chooser));
432
                update_region (self, region);
Matthias Clasen's avatar
Matthias Clasen committed
433
        }
Matthias Clasen's avatar
Matthias Clasen committed
434

Matthias Clasen's avatar
Matthias Clasen committed
435
436
437
        gtk_widget_destroy (GTK_WIDGET (chooser));
}

438
439
440
441
442
443
444
445
446
447
static const gchar *
get_effective_language (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
        if (priv->login)
                return priv->system_language;
        else
                return priv->language;
}

Matthias Clasen's avatar
Matthias Clasen committed
448
static void
449
show_language_chooser (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
450
{
Matthias Clasen's avatar
Matthias Clasen committed
451
        GtkWidget *toplevel;
Matthias Clasen's avatar
Matthias Clasen committed
452
        GtkWidget *chooser;
Matthias Clasen's avatar
Matthias Clasen committed
453
454
455

        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
        chooser = cc_language_chooser_new (toplevel);
456
        cc_language_chooser_set_language (chooser, get_effective_language (self));
Matthias Clasen's avatar
Matthias Clasen committed
457
458
459
460
461
        g_signal_connect (chooser, "response",
                          G_CALLBACK (language_response), self);
        gtk_window_present (GTK_WINDOW (chooser));
}

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
static const gchar *
get_effective_region (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
        const gchar *region;

        if (priv->login)
                region = priv->system_region;
        else
                region = priv->region;

        /* Region setting might be empty - show the language because
         * that's what LC_TIME and others will effectively be when the
         * user logs in again. */
        if (region == NULL || region[0] == '\0')
                region = get_effective_language (self);

        return region;
}

482
static void
483
show_region_chooser (CcRegionPanel *self)
484
485
486
487
488
489
{
        GtkWidget *toplevel;
        GtkWidget *chooser;

        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
        chooser = cc_format_chooser_new (toplevel);
490
        cc_format_chooser_set_region (chooser, get_effective_region (self));
491
492
493
494
495
        g_signal_connect (chooser, "response",
                          G_CALLBACK (format_response), self);
        gtk_window_present (GTK_WINDOW (chooser));
}

Matthias Clasen's avatar
Matthias Clasen committed
496
497
static void show_input_chooser (CcRegionPanel *self);
static void remove_selected_input (CcRegionPanel *self);
498
499
static void move_selected_input (CcRegionPanel *self,
                                 SystemOp       op);
Matthias Clasen's avatar
Matthias Clasen committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521

static void
permission_acquired (GObject      *source,
                     GAsyncResult *res,
                     gpointer      data)
{
        CcRegionPanel *self = data;
	CcRegionPanelPrivate *priv = self->priv;
        GError *error = NULL;
        gboolean allowed;

        allowed = g_permission_acquire_finish (priv->permission, res, &error);
        if (error) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("Failed to acquire permission: %s\n", error->message);
                g_error_free (error);
                return;
        }

        if (allowed) {
                switch (priv->op) {
                case CHOOSE_LANGUAGE:
522
                        show_language_chooser (self);
Matthias Clasen's avatar
Matthias Clasen committed
523
                        break;
524
                case CHOOSE_REGION:
525
                        show_region_chooser (self);
526
                        break;
Matthias Clasen's avatar
Matthias Clasen committed
527
528
529
530
531
532
                case ADD_INPUT:
                        show_input_chooser (self);
                        break;
                case REMOVE_INPUT:
                        remove_selected_input (self);
                        break;
533
534
535
536
                case MOVE_UP_INPUT:
                case MOVE_DOWN_INPUT:
                        move_selected_input (self, priv->op);
                        break;
Matthias Clasen's avatar
Matthias Clasen committed
537
538
539
540
541
542
543
544
                default:
                        g_warning ("Unknown privileged operation: %d\n", priv->op);
                        break;
                }
        }
}

static void
545
546
activate_language_row (CcRegionPanel *self,
                       GtkListBoxRow *row)
Matthias Clasen's avatar
Matthias Clasen committed
547
548
549
{
	CcRegionPanelPrivate *priv = self->priv;

550
        if (row == priv->language_row) {
551
552
                if (!priv->login || g_permission_get_allowed (priv->permission)) {
                        show_language_chooser (self);
Matthias Clasen's avatar
Matthias Clasen committed
553
554
555
556
557
558
559
                } else if (g_permission_get_can_acquire (priv->permission)) {
                        priv->op = CHOOSE_LANGUAGE;
                        g_permission_acquire_async (priv->permission,
                                                    NULL,
                                                    permission_acquired,
                                                    self);
                }
560
        } else if (row == priv->formats_row) {
561
562
                if (!priv->login || g_permission_get_allowed (priv->permission)) {
                        show_region_chooser (self);
563
564
565
566
567
568
569
                } else if (g_permission_get_can_acquire (priv->permission)) {
                        priv->op = CHOOSE_REGION;
                        g_permission_acquire_async (priv->permission,
                                                    NULL,
                                                    permission_acquired,
                                                    self);
                }
Matthias Clasen's avatar
Matthias Clasen committed
570
571
572
        }
}

573
574
575
576
static void
update_region_label (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
577
        const gchar *region = get_effective_region (self);
578
        gchar *name = NULL;
579

580
581
582
583
584
585
        if (region)
                name = gnome_get_country_from_locale (region, region);

        if (!name)
                name = gnome_get_country_from_locale (DEFAULT_LOCALE, DEFAULT_LOCALE);

586
587
588
589
590
591
592
593
594
595
596
597
598
599
        gtk_label_set_label (GTK_LABEL (priv->formats_label), name);
        g_free (name);
}

static void
update_region_from_setting (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;

        g_free (priv->region);
        priv->region = g_settings_get_string (priv->locale_settings, KEY_REGION);
        update_region_label (self);
}

Matthias Clasen's avatar
Matthias Clasen committed
600
static void
Matthias Clasen's avatar
Matthias Clasen committed
601
update_language_label (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
602
603
{
	CcRegionPanelPrivate *priv = self->priv;
604
        const gchar *language = get_effective_language (self);
605
        gchar *name = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
606

607
        if (language)
608
                name = gnome_get_language_from_locale (language, language);
609
610
611
612

        if (!name)
                name = gnome_get_language_from_locale (DEFAULT_LOCALE, DEFAULT_LOCALE);

Matthias Clasen's avatar
Matthias Clasen committed
613
614
        gtk_label_set_label (GTK_LABEL (priv->language_label), name);
        g_free (name);
615
616
617

        /* Formats will change too if not explicitly set. */
        update_region_label (self);
Matthias Clasen's avatar
Matthias Clasen committed
618
619
620
621
622
623
}

static void
update_language_from_user (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
624
        const gchar *language = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
625

Matthias Clasen's avatar
Matthias Clasen committed
626
627
        if (act_user_is_loaded (priv->user))
                language = act_user_get_language (priv->user);
628
629

        if (language == NULL || *language == '\0')
630
                language = setlocale (LC_MESSAGES, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
631
632
633

        g_free (priv->language);
        priv->language = g_strdup (language);
Matthias Clasen's avatar
Matthias Clasen committed
634
        update_language_label (self);
Matthias Clasen's avatar
Matthias Clasen committed
635
636
637
638
639
640
641
}

static void
setup_language_section (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *widget;
Matthias Clasen's avatar
Matthias Clasen committed
642
643
644
645
646
647

        priv->user = act_user_manager_get_user_by_id (priv->user_manager, getuid ());
        g_signal_connect_swapped (priv->user, "notify::language",
                                  G_CALLBACK (update_language_from_user), self);
        g_signal_connect_swapped (priv->user, "notify::is-loaded",
                                  G_CALLBACK (update_language_from_user), self);
Matthias Clasen's avatar
Matthias Clasen committed
648
649
650

        priv->locale_settings = g_settings_new (GNOME_SYSTEM_LOCALE_DIR);
        g_signal_connect_swapped (priv->locale_settings, "changed::" KEY_REGION,
Matthias Clasen's avatar
Matthias Clasen committed
651
                                  G_CALLBACK (update_region_from_setting), self);
Matthias Clasen's avatar
Matthias Clasen committed
652

Matthias Clasen's avatar
Matthias Clasen committed
653
        priv->language_section = WID ("language_section");
654
        priv->language_row = GTK_LIST_BOX_ROW (WID ("language_row"));
Matthias Clasen's avatar
Matthias Clasen committed
655
        priv->language_label = WID ("language_label");
656
        priv->formats_row = GTK_LIST_BOX_ROW (WID ("formats_row"));
Matthias Clasen's avatar
Matthias Clasen committed
657
658
        priv->formats_label = WID ("formats_label");

659
        priv->restart_notification = WID ("restart-revealer");
660
        widget = WID ("restart-button");
661
662
        g_signal_connect_swapped (widget, "clicked", G_CALLBACK (restart_now), self);

Matthias Clasen's avatar
Matthias Clasen committed
663
        widget = WID ("language_list");
664
        gtk_list_box_set_selection_mode (GTK_LIST_BOX (widget),
Matthias Clasen's avatar
Matthias Clasen committed
665
                                         GTK_SELECTION_NONE);
666
        gtk_list_box_set_header_func (GTK_LIST_BOX (widget),
667
                                      cc_list_box_update_header_func,
668
669
670
                                      NULL, NULL);
        g_signal_connect_swapped (widget, "row-activated",
                                  G_CALLBACK (activate_language_row), self);
Matthias Clasen's avatar
Matthias Clasen committed
671

Matthias Clasen's avatar
Matthias Clasen committed
672
        update_language_from_user (self);
673
        update_region_from_setting (self);
Matthias Clasen's avatar
Matthias Clasen committed
674
}
Matthias Clasen's avatar
Matthias Clasen committed
675

Matthias Clasen's avatar
Matthias Clasen committed
676
677
678
679
680
681
682
683
684
685
686
687
#ifdef HAVE_IBUS
static void
update_ibus_active_sources (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
        GList *rows, *l;
        GtkWidget *row;
        const gchar *type;
        const gchar *id;
        IBusEngineDesc *engine_desc;
        gchar *display_name;
        GtkWidget *label;
Matthias Clasen's avatar
Matthias Clasen committed
688

Matthias Clasen's avatar
Matthias Clasen committed
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
        rows = gtk_container_get_children (GTK_CONTAINER (priv->input_list));
        for (l = rows; l; l = l->next) {
                row = l->data;
                type = g_object_get_data (G_OBJECT (row), "type");
                id = g_object_get_data (G_OBJECT (row), "id");
                if (g_strcmp0 (type, INPUT_SOURCE_TYPE_IBUS) != 0)
                        continue;

                engine_desc = g_hash_table_lookup (priv->ibus_engines, id);
                if (engine_desc) {
                        display_name = engine_get_display_name (engine_desc);
                        label = GTK_WIDGET (g_object_get_data (G_OBJECT (row), "label"));
                        gtk_label_set_text (GTK_LABEL (label), display_name);
                        g_free (display_name);
                }
        }
        g_list_free (rows);
}
Matthias Clasen's avatar
Matthias Clasen committed
707

708
709
710
711
712
713
714
715
716
717
718
719
720
static void
update_input_chooser (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *chooser;

        chooser = g_object_get_data (G_OBJECT (self), "input-chooser");
        if (!chooser)
                return;

        cc_input_chooser_set_ibus_engines (chooser, priv->ibus_engines);
}

Matthias Clasen's avatar
Matthias Clasen committed
721
722
723
724
725
static void
fetch_ibus_engines_result (GObject       *object,
                           GAsyncResult  *result,
                           CcRegionPanel *self)
{
726
        CcRegionPanelPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
727
728
729
730
        GList *list, *l;
        GError *error;

        error = NULL;
731
        list = ibus_bus_list_engines_async_finish (IBUS_BUS (object), result, &error);
Matthias Clasen's avatar
Matthias Clasen committed
732
        if (!list && error) {
733
734
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("Couldn't finish IBus request: %s", error->message);
Matthias Clasen's avatar
Matthias Clasen committed
735
736
737
738
                g_error_free (error);
                return;
        }

739
740
741
        priv = self->priv;
        g_clear_object (&priv->ibus_cancellable);

Matthias Clasen's avatar
Matthias Clasen committed
742
743
744
745
746
747
748
        /* Maps engine ids to engine description objects */
        priv->ibus_engines = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);

        for (l = list; l; l = l->next) {
                IBusEngineDesc *engine = l->data;
                const gchar *engine_id = ibus_engine_desc_get_name (engine);

749
                if (g_str_has_prefix (engine_id, "xkb:"))
Matthias Clasen's avatar
Matthias Clasen committed
750
                        g_object_unref (engine);
751
752
                else
                        g_hash_table_replace (priv->ibus_engines, (gpointer)engine_id, engine);
Matthias Clasen's avatar
Matthias Clasen committed
753
754
755
756
        }
        g_list_free (list);

        update_ibus_active_sources (self);
757
        update_input_chooser (self);
Matthias Clasen's avatar
Matthias Clasen committed
758
}
Matthias Clasen's avatar
Matthias Clasen committed
759

Matthias Clasen's avatar
Matthias Clasen committed
760
761
762
763
static void
fetch_ibus_engines (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
764

Matthias Clasen's avatar
Matthias Clasen committed
765
        priv->ibus_cancellable = g_cancellable_new ();
Matthias Clasen's avatar
Matthias Clasen committed
766

Matthias Clasen's avatar
Matthias Clasen committed
767
768
769
770
771
        ibus_bus_list_engines_async (priv->ibus,
                                     -1,
                                     priv->ibus_cancellable,
                                     (GAsyncReadyCallback)fetch_ibus_engines_result,
                                     self);
Matthias Clasen's avatar
Matthias Clasen committed
772

Matthias Clasen's avatar
Matthias Clasen committed
773
774
  /* We've got everything we needed, don't want to be called again. */
  g_signal_handlers_disconnect_by_func (priv->ibus, fetch_ibus_engines, self);
Matthias Clasen's avatar
Matthias Clasen committed
775
776
777
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
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
maybe_start_ibus (void)
{
        /* IBus doesn't export API in the session bus. The only thing
         * we have there is a well known name which we can use as a
         * sure-fire way to activate it.
         */
        g_bus_unwatch_name (g_bus_watch_name (G_BUS_TYPE_SESSION,
                                              IBUS_SERVICE_IBUS,
                                              G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
                                              NULL,
                                              NULL,
                                              NULL,
                                              NULL));
}

static GDesktopAppInfo *
setup_app_info_for_id (const gchar *id)
{
  GDesktopAppInfo *app_info;
  gchar *desktop_file_name;
  gchar **strv;

  strv = g_strsplit (id, ":", 2);
  desktop_file_name = g_strdup_printf ("ibus-setup-%s.desktop", strv[0]);
  g_strfreev (strv);

  app_info = g_desktop_app_info_new (desktop_file_name);
  g_free (desktop_file_name);

  return app_info;
}
#endif

811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
static void
remove_no_input_row (GtkContainer *list)
{
        GList *l;

        l = gtk_container_get_children (list);
        if (!l)
                return;
        if (l->next != NULL)
                goto out;
        if (g_strcmp0 (g_object_get_data (G_OBJECT (l->data), "type"), "none") == 0)
                gtk_container_remove (list, GTK_WIDGET (l->data));
out:
        g_list_free (l);
}

Matthias Clasen's avatar
Matthias Clasen committed
827
828
829
830
831
832
static GtkWidget *
add_input_row (CcRegionPanel   *self,
               const gchar     *type,
               const gchar     *id,
               const gchar     *name,
               GDesktopAppInfo *app_info)
Matthias Clasen's avatar
Matthias Clasen committed
833
{
834
        CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
835
        GtkWidget *row;
836
        GtkWidget *box;
Matthias Clasen's avatar
Matthias Clasen committed
837
        GtkWidget *label;
Matthias Clasen's avatar
Matthias Clasen committed
838
        GtkWidget *image;
Matthias Clasen's avatar
Matthias Clasen committed
839

840
        remove_no_input_row (GTK_CONTAINER (priv->input_list));
841

842
843
844
        row = gtk_list_box_row_new ();
        box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
        gtk_container_add (GTK_CONTAINER (row), box);
Matthias Clasen's avatar
Matthias Clasen committed
845
        label = gtk_label_new (name);
Rui Matos's avatar
Rui Matos committed
846
        gtk_widget_set_halign (label, GTK_ALIGN_START);
847
848
        gtk_widget_set_margin_start (label, 20);
        gtk_widget_set_margin_end (label, 20);
Allan Day's avatar
Allan Day committed
849
850
        gtk_widget_set_margin_top (label, 12);
        gtk_widget_set_margin_bottom (label, 12);
851
        gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
852
853
854

        if (strcmp (type, INPUT_SOURCE_TYPE_IBUS) == 0) {
                image = gtk_image_new_from_icon_name ("system-run-symbolic", GTK_ICON_SIZE_BUTTON);
855
856
                gtk_widget_set_margin_start (image, 20);
                gtk_widget_set_margin_end (image, 20);
Matthias Clasen's avatar
Matthias Clasen committed
857
858
                gtk_widget_set_margin_top (image, 6);
                gtk_widget_set_margin_bottom (image, 6);
859
                gtk_style_context_add_class (gtk_widget_get_style_context (image), "dim-label");
860
                gtk_box_pack_start (GTK_BOX (box), image, FALSE, TRUE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
861
862
        }

Matthias Clasen's avatar
Matthias Clasen committed
863
        gtk_widget_show_all (row);
Matthias Clasen's avatar
Matthias Clasen committed
864
865
866
867
868
869
870
871
872
        gtk_container_add (GTK_CONTAINER (self->priv->input_list), row);

        g_object_set_data (G_OBJECT (row), "label", label);
        g_object_set_data (G_OBJECT (row), "type", (gpointer)type);
        g_object_set_data_full (G_OBJECT (row), "id", g_strdup (id), g_free);
        if (app_info) {
                g_object_set_data_full (G_OBJECT (row), "app-info", g_object_ref (app_info), g_object_unref);
        }

873
        cc_list_box_adjust_scrolling (GTK_LIST_BOX (priv->input_list));
874

Matthias Clasen's avatar
Matthias Clasen committed
875
        return row;
Matthias Clasen's avatar
Matthias Clasen committed
876
877
}

878
879
880
881
882
883
static void
add_no_input_row (CcRegionPanel *self)
{
        add_input_row (self, "none", "none", _("No input source selected"), NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
884
static void
Matthias Clasen's avatar
Matthias Clasen committed
885
886
add_input_sources (CcRegionPanel *self,
                   GVariant      *sources)
Matthias Clasen's avatar
Matthias Clasen committed
887
{
Matthias Clasen's avatar
Matthias Clasen committed
888
889
890
891
892
893
894
895
        CcRegionPanelPrivate *priv = self->priv;
        GVariantIter iter;
        const gchar *type;
        const gchar *id;
        const gchar *name;
        gchar *display_name;
        GDesktopAppInfo *app_info;

896
897
898
899
900
        if (g_variant_n_children (sources) < 1) {
                add_no_input_row (self);
                return;
        }

Matthias Clasen's avatar
Matthias Clasen committed
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
        g_variant_iter_init (&iter, sources);
        while (g_variant_iter_next (&iter, "(&s&s)", &type, &id)) {
                display_name = NULL;
                app_info = NULL;

                if (g_str_equal (type, INPUT_SOURCE_TYPE_XKB)) {
                        gnome_xkb_info_get_layout_info (priv->xkb_info, id, &name, NULL, NULL, NULL);
                        if (!name) {
                                g_warning ("Couldn't find XKB input source '%s'", id);
                                continue;
                        }
                        display_name = g_strdup (name);
                        type = INPUT_SOURCE_TYPE_XKB;
#ifdef HAVE_IBUS
                } else if (g_str_equal (type, INPUT_SOURCE_TYPE_IBUS)) {
                        IBusEngineDesc *engine_desc = NULL;

                        if (priv->ibus_engines)
                                engine_desc = g_hash_table_lookup (priv->ibus_engines, id);
                        if (engine_desc)
                                display_name = engine_get_display_name (engine_desc);

                        app_info = setup_app_info_for_id (id);
                        type = INPUT_SOURCE_TYPE_IBUS;
#endif
                } else {
                        g_warning ("Unhandled input source type '%s'", type);
                        continue;
                }

                add_input_row (self, type, id, display_name ? display_name : id, app_info);
                g_free (display_name);
                g_clear_object (&app_info);
        }
Matthias Clasen's avatar
Matthias Clasen committed
935
936
937
938
939
940
941
942
943
}

static void
add_input_sources_from_settings (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GVariant *sources;
        sources = g_settings_get_value (priv->input_settings, "sources");
        add_input_sources (self, sources);
Matthias Clasen's avatar
Matthias Clasen committed
944
945
        g_variant_unref (sources);
}
Matthias Clasen's avatar
Matthias Clasen committed
946

Matthias Clasen's avatar
Matthias Clasen committed
947
static void
Matthias Clasen's avatar
Matthias Clasen committed
948
clear_input_sources (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
949
{
Matthias Clasen's avatar
Matthias Clasen committed
950
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
951
        GList *list, *l;
Matthias Clasen's avatar
Matthias Clasen committed
952
        list = gtk_container_get_children (GTK_CONTAINER (priv->input_list));
Matthias Clasen's avatar
Matthias Clasen committed
953
        for (l = list; l; l = l->next) {
Matthias Clasen's avatar
Matthias Clasen committed
954
                gtk_container_remove (GTK_CONTAINER (priv->input_list), GTK_WIDGET (l->data));
Matthias Clasen's avatar
Matthias Clasen committed
955
956
        }
        g_list_free (list);
957

958
        cc_list_box_adjust_scrolling (GTK_LIST_BOX (priv->input_list));
Matthias Clasen's avatar
Matthias Clasen committed
959
}
Matthias Clasen's avatar
Matthias Clasen committed
960

Matthias Clasen's avatar
Matthias Clasen committed
961
962
963
964
965
966
static void
select_by_id (GtkWidget   *row,
              gpointer     data)
{
        const gchar *id = data;
        const gchar *row_id;
Matthias Clasen's avatar
Matthias Clasen committed
967

Matthias Clasen's avatar
Matthias Clasen committed
968
969
        row_id = (const gchar *)g_object_get_data (G_OBJECT (row), "id");
        if (g_strcmp0 (row_id, id) == 0)
970
                gtk_list_box_select_row (GTK_LIST_BOX (gtk_widget_get_parent (row)), GTK_LIST_BOX_ROW (row));
Matthias Clasen's avatar
Matthias Clasen committed
971
972
973
974
975
976
977
978
}

static void
select_input (CcRegionPanel *self,
              const gchar   *id)
{
        gtk_container_foreach (GTK_CONTAINER (self->priv->input_list),
                               select_by_id, (gpointer)id);
Matthias Clasen's avatar
Matthias Clasen committed
979
980
981
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
982
983
984
input_sources_changed (GSettings     *settings,
                       const gchar   *key,
                       CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
985
986
{
	CcRegionPanelPrivate *priv = self->priv;
987
        GtkListBoxRow *selected;
988
        gchar *id = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
989

990
        selected = gtk_list_box_get_selected_row (GTK_LIST_BOX (priv->input_list));
Matthias Clasen's avatar
Matthias Clasen committed
991
        if (selected)
992
                id = g_strdup (g_object_get_data (G_OBJECT (selected), "id"));
Matthias Clasen's avatar
Matthias Clasen committed
993
994
        clear_input_sources (self);
        add_input_sources_from_settings (self);
995
        if (id) {
Matthias Clasen's avatar
Matthias Clasen committed
996
                select_input (self, id);
997
998
                g_free (id);
        }
Matthias Clasen's avatar
Matthias Clasen committed
999
}
Matthias Clasen's avatar
Matthias Clasen committed
1000
1001


Matthias Clasen's avatar
Matthias Clasen committed
1002
static void
1003
update_buttons (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
1004
1005
{
	CcRegionPanelPrivate *priv = self->priv;
1006
        GtkListBoxRow *selected;
Matthias Clasen's avatar
Matthias Clasen committed
1007
        GList *children;
1008
        guint n_rows;
Matthias Clasen's avatar
Matthias Clasen committed
1009
1010

        children = gtk_container_get_children (GTK_CONTAINER (priv->input_list));
1011
        n_rows = g_list_length (children);
Matthias Clasen's avatar
Matthias Clasen committed
1012
1013
        g_list_free (children);

1014
        selected = gtk_list_box_get_selected_row (GTK_LIST_BOX (priv->input_list));
Matthias Clasen's avatar
Matthias Clasen committed
1015
        if (selected == NULL) {
1016
                gtk_widget_set_visible (priv->show_config, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
1017
1018
                gtk_widget_set_sensitive (priv->remove_input, FALSE);
                gtk_widget_set_sensitive (priv->show_layout, FALSE);
1019
1020
                gtk_widget_set_sensitive (priv->move_up_input, FALSE);
                gtk_widget_set_sensitive (priv->move_down_input, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
1021
1022
1023
1024
1025
        } else {
                GDesktopAppInfo *app_info;

                app_info = (GDesktopAppInfo *)g_object_get_data (G_OBJECT (selected), "app-info");

1026
                gtk_widget_set_visible (priv->show_config, app_info != NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1027
                gtk_widget_set_sensitive (priv->show_layout, TRUE);
1028
1029
1030
                gtk_widget_set_sensitive (priv->remove_input, n_rows > 1);
                gtk_widget_set_sensitive (priv->move_up_input, gtk_list_box_row_get_index (selected) > 0);
                gtk_widget_set_sensitive (priv->move_down_input, gtk_list_box_row_get_index (selected) < n_rows - 1);
Matthias Clasen's avatar
Matthias Clasen committed
1031
        }
Matthias Clasen's avatar
Matthias Clasen committed
1032
1033

        gtk_widget_set_visible (priv->options_button,
1034
                                n_rows > 1 && !priv->login);
Matthias Clasen's avatar
Matthias Clasen committed
1035
}
Matthias Clasen's avatar
Matthias Clasen committed
1036

Matthias Clasen's avatar
Matthias Clasen committed
1037
static void
Matthias Clasen's avatar
Matthias Clasen committed
1038
set_input_settings (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
{
	CcRegionPanelPrivate *priv = self->priv;
        const gchar *type;
        const gchar *id;
        GVariantBuilder builder;
        GList *list, *l;

        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ss)"));
        list = gtk_container_get_children (GTK_CONTAINER (priv->input_list));
        for (l = list; l; l = l->next) {
                type = (const gchar *)g_object_get_data (G_OBJECT (l->data), "type");
                id = (const gchar *)g_object_get_data (G_OBJECT (l->data), "id");
                g_variant_builder_add (&builder, "(ss)", type, id);
        }
        g_list_free (list);

        g_settings_set_value (priv->input_settings, KEY_INPUT_SOURCES, g_variant_builder_end (&builder));
        g_settings_apply (priv->input_settings);
}
Matthias Clasen's avatar
Matthias Clasen committed
1058

Matthias Clasen's avatar
Matthias Clasen committed
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
static void set_localed_input (CcRegionPanel *self);

static void
update_input (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;

        if (priv->login) {
                set_localed_input (self);
        } else {
                set_input_settings (self);
1070
1071
                if (priv->login_auto_apply)
                        set_localed_input (self);
Matthias Clasen's avatar
Matthias Clasen committed
1072
1073
1074
        }
}

1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
static gboolean
input_source_already_added (CcRegionPanel *self,
                            const gchar   *id)
{
        CcRegionPanelPrivate *priv = self->priv;
        GList *list, *l;
        gboolean retval = FALSE;

        list = gtk_container_get_children (GTK_CONTAINER (priv->input_list));
        for (l = list; l; l = l->next)
                if (g_str_equal (id, (const gchar *) g_object_get_data (G_OBJECT (l->data), "id"))) {
                        retval = TRUE;
                        break;
                }
        g_list_free (list);

        return retval;
}

Matthias Clasen's avatar
Matthias Clasen committed
1094
static void
1095
run_input_chooser (CcRegionPanel *self, GtkWidget *chooser)
Matthias Clasen's avatar
Matthias Clasen committed
1096
1097
1098
1099
1100
1101
{
        gchar *type;
        gchar *id;
        gchar *name;
        GDesktopAppInfo *app_info = NULL;

1102
        if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_OK) {
1103
1104
                if (cc_input_chooser_get_selected (chooser, &type, &id, &name) &&
                    !input_source_already_added (self, id)) {
Matthias Clasen's avatar
Matthias Clasen committed
1105
1106
1107
                        if (g_str_equal (type, INPUT_SOURCE_TYPE_IBUS)) {
                                g_free (type);
                                type = INPUT_SOURCE_TYPE_IBUS;
Matthias Clasen's avatar
Matthias Clasen committed
1108
#ifdef HAVE_IBUS
Matthias Clasen's avatar
Matthias Clasen committed
1109
                                app_info = setup_app_info_for_id (id);
Matthias Clasen's avatar
Matthias Clasen committed
1110
#endif
Matthias Clasen's avatar
Matthias Clasen committed
1111
1112
1113
1114
                        } else {
                                g_free (type);
                                type = INPUT_SOURCE_TYPE_XKB;
                        }
Matthias Clasen's avatar
Matthias Clasen committed
1115

1116
1117
1118
1119
                        add_input_row (self, type, id, name, app_info);
                        update_buttons (self);
                        update_input (self);

Matthias Clasen's avatar
Matthias Clasen committed
1120
1121
1122
1123
                        g_free (id);
                        g_free (name);
                        g_clear_object (&app_info);
                }
Matthias Clasen's avatar
Matthias Clasen committed
1124
        }
1125
        gtk_widget_hide(chooser);
Matthias Clasen's avatar
Matthias Clasen committed
1126
1127
1128
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1129
show_input_chooser (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
1130
1131
1132
1133
1134
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *chooser;
        GtkWidget *toplevel;

1135
1136
1137
1138
1139
1140
1141
        chooser = g_object_get_data (G_OBJECT (self), "input-chooser");

        if (!chooser) {
                toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
                chooser = cc_input_chooser_new (GTK_WINDOW (toplevel),
                                                priv->login,
                                                priv->xkb_info,
1142
#ifdef HAVE_IBUS
1143
                                                priv->ibus_engines
1144
#else
1145
                                                NULL
1146
#endif
1147
1148
1149
1150
1151
1152
1153
                                                );
                g_object_ref (chooser);
                g_object_set_data_full (G_OBJECT (self), "input-chooser",
                                        chooser, g_object_unref);
        } else {
                cc_input_chooser_reset (chooser);
        }
1154

1155
        run_input_chooser (self, chooser);
Matthias Clasen's avatar
Matthias Clasen committed
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
}

static void
add_input (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;

        if (!priv->login) {
                show_input_chooser (self);
        } else if (g_permission_get_allowed (priv->permission)) {
                show_input_chooser (self);
        } else if (g_permission_get_can_acquire (priv->permission)) {
                priv->op = ADD_INPUT;
                g_permission_acquire_async (priv->permission,
                                            NULL,
                                            permission_acquired,
                                            self);
        }
Matthias Clasen's avatar
Matthias Clasen committed
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
}

static GtkWidget *
find_sibling (GtkContainer *container, GtkWidget *child)
{
        GList *list, *c;
        GList *l;
        GtkWidget *sibling;

        list = gtk_container_get_children (container);
        c = g_list_find (list, child);

        for (l = c->next; l; l = l->next) {
                sibling = l->data;
                if (gtk_widget_get_visible (sibling) && gtk_widget_get_child_visible (sibling))
                        goto out;
        }

        for (l = c->prev; l; l = l->prev) {
                sibling = l->data;
                if (gtk_widget_get_visible (sibling) && gtk_widget_get_child_visible (sibling))
                        goto out;
        }

        sibling = NULL;

out:
        g_list_free (list);

        return sibling;
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1207
do_remove_selected_input (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
1208
1209
{
	CcRegionPanelPrivate *priv = self->priv;
1210
        GtkListBoxRow *selected;
Matthias Clasen's avatar
Matthias Clasen committed
1211
1212
        GtkWidget *sibling;

1213
        selected = gtk_list_box_get_selected_row (GTK_LIST_BOX (priv->input_list));
Matthias Clasen's avatar
Matthias Clasen committed
1214
1215
1216
        if (selected == NULL)
                return;

1217
1218
1219
        sibling = find_sibling (GTK_CONTAINER (priv->input_list), GTK_WIDGET (selected));
        gtk_container_remove (GTK_CONTAINER (priv->input_list), GTK_WIDGET (selected));
        gtk