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 <xfce4-session/xfsm-systemd.h>
31
#define SYSTEMD_DBUS_NAME "org.freedesktop.login1"
32
#define SYSTEMD_DBUS_PATH "/org/freedesktop/login1"
33
#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.login1.Manager"
34
#define SYSTEMD_REBOOT_ACTION "Reboot"
35
#define SYSTEMD_POWEROFF_ACTION "PowerOff"
36
#define SYSTEMD_REBOOT_TEST "org.freedesktop.login1.reboot"
37
#define SYSTEMD_POWEROFF_TEST "org.freedesktop.login1.power-off"
41
static void xfsm_systemd_finalize (GObject *object);
45
struct _XfsmSystemdClass
47
GObjectClass __parent__;
54
PolkitAuthority *authority;
55
PolkitSubject *subject;
60
G_DEFINE_TYPE (XfsmSystemd, xfsm_systemd, G_TYPE_OBJECT)
65
xfsm_systemd_class_init (XfsmSystemdClass *klass)
67
GObjectClass *gobject_class;
69
gobject_class = G_OBJECT_CLASS (klass);
70
gobject_class->finalize = xfsm_systemd_finalize;
76
xfsm_systemd_init (XfsmSystemd *systemd)
78
systemd->authority = polkit_authority_get_sync (NULL, NULL);
79
systemd->subject = polkit_unix_process_new (getpid());
85
xfsm_systemd_finalize (GObject *object)
87
XfsmSystemd *systemd = XFSM_SYSTEMD (object);
89
g_object_unref (G_OBJECT (systemd->authority));
90
g_object_unref (G_OBJECT (systemd->subject));
92
(*G_OBJECT_CLASS (xfsm_systemd_parent_class)->finalize) (object);
97
xfsm_systemd_can_method (XfsmSystemd *systemd,
102
PolkitAuthorizationResult *res;
103
GError *local_error = NULL;
107
res = polkit_authority_check_authorization_sync (systemd->authority,
111
POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
117
g_propagate_error (error, local_error);
121
*can_method = polkit_authorization_result_get_is_authorized (res)
122
|| polkit_authorization_result_get_is_challenge (res);
124
g_object_unref (G_OBJECT (res));
132
xfsm_systemd_try_method (XfsmSystemd *systemd,
136
GDBusConnection *bus;
137
GError *local_error = NULL;
139
bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
140
if (G_UNLIKELY (bus == NULL))
143
g_dbus_connection_call_sync (bus,
146
SYSTEMD_DBUS_INTERFACE,
148
g_variant_new ("(b)", TRUE),
149
NULL, 0, G_MAXINT, NULL,
152
g_object_unref (G_OBJECT (bus));
154
if (local_error != NULL)
156
g_propagate_error (error, local_error);
166
xfsm_systemd_get (void)
168
static XfsmSystemd *object = NULL;
170
if (G_LIKELY (object != NULL))
172
g_object_ref (G_OBJECT (object));
176
object = g_object_new (XFSM_TYPE_SYSTEMD, NULL);
177
g_object_add_weak_pointer (G_OBJECT (object), (gpointer) &object);
186
xfsm_systemd_try_restart (XfsmSystemd *systemd,
189
return xfsm_systemd_try_method (systemd,
190
SYSTEMD_REBOOT_ACTION,
197
xfsm_systemd_try_shutdown (XfsmSystemd *systemd,
200
return xfsm_systemd_try_method (systemd,
201
SYSTEMD_POWEROFF_ACTION,
208
xfsm_systemd_can_restart (XfsmSystemd *systemd,
209
gboolean *can_restart,
212
return xfsm_systemd_can_method (systemd,
221
xfsm_systemd_can_shutdown (XfsmSystemd *systemd,
222
gboolean *can_shutdown,
225
return xfsm_systemd_can_method (systemd,
227
SYSTEMD_POWEROFF_TEST,