[gnome-system-tools] Move from PolkitLockButton to users-dialog's UmLockButton



commit 94f5d9428d896707a5ba9c05203cd4e3848d470d
Author: Milan Bouchet-Valat <nalimilan club fr>
Date:   Sat Dec 11 17:04:33 2010 +0100

    Move from PolkitLockButton to users-dialog's UmLockButton
    
    polkit-gtk isn't compatible with GTK+ 3, so copy a lock button
    implementation based on GPermission to the source tree.
    
    This is a temporary solution, until GtkLockButton is added to GTK itself.

 configure.in                 |   25 +-
 src/common/Makefile.am       |    5 +
 src/common/gst-dialog.c      |   59 +++--
 src/common/gst-tool.c        |   16 +-
 src/common/gst-tool.h        |    2 +-
 src/common/um-lockbutton.c   |  638 ++++++++++++++++++++++++++++++++++++++++++
 src/common/um-lockbutton.h   |   68 +++++
 src/services/services-tool.c |    2 +-
 src/users/users-tool.c       |    2 +-
 9 files changed, 772 insertions(+), 45 deletions(-)
---
diff --git a/configure.in b/configure.in
index 68f53a2..8719007 100644
--- a/configure.in
+++ b/configure.in
@@ -35,8 +35,7 @@ LIBOOBS_REQUIRED=2.31.91
 GTK_REQUIRED=2.91
 GLIB_REQUIRED=2.25.3
 DBUS_REQUIRED=0.32
-POLICYKIT_GTK_REQUIRED=0.92
-
+POLKIT_REQUIRED=0.97
 
 dnl =====================================================
 dnl Cracklib checking
@@ -94,20 +93,20 @@ DBUS_CFLAGS="$DBUS_CFLAGS -DDBUS_API_SUBJECT_TO_CHANGE"
 AC_SUBST(DBUS_LIBS)
 AC_SUBST(DBUS_CFLAGS)
 
-dnl PolicyKit-GTK support
+dnl PolicyKit support
 
 have_polkit=no
-AC_ARG_ENABLE(polkit-gtk,
-	AS_HELP_STRING([--enable-polkit-gtk],[Enable PolicyKit support using polkit-gtk (default yes)]),
-	[], [enable_polkit_gtk=yes])
+AC_ARG_ENABLE(polkit,
+	AS_HELP_STRING([--enable-polkit],[Enable PolicyKit support (default yes)]),
+	[], [enable_polkit=yes])
 
-if test "x$enable_polkit_gtk" = "xyes"; then
-	PKG_CHECK_MODULES(POLKIT_GTK,[polkit-gtk-1 >= $POLICYKIT_GTK_REQUIRED], have_polkit=yes)
+if test "x$enable_polkit" = "xyes"; then
+	PKG_CHECK_MODULES(POLKIT,[polkit-gobject-1 >= $POLKIT_REQUIRED], have_polkit=yes)
 
 	if test "$have_polkit" = "yes"; then
-		AC_DEFINE(HAVE_POLKIT, [1], [whether polkit-gtk was found])
+		AC_DEFINE(HAVE_POLKIT, [1], [whether polkit was found])
 	else
-		AC_MSG_ERROR([PolicyKit support requested but polkit-gtk not found])
+		AC_MSG_ERROR([PolicyKit support requested but policykit-1 not found])
 	fi
 fi
 
@@ -230,8 +229,8 @@ fi
 dnl GST_DEPRECATED_FLAGS="-DG_DISABLE_DEPRECATED -DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED -DGDK_PIXBUF_DISABLE_DEPRECATED -DGNOME_DISABLE_DEPRECATED"
 GST_DEPRECATED_FLAGS=""
 
-GST_LIBS="$GST_DEPENDS_LIBS $POLKIT_GTK_LIBS"
-GST_CFLAGS="$GST_DEPENDS_CFLAGS $POLKIT_GTK_CFLAGS $GST_DEPRECATED_FLAGS"
+GST_LIBS="$GST_DEPENDS_LIBS $POLKIT_LIBS"
+GST_CFLAGS="$GST_DEPENDS_CFLAGS $POLKIT_CFLAGS $GST_DEPRECATED_FLAGS"
 
 GST_TOOL_LIBS="\$(top_builddir)/src/common/libsetuptool.a $GST_LIBS"
 GST_TOOL_CFLAGS="-I\$(top_srcdir)/src/common $GST_CFLAGS"
@@ -345,7 +344,7 @@ Configuration (GST):
 	Time tool:         ${enable_time}
 	Shares tool:       ${enable_shares}
 	Nautilus support:  ${enable_nautilus}
-	Use PolKit-GTK:    ${have_polkit}
+	Use PolicyKit:     ${have_polkit}
 
 	Compiler:          ${CC}
 "
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index 6698b4d..2364189 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -18,6 +18,11 @@ libsetuptool_a_SOURCES = \
 	gst-service-role.c	gst-service-role.h \
 	gst.h
 
+if HAVE_POLKIT
+libsetuptool_a_SOURCES += \
+	um-lockbutton.c		um-lockbutton.h
+endif
+
 EXTRA_DIST = CommonMakefile $(headers)
 
 -include $(top_srcdir)/git.mk
diff --git a/src/common/gst-dialog.c b/src/common/gst-dialog.c
index 7198da7..8a95558 100644
--- a/src/common/gst-dialog.c
+++ b/src/common/gst-dialog.c
@@ -30,7 +30,8 @@
 #include "gst-dialog.h"
 
 #ifdef HAVE_POLKIT
-#include <polkitgtk/polkitgtk.h>
+#include <polkit/polkit.h>
+#include "um-lockbutton.h"
 #endif
 
 #define GST_DIALOG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GST_TYPE_DIALOG, GstDialogPrivate))
@@ -43,12 +44,13 @@ struct _GstDialogPrivate {
 
 	gchar   *title;
 	gchar   *widget_name;
-	gboolean lock_button;
+	gboolean show_lock_button;
 
 	GtkBuilder *builder;
 	GtkWidget  *child;
 #ifdef HAVE_POLKIT
-	GtkWidget  *polkit_button;
+	GPermission *permission;
+	GtkWidget   *lock_button;
 #endif
 	GSList     *policy_widgets;
 
@@ -96,7 +98,7 @@ enum {
 	PROP_TOOL,
 	PROP_WIDGET_NAME,
 	PROP_TITLE,
-	PROP_LOCK_BUTTON
+	PROP_SHOW_LOCK_BUTTON
 };
 
 enum {
@@ -148,10 +150,10 @@ gst_dialog_class_init (GstDialogClass *class)
 							      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 
 	g_object_class_install_property (object_class,
-					 PROP_LOCK_BUTTON,
-					 g_param_spec_boolean ("lock_button",
-					                       "Lock button",
-					                       "Show PolkitLockButton",
+					 PROP_SHOW_LOCK_BUTTON,
+					 g_param_spec_boolean ("show_lock_button",
+					                       "Show Lock button",
+					                       "Whether to show lock button",
 					                       TRUE,
 					                       G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 
@@ -235,18 +237,35 @@ gst_dialog_constructor (GType                  type,
 	if (priv->tool) {
 		const gchar *action;
 		GtkWidget *action_area;
+		GError *error = NULL;
 
 		/* Some tools don't use the lock button at all */
-		if (priv->lock_button) {
+		if (priv->show_lock_button) {
 			action = oobs_session_get_authentication_action (priv->tool->session);
-			priv->polkit_button = polkit_lock_button_new (action);
+			priv->permission = polkit_permission_new_sync (action, NULL, NULL, &error);
+
+			if (!priv->permission) {
+				GtkWidget *message_dialog;
+
+				message_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
+				                                         GTK_DIALOG_MODAL,
+				                                         GTK_MESSAGE_ERROR,
+				                                         GTK_BUTTONS_CLOSE,
+				                                         _("Error accessing system permissions: %s"),
+				                                         error->message);
+				gtk_dialog_run (GTK_DIALOG (message_dialog));
+
+				exit (-1);
+			}
+
+			priv->lock_button = um_lock_button_new (priv->permission);
 
 			action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
-			gtk_box_pack_start (GTK_BOX (action_area), priv->polkit_button, TRUE, TRUE, 0);
-			gtk_widget_show (priv->polkit_button);
+			gtk_box_pack_start (GTK_BOX (action_area), priv->lock_button, TRUE, TRUE, 0);
+			gtk_widget_show (priv->lock_button);
 
-			g_signal_connect_swapped (priv->polkit_button, "changed",
-		                          G_CALLBACK (gst_dialog_lock_changed), dialog);
+			g_signal_connect_swapped (priv->permission, "notify",
+		                                  G_CALLBACK (gst_dialog_lock_changed), dialog);
 		}
 	}
 #endif
@@ -282,8 +301,8 @@ gst_dialog_set_property (GObject      *object,
 	case PROP_TITLE:
 		priv->title = g_value_dup_string (value);
 		break;
-	case PROP_LOCK_BUTTON:
-		priv->lock_button = g_value_get_boolean (value);
+	case PROP_SHOW_LOCK_BUTTON:
+		priv->show_lock_button = g_value_get_boolean (value);
 		break;
 	}
 }
@@ -353,13 +372,13 @@ gst_dialog_delete_event (GtkWidget   *widget,
 }
 
 GstDialog*
-gst_dialog_new (GstTool *tool, const char *widget, const char *title, gboolean lock_button)
+gst_dialog_new (GstTool *tool, const char *widget, const char *title, gboolean show_lock_button)
 {
 	return g_object_new (GST_TYPE_DIALOG,
 			     "tool", tool,
 			     "widget-name", widget,
 			     "title", title,
-	                     "lock-button", lock_button,
+	                     "show-lock-button", show_lock_button,
 			     NULL);
 }
 
@@ -651,9 +670,7 @@ gst_dialog_is_authenticated (GstDialog *dialog)
 
 	priv = GST_DIALOG_GET_PRIVATE (dialog);
 
-	return (polkit_lock_button_get_is_authorized (POLKIT_LOCK_BUTTON (priv->polkit_button)) ||
-		(polkit_lock_button_get_can_obtain (POLKIT_LOCK_BUTTON (priv->polkit_button)) &&
-		!polkit_lock_button_get_is_visible (POLKIT_LOCK_BUTTON (priv->polkit_button))));
+	return g_permission_get_allowed (priv->permission);
 #else
 	return TRUE;
 #endif
diff --git a/src/common/gst-tool.c b/src/common/gst-tool.c
index a4b55a3..c8ce304 100644
--- a/src/common/gst-tool.c
+++ b/src/common/gst-tool.c
@@ -64,7 +64,7 @@ enum {
 	PROP_NAME,
 	PROP_TITLE,
 	PROP_ICON,
-	PROP_LOCK_BUTTON
+	PROP_SHOW_LOCK_BUTTON
 };
 
 typedef struct _GstAsyncData {
@@ -111,10 +111,10 @@ gst_tool_class_init (GstToolClass *class)
 							      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 
 	g_object_class_install_property (object_class,
-					 PROP_LOCK_BUTTON,
-					 g_param_spec_boolean ("lock_button",
-					                       "Lock button",
-					                       "Show PolkitLockButton",
+					 PROP_SHOW_LOCK_BUTTON,
+					 g_param_spec_boolean ("show_lock_button",
+					                       "Show lock button",
+					                       "Whether to show lock button",
 					                       TRUE,
 					                       G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 }
@@ -237,7 +237,7 @@ gst_tool_constructor (GType                  type,
 		tool->ui_path = g_strdup_printf (INTERFACES_DIR "/%s.ui", tool->name);
 
 		widget_name = g_strdup_printf ("%s_admin", tool->name);
-		tool->main_dialog = gst_dialog_new (tool, widget_name, tool->title, tool->lock_button);
+		tool->main_dialog = gst_dialog_new (tool, widget_name, tool->title, tool->show_lock_button);
 		g_free (widget_name);
 	}
 
@@ -275,8 +275,8 @@ gst_tool_set_property (GObject      *object,
 	case PROP_ICON:
 		tool->icon = g_value_dup_string (value);
 		break;
-	case PROP_LOCK_BUTTON:
-		tool->lock_button = g_value_get_boolean (value);
+	case PROP_SHOW_LOCK_BUTTON:
+		tool->show_lock_button = g_value_get_boolean (value);
 		break;
 	}
 }
diff --git a/src/common/gst-tool.h b/src/common/gst-tool.h
index 3993fbf..d5eb598 100644
--- a/src/common/gst-tool.h
+++ b/src/common/gst-tool.h
@@ -47,7 +47,7 @@ struct _GstTool {
 	gchar *name;
 	gchar *title;
 	gchar *icon;
-	gboolean lock_button;
+	gboolean show_lock_button;
 
 	OobsSession *session;
 	GPtrArray   *objects;
diff --git a/src/common/um-lockbutton.c b/src/common/um-lockbutton.c
new file mode 100644
index 0000000..e56e184
--- /dev/null
+++ b/src/common/um-lockbutton.c
@@ -0,0 +1,638 @@
+/*
+ * Copyright (C) 2010 Red Hat, Inc.
+ * Author: Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "um-lockbutton.h"
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <polkit/polkit.h>
+
+#define P_(s) s
+
+struct _UmLockButtonPrivate
+{
+  GPermission *permission;
+
+  gchar *text_lock;
+  gchar *text_unlock;
+  gchar *text_not_authorized;
+
+  gchar *tooltip_lock;
+  gchar *tooltip_unlock;
+  gchar *tooltip_not_authorized;
+
+  GtkWidget *box;
+  GtkWidget *eventbox;
+  GtkWidget *image;
+  GtkWidget *button;
+  GtkWidget *notebook;
+
+  GtkWidget *label_lock;
+  GtkWidget *label_unlock;
+  GtkWidget *label_not_authorized;
+
+  GCancellable *cancellable;
+
+  gboolean constructed;
+};
+
+enum
+{
+  PROP_0,
+  PROP_PERMISSION,
+  PROP_TEXT_LOCK,
+  PROP_TEXT_UNLOCK,
+  PROP_TEXT_NOT_AUTHORIZED,
+  PROP_TOOLTIP_LOCK,
+  PROP_TOOLTIP_UNLOCK,
+  PROP_TOOLTIP_NOT_AUTHORIZED
+};
+
+enum
+{
+  CHANGED_SIGNAL,
+  LAST_SIGNAL,
+};
+
+static guint signals[LAST_SIGNAL] = {0, };
+
+static void update_state (UmLockButton *button);
+
+static void on_permission_changed (GPermission *permission,
+                                   GParamSpec  *pspec,
+                                   gpointer     user_data);
+
+static void on_clicked (GtkButton *button,
+                        gpointer   user_data);
+
+static void on_button_press (GtkWidget      *widget,
+                             GdkEventButton *event,
+                             gpointer        user_data);
+
+G_DEFINE_TYPE (UmLockButton, um_lock_button, GTK_TYPE_BIN);
+
+static void
+um_lock_button_finalize (GObject *object)
+{
+  UmLockButton *button = UM_LOCK_BUTTON (object);
+  UmLockButtonPrivate *priv = button->priv;
+
+  g_free (priv->text_lock);
+  g_free (priv->text_unlock);
+  g_free (priv->text_not_authorized);
+
+  g_free (priv->tooltip_lock);
+  g_free (priv->tooltip_unlock);
+  g_free (priv->tooltip_not_authorized);
+
+  if (priv->cancellable != NULL)
+    {
+      g_cancellable_cancel (priv->cancellable);
+      g_object_unref (priv->cancellable);
+    }
+
+  g_signal_handlers_disconnect_by_func (priv->permission,
+                                        on_permission_changed,
+                                        button);
+
+  g_object_unref (priv->permission);
+
+  G_OBJECT_CLASS (um_lock_button_parent_class)->finalize (object);
+}
+
+static void
+um_lock_button_get_property (GObject    *object,
+                             guint       property_id,
+                             GValue     *value,
+                             GParamSpec *pspec)
+{
+  UmLockButton *button = UM_LOCK_BUTTON (object);
+  UmLockButtonPrivate *priv = button->priv;
+
+  switch (property_id)
+    {
+    case PROP_PERMISSION:
+      g_value_set_object (value, priv->permission);
+      break;
+
+    case PROP_TEXT_LOCK:
+      g_value_set_string (value, priv->text_lock);
+      break;
+
+    case PROP_TEXT_UNLOCK:
+      g_value_set_string (value, priv->text_unlock);
+      break;
+
+    case PROP_TEXT_NOT_AUTHORIZED:
+      g_value_set_string (value, priv->text_not_authorized);
+      break;
+
+    case PROP_TOOLTIP_LOCK:
+      g_value_set_string (value, priv->tooltip_lock);
+      break;
+
+    case PROP_TOOLTIP_UNLOCK:
+      g_value_set_string (value, priv->tooltip_unlock);
+      break;
+
+    case PROP_TOOLTIP_NOT_AUTHORIZED:
+      g_value_set_string (value, priv->tooltip_not_authorized);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+um_lock_button_set_property (GObject      *object,
+                             guint         property_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+  UmLockButton *button = UM_LOCK_BUTTON (object);
+  UmLockButtonPrivate *priv = button->priv;
+
+  switch (property_id)
+    {
+    case PROP_PERMISSION:
+      priv->permission = g_value_get_object (value);
+      break;
+
+    case PROP_TEXT_LOCK:
+      g_free (priv->text_lock);
+      priv->text_lock = g_value_dup_string (value);
+      break;
+
+    case PROP_TEXT_UNLOCK:
+      g_free (priv->text_unlock);
+      priv->text_unlock = g_value_dup_string (value);
+      break;
+
+    case PROP_TEXT_NOT_AUTHORIZED:
+      g_free (priv->text_not_authorized);
+      priv->text_not_authorized = g_value_dup_string (value);
+      break;
+
+    case PROP_TOOLTIP_LOCK:
+      g_free (priv->tooltip_lock);
+      priv->tooltip_lock = g_value_dup_string (value);
+      break;
+
+    case PROP_TOOLTIP_UNLOCK:
+      g_free (priv->tooltip_unlock);
+      priv->tooltip_unlock = g_value_dup_string (value);
+      break;
+
+    case PROP_TOOLTIP_NOT_AUTHORIZED:
+      g_free (priv->tooltip_not_authorized);
+      priv->tooltip_not_authorized = g_value_dup_string (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+
+  if (priv->constructed)
+    update_state (button);
+}
+
+static void
+um_lock_button_init (UmLockButton *button)
+{
+  button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
+                                              UM_TYPE_LOCK_BUTTON,
+                                              UmLockButtonPrivate);
+}
+
+static void
+um_lock_button_constructed (GObject *object)
+{
+  UmLockButton *button = UM_LOCK_BUTTON (object);
+  UmLockButtonPrivate *priv = button->priv;
+
+  priv->constructed = TRUE;
+
+  g_signal_connect (priv->permission, "notify",
+                    G_CALLBACK (on_permission_changed), button);
+
+  priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
+  gtk_container_add (GTK_CONTAINER (button), priv->box);
+
+  priv->eventbox = gtk_event_box_new ();
+  gtk_event_box_set_visible_window (GTK_EVENT_BOX (priv->eventbox), FALSE);
+  gtk_container_add (GTK_CONTAINER (priv->box), priv->eventbox);
+  gtk_widget_show (priv->eventbox);
+
+  priv->image = gtk_image_new (); /* image is set in update_state() */
+  gtk_container_add (GTK_CONTAINER (priv->eventbox), priv->image);
+  gtk_widget_show (priv->image);
+
+  priv->notebook = gtk_notebook_new ();
+  gtk_notebook_set_show_tabs (GTK_NOTEBOOK (priv->notebook), FALSE);
+  gtk_notebook_set_show_border (GTK_NOTEBOOK (priv->notebook), FALSE);
+  gtk_widget_show (priv->notebook);
+
+  priv->button = gtk_button_new ();
+  gtk_container_add (GTK_CONTAINER (priv->button), priv->notebook);
+  gtk_widget_show (priv->button);
+
+  priv->label_lock = gtk_label_new (""); /* text is set in update_state */
+  gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->label_lock, NULL);
+  gtk_widget_show (priv->label_lock);
+
+  priv->label_unlock = gtk_label_new ("");
+  gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->label_unlock, NULL);
+  gtk_widget_show (priv->label_unlock);
+
+  priv->label_not_authorized = gtk_label_new ("");
+  gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), priv->label_not_authorized, NULL);
+  gtk_widget_show (priv->label_not_authorized);
+
+  gtk_box_pack_start (GTK_BOX (priv->box), priv->button, FALSE, FALSE, 0);
+  gtk_widget_show (priv->button);
+
+  g_signal_connect (priv->eventbox, "button-press-event",
+                    G_CALLBACK (on_button_press), button);
+  g_signal_connect (priv->button, "clicked",
+                    G_CALLBACK (on_clicked), button);
+
+  gtk_widget_set_no_show_all (priv->box, TRUE);
+
+  update_state (button);
+
+  if (G_OBJECT_CLASS (um_lock_button_parent_class)->constructed != NULL)
+    G_OBJECT_CLASS (um_lock_button_parent_class)->constructed (object);
+}
+
+static void
+um_lock_button_get_preferred_width (GtkWidget *widget,
+                                    gint      *minimum,
+                                    gint      *natural)
+{
+  UmLockButtonPrivate *priv = UM_LOCK_BUTTON (widget)->priv;
+
+  gtk_widget_get_preferred_width (priv->box, minimum, natural);
+}
+
+static void
+um_lock_button_get_preferred_height (GtkWidget *widget,
+                                     gint      *minimum,
+                                     gint      *natural)
+{
+  UmLockButtonPrivate *priv = UM_LOCK_BUTTON (widget)->priv;
+
+  gtk_widget_get_preferred_height (priv->box, minimum, natural);
+}
+
+static void
+um_lock_button_size_allocate (GtkWidget     *widget,
+                              GtkAllocation *allocation)
+{
+  UmLockButtonPrivate *priv = UM_LOCK_BUTTON (widget)->priv;
+  GtkRequisition requisition;
+  GtkAllocation child_allocation;
+
+  gtk_widget_set_allocation (widget, allocation);
+  gtk_widget_get_preferred_size (priv->box, &requisition, NULL);
+  child_allocation.x = allocation->x;
+  child_allocation.y = allocation->y;
+  child_allocation.width = requisition.width;
+  child_allocation.height = requisition.height;
+  gtk_widget_size_allocate (priv->box, &child_allocation);
+}
+
+static void
+um_lock_button_class_init (UmLockButtonClass *klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  gobject_class->finalize     = um_lock_button_finalize;
+  gobject_class->get_property = um_lock_button_get_property;
+  gobject_class->set_property = um_lock_button_set_property;
+  gobject_class->constructed  = um_lock_button_constructed;
+
+  widget_class->get_preferred_width = um_lock_button_get_preferred_width;
+  widget_class->get_preferred_height = um_lock_button_get_preferred_height;
+  widget_class->size_allocate = um_lock_button_size_allocate;
+
+  g_type_class_add_private (klass, sizeof (UmLockButtonPrivate));
+
+  g_object_class_install_property (gobject_class, PROP_PERMISSION,
+    g_param_spec_object ("permission",
+                         P_("Permission"),
+                         P_("The GPermission object controlling this button"),
+                         G_TYPE_PERMISSION,
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT_ONLY |
+                         G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_TEXT_LOCK,
+    g_param_spec_string ("text-lock",
+                         P_("Lock Text"),
+                         P_("The text to display when prompting the user to lock"),
+                         _("Lock"),
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT |
+                         G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_TEXT_UNLOCK,
+    g_param_spec_string ("text-unlock",
+                         P_("Unlock Text"),
+                         P_("The text to display when prompting the user to unlock"),
+                         _("Unlock"),
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT |
+                         G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_TEXT_NOT_AUTHORIZED,
+    g_param_spec_string ("text-not-authorized",
+                         P_("Not Authorized Text"),
+                         P_("The text to display when prompting the user cannot obtain authorization"),
+                         _("Locked"),
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT |
+                         G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_TOOLTIP_LOCK,
+    g_param_spec_string ("tooltip-lock",
+                         P_("Lock Tooltip"),
+                         P_("The tooltip to display when prompting the user to lock"),
+                         _("Dialog is unlocked.\nClick to prevent further changes"),
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT |
+                         G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_TOOLTIP_UNLOCK,
+    g_param_spec_string ("tooltip-unlock",
+                         P_("Unlock Tooltip"),
+                         P_("The tooltip to display when prompting the user to unlock"),
+                         _("Dialog is locked.\nClick to make changes"),
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT |
+                         G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_TOOLTIP_NOT_AUTHORIZED,
+    g_param_spec_string ("tooltip-not-authorized",
+                         P_("Not Authorized Tooltip"),
+                         P_("The tooltip to display when prompting the user cannot obtain authorization"),
+                         _("System policy prevents changes.\nContact your system administrator"),
+                         G_PARAM_READWRITE |
+                         G_PARAM_CONSTRUCT |
+                         G_PARAM_STATIC_STRINGS));
+}
+
+/**
+ * um_lock_button_new:
+ * @permission: a #GPermission
+ *
+ * Creates a new lock button which reflects the @permission.
+ *
+ * Returns: a new #UmLockButton
+ *
+ * Since: 3.0
+ */
+GtkWidget *
+um_lock_button_new (GPermission *permission)
+{
+  g_return_val_if_fail (permission != NULL, NULL);
+
+  return GTK_WIDGET (g_object_new (UM_TYPE_LOCK_BUTTON,
+                                   "permission", permission,
+                                   NULL));
+}
+
+static void
+update_state (UmLockButton *button)
+{
+  UmLockButtonPrivate *priv = button->priv;
+  gint page;
+  const gchar *tooltip;
+  gboolean sensitive;
+  gboolean visible;
+  GIcon *icon;
+
+  visible = TRUE;
+  sensitive = TRUE;
+
+  gtk_label_set_text (GTK_LABEL (priv->label_lock), priv->text_lock);
+  gtk_label_set_text (GTK_LABEL (priv->label_unlock), priv->text_unlock);
+  gtk_label_set_text (GTK_LABEL (priv->label_not_authorized), priv->text_not_authorized);
+
+  if (g_permission_get_allowed (priv->permission))
+    {
+      if (g_permission_get_can_release (priv->permission))
+        {
+          page = 0;
+          tooltip = priv->tooltip_lock;
+          sensitive = TRUE;
+        }
+      else
+        {
+          page = 0;
+          tooltip = "";
+          visible = FALSE;
+        }
+    }
+  else
+    {
+      if (g_permission_get_can_acquire (priv->permission))
+        {
+          page = 1;
+          tooltip = button->priv->tooltip_unlock;
+          sensitive = TRUE;
+        }
+      else
+        {
+          page = 2;
+          tooltip = button->priv->tooltip_not_authorized;
+          sensitive = FALSE;
+        }
+    }
+
+  if (g_permission_get_allowed (priv->permission))
+    {
+      gchar *names[3];
+
+      names[0] = "changes-allow-symbolic";
+      names[1] = "changes-allow";
+      names[2] = NULL;
+      icon = g_themed_icon_new_from_names (names, -1);
+    }
+  else
+    {
+      gchar *names[3];
+
+      names[0] = "changes-prevent-symbolic";
+      names[1] = "changes-prevent";
+      names[2] = NULL;
+      icon = g_themed_icon_new_from_names (names, -1);
+    }
+
+  gtk_image_set_from_gicon (GTK_IMAGE (priv->image), icon, GTK_ICON_SIZE_BUTTON);
+  g_object_unref (icon);
+
+  gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), page);
+  gtk_widget_set_tooltip_markup (priv->box, tooltip);
+
+  gtk_widget_set_sensitive (priv->box, sensitive);
+
+  if (visible)
+    gtk_widget_show (priv->box);
+  else
+    gtk_widget_hide (priv->box);
+}
+
+static void
+on_permission_changed (GPermission *permission,
+                       GParamSpec  *pspec,
+                       gpointer     user_data)
+{
+  UmLockButton *button = UM_LOCK_BUTTON (user_data);
+
+  update_state (button);
+}
+
+static void
+acquire_cb (GObject      *source,
+            GAsyncResult *result,
+            gpointer      user_data)
+{
+  UmLockButton *button = UM_LOCK_BUTTON (user_data);
+  UmLockButtonPrivate *priv = button->priv;
+  GError *error;
+  gboolean res;
+
+  error = NULL;
+  res = g_permission_acquire_finish (priv->permission, result, &error);
+
+  if (error)
+    {
+      g_warning ("Error acquiring permission: %s", error->message);
+      g_error_free (error);
+    }
+
+  g_object_unref (priv->cancellable);
+  priv->cancellable = NULL;
+
+  update_state (button);
+}
+
+static void
+release_cb (GObject      *source,
+            GAsyncResult *result,
+            gpointer      user_data)
+{
+  UmLockButton *button = UM_LOCK_BUTTON (user_data);
+  UmLockButtonPrivate *priv = button->priv;
+  GError *error;
+  gboolean res;
+
+  error = NULL;
+  res = g_permission_release_finish (priv->permission, result, &error);
+
+  if (error)
+    {
+      g_warning ("Error releasing permission: %s", error->message);
+      g_error_free (error);
+    }
+
+  g_object_unref (priv->cancellable);
+  priv->cancellable = NULL;
+
+  update_state (button);
+}
+
+static void
+handle_click (UmLockButton *button)
+{
+  UmLockButtonPrivate *priv = button->priv;
+
+  if (!g_permission_get_allowed (priv->permission) &&
+       g_permission_get_can_acquire (priv->permission))
+    {
+      /* if we already have a pending interactive check, then do nothing */
+      if (priv->cancellable != NULL)
+        goto out;
+
+      priv->cancellable = g_cancellable_new ();
+
+      g_permission_acquire_async (priv->permission,
+                                  priv->cancellable,
+                                  acquire_cb,
+                                  button);
+    }
+  else if (g_permission_get_allowed (priv->permission) &&
+           g_permission_get_can_release (priv->permission))
+    {
+      /* if we already have a pending interactive check, then do nothing */
+      if (priv->cancellable != NULL)
+        goto out;
+
+      priv->cancellable = g_cancellable_new ();
+
+      g_permission_release_async (priv->permission,
+                                  priv->cancellable,
+                                  release_cb,
+                                  button);
+    }
+
+ out: ;
+}
+
+static void
+on_clicked (GtkButton *_button,
+            gpointer   user_data)
+
+{
+  handle_click (UM_LOCK_BUTTON (user_data));
+}
+
+static void
+on_button_press (GtkWidget      *widget,
+                 GdkEventButton *event,
+                 gpointer        user_data)
+{
+  handle_click (UM_LOCK_BUTTON (user_data));
+}
+
+/**
+ * um_lock_button_get_permission:
+ * @button: a #UmLockButton
+ *
+ * Obtains the #GPermission object that controls @button.
+ *
+ * Returns: the #GPermission of @button
+ *
+ * Since: 3.0
+ */
+GPermission *
+um_lock_button_get_permission (UmLockButton *button)
+{
+  g_return_val_if_fail (UM_IS_LOCK_BUTTON (button), NULL);
+
+  return button->priv->permission;
+}
+
diff --git a/src/common/um-lockbutton.h b/src/common/um-lockbutton.h
new file mode 100644
index 0000000..3b5c6e9
--- /dev/null
+++ b/src/common/um-lockbutton.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2010 Red Hat, Inc.
+ * Author: Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __UM_LOCK_BUTTON_H__
+#define __UM_LOCK_BUTTON_H__
+
+#include <gtk/gtk.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define UM_TYPE_LOCK_BUTTON         (um_lock_button_get_type ())
+#define UM_LOCK_BUTTON(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), UM_TYPE_LOCK_BUTTON, UmLockButton))
+#define UM_LOCK_BUTTON_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), UM_LOCK_BUTTON,  UmLockButtonClass))
+#define UM_IS_LOCK_BUTTON(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), UM_TYPE_LOCK_BUTTON))
+#define UM_IS_LOCK_BUTTON_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), UM_TYPE_LOCK_BUTTON))
+#define UM_LOCK_BUTTON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), UM_TYPE_LOCK_BUTTON, UmLockButtonClass))
+
+typedef struct _UmLockButton        UmLockButton;
+typedef struct _UmLockButtonClass   UmLockButtonClass;
+typedef struct _UmLockButtonPrivate UmLockButtonPrivate;
+
+struct _UmLockButton
+{
+  GtkBin parent;
+
+  UmLockButtonPrivate *priv;
+};
+
+struct _UmLockButtonClass
+{
+  GtkBinClass parent_class;
+
+  void (*reserved0) (void);
+  void (*reserved1) (void);
+  void (*reserved2) (void);
+  void (*reserved3) (void);
+  void (*reserved4) (void);
+  void (*reserved5) (void);
+  void (*reserved6) (void);
+  void (*reserved7) (void);
+};
+
+GType        um_lock_button_get_type       (void) G_GNUC_CONST;
+GtkWidget   *um_lock_button_new            (GPermission   *permission);
+GPermission *um_lock_button_get_permission (UmLockButton *button);
+
+
+G_END_DECLS
+
+#endif  /* __UM_LOCK_BUTTON_H__ */
diff --git a/src/services/services-tool.c b/src/services/services-tool.c
index 51b5ac2..5071ec2 100644
--- a/src/services/services-tool.c
+++ b/src/services/services-tool.c
@@ -105,6 +105,6 @@ gst_services_tool_new (void)
 			     "name", "services",
 			     "title", _("Services Settings"),
 			     "icon", "network-server",
-	                     "lock-button", FALSE,
+	                     "show-lock-button", FALSE,
 			     NULL);
 }
diff --git a/src/users/users-tool.c b/src/users/users-tool.c
index 59b1276..14a01ba 100644
--- a/src/users/users-tool.c
+++ b/src/users/users-tool.c
@@ -287,6 +287,6 @@ gst_users_tool_new (void)
 			     "name", "users",
 			     "title", _("Users Settings"),
 			     "icon", "config-users",
-	                     "lock-button", FALSE,
+	                     "show-lock-button", FALSE,
 			     NULL);
 }



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