summaryrefslogtreecommitdiff
path: root/src/xfpm-systemd.c
blob: 4aa3047cf8c56c3d469bf64102dc5fb585e78958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * * Copyright (C) 2009-2011 Ali <aliov@xfce.org>
 * * Copyright (C) 2013 Andreas Müller <schnitzeltony@googlemail.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <gio/gio.h>

#include "xfpm-systemd.h"
#include "xfpm-polkit.h"

static void xfpm_systemd_finalize   (GObject *object);

static void xfpm_systemd_get_property (GObject *object,
                                       guint prop_id,
                                       GValue *value,
                                       GParamSpec *pspec);

struct XfpmSystemdPrivate
{
    gboolean         can_shutdown;
    gboolean         can_restart;
    gboolean         can_suspend;
    gboolean         can_hibernate;
#ifdef ENABLE_POLKIT
    XfpmPolkit      *polkit;
#endif
};

enum
{
    PROP_0,
    PROP_CAN_RESTART,
    PROP_CAN_SHUTDOWN,
    PROP_CAN_SUSPEND,
    PROP_CAN_HIBERNATE,
};

G_DEFINE_TYPE_WITH_PRIVATE (XfpmSystemd, xfpm_systemd, G_TYPE_OBJECT)

#define SYSTEMD_DBUS_NAME               "org.freedesktop.login1"
#define SYSTEMD_DBUS_PATH               "/org/freedesktop/login1"
#define SYSTEMD_DBUS_INTERFACE          "org.freedesktop.login1.Manager"
#define SYSTEMD_REBOOT_ACTION           "Reboot"
#define SYSTEMD_POWEROFF_ACTION         "PowerOff"
#define SYSTEMD_REBOOT_TEST             "org.freedesktop.login1.reboot"
#define SYSTEMD_POWEROFF_TEST           "org.freedesktop.login1.power-off"
#define SYSTEMD_SUSPEND_TEST            "org.freedesktop.login1.suspend"
#define SYSTEMD_HIBERNATE_TEST          "org.freedesktop.login1.hibernate"

static void
xfpm_systemd_class_init (XfpmSystemdClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = xfpm_systemd_finalize;

    object_class->get_property = xfpm_systemd_get_property;

    g_object_class_install_property (object_class,
                                     PROP_CAN_RESTART,
                                     g_param_spec_boolean ("can-restart",
                                                           NULL, NULL,
                                                           FALSE,
                                                           G_PARAM_READABLE));

    g_object_class_install_property (object_class,
                                     PROP_CAN_SHUTDOWN,
                                     g_param_spec_boolean ("can-shutdown",
                                                           NULL, NULL,
                                                           FALSE,
                                                           G_PARAM_READABLE));

    g_object_class_install_property (object_class,
                                     PROP_CAN_SUSPEND,
                                     g_param_spec_boolean ("can-suspend",
                                                           NULL, NULL,
                                                           FALSE,
                                                           G_PARAM_READABLE));

    g_object_class_install_property (object_class,
                                     PROP_CAN_HIBERNATE,
                                     g_param_spec_boolean ("can-hibernate",
                                                           NULL, NULL,
                                                           FALSE,
                                                           G_PARAM_READABLE));
}

static gboolean
xfpm_systemd_can_method (XfpmSystemd  *systemd,
                         gboolean     *can_method,
                         const gchar  *method)
{
    *can_method = FALSE;

#ifdef ENABLE_POLKIT
    *can_method = xfpm_polkit_check_auth(systemd->priv->polkit, method);

    return TRUE;
#endif

    return FALSE;
}

static void
xfpm_systemd_init (XfpmSystemd *systemd)
{
    systemd->priv = xfpm_systemd_get_instance_private (systemd);
    systemd->priv->can_shutdown = FALSE;
    systemd->priv->can_restart  = FALSE;
#ifdef ENABLE_POLKIT
    systemd->priv->polkit = xfpm_polkit_get();
#endif

    xfpm_systemd_can_method (systemd,
                             &systemd->priv->can_shutdown,
                             SYSTEMD_POWEROFF_TEST);
    xfpm_systemd_can_method (systemd,
                             &systemd->priv->can_restart,
                             SYSTEMD_REBOOT_TEST);
    xfpm_systemd_can_method (systemd,
                             &systemd->priv->can_suspend,
                             SYSTEMD_SUSPEND_TEST);
    xfpm_systemd_can_method (systemd,
                             &systemd->priv->can_hibernate,
                             SYSTEMD_HIBERNATE_TEST);
}

static void xfpm_systemd_get_property (GObject *object,
                       guint prop_id,
                       GValue *value,
                       GParamSpec *pspec)
{
    XfpmSystemd *systemd;
    systemd = XFPM_SYSTEMD (object);

    switch (prop_id)
    {
    case PROP_CAN_SHUTDOWN:
        g_value_set_boolean (value, systemd->priv->can_shutdown);
        break;
    case PROP_CAN_RESTART:
        g_value_set_boolean (value, systemd->priv->can_restart);
        break;
    case PROP_CAN_SUSPEND:
        g_value_set_boolean (value, systemd->priv->can_suspend);
        break;
    case PROP_CAN_HIBERNATE:
        g_value_set_boolean (value, systemd->priv->can_hibernate);
        break;
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
            break;
    }
}

static void
xfpm_systemd_finalize (GObject *object)
{
#ifdef ENABLE_POLKIT
    XfpmSystemd *systemd;

    systemd = XFPM_SYSTEMD (object);

    if(systemd->priv->polkit)
    {
        g_object_unref (G_OBJECT (systemd->priv->polkit));
        systemd->priv->polkit = NULL;
    }
#endif

    G_OBJECT_CLASS (xfpm_systemd_parent_class)->finalize (object);
}

XfpmSystemd *
xfpm_systemd_new (void)
{
    static gpointer systemd_obj = NULL;

    if ( G_LIKELY (systemd_obj != NULL ) )
    {
        g_object_ref (systemd_obj);
    }
    else
    {
        systemd_obj = g_object_new (XFPM_TYPE_SYSTEMD, NULL);
        g_object_add_weak_pointer (systemd_obj, &systemd_obj);
    }

    return XFPM_SYSTEMD (systemd_obj);
}

static void
xfpm_systemd_try_method (XfpmSystemd  *systemd,
                         const gchar  *method,
                         GError      **error)
{
    GDBusConnection *bus;
    GError          *local_error = NULL;
    GVariant        *var;

    bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
    if (G_LIKELY (bus != NULL))
    {
        var = g_dbus_connection_call_sync (bus,
                                           SYSTEMD_DBUS_NAME,
                                           SYSTEMD_DBUS_PATH,
                                           SYSTEMD_DBUS_INTERFACE,
                                           method,
                                           g_variant_new ("(b)", TRUE),
                                           G_VARIANT_TYPE ("()"), 0, G_MAXINT, NULL,
                                           &local_error);
        g_variant_unref (var);
        g_object_unref (G_OBJECT (bus));

        if (local_error != NULL)
        {
            g_propagate_error (error, local_error);
        }
    }
}

void xfpm_systemd_shutdown (XfpmSystemd *systemd, GError **error)
{
    xfpm_systemd_try_method (systemd,
                             SYSTEMD_POWEROFF_ACTION,
                             error);
}

void xfpm_systemd_reboot (XfpmSystemd *systemd, GError **error)
{
    xfpm_systemd_try_method (systemd,
                             SYSTEMD_REBOOT_ACTION,
                             error);
}

void xfpm_systemd_sleep (XfpmSystemd *systemd,
                         const gchar *method,
                         GError **error)
{
    xfpm_systemd_try_method (systemd, method, error);
}