Back to index

lightdm  1.3.2
power.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010-2011 Robert Ancell.
00003  * Author: Robert Ancell <robert.ancell@canonical.com>
00004  *
00005  * This library is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Lesser General Public License as published by the Free
00007  * Software Foundation; either version 3 of the License, or (at your option) any
00008  * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
00009  * license.
00010  */
00011 
00012 #include <config.h>
00013 
00014 #include <string.h>
00015 #include <gio/gio.h>
00016 
00017 #include "lightdm/power.h"
00018 
00019 static GDBusProxy *upower_proxy = NULL;
00020 static GDBusProxy *ck_proxy = NULL;
00021 
00022 static gboolean
00023 upower_call_function (const gchar *function, gboolean default_result, GError **error)
00024 {
00025     GVariant *result;
00026     gboolean function_result = FALSE;
00027 
00028     if (!upower_proxy)
00029     {
00030         upower_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
00031                                                       G_DBUS_PROXY_FLAGS_NONE,
00032                                                       NULL,
00033                                                       "org.freedesktop.UPower",
00034                                                       "/org/freedesktop/UPower",
00035                                                       "org.freedesktop.UPower",
00036                                                       NULL,
00037                                                       error);
00038         if (!upower_proxy)
00039             return FALSE;
00040     }
00041 
00042     result = g_dbus_proxy_call_sync (upower_proxy,
00043                                      function,
00044                                      NULL,
00045                                      G_DBUS_CALL_FLAGS_NONE,
00046                                      -1,
00047                                      NULL,
00048                                      error);
00049     if (!result)
00050         return default_result;
00051 
00052     if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
00053         g_variant_get (result, "(b)", &function_result);
00054 
00055     g_variant_unref (result);
00056     return function_result;
00057 }
00058 
00066 gboolean
00067 lightdm_get_can_suspend (void)
00068 {
00069     return upower_call_function ("SuspendAllowed", FALSE, NULL);
00070 }
00071 
00080 gboolean
00081 lightdm_suspend (GError **error)
00082 {
00083     return upower_call_function ("Suspend", TRUE, error);
00084 }
00085 
00093 gboolean
00094 lightdm_get_can_hibernate (void)
00095 {
00096     return upower_call_function ("HibernateAllowed", FALSE, NULL);
00097 }
00098 
00107 gboolean
00108 lightdm_hibernate (GError **error)
00109 {
00110     return upower_call_function ("Hibernate", TRUE, error);
00111 }
00112 
00113 static gboolean
00114 ck_call_function (const gchar *function, gboolean default_result, GError **error)
00115 {
00116     GVariant *result;
00117     gboolean function_result = FALSE;
00118 
00119     if (!ck_proxy)
00120     {
00121         ck_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
00122                                                   G_DBUS_PROXY_FLAGS_NONE,
00123                                                   NULL,
00124                                                   "org.freedesktop.ConsoleKit",
00125                                                   "/org/freedesktop/ConsoleKit/Manager",
00126                                                   "org.freedesktop.ConsoleKit.Manager",
00127                                                   NULL,
00128                                                   error);
00129         if (!ck_proxy)
00130             return FALSE;
00131     }
00132 
00133     result = g_dbus_proxy_call_sync (ck_proxy,
00134                                      function,
00135                                      NULL,
00136                                      G_DBUS_CALL_FLAGS_NONE,
00137                                      -1,
00138                                      NULL,
00139                                      error);
00140 
00141     if (!result)
00142         return default_result;
00143 
00144     if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(b)")))
00145         g_variant_get (result, "(b)", &function_result);
00146 
00147     g_variant_unref (result);
00148     return function_result;
00149 }
00150 
00158 gboolean
00159 lightdm_get_can_restart (void)
00160 {
00161     return ck_call_function ("CanRestart", FALSE, NULL);
00162 }
00163 
00172 gboolean
00173 lightdm_restart (GError **error)
00174 {
00175     return ck_call_function ("Restart", TRUE, error);
00176 }
00177 
00185 gboolean
00186 lightdm_get_can_shutdown (void)
00187 {
00188     return ck_call_function ("CanStop", FALSE, NULL);
00189 }
00190 
00199 gboolean
00200 lightdm_shutdown (GError **error)
00201 {
00202     return ck_call_function ("Stop", TRUE, error);
00203 }