tizen 2.3 release
[framework/base/syspopup.git] / syspopup-app / syspopup-app.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 <tizen.h>
24 #include <dlog.h>
25 #include "syspopup.h"
26 #include "syspopup-app.h"
27
28 typedef struct appdata {
29         Evas_Object *win;
30         Evas_Object *conform;
31         Evas_Object *label;
32         Evas_Object *popup;
33         bundle *b;
34         char title[TITLE_BUF_LEN];
35         char content[CONTENT_BUF_LEN];
36 } appdata_s;
37
38 int myterm(bundle *b, void *data)
39 {
40         dlog_print(DLOG_DEBUG, LOG_TAG, "myterm called");
41         return 0;
42 }
43
44 int mytimeout(bundle *b, void* data)
45 {
46         dlog_print(DLOG_DEBUG, LOG_TAG, "mytimeout called");
47         return 0;
48 }
49
50 syspopup_handler handler = {
51         .def_term_fn = myterm,
52         .def_timeout_fn = mytimeout
53 };
54
55 static bool _app_control_extra_data_cb(app_control_h app_control, const char *key, void *user_data)
56 {
57         int ret;
58         char *value;
59         appdata_s *ad = user_data;
60
61         ret = app_control_get_extra_data(app_control, key, &value);
62         if (ret != APP_CONTROL_ERROR_NONE)
63         {
64                 dlog_print(DLOG_ERROR, LOG_TAG, "app_control_get_extra_data() is failed. err = %d", ret);
65         }
66
67         if (bundle_add(ad->b, key, value) != BUNDLE_ERROR_NONE) {
68                 return APP_CONTROL_ERROR_INVALID_PARAMETER;
69         }
70
71         return true;
72 }
73
74 static void win_delete_request_cb(void *data , Evas_Object *obj , void *event_info)
75 {
76         ui_app_exit();
77 }
78
79 static void win_back_cb(void *data, Evas_Object *obj, void *event_info)
80 {
81         appdata_s *ad = data;
82         /*  Let window go to hide state. */
83         elm_win_lower(ad->win);
84 }
85
86 static void response_cb(void *data, Evas_Object * obj, void *event_info)
87 {
88         dlog_print(DLOG_DEBUG, LOG_TAG, "response_cb called");
89
90         if ((int)event_info != 5)
91                 evas_object_del(obj);
92         elm_exit();
93 }
94
95 static void block_clicked_cb(void *data, Evas_Object *obj, void *event_info)
96 {
97         dlog_print(DLOG_DEBUG, LOG_TAG, "block_clicked_cb called");
98         evas_object_del(obj);
99 }
100
101 static void create_base_gui(appdata_s *ad) {
102
103         int w;
104         int h;
105
106         ad->win = elm_win_add(NULL, PACKAGE, ELM_WIN_DIALOG_BASIC);
107         if (ad->win) {
108                 elm_win_title_set(ad->win, PACKAGE);
109                 elm_win_borderless_set(ad->win, EINA_TRUE);
110                 elm_win_alpha_set(ad->win, EINA_TRUE);
111                 ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
112                 evas_object_resize(ad->win, w, h);
113         }
114
115         elm_win_autodel_set(ad->win, EINA_TRUE);
116         evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
117
118 }
119
120 static bool app_create(void *data)
121 {
122         appdata_s *ad = data;
123
124         create_base_gui(ad);
125
126         return true;
127 }
128
129 static void app_control(app_control_h app_control, void *data)
130 {
131         /*  Handle the launch request. */
132         appdata_s *ad = data;
133
134         Evas_Object *popup;
135         const char *val;
136         int ret = 0;
137
138         if(ad->b == NULL)
139                 ad->b = bundle_create();
140
141         ret = app_control_foreach_extra_data(app_control, _app_control_extra_data_cb, data);
142         if (ret != APP_CONTROL_ERROR_NONE)
143                 dlog_print(DLOG_ERROR, LOG_TAG, "app_control_foreach_extra_data() is failed. err = %d", ret);
144
145         if (syspopup_has_popup(ad->b)) {
146                 syspopup_reset(ad->b);
147         } else {
148                 popup = elm_popup_add(ad->win);
149                 if (popup != NULL) {
150                         ret = syspopup_create(ad->b, &handler, ad->win, ad);
151                         evas_object_show(ad->win);
152
153                         if (ret == 0) {
154                                 val = bundle_get_val(ad->b, "_SYSPOPUP_TITLE_");
155                                 if (val) {
156                                         snprintf(ad->title, TITLE_BUF_LEN, "%s",
157                                                         val);
158                                 } else {
159                                         snprintf(ad->title, TITLE_BUF_LEN, "%s",
160                                                         "Unknown Title");
161                                 }
162
163                                 val = bundle_get_val(ad->b, "_SYSPOPUP_CONTENT_");
164                                 if (val) {
165                                         snprintf(ad->content, CONTENT_BUF_LEN,
166                                                         "%s", val);
167                                 } else {
168                                         snprintf(ad->content, CONTENT_BUF_LEN,
169                                                         "%s", "Unknown Content");
170                                 }
171
172                                 elm_object_style_set(popup, "char_wrap_style");
173                                 evas_object_size_hint_weight_set(popup,
174                                                 EVAS_HINT_EXPAND,
175                                                 EVAS_HINT_EXPAND);
176                                 evas_object_smart_callback_add(popup, "block,clicked", block_clicked_cb, NULL);
177                                 elm_object_part_text_set(popup, "title,text", ad->title);
178                                 elm_object_text_set(popup, ad->content);
179                                 evas_object_smart_callback_add(popup,
180                                                 "response",
181                                                 response_cb,
182                                                 NULL);
183
184                                 evas_object_show(popup);
185                         }
186                 }
187         }
188         bundle_free(ad->b);
189 }
190
191 static void app_pause(void *data)
192 {
193         /*  Take necessary actions when application becomes invisible. */
194 }
195
196 static void app_resume(void *data)
197 {
198         /*  Take necessary actions when application becomes visible. */
199 }
200
201 static void app_terminate(void *data)
202 {
203         /*  Release all resources. */
204 }
205
206 static void ui_app_lang_changed(app_event_info_h event_info, void *user_data)
207 {
208         /* APP_EVENT_LANGUAGE_CHANGED*/
209         return;
210 }
211
212 static void ui_app_orient_changed(app_event_info_h event_info, void *user_data)
213 {
214         /* APP_EVENT_DEVICE_ORIENTATION_CHANGED*/
215         return;
216 }
217
218 static void ui_app_region_changed(app_event_info_h event_info, void *user_data)
219 {
220         /* APP_EVENT_REGION_FORMAT_CHANGED*/
221 }
222
223 static void ui_app_low_battery(app_event_info_h event_info, void *user_data)
224 {
225         /* APP_EVENT_LOW_BATTERY*/
226 }
227
228 static void ui_app_low_memory(app_event_info_h event_info, void *user_data)
229 {
230         /* APP_EVENT_LOW_MEMORY*/
231 }
232
233 int main(int argc, char *argv[])
234 {
235         appdata_s ad = {0,};
236         int ret = 0;
237
238         ui_app_lifecycle_callback_s event_callback = {0,};
239         app_event_handler_h handlers[5] = {NULL, };
240
241         event_callback.create = app_create;
242         event_callback.terminate = app_terminate;
243         event_callback.pause = app_pause;
244         event_callback.resume = app_resume;
245         event_callback.app_control = app_control;
246
247         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, ui_app_low_battery, &ad);
248         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, ui_app_low_memory, &ad);
249         ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, ui_app_orient_changed, &ad);
250         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, ui_app_lang_changed, &ad);
251         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, ui_app_region_changed, &ad);
252
253         ret = ui_app_main(argc, argv, &event_callback, &ad);
254         if (ret != APP_ERROR_NONE) {
255                 dlog_print(DLOG_ERROR, LOG_TAG, "app_main() is failed. err = %d", ret);
256         }
257
258         return ret;
259 }
260