aboutsummaryrefslogtreecommitdiff
path: root/src/profile.vala
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2013-08-05 19:36:17 -0500
committerCharles Kerr <charles.kerr@canonical.com>2013-08-05 19:36:17 -0500
commit6492053ff654afbac9a078b7b67cca4a6dabecec (patch)
tree02ff781afaefd77bebfacbe4fcc54a6e312911fe /src/profile.vala
parent2d03420649eb330e7bae73929d98f0a8ea755ff7 (diff)
downloadayatana-indicator-bluetooth-6492053ff654afbac9a078b7b67cca4a6dabecec.tar.gz
ayatana-indicator-bluetooth-6492053ff654afbac9a078b7b67cca4a6dabecec.tar.bz2
ayatana-indicator-bluetooth-6492053ff654afbac9a078b7b67cca4a6dabecec.zip
promote shared functions up from Desktop to Profile so that Phone can use it too
Diffstat (limited to 'src/profile.vala')
-rw-r--r--src/profile.vala105
1 files changed, 97 insertions, 8 deletions
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 ();
+ }
}