[gnome-control-center] info: Implement setting of "PrettyHostname"



commit a089d7e710e652693898cb17cde29f4eb266e994
Author: Bastien Nocera <hadess hadess net>
Date:   Thu May 12 14:46:50 2011 +0100

    info: Implement setting of "PrettyHostname"
    
    This property can be used to name services running on the machine
    such as media sharing, device sharing, or externally visible
    services such as the Bluetooth name.
    
    We don't set the statis hostname yet though, this will need
    to be done to follow the recommendations from systemd.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=650044

 configure.ac                |    3 +-
 panels/info/Makefile.am     |    1 +
 panels/info/cc-info-panel.c |  136 +++++++++++++++++++++++++++++++++++++++++--
 panels/info/info.ui         |   12 ++--
 4 files changed, 139 insertions(+), 13 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 23ee455..67c067f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -97,7 +97,8 @@ PKG_CHECK_MODULES(DATETIME_PANEL, $COMMON_MODULES dbus-glib-1
                   polkit-gobject-1 >= $POLKIT_REQUIRED_VERSION
                   gdk-pixbuf-2.0 >= $GDKPIXBUF_REQUIRED_VERSION)
 PKG_CHECK_MODULES(DISPLAY_PANEL, $COMMON_MODULES dbus-glib-1 gnome-desktop-3.0 >= 3.1.0)
-PKG_CHECK_MODULES(INFO_PANEL, $COMMON_MODULES libgtop-2.0)
+PKG_CHECK_MODULES(INFO_PANEL, $COMMON_MODULES libgtop-2.0
+		  polkit-gobject-1 >= $POLKIT_REQUIRED_VERSION)
 PKG_CHECK_MODULES(KEYBOARD_PANEL, $COMMON_MODULES gconf-2.0 x11)
 PKG_CHECK_MODULES(MEDIA_PANEL, $COMMON_MODULES)
 PKG_CHECK_MODULES(MOUSE_PANEL, $COMMON_MODULES xi >= 1.2
diff --git a/panels/info/Makefile.am b/panels/info/Makefile.am
index 47eea26..1a9e81c 100644
--- a/panels/info/Makefile.am
+++ b/panels/info/Makefile.am
@@ -8,6 +8,7 @@ INCLUDES = 						\
 	-DGNOMECC_DATA_DIR="\"$(pkgdatadir)\""		\
 	-DDATADIR="\"$(datadir)\""			\
 	-DLIBEXECDIR="\"$(libexecdir)\""		\
+	-I$(top_srcdir)/libgnome-control-center/	\
 	$(NULL)
 
 ccpanelsdir = $(PANELS_DIR)
diff --git a/panels/info/cc-info-panel.c b/panels/info/cc-info-panel.c
index a68dff4..5e83e91 100644
--- a/panels/info/cc-info-panel.c
+++ b/panels/info/cc-info-panel.c
@@ -24,6 +24,7 @@
 #include "cc-info-panel.h"
 
 #include <sys/vfs.h>
+#include <polkit/polkit.h>
 
 #include <glib.h>
 #include <glib/gi18n.h>
@@ -68,9 +69,10 @@ struct _CcInfoPanelPrivate
   GCancellable  *cancellable;
 
   GDBusConnection     *session_bus;
-  GDBusProxy    *pk_proxy;
-  GDBusProxy    *pk_transaction_proxy;
-  GSettings     *session_settings;
+  GDBusProxy          *pk_proxy;
+  GDBusProxy          *pk_transaction_proxy;
+  GDBusProxy          *hostnamed_proxy;
+  GSettings           *session_settings;
 
   GraphicsData  *graphics_data;
 };
@@ -504,6 +506,12 @@ cc_info_panel_finalize (GObject *object)
   g_free (priv->gnome_date);
   g_free (priv->gnome_distributor);
 
+  if (priv->hostnamed_proxy != NULL)
+    {
+      g_object_unref (priv->hostnamed_proxy);
+      priv->hostnamed_proxy = NULL;
+    }
+
   G_OBJECT_CLASS (cc_info_panel_parent_class)->finalize (object);
 }
 
@@ -591,8 +599,8 @@ format_size_for_display (goffset size)
 
 static void
 query_done (GFile        *file,
-	    GAsyncResult *res,
-	    CcInfoPanel  *self)
+            GAsyncResult *res,
+            CcInfoPanel  *self)
 {
   GFileInfo *info;
   GError *error = NULL;
@@ -1037,6 +1045,118 @@ info_panel_setup_selector (CcInfoPanel  *self)
   gtk_widget_show_all (GTK_WIDGET (view));
 }
 
+static char *
+info_panel_get_hostname (CcInfoPanel  *self)
+{
+  GVariant *variant;
+  char *str;
+
+  variant = g_dbus_proxy_get_cached_property (self->priv->hostnamed_proxy,
+                                              "PrettyHostname");
+  if (!variant)
+    {
+      GError *error = NULL;
+      GVariant *inner;
+
+      /* Work around systemd-hostname not sending us back
+       * the property value when changing values */
+      variant = g_dbus_proxy_call_sync (self->priv->hostnamed_proxy,
+                                        "org.freedesktop.DBus.Properties.Get",
+                                        g_variant_new ("(ss)", "org.freedesktop.hostname1", "PrettyHostname"),
+                                        G_DBUS_CALL_FLAGS_NONE,
+                                        -1,
+                                        NULL,
+                                        &error);
+      if (variant == NULL)
+        {
+          g_warning ("Failed to get property 'PrettyHostname': %s", error->message);
+          g_error_free (error);
+          return NULL;
+        }
+
+      g_variant_get (variant, "(v)", &inner);
+      str = g_variant_dup_string (inner, NULL);
+      g_variant_unref (variant);
+    }
+  else
+    {
+      str = g_variant_dup_string (variant, NULL);
+      g_variant_unref (variant);
+    }
+
+  /* Empty strings means that we need to fallback */
+  if (str != NULL &&
+      *str == '\0')
+    {
+      g_free (str);
+      str = NULL;
+    }
+
+  return str;
+}
+
+static void
+text_changed_cb (GtkEntry        *entry,
+                 CcInfoPanel     *self)
+{
+  const char *text;
+  GVariant *variant;
+  GError *error = NULL;
+
+  text = gtk_entry_get_text (GTK_ENTRY (entry));
+
+  variant = g_dbus_proxy_call_sync (self->priv->hostnamed_proxy,
+                                    "SetPrettyHostname",
+                                    g_variant_new ("(sb)", text, FALSE),
+                                    G_DBUS_CALL_FLAGS_NONE,
+                                    -1, NULL, &error);
+  if (variant == NULL)
+    {
+      g_warning ("Could not set PrettyHostname: %s", error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      g_variant_unref (variant);
+    }
+}
+
+static void
+info_panel_setup_hostname (CcInfoPanel  *self,
+                           GPermission  *permission)
+{
+  char *str;
+  GtkWidget *entry;
+
+  entry = WID ("name_entry");
+
+  gtk_widget_show (WID ("label4"));
+  gtk_widget_show (entry);
+
+  if (g_permission_get_allowed (permission) != FALSE)
+    gtk_widget_set_sensitive (entry, TRUE);
+
+  self->priv->hostnamed_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                                               G_DBUS_PROXY_FLAGS_NONE,
+                                                               NULL,
+                                                               "org.freedesktop.hostname1",
+                                                               "/org/freedesktop/hostname1",
+                                                               "org.freedesktop.hostname1",
+                                                               NULL,
+                                                               NULL);
+
+  /* This could only happen if the policy file was installed
+   * but not hostnamed, which points to a system bug */
+  g_assert (self->priv->hostnamed_proxy);
+
+  str = info_panel_get_hostname (self);
+  gtk_entry_set_text (GTK_ENTRY (entry), str);
+  g_free (str);
+
+  g_signal_connect (G_OBJECT (entry), "changed",
+		    G_CALLBACK (text_changed_cb), self);
+}
+
 static void
 info_panel_setup_overview (CcInfoPanel  *self)
 {
@@ -1045,6 +1165,12 @@ info_panel_setup_overview (CcInfoPanel  *self)
   glibtop_mem mem;
   const glibtop_sysinfo *info;
   char       *text;
+  GPermission *permission;
+
+  permission = polkit_permission_new_sync ("org.freedesktop.hostname1.set-static-hostname", NULL, NULL, NULL);
+  /* Is hostnamed installed? */
+  if (permission != NULL)
+    info_panel_setup_hostname (self, permission);
 
   res = load_gnome_version (&self->priv->gnome_version,
                             &self->priv->gnome_distributor,
diff --git a/panels/info/info.ui b/panels/info/info.ui
index af9b0f3..4b1593a 100644
--- a/panels/info/info.ui
+++ b/panels/info/info.ui
@@ -165,13 +165,11 @@
                                   </packing>
                                 </child>
                                 <child>
-                                  <object class="GtkEntry" id="entry1">
-                                    <property name="can_focus">True</property>
-                                    <property name="no_show_all">True</property>
-                                    <property name="invisible_char">â??</property>
-                                    <property name="width_chars">12</property>
-                                    <property name="invisible_char_set">True</property>
-                                    <property name="caps_lock_warning">False</property>
+                                  <object class="GtkEntry" id="name_entry">
+                                    <property name="visible">False</property>
+                                    <property name="xalign">0</property>
+                                    <property name="text"></property>
+                                    <property name="sensitive">False</property>
                                   </object>
                                   <packing>
                                     <property name="left_attach">1</property>



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