aboutsummaryrefslogtreecommitdiff
path: root/src/indicator-bluetooth.vala
blob: fd097388d1fa110a71d00072aad43ddc96229a47 (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
/*
 * Copyright (C) 2012 Canonical Ltd.
 * Author: Robert Ancell <robert.ancell@canonical.com>
 *
 * 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, either version 3 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

public class BluetoothIndicator : AppIndicator.Indicator
{
    private RFKillManager rfkill;
    private Gtk.MenuItem status_item;
    private Gtk.MenuItem enable_item;
    private bool enable_value = false;
    private Gtk.CheckMenuItem visible_item;
    private Gtk.SeparatorMenuItem devices_separator;
    private Gtk.MenuItem devices_item;
    private List<Gtk.MenuItem> device_items;
    private Gtk.MenuItem settings_item;

    public BluetoothIndicator ()
    {
        Object (id: "indicator-bluetooth", icon_name: "bluetooth-active", category: "Hardware");

        /* Monitor killswitch status */
        rfkill = new RFKillManager ();
        rfkill.open ();
        rfkill.device_added.connect (update_rfkill);
        rfkill.device_changed.connect (update_rfkill);
        rfkill.device_deleted.connect (update_rfkill);

        /* Get/control bluetooth status from Bluez */
        var bluez = new BluezManager ();
        bluez.start ();

        set_status (AppIndicator.IndicatorStatus.ACTIVE);

        var menu = new Gtk.Menu ();
        set_menu (menu);

        status_item = new Gtk.MenuItem ();
        status_item.sensitive = false;
        status_item.visible = true;
        menu.append (status_item);

        enable_item = new Gtk.MenuItem ();
        enable_item.activate.connect (toggle_enabled);
        menu.append (enable_item);

        visible_item = new Gtk.CheckMenuItem.with_label (_("Visible"));
        visible_item.active = bluez.default_adapter.discoverable;
        bluez.default_adapter.notify["discoverable"].connect (() => { visible_item.active = bluez.default_adapter.discoverable; });
        visible_item.activate.connect (() => { bluez.default_adapter.discoverable = visible_item.active; });
        menu.append (visible_item);
    
        devices_separator = new Gtk.SeparatorMenuItem ();
        menu.append (devices_separator);

        devices_item = new Gtk.MenuItem.with_label (_("Devices"));
        devices_item.sensitive = false;
        devices_item.visible = true;
        menu.append (devices_item);

        device_items = new List<Gtk.MenuItem> ();

        var devices = bluez.default_adapter.get_devices ();
        foreach (var device in devices)
        {
            var item = new Gtk.MenuItem.with_label (device.name);
            device_items.append (item);
            menu.append (item);

            item.submenu = new Gtk.Menu ();
            var i = new Gtk.MenuItem.with_label (_("Send files..."));
            i.visible = true;
            i.activate.connect (() => { Process.spawn_command_line_async ("bluetooth-sendto --device=DEVICE --name=NAME"); }); // FIXME
            item.submenu.append (i);

            //FIXME
            //var i = new Gtk.MenuItem.with_label (_("Keyboard Settings..."));
            //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center keyboard"); });
            //var i = new Gtk.MenuItem.with_label (_("Mouse and Touchpad Settings..."));
            //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center mouse"); });
            //var i = new Gtk.MenuItem.with_label (_("Sound Settings..."));
            //i.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center sound"); });
        }

        var sep = new Gtk.SeparatorMenuItem ();
        sep.visible = true;
        menu.append (sep);

        settings_item = new Gtk.MenuItem.with_label (_("Bluetooth Settings..."));
        settings_item.activate.connect (() => { Process.spawn_command_line_async ("gnome-control-center bluetooth"); });
        settings_item.visible = true;
        menu.append (settings_item);

        update_rfkill ();
    }

    private void update_rfkill ()
    {
        var have_lock = false;
        var software_locked = false;
        var hardware_locked = false;

        foreach (var device in rfkill.get_devices ())
        {
            if (device.device_type != RFKillDeviceType.BLUETOOTH)
                continue;

            have_lock = true;
            if (device.software_lock)
                software_locked = true;
            if (device.hardware_lock)
                hardware_locked = true;
        }
        var locked = hardware_locked || software_locked;

        if (hardware_locked)
        {
            status_item.label = _("Bluetooth: Disabled");
            enable_item.visible = false;
        }
        else if (software_locked)
        {
            status_item.label = _("Bluetooth: Off");
            enable_item.label = _("Turn on Bluetooth");
            enable_item.visible = true;
            enable_value = false;
        }
        else
        {
            status_item.label = _("Bluetooth: On");
            enable_item.label = _("Turn off Bluetooth");
            enable_item.visible = true;
            enable_value = true;
        }

        /* Disable devices when locked */
        visible_item.visible = !locked;
        devices_separator.visible = !locked;
        devices_item.visible = !locked;
        foreach (var item in device_items)
            item.visible = !locked;
    }

    private void toggle_enabled ()
    {
        rfkill.set_software_lock (RFKillDeviceType.BLUETOOTH, enable_value);
    }
}

public static int main (string[] args)
{
    Intl.setlocale (LocaleCategory.ALL, "");
    Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
    Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
    Intl.textdomain (GETTEXT_PACKAGE);

    Gtk.init (ref args);

    var indicator = new BluetoothIndicator ();
    
    Gtk.main ();
    
    indicator = null;

    return Posix.EXIT_SUCCESS;
}