From 6492053ff654afbac9a078b7b67cca4a6dabecec Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 5 Aug 2013 19:36:17 -0500 Subject: promote shared functions up from Desktop to Profile so that Phone can use it too --- src/desktop.vala | 95 +++++++++---------------------------------------- src/phone.vala | 53 +++++++++++++--------------- src/profile.vala | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 139 insertions(+), 114 deletions(-) (limited to 'src') diff --git a/src/desktop.vala b/src/desktop.vala index 5eb914b..24d7f9f 100644 --- a/src/desktop.vala +++ b/src/desktop.vala @@ -21,10 +21,8 @@ class Desktop: Profile { private uint idle_rebuild_id = 0; private Settings settings; - private Bluetooth bluetooth; private SimpleActionGroup action_group; - private SimpleAction root_action; private Menu device_section; private HashTable connect_actions; @@ -41,20 +39,19 @@ class Desktop: Profile public Desktop (Bluetooth bluetooth, SimpleActionGroup action_group) { - base ("desktop"); + const string profile_name = "desktop"; + + base (bluetooth, profile_name); - this.bluetooth = bluetooth; this.action_group = action_group; connect_actions = new HashTable(direct_hash, direct_equal); settings = new Settings ("com.canonical.indicator.bluetooth"); - root_action = create_root_action (); - // build the static actions Action[] actions = {}; - actions += root_action; + actions += get_root_action (profile_name); actions += create_enabled_action (bluetooth); actions += create_discoverable_action (bluetooth); actions += create_wizard_action (); @@ -66,8 +63,10 @@ class Desktop: Profile build_menu (); - settings.changed["visible"].connect (()=> update_root_action_state()); - bluetooth.notify.connect (() => update_root_action_state()); + // know when to show the indicator & when to hide it + settings.changed["visible"].connect (()=> update_visibility()); + bluetooth.notify.connect (() => update_visibility()); + update_visibility (); // when devices change, rebuild our device section bluetooth.devices_changed.connect (()=> { @@ -80,6 +79,11 @@ class Desktop: Profile }); } + void update_visibility () + { + visible = bluetooth.powered && !bluetooth.blocked && settings.get_boolean("visible"); + } + /// /// MenuItems /// @@ -165,9 +169,7 @@ class Desktop: Profile // quick toggles section 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); + section.append_item (create_enabled_menuitem ()); item = new MenuItem ("Visible", "indicator.desktop-discoverable"); item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.switch"); section.append_item (item); @@ -185,35 +187,13 @@ class Desktop: Profile menu.append_section (null, section); } - /// - /// Action Helpers - /// - - void spawn_command_line_async (string command) - { - try { - Process.spawn_command_line_async (command); - } catch (Error e) { - warning ("unable to launch '$command': $(e.message)"); - } - } - - void show_control_center (string panel) - { - spawn_command_line_async ("gnome-control-center " + panel); - } - /// /// Actions /// - Action create_enabled_action (Bluetooth bluetooth) + void show_settings (string panel) { - 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; + spawn_command_line_async ("gnome-control-center " + panel); } Action create_discoverable_action (Bluetooth bluetooth) @@ -263,48 +243,7 @@ class Desktop: Profile Action create_show_settings_action () { var action = new SimpleAction ("desktop-show-settings", VariantType.STRING); - action.activate.connect ((action, panel) => show_control_center (panel.get_string())); + action.activate.connect ((action, panel) => show_settings (panel.get_string())); return action; } - - private Variant action_state_for_root () - { - bool blocked = bluetooth.blocked; - bool powered = bluetooth.powered; - - settings.changed["visible"].connect (()=> 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"; - } - - 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}", "accessible-desc", new Variant ("s", a11y)); - builder.add ("{sv}", "icon", icon.serialize()); - return builder.end (); - } - - SimpleAction create_root_action () - { - return new SimpleAction.stateful ("root-desktop", null, action_state_for_root()); - } - - void update_root_action_state () - { - root_action.set_state (action_state_for_root ()); - } } diff --git a/src/phone.vala b/src/phone.vala index de5febe..05e6b9d 100644 --- a/src/phone.vala +++ b/src/phone.vala @@ -19,57 +19,54 @@ class Phone: Profile { - Bluetooth bluetooth; SimpleActionGroup action_group; public Phone (Bluetooth bluetooth, SimpleActionGroup action_group) { - base ("phone"); + const string profile_name = "phone"; + base (bluetooth, profile_name); this.bluetooth = bluetooth; this.action_group = action_group; // build the static actions Action[] actions = {}; - actions += new SimpleAction.stateful ("root-phone", null, action_state_for_root()); + actions += get_root_action (profile_name); + actions += create_enabled_action (bluetooth); actions += create_settings_action (); foreach (var a in actions) action_group.insert (a); var section = new Menu (); - section.append (_("Sound settingsā€¦"), "indicator.phone-settings"); + section.append_item (create_enabled_menuitem ()); + section.append (_("Bluetooth settingsā€¦"), "indicator.phone-show-settings::bluetooth"); menu.append_section (null, section); + + // know when to show the indicator & when to hide it + bluetooth.notify.connect (() => update_visibility()); + update_visibility (); + + bluetooth.notify.connect (() => update_root_action_state()); } - Action create_settings_action () + void update_visibility () { - var action = new SimpleAction ("phone-settings", null); + visible = bluetooth.powered && !bluetooth.blocked; + } - action.activate.connect ((action, param) => { - try { - Process.spawn_command_line_async ("system-settings bluetooth"); - } catch (Error e) { - warning (@"unable to launch settings: $(e.message)"); - } - }); + /// + /// Actions + /// - return action; + void show_settings (string panel) + { + spawn_command_line_async ("system-settings " + panel); } - private Variant action_state_for_root () + Action create_settings_action () { - var label = "Hello World"; // FIXME - var a11y = "Hello World"; // FIXME - var visible = true; // FIXME - - string icon_name = "bluetooth"; // FIXME: enabled, disabled, connected, etc. - 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 (); + var action = new SimpleAction ("phone-show-settings", VariantType.STRING); + action.activate.connect ((action, panel) => show_settings (panel.get_string())); + return action; } } diff --git a/src/profile.vala b/src/profile.vala index d839121..74c8242 100644 --- a/src/profile.vala +++ b/src/profile.vala @@ -19,29 +19,32 @@ class Profile: Object { - protected string name; + protected Bluetooth bluetooth; + protected string profile_name; protected Menu root; protected Menu menu; - public Profile (string name) + protected bool visible { get; set; default = true; } + + public Profile (Bluetooth bluetooth, string profile_name) { - this.name = name; + this.bluetooth = bluetooth; + this.profile_name = profile_name; menu = new Menu (); - var root_item = new MenuItem (null, "indicator.root-" + name); - root_item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); - root_item.set_submenu (menu); + var item = create_root_menuitem (); + item.set_submenu (menu); root = new Menu (); - root.append_item (root_item); + root.append_item (item); } public void export_menu (DBusConnection connection, string object_path) { try { - debug (@"exporting '$name' on $object_path"); + debug (@"exporting '$profile_name' on $object_path"); connection.export_menu_model (object_path, this.root); } catch (Error e) @@ -49,4 +52,90 @@ class Profile: Object critical (@"Unable to export menu on $object_path: $(e.message)"); } } + + protected void spawn_command_line_async (string command) + { + try { + Process.spawn_command_line_async (command); + } catch (Error e) { + warning (@"Unable to launch '$command': $(e.message)"); + } + } + + /// + /// Menu Items + /// + + protected MenuItem create_enabled_menuitem () + { + MenuItem item = new MenuItem ("Bluetooth", "indicator.bluetooth-enabled"); + item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.switch"); + return item; + } + + private MenuItem create_root_menuitem () + { + var item = new MenuItem (null, @"indicator.root-$profile_name"); + item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); + return item; + } + + /// + /// Actions + /// + + protected Action create_enabled_action (Bluetooth bluetooth) + { + var action = new SimpleAction.stateful ("bluetooth-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; + } + + protected SimpleAction root_action = null; + + protected SimpleAction get_root_action (string profile) + { + if (root_action == null) + { + root_action = new SimpleAction.stateful (@"root-$profile", null, action_state_for_root()); + + this.notify["visible"].connect (() => update_root_action_state()); + } + + return root_action; + } + + protected void update_root_action_state () + { + root_action.set_state (action_state_for_root ()); + } + + protected Variant action_state_for_root () + { + bool blocked = bluetooth.blocked; + bool powered = bluetooth.powered; + + string a11y; + string icon_name; + if (powered && !blocked) + { + a11y = "Bluetooth (on)"; + icon_name = "bluetooth-active"; + } + else + { + a11y = "Bluetooth (off)"; + icon_name = "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.boolean (visible)); + builder.add ("{sv}", "accessible-desc", new Variant.string (a11y)); + builder.add ("{sv}", "icon", icon.serialize()); + return builder.end (); + } } -- cgit v1.2.3