~ubuntu-branches/ubuntu/precise/xfce4-power-manager/precise

« back to all changes in this revision

Viewing changes to src/xfpm-console-kit.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2010-12-09 18:28:34 UTC
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: james.westby@ubuntu.com-20101209182834-tjz13qcewqlq19eu
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * * Copyright (C) 2009 Ali <aliov@xfce.org>
 
3
 *
 
4
 * Licensed under the GNU General Public License Version 2
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#include <config.h>
 
23
#endif
 
24
 
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
 
 
29
#include <dbus/dbus-glib.h>
 
30
 
 
31
#include "xfpm-console-kit.h"
 
32
#include "xfpm-dbus-monitor.h"
 
33
 
 
34
 
 
35
static void xfpm_console_kit_finalize   (GObject *object);
 
36
 
 
37
static void xfpm_console_kit_get_property (GObject *object,
 
38
                                           guint prop_id,
 
39
                                           GValue *value,
 
40
                                           GParamSpec *pspec);
 
41
            
 
42
#define XFPM_CONSOLE_KIT_GET_PRIVATE(o) \
 
43
(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_CONSOLE_KIT, XfpmConsoleKitPrivate))
 
44
 
 
45
struct XfpmConsoleKitPrivate
 
46
{
 
47
    DBusGConnection *bus;
 
48
    DBusGProxy      *proxy;
 
49
    
 
50
    XfpmDBusMonitor *monitor;
 
51
    
 
52
    gboolean         can_shutdown;
 
53
    gboolean         can_restart;
 
54
};
 
55
 
 
56
enum
 
57
{
 
58
    PROP_0,
 
59
    PROP_CAN_RESTART,
 
60
    PROP_CAN_SHUTDOWN
 
61
};
 
62
 
 
63
G_DEFINE_TYPE (XfpmConsoleKit, xfpm_console_kit, G_TYPE_OBJECT)
 
64
 
 
65
static void
 
66
xfpm_console_kit_get_info (XfpmConsoleKit *console)
 
67
{
 
68
    GError *error = NULL;
 
69
    
 
70
    dbus_g_proxy_call (console->priv->proxy, "CanStop", &error,
 
71
                       G_TYPE_INVALID,
 
72
                       G_TYPE_BOOLEAN, &console->priv->can_shutdown,
 
73
                       G_TYPE_INVALID);
 
74
                       
 
75
    if ( error )
 
76
    {
 
77
        g_warning ("'CanStop' method failed : %s", error->message);
 
78
        g_error_free (error);
 
79
        error = NULL;
 
80
    }
 
81
    
 
82
    dbus_g_proxy_call (console->priv->proxy, "CanRestart", &error,
 
83
                       G_TYPE_INVALID,
 
84
                       G_TYPE_BOOLEAN, &console->priv->can_restart,
 
85
                       G_TYPE_INVALID);
 
86
                       
 
87
    if ( error )
 
88
    {
 
89
        g_warning ("'CanRestart' method failed : %s", error->message);
 
90
        g_error_free (error);
 
91
        error = NULL;
 
92
    }
 
93
    
 
94
}
 
95
 
 
96
static void
 
97
xfpm_console_kit_class_init (XfpmConsoleKitClass *klass)
 
98
{
 
99
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
100
 
 
101
    object_class->finalize = xfpm_console_kit_finalize;
 
102
 
 
103
    object_class->get_property = xfpm_console_kit_get_property;
 
104
    
 
105
    g_object_class_install_property (object_class,
 
106
                                     PROP_CAN_RESTART,
 
107
                                     g_param_spec_boolean ("can-restart",
 
108
                                                           NULL, NULL,
 
109
                                                           FALSE,
 
110
                                                           G_PARAM_READABLE));
 
111
 
 
112
    g_object_class_install_property (object_class,
 
113
                                     PROP_CAN_SHUTDOWN,
 
114
                                     g_param_spec_boolean ("can-shutdown",
 
115
                                                           NULL, NULL,
 
116
                                                           FALSE,
 
117
                                                           G_PARAM_READABLE));
 
118
 
 
119
    g_type_class_add_private (klass, sizeof (XfpmConsoleKitPrivate));
 
120
}
 
121
 
 
122
static void
 
123
xfpm_console_kit_init (XfpmConsoleKit *console)
 
124
{
 
125
    GError *error = NULL;
 
126
    
 
127
    console->priv = XFPM_CONSOLE_KIT_GET_PRIVATE (console);
 
128
    console->priv->can_shutdown = FALSE;
 
129
    console->priv->can_restart  = FALSE;
 
130
    
 
131
    console->priv->bus   = NULL;
 
132
    console->priv->proxy = NULL;
 
133
    
 
134
    console->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
 
135
    
 
136
    if ( error )
 
137
    {
 
138
        g_critical ("Unable to get system bus connection : %s", error->message);
 
139
        g_error_free (error);
 
140
        goto out;
 
141
    }
 
142
    
 
143
    console->priv->proxy = dbus_g_proxy_new_for_name_owner (console->priv->bus,
 
144
                                                            "org.freedesktop.ConsoleKit",
 
145
                                                            "/org/freedesktop/ConsoleKit/Manager",
 
146
                                                            "org.freedesktop.ConsoleKit.Manager",
 
147
                                                            NULL);
 
148
                                                      
 
149
    if ( !console->priv->proxy )
 
150
    {
 
151
        g_warning ("Unable to create proxy for 'org.freedesktop.ConsoleKit'");
 
152
        goto out;
 
153
    }
 
154
    
 
155
    xfpm_console_kit_get_info (console);
 
156
    
 
157
out:
 
158
    ;
 
159
}
 
160
 
 
161
static void xfpm_console_kit_get_property (GObject *object,
 
162
                                           guint prop_id,
 
163
                                           GValue *value,
 
164
                                           GParamSpec *pspec)
 
165
{
 
166
    XfpmConsoleKit *console;
 
167
    console = XFPM_CONSOLE_KIT (object);
 
168
 
 
169
    switch (prop_id)
 
170
    {
 
171
        case PROP_CAN_SHUTDOWN:
 
172
            g_value_set_boolean (value, console->priv->can_shutdown);
 
173
            break;
 
174
        case PROP_CAN_RESTART:
 
175
            g_value_set_boolean (value, console->priv->can_restart);
 
176
            break;
 
177
        default:
 
178
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
179
            break;
 
180
    }
 
181
}
 
182
 
 
183
static void
 
184
xfpm_console_kit_finalize (GObject *object)
 
185
{
 
186
    XfpmConsoleKit *console;
 
187
 
 
188
    console = XFPM_CONSOLE_KIT (object);
 
189
    
 
190
    if ( console->priv->bus )
 
191
        dbus_g_connection_unref (console->priv->bus);
 
192
        
 
193
    if ( console->priv->proxy )
 
194
        g_object_unref (console->priv->proxy);
 
195
 
 
196
    G_OBJECT_CLASS (xfpm_console_kit_parent_class)->finalize (object);
 
197
}
 
198
 
 
199
XfpmConsoleKit *
 
200
xfpm_console_kit_new (void)
 
201
{
 
202
    static gpointer console_obj = NULL;
 
203
    
 
204
    if ( G_LIKELY (console_obj != NULL ) )
 
205
    {
 
206
        g_object_ref (console_obj);
 
207
    }
 
208
    else
 
209
    {
 
210
        console_obj = g_object_new (XFPM_TYPE_CONSOLE_KIT, NULL);
 
211
        g_object_add_weak_pointer (console_obj, &console_obj);
 
212
    }
 
213
    
 
214
    return XFPM_CONSOLE_KIT (console_obj);
 
215
}
 
216
 
 
217
void xfpm_console_kit_shutdown (XfpmConsoleKit *console, GError **error)
 
218
{
 
219
    g_return_if_fail (console->priv->proxy != NULL );
 
220
    
 
221
    dbus_g_proxy_call (console->priv->proxy, "Stop", error,
 
222
                       G_TYPE_INVALID,
 
223
                       G_TYPE_BOOLEAN, &console->priv->can_shutdown,
 
224
                       G_TYPE_INVALID);
 
225
}
 
226
 
 
227
void xfpm_console_kit_reboot (XfpmConsoleKit *console, GError **error)
 
228
{
 
229
    g_return_if_fail (console->priv->proxy != NULL );
 
230
    
 
231
    dbus_g_proxy_call (console->priv->proxy, "Restart", error,
 
232
                       G_TYPE_INVALID,
 
233
                       G_TYPE_BOOLEAN, &console->priv->can_shutdown,
 
234
                       G_TYPE_INVALID);
 
235
    
 
236
}