public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [Bluez-devel] [PATCH] make bluez GNOME UIs singletons
@ 2007-10-16 18:38 Chris Rivera
  2007-10-17 14:24 ` Marcel Holtmann
  0 siblings, 1 reply; 13+ messages in thread
From: Chris Rivera @ 2007-10-16 18:38 UTC (permalink / raw)
  To: bluez-devel


[-- Attachment #1.1: Type: text/plain, Size: 423 bytes --]

Howdy folks.  The attached patch makes the applet, properties dialog, and
wizard singletons.  Currently, you can open several instances of these at a
time.  This is particularly annoying since you can launch the properties
dialog from the applet.  I also added a simple DBUS method (Present) to the
properties dialog and wizard that can be called by another instance to bring
the current instance to the foreground.

Chris

[-- Attachment #1.2: Type: text/html, Size: 454 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: bluez-gnome-singleton.patch --]
[-- Type: text/x-patch; name="bluez-gnome-singleton.patch", Size: 9733 bytes --]

Index: applet/main.c
===================================================================
RCS file: /cvsroot/bluez/gnome/applet/main.c,v
retrieving revision 1.87
diff -u -r1.87 main.c
--- applet/main.c	29 Aug 2007 20:42:18 -0000	1.87
+++ applet/main.c	16 Oct 2007 17:42:29 -0000
@@ -32,9 +32,9 @@
 #include <string.h>
 
 #include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
 
 #ifdef HAVE_HAL
-#include <dbus/dbus-glib-lowlevel.h>
 #include <hal/libhal.h>
 #endif
 
@@ -1788,6 +1784,10 @@
 	GtkWidget *menu;
 	GError *error = NULL;
 	char *str;
+	DBusGConnection *session_conn;
+	DBusGProxy *bus_proxy;
+	gboolean ret;
+	guint request_result;
 
 	bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
 	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
@@ -1807,6 +1807,41 @@
 		exit(EXIT_FAILURE);
 	}
 
+	session_conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
+	if (error != NULL) {
+		g_printerr("Connecting to session bus failed: %s\n",
+					error->message);
+		g_error_free(error);
+		dbus_g_connection_unref(conn);
+		exit(EXIT_FAILURE);
+	}
+
+	bus_proxy = dbus_g_proxy_new_for_name(session_conn,
+										  DBUS_SERVICE_DBUS,
+										  DBUS_PATH_DBUS,
+										  DBUS_INTERFACE_DBUS);
+
+	ret = dbus_g_proxy_call(bus_proxy, "RequestName", NULL,
+							G_TYPE_STRING, "org.bluez.GnomeApplet",
+							G_TYPE_UINT, 0,
+							G_TYPE_INVALID,
+							G_TYPE_UINT, &request_result,
+							G_TYPE_INVALID);
+	g_object_unref(G_OBJECT(bus_proxy));
+	if (!ret) {
+		g_printerr("Failed to request name on the session bus\n");
+		dbus_g_connection_unref(conn);
+		dbus_g_connection_unref(session_conn);
+		exit(EXIT_FAILURE);
+	}
+
+	if (request_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
+		g_print("Another instance is already running.\n");
+		dbus_g_connection_unref(conn);
+		dbus_g_connection_unref(session_conn);
+		exit(EXIT_SUCCESS);
+	}
+
 	gconf = gconf_client_get_default();
 
 #ifdef HAVE_HAL
@@ -1863,6 +1898,7 @@
 	g_list_foreach(adapter_list, adapter_free, NULL);
 
 	dbus_g_connection_unref(conn);
+	dbus_g_connection_unref(session_conn);
 
 	return 0;
 }
Index: properties/main.c
===================================================================
RCS file: /cvsroot/bluez/gnome/properties/main.c,v
retrieving revision 1.48
diff -u -r1.48 main.c
--- properties/main.c	31 Jul 2007 21:37:29 -0000	1.48
+++ properties/main.c	16 Oct 2007 17:42:29 -0000
@@ -32,6 +32,7 @@
 #include <string.h>
 
 #include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
 
 #include <glib/gi18n.h>
 
@@ -41,6 +42,9 @@
 #include "service.h"
 #include "adapter.h"
 
+#define PROPERTIES_DBUS_PATH "/org/bluez/GnomeProperties"
+#define PROPERTIES_DBUS_SERVICE "org.bluez.GnomeProperties"
+
 static void delete_callback(GtkWidget *window, GdkEvent *event,
 						gpointer user_data)
 {
@@ -58,7 +62,7 @@
 	gtk_main_quit();
 }
 
-static void create_window(GtkWidget *notebook)
+static GtkWidget *create_window(GtkWidget *notebook)
 {
 	GtkWidget *window;
 	GtkWidget *widget;
@@ -108,6 +112,7 @@
 						widget, _("General"));
 
 	gtk_widget_show_all(window);
+	return window;
 }
 
 static void name_owner_changed(DBusGProxy *object, const char *name,
@@ -139,12 +144,30 @@
 	return proxy;
 }
 
+static DBusHandlerResult MessageFunc (DBusConnection *c, DBusMessage *m, void *user_data)
+{
+	if (dbus_message_get_type(m) == DBUS_MESSAGE_TYPE_METHOD_CALL &&
+		!strncmp(PROPERTIES_DBUS_PATH, dbus_message_get_path(m), strlen(PROPERTIES_DBUS_PATH)) &&
+		!strncmp(PROPERTIES_DBUS_SERVICE, dbus_message_get_interface(m), strlen(PROPERTIES_DBUS_SERVICE)) &&
+		!strncmp("Present", dbus_message_get_member(m), 7)) {
+		GtkWindow *window = user_data;
+		gtk_window_present(window);
+	}
+
+	return DBUS_HANDLER_RESULT_HANDLED;
+}
+
 int main(int argc, char *argv[])
 {
 	GtkWidget *notebook;
-	DBusGConnection *conn;
-	DBusGProxy *manager;
+	GtkWidget *window;
 	GError *error = NULL;
+	DBusGConnection *conn, *session_conn;
+	DBusGProxy *manager;
+	DBusGProxy *bus_proxy;
+	DBusObjectPathVTable vtable;
+	guint request_result;
+	gboolean ret;
 
 	bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
 	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
@@ -160,16 +183,61 @@
 		exit(EXIT_FAILURE);
 	}
 
+	session_conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
+	if (error != NULL) {
+		g_printerr("Connecting to session bus failed: %s\n",
+					error->message);
+		g_error_free(error);
+		dbus_g_connection_unref(conn);
+		exit(EXIT_FAILURE);
+	}
+
+	bus_proxy = dbus_g_proxy_new_for_name(session_conn,
+										  DBUS_SERVICE_DBUS,
+										  DBUS_PATH_DBUS,
+										  DBUS_INTERFACE_DBUS);
+
+	ret = dbus_g_proxy_call(bus_proxy, "RequestName", NULL,
+   						G_TYPE_STRING, PROPERTIES_DBUS_SERVICE,
+							G_TYPE_UINT, 0,
+							G_TYPE_INVALID,
+							G_TYPE_UINT, &request_result,
+							G_TYPE_INVALID);
+	g_object_unref(G_OBJECT(bus_proxy));
+	if (!ret) {
+		g_printerr("Failed to request name on the session bus\n");
+		dbus_g_connection_unref(conn);
+		dbus_g_connection_unref(session_conn);
+		exit(EXIT_FAILURE);
+	}
+
+	if (request_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
+		g_print("Another instance is already running.\n");
+		DBusGProxy *proxy = dbus_g_proxy_new_for_name(session_conn,
+													  PROPERTIES_DBUS_SERVICE,
+													  PROPERTIES_DBUS_PATH,
+													  PROPERTIES_DBUS_SERVICE);
+		dbus_g_proxy_call_no_reply(proxy, "Present", G_TYPE_INVALID, G_TYPE_INVALID);
+		g_object_unref(G_OBJECT(proxy));
+		dbus_g_connection_unref(conn);
+		dbus_g_connection_unref(session_conn);
+		exit(EXIT_SUCCESS);
+	}
+
 	setup_general();
 
 	notebook = gtk_notebook_new();
-
+    
 	assign_adapter(notebook);
 
 	manager = setup_manager(conn);
 
-	create_window(notebook);
+	window = create_window(notebook);
 
+	vtable.message_function = MessageFunc;
+	vtable.unregister_function = NULL;
+	dbus_connection_register_object_path(dbus_g_connection_get_connection (session_conn),
+										 "/org/bluez/GnomeProperties", &vtable, window);
 	gtk_main();
 
 	cleanup_adapter();
@@ -181,6 +249,7 @@
 	g_object_unref(manager);
 
 	dbus_g_connection_unref(conn);
+	dbus_g_connection_unref(session_conn);
 
 	return 0;
 }
Index: wizard/main.c
===================================================================
RCS file: /cvsroot/bluez/gnome/wizard/main.c,v
retrieving revision 1.23
diff -u -r1.23 main.c
--- wizard/main.c	15 Aug 2007 07:04:03 -0000	1.23
+++ wizard/main.c	16 Oct 2007 17:42:29 -0000
@@ -26,8 +26,10 @@
 #endif
 
 #include <string.h>
+#include <stdlib.h>
 
 #include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
 
 #include <glib/gi18n.h>
 
@@ -39,6 +41,9 @@
 
 #include "bluetooth-device-selection.h"
 
+#define WIZARD_DBUS_PATH "/org/bluez/GnomeWizard"
+#define WIZARD_DBUS_SERVICE "org.bluez.GnomeWizard"
+
 static BluetoothClient *client;
 
 static gchar *address = NULL;
@@ -387,7 +392,7 @@
 	page_summary = vbox;
 }
 
-static void create_wizard(void)
+static GtkWidget *create_wizard(void)
 {
 	GtkWidget *assistant;
 
@@ -430,23 +435,90 @@
 	gtk_widget_show_all(assistant);
 
 	gtk_assistant_update_buttons_state(GTK_ASSISTANT(assistant));
+	return assistant;
+}
+
+static DBusHandlerResult MessageFunc (DBusConnection *c, DBusMessage *m, void *user_data)
+{
+	if (dbus_message_get_type(m) == DBUS_MESSAGE_TYPE_METHOD_CALL &&
+		!strncmp(WIZARD_DBUS_PATH, dbus_message_get_path(m), strlen(WIZARD_DBUS_PATH)) &&
+		!strncmp(WIZARD_DBUS_SERVICE, dbus_message_get_interface(m), strlen(WIZARD_DBUS_SERVICE)) &&
+		!strncmp("Present", dbus_message_get_member(m), 7)) {
+		GtkWindow *window = user_data;
+		gtk_window_present(window);
+	}
+
+	return DBUS_HANDLER_RESULT_HANDLED;
 }
 
 int main(int argc, char *argv[])
 {
-	bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
+	GtkWidget *window;
+	GError *error = NULL;
+	DBusGConnection *session_conn;
+	DBusGProxy *bus_proxy;
+	DBusObjectPathVTable vtable;
+	gboolean ret;
+	guint request_result;
+	
+	bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
 	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
 	textdomain(GETTEXT_PACKAGE);
 
 	gtk_init(&argc, &argv);
 
+	session_conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
+	if (error != NULL) {
+		g_printerr("Connecting to session bus failed: %s\n",
+					error->message);
+		g_error_free(error);
+		exit(EXIT_FAILURE);
+	}
+
+	bus_proxy = dbus_g_proxy_new_for_name(session_conn,
+										  DBUS_SERVICE_DBUS,
+										  DBUS_PATH_DBUS,
+										  DBUS_INTERFACE_DBUS);
+
+	ret = dbus_g_proxy_call(bus_proxy, "RequestName", NULL,
+							G_TYPE_STRING, WIZARD_DBUS_SERVICE,
+							G_TYPE_UINT, 0,
+							G_TYPE_INVALID,
+							G_TYPE_UINT, &request_result,
+							G_TYPE_INVALID);
+	g_object_unref(G_OBJECT(bus_proxy));
+	if (!ret) {
+		g_printerr("Failed to request name on the session bus\n");
+		dbus_g_connection_unref(session_conn);
+		exit(EXIT_FAILURE);
+	}
+
+	if (request_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
+		g_print("Another instance is already running.\n");
+		DBusGProxy *proxy = dbus_g_proxy_new_for_name(session_conn,
+       											  WIZARD_DBUS_SERVICE,
+													  WIZARD_DBUS_PATH,
+													  WIZARD_DBUS_SERVICE);
+		dbus_g_proxy_call_no_reply(proxy, "Present", G_TYPE_INVALID, G_TYPE_INVALID);
+		g_object_unref(G_OBJECT(proxy));
+		dbus_g_connection_unref(session_conn);
+		exit(EXIT_SUCCESS);
+	}
+
 	client = bluetooth_client_new();
 
-	create_wizard();
+	window = create_wizard();
+    
+	vtable.message_function = MessageFunc;
+	vtable.unregister_function = NULL;
+	dbus_connection_register_object_path(dbus_g_connection_get_connection (session_conn),
+										 WIZARD_DBUS_PATH, &vtable, window);
 
 	gtk_main();
 
 	g_object_unref(client);
+	
+	dbus_g_connection_unref(session_conn);
 
 	return 0;
 }

[-- Attachment #3: Type: text/plain, Size: 314 bytes --]

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/

[-- Attachment #4: Type: text/plain, Size: 164 bytes --]

_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2007-11-27 15:45 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-16 18:38 [Bluez-devel] [PATCH] make bluez GNOME UIs singletons Chris Rivera
2007-10-17 14:24 ` Marcel Holtmann
2007-10-17 14:48   ` Chris Rivera
2007-10-17 15:11     ` Bastien Nocera
2007-10-18 10:15       ` Fabien Chevalier
2007-10-31 16:14         ` Marcel Holtmann
2007-10-31 16:24           ` Bastien Nocera
2007-10-31 16:36             ` Marcel Holtmann
2007-11-13 16:24             ` Chris Rivera
2007-11-13 16:30               ` Bastien Nocera
2007-11-13 17:06                 ` Chris Rivera
2007-11-27 15:45                   ` Chris Rivera
2007-10-24 19:54       ` Chris Rivera

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox