~charlesk/gnome-control-center/make-new-panel

« back to all changes in this revision

Viewing changes to panels/region/gnome-region-panel-lang.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Bastien Nocera
 
3
 *
 
4
 * Written by: Bastien Nocera <hadess@hadess.net>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2, or (at your option)
 
9
 * any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
19
 * 02111-1307, USA.
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#  include <config.h>
 
24
#endif
 
25
 
 
26
#include <string.h>
 
27
#include <glib/gi18n.h>
 
28
 
 
29
#include "gnome-region-panel-lang.h"
 
30
#include "cc-common-language.h"
 
31
#include "gdm-languages.h"
 
32
 
 
33
static GDBusProxy *proxy = NULL;
 
34
 
 
35
static void
 
36
add_other_users_language (GHashTable *ht)
 
37
{
 
38
        GVariant *variant;
 
39
        GVariantIter *vi;
 
40
        GError *error = NULL;
 
41
        const char *str;
 
42
 
 
43
        if (proxy == NULL)
 
44
                return;
 
45
 
 
46
        variant = g_dbus_proxy_call_sync (proxy,
 
47
                                          "ListCachedUsers",
 
48
                                          NULL,
 
49
                                          G_DBUS_CALL_FLAGS_NONE,
 
50
                                          -1,
 
51
                                          NULL,
 
52
                                          &error);
 
53
        if (variant == NULL) {
 
54
                g_warning ("Failed to list existing users: %s", error->message);
 
55
                g_error_free (error);
 
56
                return;
 
57
        }
 
58
 
 
59
        g_variant_get (variant, "(ao)", &vi);
 
60
        while (g_variant_iter_loop (vi, "o", &str)) {
 
61
                GDBusProxy *user;
 
62
                GVariant *props;
 
63
                const char *name;
 
64
                char *language;
 
65
 
 
66
                user = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
 
67
                                                      G_DBUS_PROXY_FLAGS_NONE,
 
68
                                                      NULL,
 
69
                                                      "org.freedesktop.Accounts",
 
70
                                                      str,
 
71
                                                      "org.freedesktop.Accounts.User",
 
72
                                                      NULL,
 
73
                                                      &error);
 
74
                if (user == NULL) {
 
75
                        g_warning ("Failed to get proxy for user '%s': %s",
 
76
                                   str, error->message);
 
77
                        g_error_free (error);
 
78
                        error = NULL;
 
79
                        continue;
 
80
                }
 
81
                props = g_dbus_proxy_get_cached_property (user, "Language");
 
82
                name = g_variant_get_string (props, NULL);
 
83
                if (name != NULL && *name != '\0') {
 
84
                        language = gdm_get_language_from_name (name, NULL);
 
85
                        g_hash_table_insert (ht, g_strdup (name), language);
 
86
                }
 
87
                g_variant_unref (props);
 
88
                g_object_unref (user);
 
89
        }
 
90
        g_variant_iter_free (vi);
 
91
        g_variant_unref (variant);
 
92
}
 
93
 
 
94
static GHashTable *
 
95
new_ht_for_user_languages (void)
 
96
{
 
97
        GHashTable *ht;
 
98
        char *name;
 
99
        char *language;
 
100
 
 
101
        ht = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
102
 
 
103
        /* Add the languages used by other users on the system */
 
104
        add_other_users_language (ht);
 
105
 
 
106
        /* Add current locale */
 
107
        name = cc_common_language_get_current_language ();
 
108
        if (g_hash_table_lookup (ht, name) == NULL) {
 
109
                language = gdm_get_language_from_name (name, NULL);
 
110
                g_hash_table_insert (ht, name, language);
 
111
        } else {
 
112
                g_free (name);
 
113
        }
 
114
 
 
115
        return ht;
 
116
}
 
117
 
 
118
static void
 
119
selection_changed (GtkTreeSelection *selection,
 
120
                   GtkTreeView      *list)
 
121
{
 
122
        GtkTreeModel *model;
 
123
        GtkTreeIter iter;
 
124
        char *locale;
 
125
        GDBusProxy *user;
 
126
        GVariant *variant;
 
127
        GError *error = NULL;
 
128
        char *object_path;
 
129
 
 
130
        if (gtk_tree_selection_get_selected (selection, &model, &iter) == FALSE) {
 
131
                g_warning ("No selected languages, this shouldn't happen");
 
132
                return;
 
133
        }
 
134
 
 
135
        user = NULL;
 
136
        variant = NULL;
 
137
 
 
138
        gtk_tree_model_get (model, &iter,
 
139
                            LOCALE_COL, &locale,
 
140
                            -1);
 
141
 
 
142
        if (proxy == NULL) {
 
143
                g_warning ("Would change the language to '%s', but no D-Bus connection available", locale);
 
144
                goto bail;
 
145
        }
 
146
 
 
147
        variant = g_dbus_proxy_call_sync (proxy,
 
148
                                          "FindUserByName",
 
149
                                          g_variant_new ("(s)", g_get_user_name ()),
 
150
                                          G_DBUS_CALL_FLAGS_NONE,
 
151
                                          -1,
 
152
                                          NULL,
 
153
                                          &error);
 
154
        if (variant == NULL) {
 
155
                g_warning ("Could not contact accounts service to look up '%s': %s",
 
156
                           g_get_user_name (), error->message);
 
157
                g_error_free (error);
 
158
                goto bail;
 
159
        }
 
160
 
 
161
        g_variant_get (variant, "(o)", &object_path);
 
162
        user = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
 
163
                                              G_DBUS_PROXY_FLAGS_NONE,
 
164
                                              NULL,
 
165
                                              "org.freedesktop.Accounts",
 
166
                                              object_path,
 
167
                                              "org.freedesktop.Accounts.User",
 
168
                                              NULL,
 
169
                                              &error);
 
170
        g_free (object_path);
 
171
 
 
172
        if (user == NULL) {
 
173
                g_warning ("Could not create proxy for user '%s': %s",
 
174
                           g_variant_get_string (variant, NULL), error->message);
 
175
                g_error_free (error);
 
176
                goto bail;
 
177
        }
 
178
        g_variant_unref (variant);
 
179
 
 
180
        variant = g_dbus_proxy_call_sync (user,
 
181
                                          "SetLanguage",
 
182
                                          g_variant_new ("(s)", locale),
 
183
                                          G_DBUS_CALL_FLAGS_NONE,
 
184
                                          -1,
 
185
                                          NULL,
 
186
                                          &error);
 
187
        if (variant == NULL) {
 
188
                g_warning ("Failed to set the language '%s': %s", locale, error->message);
 
189
                g_error_free (error);
 
190
                goto bail;
 
191
        }
 
192
 
 
193
        /* And done */
 
194
 
 
195
bail:
 
196
        if (variant != NULL)
 
197
                g_variant_unref (variant);
 
198
        if (user != NULL)
 
199
                g_object_unref (user);
 
200
        g_free (locale);
 
201
}
 
202
 
 
203
static void
 
204
remove_timeout (gpointer data,
 
205
                GObject *where_the_object_was)
 
206
{
 
207
        guint timeout = GPOINTER_TO_UINT (data);
 
208
        g_source_remove (timeout);
 
209
}
 
210
 
 
211
static void
 
212
remove_async (gpointer data)
 
213
{
 
214
  guint id = GPOINTER_TO_UINT (data);
 
215
 
 
216
  /* if the idle is already done, this harmlessly fails */
 
217
  g_source_remove (id);
 
218
}
 
219
 
 
220
static gboolean
 
221
finish_language_setup (gpointer user_data)
 
222
{
 
223
        GtkWidget *list = (GtkWidget *) user_data;
 
224
        GtkTreeModel *model;
 
225
        GtkWidget *parent;
 
226
        GHashTable *user_langs;
 
227
        guint timeout;
 
228
        GtkTreeSelection *selection;
 
229
        guint async_id;
 
230
 
 
231
        /* Did we get called after the widget was destroyed? */
 
232
        if (list == NULL)
 
233
                return FALSE;
 
234
 
 
235
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
 
236
        user_langs = g_object_get_data (G_OBJECT (list), "user-langs");
 
237
 
 
238
        async_id = cc_common_language_add_available_languages (GTK_LIST_STORE (model), user_langs);
 
239
 
 
240
        g_object_set_data_full (G_OBJECT (list), "language-async",
 
241
                                GUINT_TO_POINTER (async_id), remove_async);
 
242
 
 
243
        parent = gtk_widget_get_toplevel (list);
 
244
        gdk_window_set_cursor (gtk_widget_get_window (parent), NULL);
 
245
 
 
246
        g_object_set_data (G_OBJECT (list), "user-langs", NULL);
 
247
        timeout = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (list), "timeout"));
 
248
        g_object_weak_unref (G_OBJECT (list), (GWeakNotify) remove_timeout, GUINT_TO_POINTER (timeout));
 
249
 
 
250
        /* And select the current language */
 
251
        cc_common_language_select_current_language (GTK_TREE_VIEW (list));
 
252
 
 
253
        /* And now listen for changes */
 
254
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (list));
 
255
        g_signal_connect (G_OBJECT (selection), "changed",
 
256
                          G_CALLBACK (selection_changed), list);
 
257
 
 
258
        return FALSE;
 
259
}
 
260
 
 
261
void
 
262
setup_language (GtkBuilder *builder)
 
263
{
 
264
        GtkWidget *treeview;
 
265
        GHashTable *user_langs;
 
266
        GtkWidget *parent;
 
267
        GdkWindow *window;
 
268
        guint timeout;
 
269
        GError *error = NULL;
 
270
 
 
271
        treeview = GTK_WIDGET (gtk_builder_get_object (builder, "display_language_treeview"));
 
272
        parent = gtk_widget_get_toplevel (treeview);
 
273
 
 
274
        /* Setup accounts service */
 
275
        proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
 
276
                                               G_DBUS_PROXY_FLAGS_NONE,
 
277
                                               NULL,
 
278
                                               "org.freedesktop.Accounts",
 
279
                                               "/org/freedesktop/Accounts",
 
280
                                               "org.freedesktop.Accounts",
 
281
                                               NULL,
 
282
                                               &error);
 
283
        if (proxy == NULL) {
 
284
                g_warning ("Failed to contact accounts service: %s", error->message);
 
285
                g_error_free (error);
 
286
        } else {
 
287
                g_object_weak_ref (G_OBJECT (treeview), (GWeakNotify) g_object_unref, proxy);
 
288
        }
 
289
 
 
290
        /* Add user languages */
 
291
        user_langs = new_ht_for_user_languages ();
 
292
        cc_common_language_setup_list (treeview, user_langs);
 
293
 
 
294
        /* Setup so that the list is populated after the list appears */
 
295
        window = gtk_widget_get_window (parent);
 
296
        if (window) {
 
297
                GdkCursor *cursor;
 
298
 
 
299
                cursor = gdk_cursor_new (GDK_WATCH);
 
300
                gdk_window_set_cursor (gtk_widget_get_window (parent), cursor);
 
301
                g_object_unref (cursor);
 
302
        }
 
303
 
 
304
        g_object_set_data_full (G_OBJECT (treeview), "user-langs",
 
305
                                user_langs, (GDestroyNotify) g_hash_table_destroy);
 
306
        timeout = g_idle_add ((GSourceFunc) finish_language_setup, treeview);
 
307
        g_object_set_data (G_OBJECT (treeview), "timeout", GUINT_TO_POINTER (timeout));
 
308
        g_object_weak_ref (G_OBJECT (treeview), (GWeakNotify) remove_timeout, GUINT_TO_POINTER (timeout));
 
309
}