[gnome-applets] cpufreq: port to PolicyKit 1.0



commit b9ca2c4e5d94015c1882fd7b834da80c615d3b43
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Aug 13 11:48:11 2009 +0200

    cpufreq: port to PolicyKit 1.0
    
    Fixes bgo#585307.

 configure.in                                       |   21 +--
 cpufreq/src/Makefile.am                            |    8 -
 cpufreq/src/cpufreq-selector.c                     |  118 ++-----------
 cpufreq/src/cpufreq-selector/Makefile.am           |    2 +-
 .../cpufreq-selector/cpufreq-selector-service.c    |  187 ++++++++------------
 .../cpufreq-selector/cpufreq-selector-service.h    |    2 +
 .../cpufreq-selector/cpufreq-selector-service.xml  |    7 +-
 .../org.gnome.cpufreqselector.policy.in            |    2 +-
 cpufreq/src/cpufreq-utils.c                        |  122 +++----------
 9 files changed, 129 insertions(+), 340 deletions(-)
---
diff --git a/configure.in b/configure.in
index 38fe0df..5bff36a 100644
--- a/configure.in
+++ b/configure.in
@@ -33,7 +33,7 @@ LIBXML_REQUIRED=2.5.0
 GWEATHER_REQUIRED=2.22.1
 GUCHARMAP2_REQUIRED=2.23.0
 GUCHARMAP_REQUIRED=1.4.0
-POLKIT_REQUIRED=0.7
+POLKIT_REQUIRED=0.92
 NETWORKMANAGER_REQUIRED=0.7
 GST10_REQUIRED=0.10.2
 dnl ***************************************************************************
@@ -210,7 +210,7 @@ AC_ARG_ENABLE([polkit],
     enable_polkit=$enableval,
     enable_polkit=auto)
 if test "x$enable_polkit" != "xno"; then
-    PKG_CHECK_MODULES(POLKIT, polkit >= $POLKIT_REQUIRED polkit-dbus >= $POLKIT_REQUIRED dbus-glib-1 >= $DBUS_GLIB_REQUIRED, HAVE_POLKIT=yes, HAVE_POLKIT=no)
+    PKG_CHECK_MODULES(POLKIT, polkit-gobject-1 >= $POLKIT_REQUIRED dbus-glib-1 >= $DBUS_GLIB_REQUIRED, HAVE_POLKIT=yes, HAVE_POLKIT=no)
     if test "x$enable_polkit" = "xyes" -a "x$HAVE_POLKIT" = "xno"; then
         AC_MSG_ERROR([PolicyKit support explicitly requested but dependencies not found])
     fi
@@ -226,22 +226,6 @@ AM_CONDITIONAL(HAVE_POLKIT, test "x$HAVE_POLKIT" = "xyes")
 AC_SUBST(POLKIT_CFLAGS)
 AC_SUBST(POLKIT_LIBS)
 
-POLKIT_GNOME_CFLAGS=
-POLKIT_GNOME_LIBS=
-polkit_gnome=no
-if test "x$enable_polkit" != "xno"; then
-    PKG_CHECK_MODULES(POLKIT_GNOME, polkit-gnome >= $POLKIT_REQUIRED, polkit_gnome=yes, polkit_gnome=no)
-    if test "x$enable_polkit" = "xyes" -a "x$polkit_gnome" = "xno"; then
-        AC_MSG_ERROR([PolicyKit-gnome support explicitly requested but dependencies not found])
-    fi
-fi
-if test "x$polkit_gnome" = "xyes"; then
-    AC_DEFINE(HAVE_POLKIT_GNOME, [1], [PolicyKit-gnome available])
-fi
-AM_CONDITIONAL(HAVE_POLKIT_GNOME, test "x$polkit_gnome" = "xyes")
-AC_SUBST(POLKIT_GNOME_CFLAGS)
-AC_SUBST(POLKIT_GNOME_LIBS)
-
 
 dnl -- check for libhal (optional) --------------------------------------------
 HAL_CFLAGS=
@@ -794,7 +778,6 @@ gnome-applets-$VERSION configure summary:
 	 - cpufreq			$build_cpufreq_applet
 	 	- building selector	$enable_selector
 		- using PolicyKit       $HAVE_POLKIT
-		- using PolicyKit-gnome $polkit_gnome
 		- enabling suid bit	$suid
 	 - drivemount			always
 	 - geyes			always
diff --git a/cpufreq/src/Makefile.am b/cpufreq/src/Makefile.am
index c254a2a..9b82538 100644
--- a/cpufreq/src/Makefile.am
+++ b/cpufreq/src/Makefile.am
@@ -9,10 +9,6 @@ INCLUDES = \
 	$(GNOME_LIBS2_CFLAGS)					\
 	$(LIBGLADE_CFLAGS)
 
-if HAVE_POLKIT_GNOME
-INCLUDES += $(POLKIT_GNOME_CFLAGS)
-endif
-
 libexec_PROGRAMS = cpufreq-applet
 
 if HAVE_LIBCPUFREQ
@@ -38,8 +34,4 @@ cpufreq_applet_LDADD =  \
 	$(LIBGLADE_LIBS)		\
 	$(LIBCPUFREQ_LIBS)
 
-if HAVE_POLKIT_GNOME
-cpufreq_applet_LDADD += $(POLKIT_GNOME_LIBS)
-endif
-
 	
diff --git a/cpufreq/src/cpufreq-selector.c b/cpufreq/src/cpufreq-selector.c
index 48f7fc7..45d0c17 100644
--- a/cpufreq/src/cpufreq-selector.c
+++ b/cpufreq/src/cpufreq-selector.c
@@ -19,19 +19,18 @@
 
 #include <config.h>
 
-#ifdef HAVE_POLKIT_GNOME
+#ifdef HAVE_POLKIT
 #include <dbus/dbus-glib.h>
-#endif /* HAVE_POLKIT_GNOME */
+#endif /* HAVE_POLKIT */
 
 #include "cpufreq-selector.h"
 
 struct _CPUFreqSelector {
 	GObject parent;
 
-#ifdef HAVE_POLKIT_GNOME
+#ifdef HAVE_POLKIT
 	DBusGConnection *system_bus;
-	DBusGConnection *session_bus;
-#endif /* HAVE_POLKIT_GNOME */
+#endif /* HAVE_POLKIT */
 };
 
 struct _CPUFreqSelectorClass {
@@ -45,10 +44,9 @@ cpufreq_selector_finalize (GObject *object)
 {
 	CPUFreqSelector *selector = CPUFREQ_SELECTOR (object);
 
-#ifdef HAVE_POLKIT_GNOME
+#ifdef HAVE_POLKIT
 	selector->system_bus = NULL;
-	selector->session_bus = NULL;
-#endif /* HAVE_POLKIT_GNOME */
+#endif /* HAVE_POLKIT */
 
 	G_OBJECT_CLASS (cpufreq_selector_parent_class)->finalize (object);
 }
@@ -77,7 +75,7 @@ cpufreq_selector_get_default (void)
 	return selector;
 }
 
-#ifdef HAVE_POLKIT_GNOME
+#ifdef HAVE_POLKIT
 typedef enum {
 	FREQUENCY,
 	GOVERNOR
@@ -114,90 +112,12 @@ cpufreq_selector_connect_to_system_bus (CPUFreqSelector *selector,
 {
 	if (selector->system_bus)
 		return TRUE;
-	
+
 	selector->system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, error);
 
 	return (selector->system_bus != NULL);
 }
 
-static gboolean
-cpufreq_selector_connect_to_session_bus (CPUFreqSelector *selector,
-					 GError         **error)
-{
-	if (selector->session_bus)
-		return TRUE;
-	
-	selector->session_bus = dbus_g_bus_get (DBUS_BUS_SESSION, error);
-
-	return (selector->session_bus != NULL);
-}
-
-static void
-dbus_auth_call_notify_cb (DBusGProxy     *proxy,
-			  DBusGProxyCall *call,
-			  gpointer        user_data)
-{
-	SelectorAsyncData *data;
-	gboolean           gained_privilege;
-	GError            *error = NULL;
-
-	data = (SelectorAsyncData *)user_data;
-	
-	if (!dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_BOOLEAN, &gained_privilege, G_TYPE_INVALID)) {
-		g_warning ("%s", error->message);
-		g_error_free (error);
-
-		selector_async_data_free (data);
-
-		return;
-	}
-
-	if (gained_privilege) {
-		switch (data->call) {
-		case FREQUENCY:
-			selector_set_frequency_async (data);
-			break;
-		case GOVERNOR:
-			selector_set_governor_async (data);
-			break;
-		default:
-			g_assert_not_reached ();
-		}
-	} else {
-		selector_async_data_free (data);
-	}
-}
-
-static void
-do_auth_async (SelectorAsyncData *data)
-{
-	DBusGProxy *proxy;
-	GError     *error = NULL;
-	
-	if (!cpufreq_selector_connect_to_session_bus (data->selector, &error)) {
-		g_warning ("%s", error->message);
-		g_error_free (error);
-
-		selector_async_data_free (data);
-
-		return;
-	}
-
-	proxy = dbus_g_proxy_new_for_name (data->selector->session_bus,
-					   "org.gnome.PolicyKit",
-					   "/org/gnome/PolicyKit/Manager",
-					   "org.gnome.PolicyKit.Manager");
-	
-	dbus_g_proxy_begin_call_with_timeout (proxy,
-					      "ShowDialog",
-					      dbus_auth_call_notify_cb,
-					      data, NULL,
-					      INT_MAX,
-					      G_TYPE_STRING, "org.gnome.cpufreqselector",
-					      G_TYPE_UINT, data->parent_xid,
-					      G_TYPE_INVALID);
-}
-
 static void
 dbus_set_call_notify_cb (DBusGProxy     *proxy,
 			 DBusGProxyCall *call,
@@ -213,12 +133,6 @@ dbus_set_call_notify_cb (DBusGProxy     *proxy,
 		return;
 	}
 
-	if (error->domain == DBUS_GERROR && DBUS_GERROR_REMOTE_EXCEPTION &&
-	    dbus_g_error_has_name (error, "org.gnome.CPUFreqSelector.NotAuthorized")) {
-		do_auth_async (data);
-		return;
-	}
-
 	selector_async_data_free (data);
 	g_warning ("%s", error->message);
 	g_error_free (error);
@@ -229,13 +143,13 @@ selector_set_frequency_async (SelectorAsyncData *data)
 {
 	DBusGProxy *proxy;
 	GError     *error = NULL;
-		
+
 	if (!cpufreq_selector_connect_to_system_bus (data->selector, &error)) {
 		g_warning ("%s", error->message);
 		g_error_free (error);
 
 		selector_async_data_free (data);
-		
+
 		return;
 	}
 
@@ -243,7 +157,7 @@ selector_set_frequency_async (SelectorAsyncData *data)
 					   "org.gnome.CPUFreqSelector",
 					   "/org/gnome/cpufreq_selector/selector",
 					   "org.gnome.CPUFreqSelector");
-	
+
 	dbus_g_proxy_begin_call_with_timeout (proxy, "SetFrequency",
 					      dbus_set_call_notify_cb,
 					      data, NULL,
@@ -278,13 +192,13 @@ selector_set_governor_async (SelectorAsyncData *data)
 {
 	DBusGProxy *proxy;
 	GError     *error = NULL;
-		
+
 	if (!cpufreq_selector_connect_to_system_bus (data->selector, &error)) {
 		g_warning ("%s", error->message);
 		g_error_free (error);
 
 		selector_async_data_free (data);
-		
+
 		return;
 	}
 
@@ -292,7 +206,7 @@ selector_set_governor_async (SelectorAsyncData *data)
 					   "org.gnome.CPUFreqSelector",
 					   "/org/gnome/cpufreq_selector/selector",
 					   "org.gnome.CPUFreqSelector");
-	
+
 	dbus_g_proxy_begin_call_with_timeout (proxy, "SetGovernor",
 					      dbus_set_call_notify_cb,
 					      data, NULL,
@@ -321,7 +235,7 @@ cpufreq_selector_set_governor_async (CPUFreqSelector *selector,
 
 	selector_set_governor_async (data);
 }
-#else /* !HAVE_POLKIT_GNOME */
+#else /* !HAVE_POLKIT */
 static void
 cpufreq_selector_run_command (CPUFreqSelector *selector,
 			      const gchar     *args)
@@ -372,4 +286,4 @@ cpufreq_selector_set_governor_async (CPUFreqSelector *selector,
 	cpufreq_selector_run_command (selector, args);
 	g_free (args);
 }
-#endif /* HAVE_POLKIT_GNOME */
+#endif /* HAVE_POLKIT */
diff --git a/cpufreq/src/cpufreq-selector/Makefile.am b/cpufreq/src/cpufreq-selector/Makefile.am
index a6c6ab5..dc9cf73 100644
--- a/cpufreq/src/cpufreq-selector/Makefile.am
+++ b/cpufreq/src/cpufreq-selector/Makefile.am
@@ -51,7 +51,7 @@ polkit_in_files = org.gnome.cpufreqselector.policy.in
 
 dbus_servicesdir = $(datadir)/dbus-1/system-services
 dbus_confdir = $(sysconfdir)/dbus-1/system.d
-polkitdir = $(datadir)/PolicyKit/policy
+polkitdir = $(datadir)/polkit-1/actions
 
 if HAVE_POLKIT
 BUILT_SOURCES = cpufreq-selector-service-glue.h
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
index 3308f67..381a9fd 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.c
@@ -17,7 +17,7 @@
  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#include <polkit-dbus/polkit-dbus.h>
+#include <polkit/polkit.h>
 #include <dbus/dbus-glib-lowlevel.h>
 
 #include "cpufreq-selector.h"
@@ -37,7 +37,7 @@ struct _CPUFreqSelectorService {
 	DBusGConnection *system_bus;
 	
 	/* PolicyKit */
-	PolKitContext   *pk_context;
+	PolkitAuthority   *authority;
 };
 
 struct _CPUFreqSelectorServiceClass {
@@ -99,9 +99,9 @@ cpufreq_selector_service_finalize (GObject *object)
 		service->selectors_max = -1;
 	}
 
-	if (service->pk_context) {
-		polkit_context_unref (service->pk_context);
-		service->pk_context = NULL;
+	if (service->authority) {
+		g_object_unref (service->authority);
+		service->authority = NULL;
 	}
 
 	G_OBJECT_CLASS (cpufreq_selector_service_parent_class)->finalize (object);
@@ -153,42 +153,6 @@ reset_killtimer (void)
 					  NULL);
 }
 
-static gboolean
-pk_io_watch_have_data (GIOChannel    *channel,
-		       GIOCondition   condition,
-		       PolKitContext *pk_context)
-{
-	polkit_context_io_func (pk_context,
-				g_io_channel_unix_get_fd (channel));
-	return TRUE;
-}
-
-static int
-pk_add_io_watch (PolKitContext *pk_context,
-		 int            watch_id)
-{
-	GIOChannel *channel;
-	guint       id = 0;
-	
-	channel = g_io_channel_unix_new (watch_id);
-	if (!channel)
-		return 0;
-
-	id = g_io_add_watch (channel, G_IO_IN,
-			     (GIOFunc)pk_io_watch_have_data,
-			     pk_context);
-	g_io_channel_unref (channel);
-
-	return id;
-}
-
-static void
-pk_remove_io_watch (PolKitContext *pk_context,
-		    int            watch_id)
-{
-	g_source_remove (watch_id);
-}
-
 gboolean
 cpufreq_selector_service_register (CPUFreqSelectorService *service,
 				   GError                **error)
@@ -198,7 +162,6 @@ cpufreq_selector_service_register (CPUFreqSelectorService *service,
 	gboolean         res;
 	guint            result;
 	GError          *err = NULL;
-	PolKitError     *pk_err = NULL;
 
 	if (service->system_bus) {
 		g_set_error (error,
@@ -268,30 +231,7 @@ cpufreq_selector_service_register (CPUFreqSelectorService *service,
 		return FALSE;
 	}
 
-	service->pk_context = polkit_context_new ();
-	polkit_context_set_io_watch_functions (service->pk_context,
-					       pk_add_io_watch,
-					       pk_remove_io_watch);
-	if (!polkit_context_init (service->pk_context, &pk_err)) {
-		polkit_context_unref (service->pk_context);
-		service->pk_context = NULL;
-
-		if (polkit_error_is_set (pk_err)) {
-			g_set_error (error,
-				     CPUFREQ_SELECTOR_SERVICE_ERROR,
-				     SERVICE_ERROR_GENERAL,
-				     "Cannot initialize libpolkit: %s",
-				     polkit_error_get_error_message (pk_err));
-			polkit_error_free (pk_err);
-		} else {
-			g_set_error (error,
-				     CPUFREQ_SELECTOR_SERVICE_ERROR,
-				     SERVICE_ERROR_GENERAL,
-				     "Cannot initialize libpolkit");
-		}
-
-		return FALSE;
-	}
+	service->authority = polkit_authority_get ();
 
 	service->system_bus = connection;
 
@@ -330,62 +270,41 @@ cpufreq_selector_service_check_policy (CPUFreqSelectorService *service,
 				       DBusGMethodInvocation  *context,
 				       GError                **error)
 {
-	PolKitAction   *pk_action;
-	PolKitCaller   *pk_caller;
-	PolKitResult    pk_result;
-	gchar          *sender;
-	DBusConnection *connection;
-	DBusError       dbus_error;
-	PolKitError    *pk_error = NULL;
+	PolkitSubject             *subject;
+	PolkitAuthorizationResult *result;
+	gchar                     *sender;
+	gboolean                   ret;
 
 	sender = dbus_g_method_get_sender (context);
-	connection = dbus_g_connection_get_connection (service->system_bus);
-
-	dbus_error_init (&dbus_error);
-	pk_caller = polkit_caller_new_from_dbus_name (connection, sender, &dbus_error);
-	if (!pk_caller) {
-		g_set_error (error,
-			     CPUFREQ_SELECTOR_SERVICE_ERROR,
-			     SERVICE_ERROR_DBUS,
-			     "Error getting information about caller: %s: %s",
-			     dbus_error.name, dbus_error.message);
-		dbus_error_free (&dbus_error);
-		g_free (sender);
-
-		return FALSE;
-	}
+	subject = polkit_system_bus_name_new (sender);
 	g_free (sender);
 
-	pk_action = polkit_action_new ();
-	polkit_action_set_action_id (pk_action, "org.gnome.cpufreqselector");
-	pk_result = polkit_context_is_caller_authorized (service->pk_context,
-							 pk_action, pk_caller,
-							 TRUE, &pk_error);
-	polkit_caller_unref (pk_caller);
-	polkit_action_unref (pk_action);
-	
-	if (polkit_error_is_set (pk_error)) {
-		g_set_error (error,
-			     CPUFREQ_SELECTOR_SERVICE_ERROR,
-			     SERVICE_ERROR_GENERAL,
-			     "Could not determine if caller is authorized: %s",
-			     polkit_error_get_error_message (pk_error));
-		polkit_error_free (pk_error);
+	result = polkit_authority_check_authorization_sync (service->authority,
+                                                            subject,
+                                                            "org.gnome.cpufreqselector",
+                                                            NULL,
+                                                            POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
+                                                            NULL, error);
+        g_object_unref (subject);
+
+        if (*error) {
+		g_warning ("Check policy: %s", (*error)->message);
+		g_object_unref (result);
 
 		return FALSE;
 	}
 
-	if (pk_result != POLKIT_RESULT_YES) {
+	ret = polkit_authorization_result_get_is_authorized (result);
+	if (!ret) {
 		g_set_error (error,
 			     CPUFREQ_SELECTOR_SERVICE_ERROR,
 			     SERVICE_ERROR_NOT_AUTHORIZED,
-			     "Caller is not authorized: %s",
-			     polkit_result_to_string_representation (pk_result));
-
-		return FALSE;
+			     "Caller is not authorized");
 	}
-			     
-	return TRUE;
+
+	g_object_unref (result);
+
+	return ret;
 }
 
 /* D-BUS interface */
@@ -518,3 +437,51 @@ cpufreq_selector_service_set_governor (CPUFreqSelectorService *service,
 	
 	return TRUE;
 }
+
+
+gboolean
+cpufreq_selector_service_can_set (CPUFreqSelectorService *service,
+				  DBusGMethodInvocation  *context)
+{
+	PolkitSubject             *subject;
+	PolkitAuthorizationResult *result;
+	gchar                     *sender;
+	gboolean                   ret;
+        GError                    *error = NULL;
+
+	reset_killtimer ();
+
+	sender = dbus_g_method_get_sender (context);
+	subject = polkit_system_bus_name_new (sender);
+	g_free (sender);
+
+	result = polkit_authority_check_authorization_sync (service->authority,
+                                                            subject,
+                                                            "org.gnome.cpufreqselector",
+                                                            NULL,
+                                                            0,
+                                                            NULL,
+							    &error);
+        g_object_unref (subject);
+
+	if (error) {
+		dbus_g_method_return_error (context, error);
+		g_error_free (error);
+
+		return FALSE;
+	}
+
+        if (polkit_authorization_result_get_is_authorized (result)) {
+		ret = TRUE;
+	} else if (polkit_authorization_result_get_is_challenge (result)) {
+		ret = TRUE;
+	} else {
+		ret = FALSE;
+	}
+
+	g_object_unref (result);
+
+        dbus_g_method_return (context, ret);
+
+	return TRUE;
+}
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h
index 95d7c94..fd32170 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.h
@@ -62,6 +62,8 @@ gboolean                cpufreq_selector_service_set_governor   (CPUFreqSelector
 								 guint                   cpu,
 								 const gchar            *governor,
 								 DBusGMethodInvocation  *context);
+gboolean               cpufreq_selector_service_can_set         (CPUFreqSelectorService *service,
+								 DBusGMethodInvocation  *context);
 
 G_END_DECLS
 
diff --git a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml
index 6b742b6..903e607 100644
--- a/cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml
+++ b/cpufreq/src/cpufreq-selector/cpufreq-selector-service.xml
@@ -13,6 +13,11 @@
       <arg name="cpu" direction="in" type="u"/>
       <arg name="governor" direction="in" type="s"/>
     </method>
-    
+
+    <method name="CanSet">
+      <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
+      <arg name="result" direction="out" type="b"/>
+    </method>
+
   </interface>
 </node>
diff --git a/cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in b/cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in
index bbf8575..25698e3 100644
--- a/cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in
+++ b/cpufreq/src/cpufreq-selector/org.gnome.cpufreqselector.policy.in
@@ -15,7 +15,7 @@
     <_message>Privileges are required to change the CPU Frequency scaling.</_message>
     <defaults>
       <allow_inactive>no</allow_inactive>
-      <allow_active>auth_admin_keep_always</allow_active>
+      <allow_active>auth_admin_keep</allow_active>
     </defaults>
   </action>
 
diff --git a/cpufreq/src/cpufreq-utils.c b/cpufreq/src/cpufreq-utils.c
index 9c8cc23..bd457ba 100644
--- a/cpufreq/src/cpufreq-utils.c
+++ b/cpufreq/src/cpufreq-utils.c
@@ -21,13 +21,6 @@
 
 #include <config.h>
 
-#ifdef HAVE_POLKIT_GNOME
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
-#include <polkit/polkit.h>
-#include <polkit-dbus/polkit-dbus.h>
-#endif
-
 #include <glib.h>
 #include <gtk/gtk.h>
 #include <sys/types.h>
@@ -39,6 +32,10 @@
 
 #include "cpufreq-utils.h"
 
+#ifdef HAVE_POLKIT
+#include <dbus/dbus-glib.h>
+#endif /* HAVE_POLKIT */
+
 guint
 cpufreq_utils_get_n_cpus (void)
 {
@@ -108,56 +105,16 @@ cpufreq_utils_display_error (const gchar *message,
 	gtk_widget_show (dialog);
 }
 
-#ifdef HAVE_POLKIT_GNOME
+#ifdef HAVE_POLKIT
 #define CACHE_VALIDITY_SEC 2
 
 static gboolean
-pk_io_watch_have_data (GIOChannel    *channel,
-		       GIOCondition   condition,
-		       PolKitContext *pk_context)
-{
-	polkit_context_io_func (pk_context,
-				g_io_channel_unix_get_fd (channel));
-	return TRUE;
-}
-
-static int
-pk_add_io_watch (PolKitContext *pk_context,
-		 int            watch_id)
-{
-	GIOChannel *channel;
-	guint       id = 0;
-
-	channel = g_io_channel_unix_new (watch_id);
-	if (!channel)
-		return 0;
-
-	id = g_io_add_watch (channel, G_IO_IN,
-			     (GIOFunc)pk_io_watch_have_data,
-			     pk_context);
-	g_io_channel_unref (channel);
-
-	return id;
-}
-
-static void
-pk_remove_io_watch (PolKitContext *pk_context,
-		    int            watch_id)
-{
-	g_source_remove (watch_id);
-}
-
-static gboolean
-selector_is_available (const gchar *action)
+selector_is_available (void)
 {
+        DBusGProxy             *proxy;
 	static DBusGConnection *system_bus = NULL;
-	static PolKitContext   *pk_context = NULL;
-	PolKitCaller           *pk_caller;
-	PolKitAction           *pk_action;
-	PolKitResult            pk_result;
-	PolKitError            *pk_error = NULL;
 	GError                 *error = NULL;
-	DBusError               dbus_error;
+	gboolean                result;
 
 	if (!system_bus) {
 		system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
@@ -169,54 +126,23 @@ selector_is_available (const gchar *action)
 		}
 	}
 
-	if (!pk_context) {
-		PolKitError *pk_error = NULL;
-		
-		pk_context = polkit_context_new ();
-		polkit_context_set_io_watch_functions (pk_context,
-						       pk_add_io_watch,
-						       pk_remove_io_watch);
-		if (!polkit_context_init (pk_context, &pk_error)) {
-			polkit_context_unref (pk_context);
-			pk_context = NULL;
-
-			if (polkit_error_is_set (pk_error)) {
-				g_warning ("%s", polkit_error_get_error_message (pk_error));
-				polkit_error_free (pk_error);
-			} else {
-				g_warning ("Cannot initialize libpolkit");
-			}
-
-			return FALSE;
-		}
-	}
-
-	dbus_error_init (&dbus_error);
-	pk_caller = polkit_caller_new_from_pid (dbus_g_connection_get_connection (system_bus),
-						getpid (), &dbus_error);
-	if (!pk_caller) {
-		g_warning ("Cannot get caller from dbus name");
-
-		return FALSE;
+        proxy = dbus_g_proxy_new_for_name (system_bus,
+                                           "org.gnome.CPUFreqSelector",
+                                           "/org/gnome/cpufreq_selector/selector",
+                                           "org.gnome.CPUFreqSelector");
+
+        if (!dbus_g_proxy_call (proxy, "CanSet", &error,
+                           	G_TYPE_INVALID,
+                           	G_TYPE_BOOLEAN, &result,
+                           	G_TYPE_INVALID)) {
+		g_warning ("Error calling org.gnome.CPUFreqSelector.CanSet: %s", error->message);
+		g_error_free (error);
+		result = FALSE;
 	}
 
-	pk_action = polkit_action_new ();
-	polkit_action_set_action_id (pk_action, action);
-	pk_result = polkit_context_is_caller_authorized (pk_context,
-							 pk_action, pk_caller,
-							 FALSE, &pk_error);
-
-	polkit_caller_unref (pk_caller);
-	polkit_action_unref (pk_action);
-	
-	if (polkit_error_is_set (pk_error)) {
-		g_warning ("%s", polkit_error_get_error_message (pk_error));
-		polkit_error_free (pk_error);
-
-		return FALSE;
-	}
+	g_object_unref (proxy);
 
-	return !(pk_result == POLKIT_RESULT_UNKNOWN || pk_result == POLKIT_RESULT_NO);
+	return result;
 }
 
 gboolean
@@ -228,13 +154,13 @@ cpufreq_utils_selector_is_available (void)
 
 	time (&now);
 	if (ABS (now - last_refreshed) > CACHE_VALIDITY_SEC) {
-		cache = selector_is_available ("org.gnome.cpufreqselector");
+		cache = selector_is_available ();
 		last_refreshed = now;
 	}
 
 	return cache;
 }
-#else /* !HAVE_POLKIT_GNOME */
+#else /* !HAVE_POLKIT */
 gboolean
 cpufreq_utils_selector_is_available (void)
 {



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