[gnome-bluetooth] lib: Add send/browser helpers



commit 3cf607c498cc929ea2b0c867078aa8f8ef978810
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Jun 29 15:25:07 2011 +0100

    lib: Add send/browser helpers

 lib/bluetooth-utils.c       |  127 +++++++++++++++++++++++++++++++++++++++++++
 lib/bluetooth-utils.h       |   13 ++++-
 lib/gnome-bluetooth.symbols |    4 ++
 3 files changed, 143 insertions(+), 1 deletions(-)
---
diff --git a/lib/bluetooth-utils.c b/lib/bluetooth-utils.c
index 15c4fa1..8d6ac35 100644
--- a/lib/bluetooth-utils.c
+++ b/lib/bluetooth-utils.c
@@ -37,6 +37,7 @@
 #endif
 
 #include <glib/gi18n-lib.h>
+#include <gtk/gtk.h>
 
 #include "bluetooth-utils.h"
 #include "gnome-bluetooth-enum-types.h"
@@ -298,4 +299,130 @@ bluetooth_uuid_to_string (const char *uuid)
 	return uuid16_custom_to_string (uuid16, uuid);
 }
 
+void
+bluetooth_send_to_address (const char *address,
+			   const char *alias)
+{
+	GPtrArray *a;
+	GError *err = NULL;
+
+	a = g_ptr_array_new_with_free_func ((GDestroyNotify) g_free);
+
+	g_ptr_array_add (a, g_strdup ("bluetooth-sendto"));
+	if (address != NULL)
+		g_ptr_array_add (a, g_strdup_printf ("--device=%s", address));
+	if (address != NULL && alias != NULL)
+		g_ptr_array_add (a, g_strdup_printf ("--name=%s", alias));
+	g_ptr_array_add (a, NULL);
+
+	if (g_spawn_async(NULL, (char **) a->pdata, NULL,
+			  G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &err) == FALSE) {
+		g_printerr("Couldn't execute command: %s\n", err->message);
+		g_error_free (err);
+	}
+
+	g_ptr_array_free (a, TRUE);
+}
+
+typedef struct {
+  GSimpleAsyncResult *result;
+  guint timestamp;
+} MountClosure;
+
+static void
+mount_ready_cb (GObject *object,
+		GAsyncResult *result,
+		gpointer user_data)
+{
+	GError *error = NULL;
+	GFile *file = G_FILE (object);
+	char *uri = g_file_get_uri (file);
+	MountClosure *closure = user_data;
+
+	if (g_file_mount_enclosing_volume_finish (file, result, &error) == FALSE) {
+		/* Ignore "already mounted" error */
+		if (error->domain == G_IO_ERROR &&
+		    error->code == G_IO_ERROR_ALREADY_MOUNTED) {
+			g_error_free (error);
+			error = NULL;
+		}
+	}
+
+	if (!error) {
+		gtk_show_uri (NULL, uri, closure->timestamp, &error);
+	}
+
+	if (error) {
+		g_simple_async_result_set_from_error (closure->result, error);
+		g_error_free (error);
+	} else {
+		g_simple_async_result_set_op_res_gboolean (closure->result, TRUE);
+	}
+
+	g_simple_async_result_complete (closure->result);
+
+	g_free (uri);
+	g_object_unref (closure->result);
+	g_free (closure);
+}
 
+/**
+ * bluetooth_browse_address_finish:
+ *
+ * @object: a #GObject
+ * @result: the #GAsyncResult from the callback
+ * @error: a #GError
+ *
+ * Returns: TRUE if the operation was successful, FALSE if error is set
+ */
+gboolean
+bluetooth_browse_address_finish (GObject      *object,
+				 GAsyncResult *result,
+				 GError      **error)
+{
+	GSimpleAsyncResult *simple;
+
+	g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
+	g_return_val_if_fail (g_simple_async_result_is_valid (result, object, bluetooth_browse_address), FALSE);
+
+	simple = G_SIMPLE_ASYNC_RESULT (result);
+	if (g_simple_async_result_propagate_error (simple, error))
+		return FALSE;
+	else
+		return TRUE;
+}
+
+/**
+ * bluetooth_browse_address:
+ *
+ * Opens a Bluetooth device in Nautilus
+ * @object: a #GObject, such as the top-level of your management application.
+ * @address: the bluetooth device to browse
+ * @callback: (scope async): the completion callback
+ * @user_data:
+ */
+void
+bluetooth_browse_address (GObject             *object,
+			  const char          *address,
+			  guint                timestamp,
+			  GAsyncReadyCallback  callback,
+			  gpointer             user_data)
+{
+	GFile *file;
+	char *uri;
+	MountClosure *closure;
+
+	g_return_if_fail (G_IS_OBJECT (object));
+	g_return_if_fail (address != NULL);
+
+	uri = g_strdup_printf ("obex://[%s]/", address);
+	file = g_file_new_for_uri (uri);
+
+	closure = g_new (MountClosure, 1);
+	closure->result = g_simple_async_result_new (object, callback, user_data, bluetooth_browse_address);
+	closure->timestamp = timestamp;
+	g_file_mount_enclosing_volume(file, G_MOUNT_MOUNT_NONE, NULL, NULL, mount_ready_cb, closure);
+
+	g_free (uri);
+	g_object_unref (file);
+}
diff --git a/lib/bluetooth-utils.h b/lib/bluetooth-utils.h
index f9e0670..53c341b 100644
--- a/lib/bluetooth-utils.h
+++ b/lib/bluetooth-utils.h
@@ -25,7 +25,7 @@
 #ifndef __BLUETOOTH_UTILS_H
 #define __BLUETOOTH_UTILS_H
 
-#include <glib-object.h>
+#include <gio/gio.h>
 #include <bluetooth-enums.h>
 
 G_BEGIN_DECLS
@@ -35,6 +35,17 @@ const gchar   *bluetooth_type_to_string (guint type);
 gboolean       bluetooth_verify_address (const char *bdaddr);
 const char    *bluetooth_uuid_to_string (const char *uuid);
 
+gboolean bluetooth_browse_address_finish (GObject *object,
+					  GAsyncResult *result,
+					  GError **error);
+void bluetooth_browse_address (GObject *object,
+			       const char *address,
+			       guint timestamp,
+			       GAsyncReadyCallback callback,
+			       gpointer user_data);
+void bluetooth_send_to_address (const char *address,
+				const char *alias);
+
 G_END_DECLS
 
 #endif /* __BLUETOOTH_UTILS_H */
diff --git a/lib/gnome-bluetooth.symbols b/lib/gnome-bluetooth.symbols
index a1034dc..0f9df68 100644
--- a/lib/gnome-bluetooth.symbols
+++ b/lib/gnome-bluetooth.symbols
@@ -27,9 +27,13 @@ bluetooth_client_get_filter_model
 bluetooth_client_get_adapter_model
 bluetooth_client_get_device_model
 bluetooth_client_get_device_filter_model
+bluetooth_class_to_type
 bluetooth_type_to_string
 bluetooth_verify_address
 bluetooth_uuid_to_string
+bluetooth_browse_address_finish
+bluetooth_browse_address
+bluetooth_send_to_address
 bluetooth_column_get_type
 bluetooth_category_get_type
 bluetooth_type_get_type



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