aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Ancell <robert.ancell@canonical.com>2012-10-11 18:20:47 +1300
committerRobert Ancell <robert.ancell@canonical.com>2012-10-11 18:20:47 +1300
commit072e04865a83f46449b3f8b9f766c54294b0474d (patch)
tree88acaa612faa510630f39e290b0ee63fc5d9af8f /src
parentca444db4d3d147b0747b563bba8ff713d5b41353 (diff)
downloadayatana-indicator-bluetooth-072e04865a83f46449b3f8b9f766c54294b0474d.tar.gz
ayatana-indicator-bluetooth-072e04865a83f46449b3f8b9f766c54294b0474d.tar.bz2
ayatana-indicator-bluetooth-072e04865a83f46449b3f8b9f766c54294b0474d.zip
Abstract out bluez more
Diffstat (limited to 'src')
-rw-r--r--src/bluez.vala139
-rw-r--r--src/indicator-bluetooth.vala72
2 files changed, 156 insertions, 55 deletions
diff --git a/src/bluez.vala b/src/bluez.vala
index 14984bf..0a5a393 100644
--- a/src/bluez.vala
+++ b/src/bluez.vala
@@ -9,34 +9,163 @@
* license.
*/
+public class BluezManager : Object
+{
+ public BluezAdapter default_adapter;
+
+ public BluezManager ()
+ {
+ }
+
+ public void start () throws IOError
+ {
+ proxy = Bus.get_proxy_sync<BluezManagerInterface> (BusType.SYSTEM, "org.bluez", "/");
+ proxy.default_adapter_changed.connect (default_adapter_changed_cb);
+ default_adapter_changed_cb (proxy.default_adapter ());
+ }
+
+ private BluezManagerInterface proxy;
+
+ private void default_adapter_changed_cb (string path)
+ {
+ default_adapter = new BluezAdapter (path);
+ }
+}
+
+public class BluezAdapter : Object
+{
+ public List<BluezDevice> get_devices ()
+ {
+ var devices = new List<BluezDevice> ();
+ foreach (var device in _devices)
+ devices.append (device);
+ return devices;
+ }
+
+ private bool _discoverable = false;
+ public bool discoverable
+ {
+ get { return _discoverable; }
+ set
+ {
+ proxy.set_property ("Discoverable", new Variant.boolean (value));
+ }
+ }
+
+ internal string path;
+ private List<BluezDevice> _devices;
+ private BluezAdapterInterface proxy;
+
+ internal BluezAdapter (string path)
+ {
+ this.path = path;
+ _devices = new List<BluezDevice> ();
+ proxy = Bus.get_proxy_sync<BluezAdapterInterface> (BusType.SYSTEM, "org.bluez", path);
+
+ proxy.property_changed.connect (property_changed_cb);
+ var properties = proxy.get_properties ();
+ var iter = HashTableIter<string, Variant> (properties);
+ string name;
+ Variant value;
+ while (iter.next (out name, out value))
+ property_changed_cb (name, value);
+
+ proxy.device_created.connect (device_created_cb);
+ foreach (var device_path in proxy.list_devices ())
+ device_created_cb (device_path);
+ }
+
+ private void property_changed_cb (string name, Variant value)
+ {
+ stderr.printf ("%s %s=%s\n", path, name, value.print (false));
+ if (name == "Discoverable" && value.is_of_type (VariantType.BOOLEAN))
+ _discoverable = value.get_boolean ();
+ }
+
+ private void device_created_cb (string path)
+ {
+ foreach (var device in _devices)
+ if (device.path == path)
+ return;
+
+ var device = new BluezDevice (path);
+ _devices.append (device);
+ }
+}
+
+public class BluezDevice : Object
+{
+ private string _name = null;
+ public string name { get { return _name; } }
+
+ internal string path;
+ private BluezDeviceInterface proxy;
+
+ internal BluezDevice (string path)
+ {
+ this.path = path;
+ proxy = Bus.get_proxy_sync<BluezDeviceInterface> (BusType.SYSTEM, "org.bluez", path);
+
+ proxy.property_changed.connect (property_changed_cb);
+ var properties = proxy.get_properties ();
+ var iter = HashTableIter<string, Variant> (properties);
+ string name;
+ Variant value;
+ while (iter.next (out name, out value))
+ property_changed_cb (name, value);
+
+ //var input_device = Bus.get_proxy_sync<BluezInputInterface> (BusType.SYSTEM, "org.bluez", path);
+ //input_device.property_changed.connect (input_property_changed_cb);
+ }
+
+ private void property_changed_cb (string name, Variant value)
+ {
+ stderr.printf ("%s %s=%s\n", path, name, value.print (false));
+ if (name == "Name" && value.is_of_type (VariantType.STRING))
+ _name = value.get_string ();
+ }
+
+ private void input_property_changed_cb (string name, Variant value)
+ {
+ stderr.printf ("%s i %s=%s\n", path, name, value.print (false));
+ }
+}
+
[DBus (name = "org.bluez.Manager")]
-public interface BluezManager : Object
+private interface BluezManagerInterface : Object
{
public abstract string default_adapter () throws IOError;
+ public signal void default_adapter_changed (string path);
}
[DBus (name = "org.bluez.Adapter")]
-public interface BluezAdapter : Object
+private interface BluezAdapterInterface : Object
{
public abstract string[] list_devices () throws IOError;
public abstract HashTable<string, Variant> get_properties () throws IOError;
public abstract void set_property (string name, Variant value) throws IOError;
+ public signal void property_changed (string name, Variant value);
+ public signal void device_created (string path);
}
[DBus (name = "org.bluez.Device")]
-public interface BluezDevice : Object
+private interface BluezDeviceInterface : Object
{
public abstract HashTable<string, Variant> get_properties () throws IOError;
+ public signal void property_changed (string name, Variant value);
}
[DBus (name = "org.bluez.Audio")]
-public interface BluezAudio : Object
+private interface BluezAudioInterface : Object
{
public abstract void connect () throws IOError;
}
[DBus (name = "org.bluez.Input")]
-public interface BluezInput : Object
+private interface BluezInputInterface : Object
{
public abstract void connect () throws IOError;
+ public abstract void disconnect () throws IOError;
+ public abstract HashTable<string, Variant> get_properties () throws IOError;
+ public signal void property_changed (string name, Variant value);
}
diff --git a/src/indicator-bluetooth.vala b/src/indicator-bluetooth.vala
index a157ab1..952613e 100644
--- a/src/indicator-bluetooth.vala
+++ b/src/indicator-bluetooth.vala
@@ -33,17 +33,8 @@ public class BluetoothIndicator : AppIndicator.Indicator
rfkill.device_deleted.connect (update_rfkill);
/* Get/control bluetooth status from Bluez */
- BluezAdapter adapter = null;
- try
- {
- var manager = Bus.get_proxy_sync<BluezManager> (BusType.SYSTEM, "org.bluez", "/");
- var path = manager.default_adapter ();
- adapter = Bus.get_proxy_sync<BluezAdapter> (BusType.SYSTEM, "org.bluez", path);
- }
- catch (IOError e)
- {
- stderr.printf ("Failed to connect to Bluez: %s", e.message);
- }
+ var bluez = new BluezManager ();
+ bluez.start ();
set_status (AppIndicator.IndicatorStatus.ACTIVE);
@@ -60,7 +51,7 @@ public class BluetoothIndicator : AppIndicator.Indicator
menu.append (enable_item);
visible_item = new Gtk.CheckMenuItem.with_label (_("Visible"));
- visible_item.activate.connect (() => { adapter.set_property ("Discoverable", new Variant.boolean (true)); }); // FIXME: Make rw
+ visible_item.activate.connect (() => { bluez.default_adapter.discoverable = true; }); // FIXME: Make rw
menu.append (visible_item);
devices_separator = new Gtk.SeparatorMenuItem ();
@@ -73,45 +64,26 @@ public class BluetoothIndicator : AppIndicator.Indicator
device_items = new List<Gtk.MenuItem> ();
- try
- {
- var devices = adapter.list_devices ();
- foreach (var path in devices)
- {
- var device = Bus.get_proxy_sync<BluezDevice> (BusType.SYSTEM, "org.bluez", path);
- var properties = device.get_properties ();
- var iter = HashTableIter<string, Variant> (properties);
- string name;
- Variant value;
- //stderr.printf ("%s\n", path);
- while (iter.next (out name, out value))
- {
- //stderr.printf (" %s=%s\n", name, value.print (false));
- if (name == "Name" && value.is_of_type (VariantType.STRING))
- {
- var item = new Gtk.MenuItem.with_label (value.get_string ());
- device_items.append (item);
- menu.append (item);
-
- item.submenu = new Gtk.Menu ();
- var i = new Gtk.MenuItem.with_label (_("Send files..."));
- i.visible = true;
- i.activate.connect (() => { Process.spawn_command_line_async ("bluetooth-sendto --device=DEVICE --name=NAME"); });
- item.submenu.append (i);
-
- //var i = new Gtk.MenuItem.with_label (_("Keyboard Settings..."));
- //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center keyboard"); });
- //var i = new Gtk.MenuItem.with_label (_("Mouse and Touchpad Settings..."));
- //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center mouse"); });
- //var i = new Gtk.MenuItem.with_label (_("Sound Settings..."));
- //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center sound"); });
- }
- }
- }
- }
- catch (IOError e)
+ var devices = bluez.default_adapter.get_devices ();
+ foreach (var device in devices)
{
- stderr.printf ("%s\n", e.message);
+ var item = new Gtk.MenuItem.with_label (device.name);
+ device_items.append (item);
+ menu.append (item);
+
+ item.submenu = new Gtk.Menu ();
+ var i = new Gtk.MenuItem.with_label (_("Send files..."));
+ i.visible = true;
+ i.activate.connect (() => { Process.spawn_command_line_async ("bluetooth-sendto --device=DEVICE --name=NAME"); }); // FIXME
+ item.submenu.append (i);
+
+ //FIXME
+ //var i = new Gtk.MenuItem.with_label (_("Keyboard Settings..."));
+ //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center keyboard"); });
+ //var i = new Gtk.MenuItem.with_label (_("Mouse and Touchpad Settings..."));
+ //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center mouse"); });
+ //var i = new Gtk.MenuItem.with_label (_("Sound Settings..."));
+ //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center sound"); });
}
var sep = new Gtk.SeparatorMenuItem ();