1 #include <Elementary.h>
4 # include "elementary_config.h"
6 #ifndef ELM_LIB_QUICKLAUNCH
7 #ifdef HAVE_ELEMENTARY_SQLITE3
12 #define ITEM_COUNT 1000
13 #define BLOCK_COUNT 10
14 #define GROUP_MAX (ITEM_COUNT/5)
16 typedef struct _My_Contact My_Contact;
17 typedef struct _Group_Title Group_Title;
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);
36 // callbacks just to see user interacting with genlist
38 _st_selected(void *data __UNUSED__, Evas_Object * obj __UNUSED__, void *event_info)
40 printf("selected: %p\n", event_info);
44 _st_clicked(void *data __UNUSED__, Evas_Object * obj __UNUSED__, void *event_info)
46 printf("clicked: %p\n", event_info);
50 _st_longpress(void *data __UNUSED__, Evas_Object * obj __UNUSED__, void *event_info)
52 printf("longpress %p\n", event_info);
55 // store callbacks to handle loading/parsing/freeing of store items from src
56 static char *group_title[GROUP_MAX];
58 static Elm_Genlist_Item_Class itc1 = {
59 "message_db", {NULL, NULL, NULL, NULL}
62 static Elm_Genlist_Item_Class itc2 = {
63 "group_title", {NULL, NULL, NULL, NULL}
66 static const Elm_Store_Item_Mapping it1_mapping[] = {
68 ELM_STORE_ITEM_MAPPING_LABEL,
69 "elm.title.1", ELM_STORE_ITEM_MAPPING_OFFSET (My_Contact, psz_name),
73 ELM_STORE_ITEM_MAPPING_LABEL,
74 "elm.title.2", ELM_STORE_ITEM_MAPPING_OFFSET (My_Contact, psz_jobtitle),
78 ELM_STORE_ITEM_MAPPING_LABEL,
79 "elm.text", ELM_STORE_ITEM_MAPPING_OFFSET (My_Contact, psz_mobile),
82 ELM_STORE_ITEM_MAPPING_END
85 static const Elm_Store_Item_Mapping it2_mapping[] = {
87 ELM_STORE_ITEM_MAPPING_CUSTOM,
90 (Elm_Store_Item_Mapping_Cb)_st_store_group_custom_label_get}}},
91 ELM_STORE_ITEM_MAPPING_END
95 _st_store_group_custom_label_get(void *data, Elm_Store_Item * sti, const char *part)
97 if (!strcmp(part, "elm.text"))
98 return strdup("group title");
103 ////// **** WARNING ***********************************************************
104 //// * This function runs inside a thread outside efl mainloop. Be careful! *
105 // ************************************************************************
107 _st_store_list(void *data __UNUSED__, Elm_Store_Item_Info * item_info)
109 if ((item_info->index % 5) == 0)
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;
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;
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;
134 item_info->data = NULL;
138 // ************************************************************************
139 //// * End of separate thread function. *
140 ////// ************************************************************************
142 ////// **** WARNING ***********************************************************
143 //// * This function runs inside a thread outside efl mainloop. Be careful! *
144 // ************************************************************************
147 _st_store_fetch(void *data __UNUSED__, Elm_Store_Item * sti, Elm_Store_Item_Info * item_info)
149 if (item_info->item_type == ELM_GENLIST_ITEM_GROUP)
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);
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, };
167 int start_idx = elm_store_item_data_index_get(sti);
169 sqlite3 *pdb = elm_store_dbsystem_db_get(sti);
171 snprintf(szbuf, BUF_SIZE, "SELECT * FROM tblEmpList ORDER BY id ASC LIMIT ?,?;");
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);
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));
184 rc = sqlite3_finalize(stmt);
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");
193 elm_store_item_data_set(sti, pmyct);
198 // ************************************************************************
199 //// * End of separate thread function. *
200 ////// ************************************************************************
202 _st_store_unfetch(void *data __UNUSED__, Elm_Store_Item * sti, Elm_Store_Item_Info * item_info)
204 if (item_info->item_type == ELM_GENLIST_ITEM_GROUP)
206 Group_Title *myit = elm_store_item_data_get(sti);
210 if (myit->group_title)
211 free(myit->group_title);
217 My_Contact *myit = elm_store_item_data_get(sti);
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);
229 elm_store_item_data_set(sti, NULL);
233 _st_store_item_select(void *data, Evas_Object * obj, void *event_info)
235 Elm_Object_Item *gli = event_info;
236 Elm_Store_Item *sti = elm_genlist_item_data_get(gli);
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);
247 test_db_store(void *data __UNUSED__, Evas_Object * obj __UNUSED__, void *event_info __UNUSED__)
249 Evas_Object *win, *bg, *gl, *bx;
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);
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);
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);
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);
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
284 int rc = sqlite3_open("./genlist.db", &p_db);
287 printf("Fail to open DB ./genlist.db\n");
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);
302 evas_object_resize(win, 480, 800);
303 evas_object_show(win);