[gnome-control-center] sharing: hide the remote login button if the service is not available



commit 2bbac66dcba8d822aac29bdd6107365851bf007c
Author: Thomas Wood <thomas wood intel com>
Date:   Thu Jan 31 12:05:44 2013 +0000

    sharing: hide the remote login button if the service is not available
    
    https://bugzilla.gnome.org/show_bug.cgi?id=692047

 panels/sharing/cc-remote-login.c  |  111 +++++++++++++++++++++++++++---------
 panels/sharing/cc-remote-login.h  |    7 ++-
 panels/sharing/cc-sharing-panel.c |   20 +++++--
 3 files changed, 103 insertions(+), 35 deletions(-)
---
diff --git a/panels/sharing/cc-remote-login.c b/panels/sharing/cc-remote-login.c
index fcd0a20..3834c03 100644
--- a/panels/sharing/cc-remote-login.c
+++ b/panels/sharing/cc-remote-login.c
@@ -23,10 +23,17 @@
 
 #define SSHD_SERVICE "sshd.service"
 
+typedef struct
+{
+  GtkSwitch    *gtkswitch;
+  GtkWidget    *button;
+  GCancellable *cancellable;
+} CallbackData;
+
 static void
 active_state_ready_callback (GObject      *source_object,
                              GAsyncResult *result,
-                             gpointer      gtkswitch)
+                             CallbackData *callback_data)
 {
   GVariant *active_variant, *tmp_variant;
   const gchar *active_state;
@@ -38,8 +45,16 @@ active_state_ready_callback (GObject      *source_object,
 
   if (!active_variant)
     {
-      g_warning ("Error getting remote login state: %s", error->message);
+      /* print a warning if there was an error but the operation was not
+       * cancelled */
+      if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_warning ("Error getting remote login state: %s", error->message);
+
       g_clear_error (&error);
+      g_free (callback_data);
+
+      /* the switch will be remain insensitive, since the current state could
+       * not be determined */
       return;
     }
 
@@ -52,19 +67,24 @@ active_state_ready_callback (GObject      *source_object,
   g_variant_unref (tmp_variant);
 
   /* set the switch to the correct state */
-  if (gtk_switch_get_active (gtkswitch) != active)
+  if (callback_data->gtkswitch)
     {
-      g_object_set_data (G_OBJECT (gtkswitch), "set-from-dbus",
-                         GINT_TO_POINTER (1));
-      gtk_switch_set_active (gtkswitch, active);
+      if (gtk_switch_get_active (callback_data->gtkswitch) != active)
+        {
+          g_object_set_data (G_OBJECT (callback_data->gtkswitch), "set-from-dbus",
+                             GINT_TO_POINTER (1));
+          gtk_switch_set_active (callback_data->gtkswitch, active);
+        }
+      gtk_widget_set_sensitive (GTK_WIDGET (callback_data->gtkswitch), TRUE);
     }
-  gtk_widget_set_sensitive (gtkswitch, TRUE);
+
+  g_free (callback_data);
 }
 
 static void
 path_ready_callback (GObject      *source_object,
                      GAsyncResult *result,
-                     gpointer      gtkswitch)
+                     CallbackData *callback_data)
 {
   GVariant *path_variant;
   gchar *object_path;
@@ -75,8 +95,25 @@ path_ready_callback (GObject      *source_object,
 
   if (!path_variant)
     {
-      g_warning ("Error getting remote login state: %s", error->message);
+      if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        {
+          g_free (callback_data);
+          g_clear_error (&error);
+
+          return;
+        }
+
+      /* this may fail if systemd or remote login service is not available */
+      g_debug ("Error getting remote login state: %s", error->message);
+
       g_clear_error (&error);
+
+      /* hide the remote login button, since the service is not available */
+      if (callback_data->button)
+        gtk_widget_hide (callback_data->button);
+
+      g_free (callback_data);
+
       return;
     }
 
@@ -93,19 +130,17 @@ path_ready_callback (GObject      *source_object,
                           (GVariantType*) "(v)",
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
-                          NULL,
-                          active_state_ready_callback,
-                          gtkswitch);
-
+                          callback_data->cancellable,
+                          (GAsyncReadyCallback) active_state_ready_callback,
+                          callback_data);
 
   g_variant_unref (path_variant);
-
 }
 
 static void
 bus_ready_callback (GObject      *source_object,
                     GAsyncResult *result,
-                    gpointer      gtkswitch)
+                    CallbackData *callback_data)
 {
   GDBusConnection *connection;
   GError *error = NULL;
@@ -114,8 +149,11 @@ bus_ready_callback (GObject      *source_object,
 
   if (!connection)
     {
-      g_warning ("Error getting remote login state: %s", error->message);
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_warning ("Error getting remote login state: %s", error->message);
       g_clear_error (&error);
+      g_free (callback_data);
+
       return;
     }
 
@@ -128,48 +166,61 @@ bus_ready_callback (GObject      *source_object,
                           (GVariantType*) "(o)",
                           G_DBUS_CALL_FLAGS_NONE,
                           -1,
-                          NULL,
-                          path_ready_callback,
-                          gtkswitch);
+                          callback_data->cancellable,
+                          (GAsyncReadyCallback) path_ready_callback,
+                          callback_data);
 }
 
-
 void
-cc_remote_login_get_enabled (GtkSwitch *gtkswitch)
+cc_remote_login_get_enabled (GCancellable *cancellable,
+                             GtkSwitch    *gtkswitch,
+                             GtkWidget    *button)
 {
+  CallbackData *callback_data;
+
   /* disable the switch until the current state is known */
   gtk_widget_set_sensitive (GTK_WIDGET (gtkswitch), FALSE);
 
-  g_bus_get (G_BUS_TYPE_SYSTEM, NULL, bus_ready_callback, gtkswitch);
-}
+  callback_data = g_new (CallbackData, 1);
+  callback_data->gtkswitch = gtkswitch;
+  callback_data->button = button;
+  callback_data->cancellable = cancellable;
 
+  g_bus_get (G_BUS_TYPE_SYSTEM, callback_data->cancellable,
+             (GAsyncReadyCallback) bus_ready_callback, callback_data);
+}
 
 static gint std_err;
 
 static void
 child_watch_func (GPid     pid,
                   gint     status,
-                  gpointer gtkswitch)
+                  gpointer user_data)
 {
+  CallbackData *callback_data = user_data;
   if (status != 0)
     {
       g_warning ("Error enabling or disabling remote login service");
 
       /* make sure the switch reflects the current status */
-      cc_remote_login_get_enabled (GTK_SWITCH (gtkswitch));
+      cc_remote_login_get_enabled (callback_data->cancellable, callback_data->gtkswitch, NULL);
     }
   g_spawn_close_pid (pid);
 
-  gtk_widget_set_sensitive (GTK_WIDGET (gtkswitch), TRUE);
+  gtk_widget_set_sensitive (GTK_WIDGET (callback_data->gtkswitch), TRUE);
+
+  g_free (user_data);
 }
 
 void
-cc_remote_login_set_enabled (GtkSwitch *gtkswitch)
+cc_remote_login_set_enabled (GCancellable *cancellable,
+                             GtkSwitch    *gtkswitch)
 {
   gchar *command[] = { "pkexec", LIBEXECDIR "/cc-remote-login-helper", NULL,
       NULL };
   GError *error = NULL;
   GPid pid;
+  CallbackData *callback_data;
 
 
   if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (gtkswitch), "set-from-dbus")) == 1)
@@ -189,7 +240,11 @@ cc_remote_login_set_enabled (GtkSwitch *gtkswitch)
                             G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL,
                             NULL, &pid, NULL, NULL, &std_err, &error);
 
-  g_child_watch_add (pid, child_watch_func, gtkswitch);
+  callback_data = g_new0 (CallbackData, 1);
+  callback_data->gtkswitch = gtkswitch;
+  callback_data->cancellable = cancellable;
+
+  g_child_watch_add (pid, child_watch_func, callback_data);
 
   if (error)
     {
diff --git a/panels/sharing/cc-remote-login.h b/panels/sharing/cc-remote-login.h
index 64bcf2e..91879b7 100644
--- a/panels/sharing/cc-remote-login.h
+++ b/panels/sharing/cc-remote-login.h
@@ -24,7 +24,10 @@
 
 #include <gtk/gtk.h>
 
-void cc_remote_login_get_enabled (GtkSwitch *gtkswitch);
-void cc_remote_login_set_enabled (GtkSwitch *gtkswitch);
+void cc_remote_login_get_enabled (GCancellable *cancellable,
+                                  GtkSwitch    *gtkswitch,
+                                  GtkWidget    *button);
+void cc_remote_login_set_enabled (GCancellable *cancellable,
+                                  GtkSwitch    *gtkswitch);
 
 #endif /* __CC_REMOTE_LOGIN_H__ **/
diff --git a/panels/sharing/cc-sharing-panel.c b/panels/sharing/cc-sharing-panel.c
index 1ad4d42..e99cfa1 100644
--- a/panels/sharing/cc-sharing-panel.c
+++ b/panels/sharing/cc-sharing-panel.c
@@ -70,6 +70,7 @@ struct _CcSharingPanelPrivate
   GtkWidget *media_sharing_dialog;
   GtkWidget *personal_file_sharing_dialog;
   GtkWidget *remote_login_dialog;
+  GCancellable *remote_login_cancellable;
   GtkWidget *screen_sharing_dialog;
 
 #ifdef HAVE_BLUETOOTH
@@ -106,10 +107,16 @@ cc_sharing_panel_dispose (GObject *object)
       priv->personal_file_sharing_dialog = NULL;
     }
 
+  if (priv->remote_login_cancellable)
+    {
+      g_cancellable_cancel (priv->remote_login_cancellable);
+      g_clear_object (&priv->remote_login_cancellable);
+    }
+
   if (priv->remote_login_dialog)
     {
-    gtk_widget_destroy (priv->remote_login_dialog);
-    priv->remote_login_dialog = NULL;
+      gtk_widget_destroy (priv->remote_login_dialog);
+      priv->remote_login_dialog = NULL;
     }
 
   if (priv->screen_sharing_dialog)
@@ -633,9 +640,10 @@ cc_sharing_panel_setup_personal_file_sharing_dialog (CcSharingPanel *self)
 
 static void
 remote_login_switch_activate (GtkSwitch      *remote_login_switch,
+                              GParamSpec     *pspec,
                               CcSharingPanel *self)
 {
-  cc_remote_login_set_enabled (remote_login_switch);
+  cc_remote_login_set_enabled (self->priv->remote_login_cancellable, remote_login_switch);
 }
 
 static void
@@ -652,8 +660,9 @@ cc_sharing_panel_setup_remote_login_dialog (CcSharingPanel *self)
                     G_CALLBACK (remote_login_switch_activate), self);
   gtk_widget_set_sensitive (WID ("remote-login-switch"), FALSE);
 
-  cc_remote_login_get_enabled (GTK_SWITCH (WID ("remote-login-switch")));
-
+  cc_remote_login_get_enabled (self->priv->remote_login_cancellable,
+                               GTK_SWITCH (WID ("remote-login-switch")),
+                               WID ("remote-login-button"));
 }
 
 static gboolean
@@ -773,6 +782,7 @@ cc_sharing_panel_init (CcSharingPanel *self)
   priv->media_sharing_dialog = WID ("media-sharing-dialog");
   priv->personal_file_sharing_dialog = WID ("personal-file-sharing-dialog");
   priv->remote_login_dialog = WID ("remote-login-dialog");
+  priv->remote_login_cancellable = g_cancellable_new ();
   priv->screen_sharing_dialog = WID ("screen-sharing-dialog");
 
   g_signal_connect (priv->bluetooth_sharing_dialog, "response",



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