56968f69e880e067d9504e35c338ddd5a849c394
[apps/home/smartsearch.git] / src / ps_app_interface.cpp
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://www.tizenopensource.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
19
20
21
22 #include "smartsearch.h"
23 #include "ps_app_interface.h"
24 #include "ps_searcher.h"
25 #include <ui-gadget.h>
26 #include <contacts-ug.h>
27 #include <aul_service.h>
28 #include <appsvc.h>
29 #include <bundle.h>
30
31 #define UG_NAME_LEN 50
32
33 static void __search_layout_ug_destroy(void *data)
34 {
35         SEARCH_FUNC_START;
36
37         struct appdata *ad = (struct appdata *)data;
38
39         //D. Because the ug will be able to remove the base object, unset the content from the effect layout.
40         elm_object_part_content_unset(ad->effect_layout,
41                                  (char *)ug_get_layout(ad->detail_ug));
42
43         evas_object_del(ad->effect_layout);
44
45         ug_destroy(ad->detail_ug);
46
47         ad->detail_ug = NULL;
48         ad->effect_layout = NULL;
49
50         SEARCH_FUNC_END;
51 }
52
53 static void __search_effect_layout_signal_hide_finished(void *data,
54                                                         Evas_Object * obj,
55                                                         const char *emission,
56                                                         const char *source)
57 {
58         SEARCH_FUNC_START;
59
60         __search_layout_ug_destroy(data);
61
62         SEARCH_FUNC_END;
63 }
64
65 static void __search_effect_layout_render_flush_post_cb(void *data, Evas * e,
66                                                         void *event_info)
67 {
68         SEARCH_FUNC_START;
69
70         struct appdata *ad = (struct appdata *)data;
71         if (!ad)
72                 return;
73
74         //UG Show Effect
75         edje_object_signal_emit(elm_layout_edje_get(ad->effect_layout),
76                                 "elm,state,show", "");
77
78         //B.Remove the EVAS_RENDER_FLUSH_POST callback again.
79         evas_event_callback_del(e, EVAS_CALLBACK_RENDER_FLUSH_POST,
80                                 __search_effect_layout_render_flush_post_cb);
81
82         SEARCH_FUNC_END;
83 }
84
85 static void __search_ug_layout_cb(struct ui_gadget *ug, enum ug_mode mode,
86                                   void *priv)
87 {
88         SEARCH_FUNC_START;
89
90         struct appdata *ad = (struct appdata *)priv;
91
92         Evas_Object *base = (Evas_Object *) ug_get_layout(ug);
93
94         if (!base)
95                 return;
96
97 #ifdef ENABLE_UG_EFFECT
98         if (!ad->effect_layout)
99                 return;
100
101         elm_object_part_content_set(ad->effect_layout, "elm.swallow.content", base);
102         //B. Add the callback event for the EVAS_RENDER_FLUSH_POST
103         //B. This is one method to not skip the total effect time in creating the ug object. 
104         evas_event_callback_add(evas_object_evas_get(ad->effect_layout),
105                                 EVAS_CALLBACK_RENDER_FLUSH_POST,
106                                 __search_effect_layout_render_flush_post_cb,
107                                 ad);
108 #else
109         evas_object_show(base);
110 #endif
111
112         SEARCH_FUNC_END;
113 }
114
115 static void __search_ug_closed_cb(struct ui_gadget *ug, void *priv)
116 {
117         SEARCH_FUNC_START;
118
119         struct appdata *ad = (struct appdata *)priv;
120
121 #ifdef ENABLE_UG_EFFECT
122         edje_object_signal_emit(elm_layout_edje_get(ad->effect_layout),
123                                 "elm,state,hide", "");
124 #else
125         Evas_Object *base = (Evas_Object *) ug_get_layout(ug);
126
127         evas_object_hide(base);
128 #endif
129
130         SEARCH_FUNC_END;
131 }
132
133 static void __search_create_effect_layout(Evas_Object * parent,
134                                           struct appdata *ad)
135 {
136         SEARCH_FUNC_START;
137
138         int ret;
139
140         //Create the layout then apply the edj
141         Evas_Object *ly = elm_layout_add(ad->win_main);
142
143         if (!ly)
144                 return;
145
146         evas_object_size_hint_weight_set(ly, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
147
148         elm_win_resize_object_add(ad->win_main, ly);
149
150         ret = elm_layout_file_set(ly, SEARCH_EDJ, "ug_effect");
151
152         //Add a signal callback function which will be called after terminating hide effect.
153         edje_object_signal_callback_add(elm_layout_edje_get(ly),
154                                         "elm,action,hide,finished", "",
155                                         __search_effect_layout_signal_hide_finished,
156                                         ad);
157
158         //store the layout object to refer it at hide effect
159         ad->effect_layout = ly;
160
161         evas_object_show(ly);
162
163         SEARCH_FUNC_END;
164 }
165
166 void search_launch_contact_view(void *data, void *record_info)
167 {
168         SEARCH_FUNC_START;
169
170         struct appdata *ad = (struct appdata *)data;
171         struct search_item_sel *cur_category_mem =
172             (struct search_item_sel *)record_info;
173
174         bundle *kb;
175
176         struct ui_gadget *ug;
177         struct ug_cbs cbs = { 0, };
178
179         char buf[10] = { 0, };
180
181         if (ad->detail_ug) {
182                 SEARCH_DEBUG_WARNING("prev ug is exist. destroy prev ug");
183                 __search_layout_ug_destroy(ad);
184         }       
185
186         kb = bundle_create();
187         if (!kb) {
188                 SEARCH_DEBUG_ERROR("contact bundle_create");
189                 return;
190         }
191
192         snprintf(buf, sizeof(buf), "%d", CT_UG_REQUEST_DETAIL);
193         bundle_add(kb, CT_UG_BUNDLE_TYPE, buf);
194         bundle_add(kb, CT_UG_BUNDLE_ID, cur_category_mem->main_id);
195
196 #ifdef ENABLE_UG_EFFECT
197         __search_create_effect_layout(ad->win_main, ad);
198 #endif
199
200         cbs.layout_cb = __search_ug_layout_cb;
201         cbs.result_cb = NULL;
202         cbs.destroy_cb = __search_ug_closed_cb;
203         cbs.priv = ad;  
204
205         ug = ug_create(NULL, UG_CONTACTS_DETAILS, UG_MODE_FULLVIEW, kb, &cbs);
206         if (!ug) {
207                 SEARCH_DEBUG_ERROR(" ...  ug_create: failed !!!");
208                 search_launch_popup_error(LAUNCH_ERROR_AUL_LAUNCH, ad);
209         } else {
210                 ad->detail_ug = ug;
211         }       
212
213         bundle_free(kb);
214
215         SEARCH_FUNC_END;
216 }
217
218 void search_launch_msg_view(void *data, void *record_info)
219 {
220         SEARCH_FUNC_START;
221
222         struct appdata *ad = (struct appdata *)data;
223         struct search_item_sel *cur_category_mem =
224             (struct search_item_sel *)record_info;
225
226         bundle *b;
227         int ret = 0;
228
229         b = bundle_create();
230         if (!b) {
231                 SEARCH_DEBUG_ERROR("search_launch_msg_view : bundle_create failed");
232                 return;
233         }
234  
235         bundle_add(b, "type", "msg_id");
236         bundle_add(b, "msgId", cur_category_mem->main_id);
237         ret = aul_launch_app("org.tizen.message", b);
238         if( (ret <= 0) && (ret != AUL_R_ETIMEOUT) ){    
239                 SEARCH_DEBUG_ERROR("msg aul launch app error(%d)", ret);
240                 search_launch_popup_error(LAUNCH_ERROR_AUL_LAUNCH, ad);
241         }
242
243         bundle_free(b); 
244
245         SEARCH_FUNC_END;
246 }
247
248 void search_launch_email_view(void *data, void *record_info)
249 {
250         SEARCH_FUNC_START;
251
252         bundle *b;
253         char *token_param = NULL;
254         char *saveptr1;
255         int i = 0;
256         int ret = 0;
257         struct appdata *ad = (struct appdata *)data;
258         struct search_item_sel *cur_category_mem =
259             (struct search_item_sel *)record_info;
260
261         b = bundle_create();
262         if (!b)
263                 return;
264
265         char livemagazine[2] = { 0, };
266         char temp_path[MAX_LENGTH_PER_PATH] = { 0, };
267         livemagazine[0] = '0';
268
269         snprintf(temp_path, MAX_LENGTH_PER_PATH - 1, "%s",
270                  cur_category_mem->path);
271
272         token_param = strtok_r(temp_path, " ", &saveptr1);
273
274         while (token_param != NULL) {
275                 if (i == 0) {
276                         bundle_add(b, "ACCOUNT_ID", token_param);       //account_id
277                 } else if (i == 1) {
278                         bundle_add(b, "MAIL_ID", token_param);  //mail_id
279                 } else if (i == 2) {
280                         bundle_add(b, "MAILBOX", token_param);  //mailbox
281                 } else {
282                         break;
283                 }
284
285                 i++;
286                 token_param = strtok_r(NULL, " ", &saveptr1);
287         }
288
289         bundle_add(b, "bLiveMagazine", livemagazine);
290
291         char runType[2] = { 0 };
292         snprintf(runType, sizeof(runType), "%d", 7);
293         bundle_add(b, "RUN_TYPE", runType);
294         ret = aul_launch_app("org.tizen.email", b);
295         if( (ret <= 0) && (ret != AUL_R_ETIMEOUT) ){
296                 SEARCH_DEBUG_ERROR("email aul launch app error(%d)", ret);
297                 search_launch_popup_error(LAUNCH_ERROR_AUL_LAUNCH, ad);
298         }
299
300         bundle_free(b);
301
302         SEARCH_FUNC_END;
303 }
304
305 void search_launch_image_view(void *data, void *record_info)
306 {
307         SEARCH_FUNC_START;
308
309         struct appdata *ad = (struct appdata *)data;
310         struct search_item_sel *cur_category_mem =
311             (struct search_item_sel *)record_info;
312         int ret;
313
314                 if (access(cur_category_mem->path, F_OK) != 0) {
315                         SEARCH_DEBUG_ERROR("image access error : %s",
316                                            cur_category_mem->path);
317                         search_launch_popup_error(LAUNCH_ERROR_FILE_NOT_FOUND,
318                                                   ad);
319                 } else {
320                 SEARCH_DEBUG_LOG("cur_category_mem->path : %s",
321                                      cur_category_mem->path);
322
323                 bundle* bd = NULL;
324                 bd = bundle_create();
325
326                 if (bd != NULL) {
327                         appsvc_set_operation(bd, APPSVC_OPERATION_VIEW);
328                         appsvc_set_uri(bd, cur_category_mem->path);
329                         ret = appsvc_run_service(bd, 0, NULL, NULL);
330
331                         if (ret < 0 )
332                                 search_launch_popup_error(LAUNCH_ERROR_APPSVC,
333                                                           ad);  
334                         bundle_free(bd);        
335                         }
336
337                 }
338
339         SEARCH_FUNC_END;
340 }
341
342 void search_launch_video_view(void *data, void *record_info)
343 {
344         SEARCH_FUNC_START;
345
346         struct appdata *ad = (struct appdata *)data;
347         struct search_item_sel *cur_category_mem =
348             (struct search_item_sel *)record_info;
349         int ret;
350
351         if (access(cur_category_mem->path, F_OK) != 0) {
352                 SEARCH_DEBUG_ERROR("video access error : %s",
353                                    cur_category_mem->path);
354                 search_launch_popup_error(LAUNCH_ERROR_FILE_NOT_FOUND, ad);
355         } else {
356                 bundle* bd = NULL;
357                 bd = bundle_create();
358                 
359                 appsvc_set_operation(bd, APPSVC_OPERATION_VIEW);
360                 appsvc_set_uri(bd, cur_category_mem->path);
361                 ret = appsvc_run_service(bd, 0, NULL, NULL);
362                 
363                 if (ret < 0) {  
364                         SEARCH_DEBUG_ERROR("video app service error(%d) : %s",
365                                            ret, cur_category_mem->path);
366                         search_launch_popup_error(LAUNCH_ERROR_APPSVC, ad);
367                 }
368
369                 bundle_free(bd);        
370         }
371
372         SEARCH_FUNC_END;
373 }
374
375 void search_launch_music_view(void *data, void *record_info)
376 {
377         SEARCH_FUNC_START;
378
379         struct appdata *ad = (struct appdata *)data;
380         struct search_item_sel *cur_category_mem =
381             (struct search_item_sel *)record_info;
382         int ret;
383
384         if (access(cur_category_mem->path, F_OK) != 0) {
385                 SEARCH_DEBUG_ERROR("music file access error : %s",
386                                    cur_category_mem->path);
387                 search_launch_popup_error(LAUNCH_ERROR_FILE_NOT_FOUND, ad);
388         } else {
389                 SEARCH_DEBUG_LOG("cur_category_mem->path : %s",
390                                  cur_category_mem->path);
391                 
392                 bundle* bd = NULL;
393                 bd = bundle_create();
394
395                 if (bd != NULL) {
396                         appsvc_set_operation(bd, APPSVC_OPERATION_VIEW);
397                         appsvc_set_uri(bd, cur_category_mem->path);
398                         ret = appsvc_run_service(bd, 0, NULL, NULL);
399
400                         if (ret < 0) {
401                                 SEARCH_DEBUG_ERROR("music app service error(%d) : %s",
402                                                    ret, cur_category_mem->path);                                
403                                 search_launch_popup_error(LAUNCH_ERROR_APPSVC,
404                                                                 ad);
405                         }
406                         bundle_free(bd);        
407                 }
408         }
409
410         SEARCH_FUNC_END;
411 }
412
413 void search_launch_calendar_view(void *data, void *record_info)
414 {
415         SEARCH_FUNC_START;
416
417         struct appdata *ad = (struct appdata *)data;
418         struct search_item_sel *cur_category_mem =
419             (struct search_item_sel *)record_info;
420         struct ui_gadget *ug;
421         struct ug_cbs cbs = { 0, };
422
423         bundle *b;
424         int ret;
425
426         if (ad->detail_ug) {
427                 SEARCH_DEBUG_WARNING("prev ug is exist. destroy prev ug");
428
429                 __search_layout_ug_destroy(ad);
430         }
431
432         b = bundle_create();
433         if (!b)
434                 return;
435
436         bundle_add(b, AUL_K_SERVICE_NAME, "view_event");
437         bundle_add(b, "index", cur_category_mem->main_id);
438
439         SEARCH_DEBUG_LOG("index : %s", cur_category_mem->main_id);
440
441         ret = aul_launch_app("org.tizen.efl-calendar", b);
442         if( (ret <= 0) && (ret != AUL_R_ETIMEOUT) ){    
443                 SEARCH_DEBUG_ERROR("calendar aul launch app error(%d)", ret);
444                 search_launch_popup_error(LAUNCH_ERROR_AUL_LAUNCH, ad);
445         }
446
447         bundle_free(b); 
448         
449         SEARCH_FUNC_END;
450 }
451
452 void search_launch_memo_view(void *data, void *record_info)
453 {
454         SEARCH_FUNC_START;
455
456         struct appdata *ad = (struct appdata *)data;
457         struct search_item_sel *cur_category_mem =
458             (struct search_item_sel *)record_info;
459         struct ui_gadget *ug;
460         struct ug_cbs cbs = { 0, };
461
462         bundle *b;      
463
464         if (ad->detail_ug) {
465                 SEARCH_DEBUG_WARNING("prev ug is exist. destroy prev ug");
466
467                 __search_layout_ug_destroy(ad);
468         }
469
470         b = bundle_create();
471         if (!b)
472                 return;
473
474         bundle_add(b, "index", cur_category_mem->main_id);
475
476         __search_create_effect_layout(ad->win_main, ad);
477
478         cbs.layout_cb = __search_ug_layout_cb;
479         cbs.result_cb = NULL;
480         cbs.destroy_cb = __search_ug_closed_cb;
481         cbs.priv = ad;
482
483         ug = ug_create(NULL, "memo-efl", UG_MODE_FULLVIEW, b, &cbs);
484         if (!ug) {
485                 SEARCH_DEBUG_ERROR("...  create_ug_email : failed !!!");
486                 search_launch_popup_error(LAUNCH_ERROR_AUL_LAUNCH, ad);
487         }
488
489         ad->detail_ug = ug;
490
491         bundle_free(b);
492
493         SEARCH_FUNC_END;
494 }
495
496 void search_launch_menu_view(void *data, void *record_info)
497 {
498         SEARCH_FUNC_START;
499
500         struct appdata *ad = (struct appdata *)data;
501         struct search_item_sel *cur_category_mem =
502             (struct search_item_sel *)record_info;
503         int ret;
504
505         ret = aul_launch_app(cur_category_mem->path, NULL);
506
507         SEARCH_DEBUG_WARNING("path : %s, ret : %d", cur_category_mem->path, ret);
508         
509         if( (ret <= 0) && (ret != AUL_R_ETIMEOUT) ){    
510                 SEARCH_DEBUG_ERROR("menu aul launch app error(%d) : %s", ret,
511                                    cur_category_mem->path);
512                 search_launch_popup_error(LAUNCH_ERROR_AUL_LAUNCH, ad);
513         }
514
515         SEARCH_FUNC_END;
516 }
517
518 static void __search_launch_popup_error_response_cb(void *data,
519                                                     Evas_Object * obj,
520                                                     void *event_info)
521 {
522         SEARCH_FUNC_START;
523
524         struct appdata *ad = (struct appdata *)data;
525
526         evas_object_del(ad->ctxpopup);
527         ad->ctxpopup = NULL;
528
529         SEARCH_FUNC_END;
530 }
531
532 void search_launch_popup_error(int error_type, void *data)
533 {
534         Evas_Object *popup;
535         Evas_Object *btn1;
536
537         struct appdata *ad = (struct appdata *)data;
538
539         if (error_type >= LAUNCH_ERROR_MAX) {
540                 SEARCH_DEBUG_ERROR("wrong error_type : %d", error_type);
541         }
542
543         if (ad->ctxpopup) {
544                 evas_object_del(ad->ctxpopup);
545                 ad->ctxpopup = NULL;
546         }
547
548         popup = elm_popup_add(ad->win_main);
549         evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND,
550                                          EVAS_HINT_EXPAND);
551
552         switch(error_type) {
553                 case LAUNCH_ERROR_FILE_NOT_FOUND:
554                         elm_object_text_set(popup,
555                                    dgettext("sys_string",
556                                             "IDS_COM_POP_FILE_NOT_FOUND"));
557                         break;
558                 case LAUNCH_ERROR_AUL_LAUNCH:
559                         /* This Message String enable to be another string in the future */
560                         elm_object_text_set(popup,
561                                            dgettext("sys_string",
562                                                     "IDS_COM_POP_SERVICE_UNAVAILABLE"));
563                         break;
564                 case LAUNCH_ERROR_APPSVC:
565                         elm_object_text_set(popup,
566                                    dgettext("sys_string",
567                                             "IDS_COM_POP_SERVICE_UNAVAILABLE"));
568                         break;
569         }
570
571         btn1 = elm_button_add(popup);
572         elm_object_text_set(btn1,
573                                 dgettext("sys_string","IDS_COM_SK_OK"));
574         evas_object_smart_callback_add(btn1, "clicked",
575                                        __search_launch_popup_error_response_cb,
576                                        ad);
577
578         elm_object_part_content_set(popup, "button1", btn1);
579
580         elm_popup_timeout_set(popup, 3.0);
581
582         evas_object_smart_callback_add(popup, "timeout",
583                                        __search_launch_popup_error_response_cb,
584                                        ad);
585         evas_object_smart_callback_add(popup, "block,clicked",
586                                        __search_launch_popup_error_response_cb,
587                                        ad);
588         evas_object_show(popup);
589
590         ad->ctxpopup = popup;
591 }
592