Move notification_get_text_input_max_length to internal
[platform/core/api/notification.git] / src / notification_status.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 <sys/types.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <glib.h>
23 #include <gio/gio.h>
24
25 #include <notification.h>
26 #include <notification_db.h>
27 #include <notification_list.h>
28 #include <notification_noti.h>
29 #include <notification_debug.h>
30 #include <notification_private.h>
31 #include <notification_status.h>
32 #include <notification_status_internal.h>
33
34 #define PATH_NAME    "/Org/Tizen/System/Notification/Status_message"
35 #define INTERFACE_NAME "org.tizen.system.notification.status_message"
36 #define MEMBER_NAME     "status_message"
37
38 struct _message_cb_data {
39         notification_status_message_cb callback;
40         void *data;
41         GDBusConnection *conn;
42         uint message_id;
43 };
44
45 static struct _message_cb_data md;
46
47 static void __notification_status_message_dbus_callback(GDBusConnection *connection,
48                                         const gchar *sender_name,
49                                         const gchar *object_path,
50                                         const gchar *interface_name,
51                                         const gchar *signal_name,
52                                         GVariant *parameters,
53                                         gpointer user_data)
54 {
55         char *message = NULL;
56
57         g_variant_get(parameters, "(&s)", &message);
58         if (strlen(message) <= 0) {
59                 /* LCOV_EXCL_START */
60                 NOTIFICATION_ERR("message has only NULL");
61                 return;
62                 /* LCOV_EXCL_STOP */
63         }
64
65         if (!md.callback) {
66                 /* LCOV_EXCL_START */
67                 NOTIFICATION_ERR("no callback");
68                 return;
69                 /* LCOV_EXCL_STOP */
70         }
71
72         md.callback(message, md.data);
73 }
74
75 EXPORT_API
76 int notification_status_monitor_message_cb_set(notification_status_message_cb callback, void *user_data)
77 {
78         GError *error = NULL;
79
80         if (!callback)
81                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
82
83         if (md.conn == NULL) {
84                 md.conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
85                 if (md.conn == NULL) {
86                         /* LCOV_EXCL_START */
87                         NOTIFICATION_ERR("Failed to connect to the D-BUS Daemon: %s",
88                                                 error->message);
89                         g_error_free(error);
90                         return NOTIFICATION_ERROR_FROM_DBUS;
91                         /* LCOV_EXCL_STOP */
92                 }
93         }
94
95         if (!md.message_id) {
96                 md.message_id = g_dbus_connection_signal_subscribe(md.conn,
97                                         NULL,
98                                         INTERFACE_NAME,
99                                         MEMBER_NAME,
100                                         PATH_NAME,
101                                         NULL,
102                                         G_DBUS_SIGNAL_FLAGS_NONE,
103                                         __notification_status_message_dbus_callback,
104                                         NULL,
105                                         NULL);
106                 if (md.message_id == 0) {
107                         /* LCOV_EXCL_START */
108                         NOTIFICATION_ERR("g_dbus_connection_signal_subscribe() failed.");
109                         g_object_unref(md.conn);
110                         return NOTIFICATION_ERROR_FROM_DBUS;
111                         /* LCOV_EXCL_STOP */
112                 }
113         }
114
115         md.callback = callback;
116         md.data = user_data;
117
118         return NOTIFICATION_ERROR_NONE;
119 }
120
121 EXPORT_API
122 int notification_status_monitor_message_cb_unset(void)
123 {
124         if (md.message_id) {
125                 g_dbus_connection_signal_unsubscribe(md.conn, md.message_id);
126                 md.message_id = 0;
127         }
128
129         if (md.conn) {
130                 g_object_unref(md.conn);
131                 md.conn = NULL;
132         }
133
134         md.callback = NULL;
135         md.data = NULL;
136
137         return NOTIFICATION_ERROR_NONE;
138 }
139
140 EXPORT_API
141 int notification_status_message_post(const char *message)
142 {
143         GError *err = NULL;
144         GDBusConnection *conn;
145         GVariant *param;
146         int ret = NOTIFICATION_ERROR_NONE;
147
148         if (!message) {
149                 NOTIFICATION_ERR("message is NULL");
150                 return NOTIFICATION_ERROR_INVALID_PARAMETER;
151         }
152
153         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
154         if (conn == NULL) {
155                 /* LCOV_EXCL_START */
156                 NOTIFICATION_ERR("g_bus_get_sync() failed: %s", err->message);
157                 ret = NOTIFICATION_ERROR_FROM_DBUS;
158                 goto end;
159                 /* LCOV_EXCL_STOP */
160         }
161
162         param = g_variant_new("(s)", message);
163
164         if (g_dbus_connection_emit_signal(conn,
165                                         NULL,
166                                         PATH_NAME,
167                                         INTERFACE_NAME,
168                                         MEMBER_NAME,
169                                         param,
170                                         &err) == FALSE) {
171                 /* LCOV_EXCL_START */
172                 NOTIFICATION_ERR("g_dbus_connection_emit_signal() failed: %s",
173                                         err->message);
174                 return NOTIFICATION_ERROR_FROM_DBUS;
175                 goto end;
176                 /* LCOV_EXCL_STOP */
177         }
178
179         if (g_dbus_connection_flush_sync(conn, NULL, &err) == FALSE) {
180                 /* LCOV_EXCL_START */
181                 NOTIFICATION_ERR("g_dbus_connection_flush_sync() failed: %s",
182                                         err->message);
183                 return NOTIFICATION_ERROR_FROM_DBUS;
184                 goto end;
185                 /* LCOV_EXCL_STOP */
186         }
187
188 end:
189         if (err)
190                 g_error_free(err); /* LCOV_EXCL_LINE */
191
192         if (conn)
193                 g_object_unref(conn);
194
195         return ret;
196 }