tizen 2.3.1 release
[apps/home/settings.git] / setting-security / src / setting-security-delete-block-list.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-security-delete-block-list.h>
22
23 static int setting_security_delete_block_list_create(void *cb);
24 static int setting_security_delete_block_list_destroy(void *cb);
25 static int setting_security_delete_block_list_update(void *cb);
26 static int setting_security_delete_block_list_cleanup(void *cb);
27
28 setting_view setting_view_security_delete_block_list = {
29         .create = setting_security_delete_block_list_create,
30         .destroy = setting_security_delete_block_list_destroy,
31         .update = setting_security_delete_block_list_update,
32         .cleanup = setting_security_delete_block_list_cleanup,
33 };
34
35 void __setting_security_delete_block_IP_list_list_get(Eina_List **list)
36 {
37         SETTING_TRACE_BEGIN;
38         char temp[SETTING_STR_SLP_LEN] = {0,};
39         COMMAND *start = g_ipHead; /* get IP Command list head */
40         COMMAND *end = g_ipTail;  /* get IP Command list tail */
41         start = start->next ; /* <== HEAD is empty. */
42
43         int chk_idx = 0;
44         while (start != end) {
45                 if (start->command != NULL) {
46                         SETTING_TRACE_DEBUG("block list name : [%d] %s", chk_idx, start->command);
47
48                         if (!safeStrCmp(temp, start->command)) {
49                                 SETTING_TRACE_DEBUG("same list skip : [%d] temp : %s, command : %s", chk_idx , temp , start->command);
50                         } else {
51                                 BlockList_Info *pNode = (BlockList_Info *)malloc(sizeof(BlockList_Info));
52                                 if (pNode == NULL) {
53                                         continue;
54                                 }
55                                 memset(pNode, 0, sizeof(BlockList_Info));
56                                 /*get name*/
57                                 g_strlcpy(pNode->name, start->command, (gsize) sizeof(pNode->name));
58
59                                 *list = eina_list_append(*list, pNode);
60                         }
61                 }
62
63                 snprintf(temp, SETTING_STR_SLP_LEN, "%s", start->command);
64                 chk_idx++;
65                 start = start->next; /* go */
66         }
67 }
68
69 void __setting_security_delete_block_URL_list_list_get(Eina_List **list)
70 {
71         SETTING_TRACE_BEGIN;
72         COMMAND *start = NULL ;
73         COMMAND *end = NULL ;
74         char temp[SETTING_STR_SLP_LEN] = {0,};
75         start = g_urlHead; /* get IP Command list head */
76         end = g_urlTail; /* get IP Command list tail */
77         start = start->next ; /* <== HEAD is empty. */
78
79         int chk_idx = 0;
80
81         while (start != end) {
82                 if (start->command != NULL) {
83                         SETTING_TRACE_DEBUG("block list name : [%d] %s", chk_idx, start->command);
84
85                         if (!safeStrCmp(temp, start->command)) {
86                                 SETTING_TRACE_DEBUG("same list skip : [%d] temp : %s, command : %s", chk_idx , temp , start->command);
87                         } else {
88                                 BlockList_Info *pNode = (BlockList_Info *)malloc(sizeof(BlockList_Info));
89                                 if (pNode == NULL) {
90                                         continue;
91                                 }
92                                 memset(pNode, 0, sizeof(BlockList_Info));
93                                 /*get name*/
94                                 g_strlcpy(pNode->name, start->command, (gsize) sizeof(pNode->name));
95
96                                 *list = eina_list_append(*list, pNode);
97                         }
98                 }
99
100                 snprintf(temp, SETTING_STR_SLP_LEN, "%s", start->command);
101                 chk_idx++;
102                 start = start->next; /* go */
103         }
104 }
105
106 void __setting_security_delete_block_PORT_list_list_get(Eina_List **list)
107 {
108         SETTING_TRACE_BEGIN;
109         COMMAND *start = NULL ;
110         COMMAND *end = NULL ;
111         char temp[SETTING_STR_SLP_LEN] = {0,};
112         start = g_portHead; /* get IP Command list head */
113         end = g_portTail; /* get IP Command list tail */
114         start = start->next ; /* <== HEAD is empty. */
115
116         int chk_idx = 0;
117
118         while (start != end) {
119                 if (start->command != NULL) {
120                         SETTING_TRACE_DEBUG("block list name : [%d] %s", chk_idx, start->command);
121
122                         if (!safeStrCmp(temp, start->command)) {
123                                 SETTING_TRACE_DEBUG("same list skip : [%d] temp : %s, command : %s", chk_idx , temp , start->command);
124                         } else {
125                                 BlockList_Info *pNode = (BlockList_Info *)malloc(sizeof(BlockList_Info));
126                                 if (pNode == NULL) {
127                                         continue;
128                                 }
129                                 memset(pNode, 0, sizeof(BlockList_Info));
130                                 /*get name*/
131                                 g_strlcpy(pNode->name, start->command, (gsize) sizeof(pNode->name));
132
133                                 *list = eina_list_append(*list, pNode);
134                         }
135                 }
136
137                 snprintf(temp, SETTING_STR_SLP_LEN, "%s", start->command);
138                 chk_idx++;
139                 start = start->next; /* go */
140         }
141 }
142
143 static int setting_security_delete_block_list_create(void *cb)
144 {
145         SETTING_TRACE_BEGIN;
146         /* error check */
147         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
148
149         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
150         Evas_Object *scroller = NULL;
151
152         Evas_Object *view_layout = elm_layout_add(ad->win_main_layout);
153         elm_layout_file_set(view_layout, SETTING_THEME_EDJ_NAME, "selinfo_bottom");
154         evas_object_size_hint_weight_set(view_layout, EVAS_HINT_EXPAND, 0.0);
155
156         /*get list*/
157         Eina_List *list = NULL;
158         char *title = NULL;
159         if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_IP) {
160                 __setting_security_delete_block_IP_list_list_get(&list);
161                 title = Keystr_BlockedIPList;
162         } else if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_URL) {
163                 __setting_security_delete_block_URL_list_list_get(&list);
164                 title = Keystr_BlockedURLList;
165         } else if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_PORT) {
166                 __setting_security_delete_block_PORT_list_list_get(&list);
167                 title = Keystr_BlockedPortList;
168         }
169
170         /*  Evas_Object * win_layout, Evas_Object * win_obj */
171         Elm_Object_Item *navi_it = setting_push_layout_navi_bar(_(title),
172                                                                 _("IDS_COM_BODY_BACK"),
173                                                                 _("IDS_COM_SK_DELETE"), NULL,
174                                                                 __setting_security_delete_block_list_cancel_cb,
175                                                                 __setting_security_delete_block_list_delete_cb,
176                                                                 NULL, ad, view_layout, ad->navi_bar,
177                                                                 NULL);
178
179         Setting_Done_List_Data *list_data = &(ad->data_block_list);
180
181         list_data->win_main = ad->win_main_layout;
182         list_data->UG_data = ad;
183         list_data->ly_parent = view_layout;
184         list_data->select_all_checked = EINA_FALSE;
185         list_data->navi_it = navi_it;
186
187         int count = eina_list_count(list);
188         int i = 0;
189         for (; i < count; i++) {
190                 BlockList_Info *pNode = NULL;
191                 pNode = (BlockList_Info *)eina_list_nth(list, i);
192                 if (pNode == NULL)
193                         continue;
194                 list_data->chk_items[i].item_name = pNode->name;
195         }
196
197         list_data->cur_item_num = i;
198         SETTING_TRACE("list_data->cur_item_num:%d", list_data->cur_item_num);
199         scroller = setting_create_done_list(list_data, NULL);
200         elm_object_part_content_set(view_layout, "elm.swallow.contents", scroller);
201
202         if (list) {
203                 eina_list_free(list);
204                 list = NULL;
205         }
206
207         if (i == 0 || !setting_done_list_is_some_item_selected(&ad->data_block_list)) {
208                 /* disable "Delete" button */
209                 Evas_Object *delete_button = elm_object_item_part_content_get(navi_it, "toolbar");
210                 elm_object_item_disabled_set(elm_toolbar_last_item_get(delete_button), EINA_TRUE);
211         }
212         setting_view_security_delete_block_list.is_create = 1;
213         SETTING_TRACE_END;
214         return SETTING_RETURN_SUCCESS;
215 }
216
217 static int setting_security_delete_block_list_destroy(void *cb)
218 {
219         SETTING_TRACE_BEGIN;
220         /* error check */
221         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
222
223         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
224
225         elm_naviframe_item_pop(ad->navi_bar);
226
227         setting_view_security_delete_block_list.is_create = 0;
228
229         return SETTING_RETURN_SUCCESS;
230 }
231
232 static int setting_security_delete_block_list_update(void *cb)
233 {
234         SETTING_TRACE_BEGIN;
235
236         SettingSecurityUG *ad = (SettingSecurityUG *) cb;
237
238         return SETTING_RETURN_SUCCESS;
239 }
240
241 static int setting_security_delete_block_list_cleanup(void *cb)
242 {
243         SETTING_TRACE_BEGIN;
244         retv_if(cb == NULL, SETTING_GENERAL_ERR_NULL_DATA_PARAMETER);
245         setting_security_delete_block_list_destroy(cb);
246         return SETTING_RETURN_SUCCESS;
247 }
248
249 static Eina_Bool __setting_security_delete_block_list_cancel_cb(void *data,
250                                                                 Evas_Object *obj, void *event_info)
251 {
252         retm_if(data == NULL, "Data parameter is NULL");
253         SettingSecurityUG *ad = (SettingSecurityUG *) data;
254         setting_view_change(&setting_view_security_delete_block_list, &setting_view_security_blocked_list, ad);
255         return EINA_TRUE;
256 }
257
258 static void __setting_security_delete_block_list_genlist_update(void *data)
259 {
260         SETTING_TRACE_BEGIN;
261         setting_retm_if(data == NULL, "Data parameter is NULL");
262         SettingSecurityUG *ad = (SettingSecurityUG *) data;
263
264         Evas_Object *view_layout = ad->data_block_list.ly_parent;
265
266         Evas_Object *scroller = NULL;
267         scroller = elm_object_part_content_get(view_layout, "elm.swallow.contents");
268         setting_retm_if(NULL == scroller, "scroller is NULL");
269
270         /* checking layout or genlist type to clear */
271         char *objtype = NULL;
272         objtype = evas_object_type_get(view_layout);
273         if (!safeStrCmp(objtype, "elm_genlist")) {
274                 elm_genlist_clear(scroller);
275                 elm_object_part_content_unset(view_layout, "elm.swallow.contents");
276                 evas_object_del(scroller);
277                 scroller = NULL;
278         }
279
280         Eina_List *list = NULL;
281         if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_IP) {
282                 char *filePath = "/home/app/.firewall/";
283                 char *fileName = "List-IP";
284                 setting_security_firewall_data_init_list();
285                 setting_security_firewall_data_get_data(g_ipHead, g_ipTail, filePath, fileName);
286                 __setting_security_delete_block_IP_list_list_get(&list);
287         } else if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_URL) {
288                 char *filePath = "/home/app/.firewall/";
289                 char *fileName = "List-url";
290                 setting_security_firewall_data_init_list();
291                 setting_security_firewall_data_get_data(g_urlHead, g_urlTail, filePath, fileName);
292                 __setting_security_delete_block_URL_list_list_get(&list);
293         } else if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_PORT) {
294                 char *filePath = "/home/app/.firewall/";
295                 char *fileName = "List-port";
296                 setting_security_firewall_data_init_list();
297                 setting_security_firewall_data_get_data(g_portHead, g_portTail, filePath, fileName);
298                 __setting_security_delete_block_PORT_list_list_get(&list);
299         }
300
301         int count = eina_list_count(list);
302         int i = 0;
303         for (; i < count; i++) {
304                 BlockList_Info *pNode = NULL;
305                 pNode = (BlockList_Info *)eina_list_nth(list, i);
306                 if (pNode == NULL)
307                         continue;
308                 ad->data_block_list.chk_items[i].item_name = pNode->name;
309         }
310
311         ad->data_block_list.cur_item_num = i;
312         SETTING_TRACE("list_data->cur_item_num:%d", ad->data_block_list.cur_item_num);
313         scroller = setting_create_done_list(&ad->data_block_list, NULL);
314         elm_object_part_content_set(view_layout, "elm.swallow.contents", scroller);
315
316         if (list) {
317                 eina_list_free(list);
318                 list = NULL;
319         }
320
321         if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_IP) {
322                 notification_status_message_post(_(KeyStr_IP_removed));
323         } else if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_URL) {
324                 notification_status_message_post(_(KeyStr_Port_removed));
325         } else if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_PORT) {
326                 notification_status_message_post(_(KeyStr_URL_removed));
327         }
328
329         if (i == 0 || !setting_done_list_is_some_item_selected(&ad->data_block_list)) {
330                 /* disable "Delete" button */
331                 if (ad->data_block_list.navi_it) {
332                         Evas_Object *delete_button = elm_object_item_part_content_get(ad->data_block_list.navi_it, "toolbar");
333                         elm_object_item_disabled_set(elm_toolbar_last_item_get(delete_button), EINA_TRUE);
334                 }
335                 if (i == 0)
336                         setting_view_change(&setting_view_security_delete_block_list, &setting_view_security_blocked_list, ad);
337         } else {
338                 if (ad->data_block_list.navi_it) {
339                         Evas_Object *delete_button = elm_object_item_part_content_get(ad->data_block_list.navi_it, "toolbar");
340                         elm_object_item_disabled_set(elm_toolbar_last_item_get(delete_button), EINA_FALSE);
341                 }
342         }
343 }
344
345 static void __setting_security_delete_block_list_delete_cb(void *data,
346                                                            Evas_Object *obj, void *event_info)
347 {
348         SETTING_TRACE_BEGIN;
349         retm_if(data == NULL, "Data parameter is NULL");
350         SettingSecurityUG *ad = (SettingSecurityUG *) data;
351
352         if (ad->data_block_list.selInfoPop) {
353                 evas_object_del(ad->data_block_list.selInfoPop);
354                 ad->data_block_list.selInfoPop = NULL;
355         }
356
357         if (!setting_done_list_is_some_item_selected(&ad->data_block_list)) {
358                 setting_create_simple_popup(NULL, ad->win_get, NULL, _("IDS_ST_POP_NO_ITEM_SELECTED"));
359         } else {
360                 int i = 0;
361                 for (; i < ad->data_block_list.cur_item_num; i++) {
362                         if (!ad->data_block_list.chk_items[i].data_GenItem->chk_status) {
363                                 continue;
364                         }
365                         /*unselect item*/
366                         setting_update_gl_item_chk_status(ad->data_block_list.chk_items[i].data_GenItem, 0);
367
368                         if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_IP) {
369                                 /*for ip */
370                                 SETTING_TRACE_DEBUG("ip : %s", ad->data_block_list.chk_items[i].data_GenItem->keyStr);
371                                 int ret = setting_security_system_command("10", ad->data_block_list.chk_items[i].data_GenItem->keyStr);
372                                 SETTING_TRACE_DEBUG("ip delete err : %d", ret);
373                         } else if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_URL) {
374                                 /*for url */
375                                 SETTING_TRACE_DEBUG("url : %s", ad->data_block_list.chk_items[i].data_GenItem->keyStr);
376                                 int ret = setting_security_system_command("18", ad->data_block_list.chk_items[i].data_GenItem->keyStr);
377                                 SETTING_TRACE_DEBUG("url delete err : %d", ret);
378                         } else if (ad->firewall_block_type == SETTING_SEC_FIREWALL_BLOCK_PORT) {
379                                 /*for port */
380                                 SETTING_TRACE_DEBUG("port : %s", ad->data_block_list.chk_items[i].data_GenItem->keyStr);
381                                 int ret = setting_security_system_command("12", ad->data_block_list.chk_items[i].data_GenItem->keyStr);
382                                 SETTING_TRACE_DEBUG("port delete err : %d", ret);
383                         }
384                 }
385
386                 /* update genlist */
387                 __setting_security_delete_block_list_genlist_update(ad);
388                 setting_view_update(&setting_view_security_delete_block_list, ad);
389
390                 /* reset sel_all item */
391                 elm_check_state_set(ad->data_block_list.select_all_checkbox, 0);
392                 ad->data_block_list.select_all_checked = EINA_FALSE;
393         }
394 }