2
* * Copyright (C) 2009-2011 Ali <aliov@xfce.org>
3
* * Copyright (C) 2013 Andreas Müller <schnitzeltony@googlemail.com>
5
* Licensed under the GNU General Public License Version 2
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30
#include <dbus/dbus-glib.h>
31
#include <polkit/polkit.h>
33
#include "xfpm-systemd.h"
36
static void xfpm_systemd_finalize (GObject *object);
38
static void xfpm_systemd_get_property (GObject *object,
43
#define XFPM_SYSTEMD_GET_PRIVATE(o) \
44
(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_SYSTEMD, XfpmSystemdPrivate))
46
struct XfpmSystemdPrivate
48
gboolean can_shutdown;
51
PolkitAuthority *authority;
52
PolkitSubject *subject;
62
G_DEFINE_TYPE (XfpmSystemd, xfpm_systemd, G_TYPE_OBJECT)
64
#define SYSTEMD_DBUS_NAME "org.freedesktop.login1"
65
#define SYSTEMD_DBUS_PATH "/org/freedesktop/login1"
66
#define SYSTEMD_DBUS_INTERFACE "org.freedesktop.login1.Manager"
67
#define SYSTEMD_REBOOT_ACTION "Reboot"
68
#define SYSTEMD_POWEROFF_ACTION "PowerOff"
69
#define SYSTEMD_REBOOT_TEST "org.freedesktop.login1.reboot"
70
#define SYSTEMD_POWEROFF_TEST "org.freedesktop.login1.power-off"
73
xfpm_systemd_class_init (XfpmSystemdClass *klass)
75
GObjectClass *object_class = G_OBJECT_CLASS (klass);
77
object_class->finalize = xfpm_systemd_finalize;
79
object_class->get_property = xfpm_systemd_get_property;
81
g_object_class_install_property (object_class,
83
g_param_spec_boolean ("can-restart",
88
g_object_class_install_property (object_class,
90
g_param_spec_boolean ("can-shutdown",
95
g_type_class_add_private (klass, sizeof (XfpmSystemdPrivate));
99
xfpm_systemd_can_method (XfpmSystemd *systemd,
100
gboolean *can_method,
103
PolkitAuthorizationResult *res;
104
GError *local_error = NULL;
107
res = polkit_authority_check_authorization_sync (systemd->priv->authority,
108
systemd->priv->subject,
111
POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
116
g_critical ("Unable to get %s : %s", method, local_error->message);
117
g_error_free (local_error);
122
*can_method = polkit_authorization_result_get_is_authorized (res) ||
123
polkit_authorization_result_get_is_challenge (res);
124
g_object_unref (G_OBJECT (res));
131
xfpm_systemd_init (XfpmSystemd *systemd)
133
systemd->priv = XFPM_SYSTEMD_GET_PRIVATE (systemd);
134
systemd->priv->authority = polkit_authority_get_sync (NULL, NULL);
135
systemd->priv->subject = polkit_unix_process_new (getpid());
136
systemd->priv->can_shutdown = FALSE;
137
systemd->priv->can_restart = FALSE;
139
if(systemd->priv->authority && systemd->priv->subject)
141
xfpm_systemd_can_method (systemd,
142
&systemd->priv->can_shutdown,
143
SYSTEMD_POWEROFF_TEST);
144
xfpm_systemd_can_method (systemd,
145
&systemd->priv->can_restart,
146
SYSTEMD_REBOOT_TEST);
150
static void xfpm_systemd_get_property (GObject *object,
155
XfpmSystemd *systemd;
156
systemd = XFPM_SYSTEMD (object);
160
case PROP_CAN_SHUTDOWN:
161
g_value_set_boolean (value, systemd->priv->can_shutdown);
163
case PROP_CAN_RESTART:
164
g_value_set_boolean (value, systemd->priv->can_restart);
167
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173
xfpm_systemd_finalize (GObject *object)
175
XfpmSystemd *systemd;
177
systemd = XFPM_SYSTEMD (object);
179
if(systemd->priv->authority)
180
g_object_unref (G_OBJECT (systemd->priv->authority));
181
if(systemd->priv->subject)
182
g_object_unref (G_OBJECT (systemd->priv->subject));
184
G_OBJECT_CLASS (xfpm_systemd_parent_class)->finalize (object);
188
xfpm_systemd_new (void)
190
static gpointer systemd_obj = NULL;
192
if ( G_LIKELY (systemd_obj != NULL ) )
194
g_object_ref (systemd_obj);
198
systemd_obj = g_object_new (XFPM_TYPE_SYSTEMD, NULL);
199
g_object_add_weak_pointer (systemd_obj, &systemd_obj);
202
return XFPM_SYSTEMD (systemd_obj);
206
xfpm_systemd_try_method (XfpmSystemd *systemd,
210
GDBusConnection *bus;
211
GError *local_error = NULL;
213
bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
214
if (G_LIKELY (bus != NULL))
216
g_dbus_connection_call_sync (bus,
219
SYSTEMD_DBUS_INTERFACE,
221
g_variant_new ("(b)", TRUE),
222
NULL, 0, G_MAXINT, NULL,
224
g_object_unref (G_OBJECT (bus));
226
if (local_error != NULL)
228
g_propagate_error (error, local_error);
233
void xfpm_systemd_shutdown (XfpmSystemd *systemd, GError **error)
235
xfpm_systemd_try_method (systemd,
236
SYSTEMD_POWEROFF_ACTION,
240
void xfpm_systemd_reboot (XfpmSystemd *systemd, GError **error)
242
xfpm_systemd_try_method (systemd,
243
SYSTEMD_REBOOT_ACTION,