Add function for remove badge data when pkg is uninstalled 39/122939/2
authorseungha.son <seungha.son@samsung.com>
Tue, 4 Apr 2017 04:42:13 +0000 (13:42 +0900)
committerseungha.son <seungha.son@samsung.com>
Tue, 4 Apr 2017 10:49:33 +0000 (19:49 +0900)
Signed-off-by: seungha.son <seungha.son@samsung.com>
Change-Id: I9556b2fa13807bdb66ccb207204e65201a71dc7b

include/badge_db.h
include/badge_internal.h
src/badge_db.c
src/badge_internal.c

index b8ad800..00b22f1 100755 (executable)
@@ -35,6 +35,7 @@ extern "C" {
 
 int badge_db_insert(const char *pkgname, const char *writable_pkg, const char *caller, uid_t uid);
 int badge_db_delete(const char *pkgname, const char *caller_pkg, uid_t uid);
+int badge_db_delete_by_pkgname(const char *pkgname, uid_t uid);
 int badge_db_set_count(const char *pkgname, const char *caller_pkg, unsigned int count, uid_t uid);
 int badge_db_get_count(const char *pkgname, unsigned int *count, uid_t uid);
 int badge_db_set_display_option(const char *pkgname, unsigned int is_display, uid_t uid);
index 87905b6..c453960 100755 (executable)
@@ -143,6 +143,8 @@ int _badge_insert(badge_h *badge, uid_t uid);
 
 int _badge_remove(const char *caller, const char *pkgname, uid_t uid);
 
+int _badge_remove_by_pkgname(const char *pkgname, uid_t uid);
+
 int _badge_set_count(const char *caller, const char *pkgname,
                        unsigned int count, uid_t uid);
 
index 5056a8c..73c0675 100755 (executable)
@@ -147,6 +147,12 @@ int badge_db_delete(const char *pkgname, const char *caller, uid_t uid)
 }
 
 EXPORT_API
+int badge_db_delete_by_pkgname(const char *pkgname, uid_t uid)
+{
+       return _badge_remove_by_pkgname(pkgname, uid);
+}
+
+EXPORT_API
 int badge_db_set_count(const char *pkgname, const char *caller, unsigned int count, uid_t uid)
 {
        return _badge_set_count(caller, pkgname, count, uid);
index b4e541f..dc5058f 100755 (executable)
@@ -634,6 +634,174 @@ return_close_db:
        return result;
 }
 
+static int _badge_remove_by_appid(const char *appid, uid_t uid, sqlite3 *db)
+{
+       int ret = BADGE_ERROR_NONE;
+       int result = BADGE_ERROR_NONE;
+       char *sqlbuf = NULL;
+
+       ret = _badge_check_data_inserted(appid, db, uid);
+       if (ret != BADGE_ERROR_ALREADY_EXIST) {
+               result = ret;
+               goto return_close_db;
+       }
+
+       sqlbuf = sqlite3_mprintf("DELETE FROM %q WHERE pkgname = %Q AND uid = %d",
+                        BADGE_TABLE_NAME, appid, uid);
+       if (!sqlbuf) {
+               /* LCOV_EXCL_START */
+               ERR("fail to alloc query");
+               result = BADGE_ERROR_OUT_OF_MEMORY;
+               goto return_close_db;
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = badge_db_exec(db, sqlbuf, NULL);
+       if (ret != BADGE_ERROR_NONE) {
+               ERR("failed to remove badge[%s], err[%d]",
+                               appid, ret);
+               result = ret;
+               goto return_close_db;
+       }
+
+       /* treating option table */
+       ret = _badge_check_option_inserted(appid, db, uid);
+       if (ret != BADGE_ERROR_ALREADY_EXIST) {
+               result = ret;
+               goto return_close_db;
+       }
+
+       sqlbuf = sqlite3_mprintf("DELETE FROM %q WHERE pkgname = %Q AND uid = %d",
+                       BADGE_OPTION_TABLE_NAME, appid, uid);
+       if (!sqlbuf) {
+               /* LCOV_EXCL_START */
+               ERR("fail to alloc query");
+               result = BADGE_ERROR_OUT_OF_MEMORY;
+               goto return_close_db;
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = badge_db_exec(db, sqlbuf, NULL);
+       if (ret != BADGE_ERROR_NONE) {
+               /* LCOV_EXCL_START */
+               ERR("failed to remove badge option[%s], err[%d]",
+                               appid, ret);
+               result = ret;
+               goto return_close_db;
+               /* LCOV_EXCL_STOP */
+       }
+
+return_close_db:
+       if (sqlbuf)
+               sqlite3_free(sqlbuf);
+
+       return result;
+}
+
+static bool _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
+{
+       bool ret = false;
+       int sLen = 0;
+       char *pTemp;
+
+       if (table == NULL || buf == NULL || index < 0) {
+               /* LCOV_EXCL_START */
+               ERR("table[%p], buf[%p], index[%d]", table, buf, index);
+               return false;
+               /* LCOV_EXCL_STOP */
+       }
+
+       pTemp = table[index];
+       if (pTemp == NULL) {
+               *buf = NULL; /* LCOV_EXCL_LINE */
+       } else {
+               sLen = strlen(pTemp);
+               if (sLen) {
+                       *buf = (char *)malloc(sLen + 1);
+                       if (*buf == NULL) {
+                               ERR("malloc is failed"); /* LCOV_EXCL_LINE */
+                               goto out;
+                       }
+                       memset(*buf, 0, sLen + 1);
+                       strncpy(*buf, pTemp, sLen);
+               } else {
+                       *buf = NULL; /* LCOV_EXCL_LINE */
+               }
+       }
+
+       ret = true;
+
+out:
+       return ret;
+}
+
+int _badge_remove_by_pkgname(const char *pkgname, uid_t uid)
+{
+       int ret = BADGE_ERROR_NONE;
+       int sql_ret;
+       int row_count = 0;
+       int col_count = 0;
+       int col_index = 0;
+       int index;
+       char *sql_query = NULL;
+       char **query_result = NULL;
+       char *appid = NULL;
+       sqlite3 *db = NULL;
+
+       sql_ret = db_util_open(BADGE_DB_PATH, &db, 0);
+       if (sql_ret != SQLITE_OK || db == NULL) {
+               ERR("Failed db util open [%s][%d]", BADGE_DB_PATH, sql_ret);
+               return BADGE_ERROR_FROM_DB;
+       }
+
+       sql_query = sqlite3_mprintf("SELECT appid FROM %s WHERE pkgname = %Q" \
+                               "AND (uid = %d OR uid = %d) ORDER BY uid DESC;",
+                               BADGE_SETTING_DB_TABLE, pkgname, uid,
+                               tzplatform_getuid(TZ_SYS_GLOBALAPP_USER));
+       if (!sql_query) {
+               ERR("fail to alloc query");
+               ret = BADGE_ERROR_FROM_DB;
+               goto out;
+       }
+
+       sql_ret = sqlite3_get_table(db, sql_query, &query_result, &row_count, &col_count, NULL);
+       if (sql_ret != SQLITE_OK && sql_ret != -1) {
+               ERR("sqlite3_get_table failed [%d][%s]", sql_ret, sql_query);
+               ret = BADGE_ERROR_FROM_DB;
+               goto out;
+       }
+
+       if (!row_count) {
+               DBG("No setting found for [%s]", pkgname);
+               ret = BADGE_ERROR_NOT_EXIST;
+               goto out;
+       }
+
+       col_index = col_count;
+
+       for (index = 0; index < row_count; index++) {
+               _get_table_field_data_string(query_result, &appid, 1, col_index++);
+               if (appid) {
+                       _badge_remove_by_appid(appid, uid, db);
+                       free(appid);
+                       appid = NULL;
+               }
+       }
+
+out:
+       if (query_result)
+               sqlite3_free_table(query_result);
+       if (sql_query)
+               sqlite3_free(sql_query);
+       if (db) {
+               sql_ret = db_util_close(db);
+               if (sql_ret != SQLITE_OK)
+                       WARN("fail to db_util_close");
+       }
+
+       return ret;
+}
+
 int _badge_set_count(const char *caller, const char *pkgname,
                        unsigned int count, uid_t uid)
 {