[gnome-control-center] region: Initial implementation of 'copy to system'



commit 749148cda67fb81ba147c726d84fc2a9481378f5
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Aug 22 12:35:35 2011 -0400

    region: Initial implementation of 'copy to system'
    
    This implementation is using the systemd-localed mechanism.
    It currently does not support keyboard layouts, unfortunately.

 configure.ac                              |    1 +
 panels/region/gnome-region-panel-system.c |  211 +++++++++++++++++++++++++++++
 panels/region/gnome-region-panel.ui       |    2 +-
 3 files changed, 213 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 962c0f8..f1a1913 100644
--- a/configure.ac
+++ b/configure.ac
@@ -110,6 +110,7 @@ PKG_CHECK_MODULES(COLOR_PANEL, $COMMON_MODULES colord >= 0.1.8)
 PKG_CHECK_MODULES(PRINTERS_PANEL, $COMMON_MODULES dbus-glib-1
                   polkit-gobject-1 >= $POLKIT_REQUIRED_VERSION)
 PKG_CHECK_MODULES(REGION_PANEL, $COMMON_MODULES libgnomekbd >= 2.91.91
+                  polkit-gobject-1 >= $POLKIT_REQUIRED_VERSION
                   libxklavier >= 5.1 libgnomekbdui >= 2.91.91)
 PKG_CHECK_MODULES(SCREEN_PANEL, $COMMON_MODULES)
 PKG_CHECK_MODULES(SOUND_PANEL, $COMMON_MODULES libxml-2.0
diff --git a/panels/region/gnome-region-panel-system.c b/panels/region/gnome-region-panel-system.c
index 9840768..d76d6b2 100644
--- a/panels/region/gnome-region-panel-system.c
+++ b/panels/region/gnome-region-panel-system.c
@@ -23,6 +23,12 @@
 #  include <config.h>
 #endif
 
+#include <string.h>
+
+#include <polkit/polkit.h>
+
+#include <glib/gi18n.h>
+
 #include <libgnomekbd/gkbd-keyboard-config.h>
 #include "cc-common-language.h"
 #include "gdm-languages.h"
@@ -30,6 +36,8 @@
 #include "gnome-region-panel-xkb.h"
 
 static GSettings *locale_settings, *xkb_settings;
+static GDBusProxy *localed_proxy;
+static GPermission *localed_permission;
 
 static void
 locale_settings_changed (GSettings *settings,
@@ -49,6 +57,7 @@ locale_settings_changed (GSettings *settings,
         display_region = gdm_get_region_from_name (region, NULL);
         label = GTK_WIDGET (gtk_builder_get_object (builder, "user_format"));
         gtk_label_set_text (GTK_LABEL (label), display_region);
+        g_object_set_data_full (G_OBJECT (label), "region", g_strdup (region), g_free);
         g_free (region);
         g_free (display_region);
 }
@@ -97,10 +106,200 @@ xkb_settings_changed (GSettings *settings,
 	g_string_free (str, TRUE);
 }
 
+static void
+on_localed_properties_changed (GDBusProxy   *proxy,
+                               GVariant     *changed_properties,
+                               const gchar **invalidated_properties,
+                               GtkBuilder   *builder)
+{
+        GVariant *res;
+        GVariant *v;
+
+        res = g_dbus_connection_call_sync (g_dbus_proxy_get_connection (proxy),
+                                           g_dbus_proxy_get_name (proxy),
+                                            g_dbus_proxy_get_object_path (proxy),
+                                           "org.freedesktop.DBus.Properties",
+                                           "Get",
+                                           g_variant_new ("(ss)",
+                                                          g_dbus_proxy_get_interface_name (proxy),
+                                                          "Locale"),
+                                           NULL,
+                                           G_DBUS_CALL_FLAGS_NONE,
+                                           -1, NULL, NULL);
+        v = g_variant_get_child_value (res, 0);
+        if (v) {
+                const gchar **strv;
+                gsize len;
+                gint i;
+                const gchar *lang, *messages, *time;
+                gchar *name;
+                GtkWidget *label;
+                GVariant *v2;
+
+                v2 = g_variant_get_variant (v);
+                strv = g_variant_get_strv (v2, &len);
+
+                lang = messages = time = NULL;
+                for (i = 0; strv[i]; i++) {
+                        if (g_str_has_prefix (strv[i], "LANG=")) {
+                                lang = strv[i] + strlen ("LANG=");
+                        }
+                        else if (g_str_has_prefix (strv[i], "LC_MESSAGES=")) {
+                                messages = strv[i] + strlen ("LC_MESSAGES=");
+                        }
+                        else if (g_str_has_prefix (strv[i], "LC_TIME=")) {
+                                time = strv[i] + strlen ("LC_TIME=");
+                        }
+                }
+                if (!messages) {
+                        messages = lang;
+                }
+                if (!time) {
+                        time = lang;
+                }
+
+                if (messages) {
+                        name = gdm_get_language_from_name (messages, NULL);
+                        label = (GtkWidget*)gtk_builder_get_object (builder, "system_display_language");
+                        gtk_label_set_text (GTK_LABEL (label), name);
+                        g_free (name);
+                }
+
+                if (time) {
+                        name = gdm_get_region_from_name (time, NULL);
+                        label = (GtkWidget*)gtk_builder_get_object (builder, "system_format");
+                        gtk_label_set_text (GTK_LABEL (label), name);
+                        g_free (name);
+                }
+                g_variant_unref (v);
+        }
+        g_variant_unref (res);
+}
+
+static void
+localed_proxy_ready (GObject      *source,
+                     GAsyncResult *res,
+                     gpointer      user_data)
+{
+        GtkBuilder *builder = user_data;
+        GError *error = NULL;
+
+        localed_proxy = g_dbus_proxy_new_finish (res, &error);
+
+        if (!localed_proxy) {
+                g_warning ("Failed to contact localed: %s\n", error->message);
+                g_error_free (error);
+                return;
+        }
+
+        g_object_weak_ref (G_OBJECT (builder), (GWeakNotify) g_object_unref, localed_proxy);
+
+        g_signal_connect (localed_proxy, "g-properties-changed",
+                          G_CALLBACK (on_localed_properties_changed), builder);
+
+        on_localed_properties_changed (localed_proxy, NULL, NULL, builder);
+}
+
+static void
+copy_settings (GtkButton *button, GtkBuilder *builder)
+{
+        const gchar *language;
+        const gchar *region;
+        GtkWidget *label;
+        GVariantBuilder *b;
+        gchar *s;
+
+        label = GTK_WIDGET (gtk_builder_get_object (builder, "user_display_language"));
+        language = g_object_get_data (G_OBJECT (label), "language");
+        label = GTK_WIDGET (gtk_builder_get_object (builder, "user_format"));
+        region = g_object_get_data (G_OBJECT (label), "region");
+
+        b = g_variant_builder_new (G_VARIANT_TYPE ("as"));
+        s = g_strconcat ("LANG=", language, NULL);
+        g_variant_builder_add (b, "s", s);
+        g_free (s);
+        if (g_strcmp0 (language, region) != 0) {
+                s = g_strconcat ("LC_TIME=", region, NULL);
+                g_variant_builder_add (b, "s", s);
+                g_free (s);
+                s = g_strconcat ("LC_NUMERIC=", region, NULL);
+                g_variant_builder_add (b, "s", s);
+                g_free (s);
+                s = g_strconcat ("LC_MONETARY=", region, NULL);
+                g_variant_builder_add (b, "s", s);
+                g_free (s);
+                s = g_strconcat ("LC_MEASUREMENT=", region, NULL);
+                g_variant_builder_add (b, "s", s);
+                g_free (s);
+        }
+
+        g_dbus_proxy_call (localed_proxy,
+                           "SetLocale",
+                           g_variant_new ("(asb)", b, TRUE),
+                           G_DBUS_CALL_FLAGS_NONE,
+                           -1, NULL, NULL, NULL);
+        g_variant_builder_unref (b);
+}
+
+static void
+on_permission_changed (GPermission *permission,
+                       GParamSpec  *pspec,
+                       GtkBuilder  *builder)
+{
+        GtkWidget *button;
+        GtkWidget *label;
+        gboolean can_acquire;
+        gboolean allowed;
+
+        if (permission) {
+                can_acquire = g_permission_get_can_acquire (permission);
+                allowed = g_permission_get_allowed (permission);
+        }
+        else {
+                can_acquire = FALSE;
+                allowed = FALSE;
+        }
+
+        button = (GtkWidget *)gtk_builder_get_object (builder, "copy_settings_button");
+        label = (GtkWidget *)gtk_builder_get_object (builder, "system-title");
+
+        if (!allowed && !can_acquire) {
+                gtk_label_set_text (GTK_LABEL (label),
+                                    _("The login screen, system accounts and new user accounts use the system-wide Region and Language settings."));
+                gtk_widget_hide (button);
+        }
+        else {
+                gtk_label_set_text (GTK_LABEL (label),
+                                    _("The login screen, system accounts and new user accounts use the system-wide Region and Language settings. You may change the system settings to match yours."));
+                gtk_widget_show (button);
+                if (allowed) {
+                        gtk_button_set_label (GTK_BUTTON (button), _("Copy Settings"));
+                }
+                else {
+                        gtk_button_set_label (GTK_BUTTON (button), _("Copy Settings..."));
+                }
+        }
+}
+
 void
 setup_system (GtkBuilder *builder)
 {
 	gchar *language;
+        GDBusConnection *bus;
+        GtkWidget *button;
+
+        button = (GtkWidget *)gtk_builder_get_object (builder, "copy_settings_button");
+        g_signal_connect (button, "clicked",
+                          G_CALLBACK (copy_settings), builder);
+
+        localed_permission = polkit_permission_new_sync ("org.freedesktop.locale1.set-locale", NULL, NULL, NULL);
+        if (localed_permission != NULL) {
+	        g_object_weak_ref (G_OBJECT (builder), (GWeakNotify) g_object_unref, localed_permission);
+
+                g_signal_connect (localed_permission, "notify",
+                                  G_CALLBACK (on_permission_changed), builder);
+        }
+        on_permission_changed (localed_permission, NULL, builder);
 
 	locale_settings = g_settings_new ("org.gnome.system.locale");
 	g_signal_connect (locale_settings, "changed::region",
@@ -120,4 +319,16 @@ setup_system (GtkBuilder *builder)
         locale_settings_changed (locale_settings, "region", builder);
 
         xkb_settings_changed (xkb_settings, "layouts", builder);
+
+        bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+        g_dbus_proxy_new (bus,
+                           G_DBUS_PROXY_FLAGS_NONE,
+                           NULL,
+                           "org.freedesktop.locale1",
+                           "/org/freedesktop/locale1",
+                           "org.freedesktop.locale1",
+                           NULL,
+                           localed_proxy_ready,
+                           builder);
+        g_object_unref (bus);
 }
diff --git a/panels/region/gnome-region-panel.ui b/panels/region/gnome-region-panel.ui
index c10fd59..b189c25 100644
--- a/panels/region/gnome-region-panel.ui
+++ b/panels/region/gnome-region-panel.ui
@@ -968,7 +968,7 @@ default settings</property>
                     <property name="column_spacing">12</property>
                     <property name="row_spacing">12</property>
                     <child>
-                      <object class="GtkLabel" id="label13">
+                      <object class="GtkLabel" id="system-title">
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
                         <property name="xalign">0</property>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]