2 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <package_manager.h>
22 #include <pkgmgr-info.h>
23 #include <tizen_type.h>
24 #include <tzplatform_config.h>
26 #include <notification.h>
27 #include <notification_db.h>
28 #include <notification_list.h>
29 #include <notification_noti.h>
30 #include <notification_debug.h>
31 #include <notification_ipc.h>
32 #include <notification_private.h>
33 #include <notification_setting.h>
34 #include <notification_setting_internal.h>
36 #define NOTIFICATION_PRIVILEGE "http://tizen.org/privilege/notification"
38 typedef struct _noti_dnd_cb_info noti_dnd_cb_info_s;
45 struct _noti_dnd_cb_info {
46 dnd_changed_cb callback;
50 static GHashTable *_noti_dnd_cb_hash = NULL;
52 EXPORT_API int notification_setting_get_setting_array_for_uid(notification_setting_h *setting_array, int *count, uid_t uid)
54 if (setting_array == NULL || count == NULL) {
55 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
56 return NOTIFICATION_ERROR_INVALID_PARAMETER;
59 return notification_ipc_request_get_setting_array(setting_array, count, uid);
62 EXPORT_API int notification_setting_get_setting_array(notification_setting_h *setting_array, int *count)
64 return notification_setting_get_setting_array_for_uid(setting_array, count, getuid());
66 EXPORT_API int notification_setting_get_setting_by_package_name_for_uid(const char *package_name, notification_setting_h *setting, uid_t uid)
68 if (package_name == NULL || setting == NULL) {
69 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
70 return NOTIFICATION_ERROR_INVALID_PARAMETER;
73 return notification_ipc_request_get_setting_by_package_name(package_name, setting, uid);
77 EXPORT_API int notification_setting_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
79 return notification_setting_get_setting_by_package_name_for_uid(package_name, setting, getuid());
84 EXPORT_API int notification_setting_get_setting(notification_setting_h *setting)
87 char *package_name = NULL;
89 package_name = notification_get_pkgname_by_pid();
91 if (package_name == NULL)
92 return NOTIFICATION_ERROR_NOT_EXIST_ID;
94 ret = notification_setting_get_setting_by_package_name(package_name, setting);
102 EXPORT_API int notification_setting_get_package_name(notification_setting_h setting, char **value)
105 if (setting == NULL || value == NULL) {
106 NOTIFICATION_ERR("Invalid parameter");
107 return NOTIFICATION_ERROR_INVALID_PARAMETER;
110 if (setting->package_name == NULL) {
111 NOTIFICATION_ERR("setting->package_name is null");
112 return NOTIFICATION_ERROR_NOT_EXIST_ID;
115 *value = SAFE_STRDUP(setting->package_name);
117 return NOTIFICATION_ERROR_NONE;
120 EXPORT_API int notification_setting_set_package_name(notification_setting_h setting, char *value)
123 if (setting == NULL || value == NULL) {
124 NOTIFICATION_ERR("Invalid parameter");
125 return NOTIFICATION_ERROR_INVALID_PARAMETER;
128 if (setting->package_name != NULL)
129 free(setting->package_name);
131 setting->package_name = SAFE_STRDUP(value);
133 return NOTIFICATION_ERROR_NONE;
136 EXPORT_API int notification_setting_get_allow_to_notify(notification_setting_h setting, bool *value)
138 if (setting == NULL || value == NULL) {
139 NOTIFICATION_ERR("Invalid parameter");
140 return NOTIFICATION_ERROR_INVALID_PARAMETER;
143 *value = setting->allow_to_notify;
145 return NOTIFICATION_ERROR_NONE;
148 EXPORT_API int notification_setting_set_allow_to_notify(notification_setting_h setting, bool value)
150 if (setting == NULL) {
151 NOTIFICATION_ERR("Invalid parameter");
152 return NOTIFICATION_ERROR_INVALID_PARAMETER;
155 setting->allow_to_notify = value;
157 return NOTIFICATION_ERROR_NONE;
160 EXPORT_API int notification_setting_get_do_not_disturb_except(notification_setting_h setting, bool *value)
162 if (setting == NULL || value == NULL) {
163 NOTIFICATION_ERR("Invalid parameter");
164 return NOTIFICATION_ERROR_INVALID_PARAMETER;
167 *value = setting->do_not_disturb_except;
169 return NOTIFICATION_ERROR_NONE;
172 EXPORT_API int notification_setting_set_do_not_disturb_except(notification_setting_h setting, bool value)
174 if (setting == NULL) {
175 NOTIFICATION_ERR("Invalid parameter");
176 return NOTIFICATION_ERROR_INVALID_PARAMETER;
179 setting->do_not_disturb_except = value;
181 return NOTIFICATION_ERROR_NONE;
184 EXPORT_API int notification_setting_get_visibility_class(notification_setting_h setting, int *value)
186 if (setting == NULL || value == NULL) {
187 NOTIFICATION_ERR("Invalid parameter");
188 return NOTIFICATION_ERROR_INVALID_PARAMETER;
191 *value = setting->visibility_class;
193 return NOTIFICATION_ERROR_NONE;
196 EXPORT_API int notification_setting_set_visibility_class(notification_setting_h setting, int value)
198 if (setting == NULL) {
199 NOTIFICATION_ERR("Invalid parameter");
200 return NOTIFICATION_ERROR_INVALID_PARAMETER;
203 setting->visibility_class = value;
205 return NOTIFICATION_ERROR_NONE;
208 EXPORT_API int notification_setting_get_pop_up_notification(notification_setting_h setting, bool *value)
210 if (setting == NULL || value == NULL) {
211 NOTIFICATION_ERR("Invalid parameter");
212 return NOTIFICATION_ERROR_INVALID_PARAMETER;
215 *value = setting->pop_up_notification;
216 return NOTIFICATION_ERROR_NONE;
219 EXPORT_API int notification_setting_set_pop_up_notification(notification_setting_h setting, bool value)
221 if (setting == NULL) {
222 NOTIFICATION_ERR("Invalid parameter");
223 return NOTIFICATION_ERROR_INVALID_PARAMETER;
226 setting->pop_up_notification = value;
227 return NOTIFICATION_ERROR_NONE;
230 EXPORT_API int notification_setting_get_lock_screen_content(notification_setting_h setting, lock_screen_content_level_e *level)
232 if (setting == NULL || level == NULL) {
233 NOTIFICATION_ERR("Invalid parameter");
234 return NOTIFICATION_ERROR_INVALID_PARAMETER;
237 *level = setting->lock_screen_content_level;
238 return NOTIFICATION_ERROR_NONE;
241 EXPORT_API int notification_setting_set_lock_screen_content(notification_setting_h setting, lock_screen_content_level_e level)
243 if (setting == NULL) {
244 NOTIFICATION_ERR("Invalid parameter");
245 return NOTIFICATION_ERROR_INVALID_PARAMETER;
248 setting->lock_screen_content_level = level;
249 return NOTIFICATION_ERROR_NONE;
252 EXPORT_API int notification_setting_update_setting_for_uid(notification_setting_h setting, uid_t uid)
254 if (setting == NULL) {
255 NOTIFICATION_ERR("Invalid parameter");
256 return NOTIFICATION_ERROR_INVALID_PARAMETER;
259 return notification_ipc_update_setting(setting, uid);
262 EXPORT_API int notification_setting_update_setting(notification_setting_h setting)
264 return notification_setting_update_setting_for_uid(setting, getuid());
267 EXPORT_API int notification_setting_free_notification(notification_setting_h setting)
269 if (setting == NULL) {
270 NOTIFICATION_ERR("Invalid parameter");
271 return NOTIFICATION_ERROR_INVALID_PARAMETER;
274 SAFE_FREE(setting->package_name);
276 /* add codes to free all properties */
280 return NOTIFICATION_ERROR_NONE;
283 static bool _is_package_in_setting_table(sqlite3 *db, const char *package_name, uid_t uid)
285 sqlite3_stmt *db_statement = NULL;
286 int sqlite3_ret = SQLITE_OK;
290 sqlite3_ret = sqlite3_prepare_v2(db, "SELECT package_name FROM notification_setting WHERE uid = ? AND package_name = ?", -1, &db_statement, NULL);
292 if (sqlite3_ret != SQLITE_OK) {
293 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
298 sqlite3_bind_int(db_statement, field_index++, uid);
299 sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
301 sqlite3_ret = sqlite3_step(db_statement);
303 if (sqlite3_ret == SQLITE_DONE) {
304 NOTIFICATION_INFO("no matched package_name found[%s][%d]", package_name, sqlite3_ret);
309 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_ROW) {
310 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
316 sqlite3_finalize(db_statement);
321 static int foreach_package_info_callback(const pkgmgrinfo_pkginfo_h package_info, void *user_data)
323 setting_local_info *info = (setting_local_info *)user_data;
324 sqlite3 *db = info->db;
325 sqlite3_stmt *db_statement = NULL;
326 char *package_name = NULL;
327 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
328 int sqlite3_ret = SQLITE_OK;
332 if ((pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkgname(package_info, &package_name)) != PACKAGE_MANAGER_ERROR_NONE) {
333 NOTIFICATION_ERR("package_info_get_package failed [%d]", pkgmgr_ret);
338 if (_is_package_in_setting_table(db, package_name, info->uid) == true) {
339 NOTIFICATION_INFO("uid %d [%s] is exist", info->uid, package_name);
343 NOTIFICATION_INFO("uid %d [%s] will be inserted", info->uid, package_name);
344 sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (uid, package_name) VALUES (?, ?) ", -1, &db_statement, NULL);
346 if (sqlite3_ret != SQLITE_OK) {
347 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
352 sqlite3_bind_int(db_statement, field_index++, info->uid);
353 sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
355 sqlite3_ret = sqlite3_step(db_statement);
357 NOTIFICATION_INFO("sqlite3_step returns[%d]", sqlite3_ret);
359 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
360 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
366 sqlite3_finalize(db_statement);
371 EXPORT_API int notification_setting_refresh_setting_table(uid_t uid)
373 int err = NOTIFICATION_ERROR_NONE;
375 int sqlite3_ret = SQLITE_OK;
376 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
377 pkgmgrinfo_pkginfo_filter_h filter;
378 setting_local_info info;
380 NOTIFICATION_INFO("refresh setting table [%d]", uid);
381 sqlite3_ret = sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL);
382 if (sqlite3_ret != SQLITE_OK || db == NULL) {
383 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
384 err = NOTIFICATION_ERROR_FROM_DB;
388 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
390 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_create(&filter);
391 if (pkgmgr_ret != PMINFO_R_OK) {
392 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_create failed [%d]", pkgmgr_ret);
393 err = NOTIFICATION_ERROR_FROM_DB;
397 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_add_string(filter, PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, NOTIFICATION_PRIVILEGE);
398 if (pkgmgr_ret != PMINFO_R_OK) {
399 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_add_string failed [%d]", pkgmgr_ret);
400 err = NOTIFICATION_ERROR_FROM_DB;
406 pkgmgr_ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(filter, foreach_package_info_callback, &info, uid);
407 if (pkgmgr_ret != PMINFO_R_OK) {
408 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo failed [%d]", pkgmgr_ret);
409 err = NOTIFICATION_ERROR_FROM_DB;
413 pkgmgrinfo_pkginfo_filter_destroy(filter);
418 if (err == NOTIFICATION_ERROR_NONE)
419 sqlite3_exec(db, "END;", NULL, NULL, NULL);
421 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
423 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
424 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
431 OPERATION_TYPE_INSERT_RECORD = 0,
432 OPERATION_TYPE_DELETE_RECORD = 1,
433 } notification_setting_operation_type;
435 static int _notification_setting_alter_package_list(notification_setting_operation_type operation_type, const char *package_name, uid_t uid)
438 sqlite3_stmt *db_statement = NULL;
439 int sqlite3_ret = SQLITE_OK;
441 bool is_package_in_setting_table = false;
442 int err = NOTIFICATION_ERROR_NONE;
444 sqlite3_ret = db_util_open(DBPATH, &db, 0);
446 if (sqlite3_ret != SQLITE_OK || db == NULL) {
447 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
448 err = NOTIFICATION_ERROR_FROM_DB;
452 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
454 is_package_in_setting_table = _is_package_in_setting_table(db, package_name, uid);
456 switch (operation_type) {
457 case OPERATION_TYPE_INSERT_RECORD:
458 if (is_package_in_setting_table == true) {
459 NOTIFICATION_INFO("[%s] is already exist", package_name);
462 NOTIFICATION_INFO("[%s] will be inserted", package_name);
463 sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (uid, package_name) VALUES (?, ?) ", -1, &db_statement, NULL);
466 case OPERATION_TYPE_DELETE_RECORD:
467 if (is_package_in_setting_table == false) {
468 NOTIFICATION_INFO("[%s] is not exist", package_name);
471 NOTIFICATION_INFO("[%s] will be removed", package_name);
472 sqlite3_ret = sqlite3_prepare_v2(db, "DELETE FROM notification_setting WHERE uid = ? AND package_name = ? ", -1, &db_statement, NULL);
478 if (sqlite3_ret != SQLITE_OK) {
479 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
480 err = NOTIFICATION_ERROR_FROM_DB;
484 sqlite3_bind_int(db_statement, field_index++, uid);
485 sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
487 sqlite3_ret = sqlite3_step(db_statement);
489 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
490 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
491 err = NOTIFICATION_ERROR_FROM_DB;
496 sqlite3_finalize(db_statement);
499 NOTIFICATION_INFO("err [%d]", err);
500 if (err == NOTIFICATION_ERROR_NONE)
501 sqlite3_exec(db, "END;", NULL, NULL, NULL);
503 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
506 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
507 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
514 EXPORT_API int notification_setting_insert_package_for_uid(const char *package_id, uid_t uid)
516 return _notification_setting_alter_package_list(OPERATION_TYPE_INSERT_RECORD, package_id, uid);
519 EXPORT_API int notification_setting_delete_package_for_uid(const char *package_id, uid_t uid)
521 return _notification_setting_alter_package_list(OPERATION_TYPE_DELETE_RECORD, package_id, uid);
524 EXPORT_API int notification_system_setting_load_system_setting_for_uid(notification_system_setting_h *system_setting, uid_t uid)
526 if (system_setting == NULL) {
527 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
528 return NOTIFICATION_ERROR_INVALID_PARAMETER;
531 return notification_ipc_request_load_system_setting(system_setting, uid);
534 EXPORT_API int notification_system_setting_load_system_setting(notification_system_setting_h *system_setting)
536 return notification_system_setting_load_system_setting_for_uid(system_setting, getuid());
539 EXPORT_API int notification_system_setting_update_system_setting_for_uid(notification_system_setting_h system_setting, uid_t uid)
541 if (system_setting == NULL) {
542 NOTIFICATION_ERR("Invalid parameter");
543 return NOTIFICATION_ERROR_INVALID_PARAMETER;
546 return notification_ipc_update_system_setting(system_setting, uid);
549 EXPORT_API int notification_system_setting_update_system_setting(notification_system_setting_h system_setting)
551 return notification_system_setting_update_system_setting_for_uid(system_setting, getuid());
554 EXPORT_API int notification_system_setting_free_system_setting(notification_system_setting_h system_setting)
556 if (system_setting == NULL) {
557 NOTIFICATION_ERR("Invalid parameter");
558 return NOTIFICATION_ERROR_INVALID_PARAMETER;
561 /* add codes to free all properties */
563 if (system_setting->dnd_allow_exceptions != NULL)
564 g_list_free(system_setting->dnd_allow_exceptions);
566 SAFE_FREE(system_setting);
568 return NOTIFICATION_ERROR_NONE;
571 EXPORT_API int notification_system_setting_get_do_not_disturb(notification_system_setting_h system_setting, bool *value)
573 if (system_setting == NULL || value == NULL) {
574 NOTIFICATION_ERR("Invalid parameter");
575 return NOTIFICATION_ERROR_INVALID_PARAMETER;
578 *value = system_setting->do_not_disturb;
580 return NOTIFICATION_ERROR_NONE;
583 EXPORT_API int notification_system_setting_set_do_not_disturb(notification_system_setting_h system_setting, bool value)
585 if (system_setting == NULL) {
586 NOTIFICATION_ERR("Invalid parameter");
587 return NOTIFICATION_ERROR_INVALID_PARAMETER;
590 system_setting->do_not_disturb = value;
592 return NOTIFICATION_ERROR_NONE;
595 EXPORT_API int notification_system_setting_get_visibility_class(notification_system_setting_h system_setting, int *value)
597 if (system_setting == NULL || value == NULL) {
598 NOTIFICATION_ERR("Invalid parameter");
599 return NOTIFICATION_ERROR_INVALID_PARAMETER;
602 *value = system_setting->visibility_class;
604 return NOTIFICATION_ERROR_NONE;
607 EXPORT_API int notification_system_setting_set_visibility_class(notification_system_setting_h system_setting, int value)
609 if (system_setting == NULL) {
610 NOTIFICATION_ERR("Invalid parameter");
611 return NOTIFICATION_ERROR_INVALID_PARAMETER;
614 system_setting->visibility_class = value;
616 return NOTIFICATION_ERROR_NONE;
619 EXPORT_API int notification_system_setting_dnd_schedule_get_enabled(notification_system_setting_h system_setting, bool *enabled)
621 if (system_setting == NULL || enabled == NULL) {
622 NOTIFICATION_ERR("Invalid parameter");
623 return NOTIFICATION_ERROR_INVALID_PARAMETER;
626 *enabled = system_setting->dnd_schedule_enabled;
628 return NOTIFICATION_ERROR_NONE;
631 EXPORT_API int notification_system_setting_dnd_schedule_set_enabled(notification_system_setting_h system_setting, bool enabled)
633 if (system_setting == NULL) {
634 NOTIFICATION_ERR("Invalid parameter");
635 return NOTIFICATION_ERROR_INVALID_PARAMETER;
638 system_setting->dnd_schedule_enabled = enabled;
640 return NOTIFICATION_ERROR_NONE;
643 EXPORT_API int notification_system_setting_dnd_schedule_get_day(notification_system_setting_h system_setting, int *day)
645 if (system_setting == NULL || day == NULL) {
646 NOTIFICATION_ERR("Invalid parameter");
647 return NOTIFICATION_ERROR_INVALID_PARAMETER;
650 *day = system_setting->dnd_schedule_day;
652 return NOTIFICATION_ERROR_NONE;
655 EXPORT_API int notification_system_setting_dnd_schedule_set_day(notification_system_setting_h system_setting, int day)
657 if (system_setting == NULL) {
658 NOTIFICATION_ERR("Invalid parameter");
659 return NOTIFICATION_ERROR_INVALID_PARAMETER;
662 system_setting->dnd_schedule_day = day;
664 return NOTIFICATION_ERROR_NONE;
667 EXPORT_API int notification_system_setting_dnd_schedule_get_start_time(notification_system_setting_h system_setting, int *hour, int *min)
669 if (system_setting == NULL || hour == NULL || min == NULL) {
670 NOTIFICATION_ERR("Invalid parameter");
671 return NOTIFICATION_ERROR_INVALID_PARAMETER;
674 *hour = system_setting->dnd_start_hour;
675 *min = system_setting->dnd_start_min;
677 return NOTIFICATION_ERROR_NONE;
680 EXPORT_API int notification_system_setting_dnd_schedule_set_start_time(notification_system_setting_h system_setting, int hour, int min)
682 if (system_setting == NULL) {
683 NOTIFICATION_ERR("Invalid parameter");
684 return NOTIFICATION_ERROR_INVALID_PARAMETER;
687 system_setting->dnd_start_hour = hour;
688 system_setting->dnd_start_min = min;
690 return NOTIFICATION_ERROR_NONE;
693 EXPORT_API int notification_system_setting_dnd_schedule_get_end_time(notification_system_setting_h system_setting, int *hour, int *min)
695 if (system_setting == NULL || hour == NULL || min == NULL) {
696 NOTIFICATION_ERR("Invalid parameter");
697 return NOTIFICATION_ERROR_INVALID_PARAMETER;
700 *hour = system_setting->dnd_end_hour;
701 *min = system_setting->dnd_end_min;
703 return NOTIFICATION_ERROR_NONE;
706 EXPORT_API int notification_system_setting_dnd_schedule_set_end_time(notification_system_setting_h system_setting, int hour, int min)
708 if (system_setting == NULL) {
709 NOTIFICATION_ERR("Invalid parameter");
710 return NOTIFICATION_ERROR_INVALID_PARAMETER;
713 system_setting->dnd_end_hour = hour;
714 system_setting->dnd_end_min = min;
716 return NOTIFICATION_ERROR_NONE;
719 EXPORT_API int notification_system_setting_get_lock_screen_content(notification_system_setting_h system_setting, lock_screen_content_level_e *level)
721 if (system_setting == NULL || level == NULL) {
722 NOTIFICATION_ERR("Invalid parameter");
723 return NOTIFICATION_ERROR_INVALID_PARAMETER;
726 *level = system_setting->lock_screen_content_level;
728 return NOTIFICATION_ERROR_NONE;
731 EXPORT_API int notification_system_setting_set_lock_screen_content(notification_system_setting_h system_setting, lock_screen_content_level_e level)
733 if (system_setting == NULL) {
734 NOTIFICATION_ERR("Invalid parameter");
735 return NOTIFICATION_ERROR_INVALID_PARAMETER;
738 system_setting->lock_screen_content_level = level;
740 return NOTIFICATION_ERROR_NONE;
743 static gint _dnd_allow_exception_compare(gconstpointer a, gconstpointer b)
745 dnd_allow_exception_h dnd_allow_exception_data_a;
750 dnd_allow_exception_data_a = (dnd_allow_exception_h)a;
752 if (dnd_allow_exception_data_a->type == (int)b)
758 EXPORT_API int notification_system_setting_get_dnd_allow_exceptions(notification_system_setting_h system_setting, dnd_allow_exception_type_e type, int *value)
760 dnd_allow_exception_h dnd_allow_exception_data;
763 if (system_setting == NULL || value == NULL) {
764 NOTIFICATION_ERR("Invalid parameter");
765 return NOTIFICATION_ERROR_INVALID_PARAMETER;
768 list = g_list_find_custom(system_setting->dnd_allow_exceptions, (gconstpointer)type, _dnd_allow_exception_compare);
770 dnd_allow_exception_data = (dnd_allow_exception_h)list->data;
771 *value = dnd_allow_exception_data->value;
773 NOTIFICATION_ERR("Invalid parameter - type");
774 return NOTIFICATION_ERROR_INVALID_PARAMETER;
777 return NOTIFICATION_ERROR_NONE;
780 EXPORT_API int notification_system_setting_set_dnd_allow_exceptions(notification_system_setting_h system_setting, dnd_allow_exception_type_e type, int value)
782 dnd_allow_exception_h dnd_allow_exception_data;
785 if (system_setting == NULL) {
786 NOTIFICATION_ERR("Invalid parameter");
787 return NOTIFICATION_ERROR_INVALID_PARAMETER;
790 list = g_list_first(system_setting->dnd_allow_exceptions);
792 for (; list != NULL; list = list->next) {
793 dnd_allow_exception_data = (dnd_allow_exception_h)list->data;
794 if (dnd_allow_exception_data->type == type) {
795 dnd_allow_exception_data->value = value;
796 return NOTIFICATION_ERROR_NONE;
800 dnd_allow_exception_data = (dnd_allow_exception_h)malloc(sizeof(struct notification_system_setting_dnd_allow_exception));
801 if (dnd_allow_exception_data == NULL)
802 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
804 dnd_allow_exception_data->type = type;
805 dnd_allow_exception_data->value = value;
807 system_setting->dnd_allow_exceptions = g_list_append(system_setting->dnd_allow_exceptions, dnd_allow_exception_data);
809 return NOTIFICATION_ERROR_NONE;
812 static gint _noti_dnd_cb_compare(gconstpointer a, gconstpointer b)
814 noti_dnd_cb_info_s *info = NULL;
819 info = (noti_dnd_cb_info_s *)a;
820 if (info->callback == b)
826 void notification_call_dnd_changed_cb_for_uid(int do_not_disturb, uid_t uid)
828 GList *noti_dnd_cb_list = NULL;
829 noti_dnd_cb_info_s *dnd_data = NULL;
831 if (_noti_dnd_cb_hash == NULL)
834 noti_dnd_cb_list = (GList *)g_hash_table_lookup(_noti_dnd_cb_hash, GUINT_TO_POINTER(uid));
835 if (noti_dnd_cb_list == NULL) {
836 NOTIFICATION_ERR("Invalide data");
840 noti_dnd_cb_list = g_list_first(noti_dnd_cb_list);
842 for (; noti_dnd_cb_list != NULL; noti_dnd_cb_list = noti_dnd_cb_list->next) {
843 dnd_data = noti_dnd_cb_list->data;
845 if (dnd_data != NULL && dnd_data->callback != NULL)
846 dnd_data->callback(dnd_data->user_data, do_not_disturb);
850 EXPORT_API int notification_register_system_setting_dnd_changed_cb_for_uid(dnd_changed_cb callback, void *user_data, uid_t uid)
852 GList *noti_dnd_list = NULL;
853 GList *noti_dnd_found_list = NULL;
854 noti_dnd_cb_info_s *dnd_data = NULL;
856 if (callback == NULL)
857 return NOTIFICATION_ERROR_INVALID_PARAMETER;
859 if (notification_ipc_monitor_init(uid) != NOTIFICATION_ERROR_NONE) {
860 NOTIFICATION_ERR("notification_ipc_monitor_init error");
861 return NOTIFICATION_ERROR_IO_ERROR;
863 if (_noti_dnd_cb_hash == NULL)
864 _noti_dnd_cb_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
866 dnd_data = (noti_dnd_cb_info_s *)malloc(sizeof(noti_dnd_cb_info_s));
867 if (dnd_data == NULL)
868 return NOTIFICATION_ERROR_OUT_OF_MEMORY;
870 dnd_data->callback = callback;
871 dnd_data->user_data = user_data;
873 noti_dnd_list = (GList *)g_hash_table_lookup(_noti_dnd_cb_hash, GUINT_TO_POINTER(uid));
875 if (noti_dnd_list == NULL) {
876 noti_dnd_list = g_list_append(noti_dnd_list, dnd_data);
877 g_hash_table_insert(_noti_dnd_cb_hash, GUINT_TO_POINTER(uid), noti_dnd_list);
879 noti_dnd_found_list = g_list_find_custom(noti_dnd_list, (gconstpointer)callback,
880 _noti_dnd_cb_compare);
881 if (noti_dnd_found_list) {
882 NOTIFICATION_ERR("Already existing callback");
884 return NOTIFICATION_ERROR_INVALID_PARAMETER;
886 noti_dnd_list = g_list_append(noti_dnd_list, dnd_data);
890 return NOTIFICATION_ERROR_NONE;
893 EXPORT_API int notification_register_system_setting_dnd_changed_cb(dnd_changed_cb callback, void *user_data)
895 return notification_register_system_setting_dnd_changed_cb_for_uid(callback, user_data, getuid());
898 EXPORT_API int notification_unregister_system_setting_dnd_changed_cb_for_uid(dnd_changed_cb callback, uid_t uid)
900 GList *noti_dnd_cb_list = NULL;
901 GList *noti_dnd_del_list = NULL;
902 noti_dnd_cb_info_s *dnd_data = NULL;
904 if (callback == NULL)
905 return NOTIFICATION_ERROR_INVALID_PARAMETER;
907 if (_noti_dnd_cb_hash == NULL)
908 return NOTIFICATION_ERROR_INVALID_PARAMETER;
910 noti_dnd_cb_list = (GList *)g_hash_table_lookup(_noti_dnd_cb_hash, GUINT_TO_POINTER(uid));
912 if (noti_dnd_cb_list == NULL)
913 return NOTIFICATION_ERROR_INVALID_PARAMETER;
915 noti_dnd_del_list = g_list_find_custom(noti_dnd_cb_list, (gconstpointer)callback,
916 _noti_dnd_cb_compare);
918 if (noti_dnd_del_list) {
919 dnd_data = g_list_nth_data(noti_dnd_del_list, 0);
920 noti_dnd_cb_list = g_list_delete_link(noti_dnd_cb_list, noti_dnd_del_list);
923 return NOTIFICATION_ERROR_INVALID_PARAMETER;
926 if (noti_dnd_cb_list == NULL) {
927 g_hash_table_steal(_noti_dnd_cb_hash, GUINT_TO_POINTER(uid));
929 noti_dnd_cb_list = g_list_first(noti_dnd_cb_list);
930 g_hash_table_replace(_noti_dnd_cb_hash, GUINT_TO_POINTER(uid), noti_dnd_cb_list);
933 if (g_hash_table_size(_noti_dnd_cb_hash) == 0)
934 notification_ipc_monitor_fini();
936 return NOTIFICATION_ERROR_NONE;
939 EXPORT_API int notification_unregister_system_setting_dnd_changed_cb(dnd_changed_cb callback)
941 return notification_unregister_system_setting_dnd_changed_cb_for_uid(callback, getuid());
944 static bool _is_uid_in_system_setting_table(sqlite3 *db, uid_t uid)
947 sqlite3_stmt *db_statement = NULL;
948 int sqlite3_ret = SQLITE_OK;
951 sqlite3_ret = sqlite3_prepare_v2(db, "SELECT uid FROM notification_system_setting WHERE uid = ?", -1, &db_statement, NULL);
952 if (sqlite3_ret != SQLITE_OK) {
953 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
958 sqlite3_bind_int(db_statement, field_index++, uid);
960 sqlite3_ret = sqlite3_step(db_statement);
961 if (sqlite3_ret == SQLITE_DONE) {
962 NOTIFICATION_INFO("no matched uid found[%d][%d]", uid, sqlite3_ret);
967 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_ROW) {
968 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
975 sqlite3_finalize(db_statement);
980 EXPORT_API int notification_system_setting_init_system_setting_table(uid_t uid)
982 int err = NOTIFICATION_ERROR_NONE;
983 int sqlite3_ret = SQLITE_OK;
986 sqlite3_stmt *db_statement = NULL;
988 NOTIFICATION_INFO("init system setting table [%d]", uid);
989 db = notification_db_open(DBPATH);
991 return get_last_result();
993 /* if notification system setting don't init. */
994 if (_is_uid_in_system_setting_table(db, uid) == true) {
995 NOTIFICATION_DBG("Notification system setting table is already initialized.");
997 NOTIFICATION_DBG("Notification system setting table is not initialized yet");
998 sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_system_setting (uid) VALUES (?) ", -1, &db_statement, NULL);
999 if (sqlite3_ret != SQLITE_OK) {
1000 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
1001 err = NOTIFICATION_ERROR_FROM_DB;
1005 sqlite3_bind_int(db_statement, field_index++, uid);
1007 sqlite3_ret = sqlite3_step(db_statement);
1008 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
1009 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
1010 err = NOTIFICATION_ERROR_FROM_DB;
1015 NOTIFICATION_DBG("Notification system setting tables initialization is success.");
1019 if (err == NOTIFICATION_ERROR_NONE)
1020 sqlite3_exec(db, "END;", NULL, NULL, NULL);
1022 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
1023 notification_db_close(&db);
1026 sqlite3_finalize(db_statement);