diff options
author | Robert Ancell <robert.ancell@canonical.com> | 2012-02-28 15:26:28 +1100 |
---|---|---|
committer | Robert Ancell <robert.ancell@canonical.com> | 2012-02-28 15:26:28 +1100 |
commit | 0708d29996aba782160fb251c714c5adb1083d3a (patch) | |
tree | 097b030795e38025cef5fc62989101dcb74583ee /src | |
parent | d3746f35d2d86e808daed04ef08d9b75f3a4af2d (diff) | |
download | lightdm-0708d29996aba782160fb251c714c5adb1083d3a.tar.gz |
Simplify ConsoleKit code
Diffstat (limited to 'src')
-rw-r--r-- | src/console-kit.c | 271 |
1 files changed, 115 insertions, 156 deletions
diff --git a/src/console-kit.c b/src/console-kit.c index 31f85f31..3c261ca1 100644 --- a/src/console-kit.c +++ b/src/console-kit.c @@ -14,60 +14,34 @@ #include "console-kit.h" -static GDBusProxy *ck_proxy = NULL; -static gboolean have_ck_proxy = FALSE; - -static gboolean -load_ck_proxy (void) -{ - if (!have_ck_proxy) - { - gchar *name; - GError *error = NULL; - - ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.ConsoleKit", - "/org/freedesktop/ConsoleKit/Manager", - "org.freedesktop.ConsoleKit.Manager", - NULL, &error); - if (error) - g_warning ("Unable to get connection to ConsoleKit: %s", error->message); - g_clear_error (&error); - - name = g_dbus_proxy_get_name_owner (ck_proxy); - if (!name) - { - g_debug ("org.freedesktop.ConsoleKit does not exist, not registering with ConsoleKit"); - g_object_unref (ck_proxy); - ck_proxy = NULL; - } - g_free (name); - } - - return ck_proxy != NULL; -} - gchar * ck_open_session (GVariantBuilder *parameters) { + GDBusConnection *bus; GVariant *result; - gchar *cookie = NULL; + gchar *cookie; GError *error = NULL; g_return_val_if_fail (parameters != NULL, NULL); - if (!load_ck_proxy ()) - return FALSE; - - result = g_dbus_proxy_call_sync (ck_proxy, - "OpenSessionWithParameters", - g_variant_new ("(a(sv))", parameters), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); + bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (error) + g_warning ("Failed to get system bus: %s", error->message); + g_clear_error (&error); + if (!bus) + return NULL; + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + "/org/freedesktop/ConsoleKit/Manager", + "org.freedesktop.ConsoleKit.Manager", + "OpenSessionWithParameters", + g_variant_new ("(a(sv))", parameters), + G_VARIANT_TYPE ("(s)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + g_object_unref (bus); if (error) g_warning ("Failed to open CK session: %s", error->message); @@ -75,45 +49,40 @@ ck_open_session (GVariantBuilder *parameters) if (!result) return NULL; - if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(s)"))) - g_variant_get (result, "(s)", &cookie); - else - g_warning ("Unexpected response from OpenSessionWithParameters: %s", g_variant_get_type_string (result)); + g_variant_get (result, "(s)", &cookie); g_variant_unref (result); - - if (cookie) - g_debug ("Opened ConsoleKit session %s", cookie); + g_debug ("Opened ConsoleKit session %s", cookie); return cookie; } static gchar * -get_ck_session (const gchar *cookie) +get_ck_session (GDBusConnection *bus, const gchar *cookie) { GVariant *result; gchar *session_path; GError *error = NULL; - if (!load_ck_proxy ()) - return NULL; - - result = g_dbus_proxy_call_sync (ck_proxy, - "GetSessionForCookie", - g_variant_new ("(s)", cookie), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); + bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + "/org/freedesktop/ConsoleKit/Manager", + "org.freedesktop.ConsoleKit.Manager", + "GetSessionForCookie", + g_variant_new ("(s)", cookie), + G_VARIANT_TYPE ("(o)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + g_object_unref (bus); if (error) g_warning ("Error getting ConsoleKit session: %s", error->message); g_clear_error (&error); if (!result) return NULL; - if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(o)"))) - g_variant_get (result, "(o)", &session_path); - else - g_warning ("Unexpected response from GetSessionForCookie: %s", g_variant_get_type_string (result)); + g_variant_get (result, "(o)", &session_path); g_variant_unref (result); return session_path; @@ -122,123 +91,119 @@ get_ck_session (const gchar *cookie) void ck_lock_session (const gchar *cookie) { - GVariant *result; - GDBusProxy *proxy; + GDBusConnection *bus; gchar *session_path; GError *error = NULL; g_return_if_fail (cookie != NULL); - if (!load_ck_proxy ()) - return; - g_debug ("Locking ConsoleKit session %s", cookie); - session_path = get_ck_session (cookie); - if (!session_path) - return; - - proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.ConsoleKit", - session_path, - "org.freedesktop.ConsoleKit.Session", - NULL, &error); - g_free (session_path); - + bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) - g_warning ("Unable to get connection to ConsoleKit session: %s", error->message); + g_warning ("Failed to get system bus: %s", error->message); g_clear_error (&error); - if (!proxy) + if (!bus) return; - result = g_dbus_proxy_call_sync (proxy, - "Lock", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - g_object_unref (proxy); - - if (error) - g_warning ("Error locking ConsoleKit session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); + session_path = get_ck_session (bus, cookie); + if (session_path) + { + GVariant *result; + + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + session_path, + "org.freedesktop.ConsoleKit.Session", + "Lock", + g_variant_new ("()"), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error locking ConsoleKit session: %s", error->message); + g_clear_error (&error); + if (result) + g_variant_unref (result); + } + g_object_unref (bus); } void ck_unlock_session (const gchar *cookie) { - GVariant *result; - GDBusProxy *proxy; + GDBusConnection *bus; gchar *session_path; GError *error = NULL; g_return_if_fail (cookie != NULL); - if (!load_ck_proxy ()) - return; - g_debug ("Unlocking ConsoleKit session %s", cookie); - session_path = get_ck_session (cookie); - if (!session_path) - return; - - proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "org.freedesktop.ConsoleKit", - session_path, - "org.freedesktop.ConsoleKit.Session", - NULL, &error); - g_free (session_path); - + bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); if (error) - g_warning ("Unable to get connection to ConsoleKit session: %s", error->message); + g_warning ("Failed to get system bus: %s", error->message); g_clear_error (&error); - if (!proxy) + if (!bus) return; - result = g_dbus_proxy_call_sync (proxy, - "Unlock", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - g_object_unref (proxy); - - if (error) - g_warning ("Error unlocking ConsoleKit session: %s", error->message); - g_clear_error (&error); - if (result) - g_variant_unref (result); + session_path = get_ck_session (bus, cookie); + if (session_path) + { + GVariant *result; + + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + session_path, + "org.freedesktop.ConsoleKit.Session", + "Unlock", + g_variant_new ("()"), + G_VARIANT_TYPE ("()"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + if (error) + g_warning ("Error unlocking ConsoleKit session: %s", error->message); + g_clear_error (&error); + if (result) + g_variant_unref (result); + } + g_object_unref (bus); } void ck_close_session (const gchar *cookie) { + GDBusConnection *bus; GVariant *result; + gboolean is_closed; GError *error = NULL; g_return_if_fail (cookie != NULL); - if (!load_ck_proxy ()) - return; - g_debug ("Ending ConsoleKit session %s", cookie); - result = g_dbus_proxy_call_sync (ck_proxy, - "CloseSession", - g_variant_new ("(s)", cookie), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); + bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error); + if (error) + g_warning ("Failed to get system bus: %s", error->message); + g_clear_error (&error); + if (!bus) + return; + result = g_dbus_connection_call_sync (bus, + "org.freedesktop.ConsoleKit", + "/org/freedesktop/ConsoleKit/Manager", + "org.freedesktop.ConsoleKit.Manager", + "CloseSession", + g_variant_new ("(s)", cookie), + G_VARIANT_TYPE ("(b)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + g_object_unref (bus); if (error) g_warning ("Error ending ConsoleKit session: %s", error->message); @@ -246,15 +211,9 @@ ck_close_session (const gchar *cookie) if (!result) return; - if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)"))) - { - gboolean is_closed; - g_variant_get (result, "(b)", &is_closed); - if (!is_closed) - g_warning ("ConsoleKit.Manager.CloseSession() returned false"); - } - else - g_warning ("Unexpected response from CloseSession: %s", g_variant_get_type_string (result)); - + g_variant_get (result, "(b)", &is_closed); g_variant_unref (result); + + if (!is_closed) + g_warning ("ConsoleKit.Manager.CloseSession() returned false"); } |