4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <package_manager.h>
27 #include <pkgmgr-info.h>
28 #include <tizen_type.h>
30 #include <notification.h>
31 #include <notification_db.h>
32 #include <notification_list.h>
33 #include <notification_noti.h>
34 #include <notification_debug.h>
35 #include <notification_ipc.h>
36 #include <notification_private.h>
37 #include <notification_setting.h>
38 #include <notification_setting_internal.h>
40 #define NOTIFICATION_PRIVILEGE "http://tizen.org/privilege/notification"
44 EXPORT_API int notification_setting_get_setting_array(notification_setting_h *setting_array, int *count)
46 int ret = NOTIFICATION_ERROR_NONE;
47 if (setting_array == NULL || count == NULL) {
48 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
49 return NOTIFICATION_ERROR_INVALID_PARAMETER;
51 ret = notification_ipc_request_get_setting_array(setting_array, count);
55 EXPORT_API int notification_setting_get_setting_by_package_name(const char *package_name, notification_setting_h *setting)
57 int ret = NOTIFICATION_ERROR_NONE;
58 if (package_name == NULL || setting == NULL) {
59 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
60 return NOTIFICATION_ERROR_INVALID_PARAMETER;
62 ret = notification_ipc_request_get_setting_by_package_name(package_name, setting);
66 EXPORT_API int notification_setting_get_setting(notification_setting_h *setting)
69 char *package_name = NULL;
71 package_name = notification_get_pkgname_by_pid();
73 if (package_name == NULL)
74 return NOTIFICATION_ERROR_NOT_EXIST_ID;
76 ret = notification_setting_get_setting_by_package_name(package_name, setting);
83 EXPORT_API int notification_setting_get_package_name(notification_setting_h setting, char **value)
85 int err = NOTIFICATION_ERROR_NONE;
87 if (setting == NULL || value == NULL) {
88 NOTIFICATION_ERR("Invalid parameter\n");
89 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
93 if (setting->package_name == NULL) {
94 NOTIFICATION_ERR("setting->package_name is null\n");
95 err = NOTIFICATION_ERROR_NOT_EXIST_ID;
99 *value = SAFE_STRDUP(setting->package_name);
106 EXPORT_API int notification_setting_set_package_name(notification_setting_h setting, char *value)
108 int err = NOTIFICATION_ERROR_NONE;
110 if (setting == NULL || value == NULL) {
111 NOTIFICATION_ERR("Invalid parameter\n");
112 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
116 if (setting->package_name != NULL)
117 free(setting->package_name);
119 setting->package_name = SAFE_STRDUP(value);
126 EXPORT_API int notification_setting_get_allow_to_notify(notification_setting_h setting, bool *value)
128 int err = NOTIFICATION_ERROR_NONE;
130 if (setting == NULL || value == NULL) {
131 NOTIFICATION_ERR("Invalid parameter\n");
132 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
136 *value = setting->allow_to_notify;
143 EXPORT_API int notification_setting_set_allow_to_notify(notification_setting_h setting, bool value)
145 int err = NOTIFICATION_ERROR_NONE;
147 if (setting == NULL) {
148 NOTIFICATION_ERR("Invalid parameter\n");
149 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
153 setting->allow_to_notify = value;
160 EXPORT_API int notification_setting_get_do_not_disturb_except(notification_setting_h setting, bool *value)
162 int err = NOTIFICATION_ERROR_NONE;
164 if (setting == NULL || value == NULL) {
165 NOTIFICATION_ERR("Invalid parameter\n");
166 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
170 *value = setting->do_not_disturb_except;
177 EXPORT_API int notification_setting_set_do_not_disturb_except(notification_setting_h setting, bool value)
179 int err = NOTIFICATION_ERROR_NONE;
181 if (setting == NULL) {
182 NOTIFICATION_ERR("Invalid parameter\n");
183 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
187 setting->do_not_disturb_except = value;
194 EXPORT_API int notification_setting_get_visibility_class(notification_setting_h setting, int *value)
196 int err = NOTIFICATION_ERROR_NONE;
198 if (setting == NULL || value == NULL) {
199 NOTIFICATION_ERR("Invalid parameter\n");
200 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
204 *value = setting->visibility_class;
211 EXPORT_API int notification_setting_set_visibility_class(notification_setting_h setting, int value)
213 int err = NOTIFICATION_ERROR_NONE;
215 if (setting == NULL) {
216 NOTIFICATION_ERR("Invalid parameter\n");
217 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
221 setting->visibility_class = value;
228 EXPORT_API int notification_setting_update_setting(notification_setting_h setting)
230 int err = NOTIFICATION_ERROR_NONE;
232 if (setting == NULL) {
233 NOTIFICATION_ERR("Invalid parameter\n");
234 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
238 err = notification_ipc_update_setting(setting);
239 if (err != NOTIFICATION_ERROR_NONE) {
240 NOTIFICATION_ERR("notification_setting_update_setting returns[%d]\n", err);
248 EXPORT_API int notification_setting_free_notification(notification_setting_h setting)
250 int err = NOTIFICATION_ERROR_NONE;
252 if (setting == NULL) {
253 NOTIFICATION_ERR("Invalid parameter\n");
254 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
258 SAFE_FREE(setting->package_name);
260 /* add codes to free all properties */
268 EXPORT_API int notification_setting_db_update(const char *package_name, int allow_to_notify, int do_not_disturb_except, int visibility_class)
270 int err = NOTIFICATION_ERROR_NONE;
275 if (package_name == NULL || strlen(package_name) == 0)
276 return NOTIFICATION_ERROR_INVALID_PARAMETER;
278 sqlret = db_util_open(DBPATH, &db, 0);
279 if (sqlret != SQLITE_OK || db == NULL) {
280 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlret);
281 return NOTIFICATION_ERROR_FROM_DB;
284 sqlbuf = sqlite3_mprintf("UPDATE %s SET allow_to_notify = %d, do_not_disturb_except = %d, visibility_class = %d " \
285 "WHERE package_name = %Q",
286 NOTIFICATION_SETTING_DB_TABLE, allow_to_notify, do_not_disturb_except, visibility_class, package_name);
288 NOTIFICATION_ERR("fail to alloc query");
289 err = NOTIFICATION_ERROR_OUT_OF_MEMORY;
290 goto return_close_db;
293 err = notification_db_exec(db, sqlbuf, NULL);
297 sqlite3_free(sqlbuf);
299 sqlret = db_util_close(db);
300 if (sqlret != SQLITE_OK)
301 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);
306 static bool _is_package_in_setting_table(sqlite3 *db, const char *package_name)
308 sqlite3_stmt *db_statement = NULL;
309 int sqlite3_ret = SQLITE_OK;
313 sqlite3_ret = sqlite3_prepare_v2(db, "SELECT package_name FROM notification_setting WHERE package_name = ?", -1, &db_statement, NULL);
315 if (sqlite3_ret != SQLITE_OK) {
316 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
321 sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
323 sqlite3_ret = sqlite3_step(db_statement);
325 if (sqlite3_ret == SQLITE_DONE) {
326 NOTIFICATION_INFO("no matched package_name found[%s][%d]", package_name, sqlite3_ret);
331 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_ROW) {
332 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
338 sqlite3_finalize(db_statement);
343 static int foreach_package_info_callback(const pkgmgrinfo_pkginfo_h package_info, void *user_data)
345 sqlite3 *db = user_data;
346 sqlite3_stmt *db_statement = NULL;
347 char *package_name = NULL;
348 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
349 int sqlite3_ret = SQLITE_OK;
353 if ((pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkgname(package_info, &package_name)) != PACKAGE_MANAGER_ERROR_NONE) {
354 NOTIFICATION_ERR("package_info_get_package failed [%d]", pkgmgr_ret);
359 if (_is_package_in_setting_table(db, package_name) == true) {
360 NOTIFICATION_INFO("[%s] is exist", package_name);
364 NOTIFICATION_INFO("[%s] will be inserted", package_name);
366 sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (package_name) VALUES (?) ", -1, &db_statement, NULL);
368 if (sqlite3_ret != SQLITE_OK) {
369 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
374 sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
376 sqlite3_ret = sqlite3_step(db_statement);
378 NOTIFICATION_INFO("sqlite3_step returns[%d]", sqlite3_ret);
380 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
381 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
387 sqlite3_finalize(db_statement);
389 NOTIFICATION_INFO("foreach_package_info_callback returns[%d]", err);
393 EXPORT_API int notification_setting_refresh_setting_table()
395 int err = NOTIFICATION_ERROR_NONE;
397 int sqlite3_ret = SQLITE_OK;
398 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
399 pkgmgrinfo_pkginfo_filter_h filter;
402 sqlite3_ret = db_util_open(DBPATH, &db, 0);
404 if (sqlite3_ret != SQLITE_OK || db == NULL) {
405 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
406 err = NOTIFICATION_ERROR_FROM_DB;
410 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
412 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_create(&filter);
413 if (pkgmgr_ret != PMINFO_R_OK) {
414 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_create failed [%d]", pkgmgr_ret);
415 err = NOTIFICATION_ERROR_FROM_DB;
419 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_add_string(filter, PMINFO_PKGINFO_PROP_PACKAGE_PRIVILEGE, NOTIFICATION_PRIVILEGE);
420 if (pkgmgr_ret != PMINFO_R_OK) {
421 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_filter_add_string failed [%d]", pkgmgr_ret);
422 err = NOTIFICATION_ERROR_FROM_DB;
426 current_uid = getuid();
428 pkgmgr_ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(filter, foreach_package_info_callback, db, current_uid);
429 if (pkgmgr_ret != PMINFO_R_OK) {
430 NOTIFICATION_ERR("pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo failed [%d]", pkgmgr_ret);
431 err = NOTIFICATION_ERROR_FROM_DB;
435 pkgmgrinfo_pkginfo_filter_destroy(filter);
441 if (err == NOTIFICATION_ERROR_NONE)
442 sqlite3_exec(db, "END;", NULL, NULL, NULL);
444 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
446 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
447 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
450 NOTIFICATION_INFO("notification_setting_refresh_setting_table returns [%08X]", err);
456 OPERATION_TYPE_INSERT_RECORD = 0,
457 OPERATION_TYPE_DELETE_RECORD = 1,
458 } notification_setting_operation_type;
460 static int _notification_setting_alter_package_list(notification_setting_operation_type operation_type, const char *package_name)
463 sqlite3_stmt *db_statement = NULL;
464 int sqlite3_ret = SQLITE_OK;
466 bool is_package_in_setting_table = false;
467 int err = NOTIFICATION_ERROR_NONE;
469 sqlite3_ret = db_util_open(DBPATH, &db, 0);
471 if (sqlite3_ret != SQLITE_OK || db == NULL) {
472 NOTIFICATION_ERR("db_util_open failed [%s][%d]", DBPATH, sqlite3_ret);
473 err = NOTIFICATION_ERROR_FROM_DB;
477 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
479 is_package_in_setting_table = _is_package_in_setting_table(db, package_name);
481 switch (operation_type) {
482 case OPERATION_TYPE_INSERT_RECORD:
483 if (is_package_in_setting_table == true) {
484 NOTIFICATION_INFO("[%s] is already exist", package_name);
487 NOTIFICATION_INFO("[%s] will be inserted", package_name);
488 sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO notification_setting (package_name) VALUES (?) ", -1, &db_statement, NULL);
491 case OPERATION_TYPE_DELETE_RECORD:
492 if (is_package_in_setting_table == false) {
493 NOTIFICATION_INFO("[%s] is not exist", package_name);
496 NOTIFICATION_INFO("[%s] will be removed", package_name);
497 sqlite3_ret = sqlite3_prepare_v2(db, "DELETE FROM notification_setting WHERE package_name = ? ", -1, &db_statement, NULL);
503 if (sqlite3_ret != SQLITE_OK) {
504 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
505 err = NOTIFICATION_ERROR_FROM_DB;
509 sqlite3_bind_text(db_statement, field_index++, package_name, -1, SQLITE_TRANSIENT);
511 sqlite3_ret = sqlite3_step(db_statement);
513 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
514 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
515 err = NOTIFICATION_ERROR_FROM_DB;
520 sqlite3_finalize(db_statement);
523 NOTIFICATION_INFO("err [%d]", err);
524 if (err == NOTIFICATION_ERROR_NONE)
525 sqlite3_exec(db, "END;", NULL, NULL, NULL);
527 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
530 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
531 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlite3_ret);
538 bool privilege_info_cb(const char *privilege_name, void *user_data)
540 bool *found = user_data;
542 if (privilege_name && strcmp(NOTIFICATION_PRIVILEGE, privilege_name) == 0) {
550 static bool _has_privilege(const char *package_id)
553 int error_from_package_info = PACKAGE_MANAGER_ERROR_NONE;
554 package_info_h package_info = NULL;
556 error_from_package_info = package_info_create(package_id, &package_info);
557 if (error_from_package_info != PACKAGE_MANAGER_ERROR_NONE) {
558 NOTIFICATION_ERR("package_info_create failed [%d]", error_from_package_info);
562 error_from_package_info = package_info_foreach_privilege_info(package_info, privilege_info_cb, &found);
564 if (error_from_package_info != PACKAGE_MANAGER_ERROR_NONE) {
565 NOTIFICATION_ERR("package_info_foreach_privilege_info failed [%d]", error_from_package_info);
572 package_info_destroy(package_info);
577 EXPORT_API int notification_setting_insert_package(const char *package_id)
579 int err = NOTIFICATION_ERROR_NONE;
581 if (_has_privilege(package_id) == true)
582 err = _notification_setting_alter_package_list(OPERATION_TYPE_INSERT_RECORD, package_id);
587 EXPORT_API int notification_setting_delete_package(const char *package_id)
589 return _notification_setting_alter_package_list(OPERATION_TYPE_DELETE_RECORD, package_id);
592 /* system setting --------------------------------*/
594 EXPORT_API int notification_system_setting_load_system_setting(notification_system_setting_h *system_setting)
596 int ret = NOTIFICATION_ERROR_NONE;
597 if (system_setting == NULL) {
598 NOTIFICATION_ERR("NOTIFICATION_ERROR_INVALID_PARAMETER");
599 return NOTIFICATION_ERROR_INVALID_PARAMETER;
601 ret = notification_ipc_request_load_system_setting(system_setting);
606 EXPORT_API int notification_system_setting_update_system_setting(notification_system_setting_h system_setting)
608 int err = NOTIFICATION_ERROR_NONE;
610 if (system_setting == NULL) {
611 NOTIFICATION_ERR("Invalid parameter\n");
612 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
616 err = notification_ipc_update_system_setting(system_setting);
617 if (err != NOTIFICATION_ERROR_NONE) {
618 NOTIFICATION_ERR("notification_ipc_update_system_setting returns[%d]\n", err);
626 EXPORT_API int notification_system_setting_free_system_setting(notification_system_setting_h system_setting)
628 int err = NOTIFICATION_ERROR_NONE;
630 if (system_setting == NULL) {
631 NOTIFICATION_ERR("Invalid parameter\n");
632 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
636 /* add codes to free all properties */
638 SAFE_FREE(system_setting);
645 EXPORT_API int notification_system_setting_get_do_not_disturb(notification_system_setting_h system_setting, bool *value)
647 int err = NOTIFICATION_ERROR_NONE;
649 if (system_setting == NULL || value == NULL) {
650 NOTIFICATION_ERR("Invalid parameter\n");
651 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
655 *value = system_setting->do_not_disturb;
662 EXPORT_API int notification_system_setting_set_do_not_disturb(notification_system_setting_h system_setting, bool value)
664 int err = NOTIFICATION_ERROR_NONE;
666 if (system_setting == NULL) {
667 NOTIFICATION_ERR("Invalid parameter\n");
668 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
672 system_setting->do_not_disturb = value;
679 EXPORT_API int notification_system_setting_get_visibility_class(notification_system_setting_h system_setting, int *value)
681 int err = NOTIFICATION_ERROR_NONE;
683 if (system_setting == NULL || value == NULL) {
684 NOTIFICATION_ERR("Invalid parameter\n");
685 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
689 *value = system_setting->visibility_class;
696 EXPORT_API int notification_system_setting_set_visibility_class(notification_system_setting_h system_setting, int value)
698 int err = NOTIFICATION_ERROR_NONE;
700 if (system_setting == NULL) {
701 NOTIFICATION_ERR("Invalid parameter\n");
702 err = NOTIFICATION_ERROR_INVALID_PARAMETER;
706 system_setting->visibility_class = value;
714 EXPORT_API int notification_setting_db_update_system_setting(int do_not_disturb, int visibility_class)
716 int err = NOTIFICATION_ERROR_NONE;
720 sqlite3_stmt *db_statement = NULL;
722 sqlret = db_util_open(DBPATH, &db, 0);
724 if (sqlret != SQLITE_OK || db == NULL) {
725 NOTIFICATION_ERR("db_util_open failed [%s][%d][%s]", DBPATH, sqlret, sqlite3_errmsg(db));
726 err = NOTIFICATION_ERROR_FROM_DB;
727 goto return_close_db;
730 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
732 sqlret = sqlite3_prepare_v2(db, "UPDATE notification_system_setting SET do_not_disturb = ?, visibility_class = ?;", -1, &db_statement, NULL);
734 if (sqlret != SQLITE_OK) {
735 NOTIFICATION_ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlret, sqlite3_errmsg(db));
736 err = NOTIFICATION_ERROR_FROM_DB;
737 goto return_close_db;
740 sqlite3_bind_int(db_statement, field_index++, do_not_disturb);
741 sqlite3_bind_int(db_statement, field_index++, visibility_class);
743 sqlret = sqlite3_step(db_statement);
745 if (sqlret != SQLITE_OK && sqlret != SQLITE_DONE) {
746 NOTIFICATION_ERR("sqlite3_step failed [%d][%s]", sqlret, sqlite3_errmsg(db));
747 err = NOTIFICATION_ERROR_FROM_DB;
748 goto return_close_db;
751 sqlret = sqlite3_changes(db);
754 NOTIFICATION_WARN("No changes on DB");
759 sqlite3_finalize(db_statement);
763 if (err == NOTIFICATION_ERROR_NONE)
764 sqlite3_exec(db, "END;", NULL, NULL, NULL);
766 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
768 sqlret = db_util_close(db);
771 if (sqlret != SQLITE_OK)
772 NOTIFICATION_WARN("fail to db_util_close - [%d]", sqlret);