ff74b422b40a2e15604e22e71fd8b6fc4b72b7bc
[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-api-mail.h>
31
32 #include <favorites.h>
33 #include <media_content.h>
34
35 #include <msg_storage.h>
36 #include <msg_storage_types.h>
37 #include <msg.h>
38
39 #include <ail.h>
40 #include <unicode/ucal.h>
41 #include <unicode/ustring.h>
42 #include <unicode/ustdio.h>
43 #include <unicode/udat.h>
44
45 char* g_keyword = NULL;
46
47 struct search_cb_data {
48         struct search_content_object *obj;
49         struct appdata *ad;
50         char *keyword;
51         struct search_genlist_info *result_info;
52 };
53
54 struct search_cb_data_browser {
55         struct search_content_object *obj;
56         struct appdata *ad;
57         char *keyword;
58         struct search_genlist_info *result_info;
59         struct search_more_result *info;
60 };
61
62 struct media_cb_data {
63         bool is_sdcard;
64         char *dirname;
65 };
66
67 int search_sql_stmt_init(sqlite3_stmt * sql_stmt)
68 {
69         SEARCH_FUNC_START;
70
71         int rc = 0;
72
73         rc = sqlite3_clear_bindings(sql_stmt);
74         if (rc != SQLITE_OK) {
75                 SEARCH_DEBUG_ERROR("sqlite3_clear_bindings error");
76                 return SEARCH_RET_FAIL;
77         }
78
79         rc = sqlite3_reset(sql_stmt);
80         if (rc != SQLITE_OK) {
81                 SEARCH_DEBUG_ERROR("sqlite3_reset error");
82                 return SEARCH_RET_FAIL;
83         }
84
85         SEARCH_FUNC_END;
86
87         return SEARCH_RET_SUCCESS;
88 }
89
90 int search_sql_stmt_finalize(sqlite3_stmt * stmt)
91 {
92         SEARCH_FUNC_START;
93
94         int ret;
95
96         ret = sqlite3_finalize(stmt);
97         if (ret != SQLITE_OK) {
98                 SEARCH_DEBUG_ERROR("sqlite3_finalize error : %d ", ret);
99                 return -1;
100         }
101
102         SEARCH_FUNC_END;
103
104         return 0;
105 }
106
107 int search_sql_prepare_stmt(sqlite3 *db_handle, const char *query,
108                                          sqlite3_stmt ** stmt_out)
109 {
110         SEARCH_FUNC_START;
111
112         int rc = 0;
113         int ret = 0;
114
115         rc = sqlite3_prepare(db_handle, query, strlen(query), stmt_out, NULL);
116         if (rc != SQLITE_OK) {
117                 SEARCH_DEBUG_ERROR("sql preapre error : %d / %s", rc, query);
118                 ret = SEARCH_RET_FAIL;
119         } else {
120                 ret = SEARCH_RET_SUCCESS;
121         }
122
123         SEARCH_FUNC_END;
124
125         return ret;
126 }
127
128 int search_sql_bind_text(sqlite3* db_handle, sqlite3_stmt *sql_stmt, int col, char *data)
129 {
130         int rc = 0;
131         int ret = SEARCH_RET_SEARCH_NONE;
132
133         rc = sqlite3_bind_text(sql_stmt, col, data, strlen(data), SQLITE_STATIC);
134         if (rc != SQLITE_OK) {
135                 SEARCH_DEBUG_ERROR("SQL BIND TEXT ERROR : %s(%d) / %s\n",
136                              sqlite3_errmsg(db_handle),
137                              rc,
138                              data);
139                 ret = SEARCH_RET_SEARCH_FAIL;
140         }
141
142         return ret;
143 }
144
145 int search_sql_bind_int(sqlite3* db_handle, sqlite3_stmt *sql_stmt, int col, int data)
146 {
147         int rc = 0;
148         int ret = SEARCH_RET_SEARCH_NONE;
149
150         rc = sqlite3_bind_int(sql_stmt, col, data);
151         if (rc != SQLITE_OK) {
152                 SEARCH_DEBUG_ERROR("SQL BIND INT ERROR : %s(%d) / %d\n",
153                              sqlite3_errmsg(db_handle),
154                              rc,
155                              data);
156                 ret = SEARCH_RET_SEARCH_FAIL;
157         }
158         return ret;
159 }
160
161 static int __search_sql_contact_search_result_cb(CTSvalue * value,
162                                                  void *user_data)
163 {
164         SEARCH_FUNC_START;
165
166         struct search_cb_data *scd = (struct search_cb_data*)user_data;
167
168         struct appdata *ad = scd->ad;
169         struct search_content_object* obj = scd->obj;
170         struct search_genlist_info *result_info = scd->result_info;
171         char *keyword = scd->keyword;
172
173         struct search_item_sel *sel_mem_info = NULL;
174         struct search_item_more_sel *sel_mem_more = NULL;
175         struct search_genlist_item_info *gen_item = NULL;
176
177         const char* contact_img_path = NULL;
178         char display_name[128] = { 0, };
179         const char *first, *last, *display, *number;
180
181         bool is_word_in_str;
182
183         if (obj->mr_info.next_point == 0) {
184                 //### Set group title
185                 result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
186                                                                                                         obj,
187                                                                                                         ad->itc_grouptitle,
188                                                                                                 obj,
189                                                                                                         NULL);
190         }
191
192         if (obj->mr_info.next_point <
193             obj->mr_info.offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
194                 sel_mem_info =
195                     (struct search_item_sel *)calloc(1,
196                                                      sizeof(struct
197                                                             search_item_sel));
198                 sel_mem_info->type = obj->contents_type;
199
200                 contact_img_path = contacts_svc_value_get_str(value,
201                                                                                 CTS_LIST_NUM_CONTACT_IMG_PATH_STR);
202
203                 if (contact_img_path != NULL) {
204                         strncpy(sel_mem_info->icon1_path, contact_img_path,
205                                         MAX_LENGTH_PER_PATH - 1);
206         }
207                 else if (obj->obj_icon_path)
208                         strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
209                                         MAX_LENGTH_PER_PATH - 1);
210
211                 /************ START : set search result to sel_mem_info buf ************/
212
213                 /***********************************************************************/
214
215                 memset(display_name, 0x00, sizeof(display_name));
216
217                 display =
218                     contacts_svc_value_get_str(value,
219                                                CTS_LIST_NUM_CONTACT_DISPLAY_STR);
220
221                 if (display) {
222                         snprintf(display_name, sizeof(display_name), "%s",
223                                  display);
224                 } else {
225                         first =
226                             contacts_svc_value_get_str(value,
227                                                        CTS_LIST_NUM_CONTACT_FIRST_STR);
228                         last =
229                             contacts_svc_value_get_str(value,
230                                                        CTS_LIST_NUM_CONTACT_LAST_STR);
231
232                         if (CTS_ORDER_NAME_FIRSTLAST ==
233                             contacts_svc_get_order(CTS_ORDER_OF_DISPLAY)) {
234                                 if (first) {
235                                         if (last) {
236                                                 snprintf(display_name,
237                                                          sizeof(display_name),
238                                                          "%s %s", first, last);
239                                         } else {
240                                                 snprintf(display_name,
241                                                          sizeof(display_name),
242                                                          "%s", first);
243                                         }
244                                 } else {
245                                         if (last) {
246                                                 snprintf(display_name,
247                                                          sizeof(display_name),
248                                                          "%s", last);
249                                         }
250                                 }
251                         } else {
252                                 if (last) {
253                                         if (first) {
254                                                 snprintf(display_name,
255                                                          sizeof(display_name),
256                                                          "%s %s", last, first);
257                                         } else {
258                                                 snprintf(display_name,
259                                                          sizeof(display_name),
260                                                          "%s", last);
261                                         }
262                                 } else {
263                                         if (first) {
264                                                 snprintf(display_name,
265                                                          sizeof(display_name),
266                                                          "%s", first);
267                                         }
268                                 }
269                         }
270                 }
271
272                 SEARCH_DEBUG_LOG("display_name : %s", display_name);
273
274                 if(strlen(display_name) > 0) {
275                         strncpy(sel_mem_info->main_buf,
276                                         search_markup_keyword(display_name, keyword, &is_word_in_str),
277                                         MAX_LENGTH_PER_LINE - 1);
278                 }
279
280                 /***********************************************************************/
281
282                 number = contacts_svc_value_get_str(value, CTS_LIST_NUM_NUMBER_STR);
283
284                 if((number) && strlen(number) > 0) {
285                         strncpy(sel_mem_info->sub_buf,
286                                         search_markup_keyword(number, keyword, &is_word_in_str),
287                                         MAX_LENGTH_PER_LINE - 1);
288                 }
289
290                 /***********************************************************************/
291
292                 snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID - 1, "%d",
293                          contacts_svc_value_get_int(value,
294                                                     CTS_LIST_NUM_CONTACT_ID_INT));
295                 SEARCH_DEBUG_LOG("Search CT_ID: %d", sel_mem_info->main_id);
296                 /***********************************************************************/
297                 /************ END : set search result to sel_mem_info buf **************/
298                 gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
299                                                                                 obj,
300                                                                                 ad->itc_pslist_2line_1icon,
301                                         sel_mem_info,
302                                                                                 obj->gl_result_grp);
303                 result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
304                 obj->mr_info.next_point++;
305         } else if (obj->mr_info.next_point ==
306                    obj->mr_info.offset +
307                    SEARCH_CATEGORY_LIST_MORE_CNT) {
308                 /* Set more title to genlist (under 'search all') */
309                 sel_mem_more =
310                     (struct search_item_more_sel *)calloc(1,
311                                                      sizeof(struct
312                                                             search_item_more_sel));
313                 sel_mem_more->obj = obj;
314
315                 result_info->more = search_add_genlist_item(SEARCH_GENLIST_MORE,
316                                                                                                 obj,
317                                                                                                 ad->itc_listmore,
318                                                                                                 sel_mem_more,
319                                                                                                 obj->gl_result_grp);
320         } else {
321                 SEARCH_DEBUG_WARNING
322                     ("__search_sql_contact_search_result_cb wrong index : %d",
323                      obj->mr_info.next_point);
324         }
325
326         SEARCH_FUNC_END;
327
328         return 0;
329 }
330
331 int search_sql_contact_search_result_by_api(struct search_content_object *obj,
332                 int offset, int limit, char *keyword, void *data, struct search_genlist_info *result_info)
333 {
334         SEARCH_FUNC_START;
335
336         struct appdata *ad = (struct appdata *)data;
337         struct search_cb_data scd = {obj, ad, keyword, result_info};
338
339         int ret = 0;
340
341         /* Check whether search result is exist or not */
342         ret =
343             contacts_svc_smartsearch_excl(keyword, limit, offset,
344                                           __search_sql_contact_search_result_cb,
345                                           &scd);
346         if (ret != CTS_SUCCESS) {
347                 SEARCH_DEBUG_WARNING("contacts svc search return error : %d",
348                                      ret);
349                 return SEARCH_RET_SEARCH_FAIL;
350         }
351
352         if (obj->mr_info.next_point == 0) {
353                 SEARCH_DEBUG_WARNING("contacts svc search no result");
354                 return SEARCH_RET_SEARCH_NONE;
355         }
356
357         SEARCH_FUNC_END;
358
359         return SEARCH_RET_SEARCH_SUCCESS;
360 }
361
362 int search_sql_calendar_search_result_by_api(struct search_content_object *obj,
363                 int offset, int limit, char *keyword, void *data, struct search_genlist_info *result_info)
364 {
365         SEARCH_FUNC_START;
366
367         int ret;
368         struct appdata *ad = (struct appdata *)data;
369
370         cal_struct *event = NULL;
371         cal_iter *iter = NULL;
372
373         struct search_item_sel *sel_mem_info = NULL;
374         struct search_item_more_sel *sel_mem_more = NULL;
375         struct search_genlist_item_info *gen_item = NULL;
376
377         int loop_cnt = 0;
378
379         char *summary = NULL;
380         bool is_word_in_str;
381
382         int time_type = 0;
383
384         long long int start_time = 0;
385         UErrorCode status = U_ZERO_ERROR;
386         UCalendar *cal;
387         int y = 0, m = 0, d = 0;
388
389         ret = calendar_svc_smartsearch_excl(keyword, offset, limit, &iter);
390
391         if (ret == CAL_SUCCESS) {
392                 ret = calendar_svc_iter_next(iter);
393                 if (ret != CAL_SUCCESS) {
394                         SEARCH_DEBUG_WARNING
395                             ("calendar svc search no result : %d", ret);
396                         return SEARCH_RET_SEARCH_NONE;
397                 }
398                 /* Set group title */
399                 if (offset == 0) {
400                         result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
401                                                                                                                 obj,
402                                                                                                                 ad->itc_grouptitle,
403                                                                                                         obj,
404                                                                                                                 NULL);
405                 }
406                 /* Set search result to genlist */
407                 do {
408                         loop_cnt++;
409
410                         if (loop_cnt == limit) {
411                                 break;
412                         }
413                         /********** START : set search result to sel_mem_info buf ************/
414
415                         ret = calendar_svc_iter_get_info(iter, &event);
416
417                         if (ret == CAL_SUCCESS) {
418                                 sel_mem_info =
419                                     (struct search_item_sel *)calloc(1,
420                                                                      sizeof
421                                                                      (struct
422                                                                       search_item_sel));
423                                 sel_mem_info->type = obj->contents_type;
424
425                         if (obj->obj_icon_path)
426                                 strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
427                                                 MAX_LENGTH_PER_PATH - 1);
428
429                                 summary =
430                                     calendar_svc_struct_get_str(event,
431                                                                 CAL_VALUE_TXT_SUMMARY);
432
433                                 if ((summary) && (strlen(summary) > 0)) {
434                                         strncpy(sel_mem_info->main_buf,
435                                                 search_markup_keyword(summary,
436                                                                      keyword,
437                                                                              &is_word_in_str),
438                                                         MAX_LENGTH_PER_LINE - 1);
439                                 }
440
441                                 snprintf(sel_mem_info->main_id,
442                                          MAX_LENGTH_PER_ID - 1, "%d",
443                                          calendar_svc_struct_get_int(event,
444                                                                      CAL_VALUE_INT_INDEX));
445
446                                 time_type = calendar_svc_struct_get_int(event, CALS_VALUE_INT_DTSTART_TYPE);
447
448                                 if (time_type == CALS_TIME_LOCALTIME) {
449                                         y = calendar_svc_struct_get_int(event, CALS_VALUE_INT_DTSTART_YEAR);
450                                         m = calendar_svc_struct_get_int(event, CALS_VALUE_INT_DTSTART_MONTH);
451                                         d = calendar_svc_struct_get_int(event, CALS_VALUE_INT_DTSTART_MDAY);
452                                 } else if (time_type == CALS_TIME_UTIME) {
453                                         start_time = calendar_svc_struct_get_lli(event,
454                                                                         CALS_VALUE_LLI_DTEND_UTIME);
455                                         UChar *_tzid;
456                                         _tzid = (UChar *)malloc(sizeof(UChar) * (strlen("Europe/Berlin") + 1));
457                                         if (_tzid) u_uastrcpy(_tzid, "Europe/Berlin");
458
459                                         cal = ucal_open(_tzid, u_strlen(_tzid), "en_US", UCAL_TRADITIONAL, &status);
460                                         ucal_setMillis(cal, (double)(start_time * 1000.0), &status);
461                                         y = ucal_get(cal, UCAL_YEAR, &status);
462                                         m = ucal_get(cal, UCAL_MONTH, &status);
463                                         d = ucal_get(cal, UCAL_DATE, &status);
464                                         ucal_close(cal);
465                                 }
466                                 snprintf(sel_mem_info->sub_buf, MAX_LENGTH_PER_LINE, "%d.%d.%d", y,m,d);
467
468                                 calendar_svc_struct_free(&event);
469                                 summary = NULL;
470
471                                 gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
472                                                                                                 obj,
473                                                                                                 ad->itc_pslist_2line_1icon,
474                                                         sel_mem_info,
475                                                                                                 obj->gl_result_grp);
476                                 result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
477                         } else {
478                                 SEARCH_DEBUG_WARNING
479                                     ("calendar_svc_iter_get_info fail : %d",
480                                      ret);
481                                 break;
482                         }
483
484                 }
485                 while (calendar_svc_iter_next(iter) == CAL_SUCCESS);
486         } else {
487                 SEARCH_DEBUG_WARNING("calendar_svc_search_list fail : %d", ret);
488                 return SEARCH_RET_SEARCH_FAIL;
489         }
490
491         /* Set more title to genlist (under 'search all') */
492         if (loop_cnt == limit) {
493                 obj->mr_info.next_point = offset + limit - 1;
494
495                 sel_mem_more =
496                     (struct search_item_more_sel *)calloc(1,
497                                                      sizeof(struct
498                                                             search_item_more_sel));
499                 sel_mem_more->obj = obj;
500
501                 result_info->more = search_add_genlist_item(SEARCH_GENLIST_MORE,
502                                                                                                 obj,
503                                                                                                 ad->itc_listmore,
504                                                                                                 sel_mem_more,
505                                                                                                 obj->gl_result_grp);
506         }
507
508         SEARCH_FUNC_END;
509         return SEARCH_RET_SEARCH_SUCCESS;
510 }
511
512 static void __search_sql_memo_search_result_cb(memo_data_t * value,
513                                                void *user_data)
514 {
515         SEARCH_FUNC_START;
516
517         struct search_cb_data *scd = (struct search_cb_data *)user_data;
518         struct appdata *ad = scd->ad;
519         struct search_content_object *obj = scd->obj;
520         struct search_genlist_info *result_info = scd->result_info;
521
522         struct search_genlist_item_info *gen_item = NULL;
523         struct search_item_sel *sel_mem_info = NULL;
524         struct search_item_more_sel *sel_mem_more = NULL;
525
526         char *keyword = scd->keyword;
527
528         bool is_word_in_str;
529
530         if(!value) {
531                 SEARCH_DEBUG_WARNING("memo data value is null");
532                 return;
533         }
534
535         if (obj->mr_info.next_point == 0) {
536                 /* Set group title */
537                 result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
538                                                                                                         obj,
539                                                                                                         ad->itc_grouptitle,
540                                                                                                 obj,
541                                                                                                         NULL);
542         }
543
544         if (obj->mr_info.next_point <
545             obj->mr_info.offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
546                 sel_mem_info =
547                     (struct search_item_sel *)calloc(1,
548                                                      sizeof(struct
549                                                             search_item_sel));
550                 sel_mem_info->type = obj->contents_type;
551
552                 /********** START : set search result to sel_mem_info buf ************/
553
554                 if (obj->obj_icon_path)
555                         strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
556                                         MAX_LENGTH_PER_PATH - 1);
557
558                 /* First, We should check comment field. It means drawing memo
559                  * If comment field is NULL, then we check content field */
560                 if ((value->comment) && (strlen(value->comment) > 0)) {
561                         strncpy(sel_mem_info->main_buf,
562                                                 search_markup_keyword(value->comment, keyword,
563                                                                                         &is_word_in_str),
564                                         MAX_LENGTH_PER_LINE - 1);
565                 } else if ((value->content) && (strlen(value->content) > 0)) {
566                         strncpy(sel_mem_info->main_buf,
567                                 search_markup_keyword(value->content, keyword,
568                                                                                 &is_word_in_str),
569                                         MAX_LENGTH_PER_LINE - 1);
570                 }
571                 snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID - 1, "%d", value->id);
572
573                 search_util_date_time_format_get_val(localtime
574                                                      (&value->modi_time),
575                                                      sel_mem_info->sub_buf);
576
577                 /********** END : set search result to sel_mem_info buf *************/
578
579                 gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
580                                                                                 obj,
581                                                                                 ad->itc_pslist_2line_1icon,
582                                         sel_mem_info,
583                                                                                 obj->gl_result_grp);
584                 result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
585
586                 obj->mr_info.next_point++;
587         } else if (obj->mr_info.next_point ==
588                    obj->mr_info.offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
589                 /* Set more title to genlist (under 'search all') */
590                 sel_mem_more =
591                     (struct search_item_more_sel *)calloc(1,
592                                                      sizeof(struct
593                                                             search_item_more_sel));
594                 sel_mem_more->obj = obj;
595
596                 result_info->more = search_add_genlist_item(SEARCH_GENLIST_MORE,
597                                                                                                 obj,
598                                                                                                 ad->itc_listmore,
599                                                                                                 sel_mem_more,
600                                                                                                 obj->gl_result_grp);
601         } else {
602                 SEARCH_DEBUG_WARNING
603                     ("__search_sql_memo_search_result_cb wrong index : %d",
604                      obj->mr_info.next_point);
605         }
606
607         SEARCH_FUNC_END;
608
609         return;
610 }
611
612 int search_sql_memo_search_result_by_api(struct search_content_object *obj,
613                 int offset, int limit, char *keyword, void *data, struct search_genlist_info *result_info)
614 {
615         SEARCH_FUNC_START;
616
617         struct appdata *ad = (struct appdata *)data;
618         struct search_cb_data scd = {obj, ad, keyword, result_info};
619
620         memo_search_data(keyword,
621                                         limit, offset,
622                                         MEMO_SORT_TITLE_ASC,
623                                         __search_sql_memo_search_result_cb,
624                                         &scd);
625
626         if (obj->mr_info.next_point == 0) {
627                 SEARCH_DEBUG_WARNING("no memo search result");
628                 return SEARCH_RET_SEARCH_NONE;
629         }
630
631         SEARCH_FUNC_END;
632         return SEARCH_RET_SEARCH_SUCCESS;
633 }
634
635 int search_sql_email_search_result_by_api(struct search_content_object *obj,
636                 int offset, int limit, char *keyword, void *data, struct search_genlist_info *result_info)
637 {
638         SEARCH_FUNC_START;
639
640         struct appdata *ad = (struct appdata *)data;
641
642         int ret = EMAIL_ERROR_NONE;
643         int i = 0;
644         email_mail_list_item_t *mail_list = NULL;
645         email_list_filter_t *filter_list = NULL;
646         email_list_sorting_rule_t *sorting_rule_list = NULL;
647
648         int filter_list_count = 5;
649         int sorting_rule_list_count = 1;
650         int mail_count = 0;
651
652         struct search_genlist_item_info *gen_item = NULL;
653         struct search_item_sel *sel_mem_info = NULL;
654         struct search_item_more_sel *sel_mem_more = NULL;
655         bool is_word_in_str;
656
657         filter_list = (email_list_filter_t *)malloc(sizeof(email_list_filter_t) * filter_list_count);
658         memset(filter_list, 0 , sizeof(email_list_filter_t) * filter_list_count);
659
660         filter_list[0].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
661         filter_list[0].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_SUBJECT;
662         filter_list[0].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_INCLUDE;
663         filter_list[0].list_filter_item.rule.key_value.string_type_value   = keyword;
664         filter_list[0].list_filter_item.rule.case_sensitivity              = EMAIL_CASE_INSENSITIVE;
665
666         filter_list[1].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
667         filter_list[1].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_OR;
668
669         filter_list[2].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
670         filter_list[2].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_TO;
671         filter_list[2].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_INCLUDE;
672         filter_list[2].list_filter_item.rule.key_value.string_type_value   = keyword;
673         filter_list[2].list_filter_item.rule.case_sensitivity              = EMAIL_CASE_INSENSITIVE;
674
675         filter_list[3].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
676         filter_list[3].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_OR;
677
678         filter_list[4].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
679         filter_list[4].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_FROM;
680         filter_list[4].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_INCLUDE;
681         filter_list[4].list_filter_item.rule.key_value.string_type_value   = keyword;
682         filter_list[4].list_filter_item.rule.case_sensitivity              = EMAIL_CASE_INSENSITIVE;
683
684         sorting_rule_list = (email_list_sorting_rule_t *)malloc(sizeof(email_list_sorting_rule_t) * sorting_rule_list_count);
685         memset(sorting_rule_list, 0, sizeof(email_list_sorting_rule_t) * sorting_rule_list_count);
686
687         sorting_rule_list[0].target_attribute                              = EMAIL_MAIL_ATTRIBUTE_SUBJECT;
688         sorting_rule_list[0].sort_order                                    = EMAIL_SORT_ORDER_ASCEND;
689
690         ret = email_get_mail_list_ex(filter_list, filter_list_count, sorting_rule_list, sorting_rule_list_count,
691                                                         offset, limit, &mail_list, &mail_count);
692
693
694         if (ret != EMAIL_ERROR_NONE) {
695                 SEARCH_DEBUG_WARNING("no email search result");
696                 return SEARCH_RET_SEARCH_FAIL;
697         }
698
699         if (mail_count == 0) {
700                 SEARCH_DEBUG_WARNING("no email search result");
701                 return SEARCH_RET_SEARCH_NONE;
702         }
703
704         if(mail_list == NULL) {
705                 SEARCH_DEBUG_WARNING("mail_list is null");
706                 return SEARCH_RET_SEARCH_NONE;
707         }
708
709         /* Set group title */
710         if (offset == 0) {
711                 result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
712                                                                         obj,
713                                                                         ad->itc_grouptitle,
714                                                                         obj,
715                                                                         NULL);
716         }
717
718         /* Set search result to genlist */
719         for(i = 0 ; i < mail_count ; i++) {
720
721                 if(i == limit -1)
722                         break;
723
724                 sel_mem_info = (struct search_item_sel *)calloc(1,
725                                                                      sizeof(struct search_item_sel));
726
727                 sel_mem_info->type = obj->contents_type;
728
729                 if (obj->obj_icon_path)
730                         strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
731                                         MAX_LENGTH_PER_PATH - 1);
732
733                 snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID - 1, "%d", mail_list[i].mail_id);
734
735                 snprintf(sel_mem_info->path, MAX_LENGTH_PER_PATH - 1, "%d %d %d",
736                                 mail_list[i].account_id, mail_list[i].mail_id, mail_list[i].mailbox_id);
737
738                 SEARCH_DEBUG_LOG("subject : %s", mail_list[i].subject);
739
740                 if((mail_list[i].subject) && (strlen(mail_list[i].subject) > 0)) {
741                         strncpy(sel_mem_info->main_buf,
742                                         search_markup_keyword(mail_list[i].subject, ad->search_word, &is_word_in_str),
743                                         MAX_LENGTH_PER_LINE - 1);
744                 }
745
746                 if((mail_list[i].from) && (strlen(mail_list[i].from) > 0)) {
747                         strncpy(sel_mem_info->sub_buf,
748                                         search_markup_keyword(mail_list[i].from, ad->search_word, &is_word_in_str),
749                                         MAX_LENGTH_PER_LINE - 1);
750                 }
751
752                 if(is_word_in_str == false && (mail_list[i].recipients) && (strlen(mail_list[i].recipients) > 0)) {
753                         strncpy(sel_mem_info->sub_buf,
754                                         search_markup_keyword(mail_list[i].recipients, ad->search_word, &is_word_in_str),
755                                         MAX_LENGTH_PER_LINE - 1);
756                 }
757
758                 gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
759                                                         obj,
760                                                         ad->itc_pslist_2line_1icon,
761                                                 sel_mem_info,
762                                                         obj->gl_result_grp);
763                 result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
764         }
765
766         SEARCH_FREE(mail_list);
767
768         /* Set more title to genlist (under 'search all') */
769         if (mail_count == limit) {
770                 obj->mr_info.next_point = offset + mail_count - 1;
771
772                 sel_mem_more =
773                     (struct search_item_more_sel *)calloc(1,
774                                                      sizeof(struct search_item_more_sel));
775                 sel_mem_more->obj = obj;
776
777                 result_info->more = search_add_genlist_item(SEARCH_GENLIST_MORE,
778                                                                         obj,
779                                                                         ad->itc_listmore,
780                                                                         sel_mem_more,
781                                                                         obj->gl_result_grp);
782         }
783
784         SEARCH_FREE(filter_list);
785         SEARCH_FREE(sorting_rule_list);
786
787         SEARCH_FUNC_END;
788         return SEARCH_RET_SEARCH_SUCCESS;
789 }
790
791 static bool __search_media_info_cb(media_info_h media, void *user_data)
792 {
793         char *media_id = NULL;
794         char *media_name = NULL;
795         char *media_path = NULL;
796         char *media_thumbnail = NULL;
797
798         struct search_item_sel *sel_mem_info = NULL;
799         Elm_Genlist_Item_Class *itc_style = NULL;
800         bool is_word_in_str = false;
801
802         struct search_cb_data *scd = (struct search_cb_data*)user_data;
803         struct appdata *ad = scd->ad;
804         struct search_content_object *obj = scd->obj;
805         struct search_genlist_info *result_info = scd->result_info;
806         char *keyword = scd->keyword;
807
808         struct search_genlist_item_info *gen_item = NULL;
809
810         media_info_get_media_id(media, &media_id);
811         media_info_get_display_name(media, &media_name);
812         media_info_get_file_path(media, &media_path);
813         media_info_get_thumbnail_path(media, &media_thumbnail);
814
815         sel_mem_info = (struct search_item_sel *)calloc(1,
816                                                      sizeof(struct search_item_sel));
817
818         sel_mem_info->type = obj->contents_type;
819
820         if ((media_thumbnail) && (strlen(media_thumbnail) > 0)
821                 && ecore_file_exists(media_thumbnail) == EINA_TRUE) {
822                 strncpy(sel_mem_info->icon1_path, media_thumbnail,
823                                 MAX_LENGTH_PER_PATH - 1);
824         } else if (obj->obj_icon_path)
825                 strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
826                                 MAX_LENGTH_PER_PATH - 1);
827
828         if (media_name && strlen(media_name)) {
829                 strncpy(sel_mem_info->main_buf,
830                                 search_markup_keyword(media_name,
831                                                                         keyword,
832                                                                         &is_word_in_str),
833                                 MAX_LENGTH_PER_LINE - 1);
834         }
835
836         if (media_id)
837                 snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID, "%s",
838                          media_id);
839
840         if (media_path)
841                 snprintf(sel_mem_info->path, MAX_LENGTH_PER_PATH - 1, "%s",
842                          media_path);
843
844         itc_style = ad->itc_pslist_1line_1icon;
845
846         /* Add Info to genlist */
847         gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
848                                         obj,
849                                         itc_style,
850                                         sel_mem_info,
851                                         obj->gl_result_grp);
852         result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
853
854         SEARCH_FREE(media_id);
855         SEARCH_FREE(media_name);
856         SEARCH_FREE(media_thumbnail);
857         SEARCH_FREE(media_path);
858
859         return true;
860         }
861
862 static bool  __search_sql_music_info_cb(media_info_h media, void *user_data)
863 {
864         char *media_id = NULL;
865         char *media_path = NULL;
866         char *media_thumbnail = NULL;
867         char *title = NULL;
868         char *artist = NULL;
869         char *album = NULL;
870
871         audio_meta_h a_handle;
872
873         media_content_storage_e storage_type = MEDIA_CONTENT_STORAGE_INTERNAL;
874
875         struct search_item_sel *sel_mem_info = NULL;
876         Elm_Genlist_Item_Class *itc_style = NULL;
877         bool is_word_in_str = false;
878
879         struct search_cb_data *scd = (struct search_cb_data*)user_data;
880         struct appdata *ad = scd->ad;
881         struct search_content_object* obj = scd->obj;
882         struct search_genlist_info *result_info = scd->result_info;
883         char *keyword = scd->keyword;
884
885         struct search_genlist_item_info *gen_item = NULL;
886
887         media_info_get_media_id(media, &media_id);
888         media_info_get_file_path(media, &media_path);
889         media_info_get_storage_type(media, &storage_type);
890         media_info_get_thumbnail_path(media, &media_thumbnail);
891
892         media_info_get_audio(media, &a_handle);
893         audio_meta_get_title(a_handle, &title);
894         audio_meta_get_album(a_handle, &album);
895         audio_meta_get_artist(a_handle, &artist);
896
897         sel_mem_info = (struct search_item_sel *)calloc(1,
898                                              sizeof(struct search_item_sel));
899
900         sel_mem_info->type = obj->contents_type;;
901
902         if ((media_thumbnail) && (strlen(media_thumbnail) > 0)
903                 && ecore_file_exists(media_thumbnail) == EINA_TRUE) {
904                 strncpy(sel_mem_info->icon1_path , media_thumbnail,
905                         MAX_LENGTH_PER_PATH -1 );
906         } else if(obj->obj_icon_path) {
907                 strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
908                         MAX_LENGTH_PER_PATH - 1);
909                 }
910
911         if ((title) && (strlen(title) > 0)) {
912                 strncpy(sel_mem_info->main_buf,
913                                 search_markup_keyword(title, keyword, &is_word_in_str),
914                                 MAX_LENGTH_PER_LINE - 1);
915         }
916
917         if (is_word_in_str) {
918                 if ((artist) && (strlen(artist) > 0)) {
919                         strncpy(sel_mem_info->sub_buf,
920                                         search_markup_keyword(artist,
921                                                                         keyword,
922                                                                         &is_word_in_str),
923                                         MAX_LENGTH_PER_LINE - 1);
924                 }
925         } else {
926                 memset(sel_mem_info->sub_buf, '\0', MAX_LENGTH_PER_LINE);
927
928                 if ((artist) && (strlen(artist) > 0)) {
929                         strncpy(sel_mem_info->sub_buf,
930                                         search_markup_keyword(artist,
931                                                                 keyword,
932                                                                 &is_word_in_str),
933                                         MAX_LENGTH_PER_LINE - 1);
934                 }
935
936                 if (is_word_in_str == false && (album) && (strlen(album) > 0)) {
937                         strncpy(sel_mem_info->sub_buf,
938                                         search_markup_keyword(album,
939                                                                 keyword,
940                                                                                         &is_word_in_str),
941                                         MAX_LENGTH_PER_LINE - 1);
942                 }
943
944                 if (is_word_in_str == false) {
945                         SEARCH_DEBUG_LOG("search_result is 0. No match keyword in title,album,artist string");
946                 }
947         }
948
949         if (storage_type == MEDIA_CONTENT_STORAGE_EXTERNAL) {
950                 itc_style = ad->itc_pslist_2line_2icon;
951                 strncpy(sel_mem_info->icon2_path, SEARCH_SDCARD_ICON,
952                                 MAX_LENGTH_PER_PATH - 1);
953         }
954         else {
955                 itc_style = ad->itc_pslist_2line_1icon;
956         }
957
958         if (media_id)
959                 snprintf(sel_mem_info->main_id,
960                                 MAX_LENGTH_PER_ID - 1, "%s", media_id);
961
962         if (media_path)
963                 strncpy(sel_mem_info->path, media_path, MAX_LENGTH_PER_PATH);
964
965         /* Add Information to genlist */
966         gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
967                                                                         obj,
968                                 itc_style,
969                                                 sel_mem_info,
970                                                                         obj->gl_result_grp);
971         result_info->itemlist =  g_list_append(result_info->itemlist, (void*)gen_item);
972
973         SEARCH_FREE(media_id);
974         SEARCH_FREE(media_path);
975         SEARCH_FREE(media_thumbnail);
976         SEARCH_FREE(title);
977         SEARCH_FREE(artist);
978         SEARCH_FREE(album);
979
980         return true;
981         }
982
983 static int __search_sql_media_search_result_by_api(struct search_content_object *obj,
984                                                                                                                                                 int offset, int limit,
985                                                                                                                                                 char *keyword,
986                                                                                                                                                 filter_h filter,
987                                                                                                                                                 media_info_cb func_cb,
988                                                                                                                                         void *data,
989                                                                                                                                         struct search_genlist_info *result_info)
990 {
991         SEARCH_FUNC_START;
992
993         struct appdata *ad = (struct appdata *)data;
994         struct search_cb_data scd = {obj, ad, keyword, result_info};
995
996         int result_cnt = 0;
997         int ret = 0;
998         int func_ret = SEARCH_RET_SEARCH_SUCCESS;
999
1000         struct search_item_more_sel *sel_mem_more = NULL;
1001
1002         media_info_get_media_count_from_db(filter, &result_cnt);
1003
1004         if (result_cnt != 0) {
1005                 /* Set group title */
1006                 if (obj->mr_info.offset== 0) {
1007                         result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
1008                                                                                                                 obj,
1009                                                                                                                 ad->itc_grouptitle,
1010                                                                                                                 obj,
1011                                                                                                                 NULL);
1012         }
1013
1014                 ret = media_info_foreach_media_from_db(filter, func_cb, &scd);
1015
1016                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1017                         SEARCH_DEBUG_WARNING("media_info_foreach_media_from_db failed : %d\n", ret);
1018                         func_ret = SEARCH_RET_SEARCH_FAIL;
1019                         goto out_func;
1020                 }
1021
1022         /* Set more title to genlist (under 'search all') */
1023         if (result_cnt == limit) {
1024                         obj->mr_info.next_point = offset + result_cnt - 1;
1025
1026                         sel_mem_more =
1027                             (struct search_item_more_sel *)calloc(1,
1028                                                              sizeof(struct search_item_more_sel));
1029                         sel_mem_more->obj = obj;
1030
1031                         result_info->more = search_add_genlist_item(SEARCH_GENLIST_MORE,
1032                                                                                                         obj,
1033                                                                                                         ad->itc_listmore,
1034                                                                                                         sel_mem_more,
1035                                                                                                         obj->gl_result_grp);
1036                 }
1037         } else {
1038                 func_ret = SEARCH_RET_SEARCH_NONE;
1039         }
1040
1041 out_func:
1042         SEARCH_FUNC_END;
1043
1044         return func_ret;
1045 }
1046
1047
1048 int search_sql_image_search_result_by_api(struct search_content_object *obj,
1049                                                                                                                                                 int offset, int limit,
1050                                                                                                                                                 char *keyword,
1051                                                                                                                                         void *data,
1052                                                                                                                                         struct search_genlist_info *result_info)
1053 {
1054         SEARCH_FUNC_START;
1055
1056         int ret = SEARCH_RET_SEARCH_SUCCESS;
1057         int len = 0;
1058         char *search_query = NULL;
1059         char *escape_added_keyword = NULL;
1060
1061         const char *image_query = "(MEDIA_TYPE=0 and MEDIA_DISPLAY_NAME like \"%%%s%%\" ESCAPE (\"%s\"))";
1062
1063         SEARCH_MALLOC(escape_added_keyword, strlen(keyword)*2+5, char);
1064         memset(escape_added_keyword, 0x0, strlen(keyword)*2+5);
1065         search_sql_make_keyword_bind_value(keyword, escape_added_keyword, SEARCH_SQL_BIND_TYPE_DUPLEX);
1066
1067         len = strlen(escape_added_keyword) + strlen(DB_ESCAPE_CHAR) + strlen(image_query) + 1;
1068         search_query = (char*)calloc(1, len);
1069
1070         snprintf(search_query, len, image_query, escape_added_keyword, "%");
1071
1072         SEARCH_DEBUG_WARNING("keyword :%s", keyword);
1073         SEARCH_DEBUG_WARNING("search_query :%s", escape_added_keyword);
1074
1075         filter_h filter;
1076
1077         ret = media_filter_create(&filter);
1078         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1079                 SEARCH_DEBUG_WARNING("media_filter_create failed : %d\n", ret);
1080                 ret = SEARCH_RET_SEARCH_FAIL;
1081                 goto out_func;
1082         }
1083
1084         ret = media_filter_set_offset(filter, offset, limit);
1085         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1086                 SEARCH_DEBUG_WARNING("media_filter_set_offset failed : %d\n", ret);
1087                 ret = SEARCH_RET_SEARCH_FAIL;
1088                 goto out_func;
1089         }
1090
1091         ret = media_filter_set_condition(filter, search_query, MEDIA_CONTENT_COLLATE_DEFAULT);
1092         if(ret != MEDIA_CONTENT_ERROR_NONE) {
1093                 SEARCH_DEBUG_WARNING("media_filter_set_condition failed : %d\n", ret);
1094                 ret = SEARCH_RET_SEARCH_FAIL;
1095                 goto out_func;
1096 }
1097
1098         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_DISPLAY_NAME, MEDIA_CONTENT_COLLATE_DEFAULT);
1099         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1100                 SEARCH_DEBUG_WARNING("media_filter_set_order failed : %d\n", ret);
1101                 ret = SEARCH_RET_SEARCH_FAIL;
1102                 goto out_func;
1103         }
1104
1105
1106         ret = __search_sql_media_search_result_by_api(obj,
1107                                                                                                 offset,
1108                                                                                                 limit,
1109                                                                                                 keyword,
1110                                                                                                 filter,
1111                                                                                                 __search_media_info_cb,
1112                                                                                                 data,
1113                                                                                                 result_info);
1114
1115 out_func:
1116         SEARCH_FREE(search_query);
1117         SEARCH_FREE(escape_added_keyword);
1118         media_filter_destroy(filter);
1119
1120         SEARCH_FUNC_END;
1121
1122         return ret;
1123 }
1124
1125 int search_sql_video_search_result_by_api(struct search_content_object *obj,
1126                 int offset, int limit, char *keyword, void *data, struct search_genlist_info *result_info)
1127 {
1128         SEARCH_FUNC_START;
1129
1130         int ret = SEARCH_RET_SEARCH_SUCCESS;
1131         int len = 0;
1132         char *search_query = NULL;
1133         char *escape_added_keyword = NULL;
1134
1135         const char *video_query = "(MEDIA_TYPE=1 and MEDIA_DISPLAY_NAME LIKE \"%%%s%%\" ESCAPE (\"%s\"))";
1136
1137         SEARCH_MALLOC(escape_added_keyword, strlen(keyword)*2+5, char);
1138         memset(escape_added_keyword, 0x0, strlen(keyword)*2+5);
1139         search_sql_make_keyword_bind_value(keyword, escape_added_keyword, SEARCH_SQL_BIND_TYPE_DUPLEX);
1140
1141         len = strlen(escape_added_keyword) + strlen(DB_ESCAPE_CHAR) + strlen(video_query) + 1;
1142         search_query = (char*)calloc(1, len);
1143
1144         snprintf(search_query, len, video_query, escape_added_keyword, DB_ESCAPE_CHAR);
1145
1146         filter_h filter;
1147
1148         ret = media_filter_create(&filter);
1149         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1150                 SEARCH_DEBUG_WARNING("media_filter_create failed : %d\n", ret);
1151                 ret = SEARCH_RET_SEARCH_FAIL;
1152                 goto out_func;
1153         }
1154
1155         ret = media_filter_set_offset(filter, offset, limit);
1156         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1157                 SEARCH_DEBUG_WARNING("media_filter_set_offset failed : %d\n", ret);
1158                 ret = SEARCH_RET_SEARCH_FAIL;
1159                 goto out_func;
1160         }
1161
1162         ret = media_filter_set_condition(filter, search_query, MEDIA_CONTENT_COLLATE_DEFAULT);
1163         if(ret != MEDIA_CONTENT_ERROR_NONE) {
1164                 SEARCH_DEBUG_WARNING("media_filter_set_condition failed : %d\n", ret);
1165                 ret = SEARCH_RET_SEARCH_FAIL;
1166                 goto out_func;
1167         }
1168
1169         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_DISPLAY_NAME, MEDIA_CONTENT_COLLATE_DEFAULT);
1170         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1171                 SEARCH_DEBUG_WARNING("media_filter_set_order failed : %d\n", ret);
1172                 ret = SEARCH_RET_SEARCH_FAIL;
1173                 goto out_func;
1174                         }
1175
1176         ret = __search_sql_media_search_result_by_api(obj,
1177                                 offset,
1178                                 limit,
1179                                 keyword,
1180                                 filter,
1181                                 __search_media_info_cb,
1182                                 data,
1183                                 result_info);
1184
1185 out_func:
1186
1187         SEARCH_FREE(search_query);
1188         SEARCH_FREE(escape_added_keyword);
1189         media_filter_destroy(filter);
1190
1191         SEARCH_FUNC_END;
1192
1193         return ret;
1194                         }
1195
1196 int search_sql_music_search_result_by_api(struct search_content_object *obj,
1197                 int offset, int limit,
1198                 char *keyword,
1199                 void *data,
1200                 struct search_genlist_info *result_info)
1201 {
1202         SEARCH_FUNC_START;
1203
1204         int ret = SEARCH_RET_SEARCH_SUCCESS;
1205         int len = 0;
1206         char *search_query = NULL;
1207         char *escape_added_keyword = NULL;
1208
1209         const char *music_query = "(MEDIA_TYPE=3 and MEDIA_TITLE LIKE \"%%%s%%\" or MEDIA_ALBUM LIKE \"%%%s%%\" or MEDIA_ARTIST LIKE \"%%%s%%\" ESCAPE (\"%s\"))";
1210
1211         SEARCH_MALLOC(escape_added_keyword, strlen(keyword)*2+5, char);
1212         memset(escape_added_keyword, 0x0, strlen(keyword)*2+5);
1213         search_sql_make_keyword_bind_value(keyword, escape_added_keyword, SEARCH_SQL_BIND_TYPE_DUPLEX);
1214
1215         len = strlen(escape_added_keyword) + strlen(DB_ESCAPE_CHAR) + strlen(music_query) + 1;
1216         search_query = (char*)calloc(1, len);
1217
1218         snprintf(search_query, len, music_query, escape_added_keyword,
1219                 escape_added_keyword, escape_added_keyword, DB_ESCAPE_CHAR);
1220
1221         filter_h filter;
1222
1223         ret = media_filter_create(&filter);
1224                 if (ret != MEDIA_CONTENT_ERROR_NONE) {
1225                 SEARCH_DEBUG_WARNING("media_filter_create failed : %d\n", ret);
1226                 ret = SEARCH_RET_SEARCH_FAIL;
1227                         goto out_func;
1228                                 }
1229
1230         ret = media_filter_set_offset(filter, offset, limit);
1231         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1232                 SEARCH_DEBUG_WARNING("media_filter_set_offset failed : %d\n", ret);
1233                 ret = SEARCH_RET_SEARCH_FAIL;
1234                 goto out_func;
1235                                 }
1236
1237         ret = media_filter_set_condition(filter, search_query, MEDIA_CONTENT_COLLATE_DEFAULT);
1238         if(ret != MEDIA_CONTENT_ERROR_NONE) {
1239                 SEARCH_DEBUG_WARNING("media_filter_set_condition failed : %d\n", ret);
1240                 ret = SEARCH_RET_SEARCH_FAIL;
1241                 goto out_func;
1242                         }
1243
1244         ret = media_filter_set_order(filter, MEDIA_CONTENT_ORDER_ASC, MEDIA_TITLE, MEDIA_CONTENT_COLLATE_DEFAULT);
1245         if (ret != MEDIA_CONTENT_ERROR_NONE) {
1246                 SEARCH_DEBUG_WARNING("media_filter_set_order failed : %d\n", ret);
1247                 ret = SEARCH_RET_SEARCH_FAIL;
1248                 goto out_func;
1249         }
1250
1251         ret = __search_sql_media_search_result_by_api(obj,
1252                                 offset,
1253                                 limit,
1254                                 keyword,
1255                                 filter,
1256                                 __search_sql_music_info_cb,
1257                                 data,
1258                                 result_info);
1259
1260 out_func:
1261
1262         SEARCH_FREE(search_query);
1263         SEARCH_FREE(escape_added_keyword);
1264         media_filter_destroy(filter);
1265
1266         SEARCH_FUNC_END;
1267
1268         return ret;
1269 }
1270
1271 ail_cb_ret_e __search_sql_menu_search_result_cb(
1272                                         const ail_appinfo_h appinfo,
1273                                         void *data)
1274 {
1275         SEARCH_FUNC_START;
1276
1277         struct search_cb_data *scd = (struct search_cb_data*)data;
1278         struct appdata *ad = scd->ad;
1279         struct search_content_object* obj = scd->obj;
1280         struct search_genlist_info *result_info = scd->result_info;
1281         char *keyword = scd->keyword;
1282
1283         struct search_genlist_item_info *gen_item = NULL;
1284         struct search_item_sel *sel_mem_info = NULL;
1285         struct search_item_more_sel *sel_mem_more = NULL;
1286
1287         bool is_word_in_str;
1288         ail_cb_ret_e ret;
1289
1290         if (obj->mr_info.loop_cnt < obj->mr_info.next_point) {
1291                 obj->mr_info.loop_cnt++;
1292                 return AIL_CB_RET_CONTINUE;
1293         }
1294
1295         if(obj->mr_info.next_point <
1296             obj->mr_info.offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
1297
1298                 sel_mem_info = (struct search_item_sel *)calloc(1, sizeof(struct search_item_sel));
1299                 sel_mem_info->type = obj->contents_type;;
1300
1301                 /********** START : set search result to sel_mem_info buf ************/
1302
1303                 char *name = NULL;
1304                 char *package = NULL;
1305                 char *icon = NULL;
1306
1307                 ail_appinfo_get_str(appinfo, AIL_PROP_ICON_STR, &icon);
1308
1309                 if (icon)
1310                         strncpy(sel_mem_info->icon1_path, icon,
1311                                         MAX_LENGTH_PER_PATH - 1);
1312
1313                 ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
1314                 if ((package) && (strlen(package) > 0)) {
1315
1316                         if(strcmp(package, "org.tizen.smartsearch") == 0) {
1317                                 SEARCH_DEBUG_LOG("'smartsearch' does not appended");
1318                                 obj->mr_info.loop_cnt++;
1319                                 SEARCH_FREE(sel_mem_info);
1320                                 return AIL_CB_RET_CONTINUE;
1321                         }
1322
1323                         strncpy(sel_mem_info->path, package, MAX_LENGTH_PER_PATH - 1);
1324
1325                         SEARCH_DEBUG_LOG("package : %s", package);
1326                 } else {
1327                         SEARCH_DEBUG_LOG("package : null");
1328                 }
1329
1330                 ail_appinfo_get_str(appinfo, AIL_PROP_NAME_STR, &name);
1331                 if ((name) && (strlen(name) > 0)) {
1332                         strncpy(sel_mem_info->main_buf,
1333                                 search_markup_keyword(name, keyword, &is_word_in_str),
1334                                         MAX_LENGTH_PER_LINE - 1);
1335
1336                         SEARCH_DEBUG_LOG("name : %s", name);
1337                 } else {
1338                         SEARCH_DEBUG_LOG("name : null");
1339 }
1340
1341                 if(obj->mr_info.next_point == 0) {
1342                         /* Set group title */
1343                         result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
1344                                                                                                                 obj,
1345                                                                                                                 ad->itc_grouptitle,
1346                                                                                                         obj,
1347                                                                                             NULL);
1348                 }
1349
1350                 /********** END : set search result to sel_mem_info buf *************/
1351
1352                 gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
1353                                                                                 obj,
1354                                                                                 ad->itc_pslist_1line_1icon,
1355                                         sel_mem_info,
1356                                                                                 obj->gl_result_grp);
1357                 result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
1358                 obj->mr_info.next_point++;
1359                 obj->mr_info.loop_cnt++;
1360
1361                 ret = AIL_CB_RET_CONTINUE;
1362
1363         } else if (obj->mr_info.next_point ==
1364                                 obj->mr_info.offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
1365
1366                 /* Check that only "SMARTSEARCH" is left in more result */
1367                 if(obj->mr_info.next_point == obj->mr_info.result_cnt-1) {
1368
1369                         char *package = NULL;
1370
1371                         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &package);
1372                         if ((package) && (strlen(package) > 0) && (strcmp(package, "org.tizen.smartsearch") == 0)) {
1373                                 SEARCH_DEBUG_LOG("Only 'SMARTSEARCH' is left in more result");
1374                                 return AIL_CB_RET_CANCEL;
1375                         }
1376                 }
1377
1378                 /* Set more title to genlist (under 'search all') */
1379                 sel_mem_more = (struct search_item_more_sel *)calloc(1, sizeof(struct search_item_more_sel));
1380                 sel_mem_more->obj = obj;
1381
1382                 result_info->more = search_add_genlist_item(SEARCH_GENLIST_MORE,
1383                                                                                                 obj,
1384                                                                                                 ad->itc_listmore,
1385                                                                                                 sel_mem_more,
1386                                                                                                 obj->gl_result_grp);
1387
1388                 ret = AIL_CB_RET_CANCEL;
1389         } else {
1390                 SEARCH_DEBUG_WARNING
1391                     ("__search_sql_memo_search_result_cb wrong index : %d",
1392                      obj->mr_info.next_point);
1393
1394                 ret = AIL_CB_RET_CANCEL;
1395         }
1396
1397         SEARCH_FUNC_END;
1398
1399         return ret;
1400 }
1401
1402 int search_sql_menu_search_result_by_api(struct search_content_object *obj,
1403                 int offset, int limit,
1404                 char *keyword,
1405                 void *data,
1406                 struct search_genlist_info *result_info)
1407 {
1408         SEARCH_FUNC_START;
1409
1410         struct appdata *ad = (struct appdata *)data;
1411         struct search_cb_data scd = {obj, ad, keyword, result_info};
1412
1413         int cnt = 0;
1414         int ret = 0;
1415
1416         ail_filter_h filter;
1417         ail_error_e rc;
1418
1419         char *escape_added_keyword = NULL;
1420         SEARCH_MALLOC(escape_added_keyword, strlen(keyword)*2+5, char);
1421         memset(escape_added_keyword, 0x0, strlen(keyword)*2+5);
1422         search_sql_make_keyword_bind_value(keyword, escape_added_keyword, SEARCH_SQL_BIND_TYPE_DUPLEX);
1423
1424         rc = ail_filter_new(&filter);
1425         if (rc != AIL_ERROR_OK) {
1426                 SEARCH_DEBUG_WARNING("menu search - ail_filter_new error : %d", rc);
1427                 return SEARCH_RET_SEARCH_FAIL;
1428         }
1429
1430         rc = ail_filter_add_str(filter, AIL_PROP_NAME_STR, escape_added_keyword);
1431         if (rc != AIL_ERROR_OK) {
1432                 SEARCH_DEBUG_WARNING("menu search - ail_filter_add_str error : %d", rc);
1433                 ret = SEARCH_RET_SEARCH_FAIL;
1434                 goto out_func;
1435         }
1436
1437         rc = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false);
1438         if (rc != AIL_ERROR_OK) {
1439                 SEARCH_DEBUG_WARNING("menu search - ail_filter_add_bool error : %d", rc);
1440                 ret = SEARCH_RET_SEARCH_FAIL;
1441                 goto out_func;
1442         }
1443
1444         rc = ail_filter_count_appinfo(filter, &cnt);
1445         if( rc != AIL_ERROR_OK) {
1446                 SEARCH_DEBUG_WARNING("menu search - ail_filter_count_appinfo error : %d", rc);
1447                 ret = SEARCH_RET_SEARCH_FAIL;
1448                 goto out_func;
1449         }
1450
1451         SEARCH_DEBUG_LOG("ail search result : %d", cnt);
1452         obj->mr_info.result_cnt = cnt;
1453
1454         if(cnt > 0) {
1455                 obj->mr_info.loop_cnt = 0;
1456                 rc = ail_filter_list_appinfo_foreach(filter, __search_sql_menu_search_result_cb, &scd);
1457                 if( rc != AIL_ERROR_OK) {
1458                         SEARCH_DEBUG_WARNING("menu search - ail_filter_list_appinfo_foreach error : %d", rc);
1459                         ret = SEARCH_RET_SEARCH_FAIL;
1460                         goto out_func;
1461                 }
1462
1463                 if (obj->mr_info.next_point == 0) {
1464                         ret = SEARCH_RET_SEARCH_NONE;
1465                 } else {
1466                         ret = SEARCH_RET_SEARCH_SUCCESS;
1467                 }
1468         } else {
1469                 ret = SEARCH_RET_SEARCH_NONE;
1470         }
1471
1472 out_func:
1473         ail_filter_destroy(filter);
1474         SEARCH_FREE(escape_added_keyword);
1475         SEARCH_FUNC_END;
1476
1477         return ret;
1478 }
1479
1480 bool __search_sql_browser_bookmark_result_cb(favorites_bookmark_entry_s *item, void *user_data)
1481 {
1482         SEARCH_FUNC_START;
1483         struct search_item_sel *sel_mem_info = NULL;
1484
1485         bool ret = false;
1486         bool is_word_in_str_main = false;
1487         bool is_word_in_str_sub = false;
1488
1489         const char* match_str = NULL;
1490
1491         struct search_cb_data_browser *scd = (struct search_cb_data_browser*)user_data;
1492         struct appdata *ad = scd->ad;
1493         struct search_content_object *obj = scd->obj;
1494         struct search_genlist_info *result_info = scd->result_info;
1495         char *keyword = scd->keyword;
1496
1497         struct search_genlist_item_info *gen_item = NULL;
1498         struct search_more_result *info = scd->info;
1499
1500         if (info->loop_cnt < info->next_point) {
1501                 info->loop_cnt++;
1502                 return true;
1503         }
1504
1505         if (info->next_point <
1506             info->offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
1507
1508                 sel_mem_info = (struct search_item_sel *)calloc(1, sizeof(struct search_item_sel));
1509                 sel_mem_info->type = obj->contents_type;;
1510
1511                 /********** START : set search result to sel_mem_info buf ************/
1512
1513                 if (item != NULL && item->title) {
1514                         match_str = search_markup_keyword(item->title, keyword, &is_word_in_str_main);
1515                         strncpy(sel_mem_info->main_buf,
1516                                         match_str,
1517                                         MAX_LENGTH_PER_LINE - 1);
1518                 }
1519
1520                 if (item != NULL && item->address) {
1521                         match_str = search_markup_keyword(item->address, keyword, &is_word_in_str_sub);
1522                         strncpy(sel_mem_info->sub_buf,
1523                                         match_str,
1524                                         MAX_LENGTH_PER_LINE - 1);
1525                         strncpy(sel_mem_info->path,
1526                                         item->address,
1527                                         MAX_LENGTH_PER_PATH - 1);
1528                 }
1529
1530                 if (is_word_in_str_main == true || is_word_in_str_sub == true) {
1531                         if (obj->gl_result_grp == NULL ) {
1532                                 /* Set group title */
1533                                 result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
1534                                                                                                                         obj,
1535                                                                                                                         ad->itc_grouptitle,
1536                                                                                                                         obj,
1537                                                                                                                         NULL);
1538                         }
1539
1540                         if (obj->obj_icon_path)
1541                                 strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
1542                                                 MAX_LENGTH_PER_PATH - 1);
1543
1544                         strncpy(sel_mem_info->icon2_path,
1545                                         SEARCH_FAVORITE_ICON,
1546                                         MAX_LENGTH_PER_PATH - 1);
1547
1548                         gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
1549                                                                                         obj,
1550                                                                                         ad->itc_pslist_2line_2icon,
1551                                                                 sel_mem_info,
1552                                                                                         obj->gl_result_grp);
1553                         result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
1554                 }
1555
1556                 /********** END : set search result to sel_mem_info buf *************/
1557                 info->next_point++;
1558                 info->loop_cnt++;
1559
1560                 ret = true;
1561         } else if (info->next_point ==
1562                                 info->offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
1563                 info->offset = info->next_point;
1564                 ret = false;
1565         } else {
1566                 SEARCH_DEBUG_WARNING
1567                     ("__search_sql_browser_bookmark_result_cb wrong index : %d",
1568                      info->next_point);
1569
1570                 ret = false;
1571         }
1572         SEARCH_FUNC_END;
1573         return ret;
1574
1575 }
1576
1577 bool __search_sql_browser_history_result_cb(favorites_history_entry_s *item, void *user_data)
1578 {
1579         SEARCH_FUNC_START;
1580         struct search_item_sel *sel_mem_info = NULL;
1581
1582         bool ret = false;
1583         bool is_word_in_str_main = false;
1584
1585         const char* match_str = NULL;
1586
1587         struct search_cb_data_browser *scd = (struct search_cb_data_browser*)user_data;
1588         struct appdata *ad = scd->ad;
1589         struct search_content_object *obj = scd->obj;
1590         struct search_genlist_info *result_info = scd->result_info;
1591         char *keyword = scd->keyword;
1592
1593         struct search_more_result *info = scd->info;
1594         struct search_genlist_item_info *gen_item = NULL;
1595
1596         if (info->loop_cnt < info->next_point) {
1597                 info->loop_cnt++;
1598                 return true;
1599         }
1600
1601         if (info->next_point <
1602             info->offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
1603
1604                 sel_mem_info = (struct search_item_sel *)calloc(1, sizeof(struct search_item_sel));
1605                 sel_mem_info->type = obj->contents_type;;
1606
1607                 /********** START : set search result to sel_mem_info buf ************/
1608
1609                 if (item != NULL && item->address) {
1610                                         SEARCH_DEBUG_WARNING("BROWSER : %s %s", item->address, keyword);
1611                         match_str = search_markup_keyword(item->address, keyword, &is_word_in_str_main);
1612                 }
1613
1614                 if (is_word_in_str_main != false ) {
1615                         if (obj->gl_result_grp == NULL ) {
1616                                 /* Set group title */
1617                         result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
1618                                                                                                                 obj,
1619                                                                                                                 ad->itc_grouptitle,
1620                                                                                                                 obj,
1621                                                                                                                 NULL);
1622                         }
1623
1624                         if (obj->obj_icon_path)
1625                                 strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
1626                                                 MAX_LENGTH_PER_PATH - 1);
1627
1628                         if (item->address)
1629                                 strncpy(sel_mem_info->path, item->address, MAX_LENGTH_PER_PATH - 1);
1630
1631                         strncpy(sel_mem_info->main_buf,
1632                                         match_str,
1633                                         MAX_LENGTH_PER_LINE - 1);
1634
1635                         gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
1636                                                                                         obj,
1637                                                                                         ad->itc_pslist_1line_1icon,
1638                                         sel_mem_info,
1639                                                                                         obj->gl_result_grp);
1640                         result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
1641                 }
1642
1643                 info->next_point++;
1644                 info->loop_cnt++;
1645
1646                 ret = true;
1647
1648         } else if (info->next_point ==
1649                                 info->offset + SEARCH_CATEGORY_LIST_MORE_CNT) {
1650                 info->offset = info->next_point;
1651                 ret = false;
1652         } else {
1653                 SEARCH_DEBUG_WARNING
1654                     ("__search_sql_browser_history_result_cb wrong index : %d",
1655                      info->next_point);
1656
1657                 ret = false;
1658         }
1659
1660         SEARCH_FUNC_END;
1661         return ret;
1662 }
1663
1664 int search_sql_browser_search_result_by_api(struct search_content_object *obj,
1665                 int offset, int limit,
1666                 char *keyword,
1667                 void *data,
1668                 struct search_genlist_info *result_info)
1669 {
1670         SEARCH_FUNC_START;
1671
1672         struct appdata *ad = (struct appdata *)data;
1673         struct search_cb_data_browser scd_browser = {obj, ad, keyword, result_info, NULL};
1674         struct search_cb_data_browser scd_history = {obj, ad, keyword, result_info, NULL};
1675
1676         static struct search_more_result browser_result[2];
1677         struct search_item_more_sel *sel_mem_more = NULL;
1678
1679         int ret = 0;
1680         int bookmark_cnt = 0, history_cnt = 0;
1681         int bookmark_ret = 0, history_ret = 0;
1682
1683         if (offset == 0) {
1684                 /* offset 0 means first search result, so we initialize struct */
1685                 memset(browser_result, 0x0, sizeof(browser_result));
1686
1687                 favorites_bookmark_get_count(&browser_result[0].result_cnt);
1688                 favorites_history_get_count(&browser_result[1].result_cnt);
1689
1690                 bookmark_cnt = browser_result[0].result_cnt;
1691                 history_cnt = browser_result[1].result_cnt;
1692                 browser_result[0].ad = browser_result[1].ad = ad;
1693                 obj->gl_result_grp = NULL;
1694
1695                 if ( bookmark_cnt <= 0 && history_cnt <= 0 )
1696                         return SEARCH_RET_SEARCH_NONE;
1697         } else {
1698                 bookmark_cnt = browser_result[0].result_cnt;
1699                 history_cnt = browser_result[1].result_cnt;
1700                 browser_result[0].loop_cnt = 0;
1701                 browser_result[1].loop_cnt = 0;
1702         }
1703
1704         if (offset >= bookmark_cnt + history_cnt) {
1705                 /* We need not to search */
1706                 return SEARCH_RET_SEARCH_NONE;
1707         }
1708
1709         scd_browser.info = &browser_result[0];
1710         scd_history.info = &browser_result[1];
1711
1712         /* offset check */
1713         if (bookmark_cnt > browser_result[0].next_point )
1714                 bookmark_ret = favorites_bookmark_foreach(__search_sql_browser_bookmark_result_cb,
1715                                                                                                 &scd_browser);
1716         if (history_cnt > browser_result[1].next_point )
1717                 history_ret = favorites_history_foreach(__search_sql_browser_history_result_cb,
1718                                                                                                 &scd_history);
1719
1720         if (bookmark_ret == FAVORITES_ERROR_DB_FAILED
1721                 && history_ret == FAVORITES_ERROR_DB_FAILED ) {
1722                 ret = SEARCH_RET_SEARCH_FAIL;
1723         }
1724         else if (obj->gl_result_grp == NULL) {
1725                 /* It means we can't get matched results */
1726                 ret = SEARCH_RET_SEARCH_NONE;
1727         }
1728         else {
1729                 /* Make More button */
1730                 if (browser_result[0].next_point < bookmark_cnt
1731                         || browser_result[1].next_point < history_cnt) {
1732                 sel_mem_more = (struct search_item_more_sel *)calloc(1, sizeof(struct search_item_more_sel));
1733                 sel_mem_more->obj = obj;
1734
1735                         result_info->more = search_add_genlist_item(SEARCH_GENLIST_MORE,
1736                                                                                                         obj,
1737                                                                                                         ad->itc_listmore,
1738                                                                                                         sel_mem_more,
1739                                                                                                         obj->gl_result_grp);
1740                 }
1741
1742                 /* return check */
1743                 obj->mr_info.next_point = browser_result[0].next_point
1744                                                                 + browser_result[1].next_point;
1745                 ret = SEARCH_RET_SEARCH_SUCCESS;
1746         }
1747
1748         return ret;
1749
1750         SEARCH_FUNC_END;
1751 }
1752
1753 int search_sql_msg_search_result_by_api(struct search_content_object *obj,
1754                 int offset, int limit,
1755                 char *keyword,
1756                 void *data,
1757                 struct search_genlist_info *result_info)
1758 {
1759         SEARCH_FUNC_START;
1760
1761         int loop_cnt = 0;
1762         int ret = 0;
1763
1764         struct search_item_sel *sel_mem_info = NULL;
1765         struct search_item_more_sel *sel_mem_more = NULL;
1766         struct appdata *ad = (struct appdata *)data;
1767         struct search_genlist_item_info *gen_item = NULL;
1768
1769         bool is_word_in_str;
1770
1771         int i = 0;
1772         int j = 0;
1773         bool is_more_btn = false;
1774         bool is_word_in_title = false;
1775
1776         msg_error_t err = MSG_SUCCESS;
1777         msg_struct_list_s msgList;
1778         msg_struct_t search_s = NULL;
1779         msg_struct_list_s *addr_list = NULL;
1780
1781         search_s = msg_create_struct(MSG_STRUCT_SEARCH_CONDITION);
1782
1783         err = msg_set_int_value(search_s, MSG_SEARCH_CONDITION_FOLDERID_INT, MSG_ALLBOX_ID);
1784         if (err != MSG_SUCCESS) {
1785                 SEARCH_DEBUG_WARNING("Fail to make search struct : MSG_SEARCH_CONDITION_FOLDERID_INT");
1786                 return SEARCH_RET_SEARCH_FAIL;
1787         };
1788
1789         err = msg_set_int_value(search_s, MSG_SEARCH_CONDITION_MSGTYPE_INT, MSG_TYPE_INVALID);
1790         if (err != MSG_SUCCESS) {
1791                 SEARCH_DEBUG_WARNING("Fail to make search struct : MSG_SEARCH_CONDITION_MSGTYPE_INT");
1792                 return SEARCH_RET_SEARCH_FAIL;
1793         }
1794
1795         err = msg_set_str_value(search_s, MSG_SEARCH_CONDITION_SEARCH_VALUE_STR, keyword, strlen(keyword));
1796         if (err != MSG_SUCCESS) {
1797                 SEARCH_DEBUG_WARNING("Fail to make search struct : MSG_SEARCH_CONDITION_SEARCH_VALUE_STR");
1798                 return SEARCH_RET_SEARCH_FAIL;
1799         }
1800
1801         err = msg_search_message(ad->msg_handle, search_s, offset, limit, &msgList);
1802
1803         if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
1804                 SEARCH_DEBUG_WARNING("msg_search_message is failed : %d", err);
1805                 return SEARCH_RET_SEARCH_FAIL;
1806         }
1807
1808         if (msgList.nCount <= 0) {
1809                 SEARCH_DEBUG_WARNING("no msg search result");
1810                 ret = SEARCH_RET_SEARCH_NONE;
1811         }  else  {
1812
1813                 /* Set group title */
1814                 if (offset == 0) {
1815                         /* Set group title */
1816                         result_info->group = search_add_genlist_item(SEARCH_GENLIST_GROUP,
1817                                                                                 obj,
1818                                                                                 ad->itc_grouptitle,
1819                                                                         obj,
1820                                                                                 NULL);
1821                 }
1822
1823                 /* Set search result to genlist */
1824
1825                 if(msgList.nCount < limit) {
1826                         loop_cnt = msgList.nCount;
1827                         is_more_btn = false;
1828                 } else {
1829                         loop_cnt = limit-1;
1830                         is_more_btn = true;
1831                 }
1832
1833                 /********** START : set search result to sel_mem_info buf ************/
1834
1835                 for (i = 0; i < loop_cnt; i++) {
1836                         int ret = 0;
1837                         const char *text_1line = NULL;
1838                         const char *markup_keyword = NULL;
1839                         const char *img_path = NULL;
1840
1841                         int msg_type = 0;
1842                         int     contact_id = -1;
1843                         int message_id = -1;
1844
1845                         char subject[MAX_SUBJECT_LEN] = {0};
1846                         char str_number[MAX_ADDRESS_VAL_LEN] = {0,};
1847                         char str_name[MAX_DISPLAY_NAME_LEN] = {0,};
1848                         char msg_text[MAX_MSG_TEXT_LEN] = {0,};
1849
1850                         CTSstruct *contact = NULL;
1851
1852                         sel_mem_info = (struct search_item_sel *)
1853                                                         calloc(1,sizeof(struct search_item_sel));
1854                         sel_mem_info->type = obj->contents_type;;
1855
1856                         err = msg_get_int_value(msgList.msg_struct_info[i], MSG_MESSAGE_ID_INT, &message_id);
1857                         if (err != MSG_SUCCESS) {
1858                                 SEARCH_DEBUG_WARNING("Fail to make search struct : MSG_MESSAGE_ID_INT %d", err);
1859                                 goto out_func;
1860                         }
1861                         snprintf(sel_mem_info->main_id, MAX_LENGTH_PER_ID - 1, "%d", message_id);
1862
1863                         err = msg_get_int_value(msgList.msg_struct_info[i], MSG_MESSAGE_TYPE_INT, &msg_type);
1864                         if (err != MSG_SUCCESS) {
1865                                 SEARCH_DEBUG_WARNING("Fail to make search struct : MSG_MESSAGE_TYPE_INT %d", err);
1866                                 goto out_func;
1867                         }
1868
1869                         SEARCH_DEBUG_LOG("mms : %d", msg_type);
1870
1871                         /* Set 1st line string */
1872                         if (msg_type == MSG_TYPE_MMS) {
1873                                 /* MMS case */
1874                                 err = msg_get_str_value(msgList.msg_struct_info[i], MSG_MESSAGE_SUBJECT_STR,
1875                                                                         subject, MAX_SUBJECT_LEN);
1876
1877                                 if (err != MSG_SUCCESS) {
1878                                         SEARCH_DEBUG_WARNING("Fail to get sms context %d", err);
1879                                         goto out_func;
1880                                 }
1881
1882                                 text_1line = subject;
1883                                 markup_keyword = search_markup_keyword(text_1line,
1884                                                                                         keyword,
1885                                                                                                         &is_word_in_title);
1886
1887                                 if (!is_word_in_title) {
1888                                         memset(msg_text, 0x00, sizeof(msg_text));
1889                                         err = msg_get_str_value(msgList.msg_struct_info[i],
1890                                                                                 MSG_MESSAGE_MMS_TEXT_STR,
1891                                                                                 msg_text,
1892                                                                                 sizeof(msg_text));
1893
1894                                         if (err != MSG_SUCCESS) {
1895                                                 SEARCH_DEBUG_WARNING("Fail to get mms context %d", err);
1896                                                 goto out_func;
1897                                         }
1898
1899                                         text_1line = msg_text;
1900
1901                                         markup_keyword = search_markup_keyword(text_1line,
1902                                                         keyword,
1903                                                                                                                 &is_word_in_title);
1904                         }
1905
1906                                 SEARCH_DEBUG_LOG("1st string : %s", text_1line);
1907
1908                                 if ((text_1line) && (strlen(text_1line) > 0 ))
1909                                         strncpy(sel_mem_info->main_buf, markup_keyword, MAX_LENGTH_PER_LINE - 1);
1910                         } else {
1911                                 /* SMS Case */
1912                                 err = msg_get_str_value(msgList.msg_struct_info[i],
1913                                                                         MSG_MESSAGE_SMS_DATA_STR,
1914                                                                         msg_text,
1915                                                                         sizeof(msg_text));
1916
1917                                 if (err != MSG_SUCCESS) {
1918                                         SEARCH_DEBUG_WARNING("Fail to get sms context %d", err);
1919                                         goto out_func;
1920                                 }
1921
1922                                 text_1line = msg_text;
1923
1924                                 markup_keyword = search_markup_keyword(text_1line,
1925                                                                                         keyword,
1926                                                                                                         &is_word_in_title);
1927                                 SEARCH_DEBUG_LOG("1st string : %s", text_1line);
1928
1929                                 if ((text_1line) && (strlen(text_1line) > 0))
1930                                         strncpy(sel_mem_info->main_buf, markup_keyword, MAX_LENGTH_PER_LINE - 1);
1931                         }
1932
1933                         err = msg_get_list_handle(msgList.msg_struct_info[i],
1934                                                                 MSG_MESSAGE_ADDR_LIST_STRUCT,
1935                                                                 (void **)&addr_list);
1936
1937                         if (err != MSG_SUCCESS) {
1938                                 SEARCH_DEBUG_WARNING("Fail to get address list handle! %d", err);
1939                                 goto out_func;
1940                         }
1941
1942                         /* Set 2nd line string */
1943                         for ( j=0; j<addr_list->nCount; j++) {
1944                                 memset(str_number, 0, sizeof(str_number));
1945                                 memset(str_name, 0, sizeof(str_name));
1946                                 contact_id = -1;
1947                                 is_word_in_str = false;
1948
1949                                 /* First We set text_2line to first address data */
1950                                 err = msg_get_str_value(addr_list->msg_struct_info[j],
1951                                                                         MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
1952                                                                         str_number,
1953                                                                         MAX_ADDRESS_VAL_LEN);
1954
1955                                 if (err != MSG_SUCCESS) {
1956                                         SEARCH_DEBUG_WARNING("Fail to get data! : MSG_ADDRESS_INFO_DISPLAYNAME_STR %d", err);
1957                                         goto out_func;
1958                                 }
1959
1960                                 err = msg_get_str_value(addr_list->msg_struct_info[j],
1961                                                                         MSG_ADDRESS_INFO_DISPLAYNAME_STR,
1962                                                                         str_name,
1963                                                                         MAX_DISPLAY_NAME_LEN);
1964
1965                                 if (err != MSG_SUCCESS) {
1966                                         SEARCH_DEBUG_WARNING("Fail to get data! : MSG_ADDRESS_INFO_ADDRESS_VALUE_STR %d", err);
1967                                         goto out_func;
1968                                 }
1969
1970                                 err = msg_get_int_value(addr_list->msg_struct_info[j],
1971                                                                         MSG_ADDRESS_INFO_CONTACT_ID_INT,
1972                                                                         &contact_id);
1973
1974                                 if (err != MSG_SUCCESS) {
1975                                         SEARCH_DEBUG_WARNING("Fail to get data! : MSG_ADDRESS_INFO_CONTACT_ID_INT %d", err);
1976                                         goto out_func;
1977                                 }
1978
1979                                 /* Set text_2line */
1980                                 markup_keyword = search_markup_keyword(str_number, keyword, &is_word_in_str);
1981
1982                                 if( 0 == j || true == is_word_in_str) {
1983                                         if (strlen(str_name) > 0) {
1984                                                 strncpy(sel_mem_info->sub_buf, str_name, MAX_LENGTH_PER_LINE - 1);
1985                                 } else {
1986                                                 strncpy(sel_mem_info->sub_buf, markup_keyword, MAX_LENGTH_PER_LINE - 1);
1987                                 }
1988
1989                                         /* Set Thumbnail Image */
1990                                         if (contact_id == -1) {
1991                                                 strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
1992                                                                 MAX_LENGTH_PER_PATH - 1);
1993                                         }
1994                                         else {
1995                                                 ret = contacts_svc_get_contact(contact_id, &contact);
1996
1997                                                 if (ret == CTS_SUCCESS) {
1998                                                         CTSvalue* value = NULL;
1999                                                         ret = contacts_svc_struct_get_value(contact, CTS_CF_BASE_INFO_VALUE, &value);
2000
2001                                                         if (ret == CTS_SUCCESS)
2002                                                                 img_path = contacts_svc_value_get_str(value, CTS_BASE_VAL_IMG_PATH_STR);
2003
2004                                                         if (img_path != NULL)
2005                                                                 strncpy(sel_mem_info->icon1_path , img_path,
2006                                                                                 MAX_LENGTH_PER_PATH -1 );
2007                                                         else if (obj->obj_icon_path)
2008                                                                 strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
2009                                                                                 MAX_LENGTH_PER_PATH - 1);
2010                                                 }
2011                                                 else if (obj->obj_icon_path)
2012                                                                 strncpy(sel_mem_info->icon1_path, obj->obj_icon_path,
2013                                                                                 MAX_LENGTH_PER_PATH - 1);
2014                                         }
2015                                 }
2016
2017                                 if ((is_word_in_title) || (is_word_in_str)) {
2018                                         break;
2019                                 }
2020                         }
2021
2022                         SEARCH_DEBUG_LOG("sel_mem_info->sub_buf : %s", sel_mem_info->sub_buf);
2023
2024                         gen_item = search_add_genlist_item(SEARCH_GENLIST_ITEM,
2025                                                         obj,
2026                                                         ad->itc_pslist_2line_1icon,
2027                                                         sel_mem_info,
2028                                                         obj->gl_result_grp);
2029
2030                         result_info->itemlist = g_list_append(result_info->itemlist, (void*)gen_item);
2031                 }
2032
2033                 msg_release_list_struct(&msgList);
2034
2035                 /* Set more title to genlist (under 'search all') */
2036                 if (is_more_btn) {
2037                         obj->mr_info.next_point = offset + limit - 1;
2038
2039                         sel_mem_more = (struct search_item_more_sel *)
2040                                         calloc(1,sizeof(struct search_item_more_sel));
2041                         sel_mem_more->obj = obj;
2042
2043                         result_info->more = search_add_genlist_item(SEARCH_GENLIST_MORE,
2044                                                                                 obj,
2045                                                                                 ad->itc_listmore,
2046                                                                                 sel_mem_more,
2047                                                                                 obj->gl_result_grp);
2048                 }
2049
2050                 ret = SEARCH_RET_SEARCH_SUCCESS;
2051         }
2052
2053 out_func:
2054         SEARCH_FUNC_END;
2055         return ret;
2056 }
2057
2058 void search_sql_update_result_object(struct search_content_object* result_obj, sqlite3* db_handle, sqlite3_stmt** db_sql)
2059 {
2060         SEARCH_FUNC_START;
2061
2062         int rc = 0;
2063
2064         sqlite3_stmt *sql_stmt = db_sql[SEARCH_STMT_UPDATE_OBJECT_INFO];
2065         SEARCH_RET_IF(sql_stmt==NULL);
2066
2067         search_sql_bind_text(db_handle, sql_stmt, 1, result_obj->name);
2068         search_sql_bind_int(db_handle, sql_stmt, 2, result_obj->string_type);
2069         search_sql_bind_int(db_handle, sql_stmt, 3, result_obj->visible);
2070         search_sql_bind_int(db_handle, sql_stmt, 4, result_obj->order);
2071         search_sql_bind_int(db_handle, sql_stmt, 5, result_obj->contents_type);
2072         search_sql_bind_int(db_handle, sql_stmt, 6, result_obj->category_type);
2073         search_sql_bind_text(db_handle, sql_stmt, 7, result_obj->def_pkg_name);
2074         search_sql_bind_int(db_handle, sql_stmt, 8, result_obj->contents_type);
2075
2076         rc = sqlite3_step(sql_stmt);
2077
2078         if (rc == SQLITE_OK)
2079                 SEARCH_DEBUG_LOG("INSERT STEP SUCCESS");
2080
2081         search_sql_stmt_init(sql_stmt);
2082
2083         SEARCH_FUNC_END;
2084
2085 }