[gconf-editor] port to PolicyKit 1.0
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gconf-editor] port to PolicyKit 1.0
- Date: Mon, 24 Aug 2009 00:01:19 +0000 (UTC)
commit 6d5c0ac4041eaac3bb76391523f167870e03164e
Author: Cosimo Cecchi <cosimoc gnome org>
Date: Mon Aug 24 01:58:31 2009 +0200
port to PolicyKit 1.0
This also removes the need of depending on polkit-dbus, and needs a new
GConf to work properly.
Based on the patch by Matthias Clasen, fix bug #585596.
configure.in | 22 +---
src/Makefile.am | 7 +-
src/gconf-editor-window.c | 50 +-------
src/gconf-policykit.c | 296 ++++++++++++++-------------------------------
src/gconf-policykit.h | 6 +-
5 files changed, 101 insertions(+), 280 deletions(-)
---
diff --git a/configure.in b/configure.in
index d15ae83..ce40820 100644
--- a/configure.in
+++ b/configure.in
@@ -34,7 +34,8 @@ changequote([,])dnl
PKG_CHECK_MODULES(GCONF_EDITOR,
gconf-2.0 >= 2.9.2
- gtk+-2.0 >= 2.12.0)
+ gtk+-2.0 >= 2.12.0
+ dbus-glib-1)
if test "$enable_maintainer_mode" = "yes"; then
DISABLE_DEPRECATED_CFLAGS="-DG_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED \
@@ -53,22 +54,6 @@ GNOME_COMMON_INIT
GNOME_COMPILE_WARNINGS([maximum])
GNOME_MAINTAINER_MODE_DEFINES
-dnl Enable or not the policykit support
-AC_ARG_WITH([policykit],
- AC_HELP_STRING([--with-policykit],
- [Compile with policykit support @<:@default=yes@:>@]),
- [with_polkit=$withval],
- [with_polkit=yes])
-
-if test x$with_polkit = xyes; then
- AC_DEFINE([HAVE_POLICYKIT], 1, [PolicyKit support is required])
- PKG_CHECK_MODULES(GCONF_EDITOR_POLKIT,
- polkit-dbus >= 0.7
- dbus-glib-1 >= 0.71)
-fi
-
-AM_CONDITIONAL([HAVE_POLICYKIT], [test x$with_polkit = xyes])
-
dnl Get the GConf defaults source, and sed it to make the mandatory source.
GCONF_DEFAULTS_SOURCE=`gconftool-2 --get-default-source`
AC_ARG_WITH(gconf-defaults-source,
@@ -101,5 +86,4 @@ AC_OUTPUT
echo "
Configuration Sources:
GConf default values source: ${GCONF_DEFAULTS_SOURCE}
-GConf mandatory values source: ${GCONF_MANDATORY_SOURCE}
-PolicyKit support: ${with_polkit}"
+GConf mandatory values source: ${GCONF_MANDATORY_SOURCE}"
diff --git a/src/Makefile.am b/src/Makefile.am
index 193db21..6a3d914 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -26,6 +26,8 @@ gconf_editor_SOURCES = \
gconf-key-editor.h \
gconf-list-model.c \
gconf-list-model.h \
+ gconf-policykit.c \
+ gconf-policykit.h \
gconf-stock-icons.c \
gconf-stock-icons.h \
gconf-tree-model.c \
@@ -42,11 +44,6 @@ gconf_editor_SOURCES = \
$(gconf_editor_built_headers) \
$(gconf_editor_built_cfiles)
-if HAVE_POLICYKIT
-gconf_editor_SOURCES += gconf-policykit.h \
- gconf-policykit.c
-endif
-
## we just punt on built sources, and check them into svn, manually updating
## when required.
regenerate-built-sources:
diff --git a/src/gconf-editor-window.c b/src/gconf-editor-window.c
index 733ff7f..2dfb2bc 100644
--- a/src/gconf-editor-window.c
+++ b/src/gconf-editor-window.c
@@ -27,9 +27,7 @@
#include "gconf-cell-renderer.h"
#include "gconf-editor-application.h"
#include "gconf-key-editor.h"
-#ifdef HAVE_POLICYKIT
-# include "gconf-policykit.h"
-#endif
+#include "gconf-policykit.h"
#include "gconf-stock-icons.h"
#include "gconf-util.h"
#include "gedit-output-window.h"
@@ -642,14 +640,12 @@ gconf_editor_window_row_expanded (GtkTreeView *tree_view,
gdk_display_flush (gtk_widget_get_display (GTK_WIDGET (gconfwindow)));
}
-#ifdef HAVE_POLICYKIT
static void
gconf_editor_popup_policykit_callback (GtkWindow *window, GError *error)
{
if (error)
gconf_editor_window_popup_error_dialog (window, _("Could not set value. Error was:\n%s"), error);
}
-#endif
static void
gconf_editor_popup_window_set_as_default (GtkAction *action, GtkWidget *callback_data)
@@ -659,13 +655,10 @@ gconf_editor_popup_window_set_as_default (GtkAction *action, GtkWidget *callback
GtkTreeIter iter;
GConfValue *value;
char *path = NULL;
-
-#ifdef HAVE_POLICYKIT
GtkWindow *window = GTK_WINDOW (callback_data);
gboolean can_use_pk;
can_use_pk = (gconfwindow->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL);
-#endif
gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (gconfwindow->list_view)),
NULL, &iter);
gtk_tree_model_get (gconfwindow->sorted_list_model, &iter,
@@ -686,21 +679,16 @@ gconf_editor_popup_window_set_as_default (GtkAction *action, GtkWidget *callback
if (!error)
return;
-#ifdef HAVE_POLICYKIT
if (!can_use_pk)
gconf_editor_window_popup_error_dialog (window, _("Could not sync value. Error was:\n%s"), error);
-#endif
}
}
-#ifdef HAVE_POLICYKIT
if (can_use_pk) {
gconf_client_suggest_sync (gconfwindow->client, NULL);
gconf_pk_set_default_async (path,
- GDK_WINDOW_XWINDOW (GTK_WIDGET (window)->window),
(GFunc) gconf_editor_popup_policykit_callback,
g_object_ref (window), g_object_unref);
}
-#endif
}
static void
@@ -711,12 +699,10 @@ gconf_editor_popup_window_set_as_mandatory (GtkAction *action, GtkWidget *callba
GtkTreeIter iter;
GConfValue *value;
char *path = NULL;
-#ifdef HAVE_POLICYKIT
GtkWindow *window = GTK_WINDOW (callback_data);
gboolean can_use_pk;
can_use_pk = (gconfwindow->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL);
-#endif
gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (gconfwindow->list_view)),
NULL, &iter);
gtk_tree_model_get (gconfwindow->sorted_list_model, &iter,
@@ -737,21 +723,16 @@ gconf_editor_popup_window_set_as_mandatory (GtkAction *action, GtkWidget *callba
if (!error)
return;
-#ifdef HAVE_POLICYKIT
if (!can_use_pk)
gconf_editor_window_popup_error_dialog (window, _("Could not sync value. Error was:\n%s"), error);
-#endif
}
}
-#ifdef HAVE_POLICYKIT
if (can_use_pk) {
gconf_client_suggest_sync (gconfwindow->client, NULL);
gconf_pk_set_mandatory_async (path,
- GDK_WINDOW_XWINDOW (GTK_WIDGET (window)->window),
(GFunc) gconf_editor_popup_policykit_callback,
g_object_ref (window), g_object_unref);
}
-#endif
}
static GtkActionEntry entries[] = {
@@ -945,20 +926,10 @@ list_view_button_press_event (GtkTreeView *tree_view, GdkEventButton *event, GCo
window->type != GCONF_EDITOR_WINDOW_TYPE_DEFAULTS);
gtk_widget_set_sensitive (gtk_ui_manager_get_widget (window->ui_manager, "/GConfKeyPopupMenu/DefaultKey"),
(gconf_util_can_edit_defaults () && window->type != GCONF_EDITOR_WINDOW_TYPE_DEFAULTS) ||
-#ifdef HAVE_POLICYKIT
- (gconf_pk_can_set_default () &&
-#else
- (
-#endif
- window->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL));
+ (gconf_pk_can_set_default ("/") && window->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL));
gtk_widget_set_sensitive (gtk_ui_manager_get_widget (window->ui_manager, "/GConfKeyPopupMenu/MandatoryKey"),
(gconf_util_can_edit_mandatory () && window->type != GCONF_EDITOR_WINDOW_TYPE_MANDATORY) ||
-#ifdef HAVE_POLICYKIT
- (gconf_pk_can_set_mandatory () &&
-#else
- (
-#endif
- window->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL));
+ (gconf_pk_can_set_mandatory ("/") && window->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL));
gtk_tree_path_free (path);
}
@@ -1058,21 +1029,10 @@ gconf_editor_window_list_view_popup_menu (GtkWidget *widget, GConfEditorWindow *
window->type != GCONF_EDITOR_WINDOW_TYPE_DEFAULTS);
gtk_widget_set_sensitive (gtk_ui_manager_get_widget (window->ui_manager, "/GConfKeyPopupMenu/DefaultKey"),
(gconf_util_can_edit_defaults () && window->type != GCONF_EDITOR_WINDOW_TYPE_DEFAULTS) ||
-#ifdef HAVE_POLICYKIT
- (gconf_pk_can_set_default () &&
-#else
- (
-#endif
-window->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL));
+ (gconf_pk_can_set_default ("/") && window->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL));
gtk_widget_set_sensitive (gtk_ui_manager_get_widget (window->ui_manager, "/GConfKeyPopupMenu/MandatoryKey"),
(gconf_util_can_edit_mandatory () && window->type != GCONF_EDITOR_WINDOW_TYPE_MANDATORY) ||
-#ifdef HAVE_POLICYKIT
- (gconf_pk_can_set_mandatory () &&
-#else
- (
-#endif
-window->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL));
-
+ (gconf_pk_can_set_mandatory ("/") && window->type == GCONF_EDITOR_WINDOW_TYPE_NORMAL));
}
else {
gtk_widget_set_sensitive (gtk_ui_manager_get_widget (window->ui_manager, "/GConfKeyPopupMenu/EditKey"),
diff --git a/src/gconf-policykit.c b/src/gconf-policykit.c
index 4b7a6c7..1f57a56 100644
--- a/src/gconf-policykit.c
+++ b/src/gconf-policykit.c
@@ -36,33 +36,11 @@
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
-#include <polkit/polkit.h>
-#include <polkit-dbus/polkit-dbus.h>
-
#include "gconf-policykit.h"
#define CACHE_VALIDITY_SEC 10
static DBusGConnection *
-get_session_bus (void)
-{
- GError *error;
- static DBusGConnection *bus = NULL;
-
- if (bus == NULL) {
- error = NULL;
- bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
- if (bus == NULL) {
- g_warning ("Couldn't connect to session bus: %s",
- error->message);
- g_error_free (error);
- }
- }
-
- return bus;
-}
-
-static DBusGConnection *
get_system_bus (void)
{
GError *error;
@@ -81,156 +59,118 @@ get_system_bus (void)
return bus;
}
-static gboolean
-pk_io_watch_have_data (GIOChannel *channel, GIOCondition condition, gpointer user_data)
-{
- int fd;
- PolKitContext *pk_context = user_data;
- fd = g_io_channel_unix_get_fd (channel);
- polkit_context_io_func (pk_context, fd);
- return TRUE;
-}
-
-static int
-pk_io_add_watch_fn (PolKitContext *pk_context, int fd)
-{
- guint id = 0;
- GIOChannel *channel;
- channel = g_io_channel_unix_new (fd);
- if (channel == NULL)
- goto out;
- id = g_io_add_watch (channel, G_IO_IN, pk_io_watch_have_data, pk_context);
- if (id == 0) {
- g_io_channel_unref (channel);
- goto out;
- }
- g_io_channel_unref (channel);
-out:
- return id;
-}
-
-static void
-pk_io_remove_watch_fn (PolKitContext *pk_context, int watch_id)
-{
- g_source_remove (watch_id);
-}
-
-static PolKitContext *
-get_pk_context (void)
+static guint
+can_set (const gchar *key, gboolean mandatory)
{
- static PolKitContext *pk_context = NULL;
-
- if (pk_context == NULL) {
- pk_context = polkit_context_new ();
- polkit_context_set_io_watch_functions (pk_context,
- pk_io_add_watch_fn,
- pk_io_remove_watch_fn);
- if (!polkit_context_init (pk_context, NULL)) {
- polkit_context_unref (pk_context);
- pk_context = NULL;
- }
- }
-
- return pk_context;
-}
+ DBusGConnection *bus;
+ DBusGProxy *proxy = NULL;
+ const gchar *keys[2];
+ const gchar *func;
+ GError *error = NULL;
+ guint res = 0;
-static gint
-can_do (const gchar *pk_action_id)
-{
- DBusConnection *system_bus;
- PolKitCaller *pk_caller;
- PolKitAction *pk_action;
- PolKitResult pk_result;
- PolKitContext *pk_context;
- DBusError dbus_error;
- gint res = 0;
-
- pk_caller = NULL;
- pk_action = NULL;
-
- system_bus = dbus_g_connection_get_connection (get_system_bus ());
- if (system_bus == NULL)
+ bus = get_system_bus ();
+ if (bus == NULL)
goto out;
- pk_context = get_pk_context ();
- if (pk_context == NULL)
+ proxy = dbus_g_proxy_new_for_name (bus,
+ "org.gnome.GConf.Defaults",
+ "/",
+ "org.gnome.GConf.Defaults");
+ if (proxy == NULL)
goto out;
- pk_action = polkit_action_new ();
- polkit_action_set_action_id (pk_action, pk_action_id);
+ keys[0] = key;
+ keys[1] = NULL;
+ func = mandatory ? "CanSetMandatory" : "CanSetSystem";
- dbus_error_init (&dbus_error);
- pk_caller = polkit_caller_new_from_pid (system_bus, getpid (), &dbus_error);
- if (pk_caller == NULL) {
- fprintf (stderr, "cannot get caller from dbus name\n");
- goto out;
- }
-
- pk_result = polkit_context_is_caller_authorized (pk_context, pk_action, pk_caller, FALSE, NULL);
-
- switch (pk_result) {
- case POLKIT_RESULT_UNKNOWN:
- case POLKIT_RESULT_NO:
- res = 0;
- break;
- case POLKIT_RESULT_YES:
- res = 2;
- break;
- default:
- /* This covers all the
- * POLKIT_RESULT_ONLY_VIA_[SELF|ADMIN]_AUTH_* cases as more of
- * these may be added in the future.
- */
- res = 1;
- break;
- }
+ if (!dbus_g_proxy_call (proxy, func,
+ &error,
+ G_TYPE_STRV, keys,
+ G_TYPE_INVALID,
+ G_TYPE_UINT, &res,
+ G_TYPE_INVALID)) {
+ g_warning ("error calling %s: %s\n", func, error->message);
+ g_error_free (error);
+ }
out:
- if (pk_action != NULL)
- polkit_action_unref (pk_action);
- if (pk_caller != NULL)
- polkit_caller_unref (pk_caller);
+ if (proxy)
+ g_object_unref (proxy);
return res;
}
+typedef struct
+{
+ time_t last_refreshed;
+ gint can_set;
+} CacheEntry;
+
+static GHashTable *defaults_cache = NULL;
+static GHashTable *mandatory_cache = NULL;
+
gint
-gconf_pk_can_set_default (void)
+gconf_pk_can_set (const gchar *key, gboolean mandatory)
{
- static gboolean cache = FALSE;
- static time_t last_refreshed = 0;
- time_t now;
+ time_t now;
+ GHashTable **cache;
+ CacheEntry *entry;
time (&now);
- if (ABS (now - last_refreshed) > CACHE_VALIDITY_SEC) {
- cache = can_do ("org.gnome.gconf.defaults.set-system");
- last_refreshed = now;
+ cache = mandatory ? &mandatory_cache : &defaults_cache;
+ if (!*cache)
+ *cache = g_hash_table_new (g_str_hash, g_str_equal);
+ entry = (CacheEntry *) g_hash_table_lookup (*cache, key);
+ if (!entry) {
+ entry = g_new0 (CacheEntry, 1);
+ g_hash_table_insert (*cache, (char *) key, entry);
+ }
+ if (ABS (now - entry->last_refreshed) > CACHE_VALIDITY_SEC) {
+ entry->can_set = can_set (key, mandatory);
+ entry->last_refreshed = now;
}
- return cache;
+ return entry->can_set;
+}
+
+gint
+gconf_pk_can_set_default (const gchar *key)
+{
+ return gconf_pk_can_set (key, FALSE);
}
gint
-gconf_pk_can_set_mandatory (void)
+gconf_pk_can_set_mandatory (const gchar *key)
+{
+ return gconf_pk_can_set (key, TRUE);
+}
+
+static void
+gconf_pk_update_can_set_cache (const gchar *key,
+ gboolean mandatory)
{
- static gboolean cache = FALSE;
- static time_t last_refreshed = 0;
time_t now;
+ GHashTable **cache;
+ CacheEntry *entry;
time (&now);
- if (ABS (now - last_refreshed) > CACHE_VALIDITY_SEC) {
- cache = can_do ("org.gnome.gconf.defaults.set-mandatory");
- last_refreshed = now;
- }
-
- return cache;
+ cache = mandatory ? &mandatory_cache : &defaults_cache;
+ if (!*cache)
+ *cache = g_hash_table_new (g_str_hash, g_str_equal);
+ entry = (CacheEntry *) g_hash_table_lookup (*cache, key);
+ if (!entry) {
+ entry = g_new0 (CacheEntry, 1);
+ g_hash_table_insert (*cache, (char *) key, entry);
+ }
+ entry->can_set = 2;
+ entry->last_refreshed = now;
}
typedef struct {
gint ref_count;
- const gchar *call;
+ gboolean mandatory;
gchar *key;
- guint transient_parent_xid;
GFunc callback;
gpointer data;
GDestroyNotify notify;
@@ -263,54 +203,8 @@ _gconf_pk_data_unref (gpointer d)
static void set_key_async (GConfPKCallbackData *data);
static void
-auth_notify (DBusGProxy *proxy,
- DBusGProxyCall *call,
- void *user_data)
-{
- GConfPKCallbackData *data = user_data;
- GError *error = NULL;
- gboolean gained_privilege;
-
- if (dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_BOOLEAN, &gained_privilege, G_TYPE_INVALID)) {
- if (gained_privilege)
- set_key_async (data);
- }
- else {
- if (data->callback)
- data->callback (data->data, error);
- else
- g_error_free (error);
- }
-}
-
-static void
-do_auth_async (const gchar *action,
- const gchar *result,
- GConfPKCallbackData *data)
-{
- DBusGConnection *bus;
- DBusGProxy *proxy;
-
- /* Now ask the user for auth... */
- bus = get_session_bus ();
- if (bus == NULL)
- return;
-
- proxy = dbus_g_proxy_new_for_name (bus,
- "org.gnome.PolicyKit",
- "/org/gnome/PolicyKit/Manager",
- "org.gnome.PolicyKit.Manager");
-
- dbus_g_proxy_begin_call_with_timeout (proxy,
- "ShowDialog",
- auth_notify,
- _gconf_pk_data_ref (data),
- _gconf_pk_data_unref,
- INT_MAX,
- G_TYPE_STRING, action,
- G_TYPE_UINT, data->transient_parent_xid,
- G_TYPE_INVALID);
-}
+gconf_pk_update_can_set_cache (const gchar *key,
+ gboolean mandatory);
static void
set_key_notify (DBusGProxy *proxy,
@@ -321,6 +215,7 @@ set_key_notify (DBusGProxy *proxy,
GError *error = NULL;
if (dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID)) {
+ gconf_pk_update_can_set_cache (data->key, data->mandatory);
if (data->callback)
data->callback (data->data, NULL);
}
@@ -335,17 +230,6 @@ set_key_notify (DBusGProxy *proxy,
if (data->callback)
data->callback (data->data, NULL);
}
- else if (dbus_g_error_has_name (error, "org.freedesktop.PolicyKit.Error.NotAuthorized")) {
- gchar **tokens;
-
- tokens = g_strsplit (error->message, " ", 2);
- g_error_free (error);
- if (g_strv_length (tokens) == 2)
- do_auth_async (tokens[0], tokens[1], data);
- else
- g_warning ("helper return string malformed");
- g_strfreev (tokens);
- }
else {
if (data->callback)
data->callback (data->data, error);
@@ -360,6 +244,7 @@ set_key_async (GConfPKCallbackData *data)
{
DBusGConnection *bus;
DBusGProxy *proxy;
+ const gchar *call;
gchar *keys[2] = { data->key, NULL };
bus = get_system_bus ();
@@ -371,8 +256,9 @@ set_key_async (GConfPKCallbackData *data)
"/",
"org.gnome.GConf.Defaults");
+ call = data->mandatory ? "SetMandatory" : "SetSystem";
dbus_g_proxy_begin_call_with_timeout (proxy,
- data->call,
+ call,
set_key_notify,
_gconf_pk_data_ref (data),
_gconf_pk_data_unref,
@@ -387,7 +273,6 @@ set_key_async (GConfPKCallbackData *data)
void
gconf_pk_set_default_async (const gchar *key,
- guint transient_parent_xid,
GFunc callback,
gpointer d,
GDestroyNotify notify)
@@ -399,9 +284,8 @@ gconf_pk_set_default_async (const gchar *key,
data = g_slice_new0 (GConfPKCallbackData);
data->ref_count = 1;
- data->call = "SetSystem";
+ data->mandatory = FALSE;
data->key = g_strdup (key);
- data->transient_parent_xid = transient_parent_xid;
data->callback = callback;
data->data = d;
data->notify = notify;
@@ -412,7 +296,6 @@ gconf_pk_set_default_async (const gchar *key,
void
gconf_pk_set_mandatory_async (const gchar *key,
- guint transient_parent_xid,
GFunc callback,
gpointer d,
GDestroyNotify notify)
@@ -424,9 +307,8 @@ gconf_pk_set_mandatory_async (const gchar *key,
data = g_slice_new0 (GConfPKCallbackData);
data->ref_count = 1;
- data->call = "SetMandatory";
+ data->mandatory = TRUE;
data->key = g_strdup (key);
- data->transient_parent_xid = transient_parent_xid;
data->callback = callback;
data->data = d;
data->notify = notify;
diff --git a/src/gconf-policykit.h b/src/gconf-policykit.h
index a535cd2..dd420a8 100644
--- a/src/gconf-policykit.h
+++ b/src/gconf-policykit.h
@@ -27,18 +27,16 @@
#include <glib.h>
-gint gconf_pk_can_set_default (void);
+gint gconf_pk_can_set_default (const gchar *key);
-gint gconf_pk_can_set_mandatory (void);
+gint gconf_pk_can_set_mandatory (const gchar *key);
void gconf_pk_set_default_async (const gchar *key,
- guint transient_parent_xid,
GFunc callback,
gpointer data,
GDestroyNotify notify);
void gconf_pk_set_mandatory_async (const gchar *key,
- guint transient_parent_xid,
GFunc callback,
gpointer data,
GDestroyNotify notify);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]