apply FSL(Flora Software License)
[apps/home/smartsearch.git] / src / ps_makeupviews.cpp
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  * 
4  * Licensed under the Flora License, Version 1.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *     http://www.tizenopensource.org/license
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19
20
21
22 #include "smartsearch.h"
23 #include "ps_searcher.h"
24 #include "ps_app_interface.h"
25
26 static char *__search_get_phone_category_name(int type)
27 {
28         SEARCH_FUNC_START;
29
30         char* name = NULL;
31
32         switch(type) {
33                 
34                 case SEARCH_TYPE_PHONE_CONTACTS:
35                 {
36                         name  = dgettext("sys_string", "IDS_COM_BODY_CONTACTS");
37                 }
38                 break;
39                 case SEARCH_TYPE_PHONE_MSG:
40                 {
41                         name  = dgettext("sys_string", "IDS_COM_BODY_MESSAGES");
42                 }
43                 break;
44                 case SEARCH_TYPE_PHONE_EMAIL:
45                 {
46                         name  = dgettext("sys_string", "IDS_COM_BODY_EMAIL");
47                 }
48                 break;
49                 case SEARCH_TYPE_PHONE_IMAGES:
50                 {
51                         name  = dgettext("sys_string", "IDS_COM_BODY_IMAGES");
52                 }
53                 break;
54                 case SEARCH_TYPE_PHONE_MUSIC:
55                 {
56                         name  = dgettext("sys_string", "IDS_COM_BODY_MUSIC");
57                 }
58                 break;
59                 case SEARCH_TYPE_PHONE_VIDEO:
60                 {
61                         name  = dgettext("sys_string", "IDS_COM_BODY_VIDEOS");
62                 }
63                 break;
64                 case SEARCH_TYPE_PHONE_CALENDAR:
65                 {
66                         name  = dgettext("sys_string", "IDS_COM_BODY_CALENDAR");
67                 }
68                 break;
69                 case SEARCH_TYPE_PHONE_MEMO:
70                 {
71                         name  = dgettext("sys_string", "IDS_COM_BODY_MEMO");
72                 }
73                 break;
74                 case SEARCH_TYPE_PHONE_MENU:
75                 {
76                         name  = dgettext("sys_string", "IDS_COM_BODY_APPLICATIONS");
77                 }
78                 break;
79         }
80
81         return name;
82 }
83
84
85 static Eina_Bool __search_gl_state_get(void *data, Evas_Object * obj,
86                                        const char *part)
87 {
88         return EINA_FALSE;
89 }
90
91 static char *__search_gl_label_get(void *data, Evas_Object * obj,
92                                    const char *part)
93 {
94         if (data == NULL) {
95                 return g_strdup("");
96         }
97
98         char *buf;
99         struct search_item_sel *sel_mem_info = (struct search_item_sel *)data;
100  
101         if (!SEARCH_STRCMP(part, "elm.text") || !SEARCH_STRCMP(part, "elm.text.1")) {
102                 buf = g_strdup(sel_mem_info->main_buf);
103         } else if (!SEARCH_STRCMP(part, "elm.text.sub") || !SEARCH_STRCMP(part, "elm.text.2")) {
104                 buf = g_strdup(sel_mem_info->sub_buf);
105         } else {
106                 buf = NULL;
107         }
108  
109         return buf;
110 }
111
112 static char *__search_gl_label_get_title(void *data, Evas_Object * obj,
113                                          const char *part)
114 {
115         SEARCH_FUNC_START;
116
117         char* category_name = NULL;
118
119         struct search_item_sel *sel_mem = (struct search_item_sel *)data;
120
121         category_name = __search_get_phone_category_name(sel_mem->type);
122
123         if(category_name)
124                 return strdup(category_name);
125         else
126                 return strdup("");
127 }
128
129 static char *__search_gl_label_get_more(void *data, Evas_Object * obj,
130                                         const char *part)
131 {
132         return strdup(gettext("IDS_SSEARCH_BODY_MORE_RESULTS"));
133 }
134
135 static void __search_gl_del(void *data, Evas_Object * obj)
136 {
137         SEARCH_FUNC_START;
138
139         struct search_item_sel *sel_mem_info = (struct search_item_sel *)data;
140
141         SEARCH_FREE(sel_mem_info);
142
143         return;
144 }
145
146 void search_gl_phone_result_sel(void *data, Evas_Object * obj, void *event_info)
147 {
148         SEARCH_FUNC_START;
149
150         struct appdata *ad = (struct appdata *)data;
151         struct search_item_sel *cur_category_mem;
152         Elm_Object_Item *it = (Elm_Object_Item *) event_info;
153         int ret = 0;
154
155         cur_category_mem =
156             (struct search_item_sel *)elm_object_item_data_get(it);
157         if (cur_category_mem == NULL) {
158                 return;
159         }
160
161         switch (cur_category_mem->type) {
162
163         case SEARCH_TYPE_PHONE_CONTACTS:
164                 search_launch_contact_view(ad, cur_category_mem);
165                 break;
166
167         case SEARCH_TYPE_PHONE_MSG:
168                 search_launch_msg_view(ad, cur_category_mem);
169                 break;
170
171         case SEARCH_TYPE_PHONE_EMAIL:
172                 search_launch_email_view(ad, cur_category_mem);
173                 break;
174
175         case SEARCH_TYPE_PHONE_IMAGES:
176                 search_launch_image_view(ad, cur_category_mem);
177                 break;
178
179         case SEARCH_TYPE_PHONE_VIDEO:
180                 search_launch_video_view(ad, cur_category_mem);
181                 break;
182
183         case SEARCH_TYPE_PHONE_MUSIC:
184                 search_launch_music_view(ad, cur_category_mem);
185                 break;
186                 
187         case SEARCH_TYPE_PHONE_CALENDAR:
188                 search_launch_calendar_view(ad, cur_category_mem);
189                 break;
190
191         case SEARCH_TYPE_PHONE_MEMO:
192                 search_launch_memo_view(ad, cur_category_mem);
193                 break;
194
195         case SEARCH_TYPE_PHONE_MENU:
196                 search_launch_menu_view(ad, cur_category_mem);
197                 break;
198
199         default:
200                 break;
201         }
202
203         elm_genlist_item_selected_set(it, EINA_FALSE);
204
205         SEARCH_FUNC_END;
206 }
207
208 void search_result_gl_init(void *data)
209 {
210         SEARCH_FUNC_START;
211
212         struct appdata *ad = (struct appdata *)data;
213         Evas_Object *genlist;
214
215
216         if (ad->itc_pslist_1line == NULL) {
217                 ad->itc_pslist_1line = elm_genlist_item_class_new();
218                 ad->itc_pslist_1line->item_style = "1text.2";
219                 ad->itc_pslist_1line->func.text_get = __search_gl_label_get;
220                 ad->itc_pslist_1line->func.content_get = NULL;
221                 ad->itc_pslist_1line->func.state_get = __search_gl_state_get;
222                 ad->itc_pslist_1line->func.del = __search_gl_del;
223         }
224
225
226         if (ad->itc_pslist_2line == NULL) {
227                 ad->itc_pslist_2line = elm_genlist_item_class_new();
228                 ad->itc_pslist_2line->item_style = "2text.8";
229                 ad->itc_pslist_2line->func.text_get = __search_gl_label_get;
230                 ad->itc_pslist_2line->func.content_get = NULL;
231                 ad->itc_pslist_2line->func.state_get = __search_gl_state_get;
232                 ad->itc_pslist_2line->func.del = __search_gl_del;
233         }
234
235         if (ad->itc_grouptitle == NULL) {
236                 ad->itc_grouptitle = elm_genlist_item_class_new();
237                 ad->itc_grouptitle->item_style = "grouptitle";
238                 ad->itc_grouptitle->func.text_get = __search_gl_label_get_title;
239                 ad->itc_grouptitle->func.state_get = __search_gl_state_get;
240                 ad->itc_grouptitle->func.del = __search_gl_del;
241         }
242
243         if (ad->itc_listmore == NULL) {
244                 ad->itc_listmore = elm_genlist_item_class_new();
245                 ad->itc_listmore->item_style = "1text.2";
246                 ad->itc_listmore->func.text_get = __search_gl_label_get_more;
247                 ad->itc_listmore->func.state_get = __search_gl_state_get;
248                 ad->itc_listmore->func.del = __search_gl_del;
249         }
250
251         ad->search_gl = NULL;
252
253         genlist = elm_genlist_add(ad->navi_bar);
254
255         // To use multiline textblock/entry/editfield in genlist, set height_for_width mode
256         // then the item's height is calculated while the item's width fits to genlist width.
257         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
258         elm_genlist_tree_effect_enabled_set(genlist, EINA_TRUE);
259
260         ad->search_gl = genlist;
261
262         evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND,
263                                          EVAS_HINT_EXPAND);
264         evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL,
265                                         EVAS_HINT_FILL);
266         SEARCH_FUNC_END;
267 }
268
269 void search_result_gl_deinit(void *data)
270 {
271         SEARCH_FUNC_START;
272
273         struct appdata *ad = (struct appdata *)data;
274
275         if (ad->itc_pslist_1line != NULL)
276                 elm_genlist_item_class_free(ad->itc_pslist_1line);
277
278         if (ad->itc_pslist_2line != NULL)
279                 elm_genlist_item_class_free(ad->itc_pslist_2line);
280
281         if (ad->itc_grouptitle != NULL)
282                 elm_genlist_item_class_free(ad->itc_grouptitle);
283
284         if (ad->itc_listmore != NULL)
285                 elm_genlist_item_class_free(ad->itc_listmore);
286
287         SEARCH_FUNC_END;
288 }
289
290 static int __search_gl_set_phone_search_result(int type, int offset, char *keyword,
291                                                void *data)
292 {
293         SEARCH_FUNC_START;
294
295         int rc = 0;
296         int category = 0;
297         int loop_cnt = 0;
298
299         int ret = 0;
300
301         int sel_cnt = SEARCH_CATEGORY_LIST_MORE_CNT + 1;
302         int func_ret = 1;
303
304         struct appdata *ad = (struct appdata *)data;
305         Evas_Object *glist;
306
307         ad->cate_info[type].offset = offset;
308
309         if (offset == 0) {
310                 ad->cate_info[type].next_point = 0;
311         }
312
313         switch (type) {
314                 
315         case SEARCH_TYPE_PHONE_CONTACTS:
316                 {
317                         func_ret =
318                             search_sql_contact_search_result_by_api(type,
319                                                                     offset,
320                                                                     sel_cnt,
321                                                                     data);
322                 }
323                 break;
324
325         case SEARCH_TYPE_PHONE_MSG:
326                 {
327                         if (ad->msg_handle != NULL) {
328                         func_ret =
329                             search_sql_msg_search_result_by_api(type, 
330                                                                 offset,
331                                                                         sel_cnt, 
332                                                                         data);
333                 }
334                 }
335                 break;
336
337         case SEARCH_TYPE_PHONE_EMAIL:
338                 {
339                         func_ret =
340                             search_sql_email_search_result_by_api(type, 
341                                                                 offset,
342                                                                         sel_cnt, 
343                                                                         data);
344                 }
345                 break;          
346         case SEARCH_TYPE_PHONE_IMAGES:
347                 {
348                         if (ad->media_handle != NULL ) {
349                         func_ret =
350                             search_sql_image_search_result_by_api(type, 
351                                                                 offset,
352                                                                         sel_cnt, 
353                                                                         data);
354                 }
355                 }
356                 break;                  
357         case SEARCH_TYPE_PHONE_MUSIC:
358                 {
359                         if (ad->media_handle != NULL ) {
360                         func_ret =
361                             search_sql_music_search_result_by_api(type, 
362                                                                 offset,
363                                                                         sel_cnt, 
364                                                                         data);
365                 }
366                 }
367                 break;
368         case SEARCH_TYPE_PHONE_VIDEO:
369                 {
370                         if (ad->media_handle != NULL ) {
371                         func_ret =
372                             search_sql_video_search_result_by_api(type, 
373                                                                 offset,
374                                                                         sel_cnt, 
375                                                                         data);
376                 }
377                 }
378                 break;          
379         
380         case SEARCH_TYPE_PHONE_CALENDAR:
381                 {
382                         func_ret =
383                             search_sql_calendar_search_result_by_api(type,
384                                                                         offset,
385                                                                         sel_cnt,
386                                                                         data);
387                 }
388                 break;
389
390         case SEARCH_TYPE_PHONE_MEMO:
391                 {
392                         func_ret =
393                             search_sql_memo_search_result_by_api(type, 
394                                                                 offset,
395                                                                         sel_cnt, 
396                                                                         data);
397                 }
398                 break;
399
400         case SEARCH_TYPE_PHONE_MENU:
401                 {
402                         func_ret =
403                             search_sql_menu_search_result_by_api(type,
404                                                                         offset,
405                                                                         sel_cnt,
406                                                                         keyword,
407                                                                         data);
408                 }
409                 break;
410         }
411
412         SEARCH_FUNC_END;
413
414         return func_ret;
415 }
416
417 void search_gl_result_more_item_append_cb(void *data, Evas_Object * obj,
418                                           void *event_info)
419 {
420         SEARCH_FUNC_START;
421
422         struct appdata *ad = (struct appdata *)data;
423         Elm_Object_Item *it = (Elm_Object_Item *) event_info;
424
425         int ret = 0;
426
427         struct search_item_sel *sel_mem =
428             (search_item_sel *) elm_object_item_data_get(it);
429         struct search_item_sel *sel_mem_info = NULL;
430         struct search_item_sel *sel_mem_more = NULL;
431
432         char* escape_added_keyword = NULL;
433         SEARCH_MALLOC(escape_added_keyword, strlen(ad->search_word)*2+5, char);
434         memset(escape_added_keyword, 0x0, strlen(ad->search_word)*2+5);
435
436         int rc = 0, i = 0;
437         int type = sel_mem->type;
438         int offset = ad->cate_info[type].next_point;
439
440         search_sql_make_keyword_bind_value(ad->search_word, escape_added_keyword, SEARCH_SQL_BIND_TYPE_DUPLEX);
441
442         elm_object_item_del(it);
443
444         __search_gl_set_phone_search_result(type, offset, escape_added_keyword, ad);
445
446         SEARCH_FUNC_END;
447 }
448
449 static int __search_set_phone_result_list(struct appdata *ad, bool is_input_keyword, const char* search_keyword)
450 {
451         SEARCH_FUNC_START;
452
453         int i = 0, is_search_result = 0, ret = 0;
454
455         if (is_input_keyword == false) { 
456                 return SEARCH_RET_SEARCH_NONE;
457         } else {
458
459                 int offset = 0;
460
461                 while (i < PHONE_CATEGORY_LIST_CNT) {
462                         ret = __search_gl_set_phone_search_result(i,
463                                                                 offset,
464                                                                 (char*)search_keyword,
465                                                                 ad);
466                         if (ret == SEARCH_RET_SEARCH_SUCCESS) {
467                                 is_search_result = 1;
468                         }
469
470                         i++;
471                 }
472
473                 if(is_search_result == 0) { 
474                         return SEARCH_RET_SEARCH_NONE;
475                 } else {
476                         return SEARCH_RET_SEARCH_SUCCESS;
477                 }
478         }
479 }
480
481 Eina_Bool search_set_result_list(void *data)
482 {
483         SEARCH_FUNC_START;
484
485         struct appdata *ad = (struct appdata *)data;
486         char *text;
487         int ret = 0;
488         bool is_input_keyword = false;
489         char* escape_added_keyword = NULL;
490         int bind_type = 0;
491         int search_ret = SEARCH_RET_SEARCH_NONE;
492
493         SEARCH_ASSERT_IF(!ad);
494
495         text = elm_entry_markup_to_utf8(elm_object_text_get(ad->search_entry));
496
497         SEARCH_FREE(ad->not_markup_search_word);
498         SEARCH_FREE(ad->search_word);
499
500         if((text) && (strlen(text) > 0)) {
501                 ad->not_markup_search_word = strdup(elm_object_text_get(ad->search_entry));
502                 ad->search_word = text;
503
504                 SEARCH_MALLOC(escape_added_keyword, strlen(ad->search_word)*2+5, char);
505                 memset(escape_added_keyword, 0x0, strlen(ad->search_word)*2+5);
506
507                 bind_type = SEARCH_SQL_BIND_TYPE_DUPLEX;
508                 
509                 search_sql_make_keyword_bind_value(ad->search_word, escape_added_keyword, bind_type);
510                 is_input_keyword = true;
511
512                 elm_genlist_clear(ad->search_gl);
513
514                 search_ret = __search_set_phone_result_list(ad, is_input_keyword, ad->search_word);
515         }
516
517         if(search_ret == SEARCH_RET_SEARCH_SUCCESS) {
518                 search_layout_set_search_list(ad);
519         } else {
520                 search_layout_set_search_no_result(ad);
521         }
522         
523         SEARCH_FREE(escape_added_keyword);
524         
525         SEARCH_FUNC_END;
526
527         return EINA_FALSE;
528 }
529
530 void search_layout_set_search_list(void* data)
531 {
532         SEARCH_FUNC_START;
533
534         struct appdata *ad = (struct appdata *)data;
535
536         if (elm_object_part_content_get(ad->sb_layout,"list_noresult")) {
537                 elm_object_part_content_unset(ad->sb_layout,"list_noresult");
538                 evas_object_hide(ad->noresult_view);
539
540                 elm_object_part_content_set(ad->sb_layout, "list", ad->search_gl);
541                 evas_object_show(ad->search_gl);
542         }
543
544         elm_genlist_item_show(elm_genlist_first_item_get(ad->search_gl), ELM_GENLIST_ITEM_SCROLLTO_TOP);
545         SEARCH_FUNC_END;
546
547         return;
548 }
549
550 void search_layout_set_search_no_result(void* data)
551 {
552         SEARCH_FUNC_START;
553
554         struct appdata *ad = (struct appdata *)data;
555
556         if (elm_object_part_content_get(ad->sb_layout,"list")) {
557                 elm_object_part_content_unset(ad->sb_layout, "list");
558                 evas_object_hide(ad->search_gl);
559
560                 elm_object_part_content_set(ad->sb_layout,"list_noresult", ad->noresult_view);
561                 evas_object_show(ad->noresult_view);
562         }
563
564         SEARCH_FUNC_END;
565
566         return;
567 }
568
569 int search_layout_get_content_list_set_status(void* data)
570 {
571         SEARCH_FUNC_START;
572
573         struct appdata *ad = (struct appdata *)data;
574         int ret;
575
576         if (elm_object_part_content_get(ad->sb_layout,"list")) {
577                 ret = SEARCH_RET_SUCCESS;
578         } else {
579                 ret = SEARCH_RET_FAIL;
580         }
581
582         SEARCH_FUNC_END;
583
584         return ret;
585 }
586         
587