1 /* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*-
3 * Copyright (C) 2010-2011 Robert Ancell.
4 * Author: Robert Ancell <robert.ancell@canonical.com>
6 * This program is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
9 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
15 #include "console-kit.h"
18 ck_open_session (GVariantBuilder *parameters)
25 g_return_val_if_fail (parameters != NULL, NULL);
27 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
29 g_warning ("Failed to get system bus: %s", error->message);
30 g_clear_error (&error);
33 result = g_dbus_connection_call_sync (bus,
34 "org.freedesktop.ConsoleKit",
35 "/org/freedesktop/ConsoleKit/Manager",
36 "org.freedesktop.ConsoleKit.Manager",
37 "OpenSessionWithParameters",
38 g_variant_new ("(a(sv))", parameters),
39 G_VARIANT_TYPE ("(s)"),
40 G_DBUS_CALL_FLAGS_NONE,
47 g_warning ("Failed to open CK session: %s", error->message);
48 g_clear_error (&error);
52 g_variant_get (result, "(s)", &cookie);
53 g_variant_unref (result);
54 g_debug ("Opened ConsoleKit session %s", cookie);
60 get_ck_session (GDBusConnection *bus, const gchar *cookie)
66 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
67 result = g_dbus_connection_call_sync (bus,
68 "org.freedesktop.ConsoleKit",
69 "/org/freedesktop/ConsoleKit/Manager",
70 "org.freedesktop.ConsoleKit.Manager",
71 "GetSessionForCookie",
72 g_variant_new ("(s)", cookie),
73 G_VARIANT_TYPE ("(o)"),
74 G_DBUS_CALL_FLAGS_NONE,
80 g_warning ("Error getting ConsoleKit session: %s", error->message);
81 g_clear_error (&error);
85 g_variant_get (result, "(o)", &session_path);
86 g_variant_unref (result);
92 ck_lock_session (const gchar *cookie)
98 g_return_if_fail (cookie != NULL);
100 g_debug ("Locking ConsoleKit session %s", cookie);
102 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
104 g_warning ("Failed to get system bus: %s", error->message);
105 g_clear_error (&error);
109 session_path = get_ck_session (bus, cookie);
114 result = g_dbus_connection_call_sync (bus,
115 "org.freedesktop.ConsoleKit",
117 "org.freedesktop.ConsoleKit.Session",
119 g_variant_new ("()"),
120 G_VARIANT_TYPE ("()"),
121 G_DBUS_CALL_FLAGS_NONE,
126 g_warning ("Error locking ConsoleKit session: %s", error->message);
127 g_clear_error (&error);
129 g_variant_unref (result);
131 g_object_unref (bus);
135 ck_unlock_session (const gchar *cookie)
137 GDBusConnection *bus;
139 GError *error = NULL;
141 g_return_if_fail (cookie != NULL);
143 g_debug ("Unlocking ConsoleKit session %s", cookie);
145 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
147 g_warning ("Failed to get system bus: %s", error->message);
148 g_clear_error (&error);
152 session_path = get_ck_session (bus, cookie);
157 result = g_dbus_connection_call_sync (bus,
158 "org.freedesktop.ConsoleKit",
160 "org.freedesktop.ConsoleKit.Session",
162 g_variant_new ("()"),
163 G_VARIANT_TYPE ("()"),
164 G_DBUS_CALL_FLAGS_NONE,
169 g_warning ("Error unlocking ConsoleKit session: %s", error->message);
170 g_clear_error (&error);
172 g_variant_unref (result);
174 g_object_unref (bus);
178 ck_activate_session (const gchar *cookie)
180 GDBusConnection *bus;
182 GError *error = NULL;
184 g_return_if_fail (cookie != NULL);
186 g_debug ("Activating ConsoleKit session %s", cookie);
188 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
190 g_warning ("Failed to get system bus: %s", error->message);
191 g_clear_error (&error);
195 session_path = get_ck_session (bus, cookie);
200 result = g_dbus_connection_call_sync (bus,
201 "org.freedesktop.ConsoleKit",
203 "org.freedesktop.ConsoleKit.Session",
205 g_variant_new ("()"),
206 G_VARIANT_TYPE ("()"),
207 G_DBUS_CALL_FLAGS_NONE,
212 g_warning ("Error activating ConsoleKit session: %s", error->message);
213 g_clear_error (&error);
215 g_variant_unref (result);
217 g_object_unref (bus);
221 ck_close_session (const gchar *cookie)
223 GDBusConnection *bus;
226 GError *error = NULL;
228 g_return_if_fail (cookie != NULL);
230 g_debug ("Ending ConsoleKit session %s", cookie);
232 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
234 g_warning ("Failed to get system bus: %s", error->message);
235 g_clear_error (&error);
238 result = g_dbus_connection_call_sync (bus,
239 "org.freedesktop.ConsoleKit",
240 "/org/freedesktop/ConsoleKit/Manager",
241 "org.freedesktop.ConsoleKit.Manager",
243 g_variant_new ("(s)", cookie),
244 G_VARIANT_TYPE ("(b)"),
245 G_DBUS_CALL_FLAGS_NONE,
249 g_object_unref (bus);
252 g_warning ("Error ending ConsoleKit session: %s", error->message);
253 g_clear_error (&error);
257 g_variant_get (result, "(b)", &is_closed);
258 g_variant_unref (result);
261 g_warning ("ConsoleKit.Manager.CloseSession() returned false");
265 ck_get_xdg_runtime_dir (const gchar *cookie)
267 GDBusConnection *bus;
269 gchar *runtime_dir = NULL;
270 GError *error = NULL;
272 g_return_val_if_fail (cookie != NULL, NULL);
274 g_debug ("Getting XDG_RUNTIME_DIR from ConsoleKit for session %s", cookie);
276 bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
278 g_warning ("Failed to get system bus: %s", error->message);
279 g_clear_error (&error);
283 session_path = get_ck_session (bus, cookie);
288 result = g_dbus_connection_call_sync (bus,
289 "org.freedesktop.ConsoleKit",
291 "org.freedesktop.ConsoleKit.Session",
293 g_variant_new ("()"),
294 G_VARIANT_TYPE ("(s)"),
295 G_DBUS_CALL_FLAGS_NONE,
300 g_warning ("Error getting XDG_RUNTIME_DIR from ConsoleKit: %s", error->message);
301 g_clear_error (&error);
305 g_variant_get (result, "(s)", &runtime_dir);
306 g_variant_unref (result);
307 g_debug ("ConsoleKit XDG_RUNTIME_DIR is %s", runtime_dir);
310 g_object_unref (bus);