File: gconf-policykit.c

package info (click to toggle)
gconf-editor 3.0.1-6
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,216 kB
  • sloc: sh: 11,060; xml: 7,126; ansic: 5,307; makefile: 209
file content (313 lines) | stat: -rw-r--r-- 8,873 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Vincent Untz <vuntz@gnome.org>
 *
 * Based on set-timezone.c from gnome-panel which was GPLv2+ with this
 * copyright:
 *    Copyright (C) 2007 David Zeuthen <david@fubar.dk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <gio/gio.h>

#include "gconf-policykit.h"

#define CACHE_VALIDITY_SEC 10

static guint
can_set (const gchar *key, gboolean mandatory)
{
        GDBusConnection *bus = NULL;
        GDBusProxy *proxy = NULL;
	const gchar *keys[2];
	const gchar *func;
	GError *error = NULL;
        GVariant *res;
        guint retval = 0;

        bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM,
                              NULL, NULL);
        if (bus == NULL)
                goto out;

        proxy = g_dbus_proxy_new_sync (bus,
                                       G_DBUS_PROXY_FLAGS_NONE,
                                       NULL,
                                       "org.gnome.GConf.Defaults",
                                       "/",
                                       "org.gnome.GConf.Defaults",
                                       NULL, NULL);
        if (proxy == NULL)
                goto out;

	keys[0] = key;
	keys[1] = NULL;
	func = mandatory ? "CanSetMandatory" : "CanSetSystem";

        res = g_dbus_proxy_call_sync (proxy, func,
                                      g_variant_new ("(^as)", keys),
                                      G_DBUS_CALL_FLAGS_NONE,
                                      -1, NULL, &error);

        if (error != NULL) {
    		g_warning ("error calling %s: %s\n", func, error->message);
    		g_error_free (error);
  	} else {
                g_variant_get (res, "(u)", &retval);
        }

out:
        if (bus)
                g_object_unref (bus);

	if (proxy)
		g_object_unref (proxy);

        return retval;
}

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 (const gchar *key, gboolean mandatory)
{
        time_t now;
	GHashTable **cache;
	CacheEntry *entry;

        time (&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 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 (const gchar *key)
{
	return gconf_pk_can_set (key, TRUE);
}

static void
gconf_pk_update_can_set_cache (const gchar *key,
                               gboolean     mandatory)
{
        time_t          now;
	GHashTable **cache;
	CacheEntry *entry;

        time (&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);
	}
        entry->can_set = 2;
        entry->last_refreshed = now;
}

typedef struct {
        gint            ref_count;
        gboolean        mandatory;
        gchar          *key;
        GFunc           callback;
        gpointer        data;
        GDestroyNotify  notify;
} GConfPKCallbackData;

static void set_key_async (GConfPKCallbackData *data);

static void
gconf_pk_update_can_set_cache (const gchar *key,
                               gboolean     mandatory);

static void
on_proxy_method_call (GObject *source,
                      GAsyncResult *res,
                      gpointer user_data)
{
        GConfPKCallbackData *data = user_data;
        GError *error = NULL;

        g_dbus_proxy_call_finish (G_DBUS_PROXY (source), res, &error);

        if (error == NULL) {
		gconf_pk_update_can_set_cache (data->key, data->mandatory);
                if (data->callback)
                        data->callback (data->data, NULL);
        } else {
                if (error->domain == G_DBUS_ERROR &&
                    error->code == G_DBUS_ERROR_NO_REPLY) {
                        /* these errors happen because dbus doesn't
                         * use monotonic clocks
                         */
                        g_warning ("ignoring no-reply error when setting key");
                        g_error_free (error);
                        if (data->callback)
                                data->callback (data->data, NULL);
                }
                else {
                        if (data->callback)
                                data->callback (data->data, error);
                }
        }
}

static void
on_gconf_proxy_created (GObject *source,
                        GAsyncResult *res,
                        gpointer user_data)
{
        GConfPKCallbackData *data = user_data;
        GDBusProxy *proxy;
	const gchar     *call;
        gchar *keys[2] = { data->key, NULL };
        gchar *dummy[2] = { NULL };
        GError *error = NULL;

        proxy = g_dbus_proxy_new_finish (res, &error);

        if (error != NULL) {
                if (data->callback)
                        data->callback (data->data, error);

                return;
        }

	call = data->mandatory ? "SetMandatory" : "SetSystem";
        g_dbus_proxy_call (proxy,
                           call, 
                           g_variant_new ("(^as^as)",
                                          keys, dummy),
                           G_DBUS_CALL_FLAGS_NONE,
                           -1, NULL,
                           on_proxy_method_call, data);
}

static void
on_system_bus_got (GObject *source,
                   GAsyncResult *res,
                   gpointer user_data)
{
        GConfPKCallbackData *data = user_data;
        GDBusConnection *bus;
        GError *error = NULL;

        bus = g_bus_get_finish (res, &error);

        if (error != NULL) {
                if (data->callback)
                        data->callback (data->data, error);

                return;
        }

        g_dbus_proxy_new (bus,
                          G_DBUS_PROXY_FLAGS_NONE,
                          NULL,
                          "org.gnome.GConf.Defaults",
                          "/",
                          "org.gnome.GConf.Defaults",
                          NULL,
                          on_gconf_proxy_created,
                          data);
}

static void
set_key_async (GConfPKCallbackData *data)
{
        g_bus_get (G_BUS_TYPE_SYSTEM,
                   NULL, on_system_bus_got, data);
}

void
gconf_pk_set_default_async (const gchar    *key,
                            GFunc           callback,
                            gpointer        d,
                            GDestroyNotify  notify)
{
        GConfPKCallbackData *data;

        if (key == NULL)
                return;

        data = g_slice_new0 (GConfPKCallbackData);
        data->mandatory = FALSE;
        data->key = g_strdup (key);
        data->callback = callback;
        data->data = d;
        data->notify = notify;

        set_key_async (data);
}

void
gconf_pk_set_mandatory_async (const gchar    *key,
                              GFunc           callback,
                              gpointer        d,
                              GDestroyNotify  notify)
{
        GConfPKCallbackData *data;

        if (key == NULL)
                return;

        data = g_slice_new0 (GConfPKCallbackData);
        data->mandatory = TRUE;
        data->key = g_strdup (key);
        data->callback = callback;
        data->data = d;
        data->notify = notify;

        set_key_async (data);
}