fixed plugin image size problem
[framework/uifw/elementary.git] / src / bin / test_db_store.c
1 #include <Elementary.h>
2
3 #ifdef HAVE_CONFIG_H
4 # include "elementary_config.h"
5 #endif
6 #ifndef ELM_LIB_QUICKLAUNCH
7 #ifdef HAVE_ELEMENTARY_SQLITE3
8 #include "sqlite3.h"
9 #endif
10
11 #define BUF_SIZE 1024
12 #define ITEM_COUNT 1000
13 #define BLOCK_COUNT 10
14 #define GROUP_MAX (ITEM_COUNT/5)
15
16 typedef struct _My_Contact My_Contact;
17 typedef struct _Group_Title Group_Title;
18
19 struct _My_Contact
20 {
21    int n_id;
22    char *psz_name;
23    char *psz_jobtitle;
24    char *psz_mobile;
25 };
26
27 struct _Group_Title
28 {
29    char *group_title;
30 };
31
32 static int group_index = -1;
33 static sqlite3 *p_db = NULL;
34 static char *_st_store_group_custom_label_get(void *data, Elm_Store_Item * sti, const char *part);
35
36 // callbacks just to see user interacting with genlist
37 static void
38 _st_selected(void *data __UNUSED__, Evas_Object * obj __UNUSED__, void *event_info)
39 {
40    printf("selected: %p\n", event_info);
41 }
42
43 static void
44 _st_clicked(void *data __UNUSED__, Evas_Object * obj __UNUSED__, void *event_info)
45 {
46    printf("clicked: %p\n", event_info);
47 }
48
49 static void
50 _st_longpress(void *data __UNUSED__, Evas_Object * obj __UNUSED__, void *event_info)
51 {
52    printf("longpress %p\n", event_info);
53 }
54
55 // store callbacks to handle loading/parsing/freeing of store items from src
56 static char *group_title[GROUP_MAX];
57
58 static Elm_Genlist_Item_Class itc1 = {
59      "message_db", {NULL, NULL, NULL, NULL}
60 };
61
62 static Elm_Genlist_Item_Class itc2 = {
63      "group_title", {NULL, NULL, NULL, NULL}
64 };
65
66 static const Elm_Store_Item_Mapping it1_mapping[] = {
67        {
68           ELM_STORE_ITEM_MAPPING_LABEL,
69           "elm.title.1", ELM_STORE_ITEM_MAPPING_OFFSET (My_Contact, psz_name),
70           {.empty = {
71                        EINA_TRUE}}},
72        {
73           ELM_STORE_ITEM_MAPPING_LABEL,
74           "elm.title.2", ELM_STORE_ITEM_MAPPING_OFFSET (My_Contact, psz_jobtitle),
75           {.empty = {
76                        EINA_TRUE}}},
77        {
78           ELM_STORE_ITEM_MAPPING_LABEL,
79           "elm.text", ELM_STORE_ITEM_MAPPING_OFFSET (My_Contact, psz_mobile),
80           {.empty = {
81                        EINA_TRUE}}},
82        ELM_STORE_ITEM_MAPPING_END
83 };
84
85 static const Elm_Store_Item_Mapping it2_mapping[] = {
86        {
87           ELM_STORE_ITEM_MAPPING_CUSTOM,
88           "elm.text", 0,
89           {.custom = {
90                         (Elm_Store_Item_Mapping_Cb)_st_store_group_custom_label_get}}},
91        ELM_STORE_ITEM_MAPPING_END
92 };
93
94 static char *
95 _st_store_group_custom_label_get(void *data, Elm_Store_Item * sti, const char *part)
96 {
97    if (!strcmp(part, "elm.text"))
98      return strdup("group title");
99
100    return strdup("");
101 }
102
103 ////// **** WARNING ***********************************************************
104 ////   * This function runs inside a thread outside efl mainloop. Be careful! *
105 //     ************************************************************************
106 static Eina_Bool
107 _st_store_list(void *data __UNUSED__, Elm_Store_Item_Info * item_info)
108 {
109    if ((item_info->index % 5) == 0)
110      {
111         char gtext[128];
112         int index_label = item_info->index / 5;
113         sprintf(gtext, "group title (%d)", index_label);
114         group_title[index_label] = strdup(gtext);
115         group_index = index_label;
116
117         item_info->item_type = ELM_GENLIST_ITEM_GROUP;
118         item_info->group_index = group_index;
119         item_info->rec_item = EINA_FALSE;
120         item_info->pre_group_index = -1;
121         item_info->item_class = &itc2;
122         item_info->mapping = it2_mapping;
123      }
124    else
125      {
126         item_info->item_type = ELM_GENLIST_ITEM_NONE;
127         item_info->group_index = group_index;
128         item_info->rec_item = EINA_FALSE;
129         item_info->pre_group_index = -1;
130         item_info->item_class = &itc1;
131         item_info->mapping = it1_mapping;
132      }
133
134    item_info->data = NULL;
135    return EINA_TRUE;
136 }
137
138 //     ************************************************************************
139 ////   * End of separate thread function.                                     *
140 ////// ************************************************************************
141
142 ////// **** WARNING ***********************************************************
143 ////   * This function runs inside a thread outside efl mainloop. Be careful! *
144 //     ************************************************************************
145
146 static void
147 _st_store_fetch(void *data __UNUSED__, Elm_Store_Item * sti, Elm_Store_Item_Info * item_info)
148 {
149    if (item_info->item_type == ELM_GENLIST_ITEM_GROUP)
150      {
151         Group_Title *pGpTitle;
152         pGpTitle = calloc(1, sizeof(Group_Title));
153         pGpTitle->group_title = strdup(group_title[item_info->group_index]);
154         elm_store_item_data_set(sti, pGpTitle);
155      }
156    else
157      {
158
159         My_Contact *pmyct;
160
161         // alloc my item in memory that holds data to show in the list
162 #ifdef HAVE_ELEMENTARY_SQLITE3
163         sqlite3_stmt* stmt = NULL;
164         char szbuf[BUF_SIZE] = {0, };
165         int rc = 0;
166
167         int start_idx = elm_store_item_data_index_get(sti);
168         int fetch_count = 1;
169         sqlite3 *pdb = elm_store_dbsystem_db_get(sti);
170
171         snprintf(szbuf, BUF_SIZE, "SELECT * FROM tblEmpList ORDER BY id ASC LIMIT ?,?;");
172
173         rc = sqlite3_prepare(pdb, szbuf, strlen(szbuf), &stmt, NULL);
174         rc = sqlite3_bind_int(stmt, 1, start_idx);
175         rc = sqlite3_bind_int(stmt, 2, fetch_count);
176         rc = sqlite3_step(stmt);
177
178         pmyct = calloc(1, sizeof(My_Contact));
179         pmyct->n_id = sqlite3_column_int(stmt, 0);
180         pmyct->psz_name = strdup((const char *)sqlite3_column_text(stmt, 1));
181         pmyct->psz_jobtitle= strdup((const char *)sqlite3_column_text(stmt, 2));
182         pmyct->psz_mobile = strdup((const char *)sqlite3_column_text(stmt, 3));
183
184         rc = sqlite3_finalize(stmt);
185 #else
186         int start_idx = elm_store_item_data_index_get(sti);
187         pmyct = calloc(1, sizeof(My_Contact));
188         pmyct->n_id = start_idx;
189         pmyct->psz_name = strdup("Name");
190         pmyct->psz_jobtitle = strdup("Title");
191         pmyct->psz_mobile = strdup("Mobile");
192 #endif
193         elm_store_item_data_set(sti, pmyct);
194      }
195    return;
196 }
197
198 //     ************************************************************************
199 ////   * End of separate thread function.                                     *
200 ////// ************************************************************************
201 static void
202 _st_store_unfetch(void *data __UNUSED__, Elm_Store_Item * sti, Elm_Store_Item_Info * item_info)
203 {
204    if (item_info->item_type == ELM_GENLIST_ITEM_GROUP)
205      {
206         Group_Title *myit = elm_store_item_data_get(sti);
207
208         if (!myit)
209           return;
210         if (myit->group_title)
211           free(myit->group_title);
212         free(myit);
213
214      }
215    else
216      {
217         My_Contact *myit = elm_store_item_data_get(sti);
218
219         if (!myit)
220           return;
221         if (myit->psz_name)
222           free(myit->psz_name);
223         if (myit->psz_jobtitle)
224           free(myit->psz_jobtitle);
225         if (myit->psz_mobile)
226           free(myit->psz_mobile);
227         free(myit);
228      }
229    elm_store_item_data_set(sti, NULL);
230 }
231
232 static void
233 _st_store_item_select(void *data, Evas_Object * obj, void *event_info)
234 {
235    Elm_Object_Item *gli = event_info;
236    Elm_Store_Item *sti = elm_genlist_item_data_get(gli);
237
238    if (sti)
239      {
240         int index = elm_store_item_data_index_get(sti);
241         printf("item %d is selected\n", index);
242         elm_genlist_item_selected_set(gli, EINA_FALSE);
243      }
244 }
245
246 void
247 test_db_store(void *data __UNUSED__, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
248 {
249    Evas_Object *win, *bg, *gl, *bx;
250    Elm_Store *st;
251
252    win = elm_win_add(NULL, "db-store", ELM_WIN_BASIC);
253    elm_win_title_set(win, "Store");
254    elm_win_autodel_set(win, 1);
255
256    bg = elm_bg_add(win);
257    elm_win_resize_object_add(win, bg);
258    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
259    evas_object_show(bg);
260
261    bx = elm_box_add(win);
262    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
263    elm_win_resize_object_add(win, bx);
264    evas_object_show(bx);
265
266    gl = elm_genlist_add(win);
267    elm_genlist_homogeneous_set(gl, EINA_TRUE);
268    elm_genlist_compress_mode_set(gl, EINA_TRUE);
269    elm_genlist_block_count_set(gl, BLOCK_COUNT);
270    elm_genlist_height_for_width_mode_set(gl, EINA_TRUE);
271    evas_object_smart_callback_add(gl, "selected", _st_selected, NULL);
272    evas_object_smart_callback_add(gl, "clicked", _st_clicked, NULL);
273    evas_object_smart_callback_add(gl, "longpressed", _st_longpress, NULL);
274    evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
275    evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
276    elm_box_pack_end(bx, gl);
277    evas_object_show(gl);
278
279    // Set the db handler to store. after open it
280    // Then store will add items as you set the item count number
281 #ifdef HAVE_ELEMENTARY_SQLITE3
282    if (!p_db)
283      {
284         int rc = sqlite3_open("./genlist.db", &p_db);
285         if(SQLITE_OK != rc)
286           {
287              printf("Fail to open DB ./genlist.db\n");
288           }
289      }
290 #endif
291
292    st = elm_store_dbsystem_new();
293    elm_store_fetch_thread_set(st, EINA_TRUE);
294    elm_store_item_count_set(st, ITEM_COUNT);
295    elm_store_list_func_set(st, _st_store_list, NULL);
296    elm_store_fetch_func_set(st, _st_store_fetch, NULL);
297    elm_store_unfetch_func_set(st, _st_store_unfetch, NULL);
298    elm_store_item_select_func_set(st, (Elm_Store_Item_Select_Cb)_st_store_item_select, NULL);
299    elm_store_target_genlist_set(st, gl);
300    elm_store_dbsystem_db_set(st, p_db);
301
302    evas_object_resize(win, 480, 800);
303    evas_object_show(win);
304 }
305 #endif