tizen 2.3.1 release
[apps/home/settings.git] / setting-network / src / setting-network-profile-delete.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-network-profile-delete.h>
22
23 static int setting_network_profile_delete_create(void *cb);
24 static int setting_network_profile_delete_destroy(void *cb);
25 static int setting_network_profile_delete_update(void *cb);
26 static int setting_network_profile_delete_cleanup(void *cb);
27 setting_view setting_view_network_profile_delete = {
28         .create = setting_network_profile_delete_create,
29         .destroy = setting_network_profile_delete_destroy,
30         .update = setting_network_profile_delete_update,
31         .cleanup = setting_network_profile_delete_cleanup,
32 };
33
34 /* ***************************************************
35  *
36  *basic func
37  *
38  ***************************************************/
39 static void __profile_delete_list_draw(SettingNetworkUG *ad)
40 {
41         SETTING_TRACE_BEGIN;
42         if (ad->profile_del_list != NULL) {
43                 eina_list_free(ad->profile_del_list);
44                 ad->profile_del_list = NULL;
45         }
46         elm_genlist_clear(ad->gl_profile_del);  /* first to clear list */
47         Elm_Object_Item *first_item;
48         /* item = elm_genlist_item_append(scroller, &itc_seperator, NULL, NULL, ELM_GENLIST_ITEM_GROUP, NULL, NULL); */
49         first_item = elm_genlist_item_append(ad->gl_profile_del, &itc_seperator, NULL, NULL,
50                                              ELM_GENLIST_ITEM_NONE, NULL, NULL);
51         elm_genlist_item_select_mode_set(first_item, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
52
53         Eina_List *elist = NULL;
54         connection_profile_h profile_h = NULL;
55         int idx = 0;
56         char *id = NULL;
57         char *apn = NULL;
58         char *name = NULL;
59         char *proxy_address = NULL;
60         connection_profile_type_e profile_type = CONNECTION_PROFILE_TYPE_CELLULAR;
61         Setting_GenGroupItem_Data *item_data = NULL;
62
63         setting_network_reget_profile_list(ad);
64         SETTING_TRACE("ad->profile_list:%p", ad->profile_list);
65         EINA_LIST_FOREACH(ad->profile_list, elist, profile_h) {
66                 SETTING_TRACE("profile_h:%p", profile_h);
67                 if (!profile_h)
68                         continue;
69                 apn = name = proxy_address = id = NULL;
70                 connection_profile_get_cellular_apn(profile_h, &apn);
71                 if (isEmptyStr(apn))
72                         continue;
73                 connection_profile_get_name(profile_h, &name);
74                 if (isEmptyStr(name))
75                         continue;
76                 connection_profile_get_proxy_address(profile_h, CONNECTION_ADDRESS_FAMILY_IPV4, &proxy_address);
77                 connection_profile_get_id(profile_h, &id);
78
79                 if (connection_profile_get_type(profile_h, &profile_type) != CONNECTION_ERROR_NONE) {
80                         SETTING_TRACE_ERROR("Fail to get profile type");
81                         continue;
82                 }
83
84                 SETTING_TRACE("Record[%s], profile_type:%d, service_type:%d, id:%s",
85                               apn, profile_type, ad->profile_service_type, id);
86
87                 item_data = (Setting_GenGroupItem_Data *) calloc(1, sizeof(Setting_GenGroupItem_Data));
88                 if (0 == item_data) {
89                         SETTING_TRACE_ERROR("calloc failed");
90                         G_FREE(apn);
91                         return;
92                 }
93                 item_data->keyStr = (char *)g_strdup(name);
94                 item_data->swallow_type = SWALLOW_Type_1CHECK;
95                 item_data->r_swallow_path = (char *)g_strdup("reveal/extended");
96                 /*item_data->chk_status = 0; */
97                 item_data->chk_change_cb = setting_network_profile_delete_check_cb;
98                 item_data->userdata = ad;
99                 item_data->sub_desc = (char *)g_strdup(apn);
100                 item_data->belongs_to = (int) profile_h;
101                 if (idx == 0) {
102                         item_data->group_style = SETTING_GROUP_STYLE_TOP;
103                 } else
104                         item_data->group_style = SETTING_GROUP_STYLE_CENTER;
105
106                 item_data->item =
107                     elm_genlist_item_append(ad->gl_profile_del, &(itc_2text_1icon_2), item_data, NULL,
108                                             ELM_GENLIST_ITEM_NONE,
109                                             setting_network_profile_delete_mouse_up_cb, ad);
110                 if (idx == 0)
111                         setting_genlist_item_groupstyle_set(item_data, SETTING_GROUP_STYLE_TOP);
112                 else
113                         setting_genlist_item_groupstyle_set(item_data, SETTING_GROUP_STYLE_CENTER);
114                 ad->profile_del_list =
115                     eina_list_append(ad->profile_del_list, item_data);
116
117                 idx++;
118         }
119         setting_genlist_item_groupstyle_set(item_data, SETTING_GROUP_STYLE_BOTTOM);
120
121         Evas_Object *toolbar = elm_object_item_part_content_get(ad->navi_it_profile_del_list, "toolbar");
122         Evas_Object *allbtn = elm_object_item_part_content_get(ad->navi_it_profile_del_list, "title_left_btn");
123         SETTING_TRACE("idx:%d", idx);
124         if (idx == 1) {
125                 setting_genlist_item_groupstyle_set(item_data, SETTING_GROUP_STYLE_NONE);
126                 /*if (button) elm_object_disabled_set(button, EINA_FALSE); */
127                 if (allbtn) elm_object_disabled_set(allbtn, EINA_FALSE);
128         } else if (idx > 1) {
129                 setting_genlist_item_groupstyle_set(item_data, SETTING_GROUP_STYLE_BOTTOM);
130                 /*if (button) elm_object_disabled_set(button, EINA_FALSE); */
131                 if (allbtn) elm_object_disabled_set(allbtn, EINA_FALSE);
132         } else {
133                 /*all are invalid,to delete directly */
134                 if (ad->profile_list != NULL) {
135                         eina_list_free(ad->profile_list);
136                         ad->profile_list = NULL;
137                 }
138                 SETTING_TRACE("To disable");
139                 elm_object_item_disabled_set(elm_toolbar_last_item_get(toolbar), EINA_TRUE);
140                 /*if (button) elm_object_disabled_set(button, EINA_TRUE); */
141                 if (allbtn) elm_object_disabled_set(allbtn, EINA_TRUE);
142                 SETTING_TRACE("To disable");
143         }
144         G_FREE(apn);
145         G_FREE(name);
146         G_FREE(proxy_address);
147         G_FREE(id);
148         return;
149 }
150
151 static int setting_network_profile_delete_create(void *cb)
152 {
153         SETTING_TRACE_BEGIN;
154         SettingNetworkUG *ad = (SettingNetworkUG *) cb;
155
156         Evas_Object *scroller = elm_genlist_add(ad->win_main_layout);
157         retvm_if(scroller == NULL, SETTING_DRAW_ERR_FAIL_SCROLLER,
158                  "Cannot set scroller object  as contento of layout");
159         elm_genlist_realization_mode_set(scroller, EINA_TRUE);
160         elm_object_style_set(scroller, "dialogue");
161         elm_genlist_clear(scroller);    /* first to clear list */
162         evas_object_smart_callback_add(scroller, "realized", __gl_realized_cb, NULL);
163         ad->gl_profile_del = scroller;
164
165         ad->navi_it_profile_del_list = setting_push_layout_navi_bar(_("IDS_COM_SK_DELETE"),
166                                                                     dgettext("sys_string", "IDS_COM_BODY_BACK"),
167                                                                     dgettext("sys_string", "IDS_COM_SK_DELETE"),
168                                                                     _("IDS_COM_SK_CANCEL"),
169                                                                     setting_network_profile_delete_click_softkey_cancel_cb,
170                                                                     setting_network_profile_delete_click_softkey_delete_cb,
171                                                                     setting_network_profile_delete_click_softkey_cancel_cb,
172                                                                     ad, scroller, ad->navi_bar, NULL);
173         /*disable Delete button */
174         Evas_Object *toolbar = elm_object_item_part_content_get(ad->navi_it_profile_del_list, "toolbar");
175         Elm_Object_Item *it = elm_toolbar_last_item_get(toolbar);
176         elm_object_item_disabled_set(it, EINA_TRUE);
177
178         Evas_Object *allbtn = elm_button_add(ad->navi_bar);
179
180         elm_object_style_set(allbtn, "naviframe/title_icon");
181         Evas_Object *icon = elm_icon_add(allbtn);
182         elm_object_text_set(allbtn, _("IDS_ST_OPT_ALL"));
183         /*elm_image_file_set(icon, EDJ_NAME, "icon.select_all"); */
184         elm_image_file_set(icon, SETTING_ICON_PATH_CFG"select_all.png", NULL);
185
186         /*evas_object_size_hint_aspect_set(icon, EVAS_ASPECT_CONTROL_VERTICAL, 1 , 1); */
187         /*elm_image_resizable_set(icon, EINA_TRUE, EINA_TRUE); */
188         elm_object_content_set(allbtn, icon);
189         elm_object_focus_allow_set(allbtn, EINA_FALSE);
190         evas_object_propagate_events_set(allbtn, EINA_FALSE);
191         evas_object_smart_callback_add(allbtn, "clicked", setting_network_profile_delete_click_softkey_select_all_cb, ad);
192         evas_object_show(allbtn);
193         elm_object_item_part_content_set(ad->navi_it_profile_del_list, "title_left_btn", allbtn);
194         __profile_delete_list_draw(ad);
195         ADD_GL_SEPARATOR(scroller);
196         setting_view_network_profile_delete.is_create = 1;
197
198         return SETTING_RETURN_SUCCESS;
199 }
200
201 static int setting_network_profile_delete_destroy(void *cb)
202 {
203         SETTING_TRACE_BEGIN;
204         SettingNetworkUG *ad = (SettingNetworkUG *) cb;
205         if (!setting_view_network_profile_delete.is_create)
206                 return SETTING_RETURN_SUCCESS;
207
208         /* free data */
209         if (ad->profile_del_list != NULL) {
210                 eina_list_free(ad->profile_del_list);
211                 ad->profile_del_list = NULL;
212         }
213         ad->gl_profile_del = NULL;
214         ad->navi_it_profile_del_list = NULL;
215         setting_view_network_profile_delete.is_create = 0;
216         elm_naviframe_item_pop(ad->navi_bar);
217
218         return SETTING_RETURN_SUCCESS;
219 }
220
221 static int setting_network_profile_delete_update(void *cb)
222 {
223         SETTING_TRACE_BEGIN;
224         /* error check */
225         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
226         __profile_delete_list_draw(cb);
227         return SETTING_RETURN_SUCCESS;
228 }
229
230 static int setting_network_profile_delete_cleanup(void *cb)
231 {
232         return setting_network_profile_delete_destroy(cb);
233 }
234
235 /* ***************************************************
236  *
237  *general func
238  *
239  ***************************************************/
240 void ___profile_selected(void *data)
241 {
242         SettingNetworkUG *ad = data;
243         retm_if(ad == NULL, "Invalid argument: ad is NULL");
244         Eina_List *check_list = ad->profile_del_list;
245         Setting_GenGroupItem_Data *item_data = NULL;
246         int selnum = 0;
247         while (check_list) {
248                 item_data = (Setting_GenGroupItem_Data *)eina_list_data_get(check_list);
249                 if (NULL == item_data)
250                         continue;
251
252                 SETTING_TRACE("%s :%d", _(item_data->keyStr), item_data->chk_status);
253                 if (item_data->chk_status) {
254                         selnum++;
255                 }
256                 check_list = eina_list_next(check_list);
257         }
258         Elm_Object_Item *navi_it = elm_naviframe_top_item_get(ad->navi_bar);
259         if (navi_it) {
260                 Evas_Object *toolbar = elm_object_item_part_content_get(navi_it, "toolbar");
261                 Elm_Object_Item *it = elm_toolbar_last_item_get(toolbar);
262                 if (selnum != 0) {
263                         char title[MAX_DISPLAY_NAME_LEN_ON_UI + 1] = {0, };
264                         snprintf(title, MAX_DISPLAY_NAME_LEN_ON_UI, _("IDS_ST_HEADER_PD_SELECTED"), selnum);
265                         elm_object_item_text_set(navi_it, _(title));
266                         elm_object_item_disabled_set(it, EINA_FALSE);
267                 } else {
268                         elm_object_item_text_set(navi_it, _("IDS_COM_SK_DELETE"));
269                         elm_object_item_disabled_set(it, EINA_TRUE);
270                 }
271         }
272 }
273
274 static void
275 setting_network_profile_delete_check_cb(void *data, Evas_Object *obj,
276                                         void *event_info)
277 {
278         retm_if(data == NULL, "Data parameter is NULL");
279         Setting_GenGroupItem_Data *list_item =
280             (Setting_GenGroupItem_Data *) data;
281         list_item->chk_status = elm_check_state_get(obj);       /*  for genlist update status */
282         ___profile_selected(list_item->userdata);
283 }
284
285 static void
286 setting_network_profile_delete_mouse_up_cb(void *data,
287                                            Evas_Object *obj,
288                                            void *event_info)
289 {
290         SETTING_TRACE_BEGIN;
291         /* error check */
292         retm_if(event_info == NULL, "Invalid argument: event info is NULL");
293
294         Elm_Object_Item *item = (Elm_Object_Item *) event_info;
295         elm_genlist_item_selected_set(item, 0);
296         Setting_GenGroupItem_Data *list_item =
297             (Setting_GenGroupItem_Data *) elm_object_item_data_get(item);
298         SETTING_TRACE("list_item->keyStr:%s", list_item->keyStr);
299
300         setting_update_gl_item_chk_status(list_item, !(list_item->chk_status));
301         ___profile_selected(list_item->userdata);
302 }
303
304 /* ***************************************************
305  *
306  *call back func
307  *
308  ***************************************************/
309 static void
310 setting_network_profile_delete_click_softkey_cancel_cb(void *data,
311                                                        Evas_Object *
312                                                        obj, void
313                                                        *event_info)
314 {
315         SETTING_TRACE_BEGIN;
316         /* error check */
317         retm_if(data == NULL, "Data parameter is NULL");
318         SettingNetworkUG *ad = (SettingNetworkUG *) data;
319         setting_view_change(&setting_view_network_profile_delete,
320                             &setting_view_network_con_list, ad);
321 }
322
323 static void __popup_deleted_response_cb(void *data, Evas_Object *obj,
324                                         void *event_info)
325 {
326         retm_if(data == NULL, "Data parameter is NULL");
327
328         SettingNetworkUG *ad = (SettingNetworkUG *) data;
329
330         setting_view_change(&setting_view_network_profile_delete, &setting_view_network_con_list, ad);
331 }
332
333 static void
334 setting_network_profile_delete_click_softkey_delete_cb(void *data,
335                                                        Evas_Object *
336                                                        obj, void
337                                                        *event_info)
338 {
339         SETTING_TRACE_BEGIN;
340         /* error check */
341         retm_if(data == NULL, "Data parameter is NULL");
342
343         SettingNetworkUG *ad = (SettingNetworkUG *) data;
344         Eina_List *check_list = ad->profile_del_list;
345         Setting_GenGroupItem_Data *item_data = NULL;
346         int err;
347         connection_profile_h profile_h;
348         char *name = NULL;
349         char *apn = NULL;
350         char *id = NULL;
351         char speciliztion[MAX_SPECIALIZITION_LEN] = { 0, };
352         char *def_apn = NULL;
353         char *def_name = NULL;
354         char *def_id = NULL;
355         connection_profile_h def_profile = NULL;
356         (void) connection_get_default_cellular_service_profile(ad->connection, ad->profile_service_type, &def_profile);
357         if (def_profile) connection_profile_get_cellular_apn(def_profile, &def_apn);
358         if (def_profile) connection_profile_get_name(def_profile, &def_name);
359         if (def_profile) connection_profile_get_id(def_profile, &def_id);
360         SETTING_TRACE("Default profile:%p, def_apn[%s], def_name[%s]", def_profile, def_apn, def_name);
361
362         bool result = TRUE;
363         Setting_GenGroupItem_Data *first_valid_item = NULL;
364         int ok_cnt = 0;
365         bool is_default = 0;
366         while (check_list) {
367                 item_data = (Setting_GenGroupItem_Data *) eina_list_data_get(check_list);
368                 if (NULL == item_data) {
369                         check_list = eina_list_next(check_list);
370                         continue;
371                 }
372                 SETTING_TRACE("item_data->chk_status:%d", item_data->chk_status);
373                 if (!item_data->chk_status) {
374                         if (!first_valid_item) first_valid_item = item_data;/*intilize at the first time */
375                         check_list = eina_list_next(check_list);
376                         continue;
377                 }
378                 profile_h = (connection_profile_h)(item_data->belongs_to);
379                 connection_profile_get_name(profile_h, &name);
380                 connection_profile_get_cellular_apn(profile_h, &apn);
381                 connection_profile_get_id(profile_h, &id);
382                 if (!safeStrCmp(def_apn, apn) && !safeStrCmp(def_name, name) && !safeStrCmp(def_id, id)) {
383                         is_default = TRUE;
384                 }
385                 if ((err = connection_remove_profile(ad->connection, profile_h)) != CONNECTION_ERROR_NONE) {
386                         SETTING_TRACE_ERROR("*** [ERR] connection_add_profile. err=%d ***", err);
387                         /*setting_create_simple_popup(ad, ad->win_get, NULL, _("IDS_COM_POP_FAILED")); */
388                         G_FREE(name);
389                         connection_profile_get_name(profile_h, &name);
390                         SETTING_TRACE("name:%s", name);
391                         if (!result) { /*already false */
392                                 g_strlcat(speciliztion, ", ", MAX_SPECIALIZITION_LEN);
393                                 g_strlcat(speciliztion, _(name), MAX_SPECIALIZITION_LEN);
394                         } else {
395                                 g_strlcat(speciliztion, _(name), MAX_SPECIALIZITION_LEN);
396                         }
397                         result = FALSE;
398                 } else {
399                         ok_cnt++;
400                         if (is_default) {
401                                 /*setting_create_simple_popup(ad, ad->win_get, NULL, _("Deleting default profile")); */
402
403                                 if (first_valid_item) {
404                                         connection_profile_h first_profile_h = (connection_profile_h)(first_valid_item->belongs_to);
405                                         (void)connection_set_default_cellular_service_profile(ad->connection,
406                                                                                               ad->profile_service_type,
407                                                                                               first_profile_h);
408                                 }
409                         }
410                         /*eina_list_remove(ad->profile_list, profile_h); */
411                 }
412
413                 G_FREE(apn);
414                 G_FREE(name);
415                 G_FREE(id);
416
417                 check_list = eina_list_next(check_list);
418         }
419
420         if (!result) {
421                 g_strlcat(speciliztion, _(": "), MAX_SPECIALIZITION_LEN);
422                 g_strlcat(speciliztion, _("IDS_COM_POP_DELETE_FAILED"), MAX_SPECIALIZITION_LEN);
423                 setting_create_simple_popup(ad, ad->win_get, NULL, _(speciliztion));
424
425                 if (ad->navi_it_profile_del_list)
426                         elm_object_item_text_set(ad->navi_it_profile_del_list, _("IDS_COM_SK_DELETE"));
427
428                 if (ok_cnt) __profile_delete_list_draw(ad);
429
430                 /*disable Delete button */
431                 Evas_Object *toolbar = elm_object_item_part_content_get(ad->navi_it_profile_del_list, "toolbar");
432                 Elm_Object_Item *it = elm_toolbar_last_item_get(toolbar);
433                 elm_object_item_disabled_set(it, EINA_TRUE);
434         }
435
436         if (result) {
437                 /*setting_create_simple_popup(ad, ad->win_get, NULL, _("IDS_COM_POP_SUCCESS")); */
438                 setting_create_popup_without_btn(ad, ad->win_get, NULL, _("IDS_COM_POP_DELETED"), __popup_deleted_response_cb, 1, FALSE, FALSE);
439                 /*setting_create_popup_without_btn(ad, ad->win_get, NULL, _("IDS_COM_POP_DELETED"), __popup_deleted_response_cb, 0.5, FALSE, FALSE); */
440         }
441
442         if (def_profile) {
443                 G_FREE(def_apn);
444                 G_FREE(def_name);
445                 G_FREE(def_id);
446         }
447 }
448
449 static void
450 setting_network_profile_delete_click_softkey_select_all_cb(void *data,
451                                                            Evas_Object *obj,
452                                                            void *event_info)
453 {
454         SETTING_TRACE_BEGIN;
455         retm_if(data == NULL, "Data parameter is NULL");
456         SettingNetworkUG *ad = data;
457         Eina_List *check_list = ad->profile_del_list;
458         Setting_GenGroupItem_Data *item_data = NULL;
459         int selnum = 0;
460         int un_selnum = 0;
461         while (check_list) {
462                 item_data = (Setting_GenGroupItem_Data *)eina_list_data_get(check_list);
463                 if (NULL == item_data)
464                         continue;
465
466                 SETTING_TRACE("%s :%d", _(item_data->keyStr), item_data->chk_status);
467                 if (item_data->chk_status) {
468                         selnum++;
469                 } else {
470                         un_selnum++;
471                 }
472                 check_list = eina_list_next(check_list);
473         }
474         SETTING_TRACE("selnum:%d, un_selnum:%d", selnum, un_selnum);
475         if (un_selnum == 0 && selnum != 0) {
476                 /*current all are selected,so it need to unselect all after click the button */
477                 SETTING_TRACE("to unselect all");
478                 check_list = ad->profile_del_list;
479                 un_selnum = 0;
480                 while (check_list) {
481                         item_data = (Setting_GenGroupItem_Data *)eina_list_data_get(check_list);
482                         if (NULL == item_data)
483                                 continue;
484                         setting_update_gl_item_chk_status(item_data, 0);
485                         un_selnum++;
486                         check_list = eina_list_next(check_list);
487                 }
488                 selnum = 0;
489         } else {
490                 /*or else, to select all */
491                 SETTING_TRACE("to select all");
492                 check_list = ad->profile_del_list;
493                 selnum = 0;
494                 while (check_list) {
495                         item_data = (Setting_GenGroupItem_Data *)eina_list_data_get(check_list);
496                         if (NULL == item_data)
497                                 continue;
498
499                         setting_update_gl_item_chk_status(item_data, 1);
500                         selnum++;
501                         check_list = eina_list_next(check_list);
502                 }
503                 un_selnum = 0;
504         }
505
506         if (ad->navi_it_profile_del_list) {
507                 Evas_Object *toolbar = elm_object_item_part_content_get(ad->navi_it_profile_del_list, "toolbar");
508                 Elm_Object_Item *it = elm_toolbar_last_item_get(toolbar);
509                 if (selnum != 0) {
510                         char title[MAX_DISPLAY_NAME_LEN_ON_UI + 1] = {0, };
511                         snprintf(title, MAX_DISPLAY_NAME_LEN_ON_UI, _("IDS_ST_HEADER_PD_SELECTED"), selnum);
512                         elm_object_item_text_set(ad->navi_it_profile_del_list, _(title));
513                         elm_object_item_disabled_set(it, EINA_FALSE);
514                 } else {
515                         elm_object_item_text_set(ad->navi_it_profile_del_list, _("IDS_COM_SK_DELETE"));
516                         elm_object_item_disabled_set(it, EINA_TRUE);
517                 }
518         }
519         return;
520 }
521
522