tizen 2.3 release
[apps/home/ug-nfc-efl.git] / ug-nfc-setting-efl / src / ug-nfc-setting-predefined-item.c
1 /*
2   * Copyright (c) 2012, 2013 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://floralicense.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 #include <glib.h>
19
20 #include "ug-nfc-setting-main.h"
21 #include "ug-nfc-setting-db.h"
22
23
24 typedef struct {
25         char *app_id;
26         char *pkgName;
27         char *iconPath;
28         char *msgDes;
29 } pd_data;
30
31
32 static int dbtable_h;
33 static int pd_item_cnt;
34 static GArray *pd_item_l;
35
36 static Elm_Genlist_Item_Class itc_sep;
37 static Elm_Genlist_Item_Class itc_1text2icon;
38
39 static void __nfc_vconf_changed_cb(keynode_t *key, void *data)
40 {
41         ugdata_t *ug_data = (ugdata_t *)data;
42         Elm_Object_Item *it, *next;
43         int boolval;
44
45         if (!ug_data || !key->keyname)
46                 return;
47
48         LOGD("key->keyname [%s]", key->keyname);
49
50         if (strncmp(key->keyname, VCONFKEY_NFC_STATE,
51                         strlen(VCONFKEY_NFC_STATE)) != 0) {
52                 return;
53         }
54
55         if (!vconf_get_bool(key->keyname, &boolval) && !boolval) {
56                 if (vconf_set_bool(VCONFKEY_NFC_PREDEFINED_ITEM_STATE,
57                         VCONFKEY_NFC_PREDEFINED_ITEM_OFF)) {
58                                 LOGE("vconf_set_bool failed");
59                 }
60                 elm_check_state_set(ug_data->pd_on_off, EINA_FALSE);
61         }
62
63         if (!vconf_get_bool(VCONFKEY_NFC_PREDEFINED_ITEM_STATE, &boolval)) {
64                 it = elm_genlist_first_item_get(ug_data->pd_genlist);
65                 while (it != NULL) {
66                         next = elm_genlist_item_next_get(it);
67
68                         if (boolval)
69                                 elm_object_item_disabled_set(it, EINA_FALSE);
70                         else
71                                 elm_object_item_disabled_set(it, EINA_TRUE);
72
73                         it = next;
74                 }
75         }
76 }
77
78 static void __title_pd_on_off_clicked_cb(void *data, Evas_Object *obj,
79         void *event_info)
80 {
81         ugdata_t *ug_data = (ugdata_t *)data;
82         Elm_Object_Item *it, *next;
83         int boolval;
84
85         if (ug_data == NULL) {
86                 LOGE("data is null");
87                 return;
88         }
89
90         __change_predefined_item_onoff_setting(ug_data);
91
92         if (!vconf_get_bool(VCONFKEY_NFC_PREDEFINED_ITEM_STATE, &boolval)) {
93                 it = elm_genlist_first_item_get(ug_data->pd_genlist);
94                 while (it != NULL) {
95                         next = elm_genlist_item_next_get(it);
96
97                         if (boolval)
98                                 elm_object_item_disabled_set(it, EINA_FALSE);
99                         else
100                                 elm_object_item_disabled_set(it, EINA_TRUE);
101
102                         it = next;
103                 }
104         }
105 }
106
107 static char *__gl_text_get(void *data, Evas_Object *obj, const char *part)
108 {
109         int index = (int) data;
110         pd_data *item = NULL;
111         char *text = NULL;
112
113         //LOGD("index : %d", index);
114         //LOGD("[Genlist] part : %s", part);
115
116         if (!strcmp(part, "elm.text") || !strcmp(part, "elm.text.1")) {
117                 item = g_array_index(pd_item_l, pd_data *, index);
118                 if ((item != NULL) && (item->msgDes != NULL)) {
119                         text = strdup(item->msgDes);
120                         //LOGD("msgDes : %s", item->msgDes);
121                 }
122
123         }
124
125         return text;
126 }
127
128 static Evas_Object *__gl_content_get(void *data, Evas_Object *obj,
129         const char *part)
130 {
131         int index = (int) data;
132         pd_data *item = NULL;
133         Evas_Object *content = NULL;
134         Evas_Object *radio_group;
135
136         //LOGD("[Genlist] index : %d", index);
137         //LOGD("[Genlist] part : %s", part);
138
139         if (!strcmp(part, "elm.icon") || !strcmp(part, "elm.icon.1")) {
140                 item = g_array_index(pd_item_l, pd_data *, index);
141
142                 radio_group = evas_object_data_get(obj, "radio");
143                 content = elm_radio_add(obj);
144                 elm_radio_state_value_set(content, index);
145                 elm_radio_group_add(content, radio_group);
146
147                 if ((item != NULL) && (item->app_id != NULL)) {
148                         char *app_id;
149
150                         app_id = vconf_get_str(VCONFKEY_NFC_PREDEFINED_ITEM);
151                         if (app_id != NULL) {
152                                 if (!strcmp(app_id, item->app_id)) {
153                                         elm_radio_value_set(content, index);
154                                 }
155                                 free(app_id);
156                         }
157                 }
158
159                 evas_object_size_hint_weight_set(content, EVAS_HINT_EXPAND,
160                         EVAS_HINT_EXPAND);
161                 evas_object_size_hint_align_set(content, EVAS_HINT_FILL,
162                         EVAS_HINT_FILL);
163
164         } else {
165                 item = g_array_index(pd_item_l, pd_data *, index);
166
167                 if ((item != NULL) && (item->iconPath != NULL)) {
168                         content = elm_image_add(obj);
169                         elm_image_file_set(content, item->iconPath, NULL);
170                         elm_image_resizable_set(content, EINA_TRUE, EINA_TRUE);
171                 }
172         }
173
174         return content;
175 }
176
177 static Eina_Bool __gl_state_get(void *data, Evas_Object *obj,
178         const char *part)
179 {
180         return EINA_FALSE;
181 }
182
183 static void __gl_sel(void *data, Evas_Object *obj, void *event_info)
184 {
185         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
186         int index;
187         pd_data *pd_item = NULL;
188
189
190         if (!item)
191                 return;
192
193         elm_genlist_item_selected_set(item, 0);
194
195         index = (int) elm_object_item_data_get(item);
196
197         pd_item = g_array_index(pd_item_l, pd_data *, index);
198
199         if ((pd_item != NULL) && (pd_item->app_id != NULL)) {
200                 if (vconf_set_str(VCONFKEY_NFC_PREDEFINED_ITEM,
201                         pd_item->app_id)) {
202                         LOGE("vconf_set_str failed");
203                 }
204         }
205         elm_genlist_item_update(item);
206 }
207
208 static void __create_pd_data(void)
209 {
210         pd_data *item = NULL;
211         int index;
212
213         if (_ug_nfc_setting_db_open_table(&dbtable_h)) {
214                 LOGE("_ug_nfc_setting_db_open_table is failed!!!!!");
215                 return;
216         }
217
218         /* get item count */
219         _ug_nfc_setting_db_get_count(&pd_item_cnt);
220         LOGD("pd_item_cnt [%d]", pd_item_cnt);
221
222         /* create item list */
223         pd_item_l = g_array_new(FALSE, FALSE,
224                 sizeof(pd_data *));
225
226         for (index = 0; index < pd_item_cnt; index++) {
227                 item = (pd_data *) malloc(sizeof(pd_data));
228                 if (!item) {
229                         LOGE("failed to allocation");
230                         continue;
231                 }
232
233                 if (!_ug_nfc_setting_db_get_next_record(dbtable_h,
234                         &item->app_id,
235                         &item->pkgName,
236                         &item->iconPath,
237                         &item->msgDes)) {
238
239                         //LOGD("app_id:%s, pkgName:%s, iconPath:%s, msgDes:%s", item->app_id, item->pkgName, item->iconPath, item->msgDes);
240
241                         g_array_append_val(pd_item_l, item);
242                 } else {
243                         free(item);
244                 }
245         }
246
247         _ug_nfc_setting_db_close_table(dbtable_h);
248 }
249
250 static void __destroy_pd_data(void)
251 {
252         if (pd_item_l != NULL) {
253                 pd_data *old_info = NULL;
254                 int index;
255
256                 for (index = 0; index < pd_item_cnt; index++) {
257                         old_info = g_array_index(pd_item_l, pd_data *, index);
258
259                         if (old_info != NULL) {
260                                 free(old_info->app_id);
261                                 free(old_info->pkgName);
262                                 free(old_info->iconPath);
263                                 free(old_info->msgDes);
264
265                                 free(old_info);
266                         }
267                 }
268                 g_array_free(pd_item_l, FALSE);
269         }
270 }
271
272 static void __genlist_free_cb(void *data, Evas *e, Evas_Object *obj,
273         void *ei)
274 {
275         Evas_Object *radio_group = evas_object_data_get(obj, "radio");
276
277         if (radio_group)
278                 evas_object_del(radio_group);
279 }
280
281 Evas_Object *__create_predefined_item_list(void *data)
282 {
283         ugdata_t *ug_data = (ugdata_t *)data;
284         Evas_Object *genlist, *radio_group;
285         Elm_Object_Item* separator;
286         int index, boolval;
287
288         /* create item data */
289         __create_pd_data();
290
291         /* make a list */
292         genlist = elm_genlist_add(ug_data->base_naviframe);
293         if (!genlist) {
294                 LOGE("genlist is null");
295                 return NULL;
296         }
297
298         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
299         evas_object_event_callback_add(genlist, EVAS_CALLBACK_FREE,
300                 __genlist_free_cb, NULL);
301
302
303         radio_group = elm_radio_add(genlist);
304         elm_radio_state_value_set(radio_group, 0);
305         elm_radio_value_set(radio_group, 0);
306         evas_object_data_set(genlist, "radio", radio_group);
307
308         itc_sep.item_style = "dialogue/separator";
309         itc_sep.func.text_get = NULL;
310         itc_sep.func.content_get = NULL;
311         itc_sep.func.state_get = NULL;
312         itc_sep.func.del = NULL;
313
314         itc_1text2icon.item_style = "1text.2icon.2";
315         itc_1text2icon.func.text_get = __gl_text_get;
316         itc_1text2icon.func.content_get = __gl_content_get;
317         itc_1text2icon.func.state_get = __gl_state_get;
318         itc_1text2icon.func.del = NULL;
319
320         /* separator */
321         separator = elm_genlist_item_append(genlist, &itc_sep, NULL, NULL,
322                 ELM_GENLIST_ITEM_NONE, NULL, NULL);
323         elm_genlist_item_select_mode_set(separator,
324                 ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
325         elm_object_item_access_unregister(separator);
326
327         /* add radio list */
328         if (!vconf_get_bool(VCONFKEY_NFC_PREDEFINED_ITEM_STATE, &boolval)) {
329                 for (index = 0; index < pd_item_cnt; index++) {
330                         Elm_Object_Item *it;
331
332                         it = elm_genlist_item_append(genlist,
333                                 &itc_1text2icon, (void *)index,
334                                 NULL, ELM_GENLIST_ITEM_NONE,
335                                 __gl_sel, NULL);
336                         if (boolval)
337                                 elm_object_item_disabled_set(it, EINA_FALSE);
338                         else
339                                 elm_object_item_disabled_set(it, EINA_TRUE);
340
341                         /* SEPARATOR */
342                         separator = elm_genlist_item_append(genlist, &itc_sep,
343                                 NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
344                         elm_genlist_item_select_mode_set(separator,
345                                 ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
346                         elm_object_item_access_unregister(separator);
347                 }
348         }
349
350         evas_object_show(genlist);
351
352         vconf_notify_key_changed(VCONFKEY_NFC_STATE ,
353                         __nfc_vconf_changed_cb, ug_data);
354
355         return genlist;
356 }
357
358 static Eina_Bool __destroy_predefined_item_list(void *data,
359         Elm_Object_Item *it)
360 {
361         ugdata_t *ug_data = (ugdata_t *)data;
362
363         __destroy_pd_data();
364
365         pd_item_cnt = 0;
366
367         if (ug_data && ug_data->ns_genlist) {
368                 elm_genlist_realized_items_update(ug_data->ns_genlist);
369         }
370
371         vconf_ignore_key_changed(VCONFKEY_NFC_STATE,
372                 __nfc_vconf_changed_cb);
373
374         return EINA_TRUE;
375 }
376
377 bool _ug_nfc_check_predefined_item_available(void)
378 {
379         if (!_ug_nfc_setting_db_open_table(&dbtable_h)) {
380                 _ug_nfc_setting_db_close_table(dbtable_h);
381                 return true;
382         } else {
383                 return false;
384         }
385 }
386
387 void _ug_nfc_predefined_item_create(void *data)
388 {
389         ugdata_t *ug_data = (ugdata_t *)data;
390         Evas_Object *genlist;
391         int boolval;
392
393         if (!ug_data) {
394                 LOGE("invalid paramater");
395                 return;
396         }
397
398         /* create setting view */
399         ug_data->pd_genlist = genlist = __create_predefined_item_list(ug_data);
400         if (!genlist)
401                 return;
402
403         /* Push navifreme */
404         ug_data->base_navi_it = elm_naviframe_item_push(ug_data->base_naviframe,
405                 IDS_USE_NFC_IN_HOME, NULL, NULL, genlist, NULL);
406         elm_naviframe_item_pop_cb_set(ug_data->base_navi_it,
407                 __destroy_predefined_item_list, ug_data);
408
409         ug_data->pd_on_off = elm_check_add(ug_data->base_naviframe);
410         elm_object_style_set(ug_data->pd_on_off, "naviframe/title_on&off");
411         elm_object_item_part_content_set(ug_data->base_navi_it,
412                 "title_right_btn", ug_data->pd_on_off);
413         evas_object_smart_callback_add(ug_data->pd_on_off, "changed",
414                 __title_pd_on_off_clicked_cb, ug_data);
415
416         if (!vconf_get_bool(VCONFKEY_NFC_PREDEFINED_ITEM_STATE, &boolval) &&
417                 boolval) {
418                 elm_check_state_set(ug_data->pd_on_off, EINA_TRUE);
419         } else {
420                 elm_check_state_set(ug_data->pd_on_off, EINA_FALSE);
421         }
422 }
423
424