From cf894302767d5ec483b5f8002bb796a284895bbf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 1 Aug 2013 18:21:05 -0500 Subject: add a bluetooth backend to track bluetooth being enabled, being hard/soft blocked, and its devices. --- src/desktop.vala | 121 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 87 insertions(+), 34 deletions(-) (limited to 'src/desktop.vala') diff --git a/src/desktop.vala b/src/desktop.vala index bd005f5..351770a 100644 --- a/src/desktop.vala +++ b/src/desktop.vala @@ -20,80 +20,133 @@ class DesktopMenu: BluetoothMenu { private Settings settings; + private Bluetooth bluetooth; - private Action[] actions; + private SimpleAction root_action; + private Action[] all_actions; public override void add_actions_to_group (SimpleActionGroup group) { base.add_actions_to_group (group); - for (var i=0; i { message("visible toggled"); }); - this.actions = {}; - this.actions += new SimpleAction.stateful ("root-desktop", null, action_state_for_root()); - this.actions += create_settings_action (); - this.actions += create_wizard_action (); + this.root_action = new SimpleAction.stateful ("root-desktop", null, action_state_for_root()); + + this.all_actions = {}; + this.all_actions += this.root_action; + this.all_actions += create_enabled_action (bluetooth); + this.all_actions += create_discoverable_action (bluetooth); + this.all_actions += create_settings_action (); + this.all_actions += create_wizard_action (); + + bluetooth.notify.connect (() => this.update_root_action_state()); + settings.changed["visible"].connect (()=> this.update_root_action_state()); + + Menu section; + MenuItem item; - var section = new Menu (); + section = new Menu (); + item = new MenuItem ("Bluetooth", "indicator.desktop-enabled"); + item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.switch"); + section.append_item (item); + item = new MenuItem ("Visible", "indicator.desktop-discoverable"); + item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.switch"); + section.append_item (item); + this.menu.append_section (null, section); + + section = new Menu (); section.append (_("Set Up New Deviceā€¦"), "indicator.desktop-wizard"); section.append (_("Bluetooth Settingsā€¦"), "indicator.desktop-settings"); this.menu.append_section (null, section); } - Action create_wizard_action () + Action create_enabled_action (Bluetooth bluetooth) { - var action = new SimpleAction ("desktop-wizard", null); + var action = new SimpleAction.stateful ("desktop-enabled", null, !bluetooth.blocked); + action.activate.connect (() => action.set_state (!action.get_state().get_boolean())); + action.notify["state"].connect (() => bluetooth.try_set_blocked (!action.get_state().get_boolean())); + bluetooth.notify["blocked"].connect (() => action.set_state (!bluetooth.blocked)); + return action; + } - action.activate.connect ((action, param) => { - try { - Process.spawn_command_line_async ("bluetooth-wizard"); - } catch (Error e) { - warning ("unable to launch settings: %s", e.message); - } - }); + Action create_discoverable_action (Bluetooth bluetooth) + { + var action = new SimpleAction.stateful ("desktop-discoverable", null, bluetooth.discoverable); + action.set_enabled (bluetooth.powered); + action.activate.connect (() => action.set_state (!action.get_state().get_boolean())); + action.notify["state"].connect (() => bluetooth.try_set_discoverable (action.get_state().get_boolean())); + bluetooth.notify["discoverable"].connect (() => action.set_state (bluetooth.discoverable)); + bluetooth.notify["powered"].connect (() => action.set_enabled (bluetooth.powered)); + return action; + } + void spawn_command_line_async (string command) + { + try { + Process.spawn_command_line_async (command); + } catch (Error e) { + warning ("unable to launch '%s': %s", command, e.message); + } + } + + Action create_wizard_action () + { + var action = new SimpleAction ("desktop-wizard", null); + action.activate.connect (() => spawn_command_line_async ("bluetooth-wizard")); return action; } Action create_settings_action () { var action = new SimpleAction ("desktop-settings", null); - - action.activate.connect ((action, param) => { - try { - Process.spawn_command_line_async ("gnome-control-center bluetooth"); - } catch (Error e) { - warning ("unable to launch settings: %s", e.message); - } - }); - + action.activate.connect (() => spawn_command_line_async ("gnome-control-center bluetooth")); return action; } protected Variant action_state_for_root () { - var label = "Hello"; // FIXME - var a11y = "Hello"; // FIXME - var visible = true; // FIXME + bool blocked = bluetooth.blocked; + bool powered = bluetooth.powered; + + settings.changed["visible"].connect (()=> this.update_root_action_state()); + + bool visible = powered && settings.get_boolean("visible"); + + string a11y; + string icon_name; + if (powered && !blocked) + { + a11y = "Bluetooth (on)"; + icon_name = "bluetooth-active"; + } + else + { + a11y = "Bluetooth (off)"; + icon_name = "bluetooth-disabled"; + } - string icon_name = "bluetooth-active"; // FIXME: enabled, disabled, connected, etc. -//indicator-bluetooth-service.vala: bluetooth_service._icon_name = enabled ? "bluetooth-active" : "bluetooth-disabled"; var icon = new ThemedIcon.with_default_fallbacks (icon_name); var builder = new VariantBuilder (new VariantType ("a{sv}")); builder.add ("{sv}", "visible", new Variant ("b", visible)); - builder.add ("{sv}", "label", new Variant ("s", label)); builder.add ("{sv}", "accessible-desc", new Variant ("s", a11y)); builder.add ("{sv}", "icon", icon.serialize()); return builder.end (); } + + void update_root_action_state () + { + root_action.set_state (action_state_for_root ()); + } } -- cgit v1.2.3