tizen 2.4 release
[apps/home/ug-setting-notification-efl.git] / ug-setting-notification-efl / src / notification-setting-info.c
1 /*
2  * Copyright (c) 2009 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
18
19 #include "notification-setting-info.h"
20 #include <notification.h>
21 #include <notification_setting.h>
22 #include <notification_setting_internal.h>
23
24 setting_info_s *setting_info = NULL;
25
26 #define VOICE_CALL_PACKAGE "org.tizen.call-ui"
27
28 void create_app_notification_list()
29 {
30         NOTISET_DBG("");
31         int i = 0;
32         int count = 0;
33         char *package_name = NULL;
34         bool allow_to_notify = false;
35         bool do_not_disturb_except = false;
36         notification_setting_h setting_array = NULL;
37         notification_setting_h *temp = NULL;
38         item_info_s *item_info = NULL;
39
40         remove_all_apps_list();
41         setting_info = calloc(1, sizeof(setting_info_s));
42
43         notification_setting_get_setting_array(&setting_array, &count);
44         ret_if(!setting_array);
45         NOTISET_DBG("count %d", count);
46
47         for (i = 0; i < count; i++) {
48                 item_info = calloc(1, sizeof(item_info_s));
49                 if (item_info) {
50                         temp = setting_array + i;
51
52                         notification_setting_get_package_name(temp, &package_name);
53                         item_info->appid = package_name;
54
55                         notification_setting_get_allow_to_notify(temp, &allow_to_notify);
56                         item_info->allow_to_notify = allow_to_notify;
57
58                         notification_setting_get_do_not_disturb_except(temp, &do_not_disturb_except);
59                         item_info->do_not_disturb_except = do_not_disturb_except;
60
61                         item_info->icon = get_app_icon(package_name);
62                         item_info->name = get_app_name(package_name);
63
64                         item_info->index = i;
65
66                         if (item_info->name && strcmp(package_name, VOICE_CALL_PACKAGE) != 0) {
67                                 setting_info->all_apps_list = eina_list_append(setting_info->all_apps_list, item_info);
68                         } else {
69                                 FREEIF(package_name);
70                                 FREEIF(item_info->name);
71                                 FREEIF(item_info->icon);
72                                 FREEIF(item_info);
73                         }
74                 }
75         }
76
77         setting_info->all_apps_list = eina_list_sort(setting_info->all_apps_list, eina_list_count(setting_info->all_apps_list), apps_sort_cb);
78         notification_setting_free_notification(setting_array);
79 }
80
81 void create_do_not_disturb_application_list()
82 {
83         NOTISET_DBG("");
84         int i = 0;
85         int count = 0;
86         char *package_name = NULL;
87         bool allow_to_notify = false;
88         bool do_not_disturb_except = false;
89         notification_setting_h setting_array = NULL;
90         notification_setting_h *temp = NULL;
91
92         remove_excepted_apps_list();
93         setting_info = calloc(1, sizeof(setting_info_s));
94
95         notification_setting_get_setting_array(&setting_array, &count);
96         ret_if(!setting_array);
97         NOTISET_DBG("count %d", count);
98
99         item_info_s *item_info = NULL;
100         for (i = 0; i < count; i++) {
101                 item_info = calloc(1, sizeof(item_info_s));
102                 if (item_info) {
103                         temp = setting_array + i;
104
105                         notification_setting_get_package_name(temp, &package_name);
106                         item_info->appid = package_name;
107
108                         notification_setting_get_allow_to_notify(temp, &allow_to_notify);
109                         item_info->allow_to_notify = allow_to_notify;
110
111                         notification_setting_get_do_not_disturb_except(temp, &do_not_disturb_except);
112                         item_info->do_not_disturb_except = do_not_disturb_except;
113
114                         item_info->icon = get_app_icon(package_name);
115                         item_info->name = get_app_name(package_name);
116
117                         item_info->index = i;
118
119                         if(item_info->name) {
120                                 if (do_not_disturb_except) {
121                                         setting_info->excepted_list = eina_list_append(setting_info->excepted_list, item_info);
122                                 } else {
123                                         setting_info->not_excepted_list = eina_list_append(setting_info->not_excepted_list, item_info);
124                                 }
125                         } else {
126                                 FREEIF(package_name);
127                                 FREEIF(item_info->name);
128                                 FREEIF(item_info->icon);
129                                 FREEIF(item_info);
130                         }
131                 }
132         }
133
134         setting_info->excepted_list = eina_list_sort(setting_info->excepted_list, eina_list_count(setting_info->excepted_list), apps_sort_cb);
135         setting_info->not_excepted_list = eina_list_sort(setting_info->not_excepted_list, eina_list_count(setting_info->not_excepted_list), apps_sort_cb);
136
137         notification_setting_free_notification(setting_array);
138 }
139
140 Eina_List *get_excepted_apps_list()
141 {
142         NOTISET_DBG("");
143         return setting_info->excepted_list;
144 }
145
146 Eina_List *get_all_apps_list()
147 {
148         NOTISET_DBG("");
149         return setting_info->all_apps_list;
150 }
151
152
153 Eina_List *get_not_excepted_apps_list()
154 {
155         NOTISET_DBG("");
156         return setting_info->not_excepted_list;
157 }
158
159
160 bool get_do_not_disturb() 
161 {
162         NOTISET_DBG("");
163
164         int err = NOTIFICATION_ERROR_NONE;
165         notification_system_setting_h system_setting = NULL;
166         bool do_not_disturb = false;
167
168         err = notification_system_setting_load_system_setting(&system_setting);
169         if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
170                 NOTISET_ERR("notification_system_setting_load_system_setting failed [%d]\n", err);
171                 goto out;
172         }
173
174         notification_system_setting_get_do_not_disturb(system_setting, &do_not_disturb);
175         NOTISET_DBG("do_not_disturb [%d]\n", do_not_disturb);
176         
177 out:
178         if (system_setting)
179                 notification_system_setting_free_system_setting(system_setting);
180
181         return do_not_disturb;
182 }
183
184 void set_do_not_disturb(bool state) 
185 {
186         NOTISET_DBG("");
187         int err = NOTIFICATION_ERROR_NONE;
188         notification_system_setting_h system_setting = NULL;
189
190         err = notification_system_setting_load_system_setting(&system_setting);
191         if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
192                 NOTISET_ERR("notification_system_setting_load_system_setting failed [%d]\n", err);
193                 goto out;
194         }
195
196         notification_system_setting_set_do_not_disturb(system_setting, state);
197         NOTISET_DBG("set do_not_disturb [%d]\n", state);
198
199         err = notification_system_setting_update_system_setting(system_setting);
200         if (err != NOTIFICATION_ERROR_NONE) {
201                 NOTISET_ERR("notification_setting_update_setting [%d]\n", err);
202                 goto out;
203         }
204 out:
205         if (system_setting)
206                 notification_system_setting_free_system_setting(system_setting);
207
208 }
209
210 bool set_allow_to_nofity(char *pkg_name, bool state)
211 {
212         NOTISET_DBG("");
213         int err = NOTIFICATION_ERROR_NONE;
214
215         notification_system_setting_h system_setting = NULL;
216
217         err = notification_setting_get_setting_by_package_name(pkg_name, &system_setting);
218         if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
219                 NOTISET_ERR("notification_setting_get_setting_by_package_name [%d]\n", err);
220                 goto out;
221         }
222
223         notification_setting_set_allow_to_notify(system_setting, state);
224         NOTISET_DBG("notification_setting_set_allow_to_notify [%d]\n", state);
225
226         err = notification_setting_update_setting(system_setting);
227         if (err != NOTIFICATION_ERROR_NONE) { 
228                 NOTISET_ERR("notification_setting_update_setting [%d]\n", err);
229                 goto out;
230         }
231
232         if (system_setting)
233                 notification_system_setting_free_system_setting(system_setting);
234         return true;
235
236 out:
237         if (system_setting)
238                 notification_system_setting_free_system_setting(system_setting);
239         
240         return false;
241
242 }
243
244 bool set_excepted_apps(char *pkg_name, bool state)
245 {
246         NOTISET_DBG("");
247
248         int err = NOTIFICATION_ERROR_NONE;
249
250         notification_system_setting_h system_setting = NULL;
251
252         err = notification_setting_get_setting_by_package_name(pkg_name, &system_setting);
253         if (err != NOTIFICATION_ERROR_NONE || system_setting == NULL) {
254                 NOTISET_ERR("notification_setting_get_setting_by_package_name [%d]\n", err);
255                 goto out;
256         }
257
258         notification_setting_set_do_not_disturb_except(system_setting, state);
259         NOTISET_DBG("notification_setting_set_do_not_disturb_except [%s] [%d]\n", pkg_name, state);
260
261         err = notification_setting_update_setting(system_setting);
262         if (err != NOTIFICATION_ERROR_NONE) {
263                 NOTISET_ERR("notification_setting_update_setting err[%d]\n", err);
264         }
265
266         if (system_setting)
267                 notification_system_setting_free_system_setting(system_setting);
268         return true;
269
270 out:
271         if (system_setting)
272                 notification_system_setting_free_system_setting(system_setting);
273
274         return false;
275
276 }
277
278 static void _remove_apps_list(Eina_List* input_list) {
279         NOTISET_DBG("");
280         item_info_s *item_info = NULL;
281
282         if(input_list) {
283                 EINA_LIST_FREE(input_list, item_info) {
284                         //NOTISET_DBG("remove %s", item_info->name);
285                         FREEIF(item_info->appid);
286                         FREEIF(item_info->name);
287                         FREEIF(item_info->icon);
288                         FREEIF(item_info);
289                 }
290         }
291 }
292
293
294 void remove_all_apps_list() {
295         NOTISET_DBG("");
296         if(setting_info) {
297                 _remove_apps_list(setting_info->all_apps_list);
298         }
299 }
300 void remove_excepted_apps_list(){
301         NOTISET_DBG("");
302         if(setting_info) {
303                 _remove_apps_list(setting_info->excepted_list);
304                 _remove_apps_list(setting_info->not_excepted_list);
305         }
306
307 }
308