cc-region-panel.c 59.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (C) 2010 Intel, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16
 *
Sergey V. Udaltsov's avatar
Sergey V. Udaltsov committed
17
 * Author: Sergey Udaltsov <svu@gnome.org>
18 19 20
 *
 */

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

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

Matthias Clasen's avatar
Matthias Clasen committed
38 39 40 41 42 43 44 45 46 47
#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
48

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

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

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

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

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

Matthias Clasen's avatar
Matthias Clasen committed
62 63
typedef enum {
        CHOOSE_LANGUAGE,
64
        CHOOSE_REGION,
Matthias Clasen's avatar
Matthias Clasen committed
65
        ADD_INPUT,
66 67 68
        REMOVE_INPUT,
        MOVE_UP_INPUT,
        MOVE_DOWN_INPUT,
Matthias Clasen's avatar
Matthias Clasen committed
69 70
} SystemOp;

71 72 73
struct _CcRegionPanel {
	CcPanel      parent_instance;

Matthias Clasen's avatar
Matthias Clasen committed
74 75 76
        GtkWidget   *login_button;
        GtkWidget   *login_label;
        gboolean     login;
77
        gboolean     login_auto_apply;
Matthias Clasen's avatar
Matthias Clasen committed
78 79 80
        GPermission *permission;
        SystemOp     op;
        GDBusProxy  *localed;
81
        GDBusProxy  *session;
82
        GCancellable *cancellable;
Matthias Clasen's avatar
Matthias Clasen committed
83

Robert Ancell's avatar
Robert Ancell committed
84
        GtkWidget *restart_revealer;
85
        gchar *needs_restart_file_path;
Matthias Clasen's avatar
Matthias Clasen committed
86

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

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

        gchar *language;
        gchar *region;
Matthias Clasen's avatar
Matthias Clasen committed
99 100
        gchar *system_language;
        gchar *system_region;
Matthias Clasen's avatar
Matthias Clasen committed
101

Matthias Clasen's avatar
Matthias Clasen committed
102
        GtkWidget *input_section;
Matthias Clasen's avatar
Matthias Clasen committed
103
        GtkWidget *options_button;
Matthias Clasen's avatar
Matthias Clasen committed
104 105 106
        GtkWidget *input_list;
        GtkWidget *add_input;
        GtkWidget *remove_input;
107 108
        GtkWidget *move_up_input;
        GtkWidget *move_down_input;
Matthias Clasen's avatar
Matthias Clasen committed
109 110
        GtkWidget *show_config;
        GtkWidget *show_layout;
Robert Ancell's avatar
Robert Ancell committed
111 112
        GtkWidget *restart_button;
        GtkWidget *language_list;
Matthias Clasen's avatar
Matthias Clasen committed
113 114 115 116 117 118 119 120

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

123 124
CC_PANEL_REGISTER (CcRegionPanel, cc_region_panel)

125
static void
Matthias Clasen's avatar
Matthias Clasen committed
126
cc_region_panel_finalize (GObject *object)
127
{
Matthias Clasen's avatar
Matthias Clasen committed
128
	CcRegionPanel *self = CC_REGION_PANEL (object);
129
	GtkWidget *chooser;
130

131 132
        g_cancellable_cancel (self->cancellable);
        g_clear_object (&self->cancellable);
133

134 135 136
        if (self->user_manager) {
                g_signal_handlers_disconnect_by_data (self->user_manager, self);
                self->user_manager = NULL;
137 138
        }

139 140 141
        if (self->user) {
                g_signal_handlers_disconnect_by_data (self->user, self);
                self->user = NULL;
142 143
        }

144 145 146 147 148 149
        g_clear_object (&self->permission);
        g_clear_object (&self->localed);
        g_clear_object (&self->session);
        g_clear_object (&self->locale_settings);
        g_clear_object (&self->input_settings);
        g_clear_object (&self->xkb_info);
150
#ifdef HAVE_IBUS
151 152 153 154 155
        g_clear_object (&self->ibus);
        if (self->ibus_cancellable)
                g_cancellable_cancel (self->ibus_cancellable);
        g_clear_object (&self->ibus_cancellable);
        g_clear_pointer (&self->ibus_engines, g_hash_table_destroy);
156
#endif
157 158 159 160
        g_free (self->language);
        g_free (self->region);
        g_free (self->system_language);
        g_free (self->system_region);
161

162
        g_clear_pointer (&self->needs_restart_file_path, g_free);
163

164
        chooser = g_object_get_data (G_OBJECT (self), "input-chooser");
Debarshi Ray's avatar
Debarshi Ray committed
165 166
        if (chooser)
                gtk_widget_destroy (chooser);
167

Matthias Clasen's avatar
Matthias Clasen committed
168
	G_OBJECT_CLASS (cc_region_panel_parent_class)->finalize (object);
169 170 171
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
172
cc_region_panel_constructed (GObject *object)
173
{
Matthias Clasen's avatar
Matthias Clasen committed
174
        CcRegionPanel *self = CC_REGION_PANEL (object);
175

Matthias Clasen's avatar
Matthias Clasen committed
176
        G_OBJECT_CLASS (cc_region_panel_parent_class)->constructed (object);
177

178
        if (self->permission)
179
                cc_shell_embed_widget_in_header (cc_panel_get_shell (CC_PANEL (object)),
180
                                                 self->login_button);
181 182
}

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

189
static void
190
cc_region_panel_class_init (CcRegionPanelClass * klass)
191
{
Robert Ancell's avatar
Robert Ancell committed
192
	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
193
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
Robert Ancell's avatar
Robert Ancell committed
194
	CcPanelClass *panel_class = CC_PANEL_CLASS (klass);
195

196 197
	panel_class->get_help_uri = cc_region_panel_get_help_uri;

Matthias Clasen's avatar
Matthias Clasen committed
198
        object_class->constructed = cc_region_panel_constructed;
199
	object_class->finalize = cc_region_panel_finalize;
Robert Ancell's avatar
Robert Ancell committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

        gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/region/region.ui");

        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, language_row);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, language_label);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, formats_row);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, formats_label);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, restart_revealer);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, input_section);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, options_button);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, input_list);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, add_input);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, remove_input);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, move_up_input);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, move_down_input);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, show_config);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, show_layout);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, restart_button);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, login_label);
        gtk_widget_class_bind_template_child (widget_class, CcRegionPanel, language_list);
Matthias Clasen's avatar
Matthias Clasen committed
220
}
221

Matthias Clasen's avatar
Matthias Clasen committed
222 223 224
static void
restart_now (CcRegionPanel *self)
{
225
        g_file_delete (g_file_new_for_path (self->needs_restart_file_path),
226 227
                                            NULL, NULL);

228
        g_dbus_proxy_call (self->session,
229 230 231 232
                           "Logout",
                           g_variant_new ("(u)", 0),
                           G_DBUS_CALL_FLAGS_NONE,
                           -1, NULL, NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
233 234 235
}

static void
236 237 238
set_restart_notification_visible (CcRegionPanel *self,
                                  const gchar   *locale,
                                  gboolean       visible)
Matthias Clasen's avatar
Matthias Clasen committed
239
{
240
        g_autofree gchar *current_locale = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
241

242 243 244 245 246
        if (locale) {
                current_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
                setlocale (LC_MESSAGES, locale);
        }

Robert Ancell's avatar
Robert Ancell committed
247
        gtk_revealer_set_reveal_child (GTK_REVEALER (self->restart_revealer), visible);
248

249
        if (locale)
250
                setlocale (LC_MESSAGES, current_locale);
251 252

        if (!visible) {
253
                g_file_delete (g_file_new_for_path (self->needs_restart_file_path),
254 255 256 257 258
                                                    NULL, NULL);

                return;
        }

259 260
        if (!g_file_set_contents (self->needs_restart_file_path, "", -1, NULL))
                g_warning ("Unable to create %s", self->needs_restart_file_path);
Matthias Clasen's avatar
Matthias Clasen committed
261 262
}

263 264 265 266 267 268
typedef struct {
        CcRegionPanel *self;
        int category;
        gchar *target_locale;
} MaybeNotifyData;

269 270 271 272 273 274 275 276 277
static void
maybe_notify_data_free (MaybeNotifyData *data)
{
        g_free (data->target_locale);
        g_free (data);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC (MaybeNotifyData, maybe_notify_data_free)

278 279 280 281 282
static void
maybe_notify_finish (GObject      *source,
                     GAsyncResult *res,
                     gpointer      data)
{
283
        g_autoptr(MaybeNotifyData) mnd = data;
284
        CcRegionPanel *self = mnd->self;
285 286 287 288 289 290
        g_autoptr(GError) error = NULL;
        g_autoptr(GVariant) retval = NULL;
        g_autofree gchar *current_lang_code = NULL;
        g_autofree gchar *current_country_code = NULL;
        g_autofree gchar *target_lang_code = NULL;
        g_autofree gchar *target_country_code = NULL;
291 292 293 294 295 296
        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);
297
                return;
298 299 300 301 302 303 304 305 306
        }

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

        if (!gnome_parse_locale (current_locale,
                                 &current_lang_code,
                                 &current_country_code,
                                 NULL,
                                 NULL))
307
                return;
308 309 310 311 312 313

        if (!gnome_parse_locale (mnd->target_locale,
                                 &target_lang_code,
                                 &target_country_code,
                                 NULL,
                                 NULL))
314
                return;
315 316 317

        if (g_str_equal (current_lang_code, target_lang_code) == FALSE ||
            g_str_equal (current_country_code, target_country_code) == FALSE)
318 319 320 321 322 323 324
                set_restart_notification_visible (self,
                                                  mnd->category == LC_MESSAGES ? mnd->target_locale : NULL,
                                                  TRUE);
        else
                set_restart_notification_visible (self,
                                                  mnd->category == LC_MESSAGES ? mnd->target_locale : NULL,
                                                  FALSE);
325 326 327 328 329 330 331 332 333 334 335 336 337 338
}

static void
maybe_notify (CcRegionPanel *self,
              int            category,
              const gchar   *target_locale)
{
        MaybeNotifyData *mnd;

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

339
        g_dbus_proxy_call (self->session,
340 341 342 343
                           "GetLocale",
                           g_variant_new ("(i)", category),
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
344
                           self->cancellable,
345 346 347 348
                           maybe_notify_finish,
                           mnd);
}

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

351 352 353 354
static void
set_system_language (CcRegionPanel *self,
                     const gchar   *language)
{
355
        if (g_strcmp0 (language, self->system_language) == 0)
356 357
                return;

358 359
        g_free (self->system_language);
        self->system_language = g_strdup (language);
360 361 362 363

        set_localed_locale (self);
}

364
static void
Matthias Clasen's avatar
Matthias Clasen committed
365 366 367
update_language (CcRegionPanel *self,
                 const gchar   *language)
{
368
        if (self->login) {
369
                set_system_language (self, language);
Matthias Clasen's avatar
Matthias Clasen committed
370
        } else {
371
                if (g_strcmp0 (language, self->language) == 0)
372
                        return;
373 374
                act_user_set_language (self->user, language);
                if (self->login_auto_apply)
375
                        set_system_language (self, language);
376
                maybe_notify (self, LC_MESSAGES, language);
Matthias Clasen's avatar
Matthias Clasen committed
377
        }
378 379 380
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
381 382 383 384 385
language_response (GtkDialog     *chooser,
                   gint           response_id,
                   CcRegionPanel *self)
{
        const gchar *language;
Matthias Clasen's avatar
Matthias Clasen committed
386 387 388

        if (response_id == GTK_RESPONSE_OK) {
                language = cc_language_chooser_get_language (GTK_WIDGET (chooser));
389
                update_language (self, language);
Matthias Clasen's avatar
Matthias Clasen committed
390
        }
Matthias Clasen's avatar
Matthias Clasen committed
391 392

        gtk_widget_destroy (GTK_WIDGET (chooser));
Matthias Clasen's avatar
Matthias Clasen committed
393 394
}

395 396 397 398
static void
set_system_region (CcRegionPanel *self,
                   const gchar   *region)
{
399
        if (g_strcmp0 (region, self->system_region) == 0)
400 401
                return;

402 403
        g_free (self->system_region);
        self->system_region = g_strdup (region);
404 405 406 407

        set_localed_locale (self);
}

408
static void
Matthias Clasen's avatar
Matthias Clasen committed
409 410 411
update_region (CcRegionPanel *self,
               const gchar   *region)
{
412
        if (self->login) {
413 414
                set_system_region (self, region);
        } else {
415
                if (g_strcmp0 (region, self->region) == 0)
416
                        return;
417 418
                g_settings_set_string (self->locale_settings, KEY_REGION, region);
                if (self->login_auto_apply)
419 420 421
                        set_system_region (self, region);
                maybe_notify (self, LC_TIME, region);
        }
Matthias Clasen's avatar
Matthias Clasen committed
422 423 424 425 426 427
}

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

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

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

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

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

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

462 463 464 465 466
static const gchar *
get_effective_region (CcRegionPanel *self)
{
        const gchar *region;

467 468
        if (self->login)
                region = self->system_region;
469
        else
470
                region = self->region;
471 472 473 474 475 476 477 478 479 480

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

        return region;
}

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

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

Matthias Clasen's avatar
Matthias Clasen committed
495 496
static void show_input_chooser (CcRegionPanel *self);
static void remove_selected_input (CcRegionPanel *self);
497 498
static void move_selected_input (CcRegionPanel *self,
                                 SystemOp       op);
Matthias Clasen's avatar
Matthias Clasen committed
499 500 501 502 503 504 505

static void
permission_acquired (GObject      *source,
                     GAsyncResult *res,
                     gpointer      data)
{
        CcRegionPanel *self = data;
506
        g_autoptr(GError) error = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
507 508
        gboolean allowed;

509
        allowed = g_permission_acquire_finish (self->permission, res, &error);
Matthias Clasen's avatar
Matthias Clasen committed
510 511 512 513 514 515 516
        if (error) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("Failed to acquire permission: %s\n", error->message);
                return;
        }

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

static void
542 543
activate_language_row (CcRegionPanel *self,
                       GtkListBoxRow *row)
Matthias Clasen's avatar
Matthias Clasen committed
544
{
545 546
        if (row == self->language_row) {
                if (!self->login || g_permission_get_allowed (self->permission)) {
547
                        show_language_chooser (self);
548 549 550
                } else if (g_permission_get_can_acquire (self->permission)) {
                        self->op = CHOOSE_LANGUAGE;
                        g_permission_acquire_async (self->permission,
Matthias Clasen's avatar
Matthias Clasen committed
551 552 553 554
                                                    NULL,
                                                    permission_acquired,
                                                    self);
                }
555 556
        } else if (row == self->formats_row) {
                if (!self->login || g_permission_get_allowed (self->permission)) {
557
                        show_region_chooser (self);
558 559 560
                } else if (g_permission_get_can_acquire (self->permission)) {
                        self->op = CHOOSE_REGION;
                        g_permission_acquire_async (self->permission,
561 562 563 564
                                                    NULL,
                                                    permission_acquired,
                                                    self);
                }
Matthias Clasen's avatar
Matthias Clasen committed
565 566 567
        }
}

568 569 570
static void
update_region_label (CcRegionPanel *self)
{
571
        const gchar *region = get_effective_region (self);
572
        g_autofree gchar *name = NULL;
573

574 575 576 577 578 579
        if (region)
                name = gnome_get_country_from_locale (region, region);

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

580
        gtk_label_set_label (GTK_LABEL (self->formats_label), name);
581 582 583 584 585
}

static void
update_region_from_setting (CcRegionPanel *self)
{
586 587
        g_free (self->region);
        self->region = g_settings_get_string (self->locale_settings, KEY_REGION);
588 589 590
        update_region_label (self);
}

Matthias Clasen's avatar
Matthias Clasen committed
591
static void
Matthias Clasen's avatar
Matthias Clasen committed
592
update_language_label (CcRegionPanel *self)
Matthias Clasen's avatar
Matthias Clasen committed
593
{
594
        const gchar *language = get_effective_language (self);
595
        g_autofree gchar *name = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
596

597
        if (language)
598
                name = gnome_get_language_from_locale (language, language);
599 600 601 602

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

603
        gtk_label_set_label (GTK_LABEL (self->language_label), name);
604 605 606

        /* Formats will change too if not explicitly set. */
        update_region_label (self);
Matthias Clasen's avatar
Matthias Clasen committed
607 608 609 610 611
}

static void
update_language_from_user (CcRegionPanel *self)
{
612
        const gchar *language = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
613

614 615
        if (act_user_is_loaded (self->user))
                language = act_user_get_language (self->user);
616 617

        if (language == NULL || *language == '\0')
618
                language = setlocale (LC_MESSAGES, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
619

620 621
        g_free (self->language);
        self->language = g_strdup (language);
Matthias Clasen's avatar
Matthias Clasen committed
622
        update_language_label (self);
Matthias Clasen's avatar
Matthias Clasen committed
623 624 625 626 627
}

static void
setup_language_section (CcRegionPanel *self)
{
628 629
        self->user = act_user_manager_get_user_by_id (self->user_manager, getuid ());
        g_signal_connect_swapped (self->user, "notify::language",
Matthias Clasen's avatar
Matthias Clasen committed
630
                                  G_CALLBACK (update_language_from_user), self);
631
        g_signal_connect_swapped (self->user, "notify::is-loaded",
Matthias Clasen's avatar
Matthias Clasen committed
632
                                  G_CALLBACK (update_language_from_user), self);
Matthias Clasen's avatar
Matthias Clasen committed
633

634 635
        self->locale_settings = g_settings_new (GNOME_SYSTEM_LOCALE_DIR);
        g_signal_connect_swapped (self->locale_settings, "changed::" KEY_REGION,
Matthias Clasen's avatar
Matthias Clasen committed
636
                                  G_CALLBACK (update_region_from_setting), self);
Matthias Clasen's avatar
Matthias Clasen committed
637

Robert Ancell's avatar
Robert Ancell committed
638
        g_signal_connect_swapped (self->restart_button, "clicked", G_CALLBACK (restart_now), self);
Matthias Clasen's avatar
Matthias Clasen committed
639

Robert Ancell's avatar
Robert Ancell committed
640
        gtk_list_box_set_selection_mode (GTK_LIST_BOX (self->language_list),
Matthias Clasen's avatar
Matthias Clasen committed
641
                                         GTK_SELECTION_NONE);
Robert Ancell's avatar
Robert Ancell committed
642
        gtk_list_box_set_header_func (GTK_LIST_BOX (self->language_list),
643
                                      cc_list_box_update_header_func,
644
                                      NULL, NULL);
Robert Ancell's avatar
Robert Ancell committed
645
        g_signal_connect_swapped (self->language_list, "row-activated",
646
                                  G_CALLBACK (activate_language_row), self);
Matthias Clasen's avatar
Matthias Clasen committed
647

Matthias Clasen's avatar
Matthias Clasen committed
648
        update_language_from_user (self);
649
        update_region_from_setting (self);
Matthias Clasen's avatar
Matthias Clasen committed
650
}
Matthias Clasen's avatar
Matthias Clasen committed
651

Matthias Clasen's avatar
Matthias Clasen committed
652 653 654 655
#ifdef HAVE_IBUS
static void
update_ibus_active_sources (CcRegionPanel *self)
{
656 657
        g_autoptr(GList) rows = NULL;
        GList *l;
Matthias Clasen's avatar
Matthias Clasen committed
658 659 660 661 662
        GtkWidget *row;
        const gchar *type;
        const gchar *id;
        IBusEngineDesc *engine_desc;
        GtkWidget *label;
Matthias Clasen's avatar
Matthias Clasen committed
663

664
        rows = gtk_container_get_children (GTK_CONTAINER (self->input_list));
Matthias Clasen's avatar
Matthias Clasen committed
665 666 667 668 669 670 671
        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;

672
                engine_desc = g_hash_table_lookup (self->ibus_engines, id);
Matthias Clasen's avatar
Matthias Clasen committed
673
                if (engine_desc) {
674
                        g_autofree gchar *display_name = engine_get_display_name (engine_desc);
Matthias Clasen's avatar
Matthias Clasen committed
675 676 677 678 679
                        label = GTK_WIDGET (g_object_get_data (G_OBJECT (row), "label"));
                        gtk_label_set_text (GTK_LABEL (label), display_name);
                }
        }
}
Matthias Clasen's avatar
Matthias Clasen committed
680

681 682 683 684 685 686 687 688 689
static void
update_input_chooser (CcRegionPanel *self)
{
        GtkWidget *chooser;

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

690
        cc_input_chooser_set_ibus_engines (chooser, self->ibus_engines);
691 692
}

Matthias Clasen's avatar
Matthias Clasen committed
693 694 695 696 697
static void
fetch_ibus_engines_result (GObject       *object,
                           GAsyncResult  *result,
                           CcRegionPanel *self)
{
698 699 700
        g_autoptr(GList) list = NULL;
        GList *l;
        g_autoptr(GError) error = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
701

702
        list = ibus_bus_list_engines_async_finish (IBUS_BUS (object), result, &error);
Matthias Clasen's avatar
Matthias Clasen committed
703
        if (!list && error) {
704 705
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("Couldn't finish IBus request: %s", error->message);
Matthias Clasen's avatar
Matthias Clasen committed
706 707 708
                return;
        }

709
        g_clear_object (&self->ibus_cancellable);
710

Matthias Clasen's avatar
Matthias Clasen committed
711
        /* Maps engine ids to engine description objects */
712
        self->ibus_engines = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_object_unref);
Matthias Clasen's avatar
Matthias Clasen committed
713 714 715 716 717

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

718
                if (g_str_has_prefix (engine_id, "xkb:"))
Matthias Clasen's avatar
Matthias Clasen committed
719
                        g_object_unref (engine);
720
                else
721
                        g_hash_table_replace (self->ibus_engines, (gpointer)engine_id, engine);
Matthias Clasen's avatar
Matthias Clasen committed
722 723 724
        }

        update_ibus_active_sources (self);
725
        update_input_chooser (self);
Matthias Clasen's avatar
Matthias Clasen committed
726
}
Matthias Clasen's avatar
Matthias Clasen committed
727

Matthias Clasen's avatar
Matthias Clasen committed
728 729 730
static void
fetch_ibus_engines (CcRegionPanel *self)
{
731
        self->ibus_cancellable = g_cancellable_new ();
Matthias Clasen's avatar
Matthias Clasen committed
732

733
        ibus_bus_list_engines_async (self->ibus,
Matthias Clasen's avatar
Matthias Clasen committed
734
                                     -1,
735
                                     self->ibus_cancellable,
Matthias Clasen's avatar
Matthias Clasen committed
736 737
                                     (GAsyncReadyCallback)fetch_ibus_engines_result,
                                     self);
Matthias Clasen's avatar
Matthias Clasen committed
738

Matthias Clasen's avatar
Matthias Clasen committed
739
  /* We've got everything we needed, don't want to be called again. */
740
  g_signal_handlers_disconnect_by_func (self->ibus, fetch_ibus_engines, self);
Matthias Clasen's avatar
Matthias Clasen committed
741 742 743
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
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)
{
762 763
        g_autofree gchar *desktop_file_name = NULL;
        g_auto(GStrv) strv = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
764

765 766
        strv = g_strsplit (id, ":", 2);
        desktop_file_name = g_strdup_printf ("ibus-setup-%s.desktop", strv[0]);
Matthias Clasen's avatar
Matthias Clasen committed
767

768
        return g_desktop_app_info_new (desktop_file_name);
Matthias Clasen's avatar
Matthias Clasen committed
769 770 771
}
#endif

772 773 774
static void
remove_no_input_row (GtkContainer *list)
{
775
        g_autoptr(GList) l = NULL;
776 777 778 779 780

        l = gtk_container_get_children (list);
        if (!l)
                return;
        if (l->next != NULL)
781
                return;
782 783 784 785
        if (g_strcmp0 (g_object_get_data (G_OBJECT (l->data), "type"), "none") == 0)
                gtk_container_remove (list, GTK_WIDGET (l->data));
}

Matthias Clasen's avatar
Matthias Clasen committed
786 787 788 789 790 791
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
792 793
{
        GtkWidget *row;
794
        GtkWidget *box;
Matthias Clasen's avatar
Matthias Clasen committed
795
        GtkWidget *label;
Matthias Clasen's avatar
Matthias Clasen committed
796
        GtkWidget *image;
Matthias Clasen's avatar
Matthias Clasen committed
797

798
        remove_no_input_row (GTK_CONTAINER (self->input_list));
799

800 801 802
        row = gtk_list_box_row_new ();
        box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
        gtk_container_add (GTK_CONTAINER (row), box);
Matthias Clasen's avatar
Matthias Clasen committed
803
        label = gtk_label_new (name);
Rui Matos's avatar
Rui Matos committed
804
        gtk_widget_set_halign (label, GTK_ALIGN_START);
805 806
        gtk_widget_set_margin_start (label, 20);
        gtk_widget_set_margin_end (label, 20);
Allan Day's avatar
Allan Day committed
807 808
        gtk_widget_set_margin_top (label, 18);
        gtk_widget_set_margin_bottom (label, 18);
809
        gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
810 811 812

        if (strcmp (type, INPUT_SOURCE_TYPE_IBUS) == 0) {
                image = gtk_image_new_from_icon_name ("system-run-symbolic", GTK_ICON_SIZE_BUTTON);
813 814
                gtk_widget_set_margin_start (image, 20);
                gtk_widget_set_margin_end (image, 20);
Matthias Clasen's avatar
Matthias Clasen committed
815 816
                gtk_widget_set_margin_top (image, 6);
                gtk_widget_set_margin_bottom (image, 6);
817
                gtk_style_context_add_class (gtk_widget_get_style_context (image), "dim-label");
818
                gtk_box_pack_start (GTK_BOX (box), image, FALSE, TRUE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
819 820
        }

Matthias Clasen's avatar
Matthias Clasen committed
821
        gtk_widget_show_all (row);
822
        gtk_container_add (GTK_CONTAINER (self->input_list), row);
Matthias Clasen's avatar
Matthias Clasen committed
823 824 825 826 827 828 829 830

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

831
        cc_list_box_adjust_scrolling (GTK_LIST_BOX (self->input_list));
832

Matthias Clasen's avatar
Matthias Clasen committed
833
        return row;
Matthias Clasen's avatar
Matthias Clasen committed
834 835
}

836 837 838 839 840 841
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
842
static void
Matthias Clasen's avatar
Matthias Clasen committed
843 844
add_input_sources (CcRegionPanel *self,
                   GVariant      *sources)
Matthias Clasen's avatar
Matthias Clasen committed
845
{
Matthias Clasen's avatar
Matthias Clasen committed
846 847 848 849
        GVariantIter iter;
        const gchar *type;
        const gchar *id;
        const gchar *name;
850 851
        g_autofree gchar *display_name = NULL;
        g_autoptr(GDesktopAppInfo) app_info = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
852

853 854 855 856 857
        if (g_variant_n_children (sources) < 1) {
                add_no_input_row (self);
                return;
        }

Matthias Clasen's avatar
Matthias Clasen committed
858 859 860 861 862 863
        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)) {