[gnome-bluetooth] sendto: Port to GDBus



commit bf8dec6760bbb4ad39adb3552853c595860fdf33
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Oct 12 17:49:59 2011 +0100

    sendto: Port to GDBus
    
    Which means we can:
    - Remove the marshallers, as GDBus doesn't need them
    - Move the obex-agent code to sendto
    - Stop linking sendto against any private code

 configure.ac                 |    9 +-
 lib/Makefile.am              |   21 +--
 lib/marshal.list             |    4 -
 lib/obex-agent.c             |  381 -----------------------------------
 lib/obex-agent.xml           |   32 ---
 sendto/Makefile.am           |    4 +-
 sendto/main.c                |  199 +++++++++----------
 sendto/obex-agent.c          |  455 ++++++++++++++++++++++++++++++++++++++++++
 {lib => sendto}/obex-agent.h |   20 +-
 9 files changed, 568 insertions(+), 557 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 63d4d29..e9c927d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -74,7 +74,6 @@ AM_CONDITIONAL([ICON_UPDATE], [test -n "$UPDATE_ICON_CACHE"])
 
 GTK_REQUIRED=2.91.3
 GLIB_REQUIRED=2.25.7
-DBUS_GLIB_REQUIRED=0.74
 NOTIFY_REQUIRED=0.7.0
 NAUTILUS_SENDTO_REQUIRED=2.29.0
 NAUTILUS_SENDTO_MAX_REQUIRED=3.1.0
@@ -98,7 +97,6 @@ PKG_CHECK_MODULES(APPLET,
 
 dnl Requires for the sendto app
 PKG_CHECK_MODULES(SENDTO,
-		  dbus-glib-1 >= $DBUS_GLIB_REQUIRED
 		  gtk+-3.0
 		  gio-2.0)
 
@@ -139,15 +137,13 @@ PKG_CHECK_MODULES(LIBGNOMEBT, gtk+-3.0)
 
 dnl Requires for the private library
 PKG_CHECK_MODULES(COMMON,
-		  dbus-glib-1 >= $DBUS_GLIB_REQUIRED
 		  gmodule-2.0
 		  gio-unix-2.0
 		  gtk+-3.0)
 
 dnl Requires for the geoclue plugin
 PKG_CHECK_MODULES(GEOCLUE,
-		  gio-2.0
-		  dbus-glib-1)
+		  gio-2.0)
 
 dnl Requires for the plugins
 PKG_CHECK_MODULES(PLUGINS,
@@ -191,9 +187,6 @@ AC_SUBST(DBUS_BINDING_TOOL)
 GDBUS_CODEGEN="gdbus-codegen"
 AC_SUBST(GDBUS_CODEGEN)
 
-GLIB_GENMARSHAL=`$PKG_CONFIG --variable=glib_genmarshal glib-2.0`
-AC_SUBST(GLIB_GENMARSHAL)
-
 GOBJECT_INTROSPECTION_CHECK([0.9.5])
 
 GNOME_COMMON_INIT
diff --git a/lib/Makefile.am b/lib/Makefile.am
index f472515..8dbd17b 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -12,7 +12,6 @@ libcommon_la_SOURCES =						\
 		bluetooth-client-private.h			\
 		bluetooth-agent.h bluetooth-agent.c		\
 		bluetooth-killswitch.h bluetooth-killswitch.c	\
-		obex-agent.h obex-agent.c			\
 		bluetooth-plugin-manager.c			\
 		bluetooth-plugin-manager.h			\
 		bluetooth-enums.h				\
@@ -27,7 +26,6 @@ libgnome_bluetooth_c_sources =		\
 	bluetooth-utils.c		\
 	bluetooth-agent.c		\
 	bluetooth-killswitch.c		\
-	marshal.c			\
 	gnome-bluetooth-enum-types.c	\
 	bluetooth-chooser.c		\
 	bluetooth-filter-widget.c	\
@@ -39,7 +37,6 @@ libgnome_bluetooth_private_headers =	\
 	bluetooth-client-private.h	\
 	bluetooth-agent.h		\
 	bluetooth-killswitch.h		\
-	marshal.h			\
 	gnome-bluetooth-enum-types.h	\
 	bluetooth-chooser-private.h	\
 	bluetooth-client-glue.h
@@ -109,10 +106,8 @@ CLEANFILES += $(gir_DATA) $(typelib_DATA)
 
 endif # HAVE_INTROSPECTION
 
-BUILT_SOURCES = marshal.h marshal.c \
-		bluetooth-client-glue.h \
+BUILT_SOURCES = bluetooth-client-glue.h \
 		bluetooth-client-glue.c \
-		obex-agent-glue.h \
 		gnome-bluetooth-enum-types.h \
 		gnome-bluetooth-enum-types.c
 
@@ -132,27 +127,15 @@ test_plugins_LDADD = libcommon.la
 
 test_class_LDADD = libcommon.la
 
-EXTRA_DIST += marshal.list \
-		bluetooth-client.xml \
-		obex-agent.xml \
-		gnome-bluetooth.symbols
+EXTRA_DIST += bluetooth-client.xml gnome-bluetooth.symbols
 
 MAINTAINERCLEANFILES = Makefile.in
 
-marshal.h: marshal.list
-	$(AM_V_GEN) $(GLIB_GENMARSHAL) --prefix=marshal $< --header > $@
-
-marshal.c: marshal.list
-	$(AM_V_GEN) $(GLIB_GENMARSHAL) --prefix=marshal $< --header --body > $@
-
 bluetooth-client-glue.c: bluetooth-client-glue.h
 
 bluetooth-client-glue.h: bluetooth-client.xml
 	$(AM_V_GEN) $(GDBUS_CODEGEN) --c-namespace= --generate-c-code=bluetooth-client-glue --interface-prefix=org.bluez $<
 
-obex-agent-glue.h: obex-agent.xml
-	$(AM_V_GEN) $(DBUS_BINDING_TOOL) --prefix=obex_agent --mode=glib-server --output=$@ $<
-
 gnome_bluetooth_headers_to_scan_for_enums = bluetooth-enums.h
 # Generate the enums source code, with glib-mkenums:
 # This is based on the same Makefile.am stuff in pango:
diff --git a/sendto/Makefile.am b/sendto/Makefile.am
index 07a2d48..09b5cd2 100644
--- a/sendto/Makefile.am
+++ b/sendto/Makefile.am
@@ -13,9 +13,9 @@ AM_CFLAGS =                                     \
 
 bin_PROGRAMS = bluetooth-sendto
 
-bluetooth_sendto_SOURCES = main.c
+bluetooth_sendto_SOURCES = main.c obex-agent.c obex-agent.h
 
-bluetooth_sendto_LDADD = $(top_builddir)/lib/libcommon.la $(top_builddir)/lib/libgnome-bluetooth.la $(SENDTO_LIBS)
+bluetooth_sendto_LDADD = $(top_builddir)/lib/libgnome-bluetooth.la $(SENDTO_LIBS)
 
 man_MANS = bluetooth-sendto.1
 
diff --git a/sendto/main.c b/sendto/main.c
index 50bfd9c..72f8dc9 100644
--- a/sendto/main.c
+++ b/sendto/main.c
@@ -32,9 +32,6 @@
 #include <gio/gio.h>
 #include <gtk/gtk.h>
 
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-lowlevel.h>
-
 #include <obex-agent.h>
 #include <bluetooth-client.h>
 #include <bluetooth-chooser.h>
@@ -44,9 +41,9 @@
 
 #define RESPONSE_RETRY 1
 
-static DBusGConnection *conn = NULL;
+static GDBusConnection *conn = NULL;
 static ObexAgent *agent = NULL;
-static DBusGProxy *client_proxy = NULL;
+static GDBusProxy *client_proxy = NULL;
 
 static GtkWidget *dialog;
 static GtkWidget *label_from;
@@ -58,7 +55,7 @@ static gchar *option_device = NULL;
 static gchar *option_device_name = NULL;
 static gchar **option_files = NULL;
 
-static DBusGProxy *current_transfer = NULL;
+static GDBusProxy *current_transfer = NULL;
 static guint64 current_size = 0;
 static guint64 total_size = 0;
 static guint64 total_sent = 0;
@@ -69,49 +66,42 @@ static int file_index = 0;
 static gint64 first_update = 0;
 static gint64 last_update = 0;
 
-static void send_notify(DBusGProxy *proxy, DBusGProxyCall *call, void *user_data);
+static void send_notify(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data);
 
 /* Agent callbacks */
-static gboolean release_callback(DBusGMethodInvocation *context, gpointer user_data);
-static gboolean request_callback(DBusGMethodInvocation *context, DBusGProxy *transfer, gpointer user_data);
-static gboolean progress_callback(DBusGMethodInvocation *context,
-				  DBusGProxy *transfer,
+static gboolean release_callback(GDBusMethodInvocation *context, gpointer user_data);
+static gboolean request_callback(GDBusMethodInvocation *context, GDBusProxy *transfer, gpointer user_data);
+static gboolean progress_callback(GDBusMethodInvocation *context,
+				  GDBusProxy *transfer,
 				  guint64 transferred,
 				  gpointer user_data);
-static gboolean complete_callback(DBusGMethodInvocation *context, DBusGProxy *transfer, gpointer user_data);
-static gboolean error_callback(DBusGMethodInvocation *context,
-			       DBusGProxy *transfer,
+static gboolean complete_callback(GDBusMethodInvocation *context, GDBusProxy *transfer, gpointer user_data);
+static gboolean error_callback(GDBusMethodInvocation *context,
+			       GDBusProxy *transfer,
 			       const char *message,
 			       gpointer user_data);
 
-static void value_free(GValue *value)
-{
-	g_value_unset(value);
-	g_free(value);
-}
-
-static GHashTable *
+static void
 send_files (void)
 {
-	GHashTable *hash;
-	GValue *value;
+	GVariant *parameters;
+	GVariantBuilder *builder;
 
-	hash = g_hash_table_new_full(g_str_hash, g_str_equal,
-				     g_free, (GDestroyNotify) value_free);
+	builder = g_variant_builder_new (G_VARIANT_TYPE_DICTIONARY);
+	g_variant_builder_add (builder, "{sv}", "Destination", g_variant_new_string (option_device));
 
-	value = g_new0(GValue, 1);
-	g_value_init(value, G_TYPE_STRING);
-	g_value_set_string(value, option_device);
-	g_hash_table_insert(hash, g_strdup("Destination"), value);
+	parameters = g_variant_new ("(a{sv}^aso)", builder, option_files, AGENT_PATH);
 
-	dbus_g_proxy_begin_call(client_proxy, "SendFiles",
-				send_notify, NULL, NULL,
-				dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), hash,
-				G_TYPE_STRV, option_files,
-				DBUS_TYPE_G_OBJECT_PATH, AGENT_PATH,
-				G_TYPE_INVALID);
+	g_dbus_proxy_call (client_proxy,
+			   "SendFiles",
+			   parameters,
+			   G_DBUS_CALL_FLAGS_NONE,
+			   -1,
+			   NULL,
+			   (GAsyncReadyCallback) send_notify,
+			   NULL);
 
-	return hash;
+	g_variant_builder_unref (builder);
 }
 
 static void
@@ -223,7 +213,14 @@ static void response_callback(GtkWidget *dialog,
 
 	if (current_transfer != NULL) {
 		obex_agent_set_error_func(agent, NULL, NULL);
-		dbus_g_proxy_call_no_reply (current_transfer, "Cancel", G_TYPE_INVALID);
+		g_dbus_proxy_call (current_transfer,
+				   "Cancel",
+				   NULL,
+				   G_DBUS_CALL_FLAGS_NONE,
+				   -1,
+				   NULL,
+				   (GAsyncReadyCallback) NULL,
+				   NULL);
 		g_object_unref (current_transfer);
 		current_transfer = NULL;
 	}
@@ -235,8 +232,8 @@ static void response_callback(GtkWidget *dialog,
 static gboolean is_palm_device(const gchar *bdaddr)
 {
 	return (g_str_has_prefix(bdaddr, "00:04:6B") ||
-			g_str_has_prefix(bdaddr, "00:07:E0") ||
-				g_str_has_prefix(bdaddr, "00:0E:20"));
+		g_str_has_prefix(bdaddr, "00:07:E0") ||
+		g_str_has_prefix(bdaddr, "00:0E:20"));
 }
 
 static void create_window(void)
@@ -342,19 +339,21 @@ static gchar *get_error_message(GError *error)
 	if (error == NULL)
 		return g_strdup(_("An unknown error occurred"));
 
-	if (error->code != DBUS_GERROR_REMOTE_EXCEPTION) {
+	if (g_dbus_error_is_remote_error (error) == FALSE) {
 		message = g_strdup(error->message);
 		goto done;
 	}
 
+	/* FIXME */
+#if 0
 	if (dbus_g_error_has_name(error, OPENOBEX_CONNECTION_FAILED) == TRUE &&
-					is_palm_device(option_device)) {
+	    is_palm_device(option_device)) {
 		message = g_strdup(_("Make sure that the remote device "
 					"is switched on and that it "
 					"accepts Bluetooth connections"));
 		goto done;
 	}
-
+#endif
 	if (*error->message == '\0')
 		message = g_strdup(_("An unknown error occurred"));
 	else
@@ -407,29 +406,29 @@ static gchar *get_device_name(const gchar *address)
 	return found_name;
 }
 
-static void get_properties_callback (DBusGProxy *proxy,
-				     DBusGProxyCall *call,
-				     void *user_data)
+static void get_properties_callback (GDBusProxy   *proxy,
+				     GAsyncResult *res,
+				     gpointer      user_data)
 {
 	GError *error = NULL;
 	gchar *filename = option_files[file_index];
 	GFile *file, *dir;
 	gchar *basename, *text, *markup;
-	GHashTable *hash;
+	GVariant *variant;
 
-	if (dbus_g_proxy_end_call (proxy, call, &error,
-				   dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &hash,
-				   G_TYPE_INVALID) != FALSE) {
-		GValue *value;
+	variant = g_dbus_proxy_call_finish (proxy, res, &error);
 
-		value = g_hash_table_lookup(hash, "Size");
-		if (value) {
-			current_size = g_value_get_uint64(value);
+	if (variant) {
+		GVariant *dict, *size;
 
+		g_variant_get (variant, "(@a{sv})", &dict);
+		size = g_variant_lookup_value (dict, "Size", G_VARIANT_TYPE_UINT64);
+		if (size) {
+			current_size = g_variant_get_uint64 (size);
 			last_update = get_system_time();
 		}
 
-		g_hash_table_destroy(hash);
+		g_variant_unref (variant);
 	}
 
 	file = g_file_new_for_path (filename);
@@ -460,24 +459,29 @@ static void get_properties_callback (DBusGProxy *proxy,
 	g_free(text);
 }
 
-static gboolean request_callback(DBusGMethodInvocation *context,
-				DBusGProxy *transfer, gpointer user_data)
+static gboolean request_callback(GDBusMethodInvocation *invocation,
+				GDBusProxy *transfer, gpointer user_data)
 {
 	g_assert (current_transfer == NULL);
 
-	dbus_g_proxy_begin_call(transfer, "GetProperties",
-				get_properties_callback, NULL, NULL,
-				G_TYPE_INVALID);
-
 	current_transfer = g_object_ref (transfer);
-
-	dbus_g_method_return(context, "");
+	g_dbus_proxy_call (current_transfer,
+			   "GetProperties",
+			   NULL,
+			   G_DBUS_CALL_FLAGS_NONE,
+			   -1,
+			   NULL,
+			   (GAsyncReadyCallback) get_properties_callback,
+			   NULL);
+
+	g_dbus_method_invocation_return_value (invocation,
+					       g_variant_new ("(s)", ""));
 
 	return TRUE;
 }
 
-static gboolean progress_callback(DBusGMethodInvocation *context,
-				DBusGProxy *transfer, guint64 transferred,
+static gboolean progress_callback(GDBusMethodInvocation *invocation,
+				GDBusProxy *transfer, guint64 transferred,
 							gpointer user_data)
 {
 	gint64 current_time;
@@ -530,13 +534,13 @@ static gboolean progress_callback(DBusGMethodInvocation *context,
 	g_free(text);
 
 done:
-	dbus_g_method_return(context);
+	g_dbus_method_invocation_return_value (invocation, NULL);
 
 	return TRUE;
 }
 
-static gboolean complete_callback(DBusGMethodInvocation *context,
-				DBusGProxy *transfer, gpointer user_data)
+static gboolean complete_callback(GDBusMethodInvocation *invocation,
+				GDBusProxy *transfer, gpointer user_data)
 {
 	total_sent += current_size;
 
@@ -549,15 +553,15 @@ static gboolean complete_callback(DBusGMethodInvocation *context,
 	if (file_index == file_count)
 		gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), 1.0);
 
-	dbus_g_method_return(context);
+	g_dbus_method_invocation_return_value (invocation, NULL);
 
 	return TRUE;
 }
 
-static gboolean release_callback(DBusGMethodInvocation *context,
+static gboolean release_callback(GDBusMethodInvocation *invocation,
 							gpointer user_data)
 {
-	dbus_g_method_return(context);
+	g_dbus_method_invocation_return_value (invocation, NULL);
 
 	agent = NULL;
 
@@ -570,8 +574,8 @@ static gboolean release_callback(DBusGMethodInvocation *context,
 	return TRUE;
 }
 
-static gboolean error_callback(DBusGMethodInvocation *context,
-			       DBusGProxy *transfer,
+static gboolean error_callback(GDBusMethodInvocation *invocation,
+			       GDBusProxy *transfer,
 			       const char *message,
 			       gpointer user_data)
 {
@@ -590,18 +594,22 @@ static gboolean error_callback(DBusGMethodInvocation *context,
 		agent = NULL;
 	}
 
-	dbus_g_method_return(context);
+	g_dbus_method_invocation_return_value (invocation, NULL);
 
 	return TRUE;
 }
 
-static void send_notify(DBusGProxy *proxy,
-				DBusGProxyCall *call, void *user_data)
+static void
+send_notify (GDBusProxy   *proxy,
+	     GAsyncResult *res,
+	     gpointer      user_data)
 {
 	GError *error = NULL;
+	GVariant *variant;
+
+	variant = g_dbus_proxy_call_finish (proxy, res, &error);
 
-	if (dbus_g_proxy_end_call(proxy, call, &error,
-						G_TYPE_INVALID) == FALSE) {
+	if (variant == NULL) {
 		char *message;
 
 		message = get_error_message(error);
@@ -618,6 +626,8 @@ static void send_notify(DBusGProxy *proxy,
 	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(progress), NULL);
 
 	first_update = get_system_time();
+
+	g_variant_unref (variant);
 }
 
 static void
@@ -807,7 +817,7 @@ int main(int argc, char *argv[])
 			total_size += st.st_size;
 	}
 
-	conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
+	conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
 	if (conn == NULL) {
 		if (error != NULL) {
 			g_printerr("Connecting to session bus failed: %s\n",
@@ -819,24 +829,6 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	dbus_g_object_register_marshaller(marshal_VOID__STRING_STRING,
-					G_TYPE_NONE, G_TYPE_STRING,
-					G_TYPE_STRING, G_TYPE_INVALID);
-
-	dbus_g_object_register_marshaller(marshal_VOID__STRING_STRING_UINT64,
-				G_TYPE_NONE, G_TYPE_STRING,
-				G_TYPE_STRING, G_TYPE_UINT64, G_TYPE_INVALID);
-
-	dbus_g_object_register_marshaller(marshal_VOID__UINT64,
-				G_TYPE_NONE, G_TYPE_UINT64, G_TYPE_INVALID);
-
-	dbus_g_object_register_marshaller(marshal_VOID__STRING_STRING_STRING,
-					  G_TYPE_NONE,
-					  DBUS_TYPE_G_OBJECT_PATH,
-					  G_TYPE_STRING,
-					  G_TYPE_STRING,
-					  G_TYPE_INVALID);
-
 	if (option_device_name == NULL)
 		option_device_name = get_device_name(option_device);
 	if (option_device_name == NULL)
@@ -844,8 +836,14 @@ int main(int argc, char *argv[])
 
 	create_window();
 
-	client_proxy = dbus_g_proxy_new_for_name(conn, "org.openobex.client",
-					"/", "org.openobex.Client");
+	client_proxy = g_dbus_proxy_new_sync (conn,
+					      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+					      NULL,
+					      "org.openobex.client",
+					      "/",
+					      "org.openobex.Client",
+					      NULL,
+					      NULL);
 
 	setup_agent ();
 
@@ -853,9 +851,8 @@ int main(int argc, char *argv[])
 
 	gtk_main();
 
-	g_object_unref(client_proxy);
-
-	dbus_g_connection_unref(conn);
+	g_object_unref (client_proxy);
+	g_object_unref (conn);
 
 	g_strfreev(option_files);
 	g_free(option_device);
diff --git a/sendto/obex-agent.c b/sendto/obex-agent.c
new file mode 100644
index 0000000..d5f58e8
--- /dev/null
+++ b/sendto/obex-agent.c
@@ -0,0 +1,455 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2005-2008  Marcel Holtmann <marcel holtmann org>
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library 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
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <gio/gio.h>
+
+#include "obex-agent.h"
+
+#ifdef DEBUG
+#define DBG(fmt, arg...) printf("%s:%s() " fmt "\n", __FILE__, __FUNCTION__ , ## arg)
+#else
+#define DBG(fmt...)
+#endif
+
+#define OBEX_SERVICE	"org.openobex.client"
+
+#define OBEX_CLIENT_PATH	"/"
+#define OBEX_CLIENT_INTERFACE	"org.openobex.Client"
+#define OBEX_TRANSFER_INTERFACE	"org.openobex.Transfer"
+
+static const gchar introspection_xml[] =
+"<node name='%s'>"
+"  <interface name='org.openobex.Agent'>"
+"    <method name='Request'>"
+"      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
+"      <arg type='o' name='transfer' direction='in'/>"
+"      <arg type='s' name='name' direction='out'/>"
+"    </method>"
+""
+"    <method name='Progress'>"
+"      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
+"      <arg type='o' name='transfer' direction='in'/>"
+"      <arg type='t' name='transferred' direction='in'/>"
+"    </method>"
+""
+"    <method name='Complete'>"
+"      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
+"      <arg type='o' name='transfer' direction='in'/>"
+"    </method>"
+""
+"    <method name='Release'>"
+"      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
+"    </method>"
+""
+"    <method name='Error'>"
+"      <annotation name='org.freedesktop.DBus.GLib.Async' value=''/>"
+"      <arg type='o' name='transfer' direction='in'/>"
+"      <arg type='s' name='message' direction='in'/>"
+"    </method>"
+"  </interface>"
+"</node>";
+
+#define OBEX_AGENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), \
+					OBEX_TYPE_AGENT, ObexAgentPrivate))
+
+typedef struct _ObexAgentPrivate ObexAgentPrivate;
+
+struct _ObexAgentPrivate {
+	GDBusConnection *conn;
+	gchar *busname;
+	gchar *path;
+	GDBusNodeInfo *introspection_data;
+	guint reg_id;
+	guint watch_id;
+
+	ObexAgentReleaseFunc release_func;
+	gpointer release_data;
+
+	ObexAgentRequestFunc request_func;
+	gpointer request_data;
+
+	ObexAgentProgressFunc progress_func;
+	gpointer progress_data;
+
+	ObexAgentCompleteFunc complete_func;
+	gpointer complete_data;
+
+	ObexAgentErrorFunc error_func;
+	gpointer error_data;
+};
+
+G_DEFINE_TYPE(ObexAgent, obex_agent, G_TYPE_OBJECT)
+
+static GDBusProxy *
+get_proxy_from_path (ObexAgent *agent,
+		     const char *path)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE (agent);
+
+	return g_dbus_proxy_new_sync (priv->conn,
+				      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+				      NULL,
+				      OBEX_SERVICE,
+				      path,
+				      OBEX_TRANSFER_INTERFACE,
+				      NULL,
+				      NULL);
+
+}
+
+static gboolean obex_agent_request(ObexAgent *agent, const char *path,
+						GDBusMethodInvocation *invocation)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	DBG("agent %p", agent);
+
+	if (priv->request_func) {
+		GDBusProxy *proxy;
+
+		proxy = get_proxy_from_path (agent, path);
+
+		priv->request_func (invocation, proxy, priv->request_data);
+
+		g_object_unref(proxy);
+	} else {
+		g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", ""));
+	}
+
+	return TRUE;
+}
+
+static gboolean obex_agent_progress(ObexAgent *agent, const char *path,
+			guint64 transferred, GDBusMethodInvocation *invocation)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+	gboolean result = FALSE;
+
+	DBG("agent %p", agent);
+
+	if (priv->progress_func) {
+		GDBusProxy *proxy;
+
+		proxy = get_proxy_from_path (agent, path);
+
+		result = priv->progress_func(invocation, proxy, transferred,
+							priv->progress_data);
+
+		g_object_unref(proxy);
+	} else {
+		g_dbus_method_invocation_return_value (invocation, NULL);
+	}
+
+	return result;
+}
+
+static gboolean obex_agent_complete(ObexAgent *agent, const char *path,
+						GDBusMethodInvocation *invocation)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+	gboolean result = FALSE;
+
+	DBG("agent %p", agent);
+
+	if (priv->complete_func) {
+		GDBusProxy *proxy;
+
+		proxy = get_proxy_from_path (agent, path);
+
+		result = priv->complete_func(invocation, proxy,
+						priv->complete_data);
+
+		g_object_unref(proxy);
+	} else {
+		g_dbus_method_invocation_return_value (invocation, NULL);
+	}
+
+	return result;
+}
+
+static gboolean obex_agent_release(ObexAgent *agent,
+						GDBusMethodInvocation *invocation)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+	gboolean result = FALSE;
+
+	DBG("agent %p", agent);
+
+	if (priv->release_func)
+		result = priv->release_func(invocation, priv->release_data);
+	else
+		g_dbus_method_invocation_return_value (invocation, NULL);
+
+	g_object_unref(agent);
+
+	return result;
+}
+
+static gboolean obex_agent_error(ObexAgent *agent,
+				 const char *path,
+				 const char *message,
+				 GDBusMethodInvocation *invocation)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+	gboolean result = FALSE;
+
+	DBG("agent %p", agent);
+
+	if (priv->error_func) {
+		GDBusProxy *proxy;
+
+		proxy = get_proxy_from_path (agent, path);
+
+		result = priv->error_func(invocation, proxy, message,
+							priv->progress_data);
+
+		g_object_unref(proxy);
+	} else {
+		g_dbus_method_invocation_return_value (invocation, NULL);
+	}
+
+	return result;
+}
+
+static void
+name_appeared_cb (GDBusConnection *connection,
+		  const gchar     *name,
+		  const gchar     *name_owner,
+		  ObexAgent       *agent)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	g_free (priv->busname);
+	priv->busname = g_strdup (name_owner);
+}
+
+static void
+name_vanished_cb (GDBusConnection *connection,
+		  const gchar     *name,
+		  ObexAgent       *agent)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	g_free (priv->busname);
+	priv->busname = NULL;
+}
+
+static void obex_agent_init(ObexAgent *agent)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	DBG("agent %p", agent);
+
+	priv->conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
+	priv->watch_id = g_bus_watch_name_on_connection (priv->conn,
+							 OBEX_SERVICE,
+							 G_BUS_NAME_WATCHER_FLAGS_NONE,
+							 (GBusNameAppearedCallback) name_appeared_cb,
+							 (GBusNameVanishedCallback) name_vanished_cb,
+							 agent,
+							 NULL);
+}
+
+static void obex_agent_finalize(GObject *agent)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	DBG("agent %p", agent);
+
+	g_bus_unwatch_name (priv->watch_id);
+	g_free (priv->busname);
+	g_free(priv->path);
+	g_dbus_node_info_unref (priv->introspection_data);
+	g_object_unref (priv->conn);
+
+	G_OBJECT_CLASS(obex_agent_parent_class)->finalize(agent);
+}
+
+static void obex_agent_class_init(ObexAgentClass *klass)
+{
+	GObjectClass *object_class = (GObjectClass *) klass;
+
+	DBG("class %p", klass);
+
+	g_type_class_add_private(klass, sizeof(ObexAgentPrivate));
+
+	object_class->finalize = obex_agent_finalize;
+}
+
+ObexAgent *obex_agent_new(void)
+{
+	ObexAgent *agent;
+
+	agent = OBEX_AGENT(g_object_new(OBEX_TYPE_AGENT, NULL));
+
+	DBG("agent %p", agent);
+
+	return agent;
+}
+
+static void
+handle_method_call (GDBusConnection       *connection,
+		    const gchar           *sender,
+		    const gchar           *object_path,
+		    const gchar           *interface_name,
+		    const gchar           *method_name,
+		    GVariant              *parameters,
+		    GDBusMethodInvocation *invocation,
+		    gpointer               user_data)
+{
+	ObexAgent *agent = (ObexAgent *) user_data;
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	if (g_str_equal (sender, priv->busname) == FALSE) {
+		g_assert_not_reached ();
+		/* FIXME, should this just be a D-Bus Error instead? */
+	}
+
+	if (g_strcmp0 (method_name, "Request") == 0) {
+		char *path;
+		g_variant_get (parameters, "(o)", &path);
+		obex_agent_request (agent, path, invocation);
+		g_free (path);
+	} else if (g_strcmp0 (method_name, "Progress") == 0) {
+		char *path;
+		guint64 transferred;
+		g_variant_get (parameters, "(ot)", &path, &transferred);
+		obex_agent_progress (agent, path, transferred, invocation);
+		g_free (path);
+	} else if (g_strcmp0 (method_name, "Complete") == 0) {
+		char *path;
+		g_variant_get (parameters, "(o)", &path);
+		obex_agent_complete (agent, path, invocation);
+		g_free (path);
+	} else if (g_strcmp0 (method_name, "Error") == 0) {
+		char *path, *message;
+		g_variant_get (parameters, "(os)", &path, &message);
+		obex_agent_error (agent, path, message, invocation);
+		g_free (path);
+		g_free (message);
+	} else if (g_strcmp0 (method_name, "Release") == 0) {
+		obex_agent_release (agent, invocation);
+	}
+}
+
+static const GDBusInterfaceVTable interface_vtable =
+{
+	handle_method_call,
+	NULL, /* GetProperty */
+	NULL, /* SetProperty */
+};
+
+gboolean obex_agent_setup(ObexAgent *agent, const char *path)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+	GError *error = NULL;
+
+	DBG("agent %p path %s", agent, path);
+
+	if (priv->path != NULL) {
+		g_warning ("Obex Agent already setup on '%s'", priv->path);
+		return FALSE;
+	}
+
+	priv->path = g_strdup (path);
+
+	char *xml;
+	xml = g_strdup_printf (introspection_xml, path);
+	priv->introspection_data = g_dbus_node_info_new_for_xml (xml, NULL);
+	g_free (xml);
+	g_assert (priv->introspection_data);
+
+	priv->reg_id = g_dbus_connection_register_object (priv->conn,
+						      priv->path,
+						      priv->introspection_data->interfaces[0],
+						      &interface_vtable,
+						      agent,
+						      NULL,
+						      &error);
+	if (priv->reg_id == 0) {
+		g_warning ("Failed to register object: %s", error->message);
+		g_error_free (error);
+	}
+
+	return TRUE;
+}
+
+void obex_agent_set_release_func(ObexAgent *agent,
+				ObexAgentReleaseFunc func, gpointer data)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	DBG("agent %p", agent);
+
+	priv->release_func = func;
+	priv->release_data = data;
+}
+
+void obex_agent_set_request_func(ObexAgent *agent,
+				ObexAgentRequestFunc func, gpointer data)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	DBG("agent %p", agent);
+
+	priv->request_func = func;
+	priv->request_data = data;
+}
+
+void obex_agent_set_progress_func(ObexAgent *agent,
+				ObexAgentProgressFunc func, gpointer data)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	DBG("agent %p", agent);
+
+	priv->progress_func = func;
+	priv->progress_data = data;
+}
+
+void obex_agent_set_complete_func(ObexAgent *agent,
+				ObexAgentCompleteFunc func, gpointer data)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	DBG("agent %p", agent);
+
+	priv->complete_func = func;
+	priv->complete_data = data;
+}
+
+void obex_agent_set_error_func(ObexAgent *agent,
+			       ObexAgentErrorFunc func, gpointer data)
+{
+	ObexAgentPrivate *priv = OBEX_AGENT_GET_PRIVATE(agent);
+
+	DBG("agent %p", agent);
+
+	priv->error_func = func;
+	priv->error_data = data;
+}
diff --git a/lib/obex-agent.h b/sendto/obex-agent.h
similarity index 83%
rename from lib/obex-agent.h
rename to sendto/obex-agent.h
index fbdb6bd..d2a9e1f 100644
--- a/lib/obex-agent.h
+++ b/sendto/obex-agent.h
@@ -25,7 +25,7 @@
 #define __OBEX_AGENT_H
 
 #include <glib-object.h>
-#include <dbus/dbus-glib.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -58,17 +58,17 @@ ObexAgent *obex_agent_new(void);
 
 gboolean obex_agent_setup(ObexAgent *agent, const char *path);
 
-typedef gboolean (*ObexAgentReleaseFunc) (DBusGMethodInvocation *context,
+typedef gboolean (*ObexAgentReleaseFunc) (GDBusMethodInvocation *context,
 								gpointer data);
-typedef gboolean (*ObexAgentRequestFunc) (DBusGMethodInvocation *context,
-					DBusGProxy *transfer, gpointer data);
-typedef gboolean (*ObexAgentProgressFunc) (DBusGMethodInvocation *context,
-					DBusGProxy *transfer,
+typedef gboolean (*ObexAgentRequestFunc) (GDBusMethodInvocation *context,
+					GDBusProxy *transfer, gpointer data);
+typedef gboolean (*ObexAgentProgressFunc) (GDBusMethodInvocation *context,
+					GDBusProxy *transfer,
 					guint64 transferred, gpointer data);
-typedef gboolean (*ObexAgentCompleteFunc) (DBusGMethodInvocation *context,
-					DBusGProxy *transfer, gpointer data);
-typedef gboolean (*ObexAgentErrorFunc) (DBusGMethodInvocation *context,
-					DBusGProxy *transfer,
+typedef gboolean (*ObexAgentCompleteFunc) (GDBusMethodInvocation *context,
+					GDBusProxy *transfer, gpointer data);
+typedef gboolean (*ObexAgentErrorFunc) (GDBusMethodInvocation *context,
+					GDBusProxy *transfer,
 					const char *message,
 					gpointer data);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]