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