1 | /*
|
---|
2 | * Copyright (C) 2010-2011 Robert Ancell.
|
---|
3 | * Author: Robert Ancell <[email protected]>
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or modify it under
|
---|
6 | * the terms of the GNU Lesser General Public License as published by the Free
|
---|
7 | * Software Foundation; either version 2 or version 3 of the License.
|
---|
8 | * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
|
---|
9 | */
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
13 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
14 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
15 | * a choice of LGPL license versions is made available with the language indicating
|
---|
16 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
17 | * of the LGPL is applied is otherwise unspecified.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "config.h"
|
---|
21 |
|
---|
22 | #include <string.h>
|
---|
23 | #include <gio/gio.h>
|
---|
24 |
|
---|
25 | #include "lightdm/power.h"
|
---|
26 |
|
---|
27 | static GDBusProxy *upower_proxy = NULL;
|
---|
28 | static GDBusProxy *ck_proxy = NULL;
|
---|
29 |
|
---|
30 | static gboolean
|
---|
31 | upower_call_function (const gchar *function, gboolean default_result, GError **error)
|
---|
32 | {
|
---|
33 | GVariant *result;
|
---|
34 | gboolean function_result = FALSE;
|
---|
35 |
|
---|
36 | if (!upower_proxy)
|
---|
37 | {
|
---|
38 | upower_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
|
---|
39 | G_DBUS_PROXY_FLAGS_NONE,
|
---|
40 | NULL,
|
---|
41 | "org.freedesktop.UPower",
|
---|
42 | "/org/freedesktop/UPower",
|
---|
43 | "org.freedesktop.UPower",
|
---|
44 | NULL,
|
---|
45 | error);
|
---|
46 | if (!upower_proxy)
|
---|
47 | return FALSE;
|
---|
48 | }
|
---|
49 |
|
---|
50 | result = g_dbus_proxy_call_sync (upower_proxy,
|
---|
51 | function,
|
---|
52 | NULL,
|
---|
53 | G_DBUS_CALL_FLAGS_NONE,
|
---|
54 | -1,
|
---|
55 | NULL,
|
---|
56 | error);
|
---|
57 | if (!result)
|
---|
58 | return default_result;
|
---|
59 |
|
---|
60 | if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
|
---|
61 | g_variant_get (result, "(b)", &function_result);
|
---|
62 |
|
---|
63 | g_variant_unref (result);
|
---|
64 | return function_result;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * lightdm_get_can_suspend:
|
---|
69 | *
|
---|
70 | * Checks if authorized to do a system suspend.
|
---|
71 | *
|
---|
72 | * Return value: #TRUE if can suspend the system
|
---|
73 | **/
|
---|
74 | gboolean
|
---|
75 | lightdm_get_can_suspend (void)
|
---|
76 | {
|
---|
77 | return upower_call_function ("SuspendAllowed", FALSE, NULL);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * lightdm_suspend:
|
---|
82 | * @error: return location for a #GError, or %NULL
|
---|
83 | *
|
---|
84 | * Triggers a system suspend.
|
---|
85 | *
|
---|
86 | * Return value: #TRUE if suspend initiated.
|
---|
87 | **/
|
---|
88 | gboolean
|
---|
89 | lightdm_suspend (GError **error)
|
---|
90 | {
|
---|
91 | return upower_call_function ("Suspend", TRUE, error);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * lightdm_get_can_hibernate:
|
---|
96 | *
|
---|
97 | * Checks if is authorized to do a system hibernate.
|
---|
98 | *
|
---|
99 | * Return value: #TRUE if can hibernate the system
|
---|
100 | **/
|
---|
101 | gboolean
|
---|
102 | lightdm_get_can_hibernate (void)
|
---|
103 | {
|
---|
104 | return upower_call_function ("HibernateAllowed", FALSE, NULL);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * lightdm_hibernate:
|
---|
109 | * @error: return location for a #GError, or %NULL
|
---|
110 | *
|
---|
111 | * Triggers a system hibernate.
|
---|
112 | *
|
---|
113 | * Return value: #TRUE if hibernate initiated.
|
---|
114 | **/
|
---|
115 | gboolean
|
---|
116 | lightdm_hibernate (GError **error)
|
---|
117 | {
|
---|
118 | return upower_call_function ("Hibernate", TRUE, error);
|
---|
119 | }
|
---|
120 |
|
---|
121 | static gboolean
|
---|
122 | ck_call_function (const gchar *function, gboolean default_result, GError **error)
|
---|
123 | {
|
---|
124 | GVariant *result;
|
---|
125 | gboolean function_result = FALSE;
|
---|
126 |
|
---|
127 | if (!ck_proxy)
|
---|
128 | {
|
---|
129 | ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
|
---|
130 | G_DBUS_PROXY_FLAGS_NONE,
|
---|
131 | NULL,
|
---|
132 | "org.freedesktop.ConsoleKit",
|
---|
133 | "/org/freedesktop/ConsoleKit/Manager",
|
---|
134 | "org.freedesktop.ConsoleKit.Manager",
|
---|
135 | NULL,
|
---|
136 | error);
|
---|
137 | if (!ck_proxy)
|
---|
138 | return FALSE;
|
---|
139 | }
|
---|
140 |
|
---|
141 | result = g_dbus_proxy_call_sync (ck_proxy,
|
---|
142 | function,
|
---|
143 | NULL,
|
---|
144 | G_DBUS_CALL_FLAGS_NONE,
|
---|
145 | -1,
|
---|
146 | NULL,
|
---|
147 | error);
|
---|
148 |
|
---|
149 | if (!result)
|
---|
150 | return default_result;
|
---|
151 |
|
---|
152 | if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
|
---|
153 | g_variant_get (result, "(b)", &function_result);
|
---|
154 |
|
---|
155 | g_variant_unref (result);
|
---|
156 | return function_result;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * lightdm_get_can_restart:
|
---|
161 | *
|
---|
162 | * Checks if is authorized to do a system restart.
|
---|
163 | *
|
---|
164 | * Return value: #TRUE if can restart the system
|
---|
165 | **/
|
---|
166 | gboolean
|
---|
167 | lightdm_get_can_restart (void)
|
---|
168 | {
|
---|
169 | return ck_call_function ("CanRestart", FALSE, NULL);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * lightdm_restart:
|
---|
174 | * @error: return location for a #GError, or %NULL
|
---|
175 | *
|
---|
176 | * Triggers a system restart.
|
---|
177 | *
|
---|
178 | * Return value: #TRUE if restart initiated.
|
---|
179 | **/
|
---|
180 | gboolean
|
---|
181 | lightdm_restart (GError **error)
|
---|
182 | {
|
---|
183 | return ck_call_function ("Restart", TRUE, error);
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * lightdm_get_can_shutdown:
|
---|
188 | *
|
---|
189 | * Checks if is authorized to do a system shutdown.
|
---|
190 | *
|
---|
191 | * Return value: #TRUE if can shutdown the system
|
---|
192 | **/
|
---|
193 | gboolean
|
---|
194 | lightdm_get_can_shutdown (void)
|
---|
195 | {
|
---|
196 | return ck_call_function ("CanStop", FALSE, NULL);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * lightdm_shutdown:
|
---|
201 | * @error: return location for a #GError, or %NULL
|
---|
202 | *
|
---|
203 | * Triggers a system shutdown.
|
---|
204 | *
|
---|
205 | * Return value: #TRUE if shutdown initiated.
|
---|
206 | **/
|
---|
207 | gboolean
|
---|
208 | lightdm_shutdown (GError **error)
|
---|
209 | {
|
---|
210 | return ck_call_function ("Stop", TRUE, error);
|
---|
211 | }
|
---|