Remove SMACK rule file(.rule) according three domain model
[apps/core/preloaded/message-app.git] / main / message.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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 #include "message.h"
18 #include <Ecore_X.h>
19 #include <ui-gadget.h>
20 #include <ui-gadget-module.h>
21 #include <sound_manager.h>
22 #include <system_info.h>
23 #include <contacts.h>
24
25 #include "msg-ui-thread-main.h"
26 #include "msg-ui-common-utility.h"
27
28 static void __msg_ui_popup_warning(Evas_Object *parent, const char *msg);
29 static service_h __msg_ui_parse_sms_uri(service_h service, const char *uri);
30 static service_h __msg_ui_parse_mmsto_uri(service_h service, const char *uri);
31 static service_h __msg_ui_parse_file_uri(service_h service, const char *uri);
32 static service_h __get_service_with_new_msg(struct appdata *ad);
33 static service_h __get_service_with_msg_id(struct appdata *ad, service_h service);
34
35 void layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv);
36 void result_cb(ui_gadget_h ug, service_h result, void *priv);
37 void destroy_cb(ui_gadget_h ug, void *priv);
38
39 struct appdata *gAppData = NULL;
40
41 static void win_del(void *data, Evas_Object *obj, void *event)
42 {
43         elm_exit();
44 }
45
46 static void main_quit_cb(void *data, Evas_Object *obj, void *event_info)
47 {
48         elm_exit();
49 }
50
51 static void lang_changed(void *data)
52 {
53         D_ENTER;
54         struct appdata *ad = data;
55
56         ug_send_event(UG_EVENT_LANG_CHANGE);
57
58         msg_ui_thread_lang_changed(ad->thread_data);
59 }
60
61 static void low_battery_cb(void *data)
62 {
63         D_ENTER;
64
65         ug_send_event(UG_EVENT_LOW_BATTERY);
66 }
67
68 static void region_changed_cb(void *data)
69 {
70         D_ENTER;
71
72         ug_send_event(UG_EVENT_REGION_CHANGE);
73 }
74
75 static void rotation_changed_cb(void *data, Evas_Object *obj, void *event)
76 {
77         D_ENTER;
78
79         struct appdata *ad = data;
80         int ret = 0;
81         int rot = THREAD_ROTATE_ANGLE_UNKNOWN;
82         int changed_rotation = elm_win_rotation_get(obj);
83
84         if(changed_rotation != ad->cur_rotation) {
85                 D_MSG("current rotation [%d], changed rotation [%d]", ad->cur_rotation, changed_rotation);
86                 ad->cur_rotation = changed_rotation;
87
88                 switch (changed_rotation) {
89                         case 0:
90                                 rot = THREAD_ROTATE_ANGLE_PORTRAIT;
91                                 ret = ug_send_event(UG_EVENT_ROTATE_PORTRAIT);
92                                 break;
93                         case 90:
94                                 rot = THREAD_ROTATE_ANGLE_LANDSCAPE_UPSIDEDOWN;
95                                 ret = ug_send_event(UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
96                                 break;
97                         case 180:
98                                 rot = THREAD_ROTATE_ANGLE_PORTRAIT_UPSIDEDOWN;
99                                 ret = ug_send_event(UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
100                                 break;
101                         case 270:
102                                 rot = THREAD_ROTATE_ANGLE_LANDSCAPE;
103                                 ret = ug_send_event(UG_EVENT_ROTATE_LANDSCAPE);
104                                 break;
105                         default:
106                                 rot = THREAD_ROTATE_ANGLE_UNKNOWN;
107                                 break;
108                 }
109         }
110
111         if (rot >= 0)
112                 msg_ui_thread_rotation_set(ad->thread_data, rot);
113
114         D_LEAVE;
115 }
116
117 static void _block_clicked_cb(void *data, Evas_Object *obj, void *event_info)
118 {
119         evas_object_del(obj);
120         elm_exit();
121 }
122
123 static void __msg_ui_popup_warning(Evas_Object *parent, const char *msg)
124 {
125         D_ENTER;
126
127         MSG_UI_RET_IF(MSG_UI_LEVEL_ASSERT, parent == NULL || msg == NULL);
128
129         Evas_Object *layout, *pu;
130
131         evas_object_show(parent);
132
133         layout = elm_layout_add(parent);
134         elm_layout_theme_set(layout, "layout", "application", "default");
135         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
136         elm_win_resize_object_add(parent, layout);
137         evas_object_show(layout);
138
139         pu = elm_popup_add(layout);
140         MSG_UI_RETM_IF(MSG_UI_LEVEL_ASSERT, pu == NULL, "Cannot add popup object\n");
141         evas_object_size_hint_weight_set(pu, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
142         evas_object_smart_callback_add(pu , "block,clicked", _block_clicked_cb, NULL);
143
144         elm_object_text_set(pu, msg);
145         elm_popup_timeout_set(pu, 2);
146         evas_object_smart_callback_add(pu, "timeout", main_quit_cb, NULL);
147         evas_object_show(pu);
148
149         D_LEAVE;
150 }
151
152 static service_h __msg_ui_parse_sms_uri(service_h service, const char *uri)
153 {
154         D_ENTER;
155
156         char *content = NULL;
157         char *recipient = NULL;
158         char *tmp = NULL;
159         char *body_text = NULL;
160
161         service_h svc_handle = NULL;
162
163         if (service_create(&svc_handle) < 0 || svc_handle == NULL) {
164                 D_EMSG("service_create() is failed !!");
165                 return NULL;
166         }
167
168         char *scheme = g_strdup(uri);
169
170         if (scheme) {
171                 strtok_r(scheme, ":", &content);
172
173                 if (content) {
174                         if (g_ascii_isdigit(content[0]) || (content[0] == '+' && g_ascii_isdigit(content[1]))) {
175                                 recipient = strtok_r(NULL, "?", &content);
176                                 if (recipient) {
177                                         D_MSG("APPSVC RECIPIENT = [%s]", recipient);
178                                         service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TO, recipient);
179                                 }
180
181                                 if (content) {
182                                         if (g_str_has_prefix(content, MSG_BUNDLE_VALUE_BODY_URI)) {
183                                                 tmp = strtok_r(NULL, "=", &content);
184
185                                                 if (content && strlen(content)) {
186                                                         char *unescape_string = g_uri_unescape_string(content, NULL);
187                                                         if (unescape_string) {
188                                                                 D_MSG("APPSVC BODY = [%s]", unescape_string);
189                                                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_BODY, unescape_string);
190                                                                 free(unescape_string);
191                                                         }
192                                                 }
193                                         }
194                                 }
195                         }
196                 }
197                 g_free(scheme);
198         } else {
199                 service_get_extra_data(service, SERVICE_DATA_TO, &recipient);
200                 D_MSG("APPSVC RECIPIENT = [%s]", recipient);
201                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TO, recipient);
202         }
203
204         /* Add body text */
205         service_get_extra_data(service, SERVICE_DATA_TEXT, &body_text);
206         if (body_text) {
207                 D_MSG("APPSVC BODY_TEXT = [%s]", body_text);
208                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_BODY, body_text);
209         }
210
211         D_LEAVE;
212         return svc_handle;
213 }
214
215 static service_h __msg_ui_parse_mmsto_uri(service_h service, const char *uri)
216 {
217         D_ENTER;
218
219         char *content = NULL;
220         char *recipient = NULL;
221         char *body_text = NULL;
222         char *subject = NULL;
223         char *attachment = NULL;
224         char *cc = NULL;
225
226         service_h svc_handle = NULL;
227
228         if (service_create(&svc_handle) < 0 || svc_handle == NULL) {
229                 D_EMSG("service_create() is failed !!");
230                 return NULL;
231         }
232
233         char *scheme = g_strdup(uri);
234
235         if (scheme) {
236                 strtok_r(scheme, ":", &content);
237
238                 if (content) {
239                         if (g_ascii_isdigit(content[0]) || (content[0] == '+' && g_ascii_isdigit(content[1]))) {
240                                 recipient = strtok_r(NULL, "?", &content);
241                                 cc = strtok_r(NULL, "&", &content);
242                                 if (cc)
243                                         strtok_r(NULL, "=", &cc);
244
245                                 D_MSG("APPSVC RECIPIENT = [%s]", recipient);
246                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TO, recipient);
247                         }
248                 }
249                 g_free(scheme);
250         } else {
251                 service_get_extra_data(service, SERVICE_DATA_TO, &recipient);
252                 D_MSG("APPSVC RECIPIENT = [%s]", recipient);
253                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TO, recipient);
254         }
255
256         /* Add body text */
257         service_get_extra_data(service, SERVICE_DATA_TEXT, &body_text);
258         if (body_text) {
259                 D_MSG("APPSVC BODY_TEXT = [%s]", body_text);
260                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_BODY, body_text);
261         }
262
263         /* Add subject */
264         service_get_extra_data(service, SERVICE_DATA_SUBJECT, &subject);
265         if (subject) {
266                 D_MSG("APPSVC SUBJECT = [%s]", subject);
267                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_SUBJECT, subject);
268         }
269
270         /* Add attachment */
271         service_get_extra_data(service, MSG_BUNDLE_KEY_ATTACHFILE, &attachment);
272         if (attachment) {
273                 D_MSG("APPSVC ATTACHMENT = [%s]", attachment);
274                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_ATTACHFILE, attachment);
275         }
276
277         D_LEAVE;
278
279         return svc_handle;
280 }
281
282 static service_h __msg_ui_parse_file_uri(service_h service, const char *uri)
283 {
284         D_ENTER;
285
286         char *content = NULL;
287         char attachment[DEF_IMG_PATH_LEN] = {0, };
288         int i = 0;
289         int len = 0;
290
291         service_h svc_handle = NULL;
292
293         if (service_create(&svc_handle) < 0 || svc_handle == NULL) {
294                 D_EMSG("service_create() is failed !!");
295                 return NULL;
296         }
297
298         char *scheme = g_strdup(uri);
299
300         if (scheme) {
301                 strtok_r(scheme, ":", &content);
302                 D_MSG("content = [%s]", content);
303
304                 if (content) {
305                         len = strlen(content) - 2;
306                         if (len <= 0) {
307                                 D_EMSG("len is less than 0 !!");
308                                 service_destroy(svc_handle);
309                                 g_free(scheme);
310                                 return NULL;
311                         }
312
313                         /* Remove '//' from content string */
314                         for (i = 0; i < len; i++) {
315                                 attachment[i] = content[i+2];
316                         }
317
318                         if (attachment[0] != '\0') {
319                                 D_MSG("APPSVC ATTACHMENT = [%s]", attachment);
320                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_ATTACHFILE, attachment);
321                         }
322                 }
323                 g_free(scheme);
324         } else {
325                 D_EMSG("scheme is NULL!!");
326                 service_destroy(svc_handle);
327                 return NULL;
328         }
329
330         D_LEAVE;
331
332         return svc_handle;
333 }
334
335 static service_h __get_service_app_svc_op(const char *operation, service_h service)
336 {
337         D_ENTER;
338
339         if (service == NULL || operation == NULL)
340                 return NULL;
341
342         char *uri = NULL;
343         char *recipient = NULL;
344         char *body_text = NULL;
345         char *attachment = NULL;
346         char *subject = NULL;
347
348         service_h svc_handle = NULL;
349
350         if (g_strcmp0(operation, SERVICE_OPERATION_SEND) == 0) {
351                 service_get_uri(service, &uri);
352                 D_MSG("APPSVC URI = [%s]", uri);
353
354                 if (uri) {
355                         if (g_str_has_prefix(uri, MSG_BUNDLE_VALUE_MMSTO_URI)) {  /* MMS URI */
356                                 svc_handle = __msg_ui_parse_mmsto_uri(service, uri);
357                         } else if (g_str_has_prefix(uri, MSG_BUNDLE_VALUE_FILE_URI)) {
358                                 /* file URI */
359                                 svc_handle = __msg_ui_parse_file_uri(service, uri);
360                                 if (svc_handle == NULL) {
361                                         D_MSG("cb is NULL");
362                                         return NULL;
363                                 }
364                         } else if (g_str_has_prefix(uri, "/")) {
365                                 if (service_create(&svc_handle) < 0 || svc_handle == NULL) {
366                                         D_EMSG("service_create() is failed !!");
367                                         return NULL;
368                                 }
369
370                                 D_MSG("APPSVC ATTACHMENT = [%s]", uri);
371                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_ATTACHFILE, uri);
372                         } else {
373                                 D_MSG("Not supported URI type");
374                                 return NULL;
375                         }
376                 } else {
377                         if (service_create(&svc_handle) < 0 || svc_handle == NULL) {
378                                 D_EMSG("service_create() is failed !!");
379                                 return NULL;
380                         }
381
382                         /* Add recipient number */
383                         service_get_extra_data(service, SERVICE_DATA_TO, &recipient);
384                         if (recipient) {
385                                 D_MSG("APPSVC RECIPIENT = [%s]", recipient);
386                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TO, recipient);
387                         }
388
389                         /* Add body text */
390                         service_get_extra_data(service, SERVICE_DATA_TEXT, &body_text);
391                         if (body_text) {
392                                 D_MSG("APPSVC BODY_TEXT = [%s]", body_text);
393                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_BODY, body_text);
394                         }
395
396                         /* Add subject */
397                         service_get_extra_data(service, SERVICE_DATA_SUBJECT, &subject);
398                         if (subject) {
399                                 D_MSG("APPSVC SUBJECT = [%s]", subject);
400                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_SUBJECT, subject);
401                         }
402
403                         /* Add attachment */
404                         service_get_extra_data(service, MSG_BUNDLE_KEY_ATTACHFILE, &attachment);
405                         if (attachment) {
406                                 D_MSG("APPSVC ATTACHMENT = [%s]", attachment);
407                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_ATTACHFILE, attachment);
408                         }
409                 }
410
411                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TYPE, MSG_BUNDLE_VALUE_COMPOSE);
412
413         } else if (g_strcmp0(operation, SERVICE_OPERATION_SEND_TEXT) == 0) {
414                 service_get_uri(service, &uri);
415                 D_MSG("APPSVC URI = [%s]", uri);
416
417                 if (uri) {
418                         if (g_str_has_prefix(uri, MSG_BUNDLE_VALUE_SMS_URI)) {  /* SMS URI */
419                                 svc_handle = __msg_ui_parse_sms_uri(service, uri);
420                         } else {
421                                 D_MSG("Not supported mime type");
422                                 return NULL;
423                         }
424                 } else {
425                         if (service_create(&svc_handle) < 0 || svc_handle == NULL) {
426                                 D_EMSG("service_create() is failed !!");
427                                 return NULL;
428                         }
429
430                         /* Add body text */
431                         service_get_extra_data(service, SERVICE_DATA_TEXT, &body_text);
432                         if (body_text) {
433                                 D_MSG("APPSVC BODY_TEXT = [%s]", body_text);
434                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_BODY, body_text);
435                         }
436
437                         /* Add recipient number */
438                         service_get_extra_data(service, SERVICE_DATA_TO, &recipient);
439                         if (recipient) {
440                                 D_MSG("APPSVC RECIPIENT = [%s]", recipient);
441                                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TO, recipient);
442                         }
443                 }
444
445                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TYPE, MSG_BUNDLE_VALUE_COMPOSE);
446         } else if (g_strcmp0(operation, SERVICE_OPERATION_DEFAULT) == 0) {
447                 char *key_type = NULL;
448
449                 service_get_extra_data(service, MSG_BUNDLE_KEY_TYPE, &key_type);
450                 if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_MSG_ID) == 0) {
451                         msg_error_t err = MSG_SUCCESS;
452                         msg_struct_t sort_rule_t = msg_create_struct(MSG_STRUCT_SORT_RULE);
453                         msg_struct_list_s peerList;
454                         bool oneThread = true;
455
456                         msg_set_int_value(sort_rule_t, MSG_SORT_RULE_SORT_TYPE_INT, MSG_SORT_BY_READ_STATUS);
457                         msg_set_bool_value(sort_rule_t, MSG_SORT_RULE_ACSCEND_BOOL, false);
458
459                         err = msg_get_thread_view_list(gAppData->msgHandle, sort_rule_t, &peerList);
460                         msg_release_struct(&sort_rule_t);
461
462                         if (peerList.nCount > 1) {
463                                 int unreadCnt = 0;
464                                 msg_get_int_value(peerList.msg_struct_info[1], MSG_THREAD_UNREAD_COUNT_INT, &unreadCnt);
465                                 if (unreadCnt > 0)
466                                         oneThread = false;
467                         }
468                         msg_release_list_struct(&peerList);
469
470                         if (oneThread == true)
471                                 svc_handle = __get_service_with_msg_id(gAppData, service);
472                 } else if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_REPORT) == 0) {
473                         svc_handle = __get_service_with_msg_id(gAppData, service);
474                 } else if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_BUBBLE) == 0) {
475                         svc_handle = __get_service_with_msg_id(gAppData, service);
476                 }
477         }
478
479         D_LEAVE;
480         return svc_handle;
481 }
482
483 static service_h __get_service_with_new_msg(struct appdata *ad)
484 {
485         D_ENTER;
486
487         msg_error_t err = MSG_SUCCESS;
488         msg_struct_list_s peerList;
489         msg_struct_t sort_rule_t = msg_create_struct(MSG_STRUCT_SORT_RULE);
490         service_h svc_handle = NULL;
491
492         int i = 0;
493         int new_sms_cnt = 0;
494         int new_mms_cnt = 0;
495         int unreadCnt = 0;
496         char buf[DEF_BUF_LEN_L] = {'0',};
497         char buf_contact[DEF_BUF_LEN] = {'0',};
498
499         msg_set_int_value(sort_rule_t, MSG_SORT_RULE_SORT_TYPE_INT, MSG_SORT_BY_THREAD_DATE);
500         msg_set_bool_value(sort_rule_t, MSG_SORT_RULE_ACSCEND_BOOL, false);
501
502         vconf_get_int(VCONFKEY_MESSAGE_RECV_SMS_STATE, &new_sms_cnt);
503         vconf_get_int(VCONFKEY_MESSAGE_RECV_MMS_STATE, &new_mms_cnt);
504
505         err = msg_get_thread_view_list(ad->msgHandle, sort_rule_t, &peerList);
506         if (err != MSG_SUCCESS) {
507                 msg_release_struct(&sort_rule_t);
508                 return NULL;
509         }
510
511         if (peerList.nCount <= 0) {
512                 msg_release_list_struct(&peerList);
513                 msg_release_struct(&sort_rule_t);
514                 return NULL;
515         }
516
517         for (i=0; i<peerList.nCount; i++){
518                 msg_get_int_value(peerList.msg_struct_info[i], MSG_THREAD_UNREAD_COUNT_INT, &unreadCnt);
519                 if (unreadCnt > 0)
520                         break;
521         }
522
523         if(i >= peerList.nCount) {
524                 msg_release_list_struct(&peerList);
525                 msg_release_struct(&sort_rule_t);
526                 return NULL;
527         }
528
529         msg_get_int_value(peerList.msg_struct_info[i], MSG_THREAD_UNREAD_COUNT_INT, &unreadCnt);
530         if(unreadCnt == new_sms_cnt+new_mms_cnt){
531                 if (service_create(&svc_handle) < 0 || svc_handle == NULL) {
532                         D_EMSG("service_create() is failed !!");
533                         msg_release_list_struct(&peerList);
534                         msg_release_struct(&sort_rule_t);
535                         return NULL;
536                 }
537
538                 msg_struct_list_s *addrList = NULL;
539                 int thread_id = 0;
540                 int contact_id = 0;
541                 char strName[DEF_THREAD_NAME_LEN+1] = {0,};
542                 char strNumber[DEF_THREAD_ADDR_LEN+1] = {0,};
543
544                 msg_get_int_value(peerList.msg_struct_info[i], MSG_THREAD_ID_INT, &thread_id);
545                 msg_get_list_handle(peerList.msg_struct_info[i], MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addrList);
546
547                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_TYPE, MSG_BUNDLE_VALUE_NEW_MSG);
548                 snprintf(buf, DEF_BUF_LEN_L, "%d", thread_id);
549                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_THREAD_ID, buf);
550                 msg_get_str_value(peerList.msg_struct_info[i], MSG_THREAD_NAME_STR, strName, DEF_THREAD_NAME_LEN);
551                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_THREAD_NAME, strName);
552
553                 msg_get_str_value(addrList->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, DEF_THREAD_ADDR_LEN);
554                 msg_get_int_value(addrList->msg_struct_info[0], MSG_ADDRESS_INFO_CONTACT_ID_INT, &contact_id);
555
556                 snprintf(buf_contact, DEF_BUF_LEN, "%d", contact_id);
557                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_THREAD_ADDRESS, strNumber);
558                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_CONTACT_ID, buf_contact);
559         }
560
561         msg_release_struct(&sort_rule_t);
562         msg_release_list_struct(&peerList);
563
564         D_LEAVE;
565
566         return svc_handle;
567 }
568
569 static service_h __get_service_with_msg_id(struct appdata *ad, service_h service)
570 {
571         D_ENTER;
572
573         service_h svc_handle = NULL;
574
575         if (service_create(&svc_handle) < 0 || svc_handle == NULL) {
576                 D_EMSG("service_create() is failed !!");
577                 return NULL;
578         }
579
580         int msg_id = 0;
581         char *msg_id_str = NULL;
582         service_get_extra_data(service, MSG_BUNDLE_KEY_MSG_ID, &msg_id_str);
583
584         if (!msg_id_str) {
585                 service_destroy(svc_handle);
586                 return NULL;
587         }
588
589         msg_id = atoi(msg_id_str);
590
591         if (msg_id <= 0) {
592                 service_destroy(svc_handle);
593                 return NULL;
594         }
595
596         msg_struct_t msgInfo = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
597         msg_struct_t sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
598         msg_error_t err = MSG_SUCCESS;
599
600         int thread_id = 0;
601         int contact_id = 0;
602         char buf_thread[DEF_BUF_LEN_S] = {0,};
603         char buf_storage[DEF_BUF_LEN_S] = {0,};
604         char buf_contact[DEF_BUF_LEN_S] = {0,};
605
606         msg_struct_list_s *addr_list = NULL;
607         char strNumber[DEF_THREAD_ADDR_LEN + 1] = {0,};
608         char strName[DEF_THREAD_NAME_LEN + 1] = {0,};
609
610         err = msg_get_message(ad->msgHandle, (msg_message_id_t)msg_id, msgInfo, sendOpt);
611
612         if (err != MSG_SUCCESS) {
613                 service_destroy(svc_handle);
614                 msg_release_struct(&msgInfo);
615                 msg_release_struct(&sendOpt);
616                 return NULL;
617         }
618
619         msg_get_int_value(msgInfo, MSG_MESSAGE_THREAD_ID_INT, &thread_id);
620         snprintf(buf_thread, sizeof(buf_thread), "%d", thread_id);
621
622         msg_get_list_handle(msgInfo, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
623
624         msg_get_str_value(addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, DEF_THREAD_ADDR_LEN);
625         msg_get_str_value(addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_DISPLAYNAME_STR, strName, DEF_THREAD_NAME_LEN);
626         msg_get_int_value(addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_CONTACT_ID_INT, &contact_id);
627         snprintf(buf_contact, sizeof(buf_contact), "%d", contact_id);
628
629         service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_THREAD_ID, buf_thread);
630         service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_THREAD_NAME, strName);
631         service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_THREAD_ADDRESS, strNumber);
632         service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_CONTACT_ID, buf_contact);
633         service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_STORAGE_ID, buf_storage);
634
635         msg_release_struct(&msgInfo);
636         msg_release_struct(&sendOpt);
637
638         D_LEAVE;
639         return svc_handle;
640 }
641
642 static Evas_Object* create_win(const char *name)
643 {
644         D_ENTER;
645
646         Evas_Object *eo;
647         int w, h;
648
649         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
650         if (eo) {
651                 elm_win_title_set(eo, name);
652                 evas_object_smart_callback_add(eo, "delete,request", win_del, NULL);
653                 ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
654                 evas_object_resize(eo, w, h);
655                 elm_win_conformant_set(eo, EINA_TRUE);
656         }
657
658         D_LEAVE;
659         return eo;
660 }
661
662 void layout_cb(ui_gadget_h ug, enum ug_mode mode, void *priv)
663 {
664         D_ENTER;
665
666         struct appdata *ad;
667         Evas_Object *base;
668
669         if (!ug || !priv)
670                 return;
671
672         ad = priv;
673
674         base = ug_get_layout(ug);
675
676         if (!base){
677                 ug_destroy(ug);
678                 return;
679         }
680
681         switch (mode) {
682                 case UG_MODE_FULLVIEW:
683                         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
684                         evas_object_show(base);
685                         break;
686                 case UG_MODE_FRAMEVIEW:
687                 default:
688                         break;
689         }
690
691         D_LEAVE;
692 }
693
694 void result_cb(ui_gadget_h ug, service_h result, void *priv)
695 {
696         D_ENTER;
697
698         MSG_UI_RET_IF(MSG_UI_LEVEL_ERR, !ug || !priv);
699
700         struct appdata *ad = priv;
701
702         PMSG_THREAD_DATA pData = (PMSG_THREAD_DATA)ad->thread_data;
703         PMSG_THREAD_LIST_DATA pListData = NULL;
704         char *buf = NULL;
705         char *str_result = NULL;
706
707         service_get_extra_data(result, MSG_BUNDLE_KEY_RESULT, &str_result);
708         if (pData == NULL) {
709                 if (!g_strcmp0(str_result, MSG_BUNDLE_VALUE_DEL_ALL))
710                         msg_ui_destroy_composer_ug(ug, false);
711
712                 elm_exit();
713                 return;
714         }
715
716         pListData = msg_ui_thread_get_current_list();
717
718         if (!g_strcmp0(str_result, MSG_BUNDLE_VALUE_DEL_ALL)) {
719                 elm_object_focus_set(pListData->genlist, EINA_TRUE);
720                 msg_ui_destroy_composer_ug(ug, false);
721
722                 if (pListData) {
723                         int del_id = 0;
724
725                         service_get_extra_data(result, MSG_BUNDLE_KEY_MSG_ID, &buf);
726                         if (buf && pListData->app_data_type != THREAD_LIST_APP_DATA_CONV) {
727                                 del_id = atoi(buf);
728                                 buf = NULL;
729                                 msg_ui_thread_list_msg_item_delete(pListData, del_id);
730                         }
731
732                         service_get_extra_data(result, MSG_BUNDLE_KEY_THREAD_ID, &buf);
733                         if (buf && pListData->app_data_type != THREAD_LIST_APP_DATA_MSG) {
734                                 del_id = atoi(buf);
735                                 buf = NULL;
736                                 msg_ui_thread_list_item_delete(pListData, del_id);
737                         }
738
739                         if (pListData->item_cnt <= 0 && pListData->search_mode == THREAD_SEARCH_ON)
740                                 msg_ui_thread_cancel_search_mode(pListData);
741
742                         if (pData->isRotate == true) {
743                                 if (!pData->split_data) {
744                                         msg_ui_thread_create_split_data();
745                                         msg_ui_thread_splitview_launch(pData, pData->split_data);
746                                 }
747                         } else {
748                                 elm_object_part_content_set(pData->panes, "left", pData->layout_main);
749                                 elm_object_part_content_unset(pData->panes, "right");
750                                 elm_panes_content_left_size_set(pData->panes, 1.0);
751                         }
752
753                         pListData->sel_gen_item = NULL;
754                         pListData->sel_thread_id = 0;
755                         pListData->sel_msg_id = 0;
756                 }
757         } else if (!g_strcmp0(str_result, MSG_BUNDLE_VALUE_DEL_BUBBLE)) {
758                 service_get_extra_data(result, MSG_BUNDLE_KEY_THREAD_ID, &buf);
759                 if (buf && pListData->app_data_type != THREAD_LIST_APP_DATA_MSG) {
760                         int thread_id = 0;
761                         thread_id = atoi(buf);
762                         buf = NULL;
763                         msg_ui_thread_list_item_update(pListData, thread_id);
764                 }
765         } else if (!g_strcmp0(str_result, MSG_BUNDLE_VALUE_KEYPAD_SHOW)) {
766                 pData->keypadIsShown = true;
767                 if (pData->isRotate == true) {
768                         elm_panes_content_left_size_set(pData->panes, 0.0);
769                         elm_object_signal_emit(pData->panes, "elm,panes,unpair", "");
770                 }
771         } else if (!g_strcmp0(str_result, MSG_BUNDLE_VALUE_KEYPAD_HIDE)) {
772                 pData->keypadIsShown = false;
773                 if (pData->isRotate == true) {
774                         elm_panes_content_left_size_set(pData->panes, 0.4);
775                         elm_object_signal_emit(pData->panes, "elm,panes,pair", "");
776                 }
777         } else {
778                 int update_id = 0;
779
780                 service_get_extra_data(result, MSG_BUNDLE_KEY_MSG_ID, &buf);
781                 if (buf && pListData->app_data_type != THREAD_LIST_APP_DATA_CONV) {
782                         update_id = atoi(buf);
783                         buf = NULL;
784                         msg_ui_thread_list_msg_item_update(pListData, update_id);
785                 }
786
787                 service_get_extra_data(result, MSG_BUNDLE_KEY_THREAD_ID, &buf);
788                 if (buf && pListData->app_data_type != THREAD_LIST_APP_DATA_MSG) {
789                         update_id = atoi(buf);
790                         buf = NULL;
791                         msg_ui_thread_list_item_update(pListData, update_id);
792                 }
793         }
794
795         D_LEAVE;
796 }
797
798 void destroy_cb(ui_gadget_h ug, void *priv)
799 {
800         D_ENTER;
801
802         MSG_UI_RET_IF(MSG_UI_LEVEL_ERR, !ug || !priv);
803
804         struct appdata *ad = (struct appdata *)priv;
805         int ug_type = msg_ui_get_composer_ug_type(ug);
806         PMSG_THREAD_LIST_DATA pListData = msg_ui_thread_get_current_list();
807
808         if (ad->layout_main) {
809                 PMSG_THREAD_DATA pData = (PMSG_THREAD_DATA)ad->thread_data;
810                 if (pListData == NULL) {
811                         msg_ui_destroy_composer_ug(ug, false);
812                         elm_exit();
813                         return;
814                 }
815
816                 if (pData) {
817                         pData->keypadIsShown = false;
818
819                         if (ug_type == MSG_COMPOSER_UG_TYPE_VIEWER) {
820                                 if (pData->isRotate == true) {
821                                         if (!pData->split_data) {
822                                                 msg_ui_thread_create_split_data();
823                                                 msg_ui_thread_splitview_launch(pData, pData->split_data);
824                                         }
825
826                                         if (pListData->search_mode == THREAD_SEARCH_ON)
827                                                 msg_ui_thread_cancel_search_mode(pListData);
828                                         else
829                                                 elm_win_lower(pData->win_main);
830                                 } else {
831                                         elm_object_part_content_set(pData->panes, "left", pData->layout_main);
832                                         elm_object_part_content_unset(pData->panes, "right");
833                                         elm_panes_content_left_size_set(pData->panes, 1.0);
834                                 }
835                         }
836                         pListData->view_mode = THREAD_NORMAL_VIEW;
837                 }
838                 msg_ui_destroy_composer_ug(ug, false);
839         } else {
840                 msg_ui_destroy_composer_ug(ug, false);
841                 elm_exit();
842         }
843 }
844
845 int msg_ui_load_composer_ug(service_h svc_handle, MessageComposerUgType ug_type, bool isListItem)
846 {
847         D_ENTER;
848
849         if (!gAppData)
850                 return MSG_UI_RET_ERR;
851
852         ugdata *composer_data = NULL;
853         ui_gadget_h ug_h = NULL;
854         bool bReset = false;
855         ugdata *exist_data = gAppData->composer_data;
856         PMSG_THREAD_LIST_DATA pListData = msg_ui_thread_get_current_list();
857
858         if (ug_type == MSG_COMPOSER_UG_TYPE_VIEWER) {
859                 while (exist_data) {
860                         if (exist_data->ug_type == MSG_COMPOSER_UG_TYPE_VIEWER) {
861                                 bReset = true;
862                                 break;
863                         }
864                         exist_data = exist_data->next_ug;
865                 }
866
867                 if (exist_data && bReset) {
868                         ug_send_message(exist_data->ug_h, svc_handle);
869                         ug_h = exist_data->ug_h;
870                 } else {
871                         service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_FROM, MSG_BUNDLE_VALUE_INTERNAL);
872                         ug_h = ug_create(NULL, MSG_COMPOSER_UG_NAME, UG_MODE_FRAMEVIEW, svc_handle, &gAppData->cbs);
873                 }
874         } else {
875                 service_add_extra_data(svc_handle, MSG_BUNDLE_KEY_FROM, MSG_BUNDLE_VALUE_INTERNAL);
876                 ug_h = ug_create(NULL, MSG_COMPOSER_UG_NAME, UG_MODE_FULLVIEW, svc_handle, &gAppData->cbs);
877         }
878
879         if (!ug_h)
880                 return MSG_UI_RET_ERR;
881
882         if (bReset == false) {
883                 composer_data = (ugdata *)calloc(1, sizeof(ugdata));
884                 composer_data->ug_h = ug_h;
885                 composer_data->ug_type = ug_type;
886
887                 exist_data = gAppData->composer_data;
888                 if (exist_data) {
889                         while (exist_data) {
890                                 if (exist_data->next_ug == NULL) {
891                                         exist_data->next_ug = composer_data;
892                                         composer_data->prev_ug = exist_data;
893                                         break;
894                                 }
895                                 exist_data = exist_data->next_ug;
896                         }
897                 } else {
898                         gAppData->composer_data = composer_data;
899                 }
900
901                 if (pListData && isListItem) {
902                         composer_data->sel_gen_item = pListData->sel_gen_item;
903                         composer_data->sel_thread_id = pListData->sel_thread_id;
904                         composer_data->sel_msg_id = pListData->sel_msg_id;
905                 }
906         } else if (exist_data && pListData && isListItem) {
907                 exist_data->sel_gen_item = pListData->sel_gen_item;
908                 exist_data->sel_thread_id = pListData->sel_thread_id;
909                 exist_data->sel_msg_id = pListData->sel_msg_id;
910         }
911
912         if (ug_type == MSG_COMPOSER_UG_TYPE_VIEWER) {
913                 PMSG_THREAD_DATA pData = (PMSG_THREAD_DATA)gAppData->thread_data;
914                 Evas_Object * layout = (Evas_Object *)ug_get_layout(ug_h);
915                 elm_object_part_content_set(pData->panes, "right", layout);
916                 msg_ui_thread_destroy_split_data(pData->split_data);
917
918                 if (pData->isRotate == false) {
919                         elm_panes_content_left_size_set(pData->panes, 0.0);
920                         evas_object_hide(pData->layout_main);
921                 }
922         }
923
924         D_LEAVE;
925         return MSG_UI_RET_SUCCESS;
926 }
927
928 Evas_Object *msg_ui_get_main_layout(void)
929 {
930         D_ENTER;
931
932         if (!gAppData)
933                 return NULL;
934
935         return gAppData->layout_main;
936 }
937
938 ugdata *msg_ui_get_composer_ug_data(ui_gadget_h ug)
939 {
940         D_ENTER;
941         ugdata *composer_data = NULL;
942
943         if (!gAppData || !gAppData->composer_data)
944                 return NULL;
945
946         composer_data = gAppData->composer_data;
947         while (composer_data) {
948                 if (composer_data->ug_h == ug)
949                         return composer_data;
950
951                 composer_data = composer_data->next_ug;
952         }
953
954         D_LEAVE;
955         return NULL;
956 }
957
958 Evas_Object *msg_ui_get_composer_ug_viewer_layout(void)
959 {
960         D_ENTER;
961         ugdata *composer_data = NULL;
962         Evas_Object * layout = NULL;
963
964         if (!gAppData || !gAppData->composer_data)
965                 return NULL;
966
967         composer_data = gAppData->composer_data;
968         while (composer_data) {
969                 if (composer_data->ug_type == MSG_COMPOSER_UG_TYPE_VIEWER) {
970                         layout = (Evas_Object *)ug_get_layout(composer_data->ug_h);
971                         break;
972                 }
973                 composer_data = composer_data->next_ug;
974         }
975
976         D_LEAVE;
977         return layout;
978 }
979
980 void msg_ui_destroy_composer_ug(ui_gadget_h ug, bool bDeleteAll)
981 {
982         D_ENTER;
983         ugdata *composer_data = NULL;
984         PMSG_THREAD_LIST_DATA pListData = NULL;
985
986         pListData = msg_ui_thread_get_current_list();
987
988         composer_data = gAppData->composer_data;
989         if (bDeleteAll) {
990                 while (composer_data) {
991                         if (composer_data->ug_h)
992                                 ug_destroy(composer_data->ug_h);
993
994                         free(composer_data);
995                         composer_data = composer_data->next_ug;
996                 }
997                 gAppData->composer_data = NULL;
998         } else {
999                 while (composer_data) {
1000                         if (ug == NULL)
1001                                 break;
1002
1003                         if (ug == composer_data->ug_h) {
1004                                 if (pListData) {
1005                                         if (pListData->genlist)
1006                                                 elm_object_focus_set(pListData->genlist, EINA_TRUE);
1007
1008                                         if ((pListData->sel_thread_id > 0) && (pListData->sel_thread_id == composer_data->sel_thread_id)) {
1009                                                 int thread_id = pListData->sel_thread_id;
1010
1011                                                 pListData->sel_thread_id = 0;
1012                                                 msg_ui_thread_list_item_update(pListData, thread_id);
1013                                         }
1014
1015                                         if ((pListData->sel_msg_id > 0) && (pListData->sel_msg_id == composer_data->sel_msg_id)) {
1016                                                 int msg_id = pListData->sel_msg_id;
1017
1018                                                 pListData->sel_msg_id = 0;
1019                                                 msg_ui_thread_list_msg_item_update(pListData, msg_id);
1020                                         }
1021
1022                                         if (pListData->sel_gen_item == composer_data->sel_gen_item)
1023                                                 pListData->sel_gen_item = NULL;
1024
1025                                         msg_ui_thread_set_title_unread_cnt(pListData);
1026                                 }
1027
1028                                 ug_destroy(ug);
1029                                 if (composer_data->prev_ug) {
1030                                         if (composer_data->next_ug)
1031                                                 composer_data->prev_ug->next_ug = composer_data->next_ug;
1032                                         else
1033                                                 composer_data->prev_ug->next_ug = NULL;
1034                                 } else {
1035                                         gAppData->composer_data = NULL;
1036                                 }
1037                                 free(composer_data);
1038                                 break;
1039                         }
1040                         composer_data = composer_data->next_ug;
1041                 }
1042         }
1043         D_LEAVE;
1044 }
1045
1046 int msg_ui_get_composer_ug_type(ui_gadget_h ug)
1047 {
1048         D_ENTER;
1049         ugdata *composer_data = NULL;
1050         int ug_type = MSG_COMPOSER_UG_TYPE_NONE;
1051
1052         if (!gAppData || !gAppData->composer_data)
1053                 return MSG_COMPOSER_UG_TYPE_NONE;
1054
1055         composer_data = gAppData->composer_data;
1056         while (composer_data) {
1057                 if (composer_data->ug_h == ug) {
1058                         ug_type = composer_data->ug_type;
1059                         break;
1060                 }
1061                 composer_data = composer_data->next_ug;
1062         }
1063
1064         D_LEAVE;
1065         return ug_type;
1066 }
1067
1068 static bool app_create(void *data)
1069 {
1070         /* return TRUE : success, return FALSE : not to run main loop */
1071
1072         D_ENTER;
1073
1074         MSG_UI_RETV_IF(MSG_UI_LEVEL_ERR, !data, MSG_UI_RET_ERR);
1075
1076         struct appdata *ad = data;
1077         Evas_Object *win;
1078         sound_manager_error_e snd_err = SOUND_MANAGER_ERROR_NONE;
1079         contacts_error_e ct_err = CONTACTS_ERROR_NONE;
1080
1081         /* create window */
1082         win = create_win(MESSAGE_PKGNAME);
1083         if (win == NULL)
1084                 return FALSE;
1085
1086         ad->win_main = win;
1087
1088         ad->bg = elm_bg_add(ad->win_main);
1089         evas_object_size_hint_weight_set(ad->bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1090         elm_win_resize_object_add(ad->win_main, ad->bg);
1091
1092         ad->conform = elm_conformant_add(win);
1093         if (ad->conform == NULL)
1094                 return FALSE;
1095
1096         evas_object_size_hint_weight_set(ad->conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1097         elm_win_resize_object_add(win, ad->conform);
1098         evas_object_show(ad->conform);
1099
1100         snd_err = sound_manager_set_session_type(SOUND_SESSION_TYPE_SHARE);
1101         if (snd_err != SOUND_MANAGER_ERROR_NONE)
1102                 D_MSG("sound_manager_set_session_type is failed, snd_err = [%d]", snd_err);
1103
1104         ct_err = contacts_connect2();
1105         if (ct_err != CONTACTS_ERROR_NONE)
1106                 D_EMSG("contacts_svc_connect2 failed : ct_err = [%d]", ct_err);
1107
1108         UG_INIT_EFL(ad->win_main, UG_OPT_INDICATOR_ENABLE);
1109         elm_win_indicator_mode_set(ad->win_main, ELM_WIN_INDICATOR_SHOW);
1110
1111         if(elm_win_wm_rotation_supported_get(ad->win_main)) {
1112                 int rotation[4] = {0, 90, 180, 270};
1113                 elm_win_wm_rotation_available_rotations_set(ad->win_main, rotation, 4);
1114                 ad->cur_rotation = elm_win_rotation_get(ad->win_main);
1115                 D_MSG("current rotation %d", ad->cur_rotation);
1116                 evas_object_smart_callback_add(ad->win_main, "wm,rotation,changed", rotation_changed_cb, ad);
1117         }
1118
1119         ad->cbs.layout_cb = layout_cb;
1120         ad->cbs.result_cb = result_cb;
1121         ad->cbs.destroy_cb = destroy_cb;
1122         ad->cbs.priv = (void *)ad;
1123
1124         gAppData = ad; // Set global app data pointer;
1125
1126         D_LEAVE;
1127         return TRUE;
1128 }
1129
1130 static void app_terminate(void *data)
1131 {
1132         D_ENTER;
1133
1134         MSG_UI_RETM_IF(MSG_UI_LEVEL_ERR, !data, "data is null");
1135
1136         struct appdata *ad = data;
1137         msg_error_t     err;
1138         contacts_error_e ct_err = CONTACTS_ERROR_NONE;
1139
1140         ug_destroy_all();
1141
1142         ct_err = contacts_disconnect2();
1143         if (ct_err != CONTACTS_ERROR_NONE)
1144                 D_EMSG("contacts_svc_disconnect2 failed : ct_err = [%d]", ct_err);
1145
1146         if (ad->thread_data)
1147                 msg_ui_thread_deinit_thread_data(ad->thread_data);
1148
1149         if (ad->msgHandle) {
1150                 err = msg_close_msg_handle(&ad->msgHandle);
1151                 D_MSG("msg_close_msg_handle, ret = [%d]", err);
1152         }
1153
1154         D_LEAVE;
1155 }
1156
1157 static void app_pause(void *data)
1158 {
1159         D_ENTER;
1160
1161         msg_ui_thread_set_app_state(MSG_UI_STATE_PAUSE);
1162         ug_pause();
1163
1164         D_LEAVE;
1165 }
1166
1167 static void app_resume(void *data)
1168 {
1169         D_ENTER;
1170         msg_ui_thread_set_app_state(MSG_UI_STATE_RUNNING);
1171
1172         ug_resume();
1173
1174         D_LEAVE;
1175 }
1176
1177 static void app_service(service_h service, void *data)
1178 {
1179         D_ENTER;
1180         MSG_UI_RETM_IF(MSG_UI_LEVEL_ERR, !data, "data is NULL");
1181
1182         struct appdata *ad = data;
1183         service_h svc_handle = NULL;
1184         msg_error_t err = MSG_SUCCESS;
1185         msg_handle_t msgHandle = NULL;
1186         bool isDefaultView = false;
1187
1188         char *operation = NULL;
1189         char *cvalue = NULL;
1190         int ret = SYSTEM_INFO_ERROR_NONE;
1191
1192         ret = system_info_get_value_string(SYSTEM_INFO_KEY_MODEL, &cvalue);
1193         if (ret == SYSTEM_INFO_ERROR_NONE && cvalue != NULL) {
1194                 if (g_strcmp0(cvalue, "Emulator") == 0) {
1195                         D_MSG("Not support in Emulator !!");
1196                         g_free(cvalue);
1197                         cvalue = NULL;
1198
1199                         service_h reply;
1200
1201                         int ret = service_create(&reply);
1202                         if (ret != SERVICE_ERROR_NONE) {
1203                                 D_EMSG("service_create() is failed : ret = %d", ret);
1204                         } else {
1205                                 service_reply_to_launch_request(reply, service, SERVICE_RESULT_CANCELED);
1206                                 service_destroy(reply);
1207                         }
1208
1209                         /* Exit application because it is not supported in Emulator. */
1210                         elm_exit();
1211                         return;
1212                 }
1213         }
1214
1215         if (cvalue) {
1216                 g_free(cvalue);
1217                 cvalue = NULL;
1218         }
1219
1220         ug_resume();
1221
1222         if (!ad->msgHandle) {
1223                 err = msg_open_msg_handle(&msgHandle);
1224                 if (err != MSG_SUCCESS) {
1225                         MSG_UI_DEBUG(MSG_UI_LEVEL_ASSERT, "msg_open_msg_handle failed, Error=[%d]\n", err);
1226                         __msg_ui_popup_warning(ad->win_main,  dgettext("sys_string", "IDS_COM_POP_SERVICE_UNAVAILABLE"));
1227                         return;
1228                 }
1229                 ad->msgHandle = msgHandle;
1230         }
1231
1232         if (service) {
1233                 service_get_operation(service, &operation);
1234                 if (operation) {
1235                         svc_handle = __get_service_app_svc_op(operation, service);
1236                 } else {
1237                         char *key_type = NULL;
1238
1239                         service_get_extra_data(service, MSG_BUNDLE_KEY_TYPE, &key_type);
1240                         if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_COMPOSE) == 0) {
1241                                 service_clone(&svc_handle, service);
1242                         } else if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_NEW_MSG) == 0) {
1243                                 svc_handle = __get_service_with_new_msg(ad);
1244                         } else if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_MSG_ID) == 0) {
1245                                 svc_handle = __get_service_with_msg_id(ad, service);
1246                         } else if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_REPORT) == 0) {
1247                                 svc_handle = __get_service_with_msg_id(ad, service);
1248                         } else if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_BUBBLE) == 0) {
1249                                 svc_handle = __get_service_with_msg_id(ad, service);
1250                         } else {
1251                                 char *mime_type = NULL;
1252
1253                                 service_get_extra_data(service, AUL_K_MIME_TYPE, &mime_type);
1254                                 if (g_strcmp0(mime_type, MSG_BUNDLE_VALUE_SMS_URI) == 0)
1255                                         svc_handle = __msg_ui_parse_sms_uri(service, NULL);
1256                                 else if (g_strcmp0(mime_type, MSG_BUNDLE_VALUE_MMSTO_URI) == 0)
1257                                         svc_handle = __msg_ui_parse_mmsto_uri(service, NULL);
1258                                 else
1259                                         isDefaultView = true;
1260                         }
1261                 }
1262
1263                 if (!svc_handle) {
1264                         isDefaultView = true;
1265                 } else {
1266                         int storge_id = 0;
1267                         char *buf = NULL;
1268
1269                         service_get_extra_data(svc_handle, MSG_BUNDLE_KEY_STORAGE_ID, &buf);
1270                         if (buf != NULL)
1271                                 storge_id = atoi(buf);
1272
1273                         if (storge_id == MSG_STORAGE_SIM)
1274                                 isDefaultView = true;
1275                 }
1276         } else {
1277                 isDefaultView = true;
1278         }
1279
1280         if (isDefaultView) {
1281                 MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "show DefaultView");
1282                 if (ad->composer_data) {
1283                         ugdata *composer_data = ad->composer_data;
1284
1285                         while (composer_data) {
1286                                 service_h composer_svc_handle = NULL;
1287                                 if (service_create(&composer_svc_handle) < 0 || composer_svc_handle == NULL)
1288                                         D_EMSG("service_create() is failed !!");
1289
1290                                 composer_data = composer_data->next_ug;
1291                         }
1292                         msg_ui_destroy_composer_ug(NULL, true);
1293                 }
1294
1295                 if (!ad->thread_data)
1296                         ad->thread_data = msg_ui_thread_init_thread_data(ad);
1297
1298                 if (svc_handle) {
1299                         /*sim message viewer*/
1300                         MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "show sim message viewer");
1301
1302                         if (!ad->layout_main) {
1303                                 ad->layout_main = msg_ui_thread_create_layout_main(ad->thread_data);
1304                                 evas_object_size_hint_weight_set(ad->layout_main, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1305                                 elm_object_content_set(ad->conform, ad->layout_main);
1306                         } else {
1307                                 PMSG_THREAD_DATA pData = (PMSG_THREAD_DATA)ad->thread_data;
1308                                 int i = 0;
1309
1310                                 if (pData->detail_layout) {
1311                                         if (pData->isRotate == true)
1312                                                 elm_object_item_del(elm_naviframe_top_item_get(pData->split_data->navi_frame));
1313                                         else
1314                                                 elm_object_item_del(elm_naviframe_top_item_get(pData->navi_frame));
1315                                         pData->detail_layout = NULL;
1316                                 }
1317
1318                                 for (i = MSG_THREAD_LIST_MAX_COUNT-1; i >= 0; i--) {
1319                                         if (pData->list_data[i] != NULL) {
1320                                                  if (pData->list_data[i]->loaded_ug) {
1321                                                         ug_destroy(pData->list_data[i]->loaded_ug);
1322                                                         pData->list_data[i]->loaded_ug = NULL;
1323                                                 }
1324                                                 elm_object_item_del(elm_naviframe_top_item_get(pData->navi_frame));
1325                                                 elm_genlist_clear(pData->list_data[i]->genlist);
1326                                                 msg_ui_thread_destroy_thread_list_data(pData->list_data[i]);
1327                                         }
1328                                 }
1329
1330                                 if (pData->split_data)
1331                                         msg_ui_thread_destroy_split_data(pData->split_data);
1332                         }
1333                         elm_win_conformant_set(ad->win_main, 1);
1334
1335                         int msg_id = 0;
1336                         char *buf = NULL;
1337                         PMSG_APP_THREAD_ADDR_INFO_S* addr_info = NULL;
1338
1339                         char* strNumber = NULL;
1340                         char* strName = NULL;
1341
1342                         service_get_extra_data(service, MSG_BUNDLE_KEY_MSG_ID, &buf);
1343                         if (buf != NULL) {
1344                                 msg_id = atoi(buf);
1345                                 buf = NULL;
1346                         }
1347
1348                         addr_info = (PMSG_APP_THREAD_ADDR_INFO_S *)calloc(1, sizeof(PMSG_APP_THREAD_ADDR_INFO_S));
1349                         addr_info[0] = (PMSG_APP_THREAD_ADDR_INFO_S)calloc(1, sizeof(MSG_APP_THREAD_ADDR_INFO_S));
1350
1351                         service_get_extra_data(svc_handle, MSG_BUNDLE_KEY_CONTACT_ID, &buf);
1352                         if (buf != NULL) {
1353                                 addr_info[0]->contact_id = atoi(buf);
1354                                 buf = NULL;
1355                         }
1356
1357                         service_get_extra_data(svc_handle, MSG_BUNDLE_KEY_THREAD_ADDRESS, &strNumber);
1358                         if (strNumber && strlen(strNumber) > 0)
1359                                 strncpy(addr_info[0]->address, strNumber, DEF_THREAD_ADDR_LEN);
1360                         else
1361                                 strncpy(addr_info[0]->address, "", DEF_THREAD_ADDR_LEN);
1362
1363                         service_get_extra_data(svc_handle, MSG_BUNDLE_KEY_THREAD_NAME, &strName);
1364                         if (strName && strlen(strName) > 0)
1365                                 strncpy(addr_info[0]->display_name, strName, DEF_THREAD_NAME_LEN);
1366                         else
1367                                 strncpy(addr_info[0]->display_name, "", DEF_THREAD_NAME_LEN);
1368
1369                         msg_update_read_status(ad->msgHandle, msg_id, true);
1370                         msg_ui_thread_launch_msg_detail_view(NULL, msg_id, 1, addr_info);
1371                         free(addr_info[0]);
1372                         free(addr_info);
1373                 } else {
1374                         if (!ad->layout_main) {
1375                                 ad->layout_main = msg_ui_thread_create_layout_main(ad->thread_data);
1376                                 evas_object_size_hint_weight_set(ad->layout_main, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1377                                 elm_object_content_set(ad->conform, ad->layout_main);
1378
1379                                 msg_ui_thread_load_thread_view(ad->thread_data, service);
1380                         } else {
1381                                 msg_ui_thread_reset_thread_view(ad->thread_data, service);
1382                         }
1383                 }
1384         } else {
1385                 MSG_UI_DEBUG(MSG_UI_LEVEL_DEBUG, "show app-service view");
1386                 /* If message app is called by app-service to display composer view,
1387                  * list view should be destroyed because previous screen should be displayed
1388                  * when user tap back button.
1389                  */
1390                  MessageComposerUgType ug_type = MSG_COMPOSER_UG_TYPE_NONE;
1391
1392                 if (operation != NULL) {
1393                         int i = 0;
1394                         PMSG_THREAD_DATA pData = (PMSG_THREAD_DATA)ad->thread_data;
1395
1396                         if (g_strcmp0(operation, SERVICE_OPERATION_SEND) == 0 ||
1397                                 g_strcmp0(operation, SERVICE_OPERATION_SEND_TEXT) == 0) {
1398                                 if (ad->layout_main) {
1399                                         if (pData) {
1400                                                 /* content unset composer */
1401                                                 if (elm_object_part_content_get(pData->panes, "right") != NULL)
1402                                                         elm_object_part_content_unset(pData->panes, "right");
1403
1404                                                 for (i = MSG_THREAD_LIST_MAX_COUNT-1; i >= 0; i--) {
1405                                                         if (pData->list_data[i] != NULL) {
1406                                                                 elm_object_item_del(elm_naviframe_top_item_get(pData->navi_frame));
1407                                                                 elm_genlist_clear(pData->list_data[i]->genlist);
1408                                                                 msg_ui_thread_destroy_thread_list_data(pData->list_data[i]);
1409                                                         }
1410                                                 }
1411
1412                                                 if (pData->split_data)
1413                                                         msg_ui_thread_destroy_split_data(pData->split_data);
1414                                         }
1415                                         evas_object_del(ad->layout_main);
1416                                         ad->layout_main = NULL;
1417                                 }
1418                                 ug_type = MSG_COMPOSER_UG_TYPE_COMPOSER;
1419                         } else if (g_strcmp0(operation, SERVICE_OPERATION_DEFAULT) == 0) {
1420                                 char *key_type = NULL;
1421
1422                                 service_get_extra_data(service, MSG_BUNDLE_KEY_TYPE, &key_type);
1423                                 if (g_strcmp0(key_type, MSG_BUNDLE_VALUE_MSG_ID) == 0 || g_strcmp0(key_type, MSG_BUNDLE_VALUE_REPORT) == 0 || g_strcmp0(key_type, MSG_BUNDLE_VALUE_BUBBLE) == 0) {
1424                                         if (ad->layout_main) {
1425                                                 if (pData) {
1426                                                         /* content unset composer */
1427                                                         if (elm_object_part_content_get(pData->panes, "right") != NULL)
1428                                                                 elm_object_part_content_unset(pData->panes, "right");
1429
1430                                                         for (i = MSG_THREAD_LIST_MAX_COUNT-1; i >= 0; i--) {
1431                                                                 if (pData->list_data[i] != NULL) {
1432                                                                         elm_object_item_del(elm_naviframe_top_item_get(pData->navi_frame));
1433                                                                         elm_genlist_clear(pData->list_data[i]->genlist);
1434                                                                         msg_ui_thread_destroy_thread_list_data(pData->list_data[i]);
1435                                                                 }
1436                                                         }
1437
1438                                                         if (pData->split_data)
1439                                                                 msg_ui_thread_destroy_split_data(pData->split_data);
1440                                                 }
1441                                                 evas_object_del(ad->layout_main);
1442                                                 ad->layout_main = NULL;
1443                                         }
1444                                         ug_type = MSG_COMPOSER_UG_TYPE_VIEWER;
1445                                 }
1446                         }
1447                 }
1448
1449                 if (ad->composer_data) {
1450                         ugdata *composer_data = ad->composer_data;
1451
1452                         while (composer_data) {
1453                                 service_h composer_svc_handle = NULL;
1454                                 if (service_create(&composer_svc_handle) < 0 || composer_svc_handle == NULL)
1455                                         D_EMSG("service_create() is failed !!");
1456
1457                                 composer_data = composer_data->next_ug;
1458                         }
1459                         msg_ui_destroy_composer_ug(NULL, true);
1460                 }
1461
1462                 ugdata *composer_data = NULL;
1463
1464                 composer_data = (ugdata*)calloc(1, sizeof(ugdata));
1465                 composer_data->ug_h = ug_create(NULL, MSG_COMPOSER_UG_NAME, UG_MODE_FULLVIEW, svc_handle, &ad->cbs);
1466                 composer_data->ug_type = ug_type;
1467                 ad->composer_data = composer_data;
1468         }
1469
1470         if (svc_handle)
1471                 service_destroy(svc_handle);
1472
1473         if (ad->win_main) {
1474                 evas_object_show(ad->win_main);
1475                 elm_win_activate(ad->win_main);
1476         }
1477
1478         if (ad->layout_main == NULL && ad->composer_data == NULL)
1479                 elm_exit();
1480
1481         D_LEAVE;
1482 }
1483
1484 int main(int argc, char *argv[])
1485 {
1486         D_ENTER;
1487
1488         struct appdata ad;
1489         memset(&ad, 0x0, sizeof(struct appdata));
1490
1491         app_event_callback_s event_callback;
1492
1493         event_callback.create = app_create;
1494         event_callback.terminate = app_terminate;
1495         event_callback.pause = app_pause;
1496         event_callback.resume = app_resume;
1497         event_callback.service = app_service;
1498         event_callback.low_memory = NULL;
1499         event_callback.low_battery = low_battery_cb;
1500         event_callback.language_changed = lang_changed;
1501         event_callback.device_orientation = NULL;
1502         event_callback.region_format_changed = region_changed_cb;
1503
1504         D_LEAVE;
1505         return app_efl_main(&argc, &argv, &event_callback, &ad);
1506 }