aboutsummaryrefslogtreecommitdiff
path: root/src/desktop.vala
blob: a7b8995fdc9b09d908768697927fe05d3f3bb7ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

class Desktop: Profile
{
  private Settings settings;
  private Bluetooth bluetooth;

  private SimpleAction root_action;
  private Action[] all_actions;

  public override void add_actions_to_group (SimpleActionGroup group)
  {
    for (var i=0; i<all_actions.length; i++)
      group.insert (all_actions[i]);
  }

  public Desktop (Bluetooth bluetooth)
  {
    base ("desktop");

    this.bluetooth = bluetooth;

    settings = new Settings ("com.canonical.indicator.bluetooth");

    root_action = new SimpleAction.stateful ("root-desktop", null, action_state_for_root());

    all_actions = {};
    all_actions += root_action;
    all_actions += create_enabled_action (bluetooth);
    all_actions += create_discoverable_action (bluetooth);
    all_actions += create_settings_action ();
    all_actions += create_wizard_action ();

    bluetooth.notify.connect (() => update_root_action_state());
    settings.changed["visible"].connect (()=> update_root_action_state());

    Menu section;
    MenuItem item;

    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);
    menu.append_section (null, section);

    section = new Menu ();
    section.append (_("Set Up New Device…"), "indicator.desktop-wizard");
    section.append (_("Bluetooth Settings…"), "indicator.desktop-settings");
    menu.append_section (null, section);
  }

  Action create_enabled_action (Bluetooth bluetooth)
  {
    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 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 (() => spawn_command_line_async ("gnome-control-center bluetooth"));
    return action;
  }

  protected 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 ();
  }

  void update_root_action_state ()
  {
    root_action.set_state (action_state_for_root ());
  }
}