aboutsummaryrefslogtreecommitdiff
path: root/src/profile.vala
blob: f2c9753f6f7ea4c325416274598ab83f3192c096 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * 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 General General Public License for more details.
 *
 * You should have received a copy of the GNU General General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

class Profile: Object
{
  protected Bluetooth bluetooth;
  protected string profile_name;
  protected Menu root;
  protected Menu menu;
  private uint menu_export_id;
  protected SimpleAction root_action;

  protected bool visible { get; set; default = true; }

  public Profile (Bluetooth bluetooth, string profile_name)
  {
    this.bluetooth = bluetooth;
    this.profile_name = profile_name;

    root_action = new SimpleAction.stateful (@"root-$profile_name",
                                             null,
                                             action_state_for_root());
    notify["visible"].connect (() => update_root_action_state());
    bluetooth.notify["enabled"].connect (() => update_root_action_state());
    bluetooth.notify["connected"].connect (() => update_root_action_state());
    menu = new Menu ();

    var item = create_root_menuitem ();
    item.set_submenu (menu);

    root = new Menu ();
    root.append_item (item);
  }

  public void export_menu (DBusConnection connection, string object_path)
  {
    try
      {
        debug (@"exporting menu '$profile_name'");
        menu_export_id = connection.export_menu_model (object_path, root);
      }
    catch (Error e)
      {
        critical (@"Unable to export menu on $object_path: $(e.message)");
      }
  }

  public void unexport_menu (DBusConnection connection)
  {
    if (menu_export_id != 0)
      {
        debug (@"unexporting menu '$profile_name'");
        connection.unexport_menu_model (menu_export_id);
        menu_export_id = 0;
      }
  }

  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 ()
  {
    var item = new MenuItem (_("Bluetooth"), "indicator.bluetooth-enabled");

    item.set_attribute ("x-ayatana-type", "s",
                        "org.ayatana.indicator.switch");

    return item;
  }

  private MenuItem create_root_menuitem ()
  {
    var item = new MenuItem (null, @"indicator.root-$profile_name");

    item.set_attribute ("x-ayatana-type", "s",
                        "org.ayatana.indicator.root");

    return item;
  }

  ///
  ///  Actions
  ///

  protected Action create_supported_action (Bluetooth bluetooth)
  {
    var action = new SimpleAction.stateful ("bluetooth-supported",
                                            null,
                                            new Variant.boolean (bluetooth.supported));

    bluetooth.notify["supported"].connect (()
        => action.set_state (new Variant.boolean (bluetooth.supported)));

    return action;
  }

  protected Action create_enabled_action (Bluetooth bluetooth)
  {
    var action = new SimpleAction.stateful ("bluetooth-enabled",
                                            VariantType.BOOLEAN,
                                            new Variant.boolean (bluetooth.enabled));

    action.activate.connect ((action, state)
        => bluetooth.try_set_enabled (state.get_boolean()));

    bluetooth.notify["enabled"].connect (()
        => action.set_state (new Variant.boolean (bluetooth.enabled)));

    return action;
  }

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

  protected Variant action_state_for_root ()
  {
    string a11y;
    string icon_name;

    if (bluetooth.connected)
      {
        a11y = "Bluetooth (connections)";
        icon_name = "bluetooth-paired";
      }
    else if (bluetooth.enabled)
      {
        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());
    builder.add ("{sv}", "title", new Variant.string (_("Bluetooth")));

    return builder.end ();
  }
}