summaryrefslogtreecommitdiff
path: root/src/watcher.vala
blob: a7ba3d524a0caa631287fc74c7073ab48d881df3 (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
namespace AstalTray {

[DBus (name="org.kde.StatusNotifierWatcher")]
internal class StatusNotifierWatcher : Object {

    private HashTable<string, string> _items;

    public string[] RegisteredStatusNotifierItems { owned get { return _items.get_values_as_ptr_array().data; } }
    public bool IsStatusNotifierHostRegistered { get; default = true; }
    public int ProtocolVersion { get; default = 0; }

    public signal void StatusNotifierItemRegistered(string service);
    public signal void StatusNotifierItemUnregistered(string service);
    public signal void StatusNotifierHostRegistered();
    public signal void StatusNotifierHostUnregistered();
    
    construct {
      _items = new HashTable<string, string>(GLib.str_hash, GLib.str_equal);
    }

    public void RegisterStatusNotifierItem(string service, BusName sender) throws DBusError, IOError {
      string busName;
      string path;
      if(service[0] == '/') {
        path = service;
        busName = sender;
      }
      else {
        busName = service;
        path = "/StatusNotifierItem";
      }

      Bus.get_sync(BusType.SESSION).signal_subscribe(
            null,
            "org.freedesktop.DBus",
            "NameOwnerChanged",
            null,
            null,
            DBusSignalFlags.NONE,
            (connection, sender_name, path, interface_name, signal_name, parameters) => {
                string name = null;
                string new_owner = null;
                string old_owner = null;
                parameters.get("(sss)", &name, &old_owner, &new_owner);
                if(new_owner == "" && _items.contains(old_owner)){
                  string full_path = _items.take(old_owner);
                  StatusNotifierItemUnregistered(full_path);
                }
            }
        );

      _items.set(busName, busName+path);

      StatusNotifierItemRegistered(busName+path);
    }

    public void RegisterStatusNotifierHost(string service) throws DBusError, IOError {
      /*
        NOTE:
        usually the watcher should keep track of registered host
        but some tray applications do net register their trayitem properly
        when hosts register/deregister. This is fixed by setting isHostRegistered
        always to true, this also make host handling logic unneccessary.
      */
    }
 
}
}