support 3rd party lock
[apps/core/preloaded/settings.git] / setting-security / src / setting-security-locktype.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-common-data-type.h>
22 #include <setting-common-data-slp-setting.h>
23 #include <setting-common-draw-widget.h>
24 #include <setting-common-view.h>
25
26 #include <setting-security-locktype.h>
27 #include <setting-security.h>
28 #include <setting-debug.h>
29 #include <ail.h>
30 #include <pkgmgr-info.h>
31
32 #define TBD 0
33
34 static int setting_security_locktype_create(void *cb);
35 static int setting_security_locktype_destroy(void *cb);
36 static int setting_security_locktype_update(void *cb);
37 static int setting_security_locktype_cleanup(void *cb);
38 setting_view setting_view_security_locktype = {
39         .create = setting_security_locktype_create,
40         .destroy = setting_security_locktype_destroy,
41         .update = setting_security_locktype_update,
42         .cleanup = setting_security_locktype_cleanup,
43 };
44
45 openlock_appdata *lockapp_data = NULL;
46
47 /* ***************************************************
48  **
49  **basic func
50  **
51  ****************************************************/
52
53 static int __get_lockapp_index_from_pkgname(char *pkg_name)
54 {
55         SETTING_TRACE_BEGIN;
56         if(pkg_name == NULL)
57                 return SETTING_RETURN_FAIL;
58
59         openlock_appdata *item = lockapp_data;
60         for(; item != NULL; item = item->next)
61         {
62                 if(safeStrCmp(item->pkg_name, pkg_name) == 0)
63                         return item->index;
64         }
65         return SETTING_RETURN_FAIL;
66 }
67 static int __get_lockapp_index_from_appname(char *app_name)
68 {
69         SETTING_TRACE_BEGIN;
70         if(app_name == NULL)
71                 return SETTING_RETURN_FAIL;
72
73         openlock_appdata *item = lockapp_data;
74         for(; item != NULL; item = item->next)
75         {
76                 if(safeStrCmp(item->app_name, app_name) == 0)
77                         return item->index;
78         }
79         return SETTING_RETURN_FAIL;
80 }
81 static char* __get_lockapp_pkgname_from_appname(char *app_name)
82 {
83         SETTING_TRACE_BEGIN;
84         if(app_name == NULL)
85                 return NULL;
86
87         openlock_appdata *item = lockapp_data;
88         for(; item != NULL; item = item->next)
89         {
90                 if(safeStrCmp(item->app_name, app_name) == 0)
91                         return item->app_name;
92         }
93         return NULL;
94 }
95
96 ail_cb_ret_e __get_appinfo_cb(const ail_appinfo_h appinfo, void *user_data)
97 {
98         SETTING_TRACE_BEGIN;
99
100         openlock_appdata *item = lockapp_data;
101         openlock_appdata *new_item = NULL;
102         char *pkg_name = NULL;
103         char *app_name = NULL;
104
105         ail_appinfo_get_str(appinfo, AIL_PROP_PACKAGE_STR, &pkg_name);
106         ail_appinfo_get_str(appinfo, AIL_PROP_NAME_STR, &app_name);
107
108         while(item->next != NULL)
109                 item = item->next;
110
111         if(item->index == 0 && item->pkg_name == NULL)
112         {
113                 /* first app */
114                 item->pkg_name = strdup(pkg_name);
115                 item->app_name = strdup(app_name);
116
117                 SETTING_TRACE_DEBUG("app info %d %s %s", item->index, item->pkg_name, item->app_name);
118         }
119         else
120         {
121                 /* create new */
122                 new_item = (openlock_appdata*)malloc(sizeof(openlock_appdata));
123                 if(new_item != NULL)
124                 {
125                         memset(new_item, 0x00, sizeof(openlock_appdata));
126                         new_item->pkg_name = strdup(pkg_name);
127                         new_item->app_name = strdup(app_name);
128                         new_item->index = item->index + 1;
129                         new_item->prev = item;
130                         item->next = new_item;
131
132                         SETTING_TRACE_DEBUG("app info %d %s %s", new_item->index, new_item->pkg_name, new_item->app_name);
133                 }
134                 else
135                 {
136                         SETTING_TRACE_DEBUG("malloc() failed");
137                 }
138         }
139         return AIL_CB_RET_CONTINUE;
140 }
141
142 static void __add_3rd_party_lock(void *data)
143 {
144         SETTING_TRACE_BEGIN;
145         ret_if(data == NULL);
146
147         SettingSecurityUG *ad = (SettingSecurityUG *) data;
148
149         /* Get info from AIL */
150         pkgmgrinfo_appinfo_filter_h filter;             //ail_filter_h filter;
151         int ret = 0;                                                    //ail_error_e ret;
152         int count = -1;                                                 //int count = 0;
153
154         ret = pkgmgrinfo_appinfo_filter_create(&filter);        //ret = ail_filter_new(&filter);
155         if(ret > 0)             //if(ret != AIL_ERROR_OK)
156         {
157                 SETTING_TRACE_DEBUG("pkgmgrinfo_appinfo_filter_create() failed"); //SETTING_TRACE_DEBUG("ail_filter_new() failed");
158                 return;
159         }
160         ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_CATEGORY, "http://tizen.org/category/lockapp");
161         //ret = ail_filter_add_str(filter, AIL_PROP_CATEGORIES_STR, "lockscreen");
162         ret = pkgmgrinfo_appinfo_filter_count(filter, &count);  ////ret = ail_filter_count_appinfo(filter, &count);
163         SETTING_TRACE_DEBUG("There is/are %d 3rd lock app(s)", count);
164         if(count <= 0)
165         {
166                 SETTING_TRACE_DEBUG("No 3rd lock app");
167                 pkgmgrinfo_appinfo_filter_destroy(filter);      //ail_filter_destroy(filter);
168                 return;
169
170         }
171
172         lockapp_data = (openlock_appdata*)malloc(sizeof(openlock_appdata));
173         if(lockapp_data == NULL)
174         {
175                 SETTING_TRACE_DEBUG("malloc() failed");
176                 return;
177         }
178         memset(lockapp_data, 0x00, sizeof(openlock_appdata));
179         lockapp_data->prev = NULL;
180         lockapp_data->next = NULL;
181         lockapp_data->pkg_name = NULL;
182         lockapp_data->app_name = NULL;
183         lockapp_data->index = 0;
184
185         pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, __get_appinfo_cb, ad);        //ail_filter_list_appinfo_foreach(filter, __get_appinfo_cb, ad);
186         pkgmgrinfo_appinfo_filter_destroy(filter);      //ail_filter_destroy(filter);
187
188         /* Add to genlist */
189         openlock_appdata *list = lockapp_data;
190         Setting_GenGroupItem_Data *item_data = NULL;
191         int index = 0;
192         for(index = 0; index < count; index++)
193         {
194                 ad->data_locktype_3rd[index] = setting_create_Gendial_field_1radio(ad->sl_scroller,
195                                                 &(itc_1text_1icon_2),
196                                                 setting_security_locktype_mouse_up_Gendial_list_cb,     /*add to sel_cb */
197                                                 ad,     /* sel data */
198                                                 SWALLOW_Type_1RADIO,
199                                                 ad->lock_type_rd, SETTING_SCREEN_LOCK_TYPE_OTHER + index,
200                                                 list->app_name,
201                                                 NULL);
202                 if (ad->data_locktype_3rd[index]) {
203                         ad->data_locktype_3rd[index]->userdata = ad;
204                 } else {
205                         SETTING_TRACE_ERROR("item_data is NULL");
206                 }
207                 list = list->next;
208         }
209         SETTING_TRACE_END;
210 }
211
212 void __add_locktype_items(void *data)
213 {
214         SETTING_TRACE_BEGIN;
215         retv_if(data == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
216
217         SettingSecurityUG *ad = (SettingSecurityUG *) data;
218
219         Elm_Object_Item *item = NULL;
220         Evas_Object *radio;
221         Setting_GenGroupItem_Data *item_data = NULL;
222
223         int index = -1;
224         int locktype = 0;
225         vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &locktype);
226
227         radio = elm_radio_add(ad->sl_scroller);
228         elm_radio_state_value_set(radio, -1);
229         ad->lock_type_rd = radio;
230
231         /* separator */
232         item = elm_genlist_item_append(ad->sl_scroller, &(ad->itc_seperator), NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
233         elm_genlist_item_select_mode_set(item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
234
235         //evas_object_smart_callback_add(ad->sl_scroller, "realized", __gl_realized_cb, NULL);
236
237         /* to do : radio menu */
238         /* 1) swipe */
239         ad->data_locktype_swipe =
240             setting_create_Gendial_field_1radio(ad->sl_scroller,
241                                                 &(itc_1text_1icon_2),
242                                                 setting_security_locktype_mouse_up_Gendial_list_cb,     /*add to sel_cb */
243                                                 ad,     /* sel data */
244                                                 SWALLOW_Type_1RADIO,
245                                                 radio, SETTING_SCREEN_LOCK_TYPE_SWIPE,
246                                                 Keystr_Swipe,
247                                                 NULL);
248         if (ad->data_locktype_swipe) {
249                 ad->data_locktype_swipe->userdata = ad;
250                 ad->data_locktype_swipe->group_style = SETTING_GROUP_STYLE_TOP;
251         } else {
252                 SETTING_TRACE_ERROR("item_data is NULL");
253         }
254
255         /* Add 3rd party lock apps in list. */
256         __add_3rd_party_lock(ad);
257
258         /* update info */
259         /* To do : If type is OTHER, should calculate something.*/
260         if(locktype == SETTING_SCREEN_LOCK_TYPE_OTHER)
261         {
262                 char *open_lock_name = NULL;
263                 open_lock_name = vconf_get_str(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR);
264                 index = __get_index_of_lockapp(open_lock_name);
265
266                 if(open_lock_name)
267                         FREE(open_lock_name);
268         }
269         /* End */
270         if(index < 0)
271                 elm_radio_value_set(radio, locktype);
272         else
273                 elm_radio_value_set(radio, locktype + index);
274 }
275
276 static int setting_security_locktype_create(void *cb)
277 {
278         SETTING_TRACE_BEGIN;
279         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
280
281         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
282
283         /* add basic layout */
284
285         Evas_Object *scroller = NULL;
286
287         setting_push_layout_navi_bar_genlist(ad->win_main_layout,
288                                                            ad->win_get,
289                                                            _(Keystr_ScreenLockType),
290                                                            _("IDS_COM_BODY_BACK"), NULL,
291                                                            setting_security_locktype_click_softkey_back_cb,
292                                                            NULL, ad, &scroller, ad->navi_bar);
293
294         ad->sl_scroller = scroller;
295
296         __add_locktype_items(ad);
297
298         setting_view_security_locktype.is_create = 1;
299
300         return SETTING_RETURN_SUCCESS;
301 }
302
303 static int setting_security_locktype_destroy(void *cb)
304 {
305         SETTING_TRACE_BEGIN;
306         /* error check */
307         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
308
309         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
310         if (ad->notify)
311         {
312                 evas_object_del(ad->notify);
313                 ad->notify = NULL;
314         }
315
316         elm_naviframe_item_pop(ad->navi_bar);
317
318         setting_view_security_locktype.is_create = 0;
319
320         return SETTING_RETURN_SUCCESS;
321 }
322
323 static int setting_security_locktype_update(void *cb)
324 {
325         SETTING_TRACE_BEGIN;
326         /* error check */
327         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
328
329         return SETTING_RETURN_SUCCESS;
330 }
331
332 static int setting_security_locktype_cleanup(void *cb)
333 {
334         SETTING_TRACE_BEGIN;
335         /* error check */
336         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
337
338         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
339
340         return setting_security_locktype_destroy(ad);
341 }
342
343 /* ***************************************************
344  **
345  **call back func
346  **
347  ****************************************************/
348
349 static void
350 setting_security_locktype_mouse_up_Gendial_list_cb(void *data, Evas_Object *obj,
351                                                void *event_info)
352 {
353         SETTING_TRACE_BEGIN;
354         /* error check */
355         setting_retm_if(data == NULL, "Data parameter is NULL");
356
357         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
358         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
359         elm_genlist_item_selected_set(item, 0);
360         Setting_GenGroupItem_Data *list_item =
361             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
362         SettingSecurityUG *ad = (SettingSecurityUG *) data;
363         int lock_type = list_item->chk_status;
364         SETTING_TRACE("clicking item[%s]", _(list_item->keyStr));
365         int old_type = elm_radio_value_get(list_item->eo_check);
366
367         ad->selected_lock_type = list_item->keyStr;
368
369         /* If lock_type is same with old_lock_type, return. */
370         if(lock_type == old_type)
371         {
372                 SETTING_TRACE_DEBUG("[Screen Lock Type] Selected same type");
373                 return;
374         }
375
376         int index = -1;
377         char *pkg_name = NULL;
378
379         switch(lock_type)
380         {
381                 case SETTING_SCREEN_LOCK_TYPE_SWIPE:
382                         if(vconf_set_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, lock_type) == 0)
383                         {
384                                 /* set radio */
385                                 elm_radio_value_set(list_item->eo_check, lock_type);
386                         }
387                         ug_destroy_me(ad->ug);
388                         break;
389                 case SETTING_SCREEN_LOCK_TYPE_OTHER:
390                         index = __get_lockapp_index_from_appname(list_item->keyStr);
391                         pkg_name = __get_lockapp_pkgname_from_appname(list_item->keyStr);
392                         SETTING_TRACE_DEBUG("3rd lock selected. index[%d] pkg_name[%s]", index, pkg_name);
393                         vconf_set_str(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR, pkg_name);
394                         vconf_set_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, lock_type);
395                         /* set radio */
396                         elm_radio_value_set(list_item->eo_check, lock_type + index);
397                         break;
398                 default:
399                         break;
400         }
401 }
402
403 static void
404 setting_security_locktype_click_softkey_back_cb(void *data, Evas_Object *obj,
405                                             void *event_info)
406 {
407         SETTING_TRACE_BEGIN;
408         /* error check */
409         retm_if(data == NULL, "[Setting > Security] Data parameter is NULL");
410
411         SettingSecurityUG *ad = (SettingSecurityUG *) data;
412         setting_view_change(&setting_view_security_locktype, &setting_view_security_main, ad);
413         SETTING_TRACE_END;
414 }