[telepathy-doc/master] Beginning of a sender example

Davyd Madeley davyd at madeley.id.au
Tue Apr 7 18:20:41 PDT 2009


---
 docs/examples/glib_salut_ft/sender.c |  122 ++++++++++++----------------------
 1 files changed, 43 insertions(+), 79 deletions(-)

diff --git a/docs/examples/glib_salut_ft/sender.c b/docs/examples/glib_salut_ft/sender.c
index 5d558cf..d098e2b 100644
--- a/docs/examples/glib_salut_ft/sender.c
+++ b/docs/examples/glib_salut_ft/sender.c
@@ -7,6 +7,7 @@
 
 #include <glib.h>
 #include <glib-object.h>
+#include <gio/gio.h>
 
 #include <telepathy-glib/connection-manager.h>
 #include <telepathy-glib/connection.h>
@@ -140,14 +141,13 @@ file_transfer_channel_ready (TpChannel		*channel,
 	handle_error (in_error);
 
 	GHashTable *map = tp_channel_borrow_immutable_properties (channel);
-	tp_asv_dump (map);
 
 	const char *filename = tp_asv_get_string (map,
 			TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename");
 	guint64 size = tp_asv_get_uint64 (map,
 			TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", NULL);
 
-	g_print ("New file transfer from %s -- `%s' (%llu bytes)\n",
+	g_print ("New file transfer to %s -- `%s' (%llu bytes)\n",
 			tp_channel_get_identifier (channel),
 			filename, size);
 
@@ -177,80 +177,27 @@ file_transfer_channel_ready (TpChannel		*channel,
 
 		GValue *value = tp_g_value_slice_new_static_string ("");
 
-		/* let us accept the file */
-		tp_cli_channel_type_file_transfer_call_accept_file (channel,
-				-1, TP_SOCKET_ADDRESS_TYPE_UNIX,
-				TP_SOCKET_ACCESS_CONTROL_LOCALHOST,
-				value, 0,
-				file_transfer_unix_cb,
-				state, NULL, NULL);
 
 		tp_g_value_slice_free (value);
 	}
 }
 
 static void
-new_channels_cb (TpConnection		*conn,
-                 const GPtrArray	*channels,
-		 gpointer		 user_data,
-		 GObject		*weak_obj)
+create_ft_channel_cb (TpConnection	*conn,
+                      const char	*object_path,
+		      GHashTable	*properties,
+		      const GError	*in_error,
+		      gpointer		 user_data,
+		      GObject		*weak_obj)
 {
 	GError *error = NULL;
-
-	/* channels has the D-Bus type a(oa{sv}), which decomposes to:
-	 *  - a GPtrArray containing a GValueArray for each channel
-	 *  - each GValueArray contains
-	 *     - an object path
-	 *     - an a{sv} map
-	 */
-
-	int i;
-	for (i = 0; i < channels->len; i++)
-	{
-		GValueArray *channel = g_ptr_array_index (channels, i);
-		char *object_path = g_value_get_boxed (
-				g_value_array_get_nth (channel, 0));
-		GHashTable *map = g_value_get_boxed (
-				g_value_array_get_nth (channel, 1));
-
-		const char *type = tp_asv_get_string (map,
-				TP_IFACE_CHANNEL ".ChannelType");
-		int handle_type = tp_asv_get_uint32 (map,
-				TP_IFACE_CHANNEL ".TargetHandleType", NULL);
-		const char *id = tp_asv_get_string (map,
-				TP_IFACE_CHANNEL ".TargetID");
-
-		g_print ("New channel: %s\n", type);
-
-		if (!strcmp (type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
-		{
-			/* new incoming file transfer, set up the channel */
-			TpChannel *ft = tp_channel_new_from_properties (conn,
-					object_path, map, &error);
-			handle_error (error);
-
-			tp_channel_call_when_ready (ft,
-					file_transfer_channel_ready,
-					NULL);
-		}
-	}
-}
-
-static void
-get_channels_cb (TpProxy	*proxy,
-		 const GValue	*value,
-		 const GError	*in_error,
-		 gpointer	 user_data,
-		 GObject	*weak_obj)
-{
 	handle_error (in_error);
 
-	g_return_if_fail (G_VALUE_HOLDS (value,
-				TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST));
-
-	GPtrArray *channels = g_value_get_boxed (value);
+	TpChannel *channel = tp_channel_new_from_properties (conn, object_path,
+			properties, &error);
+	handle_error (error);
 
-	new_channels_cb (conn, channels, user_data, weak_obj);
+	tp_channel_call_when_ready (channel, file_transfer_channel_ready, NULL);
 }
 
 static void
@@ -258,6 +205,7 @@ conn_ready (TpConnection	*conn,
             const GError	*in_error,
 	    gpointer		 user_data)
 {
+	char **argv = (char **) user_data;
 	GError *error = NULL;
 
 	g_print (" > conn_ready\n");
@@ -268,18 +216,33 @@ conn_ready (TpConnection	*conn,
 	if (tp_proxy_has_interface_by_id (conn,
 		TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
 	{
-		/* request the current channels */
-		tp_cli_dbus_properties_call_get (conn, -1,
-				TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
-				"Channels",
-				get_channels_cb,
+		/* FIXME: we should check that our client has the
+		 * FT capability */
+
+		GFile *file = g_file_new_for_commandline_arg (argv[3]);
+		GFileInfo *info = g_file_query_info (file,
+				"standard::*",
+				G_FILE_QUERY_INFO_NONE,
+				NULL, &error);
+		handle_error (error);
+
+		GHashTable *props = tp_asv_new (
+			TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
+			TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
+			TP_IFACE_CHANNEL ".TargetID", G_TYPE_STRING, argv[4],
+			TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", G_TYPE_STRING, g_file_info_get_display_name (info),
+			TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", G_TYPE_STRING, g_file_info_get_content_type (info),
+			TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", G_TYPE_UINT64, g_file_info_get_size (info),
+			NULL);
+
+		tp_cli_connection_interface_requests_call_create_channel (
+				conn, -1, props,
+				create_ft_channel_cb,
 				NULL, NULL, NULL);
 
-		/* notify of all new channels */
-		tp_cli_connection_interface_requests_connect_to_new_channels (
-				conn, new_channels_cb,
-				NULL, NULL, NULL, &error);
-		handle_error (error);
+		g_hash_table_destroy (props);
+		g_object_unref (info);
+		g_object_unref (file);
 	}
 }
 
@@ -309,6 +272,7 @@ request_connection_cb (TpConnectionManager	*cm,
 		       gpointer			 user_data,
 		       GObject			*weak_object)
 {
+	char **argv = (char **) user_data;
 	GError *error = NULL;
 
 	if (in_error) g_error ("%s", in_error->message);
@@ -316,7 +280,7 @@ request_connection_cb (TpConnectionManager	*cm,
 	conn = tp_connection_new (bus_daemon, bus_name, object_path, &error);
 	if (error) g_error ("%s", error->message);
 
-	tp_connection_call_when_ready (conn, conn_ready, NULL);
+	tp_connection_call_when_ready (conn, conn_ready, argv);
 
 	tp_cli_connection_connect_to_status_changed (conn, status_changed_cb,
 			NULL, NULL, NULL, &error);
@@ -351,7 +315,7 @@ cm_ready (TpConnectionManager	*cm,
 			"local-xmpp",
 			parameters,
 			request_connection_cb,
-			NULL, NULL, NULL);
+			argv, NULL, NULL);
 
 	g_hash_table_destroy (parameters);
 }
@@ -371,9 +335,9 @@ main (int argc, char **argv)
 
 	g_type_init ();
 
-	if (argc != 3)
+	if (argc != 5)
 	{
-		g_error ("Must provide first name and last name!");
+		g_error ("Must provide first name, last name, filename and targetID!");
 	}
 
 	/* create a main loop */
-- 
1.5.6.5




More information about the telepathy-commits mailing list