summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAli Abdallah <ali.slackware@gmail.com>2009-01-30 10:41:32 +0000
committerAli Abdallah <ali.slackware@gmail.com>2009-01-30 10:41:32 +0000
commitd7168370e8ad96b2ba2e072626af0507f0e9ea1b (patch)
treebc9cd355d886c7f8834bf935212716dbb095666d
parent10a03e90206d5601b8acba72abe7c9fd86f6ddd5 (diff)
downloadixfce4-power-manager-d7168370e8ad96b2ba2e072626af0507f0e9ea1b.tar.gz
Reserve a bus name in the dbus session so we can easily detect if XFPM is running or not
(Old svn revision: 6604)
-rw-r--r--src/xfpm-dbus-messages.c45
-rw-r--r--src/xfpm-dbus-messages.h2
-rw-r--r--src/xfpm-driver.c8
-rw-r--r--src/xfpm-main.c105
4 files changed, 114 insertions, 46 deletions
diff --git a/src/xfpm-dbus-messages.c b/src/xfpm-dbus-messages.c
index fd2fdbfc..c5454713 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 0c0a328b..6a362c32 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 312b1e06..b02d506b 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 058c6f7b..d408f8c0 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;