From: Ira W. Snyder Date: Sat, 19 Jan 2008 04:38:01 +0000 (-0800) Subject: Add single-instance detection X-Git-Url: https://www.irasnyder.com/gitweb/?p=tilda-gobject.git;a=commitdiff_plain;h=ea7ec5693d350667c028e2e0f6d45da98f740125 Add single-instance detection This makes the DBus startup routines discover if there is another instance of Tilda already running, and if so, prints a message and exits. --- diff --git a/tilda.c b/tilda.c index f640eb1..65a0df7 100644 --- a/tilda.c +++ b/tilda.c @@ -18,12 +18,13 @@ tilda_initialize_dbus () GError *error = NULL; DBusGProxy *driver_proxy; guint request_ret; + gboolean ret; // Initialize the DBus connection dbus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (dbus_connection == NULL) { - g_warning ("Unable to connect to dbus: %s", error->message); + g_warning (_("Unable to connect to DBus: %s\n"), error->message); g_error_free (error); return; } @@ -34,15 +35,24 @@ tilda_initialize_dbus () DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); - if (!org_freedesktop_DBus_request_name (driver_proxy, service_name, 0, &request_ret, &error)) + ret = org_freedesktop_DBus_request_name (driver_proxy, + service_name, + DBUS_NAME_FLAG_DO_NOT_QUEUE, + &request_ret, + &error); + + if (!ret) { - // FIXME: for whatever reason, this is wrong. The error message doesn't appear - // FIXME: when we were unable to register the service. Perhaps there's a more - // FIXME: GLib-y way of doing this? - g_warning ("Unable to register service: %s", error->message); + g_warning (_("Unable to communicate with DBus: %s\n"), error->message); g_error_free (error); } + if (request_ret == DBUS_REQUEST_NAME_REPLY_EXISTS) + { + g_critical (_("There is already an instance of Tilda running\n")); + exit (EXIT_FAILURE); + } + g_object_unref (driver_proxy); }