[gnome-bluetooth/meego-dev] Make the powerswitch affect all adaptors



commit 5a1e6d6c7c1c16da5ff9de223e299c7127d8b52b
Author: Ross Burton <ross linux intel com>
Date:   Mon Mar 22 09:15:26 2010 +0000

    Make the powerswitch affect all adaptors

 moblin/bluetooth-powerswitch.c |   73 ++++++++++++++++++++++++++++++++-------
 moblin/bluetooth-powerswitch.h |    1 +
 2 files changed, 61 insertions(+), 13 deletions(-)
---
diff --git a/moblin/bluetooth-powerswitch.c b/moblin/bluetooth-powerswitch.c
index d062784..7df569f 100644
--- a/moblin/bluetooth-powerswitch.c
+++ b/moblin/bluetooth-powerswitch.c
@@ -4,6 +4,7 @@
  *
  *  Copyright (C) 2005-2008  Marcel Holtmann <marcel holtmann org>
  *  Copyright (C) 2006-2009  Bastien Nocera <hadess hadess net>
+ *  Copyright (C) 2010       Intel Corp
  *
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -51,9 +52,40 @@ static int signals[LAST_SIGNAL] = { 0 };
 
 struct _BluetoothPowerswitchPrivate {
 	BluetoothClient *client;
+	GtkTreeModel *adapters;
 };
 
-G_DEFINE_TYPE(BluetoothPowerswitch, bluetooth_powerswitch, G_TYPE_OBJECT)
+G_DEFINE_TYPE (BluetoothPowerswitch, bluetooth_powerswitch, G_TYPE_OBJECT)
+
+static gboolean
+set_state_foreach (GtkTreeModel *model,
+		   GtkTreePath	*path,
+		   GtkTreeIter	*iter,
+		   gpointer	 data)
+{
+	DBusGProxy *proxy = NULL;
+	GValue value = { 0, };
+	PowerswitchState state = GPOINTER_TO_INT (data);
+
+	gtk_tree_model_get (model, iter, BLUETOOTH_COLUMN_PROXY, &proxy, -1);
+	if (proxy == NULL)
+		return FALSE;
+
+	g_value_init (&value, G_TYPE_BOOLEAN);
+	g_value_set_boolean (&value, state);
+
+	/* TODO: do this async to avoid blocking. Maybe _no_reply will do? */
+	dbus_g_proxy_call (proxy, "SetProperty", NULL,
+			   G_TYPE_STRING, "Powered",
+			   G_TYPE_VALUE, &value,
+			   G_TYPE_INVALID,
+			   G_TYPE_INVALID);
+
+	g_value_unset (&value);
+	g_object_unref (proxy);
+
+	return FALSE;
+}
 
 void
 bluetooth_powerswitch_set_state (BluetoothPowerswitch *powerswitch,
@@ -64,21 +96,43 @@ bluetooth_powerswitch_set_state (BluetoothPowerswitch *powerswitch,
 	g_return_if_fail (state == POWERSWITCH_STATE_OFF ||
 			  state == POWERSWITCH_STATE_ON);
 
-	g_object_set (priv->client, "default-adapter-powered", state, NULL);
+	gtk_tree_model_foreach (priv->adapters, set_state_foreach, GINT_TO_POINTER (state));
+}
+
+static gboolean
+get_state_foreach (GtkTreeModel *model,
+		   GtkTreePath	*path,
+		   GtkTreeIter	*iter,
+		   gpointer	 data)
+{
+	gboolean powered = FALSE;
+	gboolean *global_powered = data;
+
+	gtk_tree_model_get (model, iter, BLUETOOTH_COLUMN_POWERED, &powered, -1);
+
+	if (powered) {
+		/* Found a powered adaptor, so we are "on" */
+		*global_powered = TRUE;
+		return TRUE;
+	} else {
+		/* Found an unpowered adaptor, set "off" but continue looking */
+		*global_powered = FALSE;
+		return FALSE;
+	}
 }
 
 PowerswitchState
 bluetooth_powerswitch_get_state (BluetoothPowerswitch *powerswitch)
 {
 	BluetoothPowerswitchPrivate *priv;
-	gboolean powered;
+	gboolean powered = FALSE;
 
 	g_return_val_if_fail (BLUETOOTH_IS_POWERSWITCH (powerswitch), POWERSWITCH_STATE_NO_ADAPTER);
 
 	priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
 
 	if (bluetooth_powerswitch_has_powerswitches (powerswitch)) {
-		g_object_get (priv->client, "default-adapter-powered", &powered, NULL);
+		gtk_tree_model_foreach (priv->adapters, get_state_foreach, &powered);
 		return powered;
 	} else {
 		return POWERSWITCH_STATE_NO_ADAPTER;
@@ -89,18 +143,10 @@ gboolean
 bluetooth_powerswitch_has_powerswitches (BluetoothPowerswitch *powerswitch)
 {
 	BluetoothPowerswitchPrivate *priv = BLUETOOTH_POWERSWITCH_GET_PRIVATE (powerswitch);
-	char *adapter = NULL;
 
 	g_return_val_if_fail (BLUETOOTH_IS_POWERSWITCH (powerswitch), FALSE);
 
-	g_object_get (priv->client, "default-adapter", &adapter, NULL);
-
-	if (adapter) {
-		g_free (adapter);
-		return TRUE;
-	} else {
-		return FALSE;
-	}
+	return gtk_tree_model_iter_n_children (priv->adapters, NULL);
 }
 
 static void
@@ -121,6 +167,7 @@ bluetooth_powerswitch_init (BluetoothPowerswitch *powerswitch)
 	powerswitch->priv = priv;
 
 	priv->client = bluetooth_client_new ();
+	priv->adapters = bluetooth_client_get_adapter_model(priv->client);
 
 	g_signal_connect (priv->client, "notify::default-adapter-powered",
 			  G_CALLBACK (powered_notify_cb), powerswitch);
diff --git a/moblin/bluetooth-powerswitch.h b/moblin/bluetooth-powerswitch.h
index 9afa24a..b419402 100644
--- a/moblin/bluetooth-powerswitch.h
+++ b/moblin/bluetooth-powerswitch.h
@@ -4,6 +4,7 @@
  *
  *  Copyright (C) 2005-2008  Marcel Holtmann <marcel holtmann org>
  *  Copyright (C) 2006-2009  Bastien Nocera <hadess hadess net>
+ *  Copyright (C) 2010       Intel Corp
  *
  *
  *  This program is free software; you can redistribute it and/or modify



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