Tizen 2.0 Release
[platform/core/location/ug-setting-location-efl.git] / libug-setting-location-efl-appman.c
1 /*
2  * Open Service Platform
3  * Copyright (c) 2012-2013  Samsung Electronics Co., Ltd
4  *
5  * Licensed under the Flora License, Version 1.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://floralicense.org/license/
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <string.h>
19
20 #include <Elementary.h>
21 #include <location-appman.h>
22
23 #include "libug-setting-location-efl.h"
24 #include "libug-setting-location-efl-log.h"
25
26 static void _setting_location_app_cb(void *data, Evas_Object * obj, void *event_info)
27 {
28         location_appman_info *app_info = (location_appman_info *)data;
29         int onoff;
30
31         Eina_Bool state = elm_check_state_get(obj);
32
33         onoff = (state == EINA_TRUE) ? KEY_ENABLED : KEY_DISABLED;
34         location_appman_set_on(app_info->package, onoff);
35 }
36
37 static char *_setting_location_gl_app_text_get(void *data, Evas_Object * obj, const char *part)
38 {
39         location_appman_info *app_info = (location_appman_info *)data;
40         char *name;
41
42         if (location_appman_get_name(app_info->package, &name) != LOCATION_APPMAN_ERROR_NONE) {
43                 LOC_LOG("Fail to [%s] location_appman_get_name", app_info->package);
44                 return NULL;
45         }
46         LOC_LOG("package [%s], get_name [%s]", app_info->package, name);
47         return strdup(name);
48 }
49
50 static char *_setting_location_gl_app_msg_get(void *data, Evas_Object * obj, const char *part)
51 {
52         return strdup(_("IDS_LBS_BODY_APPLICATIONS_BELOW_MAY_USE_YOUR_LOCATION_DATA_IF_ALLOWED"));
53 }
54
55 static Evas_Object *_setting_location_gl_app_check_get(void *data, Evas_Object * obj, const char *part)
56 {
57         location_appman_info *app_info = (location_appman_info *)data;
58         int enabled;
59         Evas_Object *tg;
60
61         app_info->tg = tg = elm_check_add(obj);
62
63         location_appman_is_enabled(app_info->package, &enabled);
64
65         if (enabled == KEY_ENABLED) {
66                 elm_check_state_set(tg, EINA_TRUE);
67         } else {
68                 elm_check_state_set(tg, EINA_FALSE);
69         }
70
71         elm_object_style_set(tg, "on&off");
72         evas_object_propagate_events_set(tg, EINA_FALSE);
73         evas_object_smart_callback_add(tg, "changed", _setting_location_app_cb, app_info);
74
75         return tg;
76 }
77
78 static void setting_location_gl_app_sel(void *data, Evas_Object *obj, void *event_info)
79 {
80         location_appman_info *app_info = (location_appman_info *)data;
81         int onoff;
82
83         Eina_Bool state = elm_check_state_get(app_info->tg);
84
85         onoff = (!state == EINA_TRUE) ? KEY_ENABLED : KEY_DISABLED;
86         location_appman_set_on(app_info->package, onoff);
87         elm_genlist_item_update(app_info->gi_app);
88         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
89 }
90
91 void setting_location_delete_app_list(Evas_Object *genlist, struct ug_data *ugd)
92 {
93         int i;
94
95         if (ugd->itc_bot_sep != NULL) {
96                 elm_object_item_del(ugd->gi_bot_sep);
97                 elm_genlist_item_class_free(ugd->itc_bot_sep);
98         }
99
100         if (ugd->itc_msg != NULL) {
101                 elm_object_item_del(ugd->gi_msg);
102                 elm_genlist_item_class_free(ugd->itc_msg);
103         }
104
105         if (ugd->app_info == NULL) {
106                 return;
107         }
108
109         for (i = 0; i < ugd->app_count; i++) {
110                 if (ugd->app_info[i].gi_app != NULL) {
111                         elm_object_item_del(ugd->app_info[i].gi_app);
112                 }
113                 if (ugd->app_info[i].itc_app != NULL) {
114                         elm_genlist_item_class_free(ugd->app_info[i].itc_app);
115                 }
116         }
117
118         free(ugd->app_info);
119 }
120
121 void setting_location_appman_init(struct ug_data *ugd)
122 {
123         ugd->app_info = NULL;
124         ugd->itc_msg = NULL;
125         ugd->gi_msg = NULL;
126 }
127
128 int setting_location_get_location_app_list(Evas_Object *genlist, struct ug_data *ugd)
129 {
130         int i;
131         location_appman_s *app_list;
132
133         if (location_appman_get_app_list(LOCATION_APPMAN_COLUMN_INSTALLED_DATE, &ugd->app_count, &app_list) != LOCATION_APPMAN_ERROR_NONE) {
134                 LOC_LOG("Fail to location_appman_get_app_list");
135                 return FALSE;
136         }
137         setting_location_appman_init(ugd);
138         LOC_LOG("app count : %d", ugd->app_count);
139         if (ugd->app_count <= 0) {
140                 LOC_LOG("There is no location application in DB");
141                 return TRUE;
142         }
143
144         if (ugd->app_count == 1 && (strcmp(app_list[0].package, "org.tizen.setting") == 0)) {
145                 LOC_LOG("Setting application is only one location application");
146                 return TRUE;
147         }
148
149         ugd->app_info = (location_appman_info *)malloc(sizeof(location_appman_info) * ugd->app_count);
150         if (ugd->app_info == NULL) {
151                 LOC_LOG("Fail to alloc to app_info");
152                 free(app_list);
153                 return FALSE;
154         }
155
156         ugd->itc_msg = elm_genlist_item_class_new();
157         ugd->itc_msg->item_style = "multiline/1text";
158         ugd->itc_msg->func.text_get = _setting_location_gl_app_msg_get;
159         ugd->itc_msg->func.content_get = NULL;
160         ugd->itc_msg->func.state_get = NULL;
161         ugd->itc_msg->func.del = NULL;
162
163         ugd->gi_msg = elm_genlist_item_append(genlist, ugd->itc_msg, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
164         elm_genlist_item_select_mode_set(ugd->gi_msg, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
165
166         for (i = 0; i < ugd->app_count; i++) {
167                 strcpy(ugd->app_info[i].package, app_list[i].package);
168                 ugd->app_info[i].itc_app = elm_genlist_item_class_new();
169                 ugd->app_info[i].itc_app->item_style = "dialogue/1text.1icon";
170                 ugd->app_info[i].itc_app->func.text_get = _setting_location_gl_app_text_get;
171                 ugd->app_info[i].itc_app->func.content_get = _setting_location_gl_app_check_get;
172                 ugd->app_info[i].itc_app->func.state_get = NULL;
173                 ugd->app_info[i].itc_app->func.del = NULL;
174
175                 if (strcmp(app_list[i].package, "org.tizen.setting") == 0) {
176                         ugd->app_info[i].gi_app = NULL;
177                 } else {
178                         ugd->app_info[i].gi_app = elm_genlist_item_append(genlist, ugd->app_info[i].itc_app, (void *)&ugd->app_info[i], NULL, ELM_GENLIST_ITEM_NONE, setting_location_gl_app_sel, (void *)&ugd->app_info[i]);
179                 }
180         }
181
182         ugd->itc_bot_sep = elm_genlist_item_class_new();
183         ugd->itc_bot_sep->item_style = "dialogue/separator";
184         ugd->itc_bot_sep->func.text_get = NULL;
185         ugd->itc_bot_sep->func.content_get = NULL;
186         ugd->itc_bot_sep->func.state_get = NULL;
187         ugd->itc_bot_sep->func.del = NULL;
188         ugd->gi_bot_sep = elm_genlist_item_append(genlist, ugd->itc_bot_sep, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
189         elm_genlist_item_select_mode_set(ugd->gi_bot_sep, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
190
191         free(app_list);
192
193         return TRUE;
194 }