fix bug when on_resume cb is called
[framework/location/ug-setting-location-efl.git] / libug-setting-location-efl-appman.c
1 /*
2  * Copyright 2012  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://www.tizenopensource.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 #include <string.h>
18
19 #include <Elementary.h>
20 #include <location-appman.h>
21
22 #include "libug-setting-location-efl.h"
23 #include "libug-setting-location-efl-log.h"
24
25 static void _setting_location_app_cb(void *data, Evas_Object * obj, void *event_info)
26 {
27         location_appman_info *app_info = (location_appman_info *)data;
28         int onoff;
29
30         Eina_Bool state = elm_check_state_get(obj);
31
32         onoff = (state == EINA_TRUE) ? KEY_ENABLED : KEY_DISABLED;
33         location_appman_set_on(app_info->package, onoff);
34 }
35
36 static char *_setting_location_gl_app_text_get(void *data, Evas_Object * obj, const char *part)
37 {
38         location_appman_info *app_info = (location_appman_info *)data;
39         char *name;
40
41         if (location_appman_get_name(app_info->package, &name) != LOCATION_APPMAN_ERROR_NONE) {
42                 LOC_LOG("Fail to [%s] location_appman_get_name", app_info->package);
43                 return NULL;
44         }
45         LOC_LOG("package [%s], get_name [%s]", app_info->package, name);
46         return strdup(name);
47 }
48
49 static char *_setting_location_gl_app_msg_get(void *data, Evas_Object * obj, const char *part)
50 {
51         return strdup(_("IDS_LBS_BODY_APPLICATIONS_BELOW_MAY_USE_YOUR_LOCATION_DATA_IF_ALLOWED"));
52 }
53
54 static Evas_Object *_setting_location_gl_app_check_get(void *data, Evas_Object * obj, const char *part)
55 {
56         location_appman_info *app_info = (location_appman_info *)data;
57         int enabled;
58         Evas_Object *tg;
59         
60         app_info->tg = tg = elm_check_add(obj);
61
62         location_appman_is_enabled(app_info->package, &enabled);
63
64         if (enabled == KEY_ENABLED) {
65                 elm_check_state_set(tg, EINA_TRUE);
66         } else {
67                 elm_check_state_set(tg, EINA_FALSE);
68         }
69
70         elm_object_style_set(tg, "on&off");
71         evas_object_propagate_events_set(tg, EINA_FALSE);
72         evas_object_smart_callback_add(tg, "changed", _setting_location_app_cb, app_info);
73
74         return tg;
75 }
76
77 static void setting_location_gl_app_sel(void *data, Evas_Object *obj, void *event_info)
78 {
79         location_appman_info *app_info = (location_appman_info *)data;
80         int onoff;
81
82         Eina_Bool state = elm_check_state_get(app_info->tg);
83
84         elm_check_state_set(app_info->tg, !state);
85         onoff = (!state == EINA_TRUE) ? KEY_ENABLED : KEY_DISABLED;
86         location_appman_set_on(app_info->package, onoff);
87         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
88 }
89
90 void setting_location_delete_app_list(Evas_Object *genlist, struct ug_data *ugd)
91 {
92         int i;
93
94         if (ugd->itc_msg != NULL) {
95                 elm_object_item_del(ugd->gi_msg);
96                 elm_genlist_item_class_free(ugd->itc_msg);
97         }
98         if (ugd->itc_line != NULL) {
99                 elm_object_item_del(ugd->gi_line);
100                 elm_genlist_item_class_free(ugd->itc_line);
101         }
102         
103         for (i = 0; i < ugd->app_count; i++) {
104                 if (ugd->app_info[i].gi_app != NULL) {
105                         elm_object_item_del(ugd->app_info[i].gi_app);
106                 }
107                 if (ugd->app_info[i].itc_app != NULL) {
108                         elm_genlist_item_class_free(ugd->app_info[i].itc_app);
109                 }
110         }
111
112         if (ugd->app_info != NULL) {
113                 free(ugd->app_info);
114         }
115 }
116
117 int setting_location_get_location_app_list(Evas_Object *genlist, struct ug_data *ugd)
118 {
119         int i;
120         location_appman_s *app_list;
121         Elm_Object_Item *item;
122
123         if (location_appman_get_app_list(LOCATION_APPMAN_COLUMN_INSTALLED_DATE, &ugd->app_count, &app_list) != LOCATION_APPMAN_ERROR_NONE) {
124                 LOC_LOG("Fail to location_appman_get_app_list");
125                 return FALSE;
126         }
127
128         LOC_LOG("app count : %d", ugd->app_count);
129         if (ugd->app_count <= 0) {
130                 LOC_LOG("There is no location application in DB");
131                 return TRUE;
132         }
133
134         if (ugd->app_count == 1 && (strcmp(app_list[0].package, "org.tizen.setting") == 0)) {
135                 LOC_LOG("Setting application is only one location application");
136                 ugd->itc_line = NULL;
137                 ugd->itc_msg = NULL;
138                 ugd->gi_msg = NULL;
139                 ugd->gi_line = NULL;
140                 return TRUE;
141         }
142
143         ugd->app_info = (location_appman_info *)malloc(sizeof(location_appman_info) * ugd->app_count);
144
145         ugd->itc_msg = elm_genlist_item_class_new();
146         ugd->itc_msg->item_style = "multiline/1text";
147         ugd->itc_msg->func.text_get = _setting_location_gl_app_msg_get;
148         ugd->itc_msg->func.content_get = NULL;
149         ugd->itc_msg->func.state_get = NULL;
150         ugd->itc_msg->func.del = NULL;
151
152         ugd->itc_line= elm_genlist_item_class_new();
153         ugd->itc_line->item_style = "dialogue/separator/1/with_line";
154         ugd->itc_line->func.text_get = NULL;
155         ugd->itc_line->func.content_get = NULL;
156         ugd->itc_line->func.state_get = NULL;
157         ugd->itc_line->func.del = NULL;
158
159         ugd->gi_msg = elm_genlist_item_append(genlist, ugd->itc_msg, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
160         elm_genlist_item_select_mode_set(ugd->gi_msg, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
161
162         ugd->gi_line = elm_genlist_item_append(genlist, ugd->itc_line, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
163         elm_genlist_item_select_mode_set(ugd->gi_line, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
164
165         for (i = 0; i < ugd->app_count; i++) {
166                 strcpy(ugd->app_info[i].package, app_list[i].package);
167                 ugd->app_info[i].itc_app = elm_genlist_item_class_new();
168                 ugd->app_info[i].itc_app->item_style = "dialogue/1text.1icon";
169                 ugd->app_info[i].itc_app->func.text_get = _setting_location_gl_app_text_get;
170                 ugd->app_info[i].itc_app->func.content_get = _setting_location_gl_app_check_get;
171                 ugd->app_info[i].itc_app->func.state_get = NULL;
172                 ugd->app_info[i].itc_app->func.del = NULL;
173
174                 if (strcmp(app_list[i].package, "org.tizen.setting") == 0) {
175                         ugd->app_info[i].gi_app = NULL;
176                         continue;
177                 }
178
179                 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]);
180         }
181
182         free(app_list);
183
184         return TRUE;
185 }