[gnome-packagekit] Port all the code to using PolicyKit1 rather than PolicyKit



commit 2a8b216782c8b4aa3a8d922ffc205366085b8018
Author: Richard Hughes <richard hughsie com>
Date:   Mon Jul 6 16:28:08 2009 +0100

    Port all the code to using PolicyKit1 rather than PolicyKit

 configure.ac          |    6 --
 src/Makefile.am       |    2 -
 src/gpk-application.c |    1 -
 src/gpk-common.c      |    1 -
 src/gpk-consolekit.c  |  135 +-----------------------------------------
 src/gpk-dbus-task.c   |    1 -
 src/gpk-dbus.c        |  157 +++++++++++++++++++++++++++++++++++++------------
 src/gpk-watch.c       |   49 +++++----------
 8 files changed, 140 insertions(+), 212 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 4e1a1e6..a94365e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -88,7 +88,6 @@ DBUS_REQUIRED=1.1.2
 GCONF_REQUIRED=0.22
 DBUS_GLIB_REQUIRED=0.73
 LIBNOTIFY_REQUIRED=0.4.3
-POLKIT_GNOME_REQUIRED=0.8
 UNIQUE_REQUIRED=1.0.0
 GNOME_MENUS_REQUIRED=2.24.1
 GIO_REQUIRED=2.18.0
@@ -105,7 +104,6 @@ AC_SUBST(GTK_REQUIRED)
 AC_SUBST(DBUS_REQUIRED)
 AC_SUBST(DBUS_GLIB_REQUIRED)
 AC_SUBST(LIBNOTIFY_REQUIRED)
-AC_SUBST(POLKIT_GNOME_REQUIRED)
 AC_SUBST(UNIQUE_REQUIRED)
 AC_SUBST(GNOME_MENUS_REQUIRED)
 AC_SUBST(GIO_REQUIRED)
@@ -117,10 +115,6 @@ PKG_CHECK_MODULES(PACKAGEKIT, packagekit-glib >= $PACKAGEKIT_REQUIRED)
 AC_SUBST(PACKAGEKIT_CFLAGS)
 AC_SUBST(PACKAGEKIT_LIBS)
 
-PKG_CHECK_MODULES(POLKIT_GNOME, polkit-gnome >= $POLKIT_GNOME_REQUIRED)
-AC_SUBST(POLKIT_GNOME_CFLAGS)
-AC_SUBST(POLKIT_GNOME_LIBS)
-
 PKG_CHECK_MODULES(GLIB, glib-2.0 >= $GLIB_REQUIRED gobject-2.0)
 AC_SUBST(GLIB_CFLAGS)
 AC_SUBST(GLIB_LIBS)
diff --git a/src/Makefile.am b/src/Makefile.am
index 2b8965e..e287290 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -11,7 +11,6 @@ INCLUDES =						\
 	$(LIBNOTIFY_CFLAGS)				\
 	$(PACKAGEKIT_CFLAGS)				\
 	$(UNIQUE_CFLAGS)				\
-	$(POLKIT_GNOME_CFLAGS)				\
 	$(GTK_CFLAGS)					\
 	$(GNOME_MENUS_CFLAGS)				\
 	-DI_KNOW_THE_DEVICEKIT_POWER_API_IS_SUBJECT_TO_CHANGE \
@@ -115,7 +114,6 @@ shared_LIBS =						\
 	$(LIBNOTIFY_LIBS)				\
 	$(PACKAGEKIT_LIBS)				\
 	$(UNIQUE_LIBS)					\
-	$(POLKIT_GNOME_LIBS)				\
 	$(GNOME_MENUS_LIBS)				\
 	$(DEVKIT_LIBS)					\
 	$(CANBERRA_LIBS)				\
diff --git a/src/gpk-application.c b/src/gpk-application.c
index ed36068..25681e1 100644
--- a/src/gpk-application.c
+++ b/src/gpk-application.c
@@ -29,7 +29,6 @@
 #include <gconf/gconf-client.h>
 #include <math.h>
 #include <string.h>
-#include <polkit-gnome/polkit-gnome.h>
 #include <packagekit-glib/packagekit.h>
 
 #include "egg-debug.h"
diff --git a/src/gpk-common.c b/src/gpk-common.c
index c226419..3a331d3 100644
--- a/src/gpk-common.c
+++ b/src/gpk-common.c
@@ -29,7 +29,6 @@
 #include <sys/types.h>
 #include <gtk/gtk.h>
 #include <dbus/dbus-glib.h>
-#include <polkit-gnome/polkit-gnome.h>
 #include <packagekit-glib/packagekit.h>
 #include <locale.h>
 
diff --git a/src/gpk-consolekit.c b/src/gpk-consolekit.c
index 430edac..74231dc 100644
--- a/src/gpk-consolekit.c
+++ b/src/gpk-consolekit.c
@@ -26,7 +26,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <dbus/dbus-glib.h>
-#include <polkit-gnome/polkit-gnome.h>
 
 #include "egg-debug.h"
 #include "gpk-common.h"
@@ -34,101 +33,6 @@
 #include "gpk-error.h"
 
 /**
- * gpk_consolekit_try_system_restart:
- **/
-static gboolean
-gpk_consolekit_try_system_restart (DBusGProxy *proxy, GError **error)
-{
-	return dbus_g_proxy_call_with_timeout (proxy, "Restart", INT_MAX,
-					       error, G_TYPE_INVALID, G_TYPE_INVALID);
-}
-
-/**
- * gpk_consolekit_get_action_from_error:
- **/
-static PolKitAction *
-gpk_consolekit_get_action_from_error (GError *error)
-{
-	PolKitAction *action;
-	gchar *paction, *p;
-
-	action = polkit_action_new ();
-
-	paction = NULL;
-	if (g_str_has_prefix (error->message, "Not privileged for action: ")) {
-		paction = g_strdup (error->message + strlen ("Not privileged for action: "));
-		p = strchr (paction, ' ');
-		if (p)
-			*p = '\0';
-	}
-	polkit_action_set_action_id (action, paction);
-
-	g_free (paction);
-
-	return action;
-}
-
-/**
- * gpk_consolekit_get_result_from_error:
- **/
-static PolKitResult
-gpk_consolekit_get_result_from_error (GError *error)
-{
-	PolKitResult result = POLKIT_RESULT_UNKNOWN;
-	const char *p;
-
-	p = strrchr (error->message, ' ');
-	if (p) {
-		p++;
-		polkit_result_from_string_representation (p, &result);
-	}
-
-	return result;
-}
-
-/**
- * gpk_consolekit_system_restart_auth_cb:
- **/
-static void
-gpk_consolekit_system_restart_auth_cb (PolKitAction *action, gboolean gained_privilege,
-				       GError *error, DBusGProxy *proxy)
-{
-	GError *local_error;
-	gboolean res;
-
-	if (!gained_privilege) {
-		if (error != NULL) {
-			egg_warning ("Not privileged to restart system: %s", error->message);
-		}
-		return;
-	}
-
-        local_error = NULL;
-        res = gpk_consolekit_try_system_restart (proxy, &local_error);
-        if (!res) {
-                egg_warning ("Unable to restart system: %s", local_error->message);
-                g_error_free (local_error);
-        }
-}
-
-/**
- * gpk_consolekit_request_restart_priv:
- **/
-static gboolean
-gpk_consolekit_request_restart_priv (DBusGProxy *proxy, PolKitAction *action, GError **error)
-{
-        guint xid;
-        pid_t pid;
-
-        xid = 0;
-        pid = getpid ();
-
-	return polkit_gnome_auth_obtain (action, xid, pid,
-					 (PolKitGnomeAuthCB) gpk_consolekit_system_restart_auth_cb,
-					 proxy, error);
-}
-
-/**
  * gpk_restart_system:
  *
  * Return value: if we succeeded
@@ -140,9 +44,6 @@ gpk_restart_system (void)
 	DBusGConnection *connection;
 	GError *error = NULL;
 	gboolean ret;
-	PolKitAction *action;
-	PolKitAction *action2;
-	PolKitResult result;
 
 	/* check dbus connections, exit if not valid */
 	connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
@@ -163,39 +64,11 @@ gpk_restart_system (void)
 	}
 
 	/* do the method */
-	ret = gpk_consolekit_try_system_restart (proxy, &error);
+	ret = dbus_g_proxy_call_with_timeout (proxy, "Restart", INT_MAX,
+					      &error, G_TYPE_INVALID, G_TYPE_INVALID);
 	if (!ret) {
-		if (dbus_g_error_has_name (error, "org.freedesktop.ConsoleKit.Manager.NotPrivileged")) {
-			action = gpk_consolekit_get_action_from_error (error);
-			result = gpk_consolekit_get_result_from_error (error);
-
-			if (result == POLKIT_RESULT_NO) {
-				action2 = polkit_action_new ();
-				polkit_action_set_action_id (action2,
-							     "org.freedesktop.consolekit.system.restart-multiple-users");
-				if (polkit_action_equal (action, action2)) {
-					/* TRANSLATORS: could not restart the computer */
-					gpk_error_dialog (_("Failed to restart"),
-							  /* TRANSLATORS: more than one user is using this computer */
-							  _("You are not allowed to restart the computer "
-							    "because multiple users are logged in"), NULL);
-				}
-
-				g_error_free (error);
-
-				polkit_action_unref (action);
-				polkit_action_unref (action2);
-
-				return FALSE;
-			}
-			g_clear_error (&error);
-			ret = gpk_consolekit_request_restart_priv (proxy, action, &error);
-			polkit_action_unref (action);
-		}
-		if (!ret) {
-			egg_warning ("Unable to restart system: %s", error->message);
-			g_error_free (error);
-		}
+		egg_warning ("Unable to restart system: %s", error->message);
+		g_error_free (error);
 	}
 
 	g_object_unref (G_OBJECT (proxy));
diff --git a/src/gpk-dbus-task.c b/src/gpk-dbus-task.c
index 15115d3..e90076f 100644
--- a/src/gpk-dbus-task.c
+++ b/src/gpk-dbus-task.c
@@ -31,7 +31,6 @@
 #include <fontconfig/fontconfig.h>
 #include <gtk/gtk.h>
 #include <gconf/gconf-client.h>
-#include <polkit-gnome/polkit-gnome.h>
 #include <libnotify/notify.h>
 #include <packagekit-glib/packagekit.h>
 
diff --git a/src/gpk-dbus.c b/src/gpk-dbus.c
index 797fa40..3991b23 100644
--- a/src/gpk-dbus.c
+++ b/src/gpk-dbus.c
@@ -38,8 +38,6 @@
 #include <glib/gi18n.h>
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
-#include <polkit/polkit.h>
-#include <polkit-dbus/polkit-dbus.h>
 #include <packagekit-glib/packagekit.h>
 #include <gconf/gconf-client.h>
 
@@ -61,6 +59,8 @@ struct GpkDbusPrivate
 	gint			 timeout_tmp;
 	GpkX11			*x11;
 	GPtrArray		*array;
+	DBusGProxy		*proxy_session_pid;
+	DBusGProxy		*proxy_system_pid;
 };
 
 G_DEFINE_TYPE (GpkDbus, gpk_dbus, G_TYPE_OBJECT)
@@ -103,52 +103,117 @@ gpk_dbus_error_get_type (void)
 }
 
 /**
- * gpk_dbus_get_exec_for_sender:
+ * gpk_dbus_get_pid_session:
  **/
-static gchar *
-gpk_dbus_get_exec_for_sender (const gchar *sender)
+static guint
+gpk_dbus_get_pid_session (GpkDbus *dbus, const gchar *sender)
 {
-	pid_t pid;
-	gchar exec[128];
-	PolKitCaller *caller = NULL;
-	DBusError dbus_error;
-	gboolean ret = FALSE;
-	gint retval;
-	DBusConnection *connection;
-	gchar *sender_exe = NULL;
-
-	/* get a connection */
-	connection = dbus_bus_get (DBUS_BUS_SESSION, NULL);
-	if (connection == NULL)
-		egg_error ("fatal, no system dbus");
-
-	dbus_error_init (&dbus_error);
-	caller = polkit_caller_new_from_dbus_name (connection, sender, &dbus_error);
-	if (caller == NULL) {
-		egg_warning ("cannot get caller from sender %s: %s", sender, dbus_error.message);
-		dbus_error_free (&dbus_error);
+	guint pid = G_MAXUINT;
+	gboolean ret;
+	GError *error = NULL;
+
+	/* get pid from DBus (quite slow) */
+	ret = dbus_g_proxy_call (dbus->priv->proxy_session_pid, "GetConnectionUnixProcessID", &error,
+				 G_TYPE_STRING, sender,
+				 G_TYPE_INVALID,
+				 G_TYPE_UINT, &pid,
+				 G_TYPE_INVALID);
+	if (!ret) {
+		egg_debug ("failed to get pid from session: %s", error->message);
+		g_error_free (error);
 		goto out;
 	}
+out:
+	return pid;
+}
 
-	ret = polkit_caller_get_pid (caller, &pid);
+/**
+ * gpk_dbus_get_pid_system:
+ **/
+static guint
+gpk_dbus_get_pid_system (GpkDbus *dbus, const gchar *sender)
+{
+	guint pid = G_MAXUINT;
+	gboolean ret;
+	GError *error = NULL;
+
+	/* get pid from DBus (quite slow) */
+	ret = dbus_g_proxy_call (dbus->priv->proxy_system_pid, "GetConnectionUnixProcessID", &error,
+				 G_TYPE_STRING, sender,
+				 G_TYPE_INVALID,
+				 G_TYPE_UINT, &pid,
+				 G_TYPE_INVALID);
 	if (!ret) {
-		egg_warning ("cannot get pid from sender %p", sender);
+		egg_debug ("failed to get pid from system: %s", error->message);
+		g_error_free (error);
 		goto out;
 	}
+out:
+	return pid;
+}
+
+/**
+ * gpk_dbus_get_pid:
+ **/
+static guint
+gpk_dbus_get_pid (GpkDbus *dbus, const gchar *sender)
+{
+	guint pid;
+
+	g_return_val_if_fail (PK_IS_DBUS (dbus), G_MAXUINT);
+	g_return_val_if_fail (dbus->priv->proxy_session_pid != NULL, G_MAXUINT);
+	g_return_val_if_fail (dbus->priv->proxy_system_pid != NULL, G_MAXUINT);
+	g_return_val_if_fail (sender != NULL, G_MAXUINT);
 
-	retval = polkit_sysdeps_get_exe_for_pid (pid, exec, 128);
-	if (retval == -1) {
-		egg_warning ("cannot get exec for pid %i", pid);
+	/* check system bus first */
+	pid = gpk_dbus_get_pid_system (dbus, sender);
+	if (pid != G_MAXUINT)
 		goto out;
-	}
 
-	/* make a copy */
-	sender_exe = g_strdup (exec);
+	/* and then session bus */
+	pid = gpk_dbus_get_pid_session (dbus, sender);
+	if (pid != G_MAXUINT)
+		goto out;
 
+	/* should be impossible */
+	egg_warning ("could not find pid!");
 out:
-	if (caller != NULL)
-		polkit_caller_unref (caller);
-	return sender_exe;
+	return pid;
+}
+
+
+/**
+ * gpk_dbus_get_exec_for_sender:
+ **/
+static gchar *
+gpk_dbus_get_exec_for_sender (GpkDbus *dbus, const gchar *sender)
+{
+	gboolean ret;
+	gchar *filename = NULL;
+	gchar *cmdline = NULL;
+	GError *error = NULL;
+	guint pid;
+
+	g_return_val_if_fail (PK_IS_DBUS (dbus), NULL);
+	g_return_val_if_fail (sender != NULL, NULL);
+
+	/* get pid */
+	pid = gpk_dbus_get_pid (dbus, sender);
+	if (pid == G_MAXUINT) {
+		egg_warning ("failed to get PID");
+		goto out;
+	}
+
+	/* get command line from proc */
+	filename = g_strdup_printf ("/proc/%i/cmdline", pid);
+	ret = g_file_get_contents (filename, &cmdline, NULL, &error);
+	if (!ret) {
+		egg_warning ("failed to get cmdline: %s", error->message);
+		g_error_free (error);
+	}
+out:
+	g_free (filename);
+	return cmdline;
 }
 
 /**
@@ -288,8 +353,9 @@ gpk_dbus_create_task (GpkDbus *dbus, guint32 xid, const gchar *interaction, DBus
 
 	/* get the program name and set */
 	sender = dbus_g_method_get_sender (context);
-	exec = gpk_dbus_get_exec_for_sender (sender);
-	gpk_dbus_task_set_exec (task, exec);
+	exec = gpk_dbus_get_exec_for_sender (dbus, sender);
+	if (exec != NULL)
+		gpk_dbus_task_set_exec (task, exec);
 
 	/* unref on delete */
 	//g_signal_connect...
@@ -421,11 +487,26 @@ gpk_dbus_class_init (GpkDbusClass *klass)
 static void
 gpk_dbus_init (GpkDbus *dbus)
 {
+	DBusGConnection *connection;
+
 	dbus->priv = GPK_DBUS_GET_PRIVATE (dbus);
 	dbus->priv->timeout_tmp = -1;
 	dbus->priv->gconf_client = gconf_client_get_default ();
 	dbus->priv->array = g_ptr_array_new ();
 	dbus->priv->x11 = gpk_x11_new ();
+
+	/* find out PIDs on the session bus */
+	connection = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
+	dbus->priv->proxy_session_pid = dbus_g_proxy_new_for_name_owner (connection,
+								 "org.freedesktop.DBus",
+								 "/org/freedesktop/DBus/Bus",
+								 "org.freedesktop.DBus", NULL);
+	/* find out PIDs on the system bus */
+	connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, NULL);
+	dbus->priv->proxy_system_pid = dbus_g_proxy_new_for_name_owner (connection,
+								 "org.freedesktop.DBus",
+								 "/org/freedesktop/DBus/Bus",
+								 "org.freedesktop.DBus", NULL);
 }
 
 /**
@@ -444,6 +525,8 @@ gpk_dbus_finalize (GObject *object)
 	g_ptr_array_free (dbus->priv->array, TRUE);
 	g_object_unref (dbus->priv->gconf_client);
 	g_object_unref (dbus->priv->x11);
+	g_object_unref (dbus->priv->proxy_session_pid);
+	g_object_unref (dbus->priv->proxy_system_pid);
 
 	G_OBJECT_CLASS (gpk_dbus_parent_class)->finalize (object);
 }
diff --git a/src/gpk-watch.c b/src/gpk-watch.c
index 887e703..9350af8 100644
--- a/src/gpk-watch.c
+++ b/src/gpk-watch.c
@@ -37,7 +37,6 @@
 #include <gtk/gtk.h>
 #include <gconf/gconf-client.h>
 #include <libnotify/notify.h>
-#include <polkit-gnome/polkit-gnome.h>
 #include <packagekit-glib/packagekit.h>
 
 #include "egg-debug.h"
@@ -74,7 +73,6 @@ struct GpkWatchPrivate
 	PkTaskList		*tlist;
 	PkRestartEnum		 restart;
 	GConfClient		*gconf_client;
-	PolKitGnomeAction	*restart_action;
 	guint			 set_proxy_timeout;
 	gchar			*error_details;
 	gboolean		 hide_warning;
@@ -807,15 +805,6 @@ gpk_watch_popup_menu_cb (GtkStatusIcon *status_icon, guint button, guint32 times
 }
 
 /**
- * gpk_watch_restart_cb:
- **/
-static void
-gpk_watch_restart_cb (PolKitGnomeAction *action, gpointer data)
-{
-	gpk_restart_system ();
-}
-
-/**
  * gpk_watch_menu_show_messages_cb:
  **/
 static void
@@ -1247,6 +1236,15 @@ gpk_watch_menu_log_out_cb (GtkMenuItem *item, gpointer data)
 }
 
 /**
+ * gpk_watch_menu_restart_cb:
+ **/
+static void
+gpk_watch_menu_restart_cb (GtkMenuItem *item, gpointer data)
+{
+	gpk_restart_system ();
+}
+
+/**
  * gpk_watch_activate_status_cb:
  * @button: Which buttons are pressed
  *
@@ -1283,6 +1281,7 @@ gpk_watch_activate_status_cb (GtkStatusIcon *status_icon, GpkWatch *watch)
 
 	/* log out session */
 	if (watch->priv->restart == PK_RESTART_ENUM_SESSION) {
+		/* TRANSLATORS: log out of the session */
 		widget = gtk_image_menu_item_new_with_mnemonic (_("_Log out"));
 		image = gtk_image_new_from_icon_name ("system-log-out", GTK_ICON_SIZE_MENU);
 		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (widget), image);
@@ -1294,7 +1293,12 @@ gpk_watch_activate_status_cb (GtkStatusIcon *status_icon, GpkWatch *watch)
 
 	/* restart computer */
 	if (watch->priv->restart == PK_RESTART_ENUM_SYSTEM) {
-		widget = gtk_action_create_menu_item (GTK_ACTION (watch->priv->restart_action));
+		/* TRANSLATORS: this menu item restarts the computer after an update */
+		widget = gtk_image_menu_item_new_with_mnemonic (_("_Restart computer"));
+		image = gtk_image_new_from_icon_name ("system-shutdown", GTK_ICON_SIZE_MENU);
+		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (widget), image);
+		g_signal_connect (G_OBJECT (widget), "activate",
+				  G_CALLBACK (gpk_watch_menu_restart_cb), watch);
 		gtk_menu_shell_append (GTK_MENU_SHELL (menu), widget);
 		show_hide = TRUE;
 	}
@@ -1587,9 +1591,6 @@ gpk_watch_button_cancel_cb (GtkWidget *widget, GpkWatch *watch)
 static void
 gpk_watch_init (GpkWatch *watch)
 {
-	PolKitAction *pk_action;
-	PolKitGnomeAction *restart_action;
-
 	watch->priv = GPK_WATCH_GET_PRIVATE (watch);
 	watch->priv->error_details = NULL;
 	watch->priv->notification_cached_messages = NULL;
@@ -1654,23 +1655,6 @@ gpk_watch_init (GpkWatch *watch)
 	if (pk_connection_valid (watch->priv->pconnection))
 		pk_connection_changed_cb (watch->priv->pconnection, TRUE, watch);
 
-	pk_action = polkit_action_new ();
-	polkit_action_set_action_id (pk_action, "org.freedesktop.consolekit.system.restart");
-
-	restart_action = polkit_gnome_action_new_default ("restart-system", pk_action,
-							  /* TRANSLATORS: This button restarts the computer after an update */
-							  _("_Restart computer"), NULL);
-	g_object_set (restart_action,
-		      "no-icon-name", "system-shutdown",
-		      "auth-icon-name", "system-shutdown",
-		      "yes-icon-name","system-shutdown",
-		      "self-blocked-icon-name", "system-shutdown",
-		      NULL);
-	polkit_action_unref (pk_action);
-	g_signal_connect (restart_action, "activate",
-			  G_CALLBACK (gpk_watch_restart_cb), NULL);
-	watch->priv->restart_action = restart_action;
-
 	/* watch proxy keys */
 	gconf_client_add_dir (watch->priv->gconf_client, GPK_WATCH_GCONF_PROXY_HTTP,
 			      GCONF_CLIENT_PRELOAD_NONE, NULL);
@@ -1719,7 +1703,6 @@ gpk_watch_finalize (GObject *object)
 	g_object_unref (watch->priv->control);
 	g_object_unref (watch->priv->pconnection);
 	g_object_unref (watch->priv->gconf_client);
-	g_object_unref (watch->priv->restart_action);
 	g_object_unref (watch->priv->client_primary);
 	g_object_unref (watch->priv->dialog);
 



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