add reset timer new api / fix hardcording of lib path / fix build on x86_64
[framework/base/syspopup.git] / src / syspopup_core.c
1 /*
2  * syspopup
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7  * Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23 #include <stdio.h>
24 #include <glib.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <dbus/dbus.h>
28 #include <dbus/dbus-glib-lowlevel.h>
29 #include "syspopup_core.h"
30 #include "simple_util.h"
31
32 #define SYSPOPUP_NAME "_INTERNAL_SYSPOPUP_NAME_"
33
34 static syspopup *syspopup_head = NULL;
35
36 static int initialized = 0;
37 static DBusConnection *bus;
38 static int noti_fd = -1;
39 static int sp_id = 0;
40
41 static void (*_term_handler) (void *data);
42 static gboolean(*_timeout_handler) (void *data);
43
44 syspopup *_syspopup_get_head()
45 {
46         return syspopup_head;
47 }
48
49 int _syspopup_add_new(syspopup *sp)
50 {
51         if (sp == NULL)
52                 return -1;
53
54         sp->id = sp_id++;
55         sp->next = syspopup_head;
56         syspopup_head = sp;
57
58         return 0;
59 }
60
61 syspopup *_syspopup_find(const char *name)
62 {
63         syspopup *tmp;
64         tmp = syspopup_head;
65         while (tmp) {
66                 if (tmp->name) {
67                         if (strcmp(tmp->name, name) == 0)
68                                 return tmp;
69                 }
70                 tmp = tmp->next;
71         }
72         return NULL;
73 }
74
75 syspopup *_syspopup_find_by_id(int id)
76 {
77         syspopup *tmp;
78         tmp = syspopup_head;
79         while (tmp) {
80                 if (tmp->id == id)
81                         return tmp;
82                 tmp = tmp->next;
83         }
84         return NULL;
85 }
86
87 static void __syspopup_free(syspopup *sp)
88 {
89         if (sp->name != NULL)
90                 free(sp->name);
91         if (sp->dupped_bundle != NULL)
92                 bundle_free(sp->dupped_bundle);
93         free(sp);
94 }
95
96 void _syspopup_del(int id)
97 {
98         syspopup *tmp;
99         syspopup *target;
100
101         target = _syspopup_find_by_id(id);
102
103         if (syspopup_head == NULL || target == NULL)
104                 return;
105
106         if (syspopup_head == target) {
107                 syspopup_head = target->next;
108                 __syspopup_free(target);
109                 return;
110         }
111
112         tmp = syspopup_head;
113         while (tmp) {
114                 if (tmp->next == target) {
115                         tmp->next = target->next;
116                         __syspopup_free(target);
117                         return;
118                 }
119                 tmp = tmp->next;
120         }
121 }
122
123
124 static DBusHandlerResult
125 __sys_popup_dbus_signal_filter(DBusConnection *conn, DBusMessage *message,
126                                      void *user_data)
127 {
128         const char *sender;
129         const char *interface;
130         int dead_pid;
131
132         DBusError error;
133         dbus_error_init(&error);
134
135         interface = dbus_message_get_interface(message);
136         if (interface == NULL) {
137                 _E("reject by security issue - no interface\n");
138                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
139         }
140
141         if (dbus_message_is_signal(message, interface,
142                                    SYSPOPUP_DBUS_SP_TERM_SIGNAL)) {
143                 if (_term_handler)
144                         _term_handler(NULL);
145
146                 _D("term handler has been called");
147         }
148
149         return DBUS_HANDLER_RESULT_HANDLED;
150 }
151
152
153
154 int _syspopup_init(void (*term_handler) (void *),
155                    gboolean(*timeout_handler) (void *))
156 {
157         DBusError error;
158         char rule[MAX_LOCAL_BUFSZ];
159
160         if (initialized)
161                 return 0;
162
163         _term_handler = term_handler;
164         _timeout_handler = timeout_handler;
165
166         dbus_error_init(&error);
167         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
168         if (!bus) {
169                 _E("Failed to connect to the D-BUS daemon: %s", error.message);
170                 dbus_error_free(&error);
171                 return -1;
172         }
173         dbus_connection_setup_with_g_main(bus, NULL);
174
175         snprintf(rule, MAX_LOCAL_BUFSZ,
176                  "path='%s',type='signal',interface='%s'", SYSPOPUP_DBUS_PATH,
177                  SYSPOPUP_DBUS_SIGNAL_INTERFACE);
178         /* listening to messages */
179         dbus_bus_add_match(bus, rule, &error);
180         if (dbus_error_is_set(&error)) {
181                 _E("Fail to rule set: %s", error.message);
182                 dbus_error_free(&error);
183                 return -1;
184         }
185
186         if (dbus_connection_add_filter(bus, 
187                 __sys_popup_dbus_signal_filter, NULL, NULL) == FALSE)
188                 return -1;
189
190         _D("syspopup signal initialized");
191
192         initialized = 1;
193         return 0;
194 }
195
196 int _syspopup_reset_timeout(syspopup *sp, syspopup_info_t *info)
197 {
198         if (!initialized) {
199                 _E("no initialized");
200                 return -1;
201         }
202         if (sp == NULL) {
203                 _E("syspopup is NULL");
204                 return -1;
205         }
206         if (info == NULL) {
207                 _E("syspopup info is NULL");
208                 return -1;
209         }
210
211         if (sp->timeout_id != 0)
212                 g_source_remove(sp->timeout_id);
213
214         if(info->timeout > 0) {
215                 sp->timeout_id = g_timeout_add_seconds(info->timeout, _timeout_handler,
216                                                (void *)sp->id);
217                 _D("add timeout - timeout : id=%d,timeout=%d(sec)", sp->id, 
218                         info->timeout);
219         }
220
221         return 0;
222 }
223
224 int _syspopup_set_term_type(syspopup *sp, syspopup_info_t *info)
225 {
226         sp->term_act = info->term_act;
227         return 0;
228 }
229
230 int _syspopup_set_endkey_type(syspopup *sp, syspopup_info_t *info)
231 {
232         sp->endkey_act = info->endkey_act;
233         return 0;
234 }
235
236 const char *_syspopup_get_name_from_bundle(bundle *b)
237 {
238         const char *name;
239
240         if (getuid() != 0 && getuid() != 5000) {
241                 _E("syspopup permission error");
242                 return NULL;
243         }
244
245         if (b == NULL) {
246                 _E("bundle is NULL");
247                 return NULL;
248         }
249
250         name = bundle_get_val(b, SYSPOPUP_NAME);
251         if (name == NULL) {
252                 _E("this is no bundle for syspopup");
253         }
254         return name;
255 }
256
257 int _syspopup_set_name_to_bundle(bundle *b, char *popup_name)
258 {
259         if ((b == NULL) || (popup_name == NULL))
260                 return -1;
261         bundle_add(b, SYSPOPUP_NAME, popup_name);
262         return 0;
263 }
264