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 | #include "config.h"
|
---|
12 |
|
---|
13 | #include <string.h>
|
---|
14 | #include <gio/gio.h>
|
---|
15 |
|
---|
16 | #include "lightdm/power.h"
|
---|
17 |
|
---|
18 | static GDBusProxy *upower_proxy = NULL;
|
---|
19 | static GDBusProxy *ck_proxy = NULL;
|
---|
20 |
|
---|
21 | static gboolean
|
---|
22 | upower_call_function (const gchar *function, gboolean default_result, GError **error)
|
---|
23 | {
|
---|
24 | GVariant *result;
|
---|
25 | gboolean function_result = FALSE;
|
---|
26 |
|
---|
27 | if (!upower_proxy)
|
---|
28 | {
|
---|
29 | upower_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
|
---|
30 | G_DBUS_PROXY_FLAGS_NONE,
|
---|
31 | NULL,
|
---|
32 | "org.freedesktop.UPower",
|
---|
33 | "/org/freedesktop/UPower",
|
---|
34 | "org.freedesktop.UPower",
|
---|
35 | NULL,
|
---|
36 | error);
|
---|
37 | if (!upower_proxy)
|
---|
38 | return FALSE;
|
---|
39 | }
|
---|
40 |
|
---|
41 | result = g_dbus_proxy_call_sync (upower_proxy,
|
---|
42 | function,
|
---|
43 | NULL,
|
---|
44 | G_DBUS_CALL_FLAGS_NONE,
|
---|
45 | -1,
|
---|
46 | NULL,
|
---|
47 | error);
|
---|
48 | if (!result)
|
---|
49 | return default_result;
|
---|
50 |
|
---|
51 | if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
|
---|
52 | g_variant_get (result, "(b)", &function_result);
|
---|
53 |
|
---|
54 | g_variant_unref (result);
|
---|
55 | return function_result;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * lightdm_get_can_suspend:
|
---|
60 | *
|
---|
61 | * Checks if authorized to do a system suspend.
|
---|
62 | *
|
---|
63 | * Return value: #TRUE if can suspend the system
|
---|
64 | **/
|
---|
65 | gboolean
|
---|
66 | lightdm_get_can_suspend (void)
|
---|
67 | {
|
---|
68 | return upower_call_function ("SuspendAllowed", FALSE, NULL);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * lightdm_suspend:
|
---|
73 | * @error: return location for a #GError, or %NULL
|
---|
74 | *
|
---|
75 | * Triggers a system suspend.
|
---|
76 | *
|
---|
77 | * Return value: #TRUE if suspend initiated.
|
---|
78 | **/
|
---|
79 | gboolean
|
---|
80 | lightdm_suspend (GError **error)
|
---|
81 | {
|
---|
82 | return upower_call_function ("Suspend", TRUE, error);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * lightdm_get_can_hibernate:
|
---|
87 | *
|
---|
88 | * Checks if is authorized to do a system hibernate.
|
---|
89 | *
|
---|
90 | * Return value: #TRUE if can hibernate the system
|
---|
91 | **/
|
---|
92 | gboolean
|
---|
93 | lightdm_get_can_hibernate (void)
|
---|
94 | {
|
---|
95 | return upower_call_function ("HibernateAllowed", FALSE, NULL);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * lightdm_hibernate:
|
---|
100 | * @error: return location for a #GError, or %NULL
|
---|
101 | *
|
---|
102 | * Triggers a system hibernate.
|
---|
103 | *
|
---|
104 | * Return value: #TRUE if hibernate initiated.
|
---|
105 | **/
|
---|
106 | gboolean
|
---|
107 | lightdm_hibernate (GError **error)
|
---|
108 | {
|
---|
109 | return upower_call_function ("Hibernate", TRUE, error);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static gboolean
|
---|
113 | ck_call_function (const gchar *function, gboolean default_result, GError **error)
|
---|
114 | {
|
---|
115 | GVariant *result;
|
---|
116 | gboolean function_result = FALSE;
|
---|
117 |
|
---|
118 | if (!ck_proxy)
|
---|
119 | {
|
---|
120 | ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
|
---|
121 | G_DBUS_PROXY_FLAGS_NONE,
|
---|
122 | NULL,
|
---|
123 | "org.freedesktop.ConsoleKit",
|
---|
124 | "/org/freedesktop/ConsoleKit/Manager",
|
---|
125 | "org.freedesktop.ConsoleKit.Manager",
|
---|
126 | NULL,
|
---|
127 | error);
|
---|
128 | if (!ck_proxy)
|
---|
129 | return FALSE;
|
---|
130 | }
|
---|
131 |
|
---|
132 | result = g_dbus_proxy_call_sync (ck_proxy,
|
---|
133 | function,
|
---|
134 | NULL,
|
---|
135 | G_DBUS_CALL_FLAGS_NONE,
|
---|
136 | -1,
|
---|
137 | NULL,
|
---|
138 | error);
|
---|
139 |
|
---|
140 | if (!result)
|
---|
141 | return default_result;
|
---|
142 |
|
---|
143 | if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
|
---|
144 | g_variant_get (result, "(b)", &function_result);
|
---|
145 |
|
---|
146 | g_variant_unref (result);
|
---|
147 | return function_result;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * lightdm_get_can_restart:
|
---|
152 | *
|
---|
153 | * Checks if is authorized to do a system restart.
|
---|
154 | *
|
---|
155 | * Return value: #TRUE if can restart the system
|
---|
156 | **/
|
---|
157 | gboolean
|
---|
158 | lightdm_get_can_restart (void)
|
---|
159 | {
|
---|
160 | return ck_call_function ("CanRestart", FALSE, NULL);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * lightdm_restart:
|
---|
165 | * @error: return location for a #GError, or %NULL
|
---|
166 | *
|
---|
167 | * Triggers a system restart.
|
---|
168 | *
|
---|
169 | * Return value: #TRUE if restart initiated.
|
---|
170 | **/
|
---|
171 | gboolean
|
---|
172 | lightdm_restart (GError **error)
|
---|
173 | {
|
---|
174 | return ck_call_function ("Restart", TRUE, error);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * lightdm_get_can_shutdown:
|
---|
179 | *
|
---|
180 | * Checks if is authorized to do a system shutdown.
|
---|
181 | *
|
---|
182 | * Return value: #TRUE if can shutdown the system
|
---|
183 | **/
|
---|
184 | gboolean
|
---|
185 | lightdm_get_can_shutdown (void)
|
---|
186 | {
|
---|
187 | return ck_call_function ("CanStop", FALSE, NULL);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * lightdm_shutdown:
|
---|
192 | * @error: return location for a #GError, or %NULL
|
---|
193 | *
|
---|
194 | * Triggers a system shutdown.
|
---|
195 | *
|
---|
196 | * Return value: #TRUE if shutdown initiated.
|
---|
197 | **/
|
---|
198 | gboolean
|
---|
199 | lightdm_shutdown (GError **error)
|
---|
200 | {
|
---|
201 | return ck_call_function ("Stop", TRUE, error);
|
---|
202 | }
|
---|