Update org.tizen.message.spec.
[apps/core/preloaded/message-app.git] / dialog / message-dialog.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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://floralicense.org/license
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
18 #include "message-dialog.h"
19 #include "app_service.h"
20
21
22 static void __msg_ui_dialog_always_ask_left_btn_cb(void *data, Evas_Object *obj, void *event_info);
23 static void __msg_ui_dialog_always_ask_right_btn_cb(void *data, Evas_Object *obj, void *event_info);
24 static int __msg_ui_dialog_create_always_ask_popup(void *data);
25 static void __msg_ui_dialog_rotate(app_device_orientation_e orientation, void *data);
26 static void __msg_ui_dialog_win_del(void *data, Evas_Object *obj, void *event);
27 static Evas_Object *__msg_ui_dialog_create_win(const char *name);
28 static int parse_opt(int argc, char **argv, struct appdata *ad);
29
30
31 void msg_ui_dialog_exit(void *data)
32 {
33         struct appdata *ad = data;
34
35         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, " ");
36
37         if (ad->msg) {
38                 g_free(ad->msg);
39                 ad->msg = NULL;
40         }
41
42         if (ad->url) {
43                 g_free(ad->url);
44                 ad->url = NULL;
45         }
46
47         if (ad->popup != NULL) {
48                 evas_object_del(ad->popup);
49                 ad->popup = NULL;
50         }
51
52         if (ad->main_window != NULL) {
53                 evas_object_del(ad->main_window);
54                 ad->main_window = NULL;
55         }
56
57         elm_exit();
58 }
59
60 static void __msg_ui_dialog_always_ask_left_btn_cb(void *data, Evas_Object *obj, void *event_info)
61 {
62         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
63         MSG_UI_RET_IF(MSG_UI_LEVEL_ERR, !data);
64
65         struct appdata *ad = data;
66         service_h svc_h;
67         int ret = SERVICE_ERROR_NONE;
68
69         ret = service_create(&svc_h);
70         if (ret != SERVICE_ERROR_NONE) {
71                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "service_create() is failed : %d", ret);
72                 return;
73         }
74
75         ret = service_set_operation(svc_h, SERVICE_OPERATION_VIEW);
76         if (ret != SERVICE_ERROR_NONE) {
77                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "service_set_operation() is failed : %d", ret);
78                 goto DESTROY;
79         }
80
81         ret = service_set_uri(svc_h, ad->url);
82         if (ret != SERVICE_ERROR_NONE) {
83                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "service_set_uri() is failed : %d", ret);
84                 goto DESTROY;
85         }
86
87         ret = service_send_launch_request(svc_h, NULL, NULL);
88         if (ret != SERVICE_ERROR_NONE) {
89                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "service_send_launch_request() is failed : %d", ret);
90         }
91
92 DESTROY:
93         ret = service_destroy(svc_h);
94         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "service_destroy() returns : %d", ret);
95
96         msg_ui_dialog_exit(ad);
97 }
98
99 static void __msg_ui_dialog_always_ask_right_btn_cb(void *data, Evas_Object *obj, void *event_info)
100 {
101         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
102         MSG_UI_RET_IF(MSG_UI_LEVEL_ERR, !data);
103
104         struct appdata *ad = data;
105
106         msg_ui_dialog_exit(ad);
107 }
108
109 static int __msg_ui_dialog_create_always_ask_popup(void *data)
110 {
111         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
112
113         struct appdata *ad = data;
114         char display_msg[100] = { 0, };
115         Evas_Object *btn1;
116         Evas_Object *btn2;
117
118         const char *str = dgettext(MESSAGE_PKGNAME, "IDS_MSGF_POP_ABOUT_TO_GO_ONLINE_CONTINUE_Q");
119         snprintf(display_msg, sizeof(display_msg), str, ad->url);
120
121         ad->popup = elm_popup_add(ad->main_window);
122         if (ad->popup == NULL) {
123                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "Cannot add popup object !!");
124                 return MSG_UI_RET_ERR;
125         }
126
127         evas_object_size_hint_weight_set(ad->popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
128         elm_object_text_set(ad->popup, display_msg);
129         elm_popup_orient_set(ad->popup, ELM_POPUP_ORIENT_CENTER);
130
131         btn1 = elm_button_add(ad->popup);
132         elm_object_text_set(btn1, dgettext("sys_string", "IDS_COM_SK_YES"));
133         elm_object_part_content_set(ad->popup, "button1", btn1);
134         evas_object_smart_callback_add(btn1, "clicked", __msg_ui_dialog_always_ask_left_btn_cb, ad);
135
136         btn2 = elm_button_add(ad->popup);
137         elm_object_text_set(btn2, dgettext("sys_string", "IDS_COM_SK_NO"));
138         elm_object_part_content_set(ad->popup, "button2", btn2);
139         evas_object_smart_callback_add(btn2, "clicked", __msg_ui_dialog_always_ask_right_btn_cb, ad);
140
141         evas_object_show(ad->popup);
142
143         MSG_UI_LEAVE(MSG_UI_LEVEL_DEBUG);
144
145         return MSG_UI_RET_SUCCESS;
146 }
147
148 static void __msg_ui_dialog_rotate(app_device_orientation_e orientation, void *data)
149 {
150         struct appdata *ad = data;
151         int r;
152
153         if (ad == NULL || ad->main_window == NULL)
154                 return;
155
156         switch (orientation) {
157         case APP_DEVICE_ORIENTATION_0:
158                 r = 0;
159                 break;
160         case APP_DEVICE_ORIENTATION_180:
161                 r = 180;
162                 break;
163         case APP_DEVICE_ORIENTATION_270:
164                 r = 270;
165                 break;
166         case APP_DEVICE_ORIENTATION_90:
167                 r = 90;
168                 break;
169         default:
170                 r = -1;
171                 break;
172         }
173
174         if (r >= 0) {
175                 if (ad->main_window) {
176                         elm_win_rotation_with_resize_set(ad->main_window, r);
177                 } else {
178                         MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "Invalid case !!");
179                 }
180         }
181 }
182
183 static void __msg_ui_dialog_win_del(void *data, Evas_Object *obj, void *event)
184 {
185         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, " ");
186
187         elm_exit();
188 }
189
190 static Evas_Object *__msg_ui_dialog_create_win(const char *name)
191 {
192         Evas_Object *eo = NULL;
193         int w, h;
194
195         eo = elm_win_add(NULL, name, ELM_WIN_DIALOG_BASIC);
196         if (eo) {
197                 elm_win_alpha_set(eo, EINA_TRUE);
198                 elm_win_title_set(eo, name);
199                 elm_win_raise(eo);
200                 evas_object_smart_callback_add(eo, "delete,request", __msg_ui_dialog_win_del, NULL);
201                 ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
202                 evas_object_resize(eo, w, h);
203                 evas_object_show(eo);
204         }
205
206         return eo;
207 }
208
209 static bool app_create(void *data)
210 {
211         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
212
213         struct appdata *ad = data;
214
215         if (ad->dialog_mode != MSG_UI_DIALOG_MODE_PUSH_MSG_ALWAYS_ASK) {
216                 MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "Invalid mode !!");
217                 return FALSE;
218         }
219
220         /* init internationalization */
221         if (bindtextdomain(MESSAGE_PKGNAME, LOCALEDIR) == NULL) {
222                 MSG_UI_DEBUG(MSG_UI_LEVEL_ASSERT, "bindtextdomain() is failed !!");
223                 return FALSE;
224         }
225
226         if (textdomain(MESSAGE_PKGNAME) == NULL) {
227                 MSG_UI_DEBUG(MSG_UI_LEVEL_ASSERT, "textdomain() is failed !!");
228                 return FALSE;
229         }
230
231         /* Create main window */
232         ad->main_window = __msg_ui_dialog_create_win(PACKAGE);
233         if (ad->main_window == NULL) {
234                 MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "__msg_ui_dialog_create_win() is failed !!");
235                 return FALSE;
236         }
237
238         if (__msg_ui_dialog_create_always_ask_popup(ad) < 0) {
239                 MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "__msg_ui_dialog_create_always_ask_popup() is failed !!");
240                 return FALSE;
241         }
242
243         return TRUE;
244 }
245
246 static void app_terminate(void *data)
247 {
248         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
249 }
250
251 static void app_pause(void *data)
252 {
253         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
254 }
255
256 static void app_resume(void *data)
257 {
258         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
259 }
260
261 static void app_service(service_h service, void *data)
262 {
263         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
264 }
265
266 static int parse_opt(int argc, char **argv, struct appdata *ad)
267 {
268         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
269
270         int opt;
271
272         if (ad == NULL) {
273                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "Invalid argument: application data is NULL");
274                 return MSG_UI_RET_ERR;
275         }
276
277         while ((opt = getopt(argc, argv, "m:o:u:h")) != -1) {
278                 switch (opt) {
279                 case 'o':
280                         ad->msg = strdup(optarg);
281                         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "optarg = %s", ad->msg);
282                         break;
283
284                 case 'm':
285                         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "m  = %s", optarg);
286                         if (g_strcmp0(optarg, "PUSH_MSG_ALWAYS_ASK") == 0) {
287                                 ad->dialog_mode = MSG_UI_DIALOG_MODE_PUSH_MSG_ALWAYS_ASK;
288                         } else {
289                                 MSG_UI_DEBUG(MSG_UI_LEVEL_ERR, "Invalid mode: %s", optarg);
290                                 return MSG_UI_RET_ERR;
291                         }
292                         break;
293                 case 'u':
294                         ad->url = strdup(optarg);
295                         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, " ad->url = %s", ad->url);
296                         break;
297                 case 'h':
298                 default:
299                         fprintf(stderr, "Usage: %s [-h] [-m PUSH_MSG_ALWAYS_ASK] [-u LaunchURL] \n", argv[0]);
300                         fprintf(stderr, "ex) %s -m PUSH_MSG_ALWAYS_ASK -u www.google.com \n", argv[0]);
301                         return MSG_UI_RET_ERR;
302                 }
303         }
304
305         return MSG_UI_RET_SUCCESS;
306 }
307
308 int main(int argc, char **argv)
309 {
310         MSG_UI_ENTER(MSG_UI_LEVEL_DEBUG);
311
312         struct appdata ad;
313         memset(&ad, 0x0, sizeof(struct appdata));
314
315         app_event_callback_s event_callback;
316
317         event_callback.create = app_create;
318         event_callback.terminate = app_terminate;
319         event_callback.pause = app_pause;
320         event_callback.resume = app_resume;
321         event_callback.service = app_service;
322         event_callback.low_memory = NULL;
323         event_callback.low_battery = NULL;
324         event_callback.device_orientation = __msg_ui_dialog_rotate;
325         event_callback.language_changed = NULL;
326         event_callback.region_format_changed = NULL;
327
328         if (parse_opt(argc, argv, &ad) == -1) {
329                 MSG_UI_DEBUG(MSG_UI_LEVEL_ASSERT, "parse_opt() is failed !!");
330                 return MSG_UI_RET_ERR;
331         }
332
333         return app_efl_main(&argc, &argv, &event_callback, &ad);
334 }