summaryrefslogtreecommitdiff
path: root/src/proxy.vala
blob: 526cd13224cb566af6f40da7d705b8b7d65303e9 (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
namespace AstalNotifd {
[DBus (name = "org.freedesktop.Notifications")]
internal interface IDaemon : Object {
    public abstract string cache_directory { owned get; set; }
    public abstract bool ignore_timeout { get; set; }
    public abstract bool dont_disturb { get; set; }

    public abstract uint[] notification_ids() throws DBusError, IOError;
    public abstract string get_notification_json(uint id) throws DBusError, IOError;

    public signal void notified(uint id);
    public signal void resolved(uint id, ClosedReason reason);
}

internal class DaemonProxy : Object {
    public string cache_directory {
        owned get { return proxy.cache_directory; }
        set { proxy.cache_directory = value; }
    }

    public bool ignore_timeout {
        get { return proxy.ignore_timeout; }
        set { proxy.ignore_timeout = value; }
    }

    public bool dont_disturb {
        get { return proxy.dont_disturb; }
        set { proxy.dont_disturb = value; }
    }

    public uint[] notification_ids() throws DBusError, IOError {
        return proxy.notification_ids();
    }

    public string get_notification_json(uint id) throws DBusError, IOError {
        return proxy.get_notification_json(id);
    }

    public signal void notified(uint id);
    public signal void resolved(uint id, ClosedReason reason);

    IDaemon proxy;
    List<ulong> ids = new List<ulong>();

    public void stop() {
        if (ids.length() > 0) {
            foreach (var id in ids)
                SignalHandler.disconnect(proxy, id);
        }
    }

    public bool start() {
        try  {
            var bus = Bus.get_sync(BusType.SESSION, null);
            var variant = bus.call_sync(
                "org.freedesktop.Notifications",
                "/org/freedesktop/Notifications",
                "org.freedesktop.Notifications",
                "GetServerInformation",
                null,
                null,
                DBusCallFlags.NONE,
                -1,
                null);

            var name = variant.get_child_value(0).get_string();
            var vendor = variant.get_child_value(1).get_string();
            var version = variant.get_child_value(2).get_string();

            var running = name == Daemon.name
              && vendor == Daemon.vendor
              && version == Daemon.version;

            if (running) {
                proxy = Bus.get_proxy_sync(
                    BusType.SESSION,
                    "org.freedesktop.Notifications",
                    "/org/freedesktop/Notifications"
                );

                ids.append(proxy.notified.connect((id) => notified(id)));
                ids.append(proxy.resolved.connect((id, reason) => resolved(id, reason)));
                ids.append(proxy.notify.connect((pspec) => {
                    if (get_class().find_property(pspec.name) != null)
                        notify_property(pspec.name);
                }));
                return true;
            } else {
                critical("cannot get proxy: %s is already running", name);
            }
        } catch (Error err) {
            critical("cannot get proxy: %s", err.message);
        }
        return false;
    }
}
}