diff options
author | Ali Abdallah <ali@ali-xfce.org> | 2009-11-06 13:55:38 +0100 |
---|---|---|
committer | Ali Abdallah <ali@ali-xfce.org> | 2009-11-06 13:55:38 +0100 |
commit | 7d69232b827bfacb160dc2b501588023dac92141 (patch) | |
tree | 171ae2650d2d91001144399efa3320197d6c095a | |
parent | 23116a1fca688d481ed8f7d3448b829d0f54eb32 (diff) | |
download | ixfce4-power-manager-7d69232b827bfacb160dc2b501588023dac92141.tar.gz |
Added support for console kit to use Shutdown and Reboot.
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/xfpm-console-kit.c | 235 | ||||
-rw-r--r-- | src/xfpm-console-kit.h | 59 | ||||
-rw-r--r-- | src/xfpm-dkp.c | 131 | ||||
-rw-r--r-- | src/xfpm-manager.c | 23 |
5 files changed, 439 insertions, 11 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index ca99f278..dbf19cd6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,6 +15,8 @@ xfce4_power_manager_SOURCES = \ xfpm-battery-info.h \ xfpm-xfconf.c \ xfpm-xfconf.h \ + xfpm-console-kit.c \ + xfpm-console-kit.h \ xfpm-idle.c \ xfpm-idle.h \ xfpm-backlight.c \ diff --git a/src/xfpm-console-kit.c b/src/xfpm-console-kit.c new file mode 100644 index 00000000..b7145551 --- /dev/null +++ b/src/xfpm-console-kit.c @@ -0,0 +1,235 @@ +/* + * * Copyright (C) 2009 Ali <aliov@xfce.org> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <dbus/dbus-glib.h> + +#include "xfpm-console-kit.h" +#include "xfpm-dbus-monitor.h" + + +static void xfpm_console_kit_finalize (GObject *object); + +static void xfpm_console_kit_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); + +#define XFPM_CONSOLE_KIT_GET_PRIVATE(o) \ +(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_CONSOLE_KIT, XfpmConsoleKitPrivate)) + +struct XfpmConsoleKitPrivate +{ + DBusGConnection *bus; + DBusGProxy *proxy; + + XfpmDBusMonitor *monitor; + + gboolean can_shutdown; + gboolean can_restart; +}; + +enum +{ + PROP_0, + PROP_CAN_RESTART, + PROP_CAN_SHUTDOWN +}; + +G_DEFINE_TYPE (XfpmConsoleKit, xfpm_console_kit, G_TYPE_OBJECT) + +static void +xfpm_console_kit_get_info (XfpmConsoleKit *console) +{ + GError *error = NULL; + + dbus_g_proxy_call (console->priv->proxy, "CanStop", &error, + G_TYPE_INVALID, + G_TYPE_BOOLEAN, &console->priv->can_shutdown, + G_TYPE_INVALID); + + if ( error ) + { + g_warning ("'CanStop' method failed : %s", error->message); + g_error_free (error); + error = NULL; + } + + dbus_g_proxy_call (console->priv->proxy, "CanRestart", &error, + G_TYPE_INVALID, + G_TYPE_BOOLEAN, &console->priv->can_restart, + G_TYPE_INVALID); + + if ( error ) + { + g_warning ("'CanRestart' method failed : %s", error->message); + g_error_free (error); + error = NULL; + } + +} + +static void +xfpm_console_kit_class_init (XfpmConsoleKitClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = xfpm_console_kit_finalize; + + object_class->get_property = xfpm_console_kit_get_property; + + g_object_class_install_property (object_class, + PROP_CAN_RESTART, + g_param_spec_boolean ("can-restart", + NULL, NULL, + FALSE, + G_PARAM_READABLE)); + + g_object_class_install_property (object_class, + PROP_CAN_SHUTDOWN, + g_param_spec_boolean ("can-shutdown", + NULL, NULL, + FALSE, + G_PARAM_READABLE)); + + g_type_class_add_private (klass, sizeof (XfpmConsoleKitPrivate)); +} + +static void +xfpm_console_kit_init (XfpmConsoleKit *console) +{ + GError *error = NULL; + + console->priv = XFPM_CONSOLE_KIT_GET_PRIVATE (console); + console->priv->can_shutdown = FALSE; + console->priv->can_restart = FALSE; + + console->priv->bus = NULL; + console->priv->proxy = NULL; + + console->priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); + + if ( error ) + { + g_critical ("Unable to get system bus connection : %s", error->message); + g_error_free (error); + goto out; + } + + console->priv->proxy = dbus_g_proxy_new_for_name (console->priv->bus, + "org.freedesktop.ConsoleKit", + "/org/freedesktop/ConsoleKit", + "org.freedesktop.ConsoleKit"); + + if ( !console->priv->proxy ) + { + g_warning ("Unable to create proxy for 'org.freedesktop.ConsoleKit'"); + goto out; + } + + xfpm_console_kit_get_info (console); + +out: + ; +} + +static void xfpm_console_kit_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + XfpmConsoleKit *console; + console = XFPM_CONSOLE_KIT (object); + + switch (prop_id) + { + case PROP_CAN_SHUTDOWN: + g_value_set_boolean (value, console->priv->can_shutdown); + break; + case PROP_CAN_RESTART: + g_value_set_boolean (value, console->priv->can_restart); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +xfpm_console_kit_finalize (GObject *object) +{ + XfpmConsoleKit *console; + + console = XFPM_CONSOLE_KIT (object); + + if ( console->priv->bus ) + dbus_g_connection_unref (console->priv->bus); + + if ( console->priv->proxy ) + g_object_unref (console->priv->proxy); + + G_OBJECT_CLASS (xfpm_console_kit_parent_class)->finalize (object); +} + +XfpmConsoleKit * +xfpm_console_kit_new (void) +{ + static gpointer console_obj = NULL; + + if ( G_LIKELY (console_obj != NULL ) ) + { + g_object_ref (console_obj); + } + else + { + console_obj = g_object_new (XFPM_TYPE_CONSOLE_KIT, NULL); + g_object_add_weak_pointer (console_obj, &console_obj); + } + + return XFPM_CONSOLE_KIT (console_obj); +} + +void xfpm_console_kit_shutdown (XfpmConsoleKit *console, GError **error) +{ + g_return_if_fail (console->priv->proxy != NULL ); + + dbus_g_proxy_call (console->priv->proxy, "Stop", error, + G_TYPE_INVALID, + G_TYPE_BOOLEAN, &console->priv->can_shutdown, + G_TYPE_INVALID); +} + +void xfpm_console_kit_reboot (XfpmConsoleKit *console, GError **error) +{ + g_return_if_fail (console->priv->proxy != NULL ); + + dbus_g_proxy_call (console->priv->proxy, "Restart", error, + G_TYPE_INVALID, + G_TYPE_BOOLEAN, &console->priv->can_shutdown, + G_TYPE_INVALID); + +} diff --git a/src/xfpm-console-kit.h b/src/xfpm-console-kit.h new file mode 100644 index 00000000..5d5bc1d9 --- /dev/null +++ b/src/xfpm-console-kit.h @@ -0,0 +1,59 @@ +/* + * * Copyright (C) 2009 Ali <aliov@xfce.org> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __XFPM_CONSOLE_KIT_H +#define __XFPM_CONSOLE_KIT_H + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define XFPM_TYPE_CONSOLE_KIT (xfpm_console_kit_get_type () ) +#define XFPM_CONSOLE_KIT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), XFPM_TYPE_CONSOLE_KIT, XfpmConsoleKit)) +#define XFPM_IS_CONSOLE_KIT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), XFPM_TYPE_CONSOLE_KIT)) + +typedef struct XfpmConsoleKitPrivate XfpmConsoleKitPrivate; + +typedef struct +{ + GObject parent; + XfpmConsoleKitPrivate *priv; + +} XfpmConsoleKit; + +typedef struct +{ + GObjectClass parent_class; + +} XfpmConsoleKitClass; + +GType xfpm_console_kit_get_type (void) G_GNUC_CONST; + +XfpmConsoleKit *xfpm_console_kit_new (void); + +void xfpm_console_kit_shutdown (XfpmConsoleKit *console, + GError **error); + +void xfpm_console_kit_reboot (XfpmConsoleKit *console, + GError **error); + +G_END_DECLS + +#endif /* __XFPM_CONSOLE_KIT_H */ diff --git a/src/xfpm-dkp.c b/src/xfpm-dkp.c index e2ad3e21..1dc8b441 100644 --- a/src/xfpm-dkp.c +++ b/src/xfpm-dkp.c @@ -37,6 +37,8 @@ #include "xfpm-battery.h" #include "xfpm-xfconf.h" #include "xfpm-notify.h" +#include "xfpm-errors.h" +#include "xfpm-console-kit.h" #include "xfpm-inhibit.h" #include "xfpm-polkit.h" #include "xfpm-network-manager.h" @@ -69,6 +71,7 @@ struct XfpmDkpPrivate GHashTable *hash; + XfpmConsoleKit *console; XfpmInhibit *inhibit; XfpmXfconf *conf; gboolean inhibited; @@ -81,6 +84,7 @@ struct XfpmDkpPrivate gboolean auth_hibernate; /* Properties */ + gboolean on_low_battery; gboolean lid_is_present; gboolean lid_is_closed; gboolean on_battery; @@ -94,6 +98,7 @@ struct XfpmDkpPrivate enum { PROP_0, + PROP_ON_LOW_BATTERY, PROP_ON_BATTERY, PROP_AUTH_SUSPEND, PROP_AUTH_HIBERNATE, @@ -587,6 +592,12 @@ xfpm_dkp_notify_action_callback (NotifyNotification *n, gchar *action, XfpmDkp * static void xfpm_dkp_add_actions_to_notification (XfpmDkp *dkp, NotifyNotification *n) { + gboolean can_shutdown; + + g_object_get (G_OBJECT (dkp->priv->console), + "can-shutdown", &can_shutdown, + NULL); + if ( dkp->priv->can_hibernate && dkp->priv->auth_hibernate ) { xfpm_notify_add_action_to_notification( @@ -609,13 +620,14 @@ xfpm_dkp_add_actions_to_notification (XfpmDkp *dkp, NotifyNotification *n) dkp); } - xfpm_notify_add_action_to_notification( - dkp->priv->notify, - n, - "Shutdown", - _("Shutdown the system"), - (NotifyActionCallback)xfpm_dkp_notify_action_callback, - dkp); + if (can_shutdown ) + xfpm_notify_add_action_to_notification( + dkp->priv->notify, + n, + "Shutdown", + _("Shutdown the system"), + (NotifyActionCallback)xfpm_dkp_notify_action_callback, + dkp); } static void @@ -656,6 +668,11 @@ xfpm_dkp_show_critical_action_gtk (XfpmDkp *dkp) GtkWidget *img; GtkWidget *cancel; const gchar *message; + gboolean can_shutdown; + + g_object_get (G_OBJECT (dkp->priv->console), + "can-shutdown", &can_shutdown, + NULL); message = _("System is running on low power. "\ "Save your work to avoid losing data"); @@ -695,6 +712,7 @@ xfpm_dkp_show_critical_action_gtk (XfpmDkp *dkp) G_CALLBACK (xfpm_dkp_suspend_clicked), dkp); } + if ( can_shutdown ) { GtkWidget *shutdown; @@ -788,9 +806,17 @@ xfpm_dkp_battery_charge_changed_cb (XfpmBattery *battery, XfpmDkp *dkp) if ( current_charge == XFPM_BATTERY_CHARGE_CRITICAL && dkp->priv->on_battery) { xfpm_dkp_system_on_low_power (dkp, battery); + dkp->priv->on_low_battery = TRUE; + g_signal_emit (G_OBJECT (dkp), signals [LOW_BATTERY_CHANGED], 0, dkp->priv->on_low_battery); return; } + if ( dkp->priv->on_low_battery ) + { + dkp->priv->on_low_battery = FALSE; + g_signal_emit (G_OBJECT (dkp), signals [LOW_BATTERY_CHANGED], 0, dkp->priv->on_low_battery); + } + g_object_get (G_OBJECT (dkp->priv->conf), GENERAL_NOTIFICATION_CFG, ¬ify, NULL); @@ -1063,6 +1089,13 @@ xfpm_dkp_class_init (XfpmDkpClass *klass) G_PARAM_READABLE)); g_object_class_install_property (object_class, + PROP_ON_LOW_BATTERY, + g_param_spec_boolean ("on-low-battery", + NULL, NULL, + FALSE, + G_PARAM_READABLE)); + + g_object_class_install_property (object_class, PROP_AUTH_SUSPEND, g_param_spec_boolean ("auth-suspend", NULL, NULL, @@ -1113,6 +1146,7 @@ xfpm_dkp_init (XfpmDkp *dkp) dkp->priv->lid_is_present = FALSE; dkp->priv->lid_is_closed = FALSE; dkp->priv->on_battery = FALSE; + dkp->priv->on_low_battery = FALSE; dkp->priv->daemon_version = NULL; dkp->priv->can_suspend = FALSE; dkp->priv->can_hibernate = FALSE; @@ -1123,6 +1157,7 @@ xfpm_dkp_init (XfpmDkp *dkp) dkp->priv->inhibit = xfpm_inhibit_new (); dkp->priv->notify = xfpm_notify_new (); dkp->priv->conf = xfpm_xfconf_new (); + dkp->priv->console = xfpm_console_kit_new (); #ifdef HAVE_POLKIT dkp->priv->polkit = xfpm_polkit_get (); g_signal_connect_swapped (dkp->priv->polkit, "auth-changed", @@ -1236,6 +1271,7 @@ xfpm_dkp_finalize (GObject *object) g_object_unref (dkp->priv->inhibit); g_object_unref (dkp->priv->notify); g_object_unref (dkp->priv->conf); + g_object_unref (dkp->priv->console); dbus_g_connection_unref (dkp->priv->bus); @@ -1386,24 +1422,88 @@ xfpm_dkp_dbus_init (XfpmDkp *dkp) static gboolean xfpm_dkp_dbus_shutdown (XfpmDkp *dkp, GError **error) { + gboolean can_reboot; + + g_object_get (G_OBJECT (dkp->priv->console), + "can-shutdown", &can_reboot, + NULL); + + if ( !can_reboot) + { + g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED, + _("Permission denied")); + return FALSE; + } + + xfpm_console_kit_shutdown (dkp->priv->console, error); + return TRUE; } static gboolean xfpm_dkp_dbus_reboot (XfpmDkp *dkp, GError **error) { + gboolean can_reboot; + + g_object_get (G_OBJECT (dkp->priv->console), + "can-reboot", &can_reboot, + NULL); + + if ( !can_reboot) + { + g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED, + _("Permission denied")); + return FALSE; + } + + xfpm_console_kit_reboot (dkp->priv->console, error); + return TRUE; } static gboolean xfpm_dkp_dbus_hibernate (XfpmDkp * dkp, GError **error) { + if ( !dkp->priv->auth_suspend ) + { + g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED, + _("Permission denied")); + return FALSE; + + } + + if (!dkp->priv->can_hibernate ) + { + g_set_error (error, XFPM_ERROR, XFPM_ERROR_NO_HARDWARE_SUPPORT, + _("Suspend not supported")); + return FALSE; + } + + xfpm_dkp_sleep (dkp, "Hibernate", FALSE); + return TRUE; } static gboolean xfpm_dkp_dbus_suspend (XfpmDkp * dkp, GError ** error) { + if ( !dkp->priv->auth_suspend ) + { + g_set_error (error, XFPM_ERROR, XFPM_ERROR_PERMISSION_DENIED, + _("Permission denied")); + return FALSE; + + } + + if (!dkp->priv->can_suspend ) + { + g_set_error (error, XFPM_ERROR, XFPM_ERROR_NO_HARDWARE_SUPPORT, + _("Suspend not supported")); + return FALSE; + } + + xfpm_dkp_sleep (dkp, "Suspend", FALSE); + return TRUE; } @@ -1411,13 +1511,20 @@ static gboolean xfpm_dkp_dbus_can_reboot (XfpmDkp * dkp, gboolean * OUT_can_reboot, GError ** error) { + g_object_get (G_OBJECT (dkp->priv->console), + "can-reboot", OUT_can_reboot, + NULL); + return TRUE; } static gboolean xfpm_dkp_dbus_can_shutdown (XfpmDkp * dkp, - gboolean * OUT_can_reboot, + gboolean * OUT_can_shutdown, GError ** error) { + g_object_get (G_OBJECT (dkp->priv->console), + "can-shutdown", OUT_can_shutdown, + NULL); return TRUE; } @@ -1425,6 +1532,7 @@ static gboolean xfpm_dkp_dbus_can_hibernate (XfpmDkp * dkp, gboolean * OUT_can_hibernate, GError ** error) { + *OUT_can_hibernate = dkp->priv->can_hibernate; return TRUE; } @@ -1432,6 +1540,8 @@ static gboolean xfpm_dkp_dbus_can_suspend (XfpmDkp * dkp, gboolean * OUT_can_suspend, GError ** error) { + *OUT_can_suspend = dkp->priv->can_suspend; + return TRUE; } @@ -1439,6 +1549,7 @@ static gboolean xfpm_dkp_dbus_get_power_save_status (XfpmDkp * dkp, gboolean * OUT_save_power, GError ** error) { + //FIXME return TRUE; } @@ -1446,6 +1557,8 @@ static gboolean xfpm_dkp_dbus_get_on_battery (XfpmDkp * dkp, gboolean * OUT_on_battery, GError ** error) { + *OUT_on_battery = dkp->priv->on_battery; + return TRUE; } @@ -1453,5 +1566,7 @@ static gboolean xfpm_dkp_dbus_get_low_battery (XfpmDkp * dkp, gboolean * OUT_low_battery, GError ** error) { + *OUT_low_battery = dkp->priv->on_low_battery; + return TRUE; } diff --git a/src/xfpm-manager.c b/src/xfpm-manager.c index 38b41da7..a209d110 100644 --- a/src/xfpm-manager.c +++ b/src/xfpm-manager.c @@ -42,6 +42,7 @@ #include "xfpm-dbus.h" #include "xfpm-dpms.h" #include "xfpm-manager.h" +#include "xfpm-console-kit.h" #include "xfpm-button.h" #include "xfpm-backlight.h" #include "xfpm-config.h" @@ -75,6 +76,7 @@ struct XfpmManagerPrivate XfpmButton *button; XfpmXfconf *conf; XfpmBacklight *backlight; + XfpmConsoleKit *console; #ifdef HAVE_DPMS XfpmDpms *dpms; #endif @@ -119,6 +121,7 @@ xfpm_manager_finalize (GObject *object) g_object_unref (manager->priv->button); g_object_unref (manager->priv->conf); g_object_unref (manager->priv->client); + g_object_unref (manager->priv->console); g_timer_destroy (manager->priv->timer); #ifdef HAVE_DPMS @@ -171,9 +174,17 @@ xfpm_manager_reserve_names (XfpmManager *manager) static void xfpm_manager_shutdown (XfpmManager *manager) { - //FIXME, try other solutions. - if ( manager->priv->session_managed ) - xfce_sm_client_request_shutdown (manager->priv->client, XFCE_SM_CLIENT_SHUTDOWN_HINT_HALT); + GError *error = NULL; + xfpm_console_kit_shutdown (manager->priv->console, &error ); + + if ( error ) + { + g_warning ("Failed to shutdown the system : %s", error->message); + g_error_free (error); + /* Try with the session then */ + if ( manager->priv->session_managed ) + xfce_sm_client_request_shutdown (manager->priv->client, XFCE_SM_CLIENT_SHUTDOWN_HINT_HALT); + } } static void @@ -366,6 +377,7 @@ void xfpm_manager_start (XfpmManager *manager) manager->priv->dkp = xfpm_dkp_get (); manager->priv->button = xfpm_button_new (); manager->priv->conf = xfpm_xfconf_new (); + manager->priv->console = xfpm_console_kit_new (); manager->priv->backlight = xfpm_backlight_new (); @@ -477,10 +489,14 @@ static gboolean xfpm_manager_dbus_get_config (XfpmManager *manager, gboolean has_power_button = FALSE; gboolean has_battery = TRUE; gboolean has_lcd_brightness = TRUE; + gboolean can_shutdown = TRUE; gboolean has_lid = FALSE; *OUT_config = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + g_object_get (G_OBJECT (manager->priv->console), + "can-shutdown", &can_shutdown, + NULL); g_object_get (G_OBJECT (manager->priv->dkp), "auth-suspend", &auth_suspend, @@ -509,6 +525,7 @@ static gboolean xfpm_manager_dbus_get_config (XfpmManager *manager, g_hash_table_insert (*OUT_config, g_strdup ("auth-hibernate"), g_strdup (xfpm_bool_to_string (auth_hibernate))); g_hash_table_insert (*OUT_config, g_strdup ("can-suspend"), g_strdup (xfpm_bool_to_string (can_suspend))); g_hash_table_insert (*OUT_config, g_strdup ("can-hibernate"), g_strdup (xfpm_bool_to_string (can_hibernate))); + g_hash_table_insert (*OUT_config, g_strdup ("can-shutdown"), g_strdup (xfpm_bool_to_string (can_shutdown))); g_hash_table_insert (*OUT_config, g_strdup ("has-battery"), g_strdup (xfpm_bool_to_string (has_battery))); g_hash_table_insert (*OUT_config, g_strdup ("has-lid"), g_strdup (xfpm_bool_to_string (has_lid))); |