forked from FedericoCeratto/nim-libnotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libnotify.nim
88 lines (62 loc) · 2.65 KB
/
libnotify.nim
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
##
## A minimalistic wrapper for libnotify
## https://github.com/FedericoCeratto/nim-libnotify
##
## Wraps libnotify.so.4 https://developer.gnome.org/libnotify/
##
## Released under LGPLv3, see LICENSE file
## 2015 - Federico Ceratto <federico.ceratto@gmail.com>
##
{.deadCodeElim: on.}
import glib2
const libnotify_fn* = "libnotify.so.4"
type Notify = pointer
type NotificationUrgency* = enum
Low, Normal, Critical
proc notify_init*(name: cstring): bool
{.importc: "notify_init", dynlib: libnotify_fn.}
proc notify_uninit*(): void {.importc: "notify_uninit",
dynlib: libnotify_fn.}
proc notify_is_initted(): bool
{.importc: "notify_is_initted", dynlib: libnotify_fn.}
proc notify_get_app_name(): cstring
{.importc: "notify_get_app_name", dynlib: libnotify_fn.}
proc notify_set_app_name(name: cstring): void
{.importc: "notify_set_app_name", dynlib: libnotify_fn.}
proc notify_notification_new(a, b, c: cstring): Notify
{.importc: "notify_notification_new", dynlib: libnotify_fn.}
proc notify_notification_show(notification: Notify, gerror: cint): gboolean
{.importc: "notify_notification_show", dynlib: libnotify_fn.}
proc notify_notification_set_timeout(notification: Notify, timeout: cint): void
{.importc: "notify_notification_set_timeout", dynlib: libnotify_fn.}
proc notify_notification_set_urgency(notification: Notify, urgency: NotificationUrgency): void
{.importc: "notify_notification_set_urgency", dynlib: libnotify_fn.}
type NotifyClient* = ref object of RootObj
proc newNotifyClient*(app_name: string): NotifyClient =
## Initialize notification client
if notify_is_initted() == false:
doAssert notify_init(app_name)
doAssert notify_is_initted()
new(NotifyClient)
proc get_app_name*(self: NotifyClient): string =
## Get application name
return $notify_get_app_name()
proc set_app_name*(self: NotifyClient, app_name: string) =
## Set application name
notify_set_app_name(app_name.cstring)
proc send_new_notification*(self: NotifyClient, summary, body, icon_fname: string, timeout=0,
urgency=NotificationUrgency.Normal) =
## Show a notification. Optionally specify a timeout in milliseconds and an
## urgency as a NotificationUrgency.Low / Normal / Critical
echo "cstring ", icon_fname.cstring
let n = notify_notification_new(summary.cstring, body.cstring, icon_fname.cstring)
notify_notification_set_urgency(n, urgency)
if timeout != 0:
notify_notification_set_timeout(n, timeout.cint)
let error: cint = 0
doAssert notify_notification_show(n, error)
if error != 0:
raise newException(Exception, "Unhandled exception")
proc uninit*(self: NotifyClient) =
## Uninitialize the library
notify_uninit()