Apply consistent log messages.
[platform/core/system/system-popup.git] / src / common / core.c
1 /*
2  * system-popup
3  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "popup-common.h"
19
20 #define SYSPOPUP_CONTENT "_SYSPOPUP_CONTENT_"
21
22 static syspopup_handler handler;
23 static GList *popup_list = NULL;
24 static int feedback_init_processed;
25 appdata_s *app_info;
26 extern int reset_window_priority(int priority);
27
28 GList *get_popup_list(void)
29 {
30         return popup_list;
31 }
32
33 void register_popup(const struct popup_ops *ops)
34 {
35         struct object_ops *obj;
36
37         if (!ops) {
38                 _E("Invalid parameter.");
39                 return;
40         }
41
42         obj = (struct object_ops *)calloc(1, sizeof(struct object_ops));
43         if (!obj) {
44                 _E("Failed to calloc().");
45                 return;
46         }
47
48         obj->ops = ops;
49
50         popup_list = g_list_append(popup_list, obj);
51 }
52
53 static void free_obj(gpointer data)
54 {
55         struct object_ops *obj = data;
56         FREE(obj);
57 }
58
59 void unregister_all_popup(void)
60 {
61         if (popup_list) {
62                 g_list_free_full(popup_list, free_obj);
63                 popup_list = NULL;
64         }
65 }
66
67 void terminate_if_no_popup(void)
68 {
69         GList *l;
70         struct object_ops *obj;
71
72         for (l = popup_list ; l ; l = g_list_next(l)) {
73                 obj = (struct object_ops *)(l->data);
74                 if (obj->popup) {
75                         _I("Popup(%s) exists.", obj->ops->name);
76                         return;
77                 }
78         }
79         window_terminate();
80 }
81
82 static int load_popup_by_type(bundle *b)
83 {
84         char *type;
85         GList *l;
86         struct object_ops *obj;
87         int ret;
88
89         if (!b)
90                 return -EINVAL;
91
92         type = (char *)bundle_get_val(b, SYSPOPUP_CONTENT);
93         if (!type) {
94                 _E("Failed to bundle_get_val().");
95                 return -ENOMEM;
96         }
97
98         for (l = popup_list ; l ; l = g_list_next(l)) {
99                 obj = (struct object_ops *)(l->data);
100                 if (!obj || !(obj->ops) || !(obj->ops->name) || !(obj->ops->show))
101                         continue;
102                 if (strncmp(type, obj->ops->name, strlen(type) + 1))
103                         continue;
104
105                 if (obj->ops->skip && obj->ops->skip(b, obj->ops)) {
106                         terminate_if_no_popup();
107                         return 0;
108                 }
109
110                 if (obj->ops->pattern)
111                         play_multi_feedback(obj->ops->pattern);
112
113                 if (obj->ops->change)
114                         obj->ops->change(b, obj->ops);
115
116                 if (obj->ops->pre)
117                         obj->ops->pre(b, obj->ops);
118
119                 ret = obj->ops->show(b, obj->ops);
120
121                 if (obj->ops->post)
122                         obj->ops->post(b, obj->ops);
123
124                 return ret;
125         }
126         return -EINVAL;
127 }
128
129 static int release_all_handlers(void *data)
130 {
131         GList *l;
132         struct object_ops *obj;
133         static bool already = false;
134
135         if (already)
136                 return 0;
137         already = true;
138
139         for (l = popup_list ; l ; l = g_list_next(l)) {
140                 obj = (struct object_ops *)(l->data);
141                 if (obj && obj->popup) {
142                         if (obj->ops->terminate)
143                                 obj->ops->terminate(obj->ops);
144                         release_evas_object(&(obj->popup));
145                 }
146         }
147
148         unset_dbus_connection();
149         remove_window();
150         unregister_all_popup();
151         return 0;
152 }
153
154 static int terminate_by_syspopup(bundle *b, void *data)
155 {
156         return release_all_handlers(data);
157 }
158
159 static int app_create(void *data)
160 {
161         int ret;
162
163         handler.def_term_fn = terminate_by_syspopup;
164         handler.def_timeout_fn = NULL;
165
166         /* create window */
167         ret = create_window(PACKAGE);
168         if (ret < 0)
169                 return ret;
170
171         app_info = (appdata_s *)data;
172
173         if (appcore_set_i18n(LANG_DOMAIN, LOCALE_DIR) != 0)
174                 _E("Failed to appcore_set_i18n().");
175
176         ret = set_dbus_connection();
177         if (ret < 0)
178                 _E("Failed to set dbus connection: %d", ret);
179
180         if (!feedback_init_processed) {
181                 init_multi_feedback();
182                 feedback_init_processed = 1;
183         }
184
185         return 0;
186 }
187
188 static int app_terminate(void *data)
189 {
190         stop_multi_feedback();
191         feedback_init_processed = 0;
192         return release_all_handlers(data);
193 }
194
195 static int app_pause(void *data)
196 {
197         GList *l;
198         struct object_ops *obj;
199
200         for (l = popup_list ; l ; l = g_list_next(l)) {
201                 obj = (struct object_ops *)(l->data);
202                 if (obj && obj->ops) {
203                         if (obj->ops->term_pause == NULL
204                                         || obj->ops->term_pause(obj->ops)) {
205                                 unload_simple_popup(obj->ops);
206                         }
207                 }
208         }
209         terminate_if_no_popup();
210         return 0;
211 }
212
213 static int app_resume(void *data)
214 {
215         return 0;
216 }
217
218 static int app_reset(bundle *b, void *data)
219 {
220         int ret;
221         Evas_Object *win;
222
223         if (!b) {
224                 ret = -EINVAL;
225                 goto out;
226         }
227
228         if (syspopup_has_popup(b)) {
229                 syspopup_reset(b);
230         } else {
231                 win = get_window();
232                 if (!win)
233                         return -ENOMEM;
234
235                 ret = syspopup_create(b, &handler, win, NULL);
236                 if (ret < 0) {
237                         _E("Failed to syspopup_create(): %d", ret);
238                         goto out;
239                 }
240
241                 /* change window priority to normal */
242                 (void)reset_window_priority(WIN_PRIORITY_HIGH);
243         }
244
245         ret = load_popup_by_type(b);
246         if (ret < 0)
247                 goto out;
248
249         return 0;
250
251 out:
252         window_terminate();
253         return ret;
254 }
255
256 int main(int argc, char *argv[])
257 {
258         appdata_s ad;
259
260         struct appcore_ops ops = {
261                 .create = app_create,
262                 .terminate = app_terminate,
263                 .pause = app_pause,
264                 .resume = app_resume,
265                 .reset = app_reset,
266         };
267
268         memset(&ad, 0x0, sizeof(appdata_s));
269         ops.data = &ad;
270
271         return appcore_efl_main(PACKAGE, &argc, &argv, &ops);
272 }