Debian Codegraph @

click/cnotificationsettings/cnotificationsettings.go

raw
1 /*
2 Copyright 2016 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 cnotificationsettings accesses the g_settings notification settings
18
19 package cnotificationsettings
20
21 /*
22 #cgo pkg-config: gio-unix-2.0
23 #cgo pkg-config: glib-2.0
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gio/gio.h>
28
29 #define NOTIFICATION_APPS_SETTINGS_SCHEMA_ID "com.lomiri.notifications.settings.applications"
30 #define VIBRATE_SILENT_MODE_KEY "vibrate-silent-mode"
31 #define NOTIFICATION_SETTINGS_SCHEMA_ID "com.lomiri.notifications.settings"
32 #define SETTINGS_BASE_PATH "/com/lomiri/NotificationSettings/"
33 #define ENABLE_NOTIFICATIONS_KEY "enable-notifications"
34 #define USE_SOUNDS_NOTIFICATIONS_KEY "use-sounds-notifications"
35 #define USE_VIBRATIONS_NOTIFICATIONS_KEY "use-vibrations-notifications"
36 #define USE_BUBBLES_NOTIFICATIONS_KEY "use-bubbles-notifications"
37 #define USE_LIST_NOTIFICATIONS_KEY "use-list-notifications"
38
39 GSettings* get_settings() {
40 // Check if GSettings schema exists
41 GSettingsSchemaSource *source = g_settings_schema_source_get_default();
42 if (!g_settings_schema_source_lookup(source, NOTIFICATION_APPS_SETTINGS_SCHEMA_ID, TRUE)) {
43 return NULL;
44 }
45
46 return g_settings_new(NOTIFICATION_APPS_SETTINGS_SCHEMA_ID);
47 }
48
49 int vibrate_in_silent_mode() {
50 GSettings *settings = NULL;
51 int vibrateInSilentMode = 0;
52
53 settings = get_settings();
54 if (!settings) {
55 return -1;
56 }
57
58 if (g_settings_get_boolean(settings, VIBRATE_SILENT_MODE_KEY)) {
59 vibrateInSilentMode = 1;
60 }
61
62 g_object_unref(settings);
63 return vibrateInSilentMode;
64 }
65
66 GSettings* get_settings_for_app(const char *pkgname, const char *appname) {
67 GSettings *settings = NULL;
68 gchar *path;
69
70 // Check if GSettings schema exists
71 GSettingsSchemaSource *source = g_settings_schema_source_get_default();
72 if (!g_settings_schema_source_lookup(source, NOTIFICATION_SETTINGS_SCHEMA_ID, TRUE)) {
73 return NULL;
74 }
75
76 // Define notifications settings GSettings path
77 if (strcmp(pkgname, "") == 0) {
78 // Use "dpkg" as package name for legacy apps
79 path = g_strconcat(SETTINGS_BASE_PATH, "dpkg/", appname, "/", NULL);
80 } else {
81 path = g_strconcat(SETTINGS_BASE_PATH, pkgname, "/", appname, "/", NULL);
82 }
83
84 settings = g_settings_new_with_path(NOTIFICATION_SETTINGS_SCHEMA_ID, path);
85 g_free(path);
86
87 return settings;
88 }
89
90 int are_notifications_enabled(const char *pkgname, const char *appname) {
91 GSettings *notificationSettings = NULL;
92 int enableNotifications = 0;
93
94 notificationSettings = get_settings_for_app(pkgname, appname);
95 if (!notificationSettings) {
96 return -1;
97 }
98
99 if (g_settings_get_boolean(notificationSettings, ENABLE_NOTIFICATIONS_KEY)) {
100 enableNotifications = 1;
101 }
102
103 g_object_unref(notificationSettings);
104 return enableNotifications;
105 }
106
107 int can_use_sounds_notify(const char *pkgname, const char *appname) {
108 GSettings *notificationSettings = NULL;
109 int soundsNotify = 0;
110
111 notificationSettings = get_settings_for_app(pkgname, appname);
112 if (!notificationSettings) {
113 return -1;
114 }
115
116 if (g_settings_get_boolean(notificationSettings, USE_SOUNDS_NOTIFICATIONS_KEY)) {
117 soundsNotify = 1;
118 }
119
120 g_object_unref(notificationSettings);
121 return soundsNotify;
122 }
123
124 int can_use_vibrations_notify(const char *pkgname, const char *appname) {
125 GSettings *notificationSettings = NULL;
126 int vibrationsNotify = 0;
127
128 notificationSettings = get_settings_for_app(pkgname, appname);
129 if (!notificationSettings) {
130 return -1;
131 }
132
133 if (g_settings_get_boolean(notificationSettings, USE_VIBRATIONS_NOTIFICATIONS_KEY)) {
134 vibrationsNotify = 1;
135 }
136
137 g_object_unref(notificationSettings);
138 return vibrationsNotify;
139 }
140
141 int can_use_bubbles_notify(const char *pkgname, const char *appname) {
142 GSettings *notificationSettings = NULL;
143 int bubblesNotify = 0;
144
145 notificationSettings = get_settings_for_app(pkgname, appname);
146 if (!notificationSettings) {
147 return -1;
148 }
149
150 if (g_settings_get_boolean(notificationSettings, USE_BUBBLES_NOTIFICATIONS_KEY)) {
151 bubblesNotify = 1;
152 }
153
154 g_object_unref(notificationSettings);
155 return bubblesNotify;
156 }
157
158 int can_use_list_notify(const char *pkgname, const char *appname) {
159 GSettings *notificationSettings = NULL;
160 int listNotify = 0;
161
162 notificationSettings = get_settings_for_app(pkgname, appname);
163 if (!notificationSettings) {
164 return -1;
165 }
166
167 if (g_settings_get_boolean(notificationSettings, USE_LIST_NOTIFICATIONS_KEY)) {
168 listNotify = 1;
169 }
170
171 g_object_unref(notificationSettings);
172 return listNotify;
173 }
174 */
175 import "C"
176
177 import (
178 "unsafe"
179
180 "gitlab.com/ubports/development/core/lomiri-push-service/click"
181 )
182
183 // VibrateInSilentMode returns true if applications can use vibrations notify when in silent mode
184 func VibrateInSilentMode() bool {
185 return C.vibrate_in_silent_mode() != 0
186 }
187
188 // AreNotificationsEnabled returns true if the application is marked on gsettings to use notifications
189 func AreNotificationsEnabled(app *click.AppId) bool {
190 pkgname := C.CString(app.Package)
191 appname := C.CString(app.Application)
192 defer C.free(unsafe.Pointer(pkgname))
193 defer C.free(unsafe.Pointer(appname))
194 return C.are_notifications_enabled(pkgname, appname) != 0
195 }
196
197 // CanUseSoundsNotify returns true if the application is marked on gsettings to use sounds notify
198 func CanUseSoundsNotify(app *click.AppId) bool {
199 pkgname := C.CString(app.Package)
200 appname := C.CString(app.Application)
201 defer C.free(unsafe.Pointer(pkgname))
202 defer C.free(unsafe.Pointer(appname))
203 return C.can_use_sounds_notify(pkgname, appname) != 0
204 }
205
206 // CanUseVibrationsNotify returns true if the application is marked on gsettings to use vibrations notify
207 func CanUseVibrationsNotify(app *click.AppId) bool {
208 pkgname := C.CString(app.Package)
209 appname := C.CString(app.Application)
210 defer C.free(unsafe.Pointer(pkgname))
211 defer C.free(unsafe.Pointer(appname))
212 return C.can_use_vibrations_notify(pkgname, appname) != 0
213 }
214
215 // CanUseBubblesNotify returns true if the application is marked on gsettings to use bubbles notify
216 func CanUseBubblesNotify(app *click.AppId) bool {
217 pkgname := C.CString(app.Package)
218 appname := C.CString(app.Application)
219 defer C.free(unsafe.Pointer(pkgname))
220 defer C.free(unsafe.Pointer(appname))
221 return C.can_use_bubbles_notify(pkgname, appname) != 0
222 }
223
224 // CanUseListNotify returns true if the application is marked on gsettings to use list notificy
225 func CanUseListNotify(app *click.AppId) bool {
226 pkgname := C.CString(app.Package)
227 appname := C.CString(app.Application)
228 defer C.free(unsafe.Pointer(pkgname))
229 defer C.free(unsafe.Pointer(appname))
230 return C.can_use_list_notify(pkgname, appname) != 0
231 }