~donadigo/wingpanel-indicator-notifications/notification-open-app

« back to all changes in this revision

Viewing changes to src/Services/NotificationMonitor.vala

  • Committer: donadigos159 at gmail
  • Date: 2016-06-20 15:00:26 UTC
  • Revision ID: donadigos159@gmail.com-20160620150026-ny0auyhzmg5qtn3n
General code improvements; if notification clicked and does not have a default action, focus the app instead

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
public class NotificationMonitor : Object {
24
24
    private const string NOTIFY_IFACE = "org.freedesktop.Notifications";
25
25
    private const string NOTIFY_PATH = "/org/freedesktop/Notifications";
 
26
    private const string DBUS_IFACE = "org.freedesktop.DBus";
 
27
    private const string DBUS_PATH = "/";
26
28
    private const string MATCH_STRING = "eavesdrop='true',type='method_call',interface='org.freedesktop.Notifications',member='Notify'";
27
29
    private const uint32 REASON_DISMISSED = 2;
28
30
 
29
31
    private static NotificationMonitor? instance = null;
 
32
    private static INotifications? notifications_iface = null;
 
33
    private static IDBus? dbus_iface = null;
30
34
 
31
35
    private DBusConnection connection;
32
 
    public INotifications? notifications_iface = null;
33
 
    public IDBus? dbus_iface = null;
34
36
    private uint32 id_counter = 0;
35
37
 
36
38
    public signal void received (DBusMessage message, uint32 id);
43
45
        return instance;
44
46
    }
45
47
 
 
48
    public static INotifications? get_notifications_iface () {
 
49
        if (notifications_iface == null) {
 
50
            try {
 
51
                notifications_iface = Bus.get_proxy_sync (BusType.SESSION, NOTIFY_IFACE, NOTIFY_PATH); 
 
52
            } catch (Error e) {
 
53
                warning ("%s\n", e.message);
 
54
            }
 
55
        }
 
56
        
 
57
        return notifications_iface;
 
58
    }
 
59
 
 
60
    public static IDBus? get_dbus_iface () {
 
61
        if (dbus_iface == null) {
 
62
            try {
 
63
                dbus_iface = Bus.get_proxy_sync (BusType.SESSION, DBUS_IFACE, DBUS_PATH);
 
64
            } catch (Error e) {
 
65
                warning ("%s\n", e.message);
 
66
            }            
 
67
        }
 
68
 
 
69
        return dbus_iface;
 
70
    }
 
71
 
46
72
    private NotificationMonitor () {
47
73
        try {
48
74
            connection = Bus.get_sync (BusType.SESSION);
61
87
        var body = new Variant.parsed ("(%s,)", MATCH_STRING);
62
88
        message.set_body (body);
63
89
        
64
 
        try {
65
 
            notifications_iface = Bus.get_proxy_sync (BusType.SESSION, NOTIFY_IFACE, NOTIFY_PATH); 
66
 
        } catch (Error e) {
67
 
            error ("%s\n", e.message);
68
 
        }
69
 
 
70
90
        id_counter = get_current_notification_id ();
71
91
        try {
72
92
            connection.send_message (message, DBusSendMessageFlags.NONE, null);
109
129
        hints.insert ("suppress-sound", new Variant.boolean (true));
110
130
        string[] actions = {};
111
131
        try {
112
 
            return notifications_iface.notify ("", 0, "", "", "", actions, hints, 1);
 
132
            return get_notifications_iface ().notify ("", 0, "", "", "", actions, hints, 1);
113
133
        } catch (Error e) {
114
134
            error ("%s\n", e.message);
115
135
        }