Apply changed directory policy
[apps/core/preloaded/smartsearch.git] / src / ps_makeupviews.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 #include "smartsearch.h"
19 #include "ps_searcher.h"
20 #include "ps_app_interface.h"
21 #include <contacts-svc.h>
22 #include <calendar-svc-provider.h>
23 #include <memo-db.h>
24 #include <media_content.h>
25 #include <msg_types.h>
26 #include <msg_storage.h>
27 #include <msg.h>
28
29 struct search_thread_data {
30         struct appdata *ad;
31         char *keyword;
32         int is_search_result;
33 };
34
35 struct search_genlist_item_info* search_add_genlist_item(int type,
36                 struct search_content_object *obj, Elm_Genlist_Item_Class *itc_style, void *data,
37                 Elm_Object_Item *parent)
38 {
39         struct search_genlist_item_info *item = (struct search_genlist_item_info *)
40                                                                                         calloc(1,
41                                                                                         sizeof(struct search_genlist_item_info));
42
43         if (item) {
44                 item->type = type;
45                 item->obj = obj;
46                 item->itc_style = itc_style;
47                 item->data = data;
48                 item->parent = parent;
49         }
50
51         return item;
52 }
53
54 static void
55 ecore_exe_pipe_cb(void *data, void *buf, unsigned int size)
56 {
57         SEARCH_FUNC_START;
58
59
60         struct appdata *ad = (struct appdata *)data;
61         struct search_pipe_data *pipe_data = *((struct search_pipe_data**)buf);
62
63         struct search_content_object *obj = NULL;
64         struct search_genlist_item_info *item = NULL;
65         struct search_genlist_info *result_info = pipe_data->result_list;
66
67         int cmd = pipe_data->cmd;
68
69         SEARCH_DEBUG_LOG("Pipe Command : %d ret : %d", cmd, pipe_data->search_ret);
70
71         switch (cmd) {
72         case SEARCH_PIPE_CMD_CLEAR_GENLIST:
73                 elm_genlist_clear(ad->search_gl);
74                 break;
75         case SEARCH_PIPE_CMD_SET_LAYOUT:
76                 if (pipe_data->search_ret == SEARCH_RET_SEARCH_SUCCESS)
77                         search_layout_set_search_list(ad);
78                 else
79                         search_layout_set_search_no_result(ad);
80                 break;
81         case SEARCH_PIPE_CMD_ADD_GENLIST_PHONE:
82                 if (result_info) {
83                         if (result_info->group) {
84                                 item = result_info->group;
85                                 obj = item->obj;
86
87                                 obj->gl_result_grp = elm_genlist_item_append(ad->search_gl,
88                                                                         item->itc_style,
89                                                                         item->data,
90                                                                         NULL,
91                                                                         ELM_GENLIST_ITEM_GROUP,
92                                                                         NULL,
93                                                                         NULL);
94                                 elm_genlist_item_select_mode_set(obj->gl_result_grp,
95                                         ELM_OBJECT_SELECT_MODE_NONE);
96                                 SEARCH_FREE(item);
97                         }
98
99                         if (result_info->itemlist) {
100                                 GList *node = g_list_first(result_info->itemlist);
101
102                                 while (node != NULL) {
103                                         item = (struct search_genlist_item_info *)node->data;
104                                         obj = item->obj;
105                                         if (item) {
106                                                 elm_genlist_item_append(ad->search_gl,
107                                                                                         item->itc_style,
108                                                                                         item->data,
109                                                                                         obj->gl_result_grp,
110                                                                                         ELM_GENLIST_ITEM_NONE,
111                                                                                         search_gl_phone_result_sel,
112                                                                                         ad);
113                                                 SEARCH_FREE(item);
114                                         }
115                                         node = g_list_next(node);
116                                 }
117                                 g_list_free(result_info->itemlist);
118                                 result_info->itemlist = NULL;
119                         }
120
121                         if (result_info->more) {
122                                 item = result_info->more;
123                                 obj = item->obj;
124                                 elm_genlist_item_append(ad->search_gl,
125                                                                 item->itc_style,
126                                                                 item->data,
127                                                                 obj->gl_result_grp,
128                                                                 ELM_GENLIST_ITEM_NONE,
129                                                                 search_gl_result_more_item_append_cb,
130                                                                 ad);
131                                 SEARCH_FREE(item);
132                         }
133
134                         SEARCH_FREE(result_info);
135                 }
136                 break;
137                 }
138
139         SEARCH_FREE(pipe_data);
140         SEARCH_FUNC_END;
141 }
142
143 static Evas_Object *__search_gl_content_get_search_item_type(void *data, Evas_Object *obj, const char *part)
144                 {
145         Evas_Object *icon = NULL;
146         int icon_size = 0;
147
148         struct search_item_sel *sel_mem_info = (struct search_item_sel *)data;
149
150         if (!SEARCH_STRCMP(part, "elm.icon") || !SEARCH_STRCMP(part, "elm.icon.1")) {
151                 if (strlen(sel_mem_info->icon1_path) > 0 ) {
152                         icon = elm_icon_add(obj);
153                         icon_size = (int)(SEARCH_THUMBNAIL_SIZE * elm_scale_get());
154                         elm_icon_prescale_set(icon, icon_size);
155                         elm_icon_file_set(icon, sel_mem_info->icon1_path, NULL);
156                         evas_object_size_hint_min_set(icon,
157                                 SEARCH_THUMBNAIL_SIZE * elm_scale_get(),
158                                 SEARCH_THUMBNAIL_SIZE * elm_scale_get());
159                         evas_object_size_hint_max_set(icon,
160                                 SEARCH_THUMBNAIL_SIZE * elm_scale_get(),
161                                 SEARCH_THUMBNAIL_SIZE * elm_scale_get());
162                         evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
163                 }
164         } else if(!strcmp(part, "elm.icon.2")) {
165                 if (strlen(sel_mem_info->icon2_path) > 0 ) {
166                         icon = elm_icon_add(obj);
167                         icon_size = (int)(SEARCH_ICON_SIZE * elm_scale_get());
168                         elm_icon_prescale_set(icon, icon_size);
169                         elm_icon_file_set(icon, sel_mem_info->icon2_path, NULL);
170                         evas_object_size_hint_min_set(icon,
171                                 SEARCH_ICON_SIZE * elm_scale_get(),
172                                 SEARCH_ICON_SIZE * elm_scale_get());
173                         evas_object_size_hint_max_set(icon,
174                                 SEARCH_ICON_SIZE * elm_scale_get(),
175                                 SEARCH_ICON_SIZE * elm_scale_get());
176                         evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
177                 }
178                 }
179         return icon;
180                 }
181
182 static Eina_Bool __search_gl_state_get(void *data, Evas_Object * obj,
183                                        const char *part)
184 {
185         return EINA_FALSE;
186 }
187
188 static char *__search_gl_label_get_search_item_type(void *data, Evas_Object * obj,
189                                    const char *part)
190 {
191         if (data == NULL) {
192                 return g_strdup("");
193         }
194
195         char *buf;
196         struct search_item_sel *sel_mem_info = (struct search_item_sel *)data;
197
198         if (!SEARCH_STRCMP(part, "elm.text") || !SEARCH_STRCMP(part, "elm.text.1")) {
199                 buf = g_strdup(sel_mem_info->main_buf);
200         } else if (!SEARCH_STRCMP(part, "elm.text.sub") || !SEARCH_STRCMP(part, "elm.text.2")) {
201                 buf = g_strdup(sel_mem_info->sub_buf);
202         } else {
203                 buf = NULL;
204         }
205
206         return buf;
207 }
208
209 static char *__search_gl_label_get_normal_type(void *data, Evas_Object * obj,
210                                          const char *part)
211 {
212         char *title_string = (char*)data;
213         if(title_string)
214                 return strdup(title_string);
215         else
216                 return strdup("");
217 }
218
219 static char *__search_gl_label_get_group(void *data, Evas_Object * obj,
220                                          const char *part)
221 {
222         struct search_content_object *object = (struct search_content_object*)data;
223         SEARCH_DEBUG_WARNING("%s %d", object->name, object->string_type);
224         if (object) {
225                 if (object->string_type == SEARCH_STR_TYPE_APP_STRING)
226                         return strdup(gettext(object->name));
227                 else
228                         return strdup(dgettext("sys_string", object->name));
229         }
230
231                 return strdup("");
232 }
233
234 static char *__search_gl_label_get_more(void *data, Evas_Object * obj,
235                                         const char *part)
236 {
237         return strdup(gettext("IDS_SSEARCH_BODY_MORE_RESULTS"));
238 }
239
240 static void __search_gl_del_grouptitle(void *data, Evas_Object * obj)
241 {
242         SEARCH_FUNC_START;
243
244         SEARCH_FUNC_END;
245         return;
246 }
247
248 static void __search_gl_del(void *data, Evas_Object * obj)
249 {
250         struct search_item_sel *sel_mem_info = (struct search_item_sel *)data;
251
252         SEARCH_FREE(sel_mem_info);
253
254         return;
255 }
256
257 static void __search_gl_del_more_button(void *data, Evas_Object * obj)
258 {
259         struct search_item_more_sel *sel_mem_more = (struct search_item_more_sel *)data;
260
261         SEARCH_FREE(sel_mem_more);
262
263         return;
264 }
265
266 int search_app_connect(void *data)
267 {
268         SEARCH_FUNC_START;
269
270         struct appdata *ad = (struct appdata *)data;
271
272         int rc = 0, conn_ret = 0;
273         int ret = SEARCH_RET_SUCCESS;
274         int i = 0;
275
276         for (i = 0; i < SEARCH_CONT_MAX ; ++i) {
277                 switch (ad->result_obj[i].contents_type) {
278                         case SEARCH_CONT_PHONE_CONTACTS:
279                                 {
280                                         if (ad->result_obj[i].visible == EINA_TRUE) {
281                                                 rc = contacts_svc_connect();
282                                                 if (rc != CTS_SUCCESS) {
283                                                         SEARCH_DEBUG_WARNING("contacts svc connect fail : %d",rc);
284                                                 }
285                                         } else {
286                                                 rc = contacts_svc_disconnect();
287                                                 if (rc != CTS_SUCCESS) {
288                                                         SEARCH_DEBUG_WARNING("contacts svc disconnect fail : %d",rc);
289                                                 }
290                                         }
291                                 }
292                                 break;
293
294                         /* Contact-service should be conneted for display Message result.
295                            Because We should display message with contact photo
296                            if user have contact photo which have matched to tel-number of sender or receiver */
297                         case SEARCH_CONT_PHONE_MSG:
298                                 {
299                                         msg_error_t err = MSG_SUCCESS;
300
301                                         if (ad->result_obj[i].visible == EINA_TRUE) {
302
303                                                 /* Connect Contact-service if contact category status is invisible */
304                                                 if (ad->result_obj[SEARCH_CONT_PHONE_CONTACTS].visible == EINA_FALSE) {
305                                                         rc = contacts_svc_connect();
306                                                         if (rc != CTS_SUCCESS) {
307                                                                 /* If we fail to connect contact-service, display message-app icon */
308                                                                 SEARCH_DEBUG_WARNING("contacts svc connect fail : %d",rc);
309                                                         }
310                                                 }
311
312                                                 /* Connect Message-Service */
313                                                 err = msg_open_msg_handle(&ad->msg_handle);
314
315                                                 if (err != MSG_SUCCESS) {
316                                                         SEARCH_DEBUG_WARNING("msg_open_msg_handle error : %d", err);
317                                                         ad->msg_handle = NULL;
318                                                 }
319                                         } else {
320                                                 /* Disconnect Contact-service if contact category status is invisible */
321                                                 if (ad->result_obj[SEARCH_CONT_PHONE_CONTACTS].visible == EINA_FALSE) {
322                                                         rc = contacts_svc_disconnect();
323                                                         if (rc != CTS_SUCCESS)
324                                                                 SEARCH_DEBUG_WARNING("contacts svc connect fail : %d",rc);
325                                                 }
326
327                                                 /* Disconnect Message-Service */
328                                                 if (ad->msg_handle != NULL ) {
329                                                         err = msg_close_msg_handle(&ad->msg_handle);
330
331                                                         if (err != MSG_SUCCESS)
332                                                                 SEARCH_DEBUG_WARNING("msg_close_msg_handle error : %d", err);
333                                                         ad->msg_handle = NULL;
334                                                 }
335                                         }
336                                 }
337                                 break;
338                         case SEARCH_CONT_PHONE_EMAIL:
339                                 break;
340                         /* IMAGE, MUSIC, VIDEO use common handle */
341                         case SEARCH_CONT_PHONE_IMAGES:
342                         case SEARCH_CONT_PHONE_MUSIC:
343                         case SEARCH_CONT_PHONE_VIDEO:
344                                 {
345                                         if (ad->result_obj[SEARCH_CONT_PHONE_IMAGES].visible == EINA_TRUE
346                                                 || ad->result_obj[SEARCH_CONT_PHONE_MUSIC].visible == EINA_TRUE
347                                                 || ad->result_obj[SEARCH_CONT_PHONE_VIDEO].visible == EINA_TRUE ) {
348                                                 conn_ret = media_content_connect();
349
350                                                 if (conn_ret != 0)
351                                                         SEARCH_DEBUG_WARNING("media_content_connect failed : %d\n", conn_ret);
352                                         } else {
353                                                         conn_ret = media_content_disconnect();
354
355                                                         if(conn_ret < 0)
356                                                                 SEARCH_DEBUG_WARNING("media_content_disconnect failed : %d\n", conn_ret);
357                                         }
358                                 }
359                                 break;
360                         case SEARCH_CONT_PHONE_CALENDAR:
361                                 {
362                                         if (ad->result_obj[i].visible == EINA_TRUE) {
363                                                 rc = calendar_svc_connect();
364                                                 if (rc != CAL_SUCCESS) {
365                                                         SEARCH_DEBUG_WARNING
366                                                             ("calendar svc connect fail : %d",
367                                                              rc);
368                                                 }
369                                         } else {
370                                                 rc = calendar_svc_close();
371                                                 if (rc != CAL_SUCCESS) {
372                                                         SEARCH_DEBUG_WARNING
373                                                             ("calendar svc disconnect fail : %d",
374                                                              rc);
375                                                 }
376                                         }
377                                 }
378                                 break;
379                         case SEARCH_CONT_PHONE_MEMO:
380                                 {
381                                         if (ad->result_obj[i].visible == EINA_TRUE) {
382                                                 rc = memo_init(NULL);
383                                                 if (rc == -1) {
384                                                         SEARCH_DEBUG_WARNING
385                                                             ("memo svc connect fail : %d", rc);
386                                                 }
387                                         } else
388                                                 memo_fini();
389                                 }
390                                 break;
391                         }
392         }
393
394         SEARCH_FUNC_END;
395
396         return ret;
397 }
398
399 int search_app_disconnect(void *data)
400 {
401         SEARCH_FUNC_START;
402
403         struct appdata *ad = (struct appdata *)data;
404
405         int rc = 0, conn_ret = 0;
406         int i = 0;
407
408         for (i = 0; i < SEARCH_CONT_MAX ; ++i) {
409                 switch (ad->result_obj[i].contents_type) {
410                         case SEARCH_CONT_PHONE_CONTACTS:
411                                 {
412                                         rc = contacts_svc_disconnect();
413                                         if (rc != CTS_SUCCESS) {
414                                                 SEARCH_DEBUG_WARNING
415                                                     ("contacts svc disconnect fail : %d",
416                                                      rc);
417                                         }
418                                 }
419                                 break;
420                         case SEARCH_CONT_PHONE_MSG:
421                                 {
422                                         if (ad->msg_handle != NULL ) {
423                                                 msg_error_t err = MSG_SUCCESS;
424                                                 err = msg_close_msg_handle(&ad->msg_handle);
425
426                                                 if (err != MSG_SUCCESS) {
427                                                         SEARCH_DEBUG_WARNING("msg_close_msg_handle error : %d", err);
428                                                 }
429
430                                                 ad->msg_handle = NULL;
431                                         }
432                                 }
433                                 break;
434                         case SEARCH_CONT_PHONE_EMAIL:
435                                 break;
436                         case SEARCH_CONT_PHONE_IMAGES:
437                         case SEARCH_CONT_PHONE_MUSIC:
438                         case SEARCH_CONT_PHONE_VIDEO:
439                                 {
440                                         conn_ret = media_content_disconnect();
441                                         if(conn_ret != 0)
442                                                 SEARCH_DEBUG_WARNING("media_content_disconnect failed : %d\n", conn_ret);
443                                 }
444                                 break;
445                         case SEARCH_CONT_PHONE_CALENDAR:
446                                 {
447                                         rc = calendar_svc_close();
448                                         if (rc != CAL_SUCCESS) {
449                                                 SEARCH_DEBUG_WARNING
450                                                     ("calendar svc disconnect fail : %d",
451                                                      rc);
452                                         }
453                                 }
454                                 break;
455                         case SEARCH_CONT_PHONE_MEMO:
456                                 {
457                                         memo_fini();
458                                 }
459                                 break;
460                 }
461         }
462
463         SEARCH_FUNC_END;
464
465         return 0;
466 }
467
468 void search_gl_phone_result_sel(void *data, Evas_Object * obj, void *event_info)
469 {
470         SEARCH_FUNC_START;
471
472         struct appdata *ad = (struct appdata *)data;
473         struct search_item_sel *cur_category_mem;
474         Elm_Object_Item *it = (Elm_Object_Item *) event_info;
475
476         cur_category_mem =
477             (struct search_item_sel *)elm_object_item_data_get(it);
478         if (cur_category_mem == NULL) {
479                 return;
480         }
481
482         SEARCH_DEBUG_LOG("cur_category_mem->type : %d", cur_category_mem->type);
483
484         switch (cur_category_mem->type) {
485         case SEARCH_CONT_PHONE_CONTACTS:
486                 search_launch_contact_view(ad, cur_category_mem);
487                 break;
488
489         case SEARCH_CONT_PHONE_MSG:
490                 search_launch_msg_view(ad, cur_category_mem);
491                 break;
492
493         case SEARCH_CONT_PHONE_EMAIL:
494                 search_launch_email_view(ad, cur_category_mem);
495                 break;
496
497         case SEARCH_CONT_PHONE_IMAGES:
498                 search_launch_image_view(ad, cur_category_mem);
499                 break;
500
501         case SEARCH_CONT_PHONE_VIDEO:
502                 search_launch_video_view(ad, cur_category_mem);
503                 break;
504
505         case SEARCH_CONT_PHONE_MUSIC:
506                 search_launch_music_view(ad, cur_category_mem);
507                 break;
508
509         case SEARCH_CONT_PHONE_CALENDAR:
510                 search_launch_calendar_view(ad, cur_category_mem);
511                 break;
512
513         case SEARCH_CONT_PHONE_MEMO:
514                 search_launch_memo_view(ad, cur_category_mem);
515                 break;
516
517         case SEARCH_CONT_PHONE_MENU:
518                 search_launch_menu_view(ad, cur_category_mem);
519                 break;
520
521         case SEARCH_CONT_PHONE_BROWSER:
522                 search_launch_browser_view(ad, cur_category_mem);
523
524         default:
525                 break;
526         }
527
528         elm_genlist_item_selected_set(it, EINA_FALSE);
529
530         SEARCH_FUNC_END;
531 }
532
533 static int __search_gl_set_phone_search_result(struct search_content_object *obj,
534                                                                                                         int offset,
535                                                                                                         char *keyword,
536                                                                                                         void *data,
537                                                                                                         struct search_genlist_info *result_info)
538 {
539         SEARCH_FUNC_START;
540
541         int sel_cnt = SEARCH_CATEGORY_LIST_MORE_CNT + 1;
542         int func_ret = SEARCH_RET_SEARCH_NONE;
543
544         struct appdata *ad = (struct appdata *)data;
545
546         obj->mr_info.offset = offset;
547
548         if (obj->mr_info.offset == 0) {
549                 obj->mr_info.next_point = 0;
550         }
551
552         SEARCH_DEBUG_LOG("offset : %d, limit : %d keyword : %s", offset, sel_cnt, keyword);
553
554         if (obj->visible == EINA_TRUE ) {
555                 switch (obj->contents_type) {
556                         case SEARCH_CONT_PHONE_CONTACTS:
557                 {
558                         func_ret =
559                                             search_sql_contact_search_result_by_api(obj,
560                                                                     offset,
561                                                                     sel_cnt,
562                                                             keyword,
563                                                             ad,
564                                                             result_info);
565                 }
566                 break;
567
568                         case SEARCH_CONT_PHONE_MSG:
569                 {
570                         if (ad->msg_handle != NULL) {
571                         func_ret =
572                                                     search_sql_msg_search_result_by_api(obj,
573                                                                 offset,
574                                                                         sel_cnt,
575                                                             keyword,
576                                                             ad,
577                                                             result_info);
578                 }
579                 }
580                 break;
581
582                         case SEARCH_CONT_PHONE_EMAIL:
583                 {
584                         func_ret =
585                                             search_sql_email_search_result_by_api(obj,
586                                                                 offset,
587                                                                         sel_cnt,
588                                                     keyword,
589                                                     ad,
590                                                     result_info);
591                 }
592                 break;
593                         case SEARCH_CONT_PHONE_IMAGES:
594                 {
595                         func_ret =
596                                             search_sql_image_search_result_by_api(obj,
597                                                                 offset,
598                                                                         sel_cnt,
599                                                     keyword,
600                                                     ad,
601                                                     result_info);
602                 }
603                         break;
604                         case SEARCH_CONT_PHONE_MUSIC:
605                         {
606                                         func_ret =
607                                             search_sql_music_search_result_by_api(obj,
608                                                 offset,
609                                                     sel_cnt,
610                                                     keyword,
611                                                     ad,
612                                                     result_info);
613                 }
614                 break;
615                         case SEARCH_CONT_PHONE_VIDEO:
616                 {
617                         func_ret =
618                                             search_sql_video_search_result_by_api(obj,
619                                                                 offset,
620                                                                         sel_cnt,
621                                                     keyword,
622                                                     ad,
623                                                     result_info);
624                 }
625                 break;
626                         case SEARCH_CONT_PHONE_CALENDAR:
627                 {
628                         func_ret =
629                                             search_sql_calendar_search_result_by_api(obj,
630                                                                 offset,
631                                                                         sel_cnt,
632                                                     keyword,
633                                                     ad,
634                                                     result_info);
635                 }
636                 break;
637                         case SEARCH_CONT_PHONE_MEMO:
638                 {
639                         func_ret =
640                                             search_sql_memo_search_result_by_api(obj,
641                                                                         offset,
642                                                                         sel_cnt,
643                                                     keyword,
644                                                     ad,
645                                                     result_info);
646                 }
647                 break;
648                         case SEARCH_CONT_PHONE_MENU:
649                 {
650                         func_ret =
651                                             search_sql_menu_search_result_by_api(obj,
652                                                                 offset,
653                                                                         sel_cnt,
654                                                     keyword,
655                                                     ad,
656                                                     result_info);
657                 }
658                 break;
659                         case SEARCH_CONT_PHONE_BROWSER:
660                 {
661                         func_ret =
662                                                 search_sql_browser_search_result_by_api(obj,
663                                                                         offset,
664                                                                         sel_cnt,
665                                                                         keyword,
666                                                     ad,
667                                                     result_info);
668                 }
669         }
670         }
671
672         SEARCH_FUNC_END;
673         return func_ret;
674 }
675
676 static void __search_thread_heavy_cb(void *data, Ecore_Thread *th)
677 {
678         SEARCH_FUNC_START;
679
680         struct search_thread_data *thread_data = (struct search_thread_data*)data;
681         struct appdata *ad = thread_data->ad;
682         struct search_content_object *obj = NULL;
683         struct search_genlist_info *result_info = NULL;
684
685         int ret = 0;
686
687         int i = SEARCH_CONT_PHONE_MIN;
688         int category_type = 0;
689
690         SEARCH_DEBUG_LOG("Thread ID : %p Keyword : %s", th, thread_data->keyword);
691
692         thread_data->ad = ad;
693         thread_data->is_search_result = SEARCH_RET_SEARCH_NONE;
694
695         while (i <= SEARCH_CONT_PHONE_MAX) {
696                 int offset = 0;
697                 obj = &ad->result_obj[i];
698                 category_type = obj->category_type;
699
700                 if (category_type == SEARCH_OBJ_PHONE_CONTENTS) {
701                         if (ecore_thread_check(th) != EINA_TRUE) {
702                                 result_info = (struct search_genlist_info *)calloc(1, sizeof(struct search_genlist_info));
703                                 ret = __search_gl_set_phone_search_result(obj,
704                                                                 offset,
705                                                                 thread_data->keyword,
706                                                                 ad,
707                                                                 result_info);
708
709                                 if (ret == SEARCH_RET_SEARCH_SUCCESS) {
710                                         SEARCH_DEBUG_LOG("Succes to Matched result : %s %d", obj->name, ret);
711                                         thread_data->is_search_result = SEARCH_RET_SEARCH_SUCCESS;
712                                         ecore_thread_feedback(th, result_info);
713                                 } else {
714                                         SEARCH_FREE(result_info);
715                                 }
716                         }
717                 }
718                 i++;
719         }
720         SEARCH_FUNC_END;
721 }
722
723
724 static void __search_thread_feedback_cb(void *data, Ecore_Thread *th, void *msg_data)
725 {
726         SEARCH_FUNC_START;
727
728         struct search_thread_data *thread_data = (struct search_thread_data *)data;
729         struct appdata *ad = thread_data->ad;
730         struct search_genlist_info *result_list = (struct search_genlist_info *)msg_data;
731         struct search_pipe_data *pdata = NULL;
732
733         pdata = (struct search_pipe_data *)calloc(1,sizeof(struct search_pipe_data));
734         pdata->cmd = SEARCH_PIPE_CMD_ADD_GENLIST_PHONE;
735         pdata->obj = NULL;
736         pdata->result_list = result_list;
737         pdata->search_ret = thread_data->is_search_result;
738
739         if (ecore_thread_check(th) == EINA_TRUE)
740                 return;
741
742         ecore_pipe_write(ad->pipe, &pdata, sizeof(struct search_pipe_data));
743
744         SEARCH_FUNC_END;
745 }
746
747 static void __search_thread_end_cb(void *data, Ecore_Thread *th)
748 {
749         SEARCH_FUNC_START;
750
751         struct search_thread_data *thread_data = (struct search_thread_data *)data;
752         struct appdata *ad = thread_data->ad;
753         struct search_pipe_data *pdata = NULL;
754
755         pdata = (struct search_pipe_data *)calloc(1,sizeof(struct search_pipe_data));
756         pdata->cmd = SEARCH_PIPE_CMD_SET_LAYOUT;
757         pdata->search_ret = thread_data->is_search_result;
758
759         if (ecore_thread_check(th) == EINA_TRUE)
760                 return;
761
762         ecore_pipe_write(ad->pipe, &pdata, sizeof(struct search_pipe_data));
763
764         SEARCH_FREE(thread_data->keyword);
765         SEARCH_FREE(thread_data);
766
767         SEARCH_FUNC_END;
768 }
769
770 static void __search_thread_cancel_cb(void *data, Ecore_Thread *th)
771 {
772         SEARCH_FUNC_START;
773
774         struct search_thread_data *thread_data = (struct search_thread_data *)data;
775
776         SEARCH_FREE(thread_data->keyword);
777         SEARCH_FREE(thread_data);
778
779         SEARCH_FUNC_END;
780 }
781
782 void search_stop_search(void *data)
783 {
784         SEARCH_FUNC_START;
785
786         struct appdata *ad = (struct appdata *)data;
787
788         if (ad->search_thread) {
789                 ecore_thread_cancel(ad->search_thread);
790                 ad->search_thread = NULL;
791         }
792
793         if (ad->search_timer) {
794                 ecore_timer_del(ad->search_timer);
795                 ad->search_timer = NULL;
796         }
797
798         SEARCH_FUNC_END;
799 }
800
801 Ecore_Thread* search_make_result_thread(void *data)
802 {
803         SEARCH_FUNC_START;
804
805         int ret = 0;
806         Ecore_Thread *th = NULL;
807         struct appdata *ad = (struct appdata *)data;
808         struct search_thread_data *thread_data = NULL;
809
810         /* set keyword to vconf for save last search histroy */
811         ret = vconf_set_str(SMARTSEARCH_KEY_KEYWORD, ad->not_markup_search_word);
812
813         if (ret < 0)
814        SEARCH_DEBUG_WARNING("Error : set keyword to vconf");
815
816         /* Create Thread for Phone Search */
817         thread_data = (struct search_thread_data *)calloc(1, sizeof(struct search_thread_data));
818         thread_data->ad = ad;
819         thread_data->is_search_result = SEARCH_RET_SEARCH_NONE;
820         thread_data->keyword = strdup(ad->search_word);
821
822         th = ecore_thread_feedback_run(__search_thread_heavy_cb,
823                                 __search_thread_feedback_cb,
824                                 __search_thread_end_cb,
825                                 __search_thread_cancel_cb,
826                                 thread_data,
827                                 EINA_TRUE);
828
829         SEARCH_FUNC_END;
830         return th;
831 }
832
833
834 void search_result_gl_init(void *data)
835 {
836         SEARCH_FUNC_START;
837
838         struct appdata *ad = (struct appdata *)data;
839
840         ad->pipe = ecore_pipe_add(ecore_exe_pipe_cb, ad);
841         eina_lock_new(&ad->mutex);
842         eina_condition_new(&ad->condition, &ad->mutex);
843
844         Evas_Object *genlist;
845
846         if (ad->itc_pslist_1line == NULL) {
847                 ad->itc_pslist_1line = elm_genlist_item_class_new();
848                 ad->itc_pslist_1line->item_style = "1text.2";
849                 ad->itc_pslist_1line->func.text_get = __search_gl_label_get_search_item_type;
850                 ad->itc_pslist_1line->func.content_get = __search_gl_content_get_search_item_type;
851                 ad->itc_pslist_1line->func.state_get = __search_gl_state_get;
852                 ad->itc_pslist_1line->func.del = __search_gl_del;
853         }
854
855         if (ad->itc_pslist_1line_1icon == NULL) {
856                 ad->itc_pslist_1line_1icon = elm_genlist_item_class_new();
857                 ad->itc_pslist_1line_1icon->item_style = "1text.1icon.2.tb";
858                 ad->itc_pslist_1line_1icon->func.text_get = __search_gl_label_get_search_item_type;
859                 ad->itc_pslist_1line_1icon->func.content_get = __search_gl_content_get_search_item_type;
860                 ad->itc_pslist_1line_1icon->func.state_get = __search_gl_state_get;
861                 ad->itc_pslist_1line_1icon->func.del = __search_gl_del;
862         }
863
864         if (ad->itc_pslist_1line_2icon == NULL) {
865                 ad->itc_pslist_1line_2icon = elm_genlist_item_class_new();
866                 ad->itc_pslist_1line_2icon->item_style = "1text.2icon.6.tb";
867                 ad->itc_pslist_1line_2icon->func.text_get = __search_gl_label_get_search_item_type;
868                 ad->itc_pslist_1line_2icon->func.content_get = __search_gl_content_get_search_item_type;
869                 ad->itc_pslist_1line_2icon->func.state_get = __search_gl_state_get;
870                 ad->itc_pslist_1line_2icon->func.del = __search_gl_del;
871         }
872
873         if (ad->itc_pslist_2line_1icon == NULL) {
874                 ad->itc_pslist_2line_1icon = elm_genlist_item_class_new();
875                 ad->itc_pslist_2line_1icon->item_style = "2text.1icon.4.tb";
876                 ad->itc_pslist_2line_1icon->func.text_get = __search_gl_label_get_search_item_type;
877                 ad->itc_pslist_2line_1icon->func.content_get = __search_gl_content_get_search_item_type;
878                 ad->itc_pslist_2line_1icon->func.state_get = __search_gl_state_get;
879                 ad->itc_pslist_2line_1icon->func.del = __search_gl_del;
880         }
881
882         if (ad->itc_pslist_2line_2icon == NULL) {
883                 ad->itc_pslist_2line_2icon = elm_genlist_item_class_new();
884                 ad->itc_pslist_2line_2icon->item_style = "2text.2icon.4.tb";
885                 ad->itc_pslist_2line_2icon->func.text_get = __search_gl_label_get_search_item_type;
886                 ad->itc_pslist_2line_2icon->func.content_get = __search_gl_content_get_search_item_type;
887                 ad->itc_pslist_2line_2icon->func.state_get = __search_gl_state_get;
888                 ad->itc_pslist_2line_2icon->func.del = __search_gl_del;
889         }
890
891         if (ad->itc_grouptitle == NULL) {
892                 ad->itc_grouptitle = elm_genlist_item_class_new();
893                 ad->itc_grouptitle->item_style = "grouptitle";
894                 ad->itc_grouptitle->func.text_get = __search_gl_label_get_group;
895                 ad->itc_grouptitle->func.state_get = __search_gl_state_get;
896                 ad->itc_grouptitle->func.del = __search_gl_del_grouptitle;
897         }
898
899         if (ad->itc_label == NULL) {
900                 ad->itc_label = elm_genlist_item_class_new();
901                 ad->itc_label->item_style = "grouptitle";
902                 ad->itc_label->func.text_get = __search_gl_label_get_normal_type;
903                 ad->itc_label->func.state_get = __search_gl_state_get;
904                 ad->itc_label->func.del = __search_gl_del_grouptitle;
905                         }
906
907         if (ad->itc_listmore == NULL) {
908                 ad->itc_listmore = elm_genlist_item_class_new();
909                 ad->itc_listmore->item_style = "1text.2";
910                 ad->itc_listmore->func.text_get = __search_gl_label_get_more;
911                 ad->itc_listmore->func.state_get = __search_gl_state_get;
912                 ad->itc_listmore->func.del = __search_gl_del_more_button;
913         }
914
915         ad->search_gl = NULL;
916
917         genlist = elm_genlist_add(ad->navi_bar);
918
919         // To use multiline textblock/entry/editfield in genlist, set height_for_width mode
920         // then the item's height is calculated while the item's width fits to genlist width.
921         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
922         elm_genlist_tree_effect_enabled_set(genlist, EINA_TRUE);
923
924         ad->search_gl = genlist;
925
926         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND,
927                                          EVAS_HINT_EXPAND);
928         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL,
929                                         EVAS_HINT_FILL);
930         SEARCH_FUNC_END;
931 }
932
933 void search_result_gl_deinit(void *data)
934 {
935         SEARCH_FUNC_START;
936
937         struct appdata *ad = (struct appdata *)data;
938
939         ecore_pipe_del(ad->pipe);
940         eina_condition_free(&ad->condition);
941         eina_lock_free(&ad->mutex);
942
943         if (ad->itc_pslist_1line != NULL)
944                 elm_genlist_item_class_free(ad->itc_pslist_1line);
945
946         if (ad->itc_pslist_1line_1icon != NULL)
947                 elm_genlist_item_class_free(ad->itc_pslist_1line_1icon);
948
949         if (ad->itc_pslist_1line_2icon != NULL)
950                 elm_genlist_item_class_free(ad->itc_pslist_2line_1icon);
951
952         if (ad->itc_pslist_2line_1icon != NULL)
953                 elm_genlist_item_class_free(ad->itc_pslist_2line_1icon);
954
955         if (ad->itc_pslist_2line_2icon != NULL)
956                 elm_genlist_item_class_free(ad->itc_pslist_2line_2icon);
957
958         if (ad->itc_grouptitle != NULL)
959                 elm_genlist_item_class_free(ad->itc_grouptitle);
960
961         if (ad->itc_label != NULL)
962                 elm_genlist_item_class_free(ad->itc_label);
963
964         if (ad->itc_listmore != NULL)
965                 elm_genlist_item_class_free(ad->itc_listmore);
966
967         SEARCH_FUNC_END;
968 }
969
970
971 void search_gl_result_more_item_append_cb(void *data, Evas_Object * obj,
972                                           void *event_info)
973 {
974         SEARCH_FUNC_START;
975
976         struct appdata *ad = (struct appdata *)data;
977         Elm_Object_Item *it = (Elm_Object_Item *) event_info;
978         struct search_pipe_data *pdata = NULL;
979         struct search_genlist_info *result_info = NULL;
980         struct search_item_more_sel *sel_more =
981             (search_item_more_sel *)elm_object_item_data_get(it);
982
983         struct search_content_object *content_obj = sel_more->obj;
984         int offset = content_obj->mr_info.next_point;
985         int ret = SEARCH_RET_SEARCH_NONE;
986
987         elm_object_item_del(it);
988
989         result_info = (struct search_genlist_info *)calloc(1, sizeof(struct search_genlist_info));
990         ret = __search_gl_set_phone_search_result(content_obj,
991                                                 offset,
992                                                 ad->search_word,
993                                                 ad,
994                                                 result_info);
995
996         pdata = (struct search_pipe_data *)calloc(1,sizeof(struct search_pipe_data));
997         pdata->cmd = SEARCH_PIPE_CMD_ADD_GENLIST_PHONE;
998         pdata->obj = NULL;
999         pdata->result_list = result_info;
1000         pdata->search_ret = ret;
1001         ecore_pipe_write(ad->pipe, &pdata, sizeof(struct search_pipe_data));
1002
1003         pdata = (struct search_pipe_data *)calloc(1,sizeof(struct search_pipe_data));
1004         pdata->cmd = SEARCH_PIPE_CMD_SET_LAYOUT;
1005         pdata->search_ret = ret;
1006         ecore_pipe_write(ad->pipe, &pdata, sizeof(struct search_pipe_data));
1007
1008         SEARCH_FUNC_END;
1009 }
1010
1011 void search_set_result_list(void *data)
1012 {
1013         SEARCH_FUNC_START;
1014         struct appdata *ad = (struct appdata *)data;
1015         int category = ad->search_category;
1016         int ret = SEARCH_RET_SEARCH_NONE;
1017
1018         bool is_input_keyword = false;
1019         struct search_pipe_data *pipe_data = NULL;
1020         char *text = NULL;
1021
1022         search_stop_search(ad);
1023
1024         text = elm_entry_markup_to_utf8(elm_object_text_get(ad->search_entry));
1025         SEARCH_FREE(ad->not_markup_search_word);
1026         SEARCH_FREE(ad->search_word);
1027
1028         if((text) && (strlen(text) > 0)) {
1029                 ad->not_markup_search_word = strdup(elm_object_text_get(ad->search_entry));
1030                 ad->search_word = text;
1031                 is_input_keyword = true;
1032
1033                 /* set keyword to vconf for save last search histroy */
1034                 ret = vconf_set_str(SMARTSEARCH_KEY_KEYWORD, ad->not_markup_search_word);
1035
1036                 if (ret < 0)
1037                SEARCH_DEBUG_WARNING("Error : set keyword to vconf");
1038         } else {
1039                 if (category == SEARCH_CATE_PHONE) {
1040                 pipe_data = (struct search_pipe_data *)calloc(1, sizeof(struct search_pipe_data));
1041                 pipe_data->cmd = SEARCH_PIPE_CMD_SET_LAYOUT;
1042                 pipe_data->search_ret = SEARCH_RET_SEARCH_NONE;
1043                 ecore_pipe_write(ad->pipe, &pipe_data, sizeof(struct search_pipe_data));
1044                 }
1045                 /* set keyword to vconf for save last search histroy */
1046                 ret = vconf_set_str(SMARTSEARCH_KEY_KEYWORD, "");
1047
1048                 if (ret < 0)
1049                SEARCH_DEBUG_WARNING("Error : set keyword to vconf");
1050         }
1051
1052         /* If category is Phone, make a thread for search */
1053         pipe_data = (struct search_pipe_data *)calloc(1, sizeof(struct search_pipe_data));
1054         pipe_data->cmd = SEARCH_PIPE_CMD_CLEAR_GENLIST;
1055         ecore_pipe_write(ad->pipe, &pipe_data, sizeof(struct search_pipe_data));
1056
1057         switch(category) {
1058         case SEARCH_CATE_PHONE:
1059                 if (is_input_keyword) {
1060                         ad->search_thread = search_make_result_thread(ad);
1061                 }
1062         break;
1063         }
1064         SEARCH_FUNC_END;
1065 }
1066
1067 void search_layout_set_search_list(void* data)
1068 {
1069         SEARCH_FUNC_START;
1070
1071         struct appdata *ad = (struct appdata *)data;
1072
1073         if (elm_object_part_content_get(ad->sb_layout,"list_noresult")) {
1074                 elm_object_part_content_unset(ad->sb_layout,"list_noresult");
1075                 evas_object_hide(ad->noresult_view);
1076
1077                 if (ad->search_gl) {
1078                 elm_object_part_content_set(ad->sb_layout, "list", ad->search_gl);
1079                 evas_object_show(ad->search_gl);
1080         }
1081         }
1082
1083         if (ad->search_gl)
1084         elm_genlist_item_show(elm_genlist_first_item_get(ad->search_gl), ELM_GENLIST_ITEM_SCROLLTO_TOP);
1085         SEARCH_FUNC_END;
1086
1087         return;
1088 }
1089
1090 void search_layout_set_search_no_result(void* data)
1091 {
1092         SEARCH_FUNC_START;
1093
1094         struct appdata *ad = (struct appdata *)data;
1095
1096         if (elm_object_part_content_get(ad->sb_layout,"list")) {
1097                 elm_object_part_content_unset(ad->sb_layout, "list");
1098
1099                 if (ad->search_gl)
1100                 evas_object_hide(ad->search_gl);
1101
1102                 elm_object_part_content_set(ad->sb_layout,"list_noresult", ad->noresult_view);
1103                 evas_object_show(ad->noresult_view);
1104         }
1105
1106         SEARCH_FUNC_END;
1107
1108         return;
1109 }
1110
1111 int search_layout_get_content_list_set_status(void* data)
1112 {
1113         SEARCH_FUNC_START;
1114
1115         struct appdata *ad = (struct appdata *)data;
1116         int ret;
1117
1118         if (elm_object_part_content_get(ad->sb_layout,"list")) {
1119                 ret = SEARCH_RET_SUCCESS;
1120         } else {
1121                 ret = SEARCH_RET_FAIL;
1122         }
1123
1124         SEARCH_FUNC_END;
1125
1126         return ret;
1127 }
1128
1129