8ad6c45376365d3a0879485161dd09ccbd6428a8
[platform/core/uifw/inputdelegator.git] / src / w-input-emoticon.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <app_preference.h>
18 #include <Elementary.h>
19 #include <string>
20 #include <vector>
21
22 #include <vconf.h>
23 #include <vconf-keys.h>
24
25 #include "Debug.h"
26 #include "w-input-selector.h"
27
28 #define RECENT_EMOJI_LIST "recent_emoji_list"
29
30 #define EMOTICON_CNT 180
31 #define RECENT_CNT 9
32
33 extern App_Data* app_data;
34
35 static int is_content_reuse_on = 0;
36
37
38 using namespace std;
39
40 vector <int> recent_emoji_list;
41
42 typedef struct {
43     int code;
44     const char* name;
45 }Emoticon;
46
47 static Elm_Object_Item *it_emoticon_empty = NULL;
48 static Elm_Object_Item *it_emoticon_recent_group = NULL;
49 static Elm_Object_Item *it_emoticon_emoji_group = NULL;
50 static Elm_Object_Item *it_last = NULL;
51
52 static Elm_Genlist_Item_Class *itc_emoticon = NULL;
53
54 #define INITAL_ITEM_UNIT 24
55 #define LOADING_ITEM_UNIT 27
56 static int loading_done_for_item = 0;
57 Ecore_Timer *lazy_loading_timer_for_items = NULL;
58
59 #define INITAL_CONTENT_UNIT 51
60 #define LOADING_CONTENT_UNIT 9
61 static int loading_done_for_contents = 0;
62 Ecore_Timer *lazy_loading_timer_for_contents = NULL;
63
64
65 typedef struct emoticon_content
66 {
67     int index;
68     Evas_Object *content;
69     int used;
70 } emoticon_content_s;
71
72 static emoticon_content_s emoticon_contents_pool[EMOTICON_CNT] = { 0, };
73 static emoticon_content_s emoticon_recents_pool[RECENT_CNT] = { 0, };
74
75
76 Emoticon emoticon_info[EMOTICON_CNT] = {
77     {0x1f600, ""},
78     {0x1f601, ""},
79     {0x1f602, ""},
80     {0x1f603, ""},
81     {0x1f604, ""},
82     {0x1f605, ""},
83     {0x1f606, ""},
84     {0x1f609, ""},
85     {0x1f60a, ""},
86     {0x1f60b, ""},
87     {0x1f60e, ""},
88     {0x1f60d, ""},
89     {0x1f618, ""},
90     {0x1f617, ""},
91     {0x1f619, ""},
92     {0x1f61a, ""},
93     {0x263a, ""},
94     {0x1f642, ""},
95     {0x1f917, ""},
96     {0x1f607, ""},
97     {0x1f914, ""},
98     {0x1f610, ""},
99     {0x1f611, ""},
100     {0x1f636, ""},
101     {0x1f644, ""},
102     {0x1f60f, ""},
103     {0x1f623, ""},
104     {0x1f625, ""},
105     {0x1f62e, ""},
106     {0x1f910, ""},
107     {0x1f62f, ""},
108     {0x1f634, ""},
109     {0x1f62a, ""},
110     {0x1f62b, ""},
111     {0x1f60c, ""},
112     {0x1f913, ""},
113     {0x1f61b, ""},
114     {0x1f61c, ""},
115     {0x1f61d, ""},
116     {0x1f641, ""},
117     {0x1f612, ""},
118     {0x1f613, ""},
119     {0x1f614, ""},
120     {0x1f615, ""},
121     {0x1f616, ""},
122     {0x1f643, ""},
123     {0x1f637, ""},
124     {0x1f912, ""},
125     {0x1f915, ""},
126     {0x1f911, ""},
127     {0x1f632, ""},
128     {0x1f61e, ""},
129     {0x1f61f, ""},
130     {0x1f624, ""},
131     {0x1f622, ""},
132     {0x1f62d, ""},
133     {0x1f626, ""},
134     {0x1f627, ""},
135     {0x1f628, ""},
136     {0x1f629, ""},
137     {0x1f62c, ""},
138     {0x1f630, ""},
139     {0x1f631, ""},
140     {0x1f633, ""},
141     {0x1f635, ""},
142     {0x1f621, ""},
143     {0x1f620, ""},
144     {0x1f608, ""},
145     {0x1f648, ""},
146     {0x1f649, ""},
147     {0x1f64a, ""},
148     {0x1f448, ""},
149     {0x1f449, ""},
150     {0x261d, ""},
151     {0x1f446, ""},
152     {0x1f595, ""},
153     {0x1f447, ""},
154     {0x270c, ""},
155     {0x1f596, ""},
156     {0x1f918, ""},
157     {0x1f591, ""},
158     {0x1f590, ""},
159     {0x270a, ""},
160     {0x270b, ""},
161     {0x1f44a, ""},
162     {0x1f44c, ""},
163     {0x1f44d, ""},
164     {0x1f44e, ""},
165     {0x1f592, ""},
166     {0x1f593, ""},
167     {0x1f44b, ""},
168     {0x1f44f, ""},
169     {0x1f450, ""},
170     {0x1f493, ""},
171     {0x1f494, ""},
172     {0x1f495, ""},
173     {0x1f496, ""},
174     {0x1f497, ""},
175     {0x1f49d, ""},
176     {0x1f49e, ""},
177     {0x1f49f, ""},
178     {0x2763, ""},
179     {0x1f35e, ""},
180     {0x1f9c0, ""},
181     {0x1f356, ""},
182     {0x1f357, ""},
183     {0x1f354, ""},
184     {0x1f35f, ""},
185     {0x1f355, ""},
186     {0x1f32d, ""},
187     {0x1f32e, ""},
188     {0x1f32f, ""},
189     {0x1f37f, ""},
190     {0x1f372, ""},
191     {0x1f371, ""},
192     {0x1f358, ""},
193     {0x1f359, ""},
194     {0x1f35a, ""},
195     {0x1f35c, ""},
196     {0x1f35b, ""},
197     {0x1f35d, ""},
198     {0x1f360, ""},
199     {0x1f362, ""},
200     {0x1f363, ""},
201     {0x1f364, ""},
202     {0x1f365, ""},
203     {0x1f361, ""},
204     {0x1f366, ""},
205     {0x1f368, ""},
206     {0x1f367, ""},
207     {0x1f369, ""},
208     {0x1f36a, ""},
209     {0x1f382, ""},
210     {0x1f370, ""},
211     {0x1f36b, ""},
212     {0x1f36c, ""},
213     {0x1f36d, ""},
214     {0x1f36e, ""},
215     {0x1f36f, ""},
216     {0x1f37c, ""},
217     {0x2615, ""},
218     {0x1f375, ""},
219     {0x1f376, ""},
220     {0x1f37e, ""},
221     {0x1f377, ""},
222     {0x1f378, ""},
223     {0x1f379, ""},
224     {0x1f37a, ""},
225     {0x1f37b, ""},
226     {0x1f383, ""},
227     {0x1f384, ""},
228     {0x1f388, ""},
229     {0x1f389, ""},
230     {0x1f38a, ""},
231     {0x26bd, ""},
232     {0x26be, ""},
233     {0x1f3c0, ""},
234     {0x1f3c8, ""},
235     {0x1f3c9, ""},
236     {0x1f3be, ""},
237     {0x1f3b1, ""},
238     {0x1f3b3, ""},
239     {0x26f3, ""},
240     {0x26f8, ""},
241     {0x1f3a3, ""},
242     {0x1f3bf, ""},
243     {0x1f3cf, ""},
244     {0x1f3d0, ""},
245     {0x1f3d1, ""},
246     {0x1f3d2, ""},
247     {0x1f3d3, ""},
248     {0x1f3f8, ""},
249     {0x1f3af, ""},
250     {0x1f3b2, ""},
251     {0x1f3df, ""},
252     {0x1f3db, ""},
253     {0x1f3e0, ""},
254     {0x1f3e2, ""},
255     {0x1f3e5, ""},
256     {0x1f3eb, ""},
257 };
258
259
260 //---------------------------------------------------------------------------------------//
261
262 static Eina_Bool _custom_back_cb2(void *data, Elm_Object_Item *it)
263 {
264     PRINTFUNC(DLOG_DEBUG, "");
265
266     if (is_content_reuse_on) {
267         int i;
268
269         if (lazy_loading_timer_for_items != NULL) {
270             ecore_timer_del(lazy_loading_timer_for_items);
271             lazy_loading_timer_for_items = NULL;
272         }
273
274         if (lazy_loading_timer_for_contents != NULL) {
275             ecore_timer_del(lazy_loading_timer_for_contents);
276             lazy_loading_timer_for_contents = NULL;
277         }
278
279         //Recent EMOTICONS : the recent emoiton need to be updated whenever emoticon view is generated, so deleted here.
280         for (i=0;i< RECENT_CNT;i++)
281         {
282             if (emoticon_recents_pool[i].used == 0 && emoticon_recents_pool[i].content) {
283                 evas_object_del(emoticon_recents_pool[i].content);
284             }
285             emoticon_recents_pool[i].content = NULL;
286         }
287     }
288
289     _back_to_genlist_for_selector();
290     return EINA_TRUE;
291 }
292
293
294 void get_recent_emoticons(vector <int> &emoticon_list)
295 {
296     int ret = PREFERENCE_ERROR_NONE;
297     char *str = NULL;
298
299     ret = preference_get_string(RECENT_EMOJI_LIST, &str);
300     if (PREFERENCE_ERROR_NONE != ret) {
301         PRINTFUNC(DLOG_ERROR, "preference_get_string error!(%d)", ret);
302     }
303
304     emoticon_list.clear();
305
306     PRINTFUNC(DLOG_DEBUG, "str = %s", str);
307
308     if (str != NULL) {
309         char *tok, *ptr;
310         tok = strtok_r(str, ",", &ptr);
311         while (tok != NULL) {
312             PRINTFUNC(DLOG_DEBUG, "tok = %s", tok);
313             emoticon_list.push_back(strtol(tok, (char **)NULL, 10));
314             tok = strtok_r(NULL, ",", &ptr);
315         }
316     }
317
318     if (str)
319         free(str);
320
321     return;
322 }
323
324 void set_recent_emoticons(vector <int> &emoticon_list, int val)
325 {
326     unsigned int i;
327     int ret = PREFERENCE_ERROR_NONE;
328
329     if (emoticon_list.size() > 0) {
330         for (i = 0; i < emoticon_list.size(); i++) {
331             PRINTFUNC(DLOG_DEBUG, "%d == %d", emoticon_list.at(i), val);
332
333             if (emoticon_list.at(i) == val) {
334                 emoticon_list.erase(emoticon_list.begin()+i);
335                 break;
336             }
337         }
338
339         if (emoticon_list.size() >= RECENT_CNT) {
340             emoticon_list.erase(emoticon_list.end());
341         }
342     }
343
344     emoticon_list.insert(emoticon_list.begin(), val);
345
346     string stored;
347     char str[10] = {0, };
348
349     for (i = 0; i < emoticon_list.size(); i++) {
350         snprintf(str, sizeof(str), "%d", emoticon_list.at(i));
351         stored += str;
352         if (i+1 != emoticon_list.size())
353             stored += ",";
354     }
355
356     PRINTFUNC(DLOG_DEBUG, "%s", stored.c_str());
357
358     ret = preference_set_string(RECENT_EMOJI_LIST, stored.c_str());
359     if (PREFERENCE_ERROR_NONE != ret) {
360         PRINTFUNC(DLOG_ERROR, "preference_set_string error!(%d)", ret);
361     }
362 }
363
364 static void _emoticon_item_clicked_cb(void *data, Evas_Object * obj, void *event_info)
365 {
366     int index = (int)data;
367
368     PRINTFUNC(DLOG_DEBUG, "index = %d", index);
369
370     // store in recents list
371     set_recent_emoticons(recent_emoji_list, index);
372
373     int length;
374     const Eina_Unicode unicode_event[2] = { (unsigned int)emoticon_info[index].code, 0 };
375     char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
376
377     reply_to_sender_by_callback((const char*)utf_8, "emoticon");
378
379     PRINTFUNC(SECURE_DEBUG, "[%d]%s", index, utf_8);
380     if (utf_8)
381         free(utf_8);
382
383     elm_exit();
384 }
385
386 Evas_Object* get_emoticon_button(Evas_Object* parent, int index){
387     if (parent == NULL)
388         return NULL;
389
390      Evas_Object* btn = elm_button_add(parent);
391      elm_object_style_set(btn, "emoticon");
392      evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
393      evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
394
395      int length;
396      const Eina_Unicode unicode_event[2] = { (unsigned int)emoticon_info[index].code, 0 };
397      char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
398      elm_object_part_text_set(btn, "elm.text", utf_8);
399
400      if (utf_8)
401          free(utf_8);
402
403      evas_object_layer_set(btn, 32000);
404
405      evas_object_smart_callback_add(btn, "clicked", _emoticon_item_clicked_cb, (void *)index);
406
407      return btn;
408 }
409
410 Evas_Object* get_recent_emoticon_button(Evas_Object* parent, int index){
411     if (parent == NULL)
412         return NULL;
413
414      Evas_Object* btn = elm_button_add(parent);
415      elm_object_style_set(btn, "emoticon");
416      evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
417      evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
418
419      int length;
420      const Eina_Unicode unicode_event[2] = { (unsigned int)emoticon_info[recent_emoji_list.at(index)].code, 0 };
421      char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
422      elm_object_part_text_set(btn, "elm.text", utf_8);
423
424      evas_object_data_set(btn, "index", (void*) recent_emoji_list.at(index));
425
426      if (utf_8)
427          free(utf_8);
428
429      evas_object_layer_set(btn, 32000);
430      evas_object_smart_callback_add(btn, "clicked", _emoticon_item_clicked_cb, (void*) recent_emoji_list.at(index));
431
432      return btn;
433 }
434
435 static void _emoticon_gl_lang_changed(void *data, Evas_Object *obj, void *event_info)
436 {
437     elm_genlist_realized_items_update(obj);
438 }
439
440 static char * __emoticon_gl_text_get(void *data, Evas_Object *obj, const char *part)
441 {
442     //PRINTFUNC(DLOG_DEBUG,"part = %s", part);
443
444     const char* str = (const char*) data;
445     if (!str)
446         return NULL;
447
448     if (!strcmp(part, "elm.text")) {
449         //PRINTFUNC(DLOG_DEBUG,"str = %s", str);
450         return strdup(gettext(str));
451     }
452     return NULL;
453 }
454
455 static void _emoticon_gl_content_unswallowed_cb(void *data, Evas_Object *obj, void *event_info)
456 {
457     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
458
459     const Elm_Genlist_Item_Class *itc = elm_genlist_item_item_class_get(it);
460
461 //      PRINTFUNC(DLOG_DEBUG,"%s - stype[%s]", __func__, itc->item_style);
462     if (!strcmp(itc->item_style, "3button_flat")) {
463         int index = (int)elm_object_item_data_get(it);
464         //PRINTFUNC(DLOG_DEBUG,"it = %p", it);
465         PRINTFUNC(DLOG_DEBUG, "index = %d %d %d", index, index+1, index+2);
466
467         if (index < EMOTICON_CNT) {
468             emoticon_contents_pool[index].used = 0;
469         }
470         if (index + 1 < EMOTICON_CNT) {
471             emoticon_contents_pool[index+1].used = 0;
472         }
473         if (index + 2 < EMOTICON_CNT) {
474             emoticon_contents_pool[index+2].used = 0;
475         }
476     } else if (!strcmp(itc->item_style, "3button_flat_recent")) {
477         unsigned int index = (int)elm_object_item_data_get(it);
478         //PRINTFUNC(DLOG_DEBUG,"index = %d",index);
479
480         if (index < recent_emoji_list.size()) {
481             emoticon_recents_pool[index].used = 0;
482         }
483         if (index + 1 < recent_emoji_list.size()) {
484             emoticon_recents_pool[index+1].used = 0;
485         }
486         if (index + 2 < recent_emoji_list.size()) {
487             emoticon_recents_pool[index+2].used = 0;
488         }
489     }
490 }
491
492 static Evas_Object * __emoticon_gl_recent_content_get(void *data, Evas_Object *obj, const char *part)
493 {
494     if (is_content_reuse_on) {
495         unsigned int index = (unsigned int)data;
496         int new_index = 0;
497
498     //PRINTFUNC(DLOG_DEBUG,"%s %d", part, index);
499         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
500             if (!strcmp(part, "elm.icon.1")) {
501                 if (index >= recent_emoji_list.size()) return NULL;
502                 new_index = index;
503             } else if (!strcmp(part, "elm.icon.2")) {
504                 if (index + 1 >= recent_emoji_list.size()) return NULL;
505                 new_index = index + 1;
506             } else if (!strcmp(part, "elm.icon.3")) {
507                 if (index + 2 >= recent_emoji_list.size()) return NULL;
508                 new_index = index + 2;
509             }
510
511             Evas_Object* btn = NULL;
512             btn = (Evas_Object*)emoticon_recents_pool[new_index].content;
513             emoticon_recents_pool[new_index].used = 1;
514             return btn;
515         } else if (!strcmp(part, "base")) {
516             Evas_Object* btn = elm_button_add(obj);
517             elm_object_style_set(btn, "ime/transparent");
518             return btn;
519         }
520     } else {
521         unsigned int index = (unsigned int)data;
522         int new_index = 0;
523
524     //    PRINTFUNC(DLOG_DEBUG,"%s %d", part, index);
525         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
526             if (!strcmp(part, "elm.icon.1")) {
527                 if (index >= recent_emoji_list.size()) return NULL;
528                 new_index = index;
529             } else if (!strcmp(part, "elm.icon.2")) {
530                 if (index + 1 >= recent_emoji_list.size()) return NULL;
531                 new_index = index + 1;
532             } else if (!strcmp(part, "elm.icon.3")) {
533                 if (index + 2 >= recent_emoji_list.size()) return NULL;
534                 new_index = index + 2;
535             }
536             return get_recent_emoticon_button(obj, new_index);
537         } else if (!strcmp(part, "base")) {
538             Evas_Object* btn = elm_button_add(obj);
539             elm_object_style_set(btn, "ime/transparent");
540             return btn;
541         }
542     }
543     return NULL;
544 }
545
546 static Evas_Object * __emoticon_gl_emoticon_content_get(void *data, Evas_Object *obj, const char *part)
547 {
548     //PRINTFUNC(DLOG_DEBUG,"%s", __func__);
549
550     if (is_content_reuse_on) {
551         int index = (int)data;
552         int new_index = 0;
553
554         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
555             if (!strcmp(part, "elm.icon.1")) {
556                 if (index >= EMOTICON_CNT) return NULL;
557                 new_index = index;
558             } else if (!strcmp(part, "elm.icon.2")) {
559                 if (index + 1 >= EMOTICON_CNT) return NULL;
560                 new_index = index + 1;
561             } else if (!strcmp(part, "elm.icon.3")) {
562                 if (index + 2 >= EMOTICON_CNT) return NULL;
563                 new_index = index + 2;
564             }
565
566             Evas_Object* btn = NULL;
567             btn = (Evas_Object*)emoticon_contents_pool[new_index].content;
568             emoticon_contents_pool[new_index].used = 1;
569 #if 0
570             char utf_8[10] = {0, };
571             snprintf(utf_8, sizeof(utf_8), "%d", new_index);
572             elm_object_part_text_set(btn, "elm.text", strdup(utf_8));
573 #else
574             int length;
575             const Eina_Unicode unicode_event[2] = { (unsigned int)emoticon_info[new_index].code, 0 };
576             char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
577             elm_object_part_text_set(btn, "elm.text", utf_8);
578             evas_object_data_set(btn, "index", (void*)new_index);
579
580             if (utf_8)
581                 free(utf_8);
582 #endif
583             return btn;
584
585         } else if (!strcmp(part, "base")) {
586             Evas_Object* btn = elm_button_add(obj);
587             elm_object_style_set(btn, "ime/transparent");
588             return btn;
589         }
590     } else {
591         int index = (int)data;
592         int new_index = 0;
593
594         if (!strcmp(part, "elm.icon.1") ||  (!strcmp(part, "elm.icon.2")) ||  (!strcmp(part, "elm.icon.3"))) {
595             if (!strcmp(part, "elm.icon.1")) {
596                 if (index >= EMOTICON_CNT) return NULL;
597                 new_index = index;
598             } else if (!strcmp(part, "elm.icon.2")) {
599                 if (index + 1 >= EMOTICON_CNT) return NULL;
600                 new_index = index + 1;
601             } else if (!strcmp(part, "elm.icon.3")) {
602                 if (index + 2 >= EMOTICON_CNT) return NULL;
603                 new_index = index + 2;
604             }
605             return get_emoticon_button(obj, new_index);
606         } else if (!strcmp(part, "base")) {
607             Evas_Object* btn = elm_button_add(obj);
608             elm_object_style_set(btn, "ime/transparent");
609             return btn;
610         }
611     }
612     return NULL;
613 }
614
615
616 void _create_reusable_recents(Evas_Object *parent)
617 {
618     if (!parent) {
619         PRINTFUNC(DLOG_ERROR, "parent is null");
620         return;
621     }
622
623     unsigned int i;
624     for (i = 0; i < recent_emoji_list.size(); i++ ) {
625         emoticon_recents_pool[i].index = i;
626         emoticon_recents_pool[i].content = get_recent_emoticon_button(parent, i);
627         emoticon_recents_pool[i].used = 0;
628     }
629 }
630
631 static Eina_Bool _lazy_loader_cb_for_contents(void *data)
632 {
633     Evas_Object *gl = (Evas_Object *)data;
634     if (!gl) {
635         PRINTFUNC(DLOG_ERROR, "gl is null");
636         lazy_loading_timer_for_contents = NULL;
637         return ECORE_CALLBACK_CANCEL;
638     }
639
640     if (loading_done_for_contents == EMOTICON_CNT) {
641         PRINTFUNC(DLOG_DEBUG, "lazy loading contents done");
642         lazy_loading_timer_for_contents = NULL;
643         return ECORE_CALLBACK_CANCEL;
644     }
645
646     int loading_top = 0;
647     if (loading_done_for_contents + LOADING_CONTENT_UNIT > EMOTICON_CNT) {
648         loading_top = EMOTICON_CNT;
649     } else {
650         loading_top = loading_done_for_contents + LOADING_CONTENT_UNIT;
651     }
652
653     PRINTFUNC(DLOG_DEBUG, "_lazy_loader_cb_for_contents loading_done = %d", loading_done_for_contents);
654
655     int i;
656     for (i = loading_done_for_contents; i < loading_top; i++) {
657         emoticon_contents_pool[i].index = i;
658         emoticon_contents_pool[i].used = 0;
659
660         if (emoticon_contents_pool[i].content == NULL) { // reusable
661             emoticon_contents_pool[i].content = get_emoticon_button(gl, i);
662         }
663     }
664
665     loading_done_for_contents = loading_top;
666
667     return ECORE_CALLBACK_RENEW;
668 }
669
670 void create_reusable_button(Evas_Object *parent)
671 {
672     if (!parent) {
673         PRINTFUNC(DLOG_ERROR, "parent is null");
674         return;
675     }
676     loading_done_for_contents = INITAL_CONTENT_UNIT;
677
678     int i;
679     for (i = 0; i < INITAL_CONTENT_UNIT; i++ ) {
680         emoticon_contents_pool[i].index = i;
681         emoticon_contents_pool[i].used = 0;
682
683         if (emoticon_contents_pool[i].content == NULL) { // reusable
684             emoticon_contents_pool[i].content = get_emoticon_button(parent, i);
685         }
686     }
687
688     lazy_loading_timer_for_contents = ecore_timer_add(0.25, _lazy_loader_cb_for_contents, (void *)parent);
689 }
690
691 static Eina_Bool _lazy_loader_cb_for_items(void *data)
692 {
693     Evas_Object *gl = (Evas_Object *)data;
694     if (!gl) {
695         PRINTFUNC(DLOG_ERROR, "gl is null");
696         lazy_loading_timer_for_items = NULL;
697         return ECORE_CALLBACK_CANCEL;
698     }
699
700     if (loading_done_for_item == EMOTICON_CNT) {
701         PRINTFUNC(DLOG_DEBUG, "lazy loading item done");
702         //elm_genlist_realized_items_update(gl);
703         elm_genlist_item_class_free(itc_emoticon);
704         lazy_loading_timer_for_items = NULL;
705
706         return ECORE_CALLBACK_CANCEL;
707     }
708
709     int loading_top = 0;
710     if (loading_done_for_item + LOADING_ITEM_UNIT > EMOTICON_CNT) {
711         loading_top = EMOTICON_CNT;
712     } else {
713         loading_top = loading_done_for_item + LOADING_ITEM_UNIT;
714     }
715
716     if (loading_top > loading_done_for_contents) {
717         PRINTFUNC(DLOG_DEBUG, "Wait for content loading");
718         return ECORE_CALLBACK_RENEW;
719     }
720     PRINTFUNC(DLOG_DEBUG, "_lazy_loader_cb_for_items loading_done_for_item = %d", loading_done_for_item);
721
722     int i;
723     for (i = loading_done_for_item; i < loading_top; i++ ) {
724         if (i%3 == 0)
725             elm_genlist_item_append(gl, itc_emoticon, (void*)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)i);
726     }
727
728     loading_done_for_item = loading_top;
729     return ECORE_CALLBACK_RENEW;
730 }
731
732 void _create_reusable_contents(Evas_Object *gl){
733     if (!gl) {
734         PRINTFUNC(DLOG_ERROR, "gl is null");
735         return;
736     }
737     int i;
738     loading_done_for_item = INITAL_ITEM_UNIT;
739
740     for (i = 0; i < INITAL_ITEM_UNIT; i++ ) {
741         if (i%3 == 0)
742             elm_genlist_item_append(gl, itc_emoticon, (void*)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)i);
743     }
744
745     lazy_loading_timer_for_items = ecore_timer_add(0.1, _lazy_loader_cb_for_items, (void *)gl);
746 }
747
748 Evas_Object* _create_emoticon_genlist(void* data)
749 {
750     PRINTFUNC(DLOG_DEBUG, "%s", __func__);
751     App_Data* ad = (App_Data*) data;
752     if (!ad)
753         return NULL;
754
755     Evas_Object* genlist = elm_genlist_add(ad->naviframe);
756     if (NULL == genlist)
757         return NULL;
758
759     Evas_Object* circle_object_genlist = eext_circle_object_genlist_add(genlist, ad->circle_surface);
760     eext_circle_object_genlist_scroller_policy_set(circle_object_genlist, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
761     evas_object_data_set(genlist, "circle", (void *) circle_object_genlist);
762     eext_rotary_object_event_activated_set(circle_object_genlist, EINA_TRUE);
763
764     evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
765     evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
766     evas_object_show(genlist);
767
768     Elm_Object_Item *nf_emoticon_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, genlist, "empty");
769
770     elm_naviframe_item_pop_cb_set(nf_emoticon_item, _custom_back_cb2, ad);
771
772     evas_object_smart_callback_add(genlist, "language,changed", _emoticon_gl_lang_changed, genlist);
773
774     if (is_content_reuse_on) {
775         evas_object_smart_callback_add(genlist, "content,unswallowed", _emoticon_gl_content_unswallowed_cb, NULL);
776     }
777
778     return genlist;
779 }
780
781 void _update_emoticon_items(void *data)
782 {
783     PRINTFUNC(DLOG_DEBUG, "%s", __func__);
784     Evas_Object* gl = (Evas_Object*) data;
785     if (!gl)
786         return;
787
788     unsigned int i;
789     Elm_Object_Item *it = NULL;
790     Elm_Object_Item *first_it = NULL;
791
792     Elm_Genlist_Item_Class * itc_dummy = elm_genlist_item_class_new();
793     itc_dummy->item_style = "title";
794     itc_dummy->func.text_get = NULL;
795     itc_dummy->func.content_get = NULL;
796     itc_dummy->func.state_get = NULL;
797     itc_dummy->func.del = NULL;
798
799     Elm_Genlist_Item_Class *itc_group = elm_genlist_item_class_new();
800
801     itc_group->item_style = "groupindex";
802     itc_group->func.text_get = __emoticon_gl_text_get;
803     itc_group->func.content_get = NULL;
804     itc_group->func.state_get = NULL;
805     itc_group->func.del = NULL;
806
807     Elm_Genlist_Item_Class *itc_recent = elm_genlist_item_class_new();
808     if (is_content_reuse_on) {
809 //        itc_recent->content_reusable = EINA_TRUE;
810     }
811     itc_recent->item_style = "3button_flat_recent";
812     itc_recent->func.text_get = NULL;
813     itc_recent->func.content_get = __emoticon_gl_recent_content_get;
814     itc_recent->func.state_get = NULL;
815     itc_recent->func.del = NULL;
816
817     itc_emoticon = elm_genlist_item_class_new();
818     if (is_content_reuse_on) {
819 //        itc_emoticon->content_reusable = EINA_TRUE;
820     }
821     itc_emoticon->item_style = "3button_flat";
822     itc_emoticon->func.text_get = NULL;
823     itc_emoticon->func.content_get = __emoticon_gl_emoticon_content_get;
824     itc_emoticon->func.state_get = NULL;
825     itc_emoticon->func.del = NULL;
826
827     // dummy title for empty space
828     it_emoticon_empty = elm_genlist_item_append(gl, itc_dummy, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
829
830     if (recent_emoji_list.size() > 0) {
831         if (is_content_reuse_on) {
832             _create_reusable_recents(gl);
833         }
834
835         // Group Recents
836         it_emoticon_recent_group = elm_genlist_item_append(gl, itc_group, (void*)"IDS_IME_HEADER_RECENT_M_RECETLY_SENT_EMOJIS_ABB", NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)2);
837         elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
838         if (first_it == NULL)
839             first_it = it_emoticon_recent_group;
840
841         PRINTFUNC(DLOG_DEBUG, "size = %d", recent_emoji_list.size());
842
843         for (i=0;i < recent_emoji_list.size();i=i+3)
844         {
845             it = elm_genlist_item_append(gl, itc_recent, (void*)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)i);
846         }
847     }
848
849     // Group Emoticons
850     it_emoticon_emoji_group = elm_genlist_item_append(gl, itc_group, (void*)"IDS_IME_HEADER_EMOJIS_ABB", NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)2);
851     elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
852     if (first_it == NULL)
853         first_it = it_emoticon_emoji_group;
854
855     if (is_content_reuse_on) {
856         _create_reusable_contents(gl);
857     } else {
858         // Emoticons
859         for (i=0;i< EMOTICON_CNT;i=i+3) {
860             it = elm_genlist_item_append(gl, itc_emoticon, (void*)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)i);
861             it_last = it;
862         }
863         elm_genlist_item_class_free(itc_emoticon);
864     }
865     it = elm_genlist_item_next_get(first_it);
866     const Elm_Genlist_Item_Class *itc_temp = elm_genlist_item_item_class_get(it);
867     if (itc_temp == itc_group) {
868         it = elm_genlist_item_next_get(it);
869     }
870     elm_genlist_item_show(it, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
871
872     elm_genlist_item_class_free(itc_recent);
873     elm_genlist_item_class_free(itc_group);
874     elm_genlist_item_class_free(itc_dummy);
875 }
876
877 void ise_show_emoticon_list(void *data)
878 {
879     App_Data* ad = (App_Data*) data;
880     if (!ad)
881         return;
882
883     it_emoticon_empty = NULL;
884     it_emoticon_recent_group = NULL;
885     it_emoticon_emoji_group = NULL;
886     it_last = NULL;
887
888     get_recent_emoticons(recent_emoji_list);
889
890     Evas_Object* emoticon_list = NULL;
891
892     emoticon_list = _create_emoticon_genlist(ad);
893
894     if (is_content_reuse_on) {
895         create_reusable_button(ad->naviframe); // button object need to survive even if genlist is deleted.
896     }
897     _update_emoticon_items(emoticon_list);
898 }