Debian Codegraph @

messaging/cmessaging/cmessaging.go

raw
1 /*
2 Copyright 2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // package cmessaging wraps libmessaging-menu
18 package cmessaging
19
20 /*
21 #cgo pkg-config: messaging-menu
22
23 #include <glib.h>
24
25 void add_notification(const gchar* desktop_id, const gchar* notification_id,
26 const gchar* icon_path, const gchar* summary, const gchar* body,
27 gint64 timestamp, const gchar** actions, gpointer obj);
28
29 void remove_notification(const gchar* desktop_id, const gchar* notification_id);
30
31 gboolean notification_exists(const gchar* desktop_id, const gchar* notification_id);
32 */
33 import "C"
34 import "unsafe"
35
36 import (
37 "gitlab.com/ubports/development/core/lomiri-push-service/click"
38 "gitlab.com/ubports/development/core/lomiri-push-service/launch_helper"
39 "gitlab.com/ubports/development/core/lomiri-push-service/messaging/reply"
40 )
41
42 type Payload struct {
43 Ch chan *reply.MMActionReply
44 Actions []string
45 App *click.AppId
46 Tag string
47 }
48
49 func gchar(s string) *C.gchar {
50 return (*C.gchar)(C.CString(s))
51 }
52
53 func gfree(s *C.gchar) {
54 C.g_free((C.gpointer)(s))
55 }
56
57 //export handleActivate
58 func handleActivate(c_action *C.char, c_notification *C.char, obj unsafe.Pointer) {
59 payload := (*Payload)(obj)
60 action := C.GoString(c_action)
61 // Default action, only support ATM, is always "".
62 // Use the first action as the default if it's available.
63 if action == "" && len(payload.Actions) >= 2 {
64 action = payload.Actions[1]
65 }
66 mmar := &reply.MMActionReply{Notification: C.GoString(c_notification), Action: action, App: payload.App}
67 payload.Ch <- mmar
68 }
69
70 func AddNotification(desktopId string, notificationId string, card *launch_helper.Card, payload *Payload) {
71 desktop_id := gchar(desktopId)
72 defer gfree(desktop_id)
73
74 notification_id := gchar(notificationId)
75 defer gfree(notification_id)
76
77 icon_path := gchar(card.Icon)
78 defer gfree(icon_path)
79
80 summary := gchar(card.Summary)
81 defer gfree(summary)
82
83 body := gchar(card.Body)
84 defer gfree(body)
85
86 timestamp := (C.gint64)(card.Timestamp() * 1000000)
87
88 C.add_notification(desktop_id, notification_id, icon_path, summary, body, timestamp, nil, (C.gpointer)(payload))
89 }
90
91 func RemoveNotification(desktopId string, notificationId string) {
92 desktop_id := gchar(desktopId)
93 defer gfree(desktop_id)
94
95 notification_id := gchar(notificationId)
96 defer gfree(notification_id)
97
98 C.remove_notification(desktop_id, notification_id)
99 }
100
101 func NotificationExists(desktopId string, notificationId string) bool {
102 notification_id := gchar(notificationId)
103 defer gfree(notification_id)
104
105 desktop_id := gchar(desktopId)
106 defer gfree(desktop_id)
107
108 return C.notification_exists(desktop_id, notification_id) == C.TRUE
109 }
110
111 func init() {
112 go C.g_main_loop_run(C.g_main_loop_new(nil, C.FALSE))
113 }