cc-region-panel.c 59.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
Sergey V. Udaltsov's avatar
Sergey V. Udaltsov committed
18
 * Author: Sergey Udaltsov <svu@gnome.org>
19
20
21
 *
 */

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

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
#include "egg-list-box/egg-list-box.h"
Matthias Clasen's avatar
Matthias Clasen committed
51
#include <libgd/gd-notification.h>
Matthias Clasen's avatar
Matthias Clasen committed
52

Matthias Clasen's avatar
Matthias Clasen committed
53
54
55
56
57
58
59
60
61
62
#define GNOME_DESKTOP_INPUT_SOURCES_DIR "org.gnome.desktop.input-sources"
#define KEY_CURRENT_INPUT_SOURCE "current"
#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"

63
64
#define MAX_INPUT_ROWS_VISIBLE 5

Ray Strode's avatar
Ray Strode committed
65
CC_PANEL_REGISTER (CcRegionPanel, cc_region_panel)
66

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

69
70
#define REGION_PANEL_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_REGION_PANEL, CcRegionPanelPrivate))

Matthias Clasen's avatar
Matthias Clasen committed
71
72
73
74
75
76
typedef enum {
        CHOOSE_LANGUAGE,
        ADD_INPUT,
        REMOVE_INPUT
} SystemOp;

77
struct _CcRegionPanelPrivate {
78
79
	GtkBuilder *builder;

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

Matthias Clasen's avatar
Matthias Clasen committed
90
91
        GtkWidget *overlay;
        GtkWidget *notification;
Matthias Clasen's avatar
Matthias Clasen committed
92

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

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

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

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

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

127
static void
Matthias Clasen's avatar
Matthias Clasen committed
128
cc_region_panel_finalize (GObject *object)
129
{
Matthias Clasen's avatar
Matthias Clasen committed
130
131
	CcRegionPanel *self = CC_REGION_PANEL (object);
	CcRegionPanelPrivate *priv = self->priv;
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

Matthias Clasen's avatar
Matthias Clasen committed
165
	G_OBJECT_CLASS (cc_region_panel_parent_class)->finalize (object);
166
167
168
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
169
cc_region_panel_constructed (GObject *object)
170
{
Matthias Clasen's avatar
Matthias Clasen committed
171
        CcRegionPanel *self = CC_REGION_PANEL (object);
Matthias Clasen's avatar
Matthias Clasen committed
172
	CcRegionPanelPrivate *priv = self->priv;
173

Matthias Clasen's avatar
Matthias Clasen committed
174
        G_OBJECT_CLASS (cc_region_panel_parent_class)->constructed (object);
175

Matthias Clasen's avatar
Matthias Clasen committed
176
        cc_shell_embed_widget_in_header (cc_panel_get_shell (CC_PANEL (object)),
Matthias Clasen's avatar
Matthias Clasen committed
177
                                         priv->login_button);
178
179
}

180
181
182
static const char *
cc_region_panel_get_help_uri (CcPanel *panel)
{
Matthias Clasen's avatar
Matthias Clasen committed
183
        return "help:gnome-help/prefs-language";
184
185
}

186
static void
187
cc_region_panel_class_init (CcRegionPanelClass * klass)
188
189
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
190
	CcPanelClass * panel_class = CC_PANEL_CLASS (klass);
191

192
	g_type_class_add_private (klass, sizeof (CcRegionPanelPrivate));
193

194
195
	panel_class->get_help_uri = cc_region_panel_get_help_uri;

Matthias Clasen's avatar
Matthias Clasen committed
196
        object_class->constructed = cc_region_panel_constructed;
197
	object_class->finalize = cc_region_panel_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
198
}
199

Matthias Clasen's avatar
Matthias Clasen committed
200
201
202
static void
restart_now (CcRegionPanel *self)
{
203
        CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
204
205
206

        gd_notification_dismiss (GD_NOTIFICATION (self->priv->notification));

207
208
209
210
211
        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
212
213
214
}

static void
215
216
show_restart_notification (CcRegionPanel *self,
                           const gchar   *locale)
Matthias Clasen's avatar
Matthias Clasen committed
217
218
219
220
221
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *box;
        GtkWidget *label;
        GtkWidget *button;
222
        gchar *current_locale;
Matthias Clasen's avatar
Matthias Clasen committed
223
224
225
226

        if (priv->notification)
                return;

227
228
229
230
231
        if (locale) {
                current_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
                setlocale (LC_MESSAGES, locale);
        }

Matthias Clasen's avatar
Matthias Clasen committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
        priv->notification = gd_notification_new ();
        g_object_add_weak_pointer (G_OBJECT (priv->notification),
                                   (gpointer *)&priv->notification);
        box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 24);
        gtk_widget_set_margin_left (box, 12);
        gtk_widget_set_margin_right (box, 12);
        gtk_widget_set_margin_top (box, 6);
        gtk_widget_set_margin_bottom (box, 6);
        label = gtk_label_new (_("Your session needs to be restarted for changes to take effect"));
        button = gtk_button_new_with_label (_("Restart Now"));
        g_signal_connect_swapped (button, "clicked",
                                  G_CALLBACK (restart_now), self);
        gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
        gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
        gtk_widget_show_all (box);

        gtk_container_add (GTK_CONTAINER (priv->notification), box);
        gtk_overlay_add_overlay (GTK_OVERLAY (self->priv->overlay), priv->notification);
        gtk_widget_show (priv->notification);
251
252
253
254
255

        if (locale) {
                setlocale (LC_MESSAGES, current_locale);
                g_free (current_locale);
        }
Matthias Clasen's avatar
Matthias Clasen committed
256
257
}

Matthias Clasen's avatar
Matthias Clasen committed
258
259
260
261
262
263
static void
update_separator_func (GtkWidget **separator,
                       GtkWidget  *child,
                       GtkWidget  *before,
                       gpointer    user_data)
{
Matthias Clasen's avatar
Matthias Clasen committed
264
265
266
267
268
269
270
271
272
        if (before == NULL)
                return;

        if (*separator == NULL) {
                *separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
                g_object_ref_sink (*separator);
                gtk_widget_show (*separator);
        }
}
Matthias Clasen's avatar
Matthias Clasen committed
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
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)
                show_restart_notification (self,
                                           mnd->category == LC_MESSAGES ? mnd->target_locale : NULL);
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
356
static void set_localed_locale (CcRegionPanel *self);
Matthias Clasen's avatar
Matthias Clasen committed
357

358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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);
}

373
static void
Matthias Clasen's avatar
Matthias Clasen committed
374
375
376
377
378
update_language (CcRegionPanel *self,
                 const gchar   *language)
{
	CcRegionPanelPrivate *priv = self->priv;

Matthias Clasen's avatar
Matthias Clasen committed
379
        if (priv->login) {
380
                set_system_language (self, language);
Matthias Clasen's avatar
Matthias Clasen committed
381
382
        } else {
                if (g_strcmp0 (language, priv->language) == 0)
383
                        return;
Matthias Clasen's avatar
Matthias Clasen committed
384
                act_user_set_language (priv->user, language);
385
                if (priv->login_auto_apply)
386
                        set_system_language (self, language);
387
                maybe_notify (self, LC_MESSAGES, language);
Matthias Clasen's avatar
Matthias Clasen committed
388
        }
389
390
391
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
392
393
394
395
396
language_response (GtkDialog     *chooser,
                   gint           response_id,
                   CcRegionPanel *self)
{
        const gchar *language;
Matthias Clasen's avatar
Matthias Clasen committed
397
398
399

        if (response_id == GTK_RESPONSE_OK) {
                language = cc_language_chooser_get_language (GTK_WIDGET (chooser));
400
                update_language (self, language);
Matthias Clasen's avatar
Matthias Clasen committed
401
        }
Matthias Clasen's avatar
Matthias Clasen committed
402
403

        gtk_widget_destroy (GTK_WIDGET (chooser));
Matthias Clasen's avatar
Matthias Clasen committed
404
405
}

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

        if (g_strcmp0 (region, priv->region) == 0)
413
                return;
Matthias Clasen's avatar
Matthias Clasen committed
414
415

        g_settings_set_string (priv->locale_settings, KEY_REGION, region);
416
        maybe_notify (self, LC_TIME, region);
Matthias Clasen's avatar
Matthias Clasen committed
417
418
419
420
421
422
}

static void
format_response (GtkDialog *chooser,
                 gint       response_id,
                 CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
423
{
Matthias Clasen's avatar
Matthias Clasen committed
424
        const gchar *region;
Matthias Clasen's avatar
Matthias Clasen committed
425
426
427

        if (response_id == GTK_RESPONSE_OK) {
                region = cc_format_chooser_get_region (GTK_WIDGET (chooser));
428
                update_region (self, region);
Matthias Clasen's avatar
Matthias Clasen committed
429
        }
Matthias Clasen's avatar
Matthias Clasen committed
430

Matthias Clasen's avatar
Matthias Clasen committed
431
432
433
434
        gtk_widget_destroy (GTK_WIDGET (chooser));
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
435
436
show_language_chooser (CcRegionPanel *self,
                       const gchar   *language)
Matthias Clasen's avatar
Matthias Clasen committed
437
{
Matthias Clasen's avatar
Matthias Clasen committed
438
        GtkWidget *toplevel;
Matthias Clasen's avatar
Matthias Clasen committed
439
        GtkWidget *chooser;
Matthias Clasen's avatar
Matthias Clasen committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491

        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
        chooser = cc_language_chooser_new (toplevel);
        cc_language_chooser_set_language (chooser, language);
        g_signal_connect (chooser, "response",
                          G_CALLBACK (language_response), self);
        gtk_window_present (GTK_WINDOW (chooser));
}

static void show_input_chooser (CcRegionPanel *self);
static void remove_selected_input (CcRegionPanel *self);

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:
                        show_language_chooser (self, priv->system_language);
                        break;
                case ADD_INPUT:
                        show_input_chooser (self);
                        break;
                case REMOVE_INPUT:
                        remove_selected_input (self);
                        break;
                default:
                        g_warning ("Unknown privileged operation: %d\n", priv->op);
                        break;
                }
        }
}

static void
show_format_chooser (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
492
        GtkWidget *toplevel;
Matthias Clasen's avatar
Matthias Clasen committed
493
        GtkWidget *chooser;
Matthias Clasen's avatar
Matthias Clasen committed
494
495

        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
Matthias Clasen's avatar
Matthias Clasen committed
496
497
498
499
500
501
502
503
504
505
506
507
        chooser = cc_format_chooser_new (toplevel);
        cc_format_chooser_set_region (chooser, priv->region);
        g_signal_connect (chooser, "response",
                          G_CALLBACK (format_response), self);
        gtk_window_present (GTK_WINDOW (chooser));
}

static void
activate_language_child (CcRegionPanel *self, GtkWidget *child)
{
	CcRegionPanelPrivate *priv = self->priv;

Matthias Clasen's avatar
Matthias Clasen committed
508
        if (child == priv->language_row) {
Matthias Clasen's avatar
Matthias Clasen committed
509
510
511
512
513
514
515
516
517
518
519
                if (!priv->login) {
                        show_language_chooser (self, priv->language);
                } else if (g_permission_get_allowed (priv->permission)) {
                        show_language_chooser (self, priv->system_language);
                } else if (g_permission_get_can_acquire (priv->permission)) {
                        priv->op = CHOOSE_LANGUAGE;
                        g_permission_acquire_async (priv->permission,
                                                    NULL,
                                                    permission_acquired,
                                                    self);
                }
Matthias Clasen's avatar
Matthias Clasen committed
520
        } else if (child == priv->formats_row) {
Matthias Clasen's avatar
Matthias Clasen committed
521
                show_format_chooser (self);
Matthias Clasen's avatar
Matthias Clasen committed
522
523
524
        }
}

525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
static void
update_region_label (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
        const gchar *region;
        gchar *name;

        if (priv->region == NULL || priv->region[0] == '\0')
                region = priv->language;
        else
                region = priv->region;

        name = gnome_get_country_from_locale (region, region);
        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
552
static void
Matthias Clasen's avatar
Matthias Clasen committed
553
update_language_label (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
554
555
{
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
556
557
558
        const gchar *language;
        gchar *name;

Matthias Clasen's avatar
Matthias Clasen committed
559
560
561
562
        if (priv->login)
                language = priv->system_language;
        else
                language = priv->language;
563
        if (language)
564
                name = gnome_get_language_from_locale (language, language);
565
566
        else
                name = g_strdup (C_("Language", "None"));
Matthias Clasen's avatar
Matthias Clasen committed
567
568
        gtk_label_set_label (GTK_LABEL (priv->language_label), name);
        g_free (name);
569
570
571

        /* Formats will change too if not explicitly set. */
        update_region_label (self);
Matthias Clasen's avatar
Matthias Clasen committed
572
573
574
575
576
577
578
579
}

static void
update_language_from_user (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        const gchar *language;

Matthias Clasen's avatar
Matthias Clasen committed
580
581
582
583
584
585
586
        if (act_user_is_loaded (priv->user))
                language = act_user_get_language (priv->user);
        else
                language = "en_US.utf-8";

        g_free (priv->language);
        priv->language = g_strdup (language);
Matthias Clasen's avatar
Matthias Clasen committed
587
        update_language_label (self);
Matthias Clasen's avatar
Matthias Clasen committed
588
589
590
591
592
593
594
}

static void
setup_language_section (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *widget;
Matthias Clasen's avatar
Matthias Clasen committed
595
596
597
598
599
600

        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
601
602
603

        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
604
                                  G_CALLBACK (update_region_from_setting), self);
Matthias Clasen's avatar
Matthias Clasen committed
605

Matthias Clasen's avatar
Matthias Clasen committed
606
        priv->language_section = WID ("language_section");
Matthias Clasen's avatar
Matthias Clasen committed
607
608
609
610
611
612
        priv->language_row = WID ("language_row");
        priv->language_label = WID ("language_label");
        priv->formats_row = WID ("formats_row");
        priv->formats_label = WID ("formats_label");

        widget = WID ("language_list");
Matthias Clasen's avatar
Matthias Clasen committed
613
614
615
616
617
618
        egg_list_box_set_selection_mode (EGG_LIST_BOX (widget),
                                         GTK_SELECTION_NONE);
        egg_list_box_set_separator_funcs (EGG_LIST_BOX (widget),
                                          update_separator_func,
                                          NULL, NULL);
        g_signal_connect_swapped (widget, "child-activated",
Matthias Clasen's avatar
Matthias Clasen committed
619
                                  G_CALLBACK (activate_language_child), self);
Matthias Clasen's avatar
Matthias Clasen committed
620

Matthias Clasen's avatar
Matthias Clasen committed
621
        update_language_from_user (self);
622
        update_region_from_setting (self);
Matthias Clasen's avatar
Matthias Clasen committed
623
}
Matthias Clasen's avatar
Matthias Clasen committed
624

Matthias Clasen's avatar
Matthias Clasen committed
625
626
627
628
629
630
631
632
633
634
635
636
#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
637

Matthias Clasen's avatar
Matthias Clasen committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
        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
656

657
658
659
660
661
662
663
664
665
666
667
668
669
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
static void
fetch_ibus_engines_result (GObject       *object,
                           GAsyncResult  *result,
                           CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
        GList *list, *l;
        GError *error;

        error = NULL;
        list = ibus_bus_list_engines_async_finish (priv->ibus, result, &error);
        g_clear_object (&priv->ibus_cancellable);
        if (!list && error) {
                g_warning ("Couldn't finish IBus request: %s", error->message);
                g_error_free (error);
                return;
        }

        /* 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);

695
                if (g_str_has_prefix (engine_id, "xkb:"))
Matthias Clasen's avatar
Matthias Clasen committed
696
                        g_object_unref (engine);
697
698
                else
                        g_hash_table_replace (priv->ibus_engines, (gpointer)engine_id, engine);
Matthias Clasen's avatar
Matthias Clasen committed
699
700
701
702
        }
        g_list_free (list);

        update_ibus_active_sources (self);
703
        update_input_chooser (self);
Matthias Clasen's avatar
Matthias Clasen committed
704
}
Matthias Clasen's avatar
Matthias Clasen committed
705

Matthias Clasen's avatar
Matthias Clasen committed
706
707
708
709
static void
fetch_ibus_engines (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
710

Matthias Clasen's avatar
Matthias Clasen committed
711
        priv->ibus_cancellable = g_cancellable_new ();
Matthias Clasen's avatar
Matthias Clasen committed
712

Matthias Clasen's avatar
Matthias Clasen committed
713
714
715
716
717
        ibus_bus_list_engines_async (priv->ibus,
                                     -1,
                                     priv->ibus_cancellable,
                                     (GAsyncReadyCallback)fetch_ibus_engines_result,
                                     self);
Matthias Clasen's avatar
Matthias Clasen committed
718

Matthias Clasen's avatar
Matthias Clasen committed
719
720
  /* 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
721
722
723
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
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

757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
static void
adjust_input_list_scrolling (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;

        if (priv->n_input_rows >= MAX_INPUT_ROWS_VISIBLE) {
                GtkWidget *parent;
                gint height;

                parent = gtk_widget_get_parent (priv->input_scrolledwindow);
                gtk_widget_get_preferred_height (parent, NULL, &height);
                gtk_widget_set_size_request (parent, -1, height);

                gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->input_scrolledwindow),
                                                GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
        } else {
                gtk_widget_set_size_request (gtk_widget_get_parent (priv->input_scrolledwindow), -1, -1);
                gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->input_scrolledwindow),
                                                GTK_POLICY_NEVER, GTK_POLICY_NEVER);
        }
}

779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
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
795
796
797
798
799
800
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
801
{
802
        CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
803
804
        GtkWidget *row;
        GtkWidget *label;
Matthias Clasen's avatar
Matthias Clasen committed
805
        GtkWidget *image;
Matthias Clasen's avatar
Matthias Clasen committed
806

807
        remove_no_input_row (GTK_CONTAINER (priv->input_list));
808

Matthias Clasen's avatar
Matthias Clasen committed
809
810
811
812
813
814
815
816
        row = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
        label = gtk_label_new (name);
        gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
        gtk_widget_set_margin_left (label, 20);
        gtk_widget_set_margin_right (label, 20);
        gtk_widget_set_margin_top (label, 6);
        gtk_widget_set_margin_bottom (label, 6);
        gtk_box_pack_start (GTK_BOX (row), label, TRUE, TRUE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
817
818
819
820
821
822
823

        if (strcmp (type, INPUT_SOURCE_TYPE_IBUS) == 0) {
                image = gtk_image_new_from_icon_name ("system-run-symbolic", GTK_ICON_SIZE_BUTTON);
                gtk_widget_set_margin_left (image, 20);
                gtk_widget_set_margin_right (image, 20);
                gtk_widget_set_margin_top (image, 6);
                gtk_widget_set_margin_bottom (image, 6);
824
                gtk_style_context_add_class (gtk_widget_get_style_context (image), "dim-label");
Matthias Clasen's avatar
Matthias Clasen committed
825
826
827
                gtk_box_pack_start (GTK_BOX (row), image, FALSE, TRUE, 0);
        }

Matthias Clasen's avatar
Matthias Clasen committed
828
        gtk_widget_show_all (row);
Matthias Clasen's avatar
Matthias Clasen committed
829
830
831
832
833
834
835
836
837
        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);
        }

838
839
840
        priv->n_input_rows += 1;
        adjust_input_list_scrolling (self);

Matthias Clasen's avatar
Matthias Clasen committed
841
        return row;
Matthias Clasen's avatar
Matthias Clasen committed
842
843
}

844
845
846
847
848
849
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
850
static void
Matthias Clasen's avatar
Matthias Clasen committed
851
852
add_input_sources (CcRegionPanel *self,
                   GVariant      *sources)
Matthias Clasen's avatar
Matthias Clasen committed
853
{
Matthias Clasen's avatar
Matthias Clasen committed
854
855
856
857
858
859
860
861
        CcRegionPanelPrivate *priv = self->priv;
        GVariantIter iter;
        const gchar *type;
        const gchar *id;
        const gchar *name;
        gchar *display_name;
        GDesktopAppInfo *app_info;

862
863
864
865
866
        if (g_variant_n_children (sources) < 1) {
                add_no_input_row (self);
                return;
        }

Matthias Clasen's avatar
Matthias Clasen committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
        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
901
902
903
904
905
906
907
908
909
}

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
910
911
        g_variant_unref (sources);
}
Matthias Clasen's avatar
Matthias Clasen committed
912

Matthias Clasen's avatar
Matthias Clasen committed
913
static void
Matthias Clasen's avatar
Matthias Clasen committed
914
clear_input_sources (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
915
{
Matthias Clasen's avatar
Matthias Clasen committed
916
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
917
        GList *list, *l;
Matthias Clasen's avatar
Matthias Clasen committed
918
        list = gtk_container_get_children (GTK_CONTAINER (priv->input_list));
Matthias Clasen's avatar
Matthias Clasen committed
919
        for (l = list; l; l = l->next) {
Matthias Clasen's avatar
Matthias Clasen committed
920
                gtk_container_remove (GTK_CONTAINER (priv->input_list), GTK_WIDGET (l->data));
Matthias Clasen's avatar
Matthias Clasen committed
921
922
        }
        g_list_free (list);
923
924
925

        priv->n_input_rows = 0;
        adjust_input_list_scrolling (self);
Matthias Clasen's avatar
Matthias Clasen committed
926
}
Matthias Clasen's avatar
Matthias Clasen committed
927

Matthias Clasen's avatar
Matthias Clasen committed
928
929
930
931
932
933
static void
select_by_id (GtkWidget   *row,
              gpointer     data)
{
        const gchar *id = data;
        const gchar *row_id;
Matthias Clasen's avatar
Matthias Clasen committed
934

Matthias Clasen's avatar
Matthias Clasen committed
935
936
937
938
939
940
941
942
943
944
945
        row_id = (const gchar *)g_object_get_data (G_OBJECT (row), "id");
        if (g_strcmp0 (row_id, id) == 0)
                egg_list_box_select_child (EGG_LIST_BOX (gtk_widget_get_parent (row)), row);
}

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
946
947
948
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
949
950
951
input_sources_changed (GSettings     *settings,
                       const gchar   *key,
                       CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
952
953
{
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
954
        GtkWidget *selected;
955
        gchar *id = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
956
957
958

        selected = egg_list_box_get_selected_child (EGG_LIST_BOX (priv->input_list));
        if (selected)
959
                id = g_strdup (g_object_get_data (G_OBJECT (selected), "id"));
Matthias Clasen's avatar
Matthias Clasen committed
960
961
        clear_input_sources (self);
        add_input_sources_from_settings (self);
962
        if (id) {
Matthias Clasen's avatar
Matthias Clasen committed
963
                select_input (self, id);
964
965
                g_free (id);
        }
Matthias Clasen's avatar
Matthias Clasen committed
966
}
Matthias Clasen's avatar
Matthias Clasen committed
967
968


Matthias Clasen's avatar
Matthias Clasen committed
969
static void
970
update_buttons (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
971
972
973
974
975
976
977
978
979
980
981
982
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *selected;
        GList *children;
        gboolean multiple_sources;

        children = gtk_container_get_children (GTK_CONTAINER (priv->input_list));
        multiple_sources = g_list_next (children) != NULL;
        g_list_free (children);

        selected = egg_list_box_get_selected_child (EGG_LIST_BOX (priv->input_list));
        if (selected == NULL) {
983
                gtk_widget_set_visible (priv->show_config, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
984
985
986
987
988
989
990
                gtk_widget_set_sensitive (priv->remove_input, FALSE);
                gtk_widget_set_sensitive (priv->show_layout, FALSE);
        } else {
                GDesktopAppInfo *app_info;

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

991
                gtk_widget_set_visible (priv->show_config, app_info != NULL);
Matthias Clasen's avatar
Matthias Clasen committed
992
993
994
                gtk_widget_set_sensitive (priv->show_layout, TRUE);
                gtk_widget_set_sensitive (priv->remove_input, multiple_sources);
        }
Matthias Clasen's avatar
Matthias Clasen committed
995
996
997

        gtk_widget_set_visible (priv->options_button,
                                multiple_sources && !priv->login);
Matthias Clasen's avatar
Matthias Clasen committed
998
}
Matthias Clasen's avatar
Matthias Clasen committed
999

Matthias Clasen's avatar
Matthias Clasen committed
1000
static void
Matthias Clasen's avatar
Matthias Clasen committed
1001
set_input_settings (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
{
	CcRegionPanelPrivate *priv = self->priv;
        const gchar *type;
        const gchar *id;
        GVariantBuilder builder;
        GVariant *old_sources;
        const gchar *old_current_type;
        const gchar *old_current_id;
        guint old_current;
        guint old_n_sources;
        guint index;
        GList *list, *l;

        old_sources = g_settings_get_value (priv->input_settings, KEY_INPUT_SOURCES);
        old_current = g_settings_get_uint (priv->input_settings, KEY_CURRENT_INPUT_SOURCE);
        old_n_sources = g_variant_n_children (old_sources);

        if (old_n_sources > 0 && old_current < old_n_sources) {
                g_variant_get_child (old_sources, old_current,
                                     "(&s&s)", &old_current_type, &old_current_id);
        } else {
                old_current_type = "";
                old_current_id = "";
        }

        g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ss)"));
        index = 0;
        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");
                if (index != old_current &&
                    g_str_equal (type, old_current_type) &&
                    g_str_equal (id, old_current_id)) {
                        g_settings_set_uint (priv->input_settings, KEY_CURRENT_INPUT_SOURCE, index);
                }
                g_variant_builder_add (&builder, "(ss)", type, id);
                index += 1;
        }
        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);

        g_variant_unref (old_sources);
}
Matthias Clasen's avatar
Matthias Clasen committed
1048

Matthias Clasen's avatar
Matthias Clasen committed
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
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);
1060
1061
                if (priv->login_auto_apply)
                        set_localed_input (self);
Matthias Clasen's avatar
Matthias Clasen committed
1062
1063
1064
        }
}

Matthias Clasen's avatar
Matthias Clasen committed
1065
static void
Matthias Clasen's avatar
Matthias Clasen committed
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
apologize_for_no_ibus_login (CcRegionPanel *self)
{
        GtkWidget *dialog;
        GtkWidget *toplevel;
        GtkWidget *image;

        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));

        dialog = gtk_message_dialog_new (GTK_WINDOW (toplevel),
                                         GTK_DIALOG_MODAL,
                                         GTK_MESSAGE_OTHER,
                                         GTK_BUTTONS_OK,
                                         _("Sorry"));
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                  "%s", _("Input methods can't be used on the login screen"));
        image = gtk_image_new_from_icon_name ("face-sad-symbolic",
                                              GTK_ICON_SIZE_DIALOG);
        gtk_widget_show (image);
        gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);

        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
}

1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
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
1109
1110
static void
input_response (GtkWidget *chooser, gint response_id, gpointer data)
Matthias Clasen's avatar
Matthias Clasen committed
1111
1112
{
	CcRegionPanel *self = data;
Matthias Clasen's avatar
Matthias Clasen committed
1113
        CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
1114
1115
1116
1117
1118
        gchar *type;
        gchar *id;
        gchar *name;
        GDesktopAppInfo *app_info = NULL;

Matthias Clasen's avatar
Matthias Clasen committed
1119
        if (response_id == GTK_RESPONSE_OK) {
1120
1121
                if (cc_input_chooser_get_selected (chooser, &type, &id, &name) &&
                    !input_source_already_added (self, id)) {
Matthias Clasen's avatar
Matthias Clasen committed
1122
1123
1124
                        if (g_str_equal (type, INPUT_SOURCE_TYPE_IBUS)) {
                                g_free (type);
                                type = INPUT_SOURCE_TYPE_IBUS;
Matthias Clasen's avatar
Matthias Clasen committed
1125
#ifdef HAVE_IBUS
Matthias Clasen's avatar
Matthias Clasen committed
1126
                                app_info = setup_app_info_for_id (id);
Matthias Clasen's avatar
Matthias Clasen committed
1127
#endif
Matthias Clasen's avatar
Matthias Clasen committed
1128
1129
1130
1131
                        } else {
                                g_free (type);
                                type = INPUT_SOURCE_TYPE_XKB;
                        }
Matthias Clasen's avatar
Matthias Clasen committed
1132

Matthias Clasen's avatar
Matthias Clasen committed
1133
1134
1135
1136
                        if (priv->login && g_str_equal (type, INPUT_SOURCE_TYPE_IBUS)) {
                                apologize_for_no_ibus_login (self);
                        } else {
                                add_input_row (self, type, id, name, app_info);
1137
                                update_buttons (self);
Matthias Clasen's avatar
Matthias Clasen committed
1138
1139
1140
1141
1142
1143
                                update_input (self);
                        }
                        g_free (id);
                        g_free (name);
                        g_clear_object (&app_info);
                }
Matthias Clasen's avatar
Matthias Clasen committed
1144
        }
1145
1146
        gtk_widget_destroy (chooser);
        g_object_set_data (G_OBJECT (self), "input-chooser", NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1147
1148
1149
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1150
show_input_chooser (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
1151
1152
1153
1154
1155
1156
1157
1158
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *chooser;
        GtkWidget *toplevel;

        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
        chooser = cc_input_chooser_new (GTK_WINDOW (toplevel),
                                        priv->xkb_info,
1159
1160
1161
1162
1163
1164
#ifdef HAVE_IBUS
                                        priv->ibus_engines
#else
                                        NULL
#endif
                );
Matthias Clasen's avatar
Matthias Clasen committed
1165
        g_signal_connect (chooser, "response",
Matthias Clasen's avatar
Matthias Clasen committed
1166
                          G_CALLBACK (input_response), self);
1167
1168
1169
        gtk_window_present (GTK_WINDOW (chooser));

        g_object_set_data (G_OBJECT (self), "input-chooser", chooser);
Matthias Clasen's avatar
Matthias Clasen committed
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
}

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
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
}

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
1221
do_remove_selected_input (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *selected;
        GtkWidget *sibling;

        selected = egg_list_box_get_selected_child (EGG_LIST_BOX (priv->input_list));
        if (selected == NULL)
                return;

        sibling = find_sibling (GTK_CONTAINER (priv->input_list), selected);
        gtk_container_remove (GTK_CONTAINER (priv->input_list), selected);
        egg_list_box_select_child (EGG_LIST_BOX (priv->input_list), sibling);

1235
1236
1237
        priv->n_input_rows -= 1;
        adjust_input_list_scrolling (self);

1238
        update_buttons (self);
Matthias Clasen's avatar
Matthias Clasen committed
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
        update_input (self);
}

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

        if (!priv->login) {
                do_remove_selected_input (self);
        } else if (g_permission_get_allowed (priv->permission)) {
                do_remove_selected_input (self);
        } else if (g_permission_get_can_acquire (priv->permission)) {
                priv->op = REMOVE_INPUT;
                g_permission_acquire_async (priv->permission,
                                            NULL,
                                            permission_acquired,
                                            self);
        }
Matthias Clasen's avatar
Matthias Clasen committed
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
}

static void
show_selected_settings (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *selected;
        GdkAppLaunchContext *ctx;
        GDesktopAppInfo *app_info;
        const gchar *id;
        GError *error = NULL;

        selected = egg_list_box_get_selected_child (EGG_LIST_BOX (priv->input_list));
        if (selected == NULL)
                return;

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

        ctx = gdk_display_get_app_launch_context (gdk_display_get_default ());
        gdk_app_launch_context_set_timestamp (ctx, gtk_get_current_event_time ());

        id = (const gchar *)g_object_get_data (G_OBJECT (selected), "id");
        g_app_launch_context_setenv (G_APP_LAUNCH_CONTEXT (ctx),
                                     "IBUS_ENGINE_NAME", id);

        if (!g_app_info_launch (G_APP_INFO (app_info), NULL, G_APP_LAUNCH_CONTEXT (ctx), &error)) {
                g_warning ("Failed to launch input source setup: %s", error->message);
                g_error_free (error);
        }

        g_object_unref (ctx);
}

static void
show_selected_layout (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *selected;
        const gchar *type;
        const gchar *id;
        const gchar *layout;
        const gchar *variant;
        gchar *commandline;

        selected = egg_list_box_get_selected_child (EGG_LIST_BOX (priv->input_list));
        if (selected == NULL)
                return;

        type = (const gchar *)g_object_get_data (G_OBJECT (selected), "type");
        id = (const gchar *)g_object_get_data (G_OBJECT (selected), "id");

        if (g_str_equal (type, INPUT_SOURCE_TYPE_XKB)) {
                gnome_xkb_info_get_layout_info (priv->xkb_info,
                                                id, NULL, NULL,
                                                &layout, &variant);

                if (!layout || !layout[0]) {
                        g_warning ("Couldn't find XKB input source '%s'", id);
                        return;
                }
#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) {
                        layout = ibus_engine_desc_get_layout (engine_desc);
                        variant = "";
                } else {
                        g_warning ("Couldn't find IBus input source '%s'", id);
                        return;
                }
#endif
        } else {
                g_warning ("Unhandled input source type '%s'", type);
                return;
        }

        if (variant[0])
                commandline = g_strdup_printf ("gkbd-keyboard-display -l \"%s\t%s\"",
                                               layout, variant);
        else
                commandline = g_strdup_printf ("gkbd-keyboard-display -l %s",
                                               layout);

        g_spawn_command_line_async (commandline, NULL);
        g_free (commandline);
}

static void
options_response (GtkDialog     *options,
                  gint           response_id,
                  CcRegionPanel *self)
{
        gtk_widget_destroy (GTK_WIDGET (options));
}


static void
show_input_options (CcRegionPanel *self)
{
        GtkWidget *toplevel;
        GtkWidget *options;

        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
        options = cc_input_options_new (toplevel);
        g_signal_connect (options, "response",
                          G_CALLBACK (options_response), self);
        gtk_window_present (GTK_WINDOW (options));
}

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

        priv->input_settings = g_settings_new (GNOME_DESKTOP_INPUT_SOURCES_DIR);
        g_settings_delay (priv->input_settings);

        priv->xkb_info = gnome_xkb_info_new ();

#ifdef HAVE_IBUS
        ibus_init ();
        if (!priv->ibus) {
                priv->ibus = ibus_bus_new_async ();
                if (ibus_bus_is_connected (priv->ibus))
                        fetch_ibus_engines (self);
                else
                        g_signal_connect_swapped (priv->ibus, "connected",
                                                  G_CALLBACK (fetch_ibus_engines), self);
        }
        maybe_start_ibus ();
#endif

Matthias Clasen's avatar
Matthias Clasen committed
1396
        priv->input_section = WID ("input_section");
Matthias Clasen's avatar
Matthias Clasen committed
1397
1398
1399
1400
1401
1402
        priv->options_button = WID ("input_options");
        priv->input_list = WID ("input_list");
        priv->add_input = WID ("input_source_add");
        priv->remove_input = WID ("input_source_remove");
        priv->show_config = WID ("input_source_config");
        priv->show_layout = WID ("input_source_layout");
1403
        priv->input_scrolledwindow = WID ("input_scrolledwindow");
Matthias Clasen's avatar
Matthias Clasen committed
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416

        g_signal_connect_swapped (priv->options_button, "clicked",
                                  G_CALLBACK (show_input_options), self);
        g_signal_connect_swapped (priv->add_input, "clicked",
                                  G_CALLBACK (add_input), self);
        g_signal_connect_swapped (priv->remove_input, "clicked",
                                  G_CALLBACK (remove_selected_input), self);
        g_signal_connect_swapped (priv->show_config, "clicked",
                                  G_CALLBACK (show_selected_settings), self);
        g_signal_connect_swapped (priv->show_layout, "clicked",
                                  G_CALLBACK (show_selected_layout), self);

        egg_list_box_set_selection_mode (EGG_LIST_BOX (priv->input_list),
Matthias Clasen's avatar
Matthias Clasen committed
1417
                                         GTK_SELECTION_SINGLE);
Matthias Clasen's avatar
Matthias Clasen committed
1418
        egg_list_box_set_separator_funcs (EGG_LIST_BOX (priv->input_list),
Matthias Clasen's avatar
Matthias Clasen committed
1419
1420
                                          update_separator_func,
                                          NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1421
        g_signal_connect_swapped (priv->input_list, "child-selected",
1422
                                  G_CALLBACK (update_buttons), self);
Matthias Clasen's avatar
Matthias Clasen committed
1423

Matthias Clasen's avatar
Matthias Clasen committed
1424
1425
        g_signal_connect (priv->input_settings, "changed::" KEY_INPUT_SOURCES,
                          G_CALLBACK (input_sources_changed), self);
Matthias Clasen's avatar
Matthias Clasen committed
1426

Matthias Clasen's avatar
Matthias Clasen committed
1427
        add_input_sources_from_settings (self);
1428
        update_buttons (self);
Matthias Clasen's avatar
Matthias Clasen committed
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
}

static void
on_localed_properties_changed (GDBusProxy     *proxy,
                               GVariant       *changed_properties,
                               const gchar   **invalidated_properties,
                               CcRegionPanel  *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GVariant *v;

        v = g_dbus_proxy_get_cached_property (proxy, "Locale");
        if (v) {
                const gchar **strv;
                gsize len;
                gint i;
                const gchar *lang, *messages, *time;

                strv = g_variant_get_strv (v, &len);

                lang = messages = time = NULL;
                for (i = 0; strv[i]; i++) {
                        if (g_str_has_prefix (strv[i], "LANG=")) {
                                lang = strv[i] + strlen ("LANG=");
                        } else if (g_str_has_prefix (strv[i], "LC_MESSAGES=")) {
                                messages = strv[i] + strlen ("LC_MESSAGES=");
                        } else if (g_str_has_prefix (strv[i], "LC_TIME=")) {
                                time = strv[i] + strlen ("LC_TIME=");
                        }
                }
                if (!lang) {
                        lang = "";
                }
                if (!messages) {
                        messages = lang;
                }
                if (!time) {
                        time = lang;
                }
                g_free (priv->system_language);
                priv->system_language = g_strdup (messages);
                g_free (priv->system_region);
                priv->system_region = g_strdup (time);
                g_variant_unref (v);

                update_language_label (self);
        }
}

static void
add_input_sources_from_localed (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GVariant *v;
        const gchar *s;
        gchar **layouts = NULL;
        gchar **variants = NULL;
        gint i, n;

1488
1489
1490
        if (!priv->localed)
                return;

Matthias Clasen's avatar
Matthias Clasen committed
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
        v = g_dbus_proxy_get_cached_property (priv->localed, "X11Layout");
        if (v) {
                s = g_variant_get_string (v, NULL);
                layouts =