2 * Copyright (c) 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.
22 #include <pkgmgr-info.h>
23 #include <package_manager.h>
25 #include "badge_setting.h"
26 #include "badge_setting_service.h"
28 #include "badge_error.h"
29 #include "badge_log.h"
31 #define BADGE_PRIVILEGE "http://tizen.org/privilege/notification"
38 static bool _get_table_field_data_int(char **table, int *buf, int index)
40 if (table == NULL || buf == NULL || index < 0) {
42 ERR("table[%p], buf[%p], index[%d]", table, buf, index);
47 if (table[index] != NULL) {
48 *buf = atoi(table[index]);
58 static bool _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
64 if (table == NULL || buf == NULL || index < 0) {
66 ERR("table[%p], buf[%p], index[%d]", table, buf, index);
73 *buf = NULL; /* LCOV_EXCL_LINE */
77 *buf = (char *)malloc(sLen + 1);
79 ERR("malloc is failed"); /* LCOV_EXCL_LINE */
82 memset(*buf, 0, sLen + 1);
83 strncpy(*buf, pTemp, sLen);
85 *buf = NULL; /* LCOV_EXCL_LINE */
95 EXPORT_API int badge_db_get_setting_by_appid(const char *appid, badge_setting_h *setting, uid_t uid)
97 int ret = BADGE_ERROR_NONE;
102 char *sql_query = NULL;
103 char **query_result = NULL;
104 badge_setting_h result_setting;
108 return BADGE_ERROR_INVALID_PARAMETER;
110 sql_ret = db_util_open(BADGE_DB_PATH, &db, 0);
111 if (sql_ret != SQLITE_OK || db == NULL) {
112 ERR("Failed db util open [%s][%d]", BADGE_DB_PATH, sql_ret);
113 return BADGE_ERROR_FROM_DB;
116 sql_query = sqlite3_mprintf("SELECT pkgname, appid, allow_to_display FROM %s " \
117 "WHERE appid = %Q AND (uid = %d OR uid = %d) " \
118 "ORDER BY uid DESC;",
119 BADGE_SETTING_DB_TABLE, appid, uid, tzplatform_getuid(TZ_SYS_GLOBALAPP_USER));
121 ERR("fail to alloc query");
122 ret = BADGE_ERROR_FROM_DB;
126 sql_ret = sqlite3_get_table(db, sql_query, &query_result, &row_count, &col_count, NULL);
127 if (sql_ret != SQLITE_OK && sql_ret != -1) {
128 ERR("sqlite3_get_table failed [%d][%s]", sql_ret, sql_query);
129 ret = BADGE_ERROR_FROM_DB;
134 DBG("No setting found for [%s]", appid);
135 ret = BADGE_ERROR_NOT_EXIST;
139 result_setting = (struct badge_setting *)malloc(sizeof(struct badge_setting));
140 if (result_setting == NULL) {
141 ERR("fail to alloc setting");
142 ret = BADGE_ERROR_OUT_OF_MEMORY;
146 col_index = col_count;
148 _get_table_field_data_string(query_result, &(result_setting[0].pkgname), 1, col_index++);
149 _get_table_field_data_string(query_result, &(result_setting[0].appid), 1, col_index++);
150 _get_table_field_data_int(query_result, (int *)&(result_setting[0].allow_to_display), col_index++);
152 *setting = result_setting;
156 sqlite3_free_table(query_result);
158 sqlite3_free(sql_query);
160 sql_ret = db_util_close(db);
161 if (sql_ret != SQLITE_OK)
162 WARN("fail to db_util_close");
168 EXPORT_API int badge_db_update_setting(char *pkgname, char *appid, int allow_to_display, uid_t uid)
170 int ret = BADGE_ERROR_NONE;
175 if (pkgname == NULL || appid == NULL) {
176 ERR("Invalid package name or app id");
177 return BADGE_ERROR_INVALID_PARAMETER;
180 sql_ret = db_util_open(BADGE_DB_PATH, &db, 0);
181 if (sql_ret != SQLITE_OK || db == NULL) {
182 ERR("db_util_open failed [%s][%d]", BADGE_DB_PATH, sql_ret);
183 return BADGE_ERROR_FROM_DB;
186 sqlbuf = sqlite3_mprintf("UPDATE %s SET allow_to_display = %d " \
187 "WHERE pkgname = %Q AND appid = %Q " \
189 BADGE_SETTING_DB_TABLE, allow_to_display,
190 pkgname, appid, uid);
193 ERR("fail to alloc query");
194 ret = BADGE_ERROR_FROM_DB;
198 ret = badge_db_exec(db, sqlbuf, NULL);
202 sqlite3_free(sqlbuf);
204 sql_ret = db_util_close(db);
205 if (sql_ret != SQLITE_OK)
206 WARN("fail to db_util_close");
212 EXPORT_API int badge_db_get_allow_to_display_by_appid(char *appid, int *allow_to_display, uid_t uid)
214 int ret = BADGE_ERROR_NONE;
219 char *sql_query = NULL;
220 char **query_result = NULL;
224 return BADGE_ERROR_INVALID_PARAMETER;
226 sql_ret = db_util_open(BADGE_DB_PATH, &db, 0);
227 if (sql_ret != SQLITE_OK || db == NULL) {
228 ERR("Failed db util open [%s][%d]", BADGE_DB_PATH, sql_ret);
229 return BADGE_ERROR_FROM_DB;
232 sql_query = sqlite3_mprintf("SELECT allow_to_display FROM %s WHERE appid = %Q " \
233 "AND (uid = %d OR uid = %d) ORDER BY uid DESC;",
234 BADGE_SETTING_DB_TABLE, appid, uid,
235 tzplatform_getuid(TZ_SYS_GLOBALAPP_USER));
237 ERR("fail to alloc query");
238 ret = BADGE_ERROR_FROM_DB;
242 sql_ret = sqlite3_get_table(db, sql_query, &query_result, &row_count, &col_count, NULL);
243 if (sql_ret != SQLITE_OK && sql_ret != -1) {
244 ERR("sqlite3_get_table failed [%d][%s]", sql_ret, sql_query);
245 ret = BADGE_ERROR_FROM_DB;
250 DBG("No setting found for [%s]", appid);
251 ret = BADGE_ERROR_NOT_EXIST;
255 col_index = col_count;
257 _get_table_field_data_int(query_result, (int *)allow_to_display, col_index++);
261 sqlite3_free_table(query_result);
263 sqlite3_free(sql_query);
265 sql_ret = db_util_close(db);
266 if (sql_ret != SQLITE_OK)
267 WARN("fail to db_util_close");
273 static bool _is_package_in_setting_table(sqlite3 *db, const char *pkgname, const char* appid, uid_t uid)
275 sqlite3_stmt *db_statement = NULL;
276 int sqlite3_ret = SQLITE_OK;
281 sqlite3_ret = sqlite3_prepare_v2(db, "SELECT appid FROM badge_setting WHERE uid = ? AND pkgname = ? AND appid = ?", -1, &db_statement, NULL);
283 sqlite3_ret = sqlite3_prepare_v2(db, "SELECT pkgname FROM badge_setting WHERE uid = ? AND pkgname = ?", -1, &db_statement, NULL);
285 if (sqlite3_ret != SQLITE_OK) {
286 ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
291 sqlite3_bind_int(db_statement, field_index++, uid);
292 sqlite3_bind_text(db_statement, field_index++, pkgname, -1, SQLITE_TRANSIENT);
294 sqlite3_bind_text(db_statement, field_index++, appid, -1, SQLITE_TRANSIENT);
296 sqlite3_ret = sqlite3_step(db_statement);
297 if (sqlite3_ret == SQLITE_DONE) {
298 INFO("no matched appid from pkgname found[%s][%s][%d]", pkgname, appid, sqlite3_ret);
303 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_ROW) {
304 ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
311 sqlite3_finalize(db_statement);
316 static int app_info_callback(const pkgmgrinfo_appinfo_h handle, void *user_data)
318 badge_setting_info *info = (badge_setting_info *)user_data;
319 sqlite3 *db = info->db;
320 sqlite3_stmt *db_statement = NULL;
321 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
323 int sqlite3_ret = SQLITE_OK;
325 char *pkgname = NULL;
327 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
328 if (pkgmgr_ret != PACKAGE_MANAGER_ERROR_NONE) {
329 ERR("pkgmgrinfo_appinfo_get_appid failed [%d]", pkgmgr_ret);
333 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgname(handle, &pkgname);
334 if (pkgmgr_ret != PACKAGE_MANAGER_ERROR_NONE) {
335 ERR("pkgmgrinfo_appinfo_get_pkgname failed [%d]", pkgmgr_ret);
339 if (_is_package_in_setting_table(db, pkgname, appid, info->uid) == true) {
340 INFO("uid %d [%s] is exist", info->uid, appid);
344 INFO("uid %d pkgname %s [%s] will be inserted", info->uid, pkgname, appid);
345 sqlite3_ret = sqlite3_prepare_v2(db, "INSERT INTO badge_setting (uid, pkgname, appid) "
346 "VALUES (?, ?, ?) ", -1, &db_statement, NULL);
348 if (sqlite3_ret != SQLITE_OK) {
349 ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
353 sqlite3_bind_int(db_statement, field_index++, info->uid);
354 sqlite3_bind_text(db_statement, field_index++, pkgname, -1, SQLITE_TRANSIENT);
355 sqlite3_bind_text(db_statement, field_index++, appid, -1, SQLITE_TRANSIENT);
357 sqlite3_ret = sqlite3_step(db_statement);
359 INFO("sqlite3_step returns[%d]", sqlite3_ret);
361 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE)
362 ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
366 sqlite3_finalize(db_statement);
371 static int package_info_callback(const pkgmgrinfo_pkginfo_h package_info, void *user_data)
373 char *pkgname = NULL;
374 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
375 pkgmgrinfo_appinfo_filter_h handle = NULL;
376 badge_setting_info *info = (badge_setting_info *)user_data;
378 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkgname(package_info, &pkgname);
379 if (pkgmgr_ret != PACKAGE_MANAGER_ERROR_NONE) {
380 ERR("package_info_get_package failed [%d]", pkgmgr_ret);
384 pkgmgr_ret = pkgmgrinfo_appinfo_filter_create(&handle);
385 if (pkgmgr_ret != PMINFO_R_OK) {
386 ERR("pkgmgrinfo_appinfo_filter_create failed [%d]", pkgmgr_ret);
390 pkgmgr_ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_PACKAGE, pkgname);
391 if (pkgmgr_ret != PMINFO_R_OK) {
392 ERR("pkgmgrinfo_appinfo_filter_add_string failed [%d]", pkgmgr_ret);
396 pkgmgr_ret = pkgmgrinfo_appinfo_filter_add_bool(handle, PMINFO_APPINFO_PROP_APP_NODISPLAY, false);
397 if (pkgmgr_ret != PMINFO_R_OK) {
398 ERR("pkgmgrinfo_appinfo_filter_add_bool failed [%d]", pkgmgr_ret);
402 pkgmgr_ret = pkgmgrinfo_appinfo_usr_filter_foreach_appinfo(handle, app_info_callback, info, info->uid);
403 if (pkgmgr_ret != PMINFO_R_OK) {
404 ERR("pkgmgrinfo_pkginfo_filter_foreach_appinfo failed [%d]", pkgmgr_ret);
410 pkgmgrinfo_appinfo_filter_destroy(handle);
415 EXPORT_API int badge_setting_insert_package_for_uid(const char *pkgname, uid_t uid)
418 int ret = BADGE_ERROR_NONE;
419 int sqlite3_ret = SQLITE_OK;
420 int pkgmgr_ret = PACKAGE_MANAGER_ERROR_NONE;
421 badge_setting_info info;
422 pkgmgrinfo_pkginfo_filter_h handle = NULL;
424 sqlite3_ret = sqlite3_open_v2(BADGE_DB_PATH, &db, SQLITE_OPEN_READWRITE, NULL);
425 if (sqlite3_ret != SQLITE_OK || db == NULL) {
426 ERR("db_util_open failed [%s][%d]", BADGE_DB_PATH, sqlite3_ret);
427 ret = BADGE_ERROR_FROM_DB;
431 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
433 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_create(&handle);
434 if (pkgmgr_ret != PMINFO_R_OK) {
435 ERR("pkgmgrinfo_pkginfo_filter_create failed [%d]", pkgmgr_ret);
436 ret = BADGE_ERROR_FROM_DB;
440 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_add_string(handle, PMINFO_PKGINFO_PROP_PACKAGE_ID, pkgname);
441 if (pkgmgr_ret != PMINFO_R_OK) {
442 ERR("pkgmgrinfo_pkginfo_filter_add_string failed [%d]", pkgmgr_ret);
443 ret = BADGE_ERROR_FROM_DB;
449 pkgmgr_ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(handle, package_info_callback, &info, uid);
450 if (pkgmgr_ret != PMINFO_R_OK) {
451 ERR("pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo failed [%d]", pkgmgr_ret);
452 ret = BADGE_ERROR_FROM_DB;
458 pkgmgrinfo_pkginfo_filter_destroy(handle);
460 if (ret == BADGE_ERROR_NONE)
461 sqlite3_exec(db, "END;", NULL, NULL, NULL);
463 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
465 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
466 WARN("db_util_close failed [%d]", sqlite3_ret);
472 EXPORT_API int badge_setting_delete_package_for_uid(const char *pkgname, uid_t uid)
475 sqlite3_stmt *db_statement = NULL;
476 int ret = BADGE_ERROR_NONE;
477 int sqlite3_ret = SQLITE_OK;
479 bool is_package_in_setting_table = false;
481 sqlite3_ret = sqlite3_open_v2(BADGE_DB_PATH, &db, SQLITE_OPEN_READWRITE, NULL);
482 if (sqlite3_ret != SQLITE_OK || db == NULL) {
483 ERR("db_util_open failed [%s][%d]", BADGE_DB_PATH, sqlite3_ret);
484 ret = BADGE_ERROR_FROM_DB;
488 is_package_in_setting_table = _is_package_in_setting_table(db, pkgname, NULL, uid);
489 if (is_package_in_setting_table == false) {
490 INFO("[%s] is not exist", pkgname);
494 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
496 sqlite3_ret = sqlite3_prepare_v2(db, "DELETE FROM badge_setting WHERE uid = ? AND pkgname = ? ", -1, &db_statement, NULL);
497 if (sqlite3_ret != SQLITE_OK) {
498 ERR("sqlite3_prepare_v2 failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
499 ret = BADGE_ERROR_FROM_DB;
503 sqlite3_bind_int(db_statement, field_index++, uid);
504 sqlite3_bind_text(db_statement, field_index++, pkgname, -1, SQLITE_TRANSIENT);
506 sqlite3_ret = sqlite3_step(db_statement);
507 if (sqlite3_ret != SQLITE_OK && sqlite3_ret != SQLITE_DONE) {
508 ERR("sqlite3_step failed [%d][%s]", sqlite3_ret, sqlite3_errmsg(db));
509 ret = BADGE_ERROR_FROM_DB;
514 sqlite3_finalize(db_statement);
516 if (ret == BADGE_ERROR_NONE)
517 sqlite3_exec(db, "END;", NULL, NULL, NULL);
519 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
521 if ((sqlite3_ret = db_util_close(db)) != SQLITE_OK)
522 WARN("db_util_close failed [%d]", sqlite3_ret);
528 EXPORT_API int badge_setting_refresh_setting_table(uid_t uid)
530 int ret = BADGE_ERROR_NONE;
534 badge_setting_info info;
535 pkgmgrinfo_pkginfo_filter_h filter;
537 sql_ret = sqlite3_open_v2(BADGE_DB_PATH, &db, SQLITE_OPEN_READWRITE, NULL);
538 if (sql_ret != SQLITE_OK || db == NULL) {
539 ERR("sqlite3_open_v2 fail [%s][%d]", BADGE_DB_PATH, sql_ret);
540 return BADGE_ERROR_FROM_DB;
543 sqlite3_exec(db, "BEGIN immediate;", NULL, NULL, NULL);
545 pkgmgr_ret = pkgmgrinfo_pkginfo_filter_create(&filter);
546 if (pkgmgr_ret != PMINFO_R_OK) {
547 ERR("pkgmgrinfo_pkginfo_filter_create failed [%d]", pkgmgr_ret);
548 ret = BADGE_ERROR_FROM_DB;
554 pkgmgr_ret = pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo(filter, package_info_callback, &info, uid);
555 if (pkgmgr_ret != PMINFO_R_OK) {
556 ERR("pkgmgrinfo_pkginfo_usr_filter_foreach_pkginfo failed [%d]", pkgmgr_ret);
557 ret = BADGE_ERROR_FROM_DB;
563 pkgmgrinfo_pkginfo_filter_destroy(filter);
565 if (ret == BADGE_ERROR_NONE)
566 sqlite3_exec(db, "END;", NULL, NULL, NULL);
568 sqlite3_exec(db, "ROLLBACK;", NULL, NULL, NULL);
569 if ((sql_ret = db_util_close(db)) != SQLITE_OK)
570 WARN("fail to db_util_close [%d]", sql_ret);