1 module notifd;
2 import std.typecons;
3 version(linux) {
4     import ddbus;
5 }
6 
7 /**
8     A notifier that can display notifications
9 */
10 class AppNotifier {
11 private:
12     string icon;
13     string appName;
14     version(linux) {
15         Connection connection;
16         PathIface notifier;
17     }
18 
19     version(Win32) {
20 
21     }
22 
23 
24     Notification _notify(string icon, string appName, string title, string message) {
25         version(linux) {
26             return 
27                 Notification(
28                     notifier.call!uint(
29                         "Notify", 
30                         appName, 
31                         0u, 
32                         icon, 
33                         title, 
34                         message, 
35                         cast(string[])[], 
36                         cast(Variant!string[string])null, 
37                         0
38                     ),
39                     notifier
40                 );
41         }
42     }
43 
44     void initialize() {
45         version(linux) {
46             this.notifier = new PathIface(
47                 connection, 
48                 "org.freedesktop.Notifications", 
49                 "/org/freedesktop/Notifications", 
50                 "org.freedesktop.Notifications");
51         }
52     }
53 
54 public:
55 
56     /**
57         Constructs an AppNotifier
58     */
59     this(string appName) {
60         this(appName, appName);
61     }
62 
63     /**
64         Constructs an AppNotifier
65     */
66     this(string appName, string icon) {
67         this.icon = icon;
68         this.appName = appName;
69 
70         version(linux) {
71             this.connection = connectToBus();
72         }
73 
74         this.initialize();
75     }
76 
77     ~this() {
78         connection.close();
79     }
80 
81     /**
82         Send a notification.
83     */
84     Notification notify(string title, string message) {
85         return _notify(icon, appName, title, message);
86     }
87 
88     /**
89         Send a notification with a custom icon
90     */
91     Notification notify(string title, string message, string icon) {
92         return _notify(icon, appName, title, message);
93     }
94 }
95 
96 /**
97     A notification
98 */
99 struct Notification {
100 version(linux):
101     private {
102         uint id;
103         PathIface iface;
104     }
105 
106     this(uint id, PathIface iface) {
107         this.id = id;
108         this.iface = iface;
109     }
110 
111     /**
112         Closes the notification
113     */
114     void close() {
115         iface.call!DBusAny("CloseNotification", id);
116     }
117 
118 version(Win32):
119 
120 }