Fix issue about strtok detected by static analysis tool
[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     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 static Eina_Bool _custom_back_cb(void *data, Elm_Object_Item *it)
261 {
262     _back_to_genlist_for_selector();
263     return EINA_TRUE;
264 }
265
266
267 //---------------------------------------------------------------------------------------//
268
269 static Eina_Bool _custom_back_cb2(void *data, Elm_Object_Item *it)
270 {
271     PRINTFUNC(DLOG_DEBUG, "");
272
273     if (is_content_reuse_on) {
274         int i;
275
276         if (lazy_loading_timer_for_items != NULL) {
277             ecore_timer_del(lazy_loading_timer_for_items);
278             lazy_loading_timer_for_items = NULL;
279         }
280
281         if (lazy_loading_timer_for_contents != NULL) {
282             ecore_timer_del(lazy_loading_timer_for_contents);
283             lazy_loading_timer_for_contents = NULL;
284         }
285
286         //Recent EMOTICONS : the recent emoiton need to be updated whenever emoticon view is generated, so deleted here.
287         for (i=0;i< RECENT_CNT;i++)
288         {
289             if (emoticon_recents_pool[i].used == 0 && emoticon_recents_pool[i].content) {
290                 evas_object_del(emoticon_recents_pool[i].content);
291             }
292             emoticon_recents_pool[i].content = NULL;
293         }
294     }
295
296     _back_to_genlist_for_selector();
297     return EINA_TRUE;
298 }
299
300
301 void get_recent_emoticons(vector <int> &emoticon_list)
302 {
303     int ret = PREFERENCE_ERROR_NONE;
304     char *str = NULL;
305
306     ret = preference_get_string(RECENT_EMOJI_LIST, &str);
307     if (PREFERENCE_ERROR_NONE != ret) {
308         PRINTFUNC(DLOG_ERROR, "preference_get_string error!(%d)", ret);
309     }
310
311     emoticon_list.clear();
312
313     PRINTFUNC(DLOG_DEBUG, "str = %s", str);
314
315     if (str != NULL) {
316         char *tok, *ptr;
317         tok = strtok_r(str, ",", &ptr);
318         while (tok != NULL) {
319             PRINTFUNC(DLOG_DEBUG, "tok = %s", tok);
320             emoticon_list.push_back(strtol(tok, (char **)NULL, 10));
321             tok = strtok_r(NULL, ",", &ptr);
322         }
323     }
324
325     if (str)
326         free(str);
327
328     return;
329 }
330
331 void set_recent_emoticons(vector <int> &emoticon_list, int val)
332 {
333     int i;
334     int ret = PREFERENCE_ERROR_NONE;
335
336     if (emoticon_list.size() > 0) {
337         for (i = 0; i < emoticon_list.size(); i++) {
338             PRINTFUNC(DLOG_DEBUG, "%d == %d", emoticon_list.at(i), val);
339
340             if (emoticon_list.at(i) == val) {
341                 emoticon_list.erase(emoticon_list.begin()+i);
342                 break;
343             }
344         }
345
346         if (emoticon_list.size() >= RECENT_CNT) {
347             emoticon_list.erase(emoticon_list.end());
348         }
349     }
350
351     emoticon_list.insert(emoticon_list.begin(), val);
352
353     string stored;
354     char str[10] = {0, };
355
356     for (i = 0; i < emoticon_list.size(); i++) {
357         snprintf(str, sizeof(str), "%d", emoticon_list.at(i));
358         stored += str;
359         if (i+1 != emoticon_list.size())
360             stored += ",";
361     }
362
363     PRINTFUNC(DLOG_DEBUG, "%s", stored.c_str());
364
365     ret = preference_set_string(RECENT_EMOJI_LIST, stored.c_str());
366     if (PREFERENCE_ERROR_NONE != ret) {
367         PRINTFUNC(DLOG_ERROR, "preference_set_string error!(%d)", ret);
368     }
369 }
370
371 static void _emoticon_item_clicked_cb(void *data, Evas_Object * obj, void *event_info)
372 {
373     int index = (int)data;
374
375     PRINTFUNC(DLOG_DEBUG, "index = %d", index);
376
377     // store in recents list
378     set_recent_emoticons(recent_emoji_list, index);
379
380     int length;
381     const Eina_Unicode unicode_event[2] = { emoticon_info[index].code, 0 };
382     char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
383
384     reply_to_sender_by_callback((const char*)utf_8, "emoticon");
385
386     PRINTFUNC(SECURE_DEBUG, "[%d]%s", index, utf_8);
387     if (utf_8)
388         free(utf_8);
389
390     elm_exit();
391 }
392
393 Evas_Object* get_emoticon_button(Evas_Object* parent, int index){
394     if (parent == NULL)
395         return NULL;
396
397      Evas_Object* btn = elm_button_add(parent);
398      elm_object_style_set(btn, "emoticon");
399      evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
400      evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
401
402      int length;
403      const Eina_Unicode unicode_event[2] = { emoticon_info[index].code, 0 };
404      char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
405      elm_object_part_text_set(btn, "elm.text", utf_8);
406
407      if (utf_8)
408          free(utf_8);
409
410      evas_object_layer_set(btn, 32000);
411
412      evas_object_smart_callback_add(btn, "clicked", _emoticon_item_clicked_cb, (void *)index);
413
414      return btn;
415 }
416
417 Evas_Object* get_recent_emoticon_button(Evas_Object* parent, int index){
418     if (parent == NULL)
419         return NULL;
420
421      Evas_Object* btn = elm_button_add(parent);
422      elm_object_style_set(btn, "emoticon");
423      evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
424      evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
425
426      int length;
427      const Eina_Unicode unicode_event[2] = { emoticon_info[recent_emoji_list.at(index)].code, 0 };
428      char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
429      elm_object_part_text_set(btn, "elm.text", utf_8);
430
431      evas_object_data_set(btn, "index", (void*) recent_emoji_list.at(index));
432
433      if (utf_8)
434          free(utf_8);
435
436      evas_object_layer_set(btn, 32000);
437      evas_object_smart_callback_add(btn, "clicked", _emoticon_item_clicked_cb, (void*) recent_emoji_list.at(index));
438
439      return btn;
440 }
441
442 static void _emoticon_gl_lang_changed(void *data, Evas_Object *obj, void *event_info)
443 {
444     elm_genlist_realized_items_update(obj);
445 }
446
447 static char * __emoticon_gl_text_get(void *data, Evas_Object *obj, const char *part)
448 {
449     //PRINTFUNC(DLOG_DEBUG,"part = %s", part);
450
451     const char* str = (const char*) data;
452     if (!str)
453         return NULL;
454
455     if (!strcmp(part, "elm.text")) {
456         //PRINTFUNC(DLOG_DEBUG,"str = %s", str);
457         return strdup(gettext(str));
458     }
459     return NULL;
460 }
461
462 static void _emoticon_gl_content_unswallowed_cb(void *data, Evas_Object *obj, void *event_info)
463 {
464     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
465
466     const Elm_Genlist_Item_Class *itc = elm_genlist_item_item_class_get(it);
467
468 //      PRINTFUNC(DLOG_DEBUG,"%s - stype[%s]", __func__, itc->item_style);
469     if (!strcmp(itc->item_style, "3button_flat")) {
470         int index = (int)elm_object_item_data_get(it);
471         //PRINTFUNC(DLOG_DEBUG,"it = %p", it);
472         PRINTFUNC(DLOG_DEBUG, "index = %d %d %d", index, index+1, index+2);
473
474         if (index < EMOTICON_CNT) {
475             emoticon_contents_pool[index].used = 0;
476         }
477         if (index + 1 < EMOTICON_CNT) {
478             emoticon_contents_pool[index+1].used = 0;
479         }
480         if (index + 2 < EMOTICON_CNT) {
481             emoticon_contents_pool[index+2].used = 0;
482         }
483     } else if (!strcmp(itc->item_style, "3button_flat_recent")) {
484         int index = (int)elm_object_item_data_get(it);
485         //PRINTFUNC(DLOG_DEBUG,"index = %d",index);
486
487         if (index < recent_emoji_list.size()) {
488             emoticon_recents_pool[index].used = 0;
489         }
490         if (index + 1 < recent_emoji_list.size()) {
491             emoticon_recents_pool[index+1].used = 0;
492         }
493         if (index + 2 < recent_emoji_list.size()) {
494             emoticon_recents_pool[index+2].used = 0;
495         }
496     }
497 }
498
499 static Evas_Object * __emoticon_gl_recent_content_get(void *data, Evas_Object *obj, const char *part)
500 {
501     if (is_content_reuse_on) {
502         int index = (int)data;
503         int new_index = 0;
504
505     //PRINTFUNC(DLOG_DEBUG,"%s %d", part, index);
506         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
507             if (!strcmp(part, "elm.icon.1")) {
508                 if (index >= recent_emoji_list.size()) return NULL;
509                 new_index = index;
510             } else if (!strcmp(part, "elm.icon.2")) {
511                 if (index + 1 >= recent_emoji_list.size()) return NULL;
512                 new_index = index + 1;
513             } else if (!strcmp(part, "elm.icon.3")) {
514                 if (index + 2 >= recent_emoji_list.size()) return NULL;
515                 new_index = index + 2;
516             }
517
518             Evas_Object* btn = NULL;
519             btn = (Evas_Object*)emoticon_recents_pool[new_index].content;
520             emoticon_recents_pool[new_index].used = 1;
521             return btn;
522         } else if (!strcmp(part, "base")) {
523             Evas_Object* btn = elm_button_add(obj);
524             elm_object_style_set(btn, "ime/transparent");
525             return btn;
526         }
527     } else {
528         int index = (int)data;
529         int new_index = 0;
530
531     //    PRINTFUNC(DLOG_DEBUG,"%s %d", part, index);
532         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
533             if (!strcmp(part, "elm.icon.1")) {
534                 if (index >= recent_emoji_list.size()) return NULL;
535                 new_index = index;
536             } else if (!strcmp(part, "elm.icon.2")) {
537                 if (index + 1 >= recent_emoji_list.size()) return NULL;
538                 new_index = index + 1;
539             } else if (!strcmp(part, "elm.icon.3")) {
540                 if (index + 2 >= recent_emoji_list.size()) return NULL;
541                 new_index = index + 2;
542             }
543             return get_recent_emoticon_button(obj, new_index);
544         } else if (!strcmp(part, "base")) {
545             Evas_Object* btn = elm_button_add(obj);
546             elm_object_style_set(btn, "ime/transparent");
547             return btn;
548         }
549     }
550     return NULL;
551 }
552
553 static Evas_Object * __emoticon_gl_emoticon_content_get(void *data, Evas_Object *obj, const char *part)
554 {
555     //PRINTFUNC(DLOG_DEBUG,"%s", __func__);
556
557     if (is_content_reuse_on) {
558         int index = (int)data;
559         int new_index = 0;
560
561         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
562             if (!strcmp(part, "elm.icon.1")) {
563                 if (index >= EMOTICON_CNT) return NULL;
564                 new_index = index;
565             } else if (!strcmp(part, "elm.icon.2")) {
566                 if (index + 1 >= EMOTICON_CNT) return NULL;
567                 new_index = index + 1;
568             } else if (!strcmp(part, "elm.icon.3")) {
569                 if (index + 2 >= EMOTICON_CNT) return NULL;
570                 new_index = index + 2;
571             }
572
573             Evas_Object* btn = NULL;
574             btn = (Evas_Object*)emoticon_contents_pool[new_index].content;
575             emoticon_contents_pool[new_index].used = 1;
576 #if 0
577             char utf_8[10] = {0, };
578             snprintf(utf_8, sizeof(utf_8), "%d", new_index);
579             elm_object_part_text_set(btn, "elm.text", strdup(utf_8));
580 #else
581             int length;
582             const Eina_Unicode unicode_event[2] = { emoticon_info[new_index].code, 0 };
583             char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
584             elm_object_part_text_set(btn, "elm.text", utf_8);
585             evas_object_data_set(btn, "index", (void*)new_index);
586
587             if (utf_8)
588                 free(utf_8);
589 #endif
590             return btn;
591
592         } else if (!strcmp(part, "base")) {
593             Evas_Object* btn = elm_button_add(obj);
594             elm_object_style_set(btn, "ime/transparent");
595             return btn;
596         }
597     } else {
598         int index = (int)data;
599         int new_index = 0;
600
601         if (!strcmp(part, "elm.icon.1") ||  (!strcmp(part, "elm.icon.2")) ||  (!strcmp(part, "elm.icon.3"))) {
602             if (!strcmp(part, "elm.icon.1")) {
603                 if (index >= EMOTICON_CNT) return NULL;
604                 new_index = index;
605             } else if (!strcmp(part, "elm.icon.2")) {
606                 if (index + 1 >= EMOTICON_CNT) return NULL;
607                 new_index = index + 1;
608             } else if (!strcmp(part, "elm.icon.3")) {
609                 if (index + 2 >= EMOTICON_CNT) return NULL;
610                 new_index = index + 2;
611             }
612             return get_emoticon_button(obj, new_index);
613         } else if (!strcmp(part, "base")) {
614             Evas_Object* btn = elm_button_add(obj);
615             elm_object_style_set(btn, "ime/transparent");
616             return btn;
617         }
618     }
619     return NULL;
620 }
621
622
623 void _create_reusable_recents(Evas_Object *parent)
624 {
625     if (!parent) {
626         PRINTFUNC(DLOG_ERROR, "parent is null");
627         return;
628     }
629
630     int i;
631     for (i = 0; i < recent_emoji_list.size(); i++ ) {
632         emoticon_recents_pool[i].index = i;
633         emoticon_recents_pool[i].content = get_recent_emoticon_button(parent, i);
634         emoticon_recents_pool[i].used = 0;
635     }
636 }
637
638 static Eina_Bool _lazy_loader_cb_for_contents(void *data)
639 {
640     Evas_Object *gl = (Evas_Object *)data;
641     if (!gl) {
642         PRINTFUNC(DLOG_ERROR, "gl is null");
643         lazy_loading_timer_for_contents = NULL;
644         return ECORE_CALLBACK_CANCEL;
645     }
646
647     if (loading_done_for_contents == EMOTICON_CNT) {
648         PRINTFUNC(DLOG_DEBUG, "lazy loading contents done");
649         lazy_loading_timer_for_contents = NULL;
650         return ECORE_CALLBACK_CANCEL;
651     }
652
653     int loading_top = 0;
654     if (loading_done_for_contents + LOADING_CONTENT_UNIT > EMOTICON_CNT) {
655         loading_top = EMOTICON_CNT;
656     } else {
657         loading_top = loading_done_for_contents + LOADING_CONTENT_UNIT;
658     }
659
660     PRINTFUNC(DLOG_DEBUG, "_lazy_loader_cb_for_contents loading_done = %d", loading_done_for_contents);
661
662     int i;
663     for (i = loading_done_for_contents; i < loading_top; i++) {
664         emoticon_contents_pool[i].index = i;
665         emoticon_contents_pool[i].used = 0;
666
667         if (emoticon_contents_pool[i].content == NULL) { // reusable
668             emoticon_contents_pool[i].content = get_emoticon_button(gl, i);
669         }
670     }
671
672     loading_done_for_contents = loading_top;
673
674     return ECORE_CALLBACK_RENEW;
675 }
676
677 void create_reusable_button(Evas_Object *parent)
678 {
679     if (!parent) {
680         PRINTFUNC(DLOG_ERROR, "parent is null");
681         return;
682     }
683     loading_done_for_contents = INITAL_CONTENT_UNIT;
684
685     int i;
686     for (i = 0; i < INITAL_CONTENT_UNIT; i++ ) {
687         emoticon_contents_pool[i].index = i;
688         emoticon_contents_pool[i].used = 0;
689
690         if (emoticon_contents_pool[i].content == NULL) { // reusable
691             emoticon_contents_pool[i].content = get_emoticon_button(parent, i);
692         }
693     }
694
695     lazy_loading_timer_for_contents = ecore_timer_add(0.25, _lazy_loader_cb_for_contents, (void *)parent);
696 }
697
698 static Eina_Bool _lazy_loader_cb_for_items(void *data)
699 {
700     Evas_Object *gl = (Evas_Object *)data;
701     if (!gl) {
702         PRINTFUNC(DLOG_ERROR, "gl is null");
703         lazy_loading_timer_for_items = NULL;
704         return ECORE_CALLBACK_CANCEL;
705     }
706
707     if (loading_done_for_item == EMOTICON_CNT) {
708         PRINTFUNC(DLOG_DEBUG, "lazy loading item done");
709         //elm_genlist_realized_items_update(gl);
710         elm_genlist_item_class_free(itc_emoticon);
711         lazy_loading_timer_for_items = NULL;
712
713         return ECORE_CALLBACK_CANCEL;
714     }
715
716     int loading_top = 0;
717     if (loading_done_for_item + LOADING_ITEM_UNIT > EMOTICON_CNT) {
718         loading_top = EMOTICON_CNT;
719     } else {
720         loading_top = loading_done_for_item + LOADING_ITEM_UNIT;
721     }
722
723     if (loading_top > loading_done_for_contents) {
724         PRINTFUNC(DLOG_DEBUG, "Wait for content loading");
725         return ECORE_CALLBACK_RENEW;
726     }
727     PRINTFUNC(DLOG_DEBUG, "_lazy_loader_cb_for_items loading_done_for_item = %d", loading_done_for_item);
728
729     int i;
730     for (i = loading_done_for_item; i < loading_top; i++ ) {
731         if (i%3 == 0)
732             elm_genlist_item_append(gl, itc_emoticon, (void*)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)i);
733     }
734
735     loading_done_for_item = loading_top;
736     return ECORE_CALLBACK_RENEW;
737 }
738
739 void _create_reusable_contents(Evas_Object *gl){
740     if (!gl) {
741         PRINTFUNC(DLOG_ERROR, "gl is null");
742         return;
743     }
744     int i;
745     loading_done_for_item = INITAL_ITEM_UNIT;
746
747     for (i = 0; i < INITAL_ITEM_UNIT; i++ ) {
748         if (i%3 == 0)
749             elm_genlist_item_append(gl, itc_emoticon, (void*)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)i);
750     }
751
752     lazy_loading_timer_for_items = ecore_timer_add(0.1, _lazy_loader_cb_for_items, (void *)gl);
753 }
754
755 Evas_Object* _create_emoticon_genlist(void* data)
756 {
757     PRINTFUNC(DLOG_DEBUG, "%s", __func__);
758     App_Data* ad = (App_Data*) data;
759     if (!ad)
760         return NULL;
761
762     Evas_Object* genlist = elm_genlist_add(ad->naviframe);
763     if (NULL == genlist)
764         return NULL;
765
766     Evas_Object* circle_object_genlist = eext_circle_object_genlist_add(genlist, ad->circle_surface);
767     eext_circle_object_genlist_scroller_policy_set(circle_object_genlist, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
768     evas_object_data_set(genlist, "circle", (void *) circle_object_genlist);
769     eext_rotary_object_event_activated_set(circle_object_genlist, EINA_TRUE);
770
771     evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
772     evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
773     evas_object_show(genlist);
774
775     Elm_Object_Item *nf_emoticon_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, genlist, "empty");
776
777     elm_naviframe_item_pop_cb_set(nf_emoticon_item, _custom_back_cb2, ad);
778
779     evas_object_smart_callback_add(genlist, "language,changed", _emoticon_gl_lang_changed, genlist);
780
781     if (is_content_reuse_on) {
782         evas_object_smart_callback_add(genlist, "content,unswallowed", _emoticon_gl_content_unswallowed_cb, NULL);
783     }
784
785     return genlist;
786 }
787
788 void _update_emoticon_items(void *data)
789 {
790     PRINTFUNC(DLOG_DEBUG, "%s", __func__);
791     Evas_Object* gl = (Evas_Object*) data;
792     if (!gl)
793         return;
794
795     int i;
796     Elm_Object_Item *it = NULL;
797     Elm_Object_Item *first_it = NULL;
798
799     Elm_Genlist_Item_Class * itc_dummy = elm_genlist_item_class_new();
800     itc_dummy->item_style = "title";
801     itc_dummy->func.text_get = NULL;
802     itc_dummy->func.content_get = NULL;
803     itc_dummy->func.state_get = NULL;
804     itc_dummy->func.del = NULL;
805
806     Elm_Genlist_Item_Class *itc_group = elm_genlist_item_class_new();
807
808     itc_group->item_style = "groupindex";
809     itc_group->func.text_get = __emoticon_gl_text_get;
810     itc_group->func.content_get = NULL;
811     itc_group->func.state_get = NULL;
812     itc_group->func.del = NULL;
813
814     Elm_Genlist_Item_Class *itc_recent = elm_genlist_item_class_new();
815     if (is_content_reuse_on) {
816 //        itc_recent->content_reusable = EINA_TRUE;
817     }
818     itc_recent->item_style = "3button_flat_recent";
819     itc_recent->func.text_get = NULL;
820     itc_recent->func.content_get = __emoticon_gl_recent_content_get;
821     itc_recent->func.state_get = NULL;
822     itc_recent->func.del = NULL;
823
824     itc_emoticon = elm_genlist_item_class_new();
825     if (is_content_reuse_on) {
826 //        itc_emoticon->content_reusable = EINA_TRUE;
827     }
828     itc_emoticon->item_style = "3button_flat";
829     itc_emoticon->func.text_get = NULL;
830     itc_emoticon->func.content_get = __emoticon_gl_emoticon_content_get;
831     itc_emoticon->func.state_get = NULL;
832     itc_emoticon->func.del = NULL;
833
834     // dummy title for empty space
835     it_emoticon_empty = elm_genlist_item_append(gl, itc_dummy, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
836
837     if (recent_emoji_list.size() > 0) {
838         if (is_content_reuse_on) {
839             _create_reusable_recents(gl);
840         }
841
842         // Group Recents
843         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);
844         elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
845         if (first_it == NULL)
846             first_it = it_emoticon_recent_group;
847
848         PRINTFUNC(DLOG_DEBUG, "size = %d", recent_emoji_list.size());
849
850         for (i=0;i < recent_emoji_list.size();i=i+3)
851         {
852             it = elm_genlist_item_append(gl, itc_recent, (void*)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)i);
853         }
854     }
855
856     // Group Emoticons
857     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);
858     elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
859     if (first_it == NULL)
860         first_it = it_emoticon_emoji_group;
861
862     if (is_content_reuse_on) {
863         _create_reusable_contents(gl);
864     } else {
865         // Emoticons
866         for (i=0;i< EMOTICON_CNT;i=i+3) {
867             it = elm_genlist_item_append(gl, itc_emoticon, (void*)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)i);
868             it_last = it;
869         }
870         elm_genlist_item_class_free(itc_emoticon);
871     }
872     it = elm_genlist_item_next_get(first_it);
873     const Elm_Genlist_Item_Class *itc_temp = elm_genlist_item_item_class_get(it);
874     if (itc_temp == itc_group) {
875         it = elm_genlist_item_next_get(it);
876     }
877     elm_genlist_item_show(it, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
878
879     elm_genlist_item_class_free(itc_recent);
880     elm_genlist_item_class_free(itc_group);
881     elm_genlist_item_class_free(itc_dummy);
882 }
883
884 void ise_show_emoticon_list(void *data)
885 {
886     App_Data* ad = (App_Data*) data;
887     if (!ad)
888         return;
889
890     it_emoticon_empty = NULL;
891     it_emoticon_recent_group = NULL;
892     it_emoticon_emoji_group = NULL;
893     it_last = NULL;
894
895     get_recent_emoticons(recent_emoji_list);
896
897     Evas_Object* emoticon_list = NULL;
898
899     emoticon_list = _create_emoticon_genlist(ad);
900
901     if (is_content_reuse_on) {
902         create_reusable_button(ad->naviframe); // button object need to survive even if genlist is deleted.
903     }
904     _update_emoticon_items(emoticon_list);
905 }