cc-region-panel.c 34.4 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
23
#include <config.h>
#include <glib/gi18n.h>
Matthias Clasen's avatar
Matthias Clasen committed
24
#include <gio/gdesktopappinfo.h>
Matthias Clasen's avatar
Matthias Clasen committed
25

26
#include "cc-region-panel.h"
27
#include "cc-region-resources.h"
Matthias Clasen's avatar
Matthias Clasen committed
28
29
30
31
#include "cc-language-chooser.h"
#include "cc-format-chooser.h"
#include "cc-input-chooser.h"
#include "cc-input-options.h"
32

33
34
#include <gtk/gtk.h>

Matthias Clasen's avatar
Matthias Clasen committed
35
36
37
38
39
40
41
42
43
44
45
#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 "supported-ibus-engines.h"
#include "cc-ibus-utils.h"
#endif
46

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

Matthias Clasen's avatar
Matthias Clasen committed
49
#include "egg-list-box/egg-list-box.h"
Matthias Clasen's avatar
Matthias Clasen committed
50
#include <libgd/gd-notification.h>
Matthias Clasen's avatar
Matthias Clasen committed
51

Matthias Clasen's avatar
Matthias Clasen committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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"


static gboolean
strv_contains (const gchar * const *strv,
               const gchar         *str)
{
  const gchar * const *p = strv;
  for (p = strv; *p; p++)
    if (g_strcmp0 (*p, str) == 0)
      return TRUE;

  return FALSE;
}

Ray Strode's avatar
Ray Strode committed
75
CC_PANEL_REGISTER (CcRegionPanel, cc_region_panel)
76

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

79
80
#define REGION_PANEL_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CC_TYPE_REGION_PANEL, CcRegionPanelPrivate))

81
struct _CcRegionPanelPrivate {
82
83
	GtkBuilder *builder;

Matthias Clasen's avatar
Matthias Clasen committed
84
        GtkWidget *login_button;
Matthias Clasen's avatar
Matthias Clasen committed
85
86
        GtkWidget *overlay;
        GtkWidget *notification;
Matthias Clasen's avatar
Matthias Clasen committed
87

Matthias Clasen's avatar
Matthias Clasen committed
88
        GtkWidget *language_row;
Matthias Clasen's avatar
Matthias Clasen committed
89
        GtkWidget *language_label;
Matthias Clasen's avatar
Matthias Clasen committed
90
        GtkWidget *formats_row;
Matthias Clasen's avatar
Matthias Clasen committed
91
92
        GtkWidget *formats_label;

Matthias Clasen's avatar
Matthias Clasen committed
93
94
95
        ActUserManager *user_manager;
        ActUser        *user;
        GSettings      *locale_settings;
Matthias Clasen's avatar
Matthias Clasen committed
96
97
98
99

        gchar *language;
        gchar *region;

Matthias Clasen's avatar
Matthias Clasen committed
100
        GtkWidget *options_button;
Matthias Clasen's avatar
Matthias Clasen committed
101
102
103
104
105
106
107
108
109
110
111
112
113
        GtkWidget *input_list;
        GtkWidget *add_input;
        GtkWidget *remove_input;
        GtkWidget *show_config;
        GtkWidget *show_layout;

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

116
static void
Matthias Clasen's avatar
Matthias Clasen committed
117
cc_region_panel_finalize (GObject *object)
118
{
Matthias Clasen's avatar
Matthias Clasen committed
119
120
	CcRegionPanel *self = CC_REGION_PANEL (object);
	CcRegionPanelPrivate *priv = self->priv;
121

Matthias Clasen's avatar
Matthias Clasen committed
122
123
        priv->user_manager = NULL;
        priv->user = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
124
125
126
127
128
129
130
131
132
        g_clear_object (&priv->builder);
        g_clear_object (&priv->locale_settings);
        g_clear_object (&priv->input_settings);
        g_clear_object (&priv->xkb_info);
        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);
133

Matthias Clasen's avatar
Matthias Clasen committed
134
	G_OBJECT_CLASS (cc_region_panel_parent_class)->finalize (object);
135
136
137
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
138
cc_region_panel_constructed (GObject *object)
139
{
Matthias Clasen's avatar
Matthias Clasen committed
140
        CcRegionPanel *self = CC_REGION_PANEL (object);
Matthias Clasen's avatar
Matthias Clasen committed
141
	CcRegionPanelPrivate *priv = self->priv;
142

Matthias Clasen's avatar
Matthias Clasen committed
143
        G_OBJECT_CLASS (cc_region_panel_parent_class)->constructed (object);
144

Matthias Clasen's avatar
Matthias Clasen committed
145
        priv->login_button = gtk_toggle_button_new_with_label (_("Login Screen"));
146

Matthias Clasen's avatar
Matthias Clasen committed
147
        cc_shell_embed_widget_in_header (cc_panel_get_shell (CC_PANEL (object)),
Matthias Clasen's avatar
Matthias Clasen committed
148
                                         priv->login_button);
Matthias Clasen's avatar
Matthias Clasen committed
149
        gtk_widget_show (priv->login_button);
150
151
}

152
153
154
static const char *
cc_region_panel_get_help_uri (CcPanel *panel)
{
Matthias Clasen's avatar
Matthias Clasen committed
155
        return "help:gnome-help/prefs-language";
156
157
}

158
static void
159
cc_region_panel_class_init (CcRegionPanelClass * klass)
160
161
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
162
	CcPanelClass * panel_class = CC_PANEL_CLASS (klass);
163

164
	g_type_class_add_private (klass, sizeof (CcRegionPanelPrivate));
165

166
167
	panel_class->get_help_uri = cc_region_panel_get_help_uri;

Matthias Clasen's avatar
Matthias Clasen committed
168
        object_class->constructed = cc_region_panel_constructed;
169
	object_class->finalize = cc_region_panel_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
170
}
171

Matthias Clasen's avatar
Matthias Clasen committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
static void
restart_now (CcRegionPanel *self)
{
        GDBusConnection *bus;

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

        g_print ("Ta Da! Restarting...\n");
        bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
        g_dbus_connection_call (bus,
                                "org.gnome.SessionManager",
                                "/org/gnome/SessionManager",
                                "org.gnome.SessionManager",
                                "Logout",
                                g_variant_new ("(u)", 0),
                                NULL, 0, G_MAXINT,
                                NULL, NULL, NULL);
}

static void
show_restart_notification (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *box;
        GtkWidget *label;
        GtkWidget *button;

        if (priv->notification)
                return;

        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);
}

Matthias Clasen's avatar
Matthias Clasen committed
223
224
225
226
227
228
static void
update_separator_func (GtkWidget **separator,
                       GtkWidget  *child,
                       GtkWidget  *before,
                       gpointer    user_data)
{
Matthias Clasen's avatar
Matthias Clasen committed
229
230
231
232
233
234
235
236
237
        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
238

Matthias Clasen's avatar
Matthias Clasen committed
239
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
240
241
242
243
244
update_language (CcRegionPanel *self,
                 const gchar   *language)
{
	CcRegionPanelPrivate *priv = self->priv;

Matthias Clasen's avatar
Matthias Clasen committed
245
246
247
        if (g_strcmp0 (language, priv->language) == 0)
                return FALSE;

Matthias Clasen's avatar
Matthias Clasen committed
248
        act_user_set_language (priv->user, language);
Matthias Clasen's avatar
Matthias Clasen committed
249
        return TRUE;
250
251
252
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
253
254
255
256
257
language_response (GtkDialog     *chooser,
                   gint           response_id,
                   CcRegionPanel *self)
{
        const gchar *language;
Matthias Clasen's avatar
Matthias Clasen committed
258
259
260
261
262
263
264
265
        gboolean changed;

        changed = FALSE;

        if (response_id == GTK_RESPONSE_OK) {
                language = cc_language_chooser_get_language (GTK_WIDGET (chooser));
                changed = update_language (self, language);
        }
Matthias Clasen's avatar
Matthias Clasen committed
266
267

        gtk_widget_destroy (GTK_WIDGET (chooser));
Matthias Clasen's avatar
Matthias Clasen committed
268
269
270
271
272
273
274
275
276
277

        if (changed)
                show_restart_notification (self);
}

static gboolean
update_region (CcRegionPanel *self,
               const gchar   *region)
{
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
278
279
280
281
282
283
284

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

        g_print ("update region to %s\n", region);
        g_settings_set_string (priv->locale_settings, KEY_REGION, region);
        return TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
285
286
287
288
289
290
}

static void
format_response (GtkDialog *chooser,
                 gint       response_id,
                 CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
291
{
Matthias Clasen's avatar
Matthias Clasen committed
292
        const gchar *region;
Matthias Clasen's avatar
Matthias Clasen committed
293
294
295
296
297
298
299
300
        gboolean changed;

        changed = FALSE;

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

Matthias Clasen's avatar
Matthias Clasen committed
302
        gtk_widget_destroy (GTK_WIDGET (chooser));
Matthias Clasen's avatar
Matthias Clasen committed
303
304
305

        if (changed)
                show_restart_notification (self);
Matthias Clasen's avatar
Matthias Clasen committed
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
}

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

        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
        if (child == priv->language_row) {
                chooser = cc_language_chooser_new (toplevel);
                cc_language_chooser_set_language (chooser, priv->language);
                g_signal_connect (chooser, "response",
                                  G_CALLBACK (language_response), self);
                gtk_window_present (GTK_WINDOW (chooser));
        } else if (child == priv->formats_row) {
                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
Matthias Clasen's avatar
Matthias Clasen committed
332
update_language_from_user (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
333
334
{
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
335
336
337
338
339
340
341
342
343
344
345
346
347
        const gchar *language;
        gchar *name;

        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);
        name = gnome_get_language_from_name (language, language);
        gtk_label_set_label (GTK_LABEL (priv->language_label), name);
        g_free (name);
Matthias Clasen's avatar
Matthias Clasen committed
348
349
350
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
351
update_region_from_setting (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
352
353
354
355
356
357
{
	CcRegionPanelPrivate *priv = self->priv;
        gchar *name;

        g_free (priv->region);
        priv->region = g_settings_get_string (priv->locale_settings, KEY_REGION);
Matthias Clasen's avatar
Matthias Clasen committed
358
        g_print ("set label to %s\n", priv->region);
Matthias Clasen's avatar
Matthias Clasen committed
359
360
361
362
        name = gnome_get_region_from_name (priv->region, priv->region);
        gtk_label_set_label (GTK_LABEL (priv->formats_label), name);
        g_free (name);
}
Matthias Clasen's avatar
Matthias Clasen committed
363

Matthias Clasen's avatar
Matthias Clasen committed
364
365
366
367
368
static void
setup_language_section (CcRegionPanel *self)
{
	CcRegionPanelPrivate *priv = self->priv;
        GtkWidget *widget;
Matthias Clasen's avatar
Matthias Clasen committed
369
370
371
372
373
374
375

        priv->user_manager = act_user_manager_get_default ();
        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
376
377
378

        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
379
                                  G_CALLBACK (update_region_from_setting), self);
Matthias Clasen's avatar
Matthias Clasen committed
380
381
382
383
384
385
386

        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
387
388
389
390
391
392
        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
393
                                  G_CALLBACK (activate_language_child), self);
Matthias Clasen's avatar
Matthias Clasen committed
394

Matthias Clasen's avatar
Matthias Clasen committed
395
        update_region_from_setting (self);
Matthias Clasen's avatar
Matthias Clasen committed
396
        update_language_from_user (self);
Matthias Clasen's avatar
Matthias Clasen committed
397
}
Matthias Clasen's avatar
Matthias Clasen committed
398

Matthias Clasen's avatar
Matthias Clasen committed
399
400
401
402
403
404
405
406
407
408
409
410
#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
411

Matthias Clasen's avatar
Matthias Clasen committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
        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
430

Matthias Clasen's avatar
Matthias Clasen committed
431
432
433
434
435
436
437
438
439
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
static void
fetch_ibus_engines_result (GObject       *object,
                           GAsyncResult  *result,
                           CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
        gboolean show_all_sources;
        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;
        }

        show_all_sources = g_settings_get_boolean (priv->input_settings, "show-all-sources");

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

                if (show_all_sources || strv_contains (supported_ibus_engines, engine_id))
                        g_hash_table_replace (priv->ibus_engines, (gpointer)engine_id, engine);
                else
                        g_object_unref (engine);
        }
        g_list_free (list);

        update_ibus_active_sources (self);
}
Matthias Clasen's avatar
Matthias Clasen committed
468

Matthias Clasen's avatar
Matthias Clasen committed
469
470
471
472
static void
fetch_ibus_engines (CcRegionPanel *self)
{
        CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
473

Matthias Clasen's avatar
Matthias Clasen committed
474
        priv->ibus_cancellable = g_cancellable_new ();
Matthias Clasen's avatar
Matthias Clasen committed
475

Matthias Clasen's avatar
Matthias Clasen committed
476
477
478
479
480
        ibus_bus_list_engines_async (priv->ibus,
                                     -1,
                                     priv->ibus_cancellable,
                                     (GAsyncReadyCallback)fetch_ibus_engines_result,
                                     self);
Matthias Clasen's avatar
Matthias Clasen committed
481

Matthias Clasen's avatar
Matthias Clasen committed
482
483
  /* 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
484
485
486
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
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

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
526
527
528
{
        GtkWidget *row;
        GtkWidget *label;
Matthias Clasen's avatar
Matthias Clasen committed
529
        GtkWidget *image;
Matthias Clasen's avatar
Matthias Clasen committed
530
531
532
533
534
535
536
537
538

        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
539
540
541
542
543
544
545
546
547
548

        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);
                gtk_box_pack_start (GTK_BOX (row), image, FALSE, TRUE, 0);
        }

Matthias Clasen's avatar
Matthias Clasen committed
549
        gtk_widget_show_all (row);
Matthias Clasen's avatar
Matthias Clasen committed
550
551
552
553
554
555
556
557
558
559
        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);
        }

        return row;
Matthias Clasen's avatar
Matthias Clasen committed
560
561
562
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
563
populate_with_active_sources (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
564
{
Matthias Clasen's avatar
Matthias Clasen committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
        CcRegionPanelPrivate *priv = self->priv;
        GVariant *sources;
        GVariantIter iter;
        const gchar *type;
        const gchar *id;
        const gchar *name;
        gchar *display_name;
        GDesktopAppInfo *app_info;

        sources = g_settings_get_value (priv->input_settings, "sources");
        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);
        }
        g_variant_unref (sources);
}
Matthias Clasen's avatar
Matthias Clasen committed
611

Matthias Clasen's avatar
Matthias Clasen committed
612
613
614
615
616
617
618
619
620
621
static void
container_remove_all (GtkContainer *container)
{
        GList *list, *l;
        list = gtk_container_get_children (container);
        for (l = list; l; l = l->next) {
                gtk_container_remove (container, GTK_WIDGET (l->data));
        }
        g_list_free (list);
}
Matthias Clasen's avatar
Matthias Clasen committed
622

Matthias Clasen's avatar
Matthias Clasen committed
623
624
625
626
627
628
static void
select_by_id (GtkWidget   *row,
              gpointer     data)
{
        const gchar *id = data;
        const gchar *row_id;
Matthias Clasen's avatar
Matthias Clasen committed
629

Matthias Clasen's avatar
Matthias Clasen committed
630
631
632
633
634
635
636
637
638
639
640
        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
641
642
643
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
644
645
646
input_sources_changed (GSettings     *settings,
                       const gchar   *key,
                       CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
647
648
{
	CcRegionPanelPrivate *priv = self->priv;
Matthias Clasen's avatar
Matthias Clasen committed
649
650
651
652
653
654
655
656
657
658
659
        GtkWidget *selected;
        const gchar *id = NULL;

        selected = egg_list_box_get_selected_child (EGG_LIST_BOX (priv->input_list));
        if (selected)
                id = (const gchar *)g_object_get_data (G_OBJECT (selected), "id");
        container_remove_all (GTK_CONTAINER (priv->input_list));
        populate_with_active_sources (self);
        if (id)
                select_input (self, id);
}
Matthias Clasen's avatar
Matthias Clasen committed
660
661


Matthias Clasen's avatar
Matthias Clasen committed
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
static void
update_button_sensitivity (CcRegionPanel *self)
{
	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) {
                gtk_widget_set_sensitive (priv->remove_input, FALSE);
                gtk_widget_set_sensitive (priv->show_config, 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");

                gtk_widget_set_sensitive (priv->show_config, app_info != NULL);
                gtk_widget_set_sensitive (priv->show_layout, TRUE);
                gtk_widget_set_sensitive (priv->remove_input, multiple_sources);
        }
        gtk_widget_set_visible (priv->options_button, multiple_sources);
}
Matthias Clasen's avatar
Matthias Clasen committed
690

Matthias Clasen's avatar
Matthias Clasen committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
static void
update_configuration (CcRegionPanel *self)
{
	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
739

Matthias Clasen's avatar
Matthias Clasen committed
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
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
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
static void
select_input_child (CcRegionPanel *self, GtkWidget *child)
{
        update_button_sensitivity (self);
}

static void
chooser_response (GtkWidget *chooser, gint response_id, gpointer data)
{
	CcRegionPanel *self = data;
	CcRegionPanelPrivate *priv = self->priv;
        gchar *type;
        gchar *id;
        gchar *name;
        GDesktopAppInfo *app_info = NULL;

        if (cc_input_chooser_get_selected (chooser, &type, &id, &name)) {
                if (g_str_equal (type, INPUT_SOURCE_TYPE_IBUS)) {
                        g_free (type);
                        type = INPUT_SOURCE_TYPE_IBUS;
#ifdef HAVE_IBUS
                        app_info = setup_app_info_for_id (id);
#endif
                } else {
                        g_free (type);
                        type = INPUT_SOURCE_TYPE_XKB;
                }
                add_input_row (self, type, id, name, app_info);
                g_free (id);
                g_free (name);
                g_clear_object (&app_info);

                update_button_sensitivity (self);
                update_configuration (self);
        }

        gtk_widget_destroy (chooser);
}

static void
add_input (CcRegionPanel *self)
{
	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,
                                        priv->ibus_engines);
        g_signal_connect (chooser, "response",
                          G_CALLBACK (chooser_response), self);
}

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
remove_selected_input (CcRegionPanel *self)
{
	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);

        update_button_sensitivity (self);
        update_configuration (self);
}

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

        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");

        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
998
                                         GTK_SELECTION_SINGLE);
Matthias Clasen's avatar
Matthias Clasen committed
999
        egg_list_box_set_separator_funcs (EGG_LIST_BOX (priv->input_list),
Matthias Clasen's avatar
Matthias Clasen committed
1000
1001
                                          update_separator_func,
                                          NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1002
1003
        g_signal_connect_swapped (priv->input_list, "child-selected",
                                  G_CALLBACK (select_input_child), self);
Matthias Clasen's avatar
Matthias Clasen committed
1004

Matthias Clasen's avatar
Matthias Clasen committed
1005
1006
        g_signal_connect (priv->input_settings, "changed::" KEY_INPUT_SOURCES,
                          G_CALLBACK (input_sources_changed), self);
Matthias Clasen's avatar
Matthias Clasen committed
1007

Matthias Clasen's avatar
Matthias Clasen committed
1008
        populate_with_active_sources (self);
Matthias Clasen's avatar
Matthias Clasen committed
1009

Matthias Clasen's avatar
Matthias Clasen committed
1010
        update_button_sensitivity (self);
Matthias Clasen's avatar
Matthias Clasen committed
1011
1012
1013
1014
}

static void
cc_region_panel_init (CcRegionPanel *self)
1015
{
1016
	CcRegionPanelPrivate *priv;
1017
1018
	GError *error = NULL;

1019
	priv = self->priv = REGION_PANEL_PRIVATE (self);
1020
        g_resources_register (cc_region_get_resource ());
1021
1022
1023

	priv->builder = gtk_builder_new ();

1024
	gtk_builder_add_from_resource (priv->builder,
Matthias Clasen's avatar
Matthias Clasen committed
1025
                                       "/org/gnome/control-center/region/region.ui",
1026
                                       &error);
1027
1028
	if (error != NULL) {
		g_warning ("Error loading UI file: %s", error->message);
Bastien Nocera's avatar
Bastien Nocera committed
1029
		g_error_free (error);
1030
1031
1032
		return;
	}

Matthias Clasen's avatar
Matthias Clasen committed
1033
1034
        setup_language_section (self);
        setup_input_section (self);
1035

Matthias Clasen's avatar
Matthias Clasen committed
1036
1037
        priv->overlay = GTK_WIDGET (gtk_builder_get_object (priv->builder, "overlay"));
	gtk_widget_reparent (priv->overlay, GTK_WIDGET (self));
1038
}