58414c6b3c501a0d6acdfd00824d065e8eadb4bc
[apps/home/smartsearch.git] / src / ps_searcher.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
24 #include <sqlite3.h>
25
26 #include <contacts-svc.h>
27 #include <calendar-svc-provider.h>
28 #include <memo-db.h>
29
30 #include <email-types.h>
31 #include <email-api.h>
32
33 #include <visual-svc.h>
34 #include <visual-svc-types.h>
35 #include <audio-svc.h>
36 #include <audio-svc-types.h> 
37 #include <audio-svc-error.h>
38
39 #include <ail.h>
40
41 static GList *g_history_list;
42
43 char* g_keyword = NULL;
44
45 int search_sql_stmt_init(sqlite3_stmt * sql_stmt)
46 {
47         SEARCH_FUNC_START;
48
49         int rc = 0;
50
51         rc = sqlite3_clear_bindings(sql_stmt);
52         if (rc != SQLITE_OK) {
53                 SEARCH_DEBUG_ERROR("sqlite3_clear_bindings error");
54                 return SEARCH_RET_FAIL;
55         }
56
57         rc = sqlite3_reset(sql_stmt);
58         if (rc != SQLITE_OK) {
59                 SEARCH_DEBUG_ERROR("sqlite3_reset error");
60                 return SEARCH_RET_FAIL;
61         }
62
63         SEARCH_FUNC_END;
64 }
65
66 int search_sql_stmt_finalize(sqlite3_stmt * stmt)
67 {
68         SEARCH_FUNC_START;
69
70         int ret;
71
72         ret = sqlite3_finalize(stmt);
73         if (ret != SQLITE_OK) {
74                 SEARCH_DEBUG_ERROR("sqlite3_finalize error : %d ", ret);
75                 return -1;
76         }
77
78         SEARCH_FUNC_END;
79
80         return 0;
81 }
82
83 int search_sql_prepare_stmt(sqlite3 *db_handle, const char *query,
84                                          sqlite3_stmt ** stmt_out)
85 {
86         SEARCH_FUNC_START;
87
88         int rc = 0;
89         int ret = 0;
90
91         rc = sqlite3_prepare(db_handle, query, strlen(query), stmt_out, NULL);
92         if (rc != SQLITE_OK) {
93                 SEARCH_DEBUG_ERROR("sql preapre error : %d / %s", rc, query);
94                 ret = SEARCH_RET_FAIL;
95         } else {
96                 ret = SEARCH_RET_SUCCESS;
97         }
98
99         SEARCH_FUNC_END;
100
101         return ret;
102 }
103
104 static int __search_sql_contact_search_result_cb(CTSvalue * value,
105                                                  void *user_data)
106 {
107         SEARCH_FUNC_START;
108
109         struct appdata *ad = (struct appdata *)user_data;
110
111         struct search_item_sel *sel_mem = NULL;
112         int type = SEARCH_TYPE_PHONE_CONTACTS;
113
114         struct search_item_sel *sel_mem_info = NULL;
115         char display_name[128] = { 0, };
116         const char *first, *last, *display, *number;
117
118         bool is_word_in_str;
119
120         if (ad->cate_info[type].next_point == 0) {
121                 //### Set group title 
122
123                 sel_mem =
124                     (struct search_item_sel *)calloc(1,
125                                                      sizeof(struct
126                                                             search_item_sel));
127                 sel_mem->type = type;
128
129                 ad->gl_result_group_item[type] =
130                     elm_genlist_item_append(ad->search_gl,
131                                             (ad->itc_grouptitle), sel_mem,
132                                             (Elm_Object_Item *) NULL,
133                                             ELM_GENLIST_ITEM_GROUP, NULL, NULL);
134         }
135
136         if (ad->cate_info[type].next_point <
137             ad->cate_info[type].offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
138                 sel_mem_info =
139                     (struct search_item_sel *)calloc(1,
140                                                      sizeof(struct
141                                                             search_item_sel));
142                 sel_mem_info->type = type;
143
144                 /************ START : set search result to sel_mem_info buf ************/
145
146                 /***********************************************************************/
147
148                 memset(display_name, 0x00, sizeof(display_name));
149
150                 display =
151                     contacts_svc_value_get_str(value,
152                                                CTS_LIST_NUM_CONTACT_DISPLAY_STR);
153
154                 if (display) {
155                         snprintf(display_name, sizeof(display_name), "%s",
156                                  display);
157                 } else {
158                         first =
159                             contacts_svc_value_get_str(value,
160                                                        CTS_LIST_NUM_CONTACT_FIRST_STR);
161                         last =
162                             contacts_svc_value_get_str(value,
163                                                        CTS_LIST_NUM_CONTACT_LAST_STR);
164
165                         if (CTS_ORDER_NAME_FIRSTLAST ==
166                             contacts_svc_get_order(CTS_ORDER_OF_DISPLAY)) {
167                                 if (first) {
168                                         if (last) {
169                                                 snprintf(display_name,
170                                                          sizeof(display_name),
171                                                          "%s %s", first, last);
172                                         } else {
173                                                 snprintf(display_name,
174                                                          sizeof(display_name),
175                                                          "%s", first);
176                                         }
177                                 } else {
178                                         if (last) {
179                                                 snprintf(display_name,
180                                                          sizeof(display_name),
181                                                          "%s", last);
182                                         }
183                                 }
184                         } else {
185                                 if (last) {
186                                         if (first) {
187                                                 snprintf(display_name,
188                                                          sizeof(display_name),
189                                                          "%s %s", last, first);
190                                         } else {
191                                                 snprintf(display_name,
192                                                          sizeof(display_name),
193                                                          "%s", last);
194                                         }
195                                 } else {
196                                         if (first) {
197                                                 snprintf(display_name,
198                                                          sizeof(display_name),
199                                                          "%s", first);
200                                         }
201                                 }
202                         }
203                 }
204
205                 if((display_name) && strlen(display_name) > 0) {
206                         char* markup_text_1line = elm_entry_utf8_to_markup(display_name);
207                         
208                         strncpy(sel_mem_info->main_buf,
209                                         search_markup_keyword(markup_text_1line, ad->not_markup_search_word, &is_word_in_str),
210                                         MAX_LENGTH_PER_LINE - 1);
211
212                         SEARCH_FREE(markup_text_1line);
213                 }       
214
215                 /***********************************************************************/
216
217                 number = contacts_svc_value_get_str(value, CTS_LIST_NUM_NUMBER_STR);
218
219                 if((number) && strlen(number) > 0) {
220                         char* markup_text_2line = elm_entry_utf8_to_markup(number);
221                         
222                         strncpy(sel_mem_info->sub_buf,
223                                         search_markup_keyword(markup_text_2line, ad->not_markup_search_word, &is_word_in_str),
224                                         MAX_LENGTH_PER_LINE - 1);
225                         SEARCH_FREE(markup_text_2line);
226                 }
227                         
228                 /***********************************************************************/
229
230                 snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID - 1, "%d",
231                          contacts_svc_value_get_int(value,
232                                                     CTS_LIST_NUM_CONTACT_ID_INT));
233
234                 /***********************************************************************/
235                 /************ END : set search result to sel_mem_info buf **************/
236
237                 elm_genlist_item_append(ad->search_gl, (ad->itc_pslist_2line),
238                                         sel_mem_info,
239                                         ad->gl_result_group_item[type],
240                                         ELM_GENLIST_ITEM_NONE,
241                                         search_gl_phone_result_sel, ad);
242
243                 ad->cate_info[type].next_point++;
244         } else if (ad->cate_info[type].next_point ==
245                    ad->cate_info[type].offset +
246                    SEARCH_CATEGORY_LIST_MORE_CNT) {
247                 /* Set more title to genlist (under 'search all') */
248                 sel_mem =
249                     (struct search_item_sel *)calloc(1,
250                                                      sizeof(struct
251                                                             search_item_sel));
252                 sel_mem->type = type;
253
254                 elm_genlist_item_append(ad->search_gl, (ad->itc_listmore),
255                                         sel_mem, ad->gl_result_group_item[type],
256                                         ELM_GENLIST_ITEM_NONE,
257                                         search_gl_result_more_item_append_cb,
258                                         ad);
259         } else {
260                 SEARCH_DEBUG_WARNING
261                     ("__search_sql_contact_search_result_cb wrong index : %d",
262                      ad->cate_info[type].next_point);
263         }
264
265         SEARCH_FUNC_END;
266
267         return 0;
268 }
269
270 int search_sql_contact_search_result_by_api(int type, int offset, int limit,
271                                             void *data)
272 {
273         SEARCH_FUNC_START;
274
275         struct appdata *ad = (struct appdata *)data;
276         int ret = 0;
277
278         /* Check whether search result is exist or not */
279         ret =
280             contacts_svc_smartsearch_excl(ad->search_word, limit, offset,
281                                           __search_sql_contact_search_result_cb,
282                                           data);
283         if (ret != CTS_SUCCESS) {
284                 SEARCH_DEBUG_WARNING("contacts svc search return error : %d",
285                                      ret);
286                 return SEARCH_RET_SEARCH_FAIL;
287         }
288
289         if (ad->cate_info[type].next_point == 0) {
290                 SEARCH_DEBUG_WARNING("contacts svc search no result");
291                 return SEARCH_RET_SEARCH_NONE;
292         }
293
294         SEARCH_FUNC_END;
295
296         return SEARCH_RET_SEARCH_SUCCESS;
297 }
298
299 int search_sql_calendar_search_result_by_api(int type, int offset, int limit,
300                                              void *data)
301 {
302         SEARCH_FUNC_START;
303
304         int ret;
305         struct appdata *ad = (struct appdata *)data;
306
307         cal_struct *event = NULL;
308         cal_iter *iter = NULL;
309
310         struct search_item_sel *sel_mem = NULL;
311         Elm_Object_Item *git = NULL;
312         Evas_Object *glist;
313
314         struct search_item_sel *sel_mem_info = NULL;
315         int loop_cnt = 0;
316
317         char *summary = NULL;
318         bool is_word_in_str;
319
320         time_t start_time = 0;
321
322         ret =
323             calendar_svc_search_list(0, 0, CAL_STRUCT_SCHEDULE,
324                                      CAL_VALUE_TXT_SUMMARY, ad->search_word,
325                                      offset, limit, &iter);
326
327         if (ret == CAL_SUCCESS) {
328                 ret = calendar_svc_iter_next(iter);
329                 if (ret != CAL_SUCCESS) {
330                         SEARCH_DEBUG_WARNING
331                             ("calendar svc search no result : %d", ret);
332                         return SEARCH_RET_SEARCH_NONE;
333                 }
334                 /* Set group title */
335                 glist = ad->search_gl;
336
337                 if (offset == 0) {
338                         sel_mem =
339                             (struct search_item_sel *)calloc(1,
340                                                              sizeof(struct
341                                                                     search_item_sel));
342                         sel_mem->type = type;
343
344                         git =
345                             elm_genlist_item_append(glist,
346                                                     (ad->itc_grouptitle),
347                                                     sel_mem,
348                                                     (Elm_Object_Item *) NULL,
349                                                     ELM_GENLIST_ITEM_GROUP,
350                                                     NULL, NULL);
351
352                         ad->gl_result_group_item[type] = git;
353                 }
354                 /* Set search result to genlist */
355                 do {
356                         loop_cnt++;
357
358                         if (loop_cnt == limit) {
359                                 break;
360                         }
361                         /********** START : set search result to sel_mem_info buf ************/
362
363                         ret = calendar_svc_iter_get_info(iter, &event);
364
365                         if (ret == CAL_SUCCESS) {
366                                 sel_mem_info =
367                                     (struct search_item_sel *)calloc(1,
368                                                                      sizeof
369                                                                      (struct
370                                                                       search_item_sel));
371                                 sel_mem_info->type = type;
372
373                                 summary =
374                                     calendar_svc_struct_get_str(event,
375                                                                 CAL_VALUE_TXT_SUMMARY);
376
377                                 if ((summary) && (strlen(summary) > 0)) {       
378                                         char* markup_text_1line = elm_entry_utf8_to_markup(summary);
379                                         
380                                         strncpy(sel_mem_info->main_buf,
381                                                         search_markup_keyword(markup_text_1line,
382                                                                              ad->not_markup_search_word,
383                                                                              &is_word_in_str),
384                                                         MAX_LENGTH_PER_LINE - 1);
385                                         
386                                         SEARCH_FREE(markup_text_1line);
387                                 }       
388
389                                 snprintf(sel_mem_info->main_id,
390                                          MAX_LENGTH_PER_ID - 1, "%d",
391                                          calendar_svc_struct_get_int(event,
392                                                                      CAL_VALUE_INT_INDEX));
393
394                                 start_time =
395                                     calendar_svc_struct_get_time(event,
396                                                                  CAL_VALUE_GMT_START_DATE_TIME,
397                                                                  CAL_TZ_FLAG_GMT);
398                                 search_util_date_time_format_get_val(localtime
399                                                                      (&start_time),
400                                                                      sel_mem_info->sub_buf);
401
402                                 calendar_svc_struct_free(&event);
403                                 summary = NULL;
404
405                                 elm_genlist_item_append(glist,
406                                                         (ad->itc_pslist_1line),
407                                                         sel_mem_info,
408                                                         ad->gl_result_group_item[type],
409                                                         ELM_GENLIST_ITEM_NONE,
410                                                         search_gl_phone_result_sel,
411                                                         ad);
412                                 sel_mem_info = NULL;
413                         } else {
414                                 SEARCH_DEBUG_WARNING
415                                     ("calendar_svc_iter_get_info fail : %d",
416                                      ret);
417                                 break;
418                         }
419
420                 }
421                 while (calendar_svc_iter_next(iter) == CAL_SUCCESS);
422         } else {
423                 SEARCH_DEBUG_WARNING("calendar_svc_search_list fail : %d", ret);
424                 return SEARCH_RET_SEARCH_FAIL;
425         }
426
427         /* Set more title to genlist (under 'search all') */
428         if (loop_cnt == limit) {
429                 ad->cate_info[type].next_point = offset + limit - 1;
430
431                 sel_mem =
432                     (struct search_item_sel *)calloc(1,
433                                                      sizeof(struct
434                                                             search_item_sel));
435                 sel_mem->type = type;
436
437                 elm_genlist_item_append(ad->search_gl, (ad->itc_listmore),
438                                         sel_mem, ad->gl_result_group_item[type],
439                                         ELM_GENLIST_ITEM_NONE,
440                                         search_gl_result_more_item_append_cb,
441                                         ad);
442         }
443
444         SEARCH_FUNC_END;
445         return SEARCH_RET_SEARCH_SUCCESS;
446 }
447
448 static void __search_sql_memo_search_result_cb(memo_data_t * value,
449                                                void *user_data)
450 {
451         SEARCH_FUNC_START;
452
453         struct appdata *ad = (struct appdata *)user_data;
454
455         struct search_item_sel *sel_mem = NULL;
456         int type = SEARCH_TYPE_PHONE_MEMO;
457
458         struct search_item_sel *sel_mem_info = NULL;
459
460         bool is_word_in_str;
461
462         if(!value) {
463                 SEARCH_DEBUG_WARNING("memo data value is null");
464                 return;
465         }
466
467         if (ad->cate_info[type].next_point == 0) {
468                 /* Set group title */
469
470                 sel_mem =
471                     (struct search_item_sel *)calloc(1,
472                                                      sizeof(struct
473                                                             search_item_sel));
474                 sel_mem->type = type;
475
476                 ad->gl_result_group_item[type] =
477                     elm_genlist_item_append(ad->search_gl,
478                                             (ad->itc_grouptitle), sel_mem,
479                                             (Elm_Object_Item *) NULL,
480                                             ELM_GENLIST_ITEM_GROUP, NULL, NULL);
481         }
482
483         if (ad->cate_info[type].next_point <
484             ad->cate_info[type].offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
485                 sel_mem_info =
486                     (struct search_item_sel *)calloc(1,
487                                                      sizeof(struct
488                                                             search_item_sel));
489                 sel_mem_info->type = type;
490
491                 /********** START : set search result to sel_mem_info buf ************/
492                 /* First, We should check comment field. It means drawing memo
493                  * If comment field is NULL, then we check content field */
494                 if ((value->comment) && (strlen(value->comment) > 0)) {
495                         char* markup_text_1line = elm_entry_utf8_to_markup(value->comment);
496                                 
497                         strncpy(sel_mem_info->main_buf,
498                                         search_markup_keyword(markup_text_1line, ad->not_markup_search_word,
499                                                                                         &is_word_in_str),
500                                         MAX_LENGTH_PER_LINE - 1);
501
502                         SEARCH_FREE(markup_text_1line);
503                 } else if ((value->content) && (strlen(value->content) > 0)) {
504                         char* markup_text_1line = elm_entry_utf8_to_markup(value->content);
505
506                         strncpy(sel_mem_info->main_buf,
507                                         search_markup_keyword(markup_text_1line, ad->not_markup_search_word,
508                                                                                 &is_word_in_str),
509                                         MAX_LENGTH_PER_LINE - 1);
510
511                         SEARCH_FREE(markup_text_1line);                         
512                 }
513                 snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID - 1, "%d", value->id);
514
515                 search_util_date_time_format_get_val(localtime
516                                                      (&value->modi_time),
517                                                      sel_mem_info->sub_buf);
518
519                 /********** END : set search result to sel_mem_info buf *************/
520
521                 elm_genlist_item_append(ad->search_gl, (ad->itc_pslist_1line),
522                                         sel_mem_info,
523                                         ad->gl_result_group_item[type],
524                                         ELM_GENLIST_ITEM_NONE,
525                                         search_gl_phone_result_sel, ad);
526
527                 ad->cate_info[type].next_point++;
528         } else if (ad->cate_info[type].next_point ==
529                    ad->cate_info[type].offset +
530                    SEARCH_CATEGORY_LIST_MORE_CNT) {
531                 /* Set more title to genlist (under 'search all') */
532                 sel_mem =
533                     (struct search_item_sel *)calloc(1,
534                                                      sizeof(struct
535                                                             search_item_sel));
536                 sel_mem->type = type;
537
538                 elm_genlist_item_append(ad->search_gl, (ad->itc_listmore),
539                                         sel_mem, ad->gl_result_group_item[type],
540                                         ELM_GENLIST_ITEM_NONE,
541                                         search_gl_result_more_item_append_cb,
542                                         ad);
543         } else {
544                 SEARCH_DEBUG_WARNING
545                     ("__search_sql_memo_search_result_cb wrong index : %d",
546                      ad->cate_info[type].next_point);
547         }
548
549         SEARCH_FUNC_END;
550
551         return;
552 }
553
554 int search_sql_memo_search_result_by_api(int type, int offset, int limit,
555                                          void *data)
556 {
557         SEARCH_FUNC_START;
558
559         struct appdata *ad = (struct appdata *)data;
560
561         memo_search_data(ad->search_word, 
562                                         limit, offset, 
563                                         MEMO_SORT_TITLE_ASC,
564                                         __search_sql_memo_search_result_cb, 
565                                         data);
566
567         if (ad->cate_info[type].next_point == 0) {
568                 SEARCH_DEBUG_WARNING("no memo search result");
569                 return SEARCH_RET_SEARCH_NONE;
570         }
571
572         SEARCH_FUNC_END;
573         return SEARCH_RET_SEARCH_SUCCESS;
574 }
575
576 int search_sql_email_search_result_by_api(int type, int offset, int limit,
577                                          void *data)
578 {
579         SEARCH_FUNC_START;
580
581         struct appdata *ad = (struct appdata *)data;
582
583         int result_cnt = 0;
584         int ret = 0;
585         int i = 0;
586         emf_mail_list_item_t *mail_list = NULL;
587
588         struct search_item_sel *sel_mem = NULL;
589         Elm_Object_Item *git = NULL;
590         Evas_Object *glist;
591
592         struct search_item_sel *sel_mem_info = NULL;
593         bool is_word_in_str;
594
595         ret = email_find_mail(0, NULL, 0, EMF_SEARCH_FILTER_ALL , 
596                 ad->search_word, offset, limit, EMF_SORT_DATETIME_HIGH,
597                 &mail_list, &result_cnt);
598
599         if(ret < 1) {
600                 SEARCH_DEBUG_WARNING("no email search result"); 
601                 return SEARCH_RET_SEARCH_FAIL;
602         }
603
604         if(result_cnt == 0) {
605                 SEARCH_DEBUG_WARNING("no email search result");
606                 return SEARCH_RET_SEARCH_NONE;
607         }
608
609         if(mail_list == NULL) {
610                 SEARCH_DEBUG_WARNING("mail_list is null");
611                 return SEARCH_RET_SEARCH_NONE;
612         }
613
614         /* Set group title */
615         glist = ad->search_gl;
616
617         if (offset == 0) {
618                 sel_mem = (struct search_item_sel *)calloc(1,
619                                 sizeof(struct search_item_sel));
620
621                 sel_mem->type = type;
622
623                 git = elm_genlist_item_append(glist,
624                                                         (ad->itc_grouptitle),
625                                                         sel_mem,
626                                                         (Elm_Object_Item *) NULL,
627                                                         ELM_GENLIST_ITEM_GROUP,
628                                                         NULL, NULL);
629
630                 ad->gl_result_group_item[type] = git;
631         }
632
633         /* Set search result to genlist */
634         for(i=0;i<result_cnt;i++) {
635
636                 if(i == limit -1)
637                         break;
638                 
639                 sel_mem_info = (struct search_item_sel *)calloc(1,
640                                                                      sizeof(struct search_item_sel));
641
642                 sel_mem_info->type = type;
643
644                 snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID - 1, "%d", mail_list[i].mail_id);
645
646                 snprintf(sel_mem_info->path, MAX_LENGTH_PER_PATH - 1, "%d %d %s",
647                                 mail_list[i].account_id, mail_list[i].mail_id, mail_list[i].mailbox_name);
648
649                 SEARCH_DEBUG_LOG("subject : %s", mail_list[i].subject);
650
651                 if((mail_list[i].subject) && (strlen(mail_list[i].subject) > 0)) {
652                         char* markup_text_1line = elm_entry_utf8_to_markup(mail_list[i].subject);
653                         
654                         strncpy(sel_mem_info->main_buf,
655                                         search_markup_keyword(markup_text_1line, ad->not_markup_search_word, &is_word_in_str),
656                                         MAX_LENGTH_PER_LINE - 1);
657                         SEARCH_FREE(markup_text_1line);
658                 } 
659
660                 if((mail_list[i].from) && (strlen(mail_list[i].from) > 0)) {
661                         char* markup_text_2line = elm_entry_utf8_to_markup(mail_list[i].from);          
662
663                         strncpy(sel_mem_info->sub_buf,
664                                         search_markup_keyword(markup_text_2line, ad->not_markup_search_word, &is_word_in_str),
665                                         MAX_LENGTH_PER_LINE - 1);
666                         SEARCH_FREE(markup_text_2line);
667                 }       
668
669                 if(is_word_in_str == false && (mail_list[i].recipients) && (strlen(mail_list[i].recipients) > 0)) {
670                         char* markup_text_2line = elm_entry_utf8_to_markup(mail_list[i].recipients);
671                         
672                         strncpy(sel_mem_info->sub_buf,
673                                         search_markup_keyword(markup_text_2line, ad->not_markup_search_word, &is_word_in_str),
674                                         MAX_LENGTH_PER_LINE - 1);
675                         SEARCH_FREE(markup_text_2line);
676                 }
677
678                 elm_genlist_item_append(glist,
679                                                 (ad->itc_pslist_2line),
680                                                 sel_mem_info,
681                                                 ad->gl_result_group_item
682                                                 [type],
683                                                 ELM_GENLIST_ITEM_NONE,
684                                                 search_gl_phone_result_sel,
685                                                 ad);
686                 sel_mem_info = NULL;
687         }
688
689         SEARCH_FREE(mail_list);
690         
691         /* Set more title to genlist (under 'search all') */
692         if (result_cnt == limit) {
693                 ad->cate_info[type].next_point = offset + result_cnt - 1;
694
695                 sel_mem =
696                     (struct search_item_sel *)calloc(1,
697                                                      sizeof(struct search_item_sel));
698                 sel_mem->type = type;
699
700                 elm_genlist_item_append(ad->search_gl, (ad->itc_listmore),
701                                         sel_mem, ad->gl_result_group_item[type],
702                                         ELM_GENLIST_ITEM_NONE,
703                                         search_gl_result_more_item_append_cb,
704                                         ad);
705         }
706
707         SEARCH_FUNC_END;
708         return SEARCH_RET_SEARCH_SUCCESS;
709 }
710
711 static int __search_sql_media_search_item_list_search_cb( Mitem* item, void* user_data) 
712 {
713         GList** list = (GList**) user_data;
714         *list = g_list_append( *list, item );
715
716         return 0;
717 }
718
719 static int __search_sql_media_search_result_by_api(int type, int offset, int limit,
720                                          minfo_file_type media_type, void *data)
721 {
722         SEARCH_FUNC_START;
723
724         struct appdata *ad = (struct appdata *)data;
725
726         int result_cnt = 0;
727         int ret = 0;
728         int i = 0;
729         int func_ret = SEARCH_RET_SEARCH_SUCCESS;
730
731         struct search_item_sel *sel_mem = NULL;
732         Elm_Object_Item *git = NULL;
733         Evas_Object *glist;
734
735         struct search_item_sel *sel_mem_info = NULL;
736         bool is_word_in_str;
737         
738         GList *p_media_list = NULL;
739         Mitem* item = NULL;
740         minfo_item_filter item_filter;
741
742         memset(&item_filter, 0x00, sizeof(minfo_item_filter));
743
744         minfo_search_field_t search_field = MINFO_SEARCH_BY_NAME;
745         minfo_folder_type folder_type = MINFO_CLUSTER_TYPE_ALL;
746
747         item_filter.file_type = media_type;
748
749         /* MINFO_MEDIA_SORT_BY_NAME_DESC, MINFO_MEDIA_SORT_BY_DATE_DESC, MINFO_MEDIA_SORT_BY_DATE_ASC ... */
750         item_filter.sort_type = MINFO_MEDIA_SORT_BY_NAME_ASC;
751
752         item_filter.start_pos = offset;
753         item_filter.end_pos = offset+limit-1;
754
755         item_filter.with_meta = 0;
756         item_filter.favorite = MINFO_MEDIA_FAV_ALL;
757         
758         /* get a set of items */
759         ret = minfo_get_item_list_search(ad->media_handle,
760                                                         search_field,
761                                                         ad->search_word, 
762                                                         folder_type, 
763                                                         item_filter, 
764                                                         __search_sql_media_search_item_list_search_cb, 
765                                                         &p_media_list);
766         if (ret < 0) {
767                 SEARCH_DEBUG_WARNING("minfo_get_item_list_search failed : %d\n", ret);
768                 func_ret = SEARCH_RET_SEARCH_FAIL;
769                 goto out_func;
770         }
771
772         if (p_media_list == NULL) {
773                 SEARCH_DEBUG_WARNING("minfo_get_item_list_search media list null\n");
774                 func_ret = SEARCH_RET_SEARCH_FAIL;
775                 goto out_func;
776         }
777
778         result_cnt = g_list_length(p_media_list);
779         if (result_cnt == 0) {
780                 SEARCH_DEBUG_WARNING("minfo_item_list length 0\n");
781                 func_ret = SEARCH_RET_SEARCH_FAIL;
782                 goto out_func;
783         }
784
785         /* Set group title */
786         glist = ad->search_gl;
787
788         if (offset == 0) {
789                 sel_mem = (struct search_item_sel *)calloc(1,
790                                 sizeof(struct search_item_sel));
791
792                 sel_mem->type = type;
793
794                 git = elm_genlist_item_append(glist,
795                                                         (ad->itc_grouptitle),
796                                                         sel_mem,
797                                                         (Elm_Object_Item *) NULL,
798                                                         ELM_GENLIST_ITEM_GROUP,
799                                                         NULL, NULL);
800
801                 ad->gl_result_group_item[type] = git;
802         }
803
804         /* Set search result to genlist */
805         for (i = 0; i < result_cnt; i++) {
806
807                 if(i == limit -1)
808                         break;
809
810                 item = NULL;
811                 item = (Mitem*)g_list_nth_data(p_media_list, i);
812
813                 if(item == NULL) {
814                         break;
815                 }
816                 
817                 sel_mem_info = (struct search_item_sel *)calloc(1,
818                                                                      sizeof(struct search_item_sel));
819
820                 sel_mem_info->type = type;
821
822                 if ((item->display_name) && (strlen(item->display_name) > 0)) {
823                         char* markup_text_1line = elm_entry_utf8_to_markup(item->display_name);
824                         
825                         strncpy(sel_mem_info->main_buf,
826                                         search_markup_keyword(markup_text_1line,
827                                                                                         ad->not_markup_search_word,
828                                                                                         &is_word_in_str),
829                                         MAX_LENGTH_PER_LINE - 1);
830                         SEARCH_FREE(markup_text_1line);
831                 }       
832
833                 snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID, "%d",
834                                  item->type);
835
836                 snprintf(sel_mem_info->path, MAX_LENGTH_PER_PATH - 1, "%s",
837                          item->file_url);
838
839                 elm_genlist_item_append(glist,
840                                                 (ad->itc_pslist_1line),
841                                                 sel_mem_info,
842                                                 ad->gl_result_group_item
843                                                 [type],
844                                                 ELM_GENLIST_ITEM_NONE,
845                                                 search_gl_phone_result_sel,
846                                                 ad);
847                 sel_mem_info = NULL;
848         }
849
850         /* delete list to avoid memory leak */
851         for (i = 0; i < result_cnt; i++) {
852                 item = (Mitem*)g_list_nth_data(p_media_list, i);
853                 minfo_destroy_mtype_item(item);
854         }
855
856         g_list_free(p_media_list);
857         p_media_list = NULL;
858
859         /* Set more title to genlist (under 'search all') */
860         if (result_cnt == limit) {
861                 ad->cate_info[type].next_point = offset + result_cnt - 1;
862
863                 sel_mem =
864                     (struct search_item_sel *)calloc(1,
865                                                      sizeof(struct search_item_sel));
866                 sel_mem->type = type;
867
868                 elm_genlist_item_append(ad->search_gl, (ad->itc_listmore),
869                                         sel_mem, ad->gl_result_group_item[type],
870                                         ELM_GENLIST_ITEM_NONE,
871                                         search_gl_result_more_item_append_cb,
872                                         ad);
873         }
874
875 out_func:
876         SEARCH_FUNC_END;
877         return SEARCH_RET_SEARCH_SUCCESS;
878 }
879
880
881 int search_sql_image_search_result_by_api(int type, int offset, int limit,
882                                          void *data)
883 {
884         SEARCH_FUNC_START;
885
886         int ret;
887
888         ret = __search_sql_media_search_result_by_api(type,  
889                                                                                                 offset, 
890                                                                                                 limit, 
891                                                                                                 MINFO_ITEM_IMAGE, 
892                                                                                                 data);
893
894         SEARCH_FUNC_END;
895
896         return ret;
897 }
898
899 int search_sql_video_search_result_by_api(int type, int offset, int limit,
900                                          void *data)
901 {
902         SEARCH_FUNC_START;
903
904         int ret;
905
906         ret = __search_sql_media_search_result_by_api(type,  
907                                                                                                 offset, 
908                                                                                                 limit, 
909                                                                                                 MINFO_ITEM_VIDEO, 
910                                                                                                 data);
911
912         SEARCH_FUNC_END;
913
914         return ret;
915 }
916
917 int search_sql_music_search_result_by_api(int type, int offset, int limit,
918                                          void *data)
919 {
920         SEARCH_FUNC_START;
921
922         struct appdata *ad = (struct appdata *)data;
923
924         int loop_cnt = 0;
925         int ret = SEARCH_RET_SEARCH_NONE;
926         int i = 0;
927         int rc = 0;
928
929         struct search_item_sel *sel_mem = NULL;
930         Elm_Object_Item *git = NULL;
931
932         struct search_item_sel *sel_mem_info = NULL;
933         bool is_word_in_str;
934         
935         AudioHandleType *handle = NULL;
936
937         rc = audio_svc_search_item_new(&handle, limit);
938         if (rc < 0) {
939                 SEARCH_DEBUG_WARNING("audio_svc_search_item_new failed:%d\n", 
940                                                         rc);
941                 return -1;
942         }
943
944         rc = audio_svc_list_by_search(ad->media_handle,
945                         handle,
946                         AUDIO_SVC_ORDER_BY_TITLE_ASC, 
947                         offset, 
948                         limit, 
949                         AUDIO_SVC_SEARCH_TITLE, 
950                         ad->search_word, 
951                         strlen(ad->search_word), 
952                         AUDIO_SVC_SEARCH_ALBUM, 
953                         ad->search_word, 
954                         strlen(ad->search_word), 
955                         AUDIO_SVC_SEARCH_ARTIST, 
956                         ad->search_word, 
957                         strlen(ad->search_word), 
958                         -1);
959
960         if (rc != AUDIO_SVC_ERROR_NONE) {
961                 SEARCH_DEBUG_WARNING("audio_svc_list_by_search error : %d", 
962                                                         rc);
963
964                 ret = SEARCH_RET_SEARCH_FAIL;
965                 goto out_func;
966         }
967
968         for (loop_cnt = 0; loop_cnt < limit; loop_cnt++) {
969                 AudioHandleType *item = NULL;
970                 char *title = NULL;
971                 char *artist = NULL;
972                 char *pathname = NULL;
973                 char *album = NULL;
974                 int size = 0;
975                 char* audio_id = NULL;
976                 
977                 rc = audio_svc_search_item_get(handle, loop_cnt, &item);
978                 if (rc < 0) {
979                         break;
980                 } else {
981                         if (loop_cnt == limit -1)
982                         break;
983
984                         audio_svc_item_get_val(item,
985                                         AUDIO_SVC_TRACK_DATA_AUDIO_ID, &audio_id, &size,
986                                         AUDIO_SVC_TRACK_DATA_PATHNAME, &pathname, &size,
987                                         AUDIO_SVC_TRACK_DATA_TITLE, &title, &size,
988                                         AUDIO_SVC_TRACK_DATA_ARTIST, &artist, &size,
989                                         AUDIO_SVC_TRACK_DATA_ALBUM, &album, &size,
990                                         -1);
991
992                         if( audio_id == NULL) {
993                                 SEARCH_DEBUG_WARNING("audio_id value error. loop break");
994                                 break;
995                         }       
996
997                         ret = SEARCH_RET_SEARCH_SUCCESS;
998
999                         if ((loop_cnt == 0) && (offset == 0)) {
1000                                         /* Set group title */
1001                                         sel_mem = (struct search_item_sel *)calloc(1,
1002                                                         sizeof(struct search_item_sel));
1003
1004                                         sel_mem->type = type;
1005
1006                                         git = elm_genlist_item_append(ad->search_gl,
1007                                                                                 (ad->itc_grouptitle),
1008                                                                                 sel_mem,
1009                                                                                 (Elm_Object_Item *) NULL,
1010                                                                                 ELM_GENLIST_ITEM_GROUP,
1011                                                                                 NULL, NULL);
1012
1013                                         ad->gl_result_group_item[type] = git;
1014                         }
1015
1016                         sel_mem_info = (struct search_item_sel *)calloc(1,
1017                                                                      sizeof(struct search_item_sel));
1018
1019                         sel_mem_info->type = type;
1020
1021                         if ((title) && (strlen(title) > 0)) {
1022                                 char* markup_text_1line = elm_entry_utf8_to_markup(title);
1023
1024                                 strncpy(sel_mem_info->main_buf,
1025                                                 search_markup_keyword(markup_text_1line, ad->not_markup_search_word, &is_word_in_str),
1026                                                 MAX_LENGTH_PER_LINE - 1);
1027                                 SEARCH_FREE(markup_text_1line);
1028                         }       
1029
1030                         if (is_word_in_str) {
1031                                 if ((artist) && (strlen(artist) > 0)) { 
1032                                         char* markup_text_2line = elm_entry_utf8_to_markup(artist);
1033                                         
1034                                         strncpy(sel_mem_info->sub_buf,
1035                                                         search_markup_keyword(markup_text_2line,
1036                                                                                         ad->not_markup_search_word,
1037                                                                                         &is_word_in_str),
1038                                                         MAX_LENGTH_PER_LINE - 1);
1039                                         SEARCH_FREE(markup_text_2line);                         
1040                                 }       
1041                         } else {
1042                                 memset(sel_mem_info->sub_buf, '\0', MAX_LENGTH_PER_LINE);
1043
1044                                 if ((artist) && (strlen(artist) > 0)) { 
1045                                         char* markup_text_2line = elm_entry_utf8_to_markup(artist);
1046                                         
1047                                         strncpy(sel_mem_info->sub_buf,
1048                                                         search_markup_keyword(markup_text_2line,
1049                                                                                                         ad->not_markup_search_word,
1050                                                                                                         &is_word_in_str),
1051                                                         MAX_LENGTH_PER_LINE - 1);
1052                                         SEARCH_FREE(markup_text_2line);                         
1053                                 }       
1054                                                 
1055                                 if (is_word_in_str == false && (album) && (strlen(album) > 0)) {
1056                                         char* markup_text_2line = elm_entry_utf8_to_markup(album);
1057
1058                                         strncpy(sel_mem_info->sub_buf,
1059                                                         search_markup_keyword(markup_text_2line,
1060                                                                                                         ad->not_markup_search_word,
1061                                                                                                         &is_word_in_str),
1062                                                         MAX_LENGTH_PER_LINE - 1);
1063                                         SEARCH_FREE(markup_text_2line);                                 
1064                                 }       
1065
1066                                 if (is_word_in_str == false) {
1067                                         SEARCH_DEBUG_LOG("search_result is 0. No match keyword in title,album,artist string");
1068                                 }
1069                         }
1070
1071                         snprintf(sel_mem_info->main_id,
1072                                         MAX_LENGTH_PER_ID - 1, "%s", audio_id);
1073
1074                         strncpy(sel_mem_info->path, pathname, MAX_LENGTH_PER_PATH);
1075
1076                         elm_genlist_item_append(ad->search_gl,
1077                                                 (ad->itc_pslist_2line),
1078                                                 sel_mem_info,
1079                                                 ad->gl_result_group_item[type],
1080                                                 ELM_GENLIST_ITEM_NONE,
1081                                                 search_gl_phone_result_sel,
1082                                                 ad);
1083                         sel_mem_info = NULL;
1084                 }
1085         }
1086
1087         /* Set more title to genlist (under 'search all') */
1088         if (loop_cnt == limit) {
1089                 ad->cate_info[type].next_point = offset + loop_cnt - 1;
1090
1091                 sel_mem =
1092                     (struct search_item_sel *)calloc(1,
1093                                                      sizeof(struct search_item_sel));
1094                 sel_mem->type = type;
1095
1096                 elm_genlist_item_append(ad->search_gl, (ad->itc_listmore),
1097                                         sel_mem, ad->gl_result_group_item[type],
1098                                         ELM_GENLIST_ITEM_NONE,
1099                                         search_gl_result_more_item_append_cb,
1100                                         ad);
1101         }
1102         
1103 out_func:       
1104
1105         audio_svc_search_item_free(handle);
1106
1107         SEARCH_FUNC_END;
1108
1109         return ret;
1110 }
1111
1112 ail_cb_ret_e __search_sql_menu_search_result_cb(
1113                                         const ail_appinfo_h appinfo, 
1114                                         void *data)
1115 {
1116         SEARCH_FUNC_START;
1117         
1118         struct appdata *ad = (struct appdata *)data;
1119
1120         struct search_item_sel *sel_mem = NULL;
1121         struct search_item_sel *sel_mem_info = NULL;
1122
1123         int type = SEARCH_TYPE_PHONE_MENU;
1124
1125         bool is_word_in_str;
1126         ail_cb_ret_e ret;
1127
1128         if(ad->cate_info[type].loop_cnt < ad->cate_info[type].next_point) {
1129                 ad->cate_info[type].loop_cnt++;
1130                 return AIL_CB_RET_CONTINUE;
1131         }
1132
1133         if(ad->cate_info[type].next_point <
1134             ad->cate_info[type].offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
1135         
1136                 sel_mem_info = (struct search_item_sel *)calloc(1, sizeof(struct search_item_sel));
1137                 sel_mem_info->type = type;
1138
1139                 /********** START : set search result to sel_mem_info buf ************/
1140
1141                 char *name = NULL;
1142                 char *package = NULL;
1143
1144                 ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
1145                 if ((package) && (strlen(package) > 0)) {
1146
1147                         if(strcmp(package, SEARCHPKG) == 0) {
1148                                 ad->cate_info[type].loop_cnt++;
1149                                 SEARCH_FREE(sel_mem_info);
1150                                 return AIL_CB_RET_CONTINUE;
1151                         }
1152
1153                         strncpy(sel_mem_info->path, package, MAX_LENGTH_PER_PATH - 1);
1154                 }
1155
1156                 ail_appinfo_get_str(appinfo, AIL_PROP_NAME_STR, &name);
1157                 if ((name) && (strlen(name) > 0)) {
1158                         char* markup_text_1line = elm_entry_utf8_to_markup(name);
1159                         
1160                         strncpy(sel_mem_info->main_buf,
1161                                         search_markup_keyword(markup_text_1line, ad->not_markup_search_word,&is_word_in_str),
1162                                         MAX_LENGTH_PER_LINE - 1);
1163                         SEARCH_FREE(markup_text_1line);
1164                         
1165                         SEARCH_DEBUG_LOG("name : %s", name);
1166                 } else {
1167                         SEARCH_DEBUG_LOG("name : null");
1168 }
1169 if(ad->cate_info[type].next_point == 0) {
1170                         /* Set group title */
1171                         sel_mem = (struct search_item_sel *)calloc(1, sizeof(struct search_item_sel));
1172                         sel_mem->type = type;
1173                         ad->gl_result_group_item[type] = elm_genlist_item_append(
1174                                                                                                 ad->search_gl,
1175                                                                                             (ad->itc_grouptitle),
1176                                                                                             sel_mem,
1177                                                                                             (Elm_Object_Item *) NULL,
1178                                                                                             ELM_GENLIST_ITEM_GROUP,
1179                                                                                             NULL,
1180                                                                                             NULL);
1181                 }
1182
1183                 /********** END : set search result to sel_mem_info buf *************/
1184
1185                 elm_genlist_item_append(ad->search_gl, 
1186                                         (ad->itc_pslist_1line),
1187                                         sel_mem_info,
1188                                         ad->gl_result_group_item[type],
1189                                         ELM_GENLIST_ITEM_NONE,
1190                                         search_gl_phone_result_sel, 
1191                                         ad);
1192
1193                 ad->cate_info[type].next_point++;
1194                 ad->cate_info[type].loop_cnt++;
1195
1196                 ret = AIL_CB_RET_CONTINUE;
1197                 
1198         } else if (ad->cate_info[type].next_point ==
1199                                 ad->cate_info[type].offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
1200
1201                 /* Check that only "SMARTSEARCH" is left in more result */
1202                 if(ad->cate_info[type].next_point == ad->cate_info[type].result_cnt-1) {
1203
1204                         char *package = NULL;
1205
1206                         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
1207                         if ((package) && (strlen(package) > 0) && (strcmp(package, SEARCHPKG) == 0)) {
1208                                 return AIL_CB_RET_CANCEL;
1209                         }
1210                 }
1211
1212                 /* Set more title to genlist (under 'search all') */
1213                 sel_mem = (struct search_item_sel *)calloc(1, sizeof(struct search_item_sel));
1214                 sel_mem->type = type;
1215
1216                 elm_genlist_item_append(ad->search_gl, (ad->itc_listmore),
1217                                         sel_mem, ad->gl_result_group_item[type],
1218                                         ELM_GENLIST_ITEM_NONE,
1219                                         search_gl_result_more_item_append_cb,
1220                                         ad);
1221
1222                 ret = AIL_CB_RET_CANCEL;
1223         } else {
1224                 SEARCH_DEBUG_WARNING
1225                     ("__search_sql_memo_search_result_cb wrong index : %d",
1226                      ad->cate_info[type].next_point);
1227
1228                 ret = AIL_CB_RET_CANCEL;
1229         }
1230
1231         SEARCH_FUNC_END;
1232
1233         return ret;
1234 }
1235
1236 int search_sql_menu_search_result_by_api(int type, int offset, int limit,
1237                                          char *keyword, void *data)
1238 {
1239         SEARCH_FUNC_START;
1240
1241         struct appdata *ad = (struct appdata *)data;
1242
1243         int cnt = 0;
1244         int ret = 0;
1245
1246         ail_filter_h filter;
1247         ail_error_e rc;
1248
1249         rc = ail_filter_new(&filter);
1250         if (rc != AIL_ERROR_OK) {
1251                 SEARCH_DEBUG_WARNING("menu search - ail_filter_new error : %d", rc);
1252                 return SEARCH_RET_SEARCH_FAIL;
1253         }
1254
1255         rc = ail_filter_add_str(filter, AIL_PROP_NAME_STR, keyword);
1256         if (rc != AIL_ERROR_OK) {
1257                 SEARCH_DEBUG_WARNING("menu search - ail_filter_add_str error : %d", rc);
1258                 ret = SEARCH_RET_SEARCH_FAIL;
1259                 goto out_func;
1260         }
1261
1262         rc = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
1263         if (rc != AIL_ERROR_OK) {
1264                 SEARCH_DEBUG_WARNING("menu search - ail_filter_add_bool error : %d", rc);
1265                 ret = SEARCH_RET_SEARCH_FAIL;
1266                 goto out_func;
1267         }
1268
1269         rc = ail_filter_count_appinfo(filter, &cnt);
1270         if( rc != AIL_ERROR_OK) {
1271                 SEARCH_DEBUG_WARNING("menu search - ail_filter_count_appinfo error : %d", rc);
1272                 ret = SEARCH_RET_SEARCH_FAIL;
1273                 goto out_func;
1274         }
1275
1276         ad->cate_info[type].result_cnt = cnt;
1277
1278         if(cnt > 0) {
1279                 ad->cate_info[type].loop_cnt = 0;
1280                 rc = ail_filter_list_appinfo_foreach(filter, __search_sql_menu_search_result_cb, data);
1281                 if( rc != AIL_ERROR_OK) {
1282                         SEARCH_DEBUG_WARNING("menu search - ail_filter_list_appinfo_foreach error : %d", rc);
1283                         ret = SEARCH_RET_SEARCH_FAIL;
1284                         goto out_func;
1285                 }
1286
1287                 if(ad->cate_info[type].next_point == 0) {
1288                         ret = SEARCH_RET_SEARCH_NONE;
1289                 } else {
1290                         ret = SEARCH_RET_SEARCH_SUCCESS;
1291                 }
1292         } else {
1293                 ret = SEARCH_RET_SEARCH_NONE;
1294         }
1295
1296 out_func:
1297         ail_filter_destroy(filter);
1298
1299         SEARCH_FUNC_END;
1300
1301         return ret;
1302 }
1303
1304 int search_sql_msg_search_result_by_api(int type, int offset, int limit,
1305                                          void *data)
1306 {
1307         SEARCH_FUNC_START;
1308
1309         struct appdata *ad = (struct appdata *)data;
1310
1311         int loop_cnt = 0;
1312         int ret = 0;
1313
1314         struct search_item_sel *sel_mem = NULL;
1315         Elm_Object_Item *git = NULL;
1316
1317         struct search_item_sel *sel_mem_info = NULL;
1318         bool is_word_in_str;
1319
1320         Evas_Object *glist;
1321
1322         MSG_HANDLE_T msgHandle = NULL;
1323         MSG_ERROR_T err = MSG_SUCCESS;
1324         MSG_LIST_S msgList;
1325         int i = 0;
1326         int j = 0;
1327         bool is_more_btn = false;
1328         bool is_word_in_title = false;
1329
1330         int address_cnt = 0;
1331
1332         MSG_SEARCH_CONDITION_S searchCon = {0,};
1333
1334         searchCon.msgType = MSG_TYPE_INVALID;
1335         searchCon.folderId = MSG_ALLBOX_ID;
1336         searchCon.pSearchVal = ad->search_word;
1337
1338         err = msg_search_message(ad->msg_handle, &searchCon, offset, limit, &msgList);
1339
1340         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
1341                 SEARCH_DEBUG_WARNING("msg_search_message is failed : %d", err);
1342                 return SEARCH_RET_SEARCH_FAIL;
1343         }
1344
1345         if (msgList.nCount <= 0) {
1346                 SEARCH_DEBUG_WARNING("no msg search result");
1347                 ret = SEARCH_RET_SEARCH_NONE;
1348         }  else  {
1349
1350                 /* Set group title */
1351                 glist = ad->search_gl;
1352
1353                 if (offset == 0) {
1354                         sel_mem =
1355                             (struct search_item_sel *)calloc(1,
1356                                                              sizeof(struct
1357                                                                     search_item_sel));
1358                         sel_mem->type = type;
1359
1360                         git =
1361                             elm_genlist_item_append(glist,
1362                                                     (ad->itc_grouptitle),
1363                                                     sel_mem,
1364                                                     (Elm_Object_Item *) NULL,
1365                                                     ELM_GENLIST_ITEM_GROUP,
1366                                                     NULL, NULL);
1367
1368                         ad->gl_result_group_item[type] = git;
1369                 }
1370
1371                 /* Set search result to genlist */
1372
1373                 if(msgList.nCount < limit) {
1374                         loop_cnt = msgList.nCount;
1375                         is_more_btn = false;
1376                 } else {
1377                         loop_cnt = limit-1;
1378                         is_more_btn = true;
1379                 }
1380
1381                 /********** START : set search result to sel_mem_info buf ************/
1382
1383                 for (i = 0; i < loop_cnt; i++) {
1384                         const char *text_1line = NULL;
1385                         const char *text_2line = NULL;
1386                         const char *msg_address = NULL;
1387                         const char *markup_keyword = NULL;
1388
1389                         sel_mem_info = (struct search_item_sel *)
1390                                                         calloc(1,sizeof(struct search_item_sel));
1391                         sel_mem_info->type = type;
1392
1393                         snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID - 1, "%d", msg_get_message_id(msgList.msgInfo[i]));
1394
1395                         /* Set 1st line string */
1396                         if(msg_is_mms(msgList.msgInfo[i])) {
1397                                 /* MMS case */                          
1398                                 char* markup_text_1line = NULL;
1399                                 
1400                                 text_1line = msg_get_subject(msgList.msgInfo[i]);
1401                                 markup_text_1line = elm_entry_utf8_to_markup(text_1line);
1402                                 
1403                                 markup_keyword = search_markup_keyword(markup_text_1line,
1404                                                                                                                 ad->not_markup_search_word,
1405                                                                                                         &is_word_in_title);
1406
1407                                 if (!is_word_in_title) {
1408                                         text_1line = msg_mms_get_text_contents(msgList.msgInfo[i]);
1409                                         markup_text_1line = elm_entry_utf8_to_markup(text_1line);                                               
1410                                         markup_keyword = search_markup_keyword(markup_text_1line,
1411                                                                                                                         ad->not_markup_search_word,
1412                                                                                                                 &is_word_in_title);
1413                         }       
1414
1415                                 if ((text_1line) && (strlen(text_1line) > 0 ))
1416                                         strncpy(sel_mem_info->main_buf, markup_keyword, MAX_LENGTH_PER_LINE - 1);
1417
1418                                 SEARCH_FREE(markup_text_1line);
1419                         } else {
1420                                 /* SMS Case */
1421                                 char* markup_text_1line = NULL;                         
1422                                 
1423                                 text_1line = msg_sms_get_message_body(msgList.msgInfo[i]);
1424                                 markup_text_1line = elm_entry_utf8_to_markup(text_1line);
1425                                 markup_keyword = search_markup_keyword(markup_text_1line,
1426                                                                                                                 ad->not_markup_search_word,
1427                                                                                                         &is_word_in_title);
1428
1429                                 if ((text_1line) && (strlen(text_1line) > 0))
1430                                         strncpy(sel_mem_info->main_buf, markup_keyword, MAX_LENGTH_PER_LINE - 1);
1431
1432                                 SEARCH_FREE(markup_text_1line);                         
1433                         }
1434
1435                         /* Set 2nd line string */
1436                         address_cnt = msg_get_address_count(msgList.msgInfo[i]);
1437
1438                         for(j = 0; j < address_cnt; j++) {
1439
1440                                 if(msg_get_ith_contact_id(msgList.msgInfo[i],j) > 0) {
1441                                         text_2line = msg_get_ith_name(msgList.msgInfo[i],j);
1442                                 } else {
1443                                         text_2line = msg_get_ith_address(msgList.msgInfo[i],j);
1444                                 }
1445
1446                                 if ((text_2line) && (strlen(text_2line) > 0)) {
1447                                         char* markup_text_2line = NULL;
1448
1449                                         markup_text_2line = elm_entry_utf8_to_markup(text_2line);               
1450                                         strncpy(sel_mem_info->sub_buf,
1451                                                         search_markup_keyword(markup_text_2line,
1452                                                                                                 ad->not_markup_search_word,
1453                                                                                         &is_word_in_str),
1454                                                         MAX_LENGTH_PER_LINE - 1);
1455                                         SEARCH_FREE(markup_text_2line);                                 
1456                                 }
1457
1458                                 if ((is_word_in_title) || (is_word_in_str)) {
1459                                         break;
1460                                 }
1461                         }
1462
1463                         elm_genlist_item_append(glist,
1464                                                         (ad->itc_pslist_2line),
1465                                                         sel_mem_info,
1466                                                         ad->gl_result_group_item[type],
1467                                                         ELM_GENLIST_ITEM_NONE,
1468                                                         search_gl_phone_result_sel,
1469                                                         ad);
1470                                 sel_mem_info = NULL;
1471                 }
1472
1473                 msg_release_message_list(&msgList);
1474  
1475                 /* Set more title to genlist (under 'search all') */
1476                 if (is_more_btn) {
1477                         ad->cate_info[type].next_point = offset + limit - 1;
1478
1479                         sel_mem = (struct search_item_sel *)
1480                                         calloc(1,sizeof(struct search_item_sel));
1481                         sel_mem->type = type;
1482
1483                         elm_genlist_item_append(ad->search_gl, (ad->itc_listmore),
1484                                                 sel_mem, ad->gl_result_group_item[type],
1485                                                 ELM_GENLIST_ITEM_NONE,
1486                                                 search_gl_result_more_item_append_cb,
1487                                                 ad);
1488                 }
1489
1490                 ret = SEARCH_RET_SEARCH_SUCCESS;
1491         }
1492
1493         SEARCH_FUNC_END;
1494         return ret;
1495 }       
1496