aboutsummaryrefslogtreecommitdiff
path: root/src/indicator-bluetooth.vala
blob: 88cb17554de84287a33e1ae2777dcf88ea8d2199 (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
[DBus (name = "org.bluez.Manager")]
interface BluezManager : Object
{
    public abstract string default_adapter () throws IOError;
}

[DBus (name = "org.bluez.Adapter")]
interface BluezAdapter : Object
{
    public abstract string[] list_devices () throws IOError;
    public abstract HashTable<string, Variant> get_properties () throws IOError;
    public abstract void set_property (string name, Variant value) throws IOError;
}

[DBus (name = "org.bluez.Device")]
interface BluezDevice : Object
{
    public abstract HashTable<string, Variant> get_properties () throws IOError;
}

[DBus (name = "org.bluez.Audio")]
interface BluezAudio : Object
{
    public abstract void connect () throws IOError;
}

[DBus (name = "org.bluez.Input")]
interface BluezInput : Object
{
    public abstract void connect () throws IOError;
}

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);

    BluezAdapter adapter;
    try
    {
        var manager = Bus.get_proxy_sync<BluezManager> (BusType.SYSTEM, "org.bluez", "/");
        var path = manager.default_adapter ();
        adapter = Bus.get_proxy_sync<BluezAdapter> (BusType.SYSTEM, "org.bluez", path);
    }
    catch (IOError e)
    {
        return Posix.EXIT_FAILURE;
    }

    var indicator = new AppIndicator.Indicator ("indicator-bluetooth", "bluetooth-active", AppIndicator.IndicatorCategory.HARDWARE);
    indicator.set_status (AppIndicator.IndicatorStatus.ACTIVE);

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

    var item = new Gtk.MenuItem.with_label ("Bluetooth: On");
    item.sensitive = false;
    item.show ();
    menu.append (item);

    item = new Gtk.MenuItem.with_label ("Turn off Bluetooth");
    item.show ();
    menu.append (item);

    item = new Gtk.CheckMenuItem.with_label (_("Visible"));
    item.activate.connect (() => { adapter.set_property ("Discoverable", new Variant.boolean (true)); });
    item.show ();
    menu.append (item);
    
    var sep = new Gtk.SeparatorMenuItem ();
    sep.show ();
    menu.append (sep);

    item = new Gtk.MenuItem.with_label (_("Devices"));
    item.sensitive = false;
    item.show ();
    menu.append (item);

    try
    {
        var devices = adapter.list_devices ();
        foreach (var path in devices)
        {
            var device = Bus.get_proxy_sync<BluezDevice> (BusType.SYSTEM, "org.bluez", path);
            var properties = device.get_properties ();
            var iter = HashTableIter<string, Variant> (properties);
            string name;
            Variant value;
            //stderr.printf ("%s\n", path);
            while (iter.next (out name, out value))
            {
                //stderr.printf ("  %s=%s\n", name, value.print (false));
                if (name == "Name" && value.is_of_type (VariantType.STRING))
                {
                    item = new Gtk.MenuItem.with_label (value.get_string ());
                    item.show ();
                    menu.append (item);

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

                    //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"); });
                }
            }
        }
    }
    catch (IOError e)
    {
        stderr.printf ("%s\n", e.message);
        return Posix.EXIT_FAILURE;
    }

    sep = new Gtk.SeparatorMenuItem ();
    sep.show ();
    menu.append (sep);

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

    Gtk.main ();

    return 0;
}