2e05e3c9237d734b862058c5c6cfacd945fe3c77
[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
24 #include <glib.h>
25 #include <heynoti.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "syspopup_core.h"
29 #include "simple_util.h"
30
31 #define SYSPOPUP_NAME "_INTERNAL_SYSPOPUP_NAME_"
32
33 static syspopup *syspopup_head = NULL;
34
35 static int initialized = 0;
36 static int noti_fd = -1;
37 static int sp_id = 0;
38
39 static void (*_term_handler) (void *data);
40 static gboolean(*_timeout_handler) (void *data);
41
42 syspopup *_syspopup_get_head()
43 {
44         return syspopup_head;
45 }
46
47 int _syspopup_add_new(syspopup *sp)
48 {
49         if (sp == NULL)
50                 return -1;
51
52         sp->id = sp_id++;
53         sp->next = syspopup_head;
54         syspopup_head = sp;
55
56         return 0;
57 }
58
59 syspopup *_syspopup_find(const char *name)
60 {
61         syspopup *tmp;
62         tmp = syspopup_head;
63         while (tmp) {
64                 if (tmp->name) {
65                         if (strcmp(tmp->name, name) == 0)
66                                 return tmp;
67                 }
68                 tmp = tmp->next;
69         }
70         return NULL;
71 }
72
73 syspopup *_syspopup_find_by_id(int id)
74 {
75         syspopup *tmp;
76         tmp = syspopup_head;
77         while (tmp) {
78                 if (tmp->id == id)
79                         return tmp;
80                 tmp = tmp->next;
81         }
82         return NULL;
83 }
84
85 static void __syspopup_free(syspopup *sp)
86 {
87         if (sp->name != NULL)
88                 free(sp->name);
89         if (sp->dupped_bundle != NULL)
90                 bundle_free(sp->dupped_bundle);
91         free(sp);
92 }
93
94 void _syspopup_del(int id)
95 {
96         syspopup *tmp;
97         syspopup *target;
98
99         target = _syspopup_find_by_id(id);
100
101         if (syspopup_head == NULL || target == NULL)
102                 return;
103
104         if (syspopup_head == target) {
105                 syspopup_head = target->next;
106                 __syspopup_free(target);
107                 return;
108         }
109
110         tmp = syspopup_head;
111         while (tmp) {
112                 if (tmp->next == target) {
113                         tmp->next = target->next;
114                         __syspopup_free(target);
115                         return;
116                 }
117                 tmp = tmp->next;
118         }
119 }
120
121 int _syspopup_init(void (*term_handler) (void *),
122                    gboolean(*timeout_handler) (void *))
123 {
124         if (initialized)
125                 return 0;
126
127         _term_handler = term_handler;
128         _timeout_handler = timeout_handler;
129
130         noti_fd = heynoti_init();
131         if (noti_fd < 0) {
132                 _E("heynoti add failed\n");
133                 return -1;
134         }
135
136         if (heynoti_subscribe_file(noti_fd, SYSPOPUP_TERM_NOTI_PATH,
137                                    _term_handler, NULL,
138                                    IN_CLOSE_WRITE | IN_DELETE_SELF) < 0) {
139                 _E("heynoti subscribe file failed");
140                 return -1;
141         } else {
142                 if (heynoti_attach_handler(noti_fd) < 0)
143                         _E("heynoti attach failed");
144         }
145
146         initialized = 1;
147         return 0;
148 }
149
150 int _syspopup_reset_timeout(syspopup *sp, syspopup_info_t *info)
151 {
152         if (!initialized) {
153                 _E("no initialized");
154                 return -1;
155         }
156         if (sp == NULL) {
157                 _E("syspopup is NULL");
158                 return -1;
159         }
160         if (info == NULL) {
161                 _E("syspopup info is NULL");
162                 return -1;
163         }
164
165         if (sp->timeout_id != 0)
166                 g_source_remove(sp->timeout_id);
167         sp->timeout_id = g_timeout_add_seconds(info->timeout, _timeout_handler,
168                                                (void *)sp->id);
169         _D("add timeout - timeout : id=%d,timeout=%d(sec)", sp->id,
170            info->timeout);
171
172         return 0;
173 }
174
175 int _syspopup_set_term_type(syspopup *sp, syspopup_info_t *info)
176 {
177         sp->term_act = info->term_act;
178         return 0;
179 }
180
181 int _syspopup_set_endkey_type(syspopup *sp, syspopup_info_t *info)
182 {
183         sp->endkey_act = info->endkey_act;
184         return 0;
185 }
186
187 const char *_syspopup_get_name_from_bundle(bundle *b)
188 {
189         const char *name;
190
191         if (getuid() != 0 && getuid() != 5000) {
192                 _E("syspopup permission error");
193                 return NULL;
194         }
195
196         if (b == NULL) {
197                 _E("bundle is NULL");
198                 return NULL;
199         }
200
201         name = bundle_get_val(b, SYSPOPUP_NAME);
202         if (name == NULL) {
203                 _E("this is no bundle for syspopup");
204         }
205         return name;
206 }
207
208 int _syspopup_set_name_to_bundle(bundle *b, char *popup_name)
209 {
210         if ((b == NULL) || (popup_name == NULL))
211                 return -1;
212         bundle_add(b, SYSPOPUP_NAME, popup_name);
213         return 0;
214 }
215