~ubuntu-branches/ubuntu/karmic/gnome-power-manager/karmic-updates

« back to all changes in this revision

Viewing changes to src/gpm-inhibit.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-06-03 16:06:07 UTC
  • mfrom: (2.1.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20090603160607-r4v2e7h62aqej4jc
Tags: 2.27.1-0ubuntu1
* New upstream release:
  - Add DOAP file.
  - Connect to gnome-session and exit on logout.
  - Change the default lid close on AC policy to be suspend, not blank.
  - Show the processor device if we have capability, not if we have events.
  - Add support for the DeviceKit-power property lid-is-closed so we can now
    run without --enable-legacy-buttons.
  - Convert from libglade to GtkBuilder.
  - Expose graph details as properties on the object, and constrain time to
    user value. (GNOME #581910).
  - Add a composite device to GpmEngine so we can DTRT for multiple primary
    batteries.
  - Lots of bug fixes.
  - Translation updates.
* debian/rules: Drop --enable-legacy-buttons, not necessary any more.
* debian/control.in: Drop libhal-dev and libglade2-dev build dependencies,
  add new libdevkit-power-gobject-dev build dep.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
 
 *
3
 
 * Copyright (C) 2006-2009 Richard Hughes <richard@hughsie.com>
4
 
 *
5
 
 * Licensed under the GNU General Public License Version 2
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <glib.h>
25
 
#include <dbus/dbus-glib.h>
26
 
#include <dbus/dbus-glib-lowlevel.h>
27
 
#include <glib/gi18n.h>
28
 
#include <string.h>
29
 
#include <gconf/gconf-client.h>
30
 
 
31
 
#include "egg-debug.h"
32
 
#include "gpm-common.h"
33
 
#include "gpm-inhibit.h"
34
 
 
35
 
static void     gpm_inhibit_finalize   (GObject         *object);
36
 
 
37
 
#define GPM_INHIBIT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GPM_TYPE_INHIBIT, GpmInhibitPrivate))
38
 
#define GPM_SESSION_MANAGER_SERVICE                     "org.gnome.SessionManager"
39
 
#define GPM_SESSION_MANAGER_PATH                        "/org/gnome/SessionManager"
40
 
#define GPM_SESSION_MANAGER_INTERFACE                   "org.gnome.SessionManager"
41
 
 
42
 
typedef struct
43
 
{
44
 
        gchar                   *application;
45
 
        gchar                   *reason;
46
 
        gchar                   *connection;
47
 
        guint32                  cookie;
48
 
} GpmInhibitData;
49
 
 
50
 
struct GpmInhibitPrivate
51
 
{
52
 
        GSList                  *list;
53
 
        DBusGProxy              *proxy;
54
 
        DBusGProxy              *proxy_session;
55
 
        GConfClient             *conf;
56
 
        gboolean                 ignore_inhibits;
57
 
};
58
 
 
59
 
enum {
60
 
        HAS_INHIBIT_CHANGED,
61
 
        LAST_SIGNAL
62
 
};
63
 
 
64
 
static guint signals [LAST_SIGNAL] = { 0 };
65
 
 
66
 
G_DEFINE_TYPE (GpmInhibit, gpm_inhibit, G_TYPE_OBJECT)
67
 
 
68
 
/**
69
 
 * gpm_inhibit_error_quark:
70
 
 * Return value: Our personal error quark.
71
 
 **/
72
 
GQuark
73
 
gpm_inhibit_error_quark (void)
74
 
{
75
 
        static GQuark quark = 0;
76
 
        if (!quark)
77
 
                quark = g_quark_from_static_string ("gpm_inhibit_error");
78
 
        return quark;
79
 
}
80
 
 
81
 
/**
82
 
 * gpm_inhibit_cookie_compare_func
83
 
 * @a: Pointer to the data to test
84
 
 * @b: Pointer to a cookie to compare
85
 
 *
86
 
 * A GCompareFunc for comparing a cookie to a list.
87
 
 *
88
 
 * Return value: 0 if cookie matches
89
 
 **/
90
 
static gint
91
 
gpm_inhibit_cookie_compare_func (gconstpointer a, gconstpointer b)
92
 
{
93
 
        GpmInhibitData *data;
94
 
        guint32 cookie;
95
 
        data = (GpmInhibitData*) a;
96
 
        cookie = *((guint32*) b);
97
 
        if (cookie == data->cookie)
98
 
                return 0;
99
 
        return 1;
100
 
}
101
 
 
102
 
/**
103
 
 * gpm_inhibit_find_cookie:
104
 
 * @inhibit: This inhibit instance
105
 
 * @cookie: The cookie we are looking for
106
 
 *
107
 
 * Finds the data in the cookie list.
108
 
 *
109
 
 * Return value: The cookie data, or NULL if not found
110
 
 **/
111
 
static GpmInhibitData *
112
 
gpm_inhibit_find_cookie (GpmInhibit *inhibit, guint32 cookie)
113
 
{
114
 
        GpmInhibitData *data;
115
 
        GSList *ret;
116
 
        /* search list */
117
 
        ret = g_slist_find_custom (inhibit->priv->list, &cookie,
118
 
                                   gpm_inhibit_cookie_compare_func);
119
 
        if (ret == NULL)
120
 
                return NULL;
121
 
        data = (GpmInhibitData *) ret->data;
122
 
        return data;
123
 
}
124
 
 
125
 
/**
126
 
 * gpm_inhibit_inhibit:
127
 
 * @application: Application name, e.g. "Nautilus"
128
 
 * @reason: Reason for inhibiting, e.g. "Copying files"
129
 
 *
130
 
 * Allocates a random cookie used to identify the connection, as multiple
131
 
 * inhibit requests can come from one caller sharing a dbus connection.
132
 
 * We need to refcount internally, and data is saved in the GpmInhibitData
133
 
 * struct.
134
 
 *
135
 
 * Return value: a new random cookie.
136
 
 **/
137
 
void
138
 
gpm_inhibit_inhibit (GpmInhibit  *inhibit, const gchar *application, const gchar *reason, DBusGMethodInvocation *context)
139
 
{
140
 
        gboolean ret;
141
 
        GError *error = NULL;
142
 
        const gchar *connection;
143
 
        GpmInhibitData *data;
144
 
        guint inhibit_cookie = 0;
145
 
 
146
 
        /* as we are async, we can get the sender */
147
 
        connection = dbus_g_method_get_sender (context);
148
 
 
149
 
        /* handle where the application does not add required data */
150
 
        if (application == NULL || reason == NULL) {
151
 
                egg_warning ("Recieved Inhibit, but application "
152
 
                             "did not set the parameters correctly");
153
 
                dbus_g_method_return (context, -1);
154
 
                return;
155
 
        }
156
 
 
157
 
        /* if there's no gnome-session, this will be NULL */
158
 
        if (inhibit->priv->proxy_session == NULL)
159
 
                goto skip;
160
 
 
161
 
        /* proxy to gnome-session */
162
 
        ret = dbus_g_proxy_call (inhibit->priv->proxy_session, "Inhibit", &error,
163
 
                                 G_TYPE_STRING, application, /* app_id */
164
 
                                 G_TYPE_UINT, 0, /* toplevel_xid */
165
 
                                 G_TYPE_STRING, reason, /* reason*/
166
 
                                 G_TYPE_UINT, 8, /* flags */
167
 
                                 G_TYPE_INVALID,
168
 
                                 G_TYPE_UINT, &inhibit_cookie,
169
 
                                 G_TYPE_INVALID);
170
 
        if (!ret) {
171
 
                egg_warning ("failed to proxy: %s", error->message);
172
 
                dbus_g_method_return_error (context, error);
173
 
                return;
174
 
        }
175
 
 
176
 
skip:
177
 
        /* seems okay, add to list */
178
 
        data = g_new (GpmInhibitData, 1);
179
 
        data->cookie = inhibit_cookie;
180
 
        data->application = g_strdup (application);
181
 
        data->connection = g_strdup (connection);
182
 
        data->reason = g_strdup (reason);
183
 
 
184
 
        inhibit->priv->list = g_slist_append (inhibit->priv->list, (gpointer) data);
185
 
 
186
 
        egg_debug ("Recieved Inhibit from '%s' (%s) because '%s' saving as #%i",
187
 
                   data->application, data->connection, data->reason, data->cookie);
188
 
 
189
 
        /* only emit event on the first one */
190
 
        if (g_slist_length (inhibit->priv->list) == 1)
191
 
                g_signal_emit (inhibit, signals [HAS_INHIBIT_CHANGED], 0, TRUE);
192
 
 
193
 
        dbus_g_method_return (context, data->cookie);
194
 
}
195
 
 
196
 
/* free one element in GpmInhibitData struct */
197
 
static void
198
 
gpm_inhibit_free_data_object (GpmInhibitData *data)
199
 
{
200
 
        g_free (data->application);
201
 
        g_free (data->reason);
202
 
        g_free (data->connection);
203
 
        g_free (data);
204
 
}
205
 
 
206
 
/**
207
 
 * gpm_inhibit_un_inhibit:
208
 
 * @application: Application name
209
 
 * @cookie: The cookie that we used to register
210
 
 *
211
 
 * Removes a cookie and associated data from the GpmInhibitData struct.
212
 
 **/
213
 
gboolean
214
 
gpm_inhibit_un_inhibit (GpmInhibit *inhibit, guint32 cookie, GError **error)
215
 
{
216
 
        gboolean ret;
217
 
        GpmInhibitData *data;
218
 
 
219
 
        /* Only remove the correct cookie */
220
 
        data = gpm_inhibit_find_cookie (inhibit, cookie);
221
 
        if (data == NULL) {
222
 
                *error = g_error_new (gpm_inhibit_error_quark (),
223
 
                                      GPM_INHIBIT_ERROR_GENERAL,
224
 
                                      "Cannot find registered program for #%i, so "
225
 
                                      "cannot do UnInhibit!", cookie);
226
 
                return FALSE;
227
 
        }
228
 
        egg_debug ("UnInhibit okay #%i", cookie);
229
 
 
230
 
        /* if there's no gnome-session, this will be NULL */
231
 
        if (inhibit->priv->proxy_session == NULL)
232
 
                goto skip;
233
 
 
234
 
        /* proxy to gnome-session */
235
 
        ret = dbus_g_proxy_call (inhibit->priv->proxy_session, "Uninhibit", error,
236
 
                                 G_TYPE_UINT, cookie,
237
 
                                 G_TYPE_INVALID,
238
 
                                 G_TYPE_INVALID);
239
 
        if (!ret) {
240
 
                egg_warning ("failed to proxy: %s", (*error)->message);
241
 
                return FALSE;
242
 
        }
243
 
 
244
 
skip:
245
 
        gpm_inhibit_free_data_object (data);
246
 
 
247
 
        /* remove it from the list */
248
 
        inhibit->priv->list = g_slist_remove (inhibit->priv->list, (gconstpointer) data);
249
 
 
250
 
        /* only emit event on the last one */
251
 
        if (g_slist_length (inhibit->priv->list) == 0)
252
 
                g_signal_emit (inhibit, signals [HAS_INHIBIT_CHANGED], 0, FALSE);
253
 
 
254
 
        return TRUE;
255
 
}
256
 
 
257
 
/**
258
 
 * gpm_inhibit_remove_dbus:
259
 
 * @connection: Connection name
260
 
 * @application: Application name
261
 
 * @cookie: The cookie that we used to register
262
 
 *
263
 
 * Checks to see if the dbus closed session is registered, in which case
264
 
 * unregister it.
265
 
 **/
266
 
static void
267
 
gpm_inhibit_remove_dbus (GpmInhibit *inhibit, const gchar *connection)
268
 
{
269
 
        guint a;
270
 
        GpmInhibitData *data;
271
 
        gboolean ret;
272
 
        GError *error = NULL;
273
 
 
274
 
        /* Remove *any* connections that match the connection */
275
 
        for (a=0; a<g_slist_length (inhibit->priv->list); a++) {
276
 
                data = (GpmInhibitData *) g_slist_nth_data (inhibit->priv->list, a);
277
 
                if (strcmp (data->connection, connection) == 0) {
278
 
                        egg_debug ("Auto-revoked idle inhibit on '%s'.", data->application);
279
 
 
280
 
                        /* if there's no gnome-session, this will be NULL */
281
 
                        if (inhibit->priv->proxy_session == NULL)
282
 
                                goto skip;
283
 
 
284
 
                        /* proxy to gnome-session */
285
 
                        ret = dbus_g_proxy_call (inhibit->priv->proxy_session, "Uninhibit", &error,
286
 
                                                 G_TYPE_UINT, data->cookie,
287
 
                                                 G_TYPE_INVALID,
288
 
                                                 G_TYPE_INVALID);
289
 
                        if (!ret) {
290
 
                                egg_warning ("failed to proxy: %s", error->message);
291
 
                                g_error_free (error);
292
 
                        }
293
 
skip:
294
 
                        gpm_inhibit_free_data_object (data);
295
 
                        /* remove it from the list */
296
 
                        inhibit->priv->list = g_slist_remove (inhibit->priv->list, (gconstpointer) data);
297
 
                }
298
 
        }
299
 
}
300
 
 
301
 
/**
302
 
 * gpm_inhibit_name_owner_changed_cb:
303
 
 **/
304
 
static void
305
 
gpm_inhibit_name_owner_changed_cb (DBusGProxy *proxy, const gchar *name,
306
 
                                   const gchar *prev, const gchar *new,
307
 
                                   GpmInhibit *inhibit)
308
 
{
309
 
        if (strlen (new) == 0)
310
 
                gpm_inhibit_remove_dbus (inhibit, name);
311
 
}
312
 
 
313
 
/**
314
 
 * gpm_inhibit_has_inhibit
315
 
 *
316
 
 * Checks to see if we are being stopped from performing an action.
317
 
 *
318
 
 * TRUE if the action is OK, i.e. we have *not* been inhibited.
319
 
 **/
320
 
gboolean
321
 
gpm_inhibit_has_inhibit (GpmInhibit *inhibit, gboolean *has_inihibit, GError **error)
322
 
{
323
 
        guint length;
324
 
 
325
 
        length = g_slist_length (inhibit->priv->list);
326
 
 
327
 
        if (inhibit->priv->ignore_inhibits) {
328
 
                egg_debug ("Inhibit ignored through gconf policy!");
329
 
                *has_inihibit = FALSE;
330
 
        }
331
 
 
332
 
        /* An inhibit can stop the action */
333
 
        if (length == 0) {
334
 
                egg_debug ("Valid as no inhibitors");
335
 
                *has_inihibit = FALSE;
336
 
        } else {
337
 
                /* we have at least one application blocking the action */
338
 
                egg_debug ("We have %i valid inhibitors", length);
339
 
                *has_inihibit = TRUE;
340
 
        }
341
 
 
342
 
        /* we always return successful for DBUS */
343
 
        return TRUE;
344
 
}
345
 
 
346
 
/**
347
 
 * gpm_inhibit_get_message:
348
 
 *
349
 
 * @message: Description string, e.g. "Nautilus because 'copying files'"
350
 
 * @action: Action we wanted to do, e.g. "suspend"
351
 
 *
352
 
 * Returns a localised message text describing what application has inhibited
353
 
 * the action, and why.
354
 
 *
355
 
 **/
356
 
void
357
 
gpm_inhibit_get_message (GpmInhibit *inhibit, GString *message, const gchar *action)
358
 
{
359
 
        guint a;
360
 
        GpmInhibitData *data;
361
 
        gchar *boldstr;
362
 
        gchar *italicstr;
363
 
 
364
 
        if (g_slist_length (inhibit->priv->list) == 1) {
365
 
                data = (GpmInhibitData *) g_slist_nth_data (inhibit->priv->list, 0);
366
 
                boldstr = g_strdup_printf ("<b>%s</b>", data->application);
367
 
                italicstr = g_strdup_printf ("<b>%s</b>", data->reason);
368
 
                
369
 
                if (strcmp (action, "suspend") == 0) {
370
 
                        /*Translators: first %s is an application name, second %s is the reason*/
371
 
                        g_string_append_printf (message, _("%s has stopped the suspend from taking place: %s."),
372
 
                                boldstr, italicstr);
373
 
 
374
 
                } else if (strcmp (action, "hibernate") == 0) {
375
 
                        /*Translators: first %s is an application name, second %s is the reason*/
376
 
                        g_string_append_printf (message, _("%s has stopped the hibernate from taking place: %s."),
377
 
                                        boldstr, italicstr); 
378
 
 
379
 
                } else if (strcmp (action, "policy action") == 0) {
380
 
                        /*Translators: first %s is an application name, second %s is the reason*/
381
 
                        g_string_append_printf (message, _("%s has stopped the policy action from taking place: %s."),
382
 
                                        boldstr, italicstr); 
383
 
 
384
 
                } else if (strcmp (action, "reboot") == 0) {
385
 
                        /*Translators: first %s is an application name, second %s is the reason*/
386
 
                        g_string_append_printf (message, _("%s has stopped the reboot from taking place: %s."),
387
 
                                        boldstr, italicstr); 
388
 
 
389
 
                } else if (strcmp (action, "shutdown") == 0) {
390
 
                        /*Translators: first %s is an application name, second %s is the reason*/
391
 
                        g_string_append_printf (message, _("%s has stopped the shutdown from taking place: %s."),
392
 
                                boldstr, italicstr); 
393
 
 
394
 
                } else if (strcmp (action, "timeout action") == 0) {
395
 
                        /*Translators: first %s is an application name, second %s is the reason*/
396
 
                        g_string_append_printf (message, _("%s has stopped the timeout action from taking place: %s."),
397
 
                                boldstr, italicstr); 
398
 
                }
399
 
 
400
 
                g_free (boldstr);
401
 
                g_free (italicstr);
402
 
 
403
 
        } else {
404
 
                if (strcmp (action, "suspend") == 0) {
405
 
                        g_string_append (message, _("Multiple applications have stopped the suspend from taking place."));
406
 
 
407
 
                } else if (strcmp (action, "hibernate") == 0) {
408
 
                        g_string_append (message, _("Multiple applications have stopped the hibernate from taking place."));
409
 
 
410
 
                } else if (strcmp (action, "policy action") == 0) {
411
 
                        g_string_append (message, _("Multiple applications have stopped the policy action from taking place."));
412
 
 
413
 
                } else if (strcmp (action, "reboot") == 0) {
414
 
                        g_string_append (message, _("Multiple applications have stopped the reboot from taking place.")); 
415
 
 
416
 
                } else if (strcmp (action, "shutdown") == 0) {
417
 
                        g_string_append (message, _("Multiple applications have stopped the shutdown from taking place."));
418
 
 
419
 
                } else if (strcmp (action, "timeout action") == 0) {
420
 
                        g_string_append (message, _("Multiple applications have stopped the suspend from taking place."));
421
 
                }
422
 
                
423
 
                for (a=0; a<g_slist_length (inhibit->priv->list); a++) {
424
 
                        data = (GpmInhibitData *) g_slist_nth_data (inhibit->priv->list, a);
425
 
                        g_string_append_printf (message,
426
 
                                                "\n<b>%s</b> : <i>%s</i>",
427
 
                                                data->application, data->reason);
428
 
                }
429
 
        }
430
 
}
431
 
 
432
 
/** intialise the class */
433
 
static void
434
 
gpm_inhibit_class_init (GpmInhibitClass *klass)
435
 
{
436
 
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
437
 
        object_class->finalize = gpm_inhibit_finalize;
438
 
 
439
 
        signals [HAS_INHIBIT_CHANGED] =
440
 
                g_signal_new ("has-inhibit-changed",
441
 
                              G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
442
 
                              G_STRUCT_OFFSET (GpmInhibitClass, has_inhibit_changed),
443
 
                              NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
444
 
                              G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
445
 
        g_type_class_add_private (klass, sizeof (GpmInhibitPrivate));
446
 
}
447
 
 
448
 
/** intialise the object */
449
 
static void
450
 
gpm_inhibit_init (GpmInhibit *inhibit)
451
 
{
452
 
        DBusGConnection *connection;
453
 
        GError *error = NULL;
454
 
 
455
 
        inhibit->priv = GPM_INHIBIT_GET_PRIVATE (inhibit);
456
 
        inhibit->priv->list = NULL;
457
 
        inhibit->priv->conf = gconf_client_get_default ();
458
 
 
459
 
        connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
460
 
        if (error != NULL) {
461
 
                egg_warning ("Cannot connect to bus: %s", error->message);
462
 
                g_error_free (error);
463
 
        }
464
 
        inhibit->priv->proxy = dbus_g_proxy_new_for_name_owner (connection,
465
 
                                                                DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
466
 
                                                                DBUS_INTERFACE_DBUS, &error);
467
 
        if (inhibit->priv->proxy == NULL) {
468
 
                egg_warning ("failed to get proxy: %s", error->message);
469
 
                g_error_free (error);
470
 
                return;
471
 
        }
472
 
        dbus_g_proxy_add_signal (inhibit->priv->proxy, "NameOwnerChanged",
473
 
                                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
474
 
        dbus_g_proxy_connect_signal (inhibit->priv->proxy, "NameOwnerChanged",
475
 
                                     G_CALLBACK (gpm_inhibit_name_owner_changed_cb),
476
 
                                     inhibit, NULL);
477
 
 
478
 
        inhibit->priv->proxy_session = dbus_g_proxy_new_for_name_owner (connection,
479
 
                                                                        GPM_SESSION_MANAGER_SERVICE, GPM_SESSION_MANAGER_PATH,
480
 
                                                                        GPM_SESSION_MANAGER_INTERFACE, &error);
481
 
        if (inhibit->priv->proxy_session == NULL) {
482
 
                egg_warning ("failed to get proxy: %s", error->message);
483
 
                g_error_free (error);
484
 
        }
485
 
 
486
 
        /* Do we ignore inhibit requests? */
487
 
        inhibit->priv->ignore_inhibits = gconf_client_get_bool (inhibit->priv->conf, GPM_CONF_IGNORE_INHIBITS, NULL);
488
 
}
489
 
 
490
 
/** finalise the object */
491
 
static void
492
 
gpm_inhibit_finalize (GObject *object)
493
 
{
494
 
        GpmInhibit *inhibit;
495
 
        guint a;
496
 
        GpmInhibitData *data;
497
 
 
498
 
        g_return_if_fail (object != NULL);
499
 
        g_return_if_fail (GPM_IS_INHIBIT (object));
500
 
 
501
 
        inhibit = GPM_INHIBIT (object);
502
 
        inhibit->priv = GPM_INHIBIT_GET_PRIVATE (inhibit);
503
 
 
504
 
        for (a=0; a<g_slist_length (inhibit->priv->list); a++) {
505
 
                data = (GpmInhibitData *) g_slist_nth_data (inhibit->priv->list, a);
506
 
                gpm_inhibit_free_data_object (data);
507
 
        }
508
 
        g_slist_free (inhibit->priv->list);
509
 
 
510
 
        g_object_unref (inhibit->priv->conf);
511
 
        g_object_unref (inhibit->priv->proxy);
512
 
        if (inhibit->priv->proxy_session != NULL)
513
 
                g_object_unref (inhibit->priv->proxy_session);
514
 
        G_OBJECT_CLASS (gpm_inhibit_parent_class)->finalize (object);
515
 
}
516
 
 
517
 
/** create the object */
518
 
GpmInhibit *
519
 
gpm_inhibit_new (void)
520
 
{
521
 
        GpmInhibit *inhibit;
522
 
        inhibit = g_object_new (GPM_TYPE_INHIBIT, NULL);
523
 
        return GPM_INHIBIT (inhibit);
524
 
}
525
 
 
526
 
/***************************************************************************
527
 
 ***                          MAKE CHECK TESTS                           ***
528
 
 ***************************************************************************/
529
 
#ifdef EGG_TEST
530
 
#include "egg-test.h"
531
 
#include "egg-dbus-proxy.h"
532
 
#include "gpm-common.h"
533
 
 
534
 
/** cookie is returned as an unsigned integer */
535
 
static gboolean
536
 
inhibit (EggDbusProxy       *gproxy,
537
 
         const gchar     *appname,
538
 
         const gchar     *reason,
539
 
         guint           *cookie)
540
 
{
541
 
        GError  *error = NULL;
542
 
        gboolean ret;
543
 
        DBusGProxy *proxy;
544
 
 
545
 
        g_return_val_if_fail (cookie != NULL, FALSE);
546
 
 
547
 
        proxy = egg_dbus_proxy_get_proxy (gproxy);
548
 
        if (proxy == NULL) {
549
 
                g_warning ("not connected");
550
 
                return FALSE;
551
 
        }
552
 
 
553
 
        ret = dbus_g_proxy_call (proxy, "Inhibit", &error,
554
 
                                 G_TYPE_STRING, appname,
555
 
                                 G_TYPE_STRING, reason,
556
 
                                 G_TYPE_INVALID,
557
 
                                 G_TYPE_UINT, cookie,
558
 
                                 G_TYPE_INVALID);
559
 
        if (error) {
560
 
                g_debug ("ERROR: %s", error->message);
561
 
                g_error_free (error);
562
 
                *cookie = 0;
563
 
        }
564
 
        if (!ret) {
565
 
                /* abort as the DBUS method failed */
566
 
                g_warning ("Inhibit failed!");
567
 
        }
568
 
 
569
 
        return ret;
570
 
}
571
 
 
572
 
static gboolean
573
 
uninhibit (EggDbusProxy *gproxy,
574
 
           guint      cookie)
575
 
{
576
 
        GError *error = NULL;
577
 
        gboolean ret;
578
 
        DBusGProxy *proxy;
579
 
 
580
 
        proxy = egg_dbus_proxy_get_proxy (gproxy);
581
 
        if (proxy == NULL) {
582
 
                g_warning ("not connected");
583
 
                return FALSE;
584
 
        }
585
 
 
586
 
        ret = dbus_g_proxy_call (proxy, "UnInhibit", &error,
587
 
                                 G_TYPE_UINT, cookie,
588
 
                                 G_TYPE_INVALID,
589
 
                                 G_TYPE_INVALID);
590
 
        if (error) {
591
 
                egg_debug ("ERROR: %s", error->message);
592
 
                g_error_free (error);
593
 
        }
594
 
        return ret;
595
 
}
596
 
 
597
 
static gboolean
598
 
gpm_inhibit_test_has_inhibit (EggDbusProxy *gproxy, gboolean *has_inhibit)
599
 
{
600
 
        GError  *error = NULL;
601
 
        gboolean ret;
602
 
        DBusGProxy *proxy;
603
 
 
604
 
        proxy = egg_dbus_proxy_get_proxy (gproxy);
605
 
        if (proxy == NULL) {
606
 
                g_warning ("not connected");
607
 
                return FALSE;
608
 
        }
609
 
 
610
 
        ret = dbus_g_proxy_call (proxy, "HasInhibit", &error,
611
 
                                 G_TYPE_INVALID,
612
 
                                 G_TYPE_BOOLEAN, has_inhibit,
613
 
                                 G_TYPE_INVALID);
614
 
        if (error) {
615
 
                g_debug ("ERROR: %s", error->message);
616
 
                g_error_free (error);
617
 
        }
618
 
        if (!ret) {
619
 
                /* abort as the DBUS method failed */
620
 
                g_warning ("HasInhibit failed!");
621
 
        }
622
 
 
623
 
        return ret;
624
 
}
625
 
 
626
 
void
627
 
gpm_inhibit_test (gpointer data)
628
 
{
629
 
        gboolean ret;
630
 
        gboolean valid;
631
 
        guint cookie1 = 0;
632
 
        guint cookie2 = 0;
633
 
        DBusGConnection *connection;
634
 
        EggDbusProxy *gproxy;
635
 
        EggTest *test = (EggTest *) data;
636
 
 
637
 
        if (egg_test_start (test, "GpmInhibit") == FALSE) {
638
 
                return;
639
 
        }
640
 
 
641
 
        gproxy = egg_dbus_proxy_new ();
642
 
        connection = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
643
 
        egg_dbus_proxy_assign (gproxy, connection, GPM_DBUS_SERVICE,
644
 
                               GPM_DBUS_PATH_INHIBIT, GPM_DBUS_INTERFACE_INHIBIT);
645
 
 
646
 
        if (gproxy == NULL) {
647
 
                g_warning ("Unable to get connection to power manager");
648
 
                return;
649
 
        }
650
 
 
651
 
        /************************************************************/
652
 
        egg_test_title (test, "make sure we are not inhibited");
653
 
        ret = gpm_inhibit_test_has_inhibit (gproxy, &valid);
654
 
        if (!ret) {
655
 
                egg_test_failed (test, "Unable to test validity");
656
 
        } else if (valid) {
657
 
                egg_test_failed (test, "Already inhibited");
658
 
        } else {
659
 
                egg_test_success (test, NULL);
660
 
        }
661
 
 
662
 
        /************************************************************/
663
 
        egg_test_title (test, "clear an invalid cookie");
664
 
        ret = uninhibit (gproxy, 123456);
665
 
        if (!ret) {
666
 
                egg_test_success (test, "invalid cookie failed as expected");
667
 
        } else {
668
 
                egg_test_failed (test, "should have rejected invalid cookie");
669
 
        }
670
 
 
671
 
        /************************************************************/
672
 
        egg_test_title (test, "get auto cookie 1");
673
 
        ret = inhibit (gproxy,
674
 
                                  "gnome-power-self-test",
675
 
                                  "test inhibit",
676
 
                                  &cookie1);
677
 
        if (!ret) {
678
 
                egg_test_failed (test, "Unable to inhibit");
679
 
        } else if (cookie1 == 0) {
680
 
                egg_test_failed (test, "Cookie invalid (cookie: %u)", cookie1);
681
 
        } else {
682
 
                egg_test_success (test, "cookie: %u", cookie1);
683
 
        }
684
 
 
685
 
        /************************************************************/
686
 
        egg_test_title (test, "make sure we are auto inhibited");
687
 
        ret = gpm_inhibit_test_has_inhibit (gproxy, &valid);
688
 
        if (!ret) {
689
 
                egg_test_failed (test, "Unable to test validity");
690
 
        } else if (valid) {
691
 
                egg_test_success (test, "inhibited");
692
 
        } else {
693
 
                egg_test_failed (test, "inhibit failed");
694
 
        }
695
 
 
696
 
        /************************************************************/
697
 
        egg_test_title (test, "get cookie 2");
698
 
        ret = inhibit (gproxy,
699
 
                                  "gnome-power-self-test",
700
 
                                  "test inhibit",
701
 
                                  &cookie2);
702
 
        if (!ret) {
703
 
                egg_test_failed (test, "Unable to inhibit");
704
 
        } else if (cookie2 == 0) {
705
 
                egg_test_failed (test, "Cookie invalid (cookie: %u)", cookie2);
706
 
        } else {
707
 
                egg_test_success (test, "cookie: %u", cookie2);
708
 
        }
709
 
 
710
 
        /************************************************************/
711
 
        egg_test_title (test, "clear cookie 1");
712
 
        ret = uninhibit (gproxy, cookie1);
713
 
        if (!ret) {
714
 
                egg_test_failed (test, "cookie failed to clear");
715
 
        } else {
716
 
                egg_test_success (test, NULL);
717
 
        }
718
 
 
719
 
        /************************************************************/
720
 
        egg_test_title (test, "make sure we are still inhibited");
721
 
        ret = gpm_inhibit_test_has_inhibit (gproxy, &valid);
722
 
        if (!ret) {
723
 
                egg_test_failed (test, "Unable to test validity");
724
 
        } else if (valid) {
725
 
                egg_test_success (test, "inhibited");
726
 
        } else {
727
 
                egg_test_failed (test, "inhibit failed");
728
 
        }
729
 
 
730
 
        /************************************************************/
731
 
        egg_test_title (test, "clear cookie 2");
732
 
        ret = uninhibit (gproxy, cookie2);
733
 
        if (!ret) {
734
 
                egg_test_failed (test, "cookie failed to clear");
735
 
        } else {
736
 
                egg_test_success (test, NULL);
737
 
        }
738
 
 
739
 
        g_object_unref (gproxy);
740
 
 
741
 
        egg_test_end (test);
742
 
}
743
 
 
744
 
#endif
745