diff --git a/src/xfpm-dbus-messages.c b/src/xfpm-dbus-messages.c index fd2fdbfce51a26a4be535a6e243147a16a5883ee..c545471381b2ddefb495ce6989a2bdfda5a269b3 100644 --- a/src/xfpm-dbus-messages.c +++ b/src/xfpm-dbus-messages.c @@ -270,3 +270,48 @@ xfpm_dbus_send_nm_message (const gchar *signal) return; } } + +gboolean xfpm_dbus_register_name(DBusConnection *connection) +{ + DBusError error; + + dbus_error_init(&error); + + int ret = + dbus_bus_request_name(connection,XFPM_PM_IFACE,0,&error); + + if ( dbus_error_is_set(&error) ) + { + printf("Error: %s\n",error.message); + dbus_error_free(&error); + return FALSE; + } + + if ( ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER ) + { + return TRUE; + } + + return FALSE; +} + +gboolean xfpm_dbus_release_name(DBusConnection *connection) +{ + DBusError error; + + dbus_error_init(&error); + + int ret = + dbus_bus_release_name(connection,XFPM_PM_IFACE,&error); + + if ( dbus_error_is_set(&error) ) + { + printf("Error: %s\n",error.message); + dbus_error_free(&error); + return FALSE; + } + + if ( ret == -1 ) return FALSE; + + return TRUE; +} diff --git a/src/xfpm-dbus-messages.h b/src/xfpm-dbus-messages.h index 0c0a328bdd816a130bf4ceb5f05a1798d36863d0..6a362c32f14b68e3cfbdcdd97a710a28aac361fe 100644 --- a/src/xfpm-dbus-messages.h +++ b/src/xfpm-dbus-messages.h @@ -38,5 +38,7 @@ gboolean xfpm_dbus_send_message (const gchar *signal); gboolean xfpm_dbus_send_message_with_reply (const gchar *signal,gint *get_reply); gboolean xfpm_dbus_send_customize_message (guint32 socket_id); void xfpm_dbus_send_nm_message (const gchar *signal); +gboolean xfpm_dbus_register_name(DBusConnection *connection); +gboolean xfpm_dbus_release_name(); #endif /* __XFPM_DBUS_MESSAGES */ diff --git a/src/xfpm-driver.c b/src/xfpm-driver.c index 312b1e06dbdd3291559960645ead2c7f35824d4d..b02d506bd0b0fd22146b44053ca99a62377449bc 100644 --- a/src/xfpm-driver.c +++ b/src/xfpm-driver.c @@ -1246,6 +1246,8 @@ xfpm_driver_monitor (XfpmDriver *drv) } + xfpm_dbus_register_name(priv->conn); + xfpm_driver_load_config(drv); _get_system_form_factor(priv); @@ -1254,8 +1256,12 @@ xfpm_driver_monitor (XfpmDriver *drv) xfpm_driver_load_all(drv); g_main_loop_run(priv->loop); - + + xfpm_dbus_release_name(priv->conn); + dbus_connection_remove_filter(priv->conn,xfpm_driver_signal_filter,NULL); + + xfconf_shutdown(); return TRUE; diff --git a/src/xfpm-main.c b/src/xfpm-main.c index 058c6f7be7a9ff2ac2503c21d309398bfee93b7d..d408f8c04c2389e7bba8d62541875d394216ebd1 100644 --- a/src/xfpm-main.c +++ b/src/xfpm-main.c @@ -185,15 +185,30 @@ int main(int argc,char **argv) return EXIT_FAILURE; } + DBusConnection *connection; + DBusError derror; + + dbus_error_init(&derror); + + connection = dbus_bus_get(DBUS_BUS_SESSION, &derror); + + if ( dbus_error_is_set(&derror) ) + { + xfpm_popup_message(_("Xfce power manager"), + _("Unable to run Xfce4 power manager, " \ + "make sure the hardware abstract layer and the message bus daemon "\ + "are running"), + GTK_MESSAGE_ERROR); + g_error(_("Unable to load xfce4 power manager")); + g_print("\n"); + dbus_error_free(&derror); + return EXIT_FAILURE; + } + if ( config ) { - int reply; - if (!xfpm_dbus_send_message_with_reply("Running",&reply)) - { - return EXIT_FAILURE; - } - if ( reply != 1 ) - { + if (!xfpm_dbus_name_has_owner(connection,XFPM_PM_IFACE)) + { g_print(_("Xfce power manager is not running")); g_print("\n"); gboolean ret = @@ -205,56 +220,56 @@ int main(int argc,char **argv) g_spawn_command_line_async("xfce4-power-manager -r",NULL); } return EXIT_SUCCESS; - } - xfpm_dbus_send_customize_message(socket_id); - return EXIT_SUCCESS; - } - + } + else + { + xfpm_dbus_send_customize_message(socket_id); + return EXIT_SUCCESS; + } + } + if ( run ) { - int reply; - if (!xfpm_dbus_send_message_with_reply("Running",&reply)) - { - return EXIT_FAILURE; - } - - if ( reply == 1 ) - { + if (!xfpm_dbus_name_has_owner(connection,XFPM_PM_IFACE)) + { + + XfpmDriver *driver = xfpm_driver_new(); + autostart(); + if (!xfpm_driver_monitor(driver)) + { + xfpm_popup_message(_("Xfce power manager"), + _("Unable to run Xfce4 power manager, " \ + "make sure the hardware abstract layer and the message bus daemon "\ + "are running"), + GTK_MESSAGE_ERROR); + g_error(_("Unable to load xfce4 power manager")); + g_print("\n"); + g_object_unref(driver); + return EXIT_FAILURE; + } + } + else + { g_print(_("Xfce power manager is already running")); g_print("\n"); return EXIT_SUCCESS; - } - XfpmDriver *driver = xfpm_driver_new(); - autostart(); - if (!xfpm_driver_monitor(driver)) - { - xfpm_popup_message(_("Xfce power manager"), - _("Unable to run Xfce4 power manager, " \ - "make sure the hardware abstract layer and the message bus daemon "\ - "are running"), - GTK_MESSAGE_ERROR); - g_error(_("Unable to load xfce4 power manager")); - g_print("\n"); - g_object_unref(driver); - return EXIT_FAILURE; - } - } - + } + + } + if ( quit ) { - int reply; - if (!xfpm_dbus_send_message_with_reply("Quit",&reply)) - { - return EXIT_FAILURE; - } - - if ( reply == 0 ) + if (!xfpm_dbus_name_has_owner(connection,XFPM_PM_IFACE)) { g_print(_("Xfce power manager is not running")); g_print("\n"); return EXIT_SUCCESS; } - return EXIT_SUCCESS; + else + { + xfpm_dbus_send_message("Quit"); + } + } return EXIT_SUCCESS;