Move notification_get_text_input_max_length to internal
[platform/core/api/notification.git] / src / notification_ongoing.c
1 /*
2  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include <glib.h>
22 #include <gio/gio.h>
23
24 #include <notification_db.h>
25 #include <notification_debug.h>
26 #include <notification_ongoing.h>
27 #include <notification_private.h>
28
29 #define PATH_NAME       "/dbus/signal"
30 #define INTERFACE_NAME  "notification.ongoing"
31 #define MEMBER_PROGRESS "update_progress"
32 #define MEMBER_SIZE     "update_size"
33 #define MEMBER_CONTENT  "update_content"
34
35 struct _ongoing_update_cb_data {
36         notification_ongoing_update_cb callback;
37         void *data;
38         GDBusConnection *conn;
39         guint subscribe_id;
40 };
41
42 static struct _ongoing_update_cb_data od;
43
44 static void __notification_ongoing_update_callback(GDBusConnection *connection,
45                                         const gchar *sender_name,
46                                         const gchar *object_path,
47                                         const gchar *interface_name,
48                                         const gchar *signal_name,
49                                         GVariant *parameters,
50                                         gpointer user_data)
51 {
52         struct ongoing_info_s *info;
53         char *pkgname = NULL;
54         int priv_id = 0;
55         double progress = 0;
56         double size = 0;
57         char *content = NULL;
58
59         info = calloc(1, sizeof(struct ongoing_info_s));
60         if (info == NULL) {
61                 /* LCOV_EXCL_START */
62                 NOTIFICATION_ERR("Out of memory");
63                 return;
64                 /* LCOV_EXCL_STOP */
65         }
66
67         if (g_strcmp0(signal_name, MEMBER_PROGRESS) == 0) {
68                 g_variant_get(parameters, "(&sid)", &pkgname, &priv_id, &progress);
69                 info->type = ONGOING_TYPE_PROGRESS;
70         } else if (g_strcmp0(signal_name, MEMBER_SIZE) == 0) {
71                 g_variant_get(parameters, "(&sid)", &pkgname, &priv_id, &size);
72                 info->type = ONGOING_TYPE_SIZE;
73
74         } else if (g_strcmp0(signal_name, MEMBER_CONTENT) == 0) {
75                 g_variant_get(parameters, "(&si&s)", &pkgname, &priv_id, &content);
76                 info->type = ONGOING_TYPE_CONTENT;
77         }
78
79         if (pkgname == NULL) {
80                 /* LCOV_EXCL_START */
81                 NOTIFICATION_ERR("pkgname is null");
82                 free(info);
83                 return;
84                 /* LCOV_EXCL_STOP */
85         }
86
87         info->pkgname = pkgname;
88         info->priv_id = priv_id;
89         info->progress = progress;
90         info->size = size;
91         info->content = content;
92
93         od.callback(info, od.data);
94
95         free(info);
96 }
97
98 static int __send_ongoing_update_signal(const char *signal_name, GVariant *param)
99 {
100         GError *err = NULL;
101         GDBusConnection *conn;
102         int ret = NOTIFICATION_ERROR_NONE;
103
104         if (signal_name == NULL)
105                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
106
107         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
108         if (conn == NULL) {
109                 /* LCOV_EXCL_START */
110                 NOTIFICATION_ERR("g_bus_get_sync() failed: %s", err->message);
111                 ret = NOTIFICATION_ERROR_FROM_DBUS;
112                 goto end;
113                 /* LCOV_EXCL_STOP */
114         }
115
116         if (g_dbus_connection_emit_signal(conn,
117                                         NULL,
118                                         PATH_NAME,
119                                         INTERFACE_NAME,
120                                         signal_name,
121                                         param,
122                                         &err) == FALSE) {
123                 /* LCOV_EXCL_START */
124                 NOTIFICATION_ERR("g_dbus_connection_emit_signal() failed: %s",
125                                         err->message);
126                 ret = NOTIFICATION_ERROR_FROM_DBUS;
127                 goto end;
128                 /* LCOV_EXCL_STOP */
129         }
130
131         if (g_dbus_connection_flush_sync(conn, NULL, &err) == FALSE) {
132                 /* LCOV_EXCL_START */
133                 NOTIFICATION_ERR("g_dbus_connection_flush_sync() failed: %s",
134                                         err->message);
135                 ret = NOTIFICATION_ERROR_FROM_DBUS;
136                 goto end;
137                 /* LCOV_EXCL_STOP */
138         }
139
140
141 end:
142         if (err)
143                 g_error_free(err);
144
145         if (conn)
146                 g_object_unref(conn);
147
148
149         return ret;
150 }
151
152 EXPORT_API
153 int notification_ongoing_update_cb_set(notification_ongoing_update_cb callback,
154                                         void *user_data)
155 {
156         GError *error = NULL;
157
158         if (!callback)
159                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
160
161         if (od.conn == NULL) {
162                 od.conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
163                 if (od.conn == NULL) {
164                         /* LCOV_EXCL_START */
165                         NOTIFICATION_ERR("Failed to connect to the D-BUS Daemon: %s",
166                                                 error->message);
167                         g_error_free(error);
168                         return NOTIFICATION_ERROR_FROM_DBUS;
169                         /* LCOV_EXCL_STOP */
170                 }
171         }
172
173         if (!od.subscribe_id) {
174                 od.subscribe_id = g_dbus_connection_signal_subscribe(od.conn,
175                                         NULL,
176                                         INTERFACE_NAME,
177                                         NULL,
178                                         PATH_NAME,
179                                         NULL,
180                                         G_DBUS_SIGNAL_FLAGS_NONE,
181                                         __notification_ongoing_update_callback,
182                                         NULL,
183                                         NULL);
184                 if (od.subscribe_id == 0) {
185                         /* LCOV_EXCL_START */
186                         NOTIFICATION_ERR("g_dbus_connection_signal_subscribe() failed");
187                         g_object_unref(od.conn);
188                         return NOTIFICATION_ERROR_FROM_DBUS;
189                         /* LCOV_EXCL_STOP */
190                 }
191         }
192
193         od.callback = callback;
194         od.data = user_data;
195
196         return NOTIFICATION_ERROR_NONE;
197 }
198
199 EXPORT_API
200 int notification_ongoing_update_cb_unset(void)
201 {
202         if (od.subscribe_id) {
203                 g_dbus_connection_signal_unsubscribe(od.conn, od.subscribe_id);
204                 od.subscribe_id = 0;
205         }
206
207         if (od.conn) {
208                 g_object_unref(od.conn);
209                 od.conn = NULL;
210         }
211
212         od.callback = NULL;
213         od.data = NULL;
214
215         return NOTIFICATION_ERROR_NONE;
216 }
217
218 int notification_ongoing_update_progress(const char *caller_pkgname,
219                                         int priv_id, double progress)
220 {
221         GVariant *param;
222         int ret;
223
224         param = g_variant_new("(sid)", caller_pkgname, priv_id, progress);
225
226         ret = __send_ongoing_update_signal(MEMBER_PROGRESS, param);
227         if (ret != NOTIFICATION_ERROR_NONE) {
228                 /* LCOV_EXCL_START */
229                 NOTIFICATION_ERR("notification_ongoing_update_progress failed %d",
230                                         ret);
231                 return ret;
232                 /* LCOV_EXCL_STOP */
233         }
234
235         return NOTIFICATION_ERROR_NONE;
236 }
237
238 int notification_ongoing_update_size(const char *caller_pkgname,
239                                         int priv_id, double size)
240 {
241         GVariant *param;
242         int ret;
243
244         param = g_variant_new("(sid)", caller_pkgname, priv_id, size);
245
246         ret = __send_ongoing_update_signal(MEMBER_SIZE, param);
247         if (ret != NOTIFICATION_ERROR_NONE) {
248                 /* LCOV_EXCL_START */
249                 NOTIFICATION_ERR("notification_ongoing_update_size failed %d",
250                                         ret);
251                 return ret;
252                 /* LCOV_EXCL_STOP */
253         }
254
255         return NOTIFICATION_ERROR_NONE;
256 }
257
258 int notification_ongoing_update_content(const char *caller_pkgname,
259                                         int priv_id, const char *content)
260 {
261         GVariant *param;
262         int ret;
263
264         param = g_variant_new("(sis)", caller_pkgname, priv_id, content);
265
266         ret = __send_ongoing_update_signal(MEMBER_CONTENT, param);
267         if (ret != NOTIFICATION_ERROR_NONE) {
268                 /* LCOV_EXCL_START */
269                 NOTIFICATION_ERR("notification_ongoing_update_content failed %d",
270                                         ret);
271                 return ret;
272                 /* LCOV_EXCL_STOP */
273         }
274         return NOTIFICATION_ERROR_NONE;
275 }