Add db busy handler
[platform/core/appfw/pkgmgr-info.git] / src / pkgmgrinfo_appinfo.c
index c48d6d9..6d9bc49 100644 (file)
@@ -52,9 +52,12 @@ static const char join_app_control[] =
 static const char join_metadata[] =
        " LEFT OUTER JOIN package_app_app_metadata"
        "  ON ai.app_id=package_app_app_metadata.app_id ";
+static const char join_appinfo_for_uid[] =
+       " LEFT OUTER JOIN package_app_info_for_uid"
+       " ON ai.app_id=package_app_info_for_uid.app_id ";
 
 static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
-       const char *locale, char **query, GList **bind_params)
+       const char *locale, uid_t uid, char **query, GList **bind_params)
 {
        int joined = 0;
        size_t len = 0;
@@ -75,7 +78,7 @@ static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
        }
 
        for (list = filter->list; list; list = list->next) {
-               joined |= __get_filter_condition(list->data, &condition, bind_params);
+               joined |= __get_filter_condition(list->data, uid, &condition, bind_params);
                if (condition == NULL)
                        continue;
 
@@ -105,6 +108,10 @@ static int _get_filtered_query(pkgmgrinfo_filter_x *filter,
                strncat(tmp_query, join_metadata, MAX_QUERY_LEN - len - 1);
                len += strlen(join_metadata);
        }
+       if (joined & E_PMINFO_APPINFO_JOIN_APPINFO_FOR_UID) {
+               strncat(tmp_query, join_appinfo_for_uid, MAX_QUERY_LEN - len - 1);
+               len += strlen(join_appinfo_for_uid);
+       }
        strncat(tmp_query, buf, MAX_QUERY_LEN - len - 1);
 
        len += strlen(buf);
@@ -397,35 +404,46 @@ static GList *__get_background_category(const char *value)
 
 }
 
-static void __get_splash_screen_display(sqlite3 *db, const char *appid, uid_t uid, char **value)
+static int __get_appinfo_for_uid(sqlite3 *db, application_x *info, uid_t uid)
 {
        static const char query_raw[] =
-               "SELECT is_splash_screen_enabled FROM package_app_info_for_uid "
-               "WHERE app_id='%s' AND uid='%d'";
+               "SELECT is_splash_screen_enabled, is_disabled "
+               "FROM package_app_info_for_uid WHERE app_id='%s' AND uid='%d'";
        int ret;
        char *query;
+       char *is_disabled = NULL;
        sqlite3_stmt *stmt;
 
-       query = sqlite3_mprintf(query_raw, appid, uid);
+       query = sqlite3_mprintf(query_raw, info->appid, uid);
        if (query == NULL) {
                LOGE("out of memory");
-               return;
+               return PMINFO_R_ERROR;
        }
 
        ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
        sqlite3_free(query);
        if (ret != SQLITE_OK) {
                LOGE("sqlite3_prepare_v2() failed: %s", sqlite3_errmsg(db));
-               return;
+               return PMINFO_R_ERROR;
        }
 
        while (sqlite3_step(stmt) == SQLITE_ROW) {
-               if (*value)
-                       free(*value);
-               _save_column_str(stmt, 0, value);
+               if (info->splash_screen_display)
+                       free(info->splash_screen_display);
+               _save_column_str(stmt, 0, &info->splash_screen_display);
+               if (strcasecmp(info->is_disabled, "false") == 0) {
+                       _save_column_str(stmt, 1, &is_disabled);
+                       if (strcasecmp(is_disabled, "true") == 0) {
+                               free(info->is_disabled);
+                               info->is_disabled = is_disabled;
+                       } else {
+                               free(is_disabled);
+                       }
+               }
        }
 
        sqlite3_finalize(stmt);
+       return PMINFO_R_OK;
 }
 
 static void __free_applications(gpointer data)
@@ -551,7 +569,7 @@ static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
        if (dbpath == NULL)
                return PMINFO_R_ERROR;
 
-       ret = sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL);
+       ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
        if (ret != SQLITE_OK) {
                _LOGE("failed to open db: %d", ret);
                free(dbpath);
@@ -574,7 +592,7 @@ static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
 
        is_check_storage = __check_app_storage_status(filter);
 
-       ret = _get_filtered_query(filter, locale, &constraint, &bind_params);
+       ret = _get_filtered_query(filter, locale, uid, &constraint, &bind_params);
        if (ret != PMINFO_R_OK) {
                LOGE("Failed to get WHERE clause");
                goto catch;
@@ -661,11 +679,17 @@ static int _appinfo_get_applications(uid_t db_uid, uid_t uid,
                _save_column_str(stmt, idx++, &info->support_ambient);
 
                info->for_all_users =
-                       strdup((uid != GLOBAL_USER) ? "false" : "true");
-
-               if (db_uid == GLOBAL_USER)
-                       __get_splash_screen_display(db, info->appid, db_uid,
-                                       &info->splash_screen_display);
+                       strdup((db_uid != GLOBAL_USER) ? "false" : "true");
+
+               if (db_uid == GLOBAL_USER) {
+                       ret = __get_appinfo_for_uid(db, info, uid);
+                       if (ret != PMINFO_R_OK) {
+                               LOGI("Failed to get appinfo for given uid[%d]", (int)uid);
+                               pkgmgrinfo_basic_free_application(info);
+                               info = NULL;
+                               continue;
+                       }
+               }
 
                info->background_category = __get_background_category(
                                bg_category_str);
@@ -783,7 +807,7 @@ static int _pkgmgrinfo_get_appinfo(const char *appid, uid_t uid,
        ret = _appinfo_get_applications(uid, uid, locale, filter,
                        PMINFO_APPINFO_GET_ALL, list);
        if (!g_hash_table_size(list) && uid != GLOBAL_USER)
-               ret = _appinfo_get_applications(GLOBAL_USER, GLOBAL_USER, locale, filter,
+               ret = _appinfo_get_applications(GLOBAL_USER, uid, locale, filter,
                                PMINFO_APPINFO_GET_ALL, list);
 
        if (!g_hash_table_size(list)) {
@@ -1296,9 +1320,10 @@ static int _appinfo_get_filtered_foreach_appinfo(uid_t uid,
                return PMINFO_R_ERROR;
        }
 
-       if (__check_disable_filter_exist(filter) == false)
+       if (__check_disable_filter_exist(filter) == false) {
                pkgmgrinfo_appinfo_filter_add_bool(filter,
                                PMINFO_APPINFO_PROP_APP_DISABLE, false);
+       }
 
        ret = _appinfo_get_applications(uid, uid, locale, filter, flag, list);
        if (ret == PMINFO_R_OK && uid != GLOBAL_USER)
@@ -1356,15 +1381,6 @@ API int pkgmgrinfo_appinfo_get_usr_list(pkgmgrinfo_pkginfo_h handle,
                return PMINFO_R_ERROR;
        }
 
-       if (uid == GLOBAL_USER) {
-               if (pkgmgrinfo_appinfo_filter_add_int(filter,
-                                       PMINFO_APPINFO_PROP_APP_DISABLE_FOR_USER,
-                                       (int)getuid())) {
-                       pkgmgrinfo_appinfo_filter_destroy(filter);
-                       return PMINFO_R_ERROR;
-               }
-       }
-
        comp_str = __appcomponent_str(component);
 
        if (comp_str) {
@@ -1412,15 +1428,6 @@ API int pkgmgrinfo_appinfo_get_usr_installed_list_full(
                return PMINFO_R_ERROR;
        }
 
-       if (uid == GLOBAL_USER) {
-               if (pkgmgrinfo_appinfo_filter_add_int(filter,
-                                       PMINFO_APPINFO_PROP_APP_DISABLE_FOR_USER,
-                                       (int)getuid())) {
-                       pkgmgrinfo_appinfo_filter_destroy(filter);
-                       return PMINFO_R_ERROR;
-               }
-       }
-
        ret = _appinfo_get_filtered_foreach_appinfo(uid, filter, flag, app_func,
                        user_data);
 
@@ -1618,7 +1625,7 @@ static char *_get_localed_label(const char *appid, const char *locale, uid_t uid
                goto err;
        }
 
-       if (sqlite3_open_v2(parser_db, &db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
+       if (__open_db(parser_db, &db, SQLITE_OPEN_READONLY) != SQLITE_OK) {
                _LOGE("DB open fail\n");
                free(parser_db);
                goto err;