2
* Copyright (C) 2012 Christian Hesse
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2, or (at your option)
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23
#include <polkit/polkit.h>
24
#include <dbus/dbus-glib.h>
25
#include <dbus/dbus-glib-lowlevel.h>
27
#include <libxfsm/xfsm-util.h>
28
#include <xfce4-session/xfsm-systemd.h>
32
#define SYSTEMD_DBUS_NAME "org.freedesktop.login1"
33
#define SYSTEMD_DBUS_PATH "/org/freedesktop/login1"
34
#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.login1.Manager"
35
#define SYSTEMD_REBOOT_ACTION "Reboot"
36
#define SYSTEMD_POWEROFF_ACTION "PowerOff"
37
#define SYSTEMD_SUSPEND_ACTION "Suspend"
38
#define SYSTEMD_HIBERNATE_ACTION "Hibernate"
39
#define SYSTEMD_REBOOT_TEST "org.freedesktop.login1.reboot"
40
#define SYSTEMD_POWEROFF_TEST "org.freedesktop.login1.power-off"
41
#define SYSTEMD_SUSPEND_TEST "org.freedesktop.login1.suspend"
42
#define SYSTEMD_HIBERNATE_TEST "org.freedesktop.login1.hibernate"
46
static void xfsm_systemd_finalize (GObject *object);
50
struct _XfsmSystemdClass
52
GObjectClass __parent__;
59
PolkitAuthority *authority;
60
PolkitSubject *subject;
65
G_DEFINE_TYPE (XfsmSystemd, xfsm_systemd, G_TYPE_OBJECT)
70
xfsm_systemd_class_init (XfsmSystemdClass *klass)
72
GObjectClass *gobject_class;
74
gobject_class = G_OBJECT_CLASS (klass);
75
gobject_class->finalize = xfsm_systemd_finalize;
81
xfsm_systemd_init (XfsmSystemd *systemd)
83
systemd->authority = polkit_authority_get_sync (NULL, NULL);
84
systemd->subject = polkit_unix_process_new (getpid());
90
xfsm_systemd_finalize (GObject *object)
92
XfsmSystemd *systemd = XFSM_SYSTEMD (object);
94
g_object_unref (G_OBJECT (systemd->authority));
95
g_object_unref (G_OBJECT (systemd->subject));
97
(*G_OBJECT_CLASS (xfsm_systemd_parent_class)->finalize) (object);
103
xfsm_systemd_lock_screen (GError **error)
105
XfconfChannel *channel;
108
channel = xfsm_open_config ();
109
if (xfconf_channel_get_bool (channel, "/shutdown/LockScreen", FALSE))
110
ret = g_spawn_command_line_async ("xflock4", error);
118
xfsm_systemd_can_method (XfsmSystemd *systemd,
119
gboolean *can_method,
123
PolkitAuthorizationResult *res;
124
GError *local_error = NULL;
128
res = polkit_authority_check_authorization_sync (systemd->authority,
132
POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
138
g_propagate_error (error, local_error);
142
*can_method = polkit_authorization_result_get_is_authorized (res)
143
|| polkit_authorization_result_get_is_challenge (res);
145
g_object_unref (G_OBJECT (res));
153
xfsm_systemd_try_method (XfsmSystemd *systemd,
157
GDBusConnection *bus;
158
GError *local_error = NULL;
160
bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
161
if (G_UNLIKELY (bus == NULL))
164
g_dbus_connection_call_sync (bus,
167
SYSTEMD_DBUS_INTERFACE,
169
g_variant_new ("(b)", TRUE),
170
NULL, 0, G_MAXINT, NULL,
173
g_object_unref (G_OBJECT (bus));
175
if (local_error != NULL)
177
g_propagate_error (error, local_error);
187
xfsm_systemd_get (void)
189
static XfsmSystemd *object = NULL;
191
if (G_LIKELY (object != NULL))
193
g_object_ref (G_OBJECT (object));
197
object = g_object_new (XFSM_TYPE_SYSTEMD, NULL);
198
g_object_add_weak_pointer (G_OBJECT (object), (gpointer) &object);
207
xfsm_systemd_try_restart (XfsmSystemd *systemd,
210
return xfsm_systemd_try_method (systemd,
211
SYSTEMD_REBOOT_ACTION,
218
xfsm_systemd_try_shutdown (XfsmSystemd *systemd,
221
return xfsm_systemd_try_method (systemd,
222
SYSTEMD_POWEROFF_ACTION,
229
xfsm_systemd_try_suspend (XfsmSystemd *systemd,
232
if (!xfsm_systemd_lock_screen (error))
235
return xfsm_systemd_try_method (systemd,
236
SYSTEMD_SUSPEND_ACTION,
243
xfsm_systemd_try_hibernate (XfsmSystemd *systemd,
246
if (!xfsm_systemd_lock_screen (error))
249
return xfsm_systemd_try_method (systemd,
250
SYSTEMD_HIBERNATE_ACTION,
257
xfsm_systemd_can_restart (XfsmSystemd *systemd,
258
gboolean *can_restart,
261
return xfsm_systemd_can_method (systemd,
270
xfsm_systemd_can_shutdown (XfsmSystemd *systemd,
271
gboolean *can_shutdown,
274
return xfsm_systemd_can_method (systemd,
276
SYSTEMD_POWEROFF_TEST,
283
xfsm_systemd_can_suspend (XfsmSystemd *systemd,
284
gboolean *can_suspend,
285
gboolean *auth_suspend,
288
gboolean ret = FALSE;
290
ret = xfsm_systemd_can_method (systemd,
292
SYSTEMD_SUSPEND_TEST,
294
*auth_suspend = *can_suspend;
301
xfsm_systemd_can_hibernate (XfsmSystemd *systemd,
302
gboolean *can_hibernate,
303
gboolean *auth_hibernate,
306
gboolean ret = FALSE;
308
ret = xfsm_systemd_can_method (systemd,
310
SYSTEMD_HIBERNATE_TEST,
312
*auth_hibernate = *can_hibernate;