-static int __bind_params(sqlite3_stmt *stmt, GList *params)
-{
- GList *tmp_list = NULL;
- int idx = 0;
- int ret;
-
- if (stmt == NULL || params == NULL)
- return PMINFO_R_EINVAL;
-
- tmp_list = params;
- while (tmp_list) {
- ret = sqlite3_bind_text(stmt, ++idx, (char *)tmp_list->data, -1, SQLITE_STATIC);
- if (ret != SQLITE_OK)
- return PMINFO_R_ERROR;
- tmp_list = tmp_list->next;
- }
-
- return PMINFO_R_OK;
-}
-
-static bool __check_package_storage_status(pkgmgrinfo_filter_x *tmp_filter)
-{
- GSList *tmp_list = NULL;
- pkgmgrinfo_node_x *tmp_node = NULL;
- int property = -1;
-
- property = _pminfo_pkginfo_convert_to_prop_bool(PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE);
- for (tmp_list = tmp_filter->list; tmp_list != NULL;
- tmp_list = g_slist_next(tmp_list)) {
- tmp_node = (pkgmgrinfo_node_x *)tmp_list->data;
- if (property == tmp_node->prop) {
- if (strcmp(tmp_node->value, "true") == 0)
- return true;
- else
- return false;
- }
- }
- return true;
-}
-
-static int _pkginfo_get_packages(uid_t uid, const char *locale,
- pkgmgrinfo_filter_x *filter, int flag, GHashTable *packages)
-{
- static const char query_raw[] =
- "SELECT DISTINCT pi.package, pi.package_version, "
- "pi.install_location, pi.package_removable, "
- "pi.package_preload, pi.package_readonly, pi.package_update, "
- "pi.package_appsetting, pi.package_system, pi.package_type, "
- "pi.package_size, pi.installed_time, pi.installed_storage, "
- "pi.storeclient_id, pi.mainapp_id, pi.package_url, "
- "pi.root_path, pi.csc_path, pi.package_nodisplay, "
- "pi.package_api_version, pi.package_support_disable, "
- "pi.package_tep_name, pi.package_zip_mount_file, pi.external_path, "
- "pi.package_support_mode";
- static const char query_author[] =
- ", pi.author_name, pi.author_email, pi.author_href";
- static const char query_label[] =
- ", COALESCE("
- "(SELECT package_label FROM package_localized_info WHERE pi.package=package AND package_locale=?), "
- "(SELECT package_label FROM package_localized_info WHERE pi.package=package AND package_locale='No Locale'))";
- static const char query_icon[] =
- ", COALESCE("
- "(SELECT package_icon FROM package_localized_info WHERE pi.package=package AND package_locale=?), "
- "(SELECT package_icon FROM package_localized_info WHERE pi.package=package AND package_locale='No Locale'))";
- static const char query_description[] =
- ", COALESCE("
- "(SELECT package_description FROM package_localized_info WHERE pi.package=package AND package_locale=?), "
- "(SELECT package_description FROM package_localized_info WHERE pi.package=package AND package_locale='No Locale'))";
- static const char query_from_clause[] = " FROM package_info as pi";
- int ret = PMINFO_R_ERROR;
- int idx = 0;
- int query_len = 0;
- char *dbpath;
- char *tmp_record = NULL;
- char *constraints = NULL;
- char query[MAX_QUERY_LEN] = { '\0' };
- package_x *info = NULL;
- author_x *author = NULL;
- GList *bind_params = NULL;
- sqlite3 *db;
- sqlite3_stmt *stmt;
- pkgmgrinfo_filter_x *tmp_filter = NULL;
- bool is_check_storage = true;
-
- dbpath = getUserPkgParserDBPathUID(uid);
- if (dbpath == NULL)
- return PMINFO_R_ERROR;
-
- ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
- if (ret != SQLITE_OK) {
- _LOGD("failed to open db: %d", ret);
- free(dbpath);
- return PMINFO_R_ERROR;
- }
- free(dbpath);
-
- if (filter != NULL) {
- tmp_filter = filter;
- } else {
- ret = pkgmgrinfo_pkginfo_filter_create((void *)&tmp_filter);
- if (ret != PMINFO_R_OK) {
- _LOGE("Failed to create filter");
- return PMINFO_R_ERROR;
- }
- }
-
- is_check_storage = __check_package_storage_status(tmp_filter);
-
- query_len = strlen(query_raw);
- snprintf(query, MAX_QUERY_LEN - 1, "%s", query_raw);
- if (flag & PMINFO_PKGINFO_GET_AUTHOR) {
- strncat(query, query_author, MAX_QUERY_LEN - query_len - 1);
- query_len += strlen(query_author);
- }
- if (flag & PMINFO_PKGINFO_GET_LABEL) {
- strncat(query, query_label, MAX_QUERY_LEN - query_len - 1);
- query_len += strlen(query_label);
- bind_params = g_list_append(bind_params, strdup(locale));
- }
- if (flag & PMINFO_PKGINFO_GET_ICON) {
- strncat(query, query_icon, MAX_QUERY_LEN - query_len - 1);
- query_len += strlen(query_icon);
- bind_params = g_list_append(bind_params, strdup(locale));
- }
- if (flag & PMINFO_PKGINFO_GET_DESCRIPTION) {
- strncat(query, query_description, MAX_QUERY_LEN - query_len - 1);
- query_len += strlen(query_description);
- bind_params = g_list_append(bind_params, strdup(locale));
- }
-
- strncat(query, query_from_clause, MAX_QUERY_LEN - query_len - 1);
- query_len += strlen(query_from_clause);
-
- ret = _get_filtered_query(tmp_filter, locale, uid, &constraints, &bind_params);
- if (ret != PMINFO_R_OK) {
- LOGE("Failed to get WHERE clause");
- goto catch;
- }
-
- if (constraints)
- strncat(query, constraints, MAX_QUERY_LEN - query_len - 1);
-
- ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
- if (ret != SQLITE_OK) {
- LOGE("prepare failed: %s", sqlite3_errmsg(db));
- ret = PMINFO_R_ERROR;
- goto catch;
- }
-
- ret = __bind_params(stmt, bind_params);
- if (ret != SQLITE_OK) {
- LOGE("Failed to bind parameters");
- goto catch;
- }
-
- while (sqlite3_step(stmt) == SQLITE_ROW) {
- info = calloc(1, sizeof(package_x));
- if (info == NULL) {
- LOGE("out of memory");
- ret = PMINFO_R_ERROR;
- goto catch;
- }
- idx = 0;
- _save_column_str(stmt, idx++, &info->package);
- if (g_hash_table_contains(packages,
- (gconstpointer)info->package)) {
- free(info->package);
- free(info);
- info = NULL;
- continue;
- }
- _save_column_str(stmt, idx++, &info->version);
- _save_column_str(stmt, idx++, &info->installlocation);
- _save_column_str(stmt, idx++, &info->removable);
- _save_column_str(stmt, idx++, &info->preload);
- _save_column_str(stmt, idx++, &info->readonly);
- _save_column_str(stmt, idx++, &info->update);
- _save_column_str(stmt, idx++, &info->appsetting);
- _save_column_str(stmt, idx++, &info->system);
- _save_column_str(stmt, idx++, &info->type);
- _save_column_str(stmt, idx++, &info->package_size);
- _save_column_str(stmt, idx++, &info->installed_time);
- _save_column_str(stmt, idx++, &info->installed_storage);
- _save_column_str(stmt, idx++, &info->storeclient_id);
- _save_column_str(stmt, idx++, &info->mainapp_id);
- _save_column_str(stmt, idx++, &info->package_url);
- _save_column_str(stmt, idx++, &info->root_path);
- _save_column_str(stmt, idx++, &info->csc_path);
- _save_column_str(stmt, idx++, &info->nodisplay_setting);
- _save_column_str(stmt, idx++, &info->api_version);
- _save_column_str(stmt, idx++, &info->support_disable);
- _save_column_str(stmt, idx++, &info->tep_name);
- _save_column_str(stmt, idx++, &info->zip_mount_file);
- _save_column_str(stmt, idx++, &info->external_path);
- _save_column_str(stmt, idx++, &info->support_mode);
- info->for_all_users =
- strdup((uid != GLOBAL_USER) ? "false" : "true");
-
- if (flag & PMINFO_PKGINFO_GET_AUTHOR) {
- /* TODO : author should be retrieved at package_localized_info */
- author = calloc(1, sizeof(author_x));
- if (author == NULL) {
- ret = PMINFO_R_ERROR;
- goto catch;
- }
- _save_column_str(stmt, idx++, &author->text);
- _save_column_str(stmt, idx++, &author->email);
- _save_column_str(stmt, idx++, &author->href);
- info->author = g_list_append(info->author, author);
- }
-
- if (flag & PMINFO_PKGINFO_GET_LABEL) {
- tmp_record = NULL;
- _save_column_str(stmt, idx++, &tmp_record);
-
- if (_add_label_info_into_list(locale, tmp_record, &info->label)) {
- ret = PMINFO_R_ERROR;
- goto catch;
- }
- }
-
- if (flag & PMINFO_PKGINFO_GET_ICON) {
- tmp_record = NULL;
- _save_column_str(stmt, idx++, &tmp_record);
- if (_add_icon_info_into_list(locale, tmp_record, &info->icon)) {
- ret = PMINFO_R_ERROR;
- goto catch;
- }
- }
-
- if (flag & PMINFO_PKGINFO_GET_DESCRIPTION) {
- tmp_record = NULL;
- _save_column_str(stmt, idx++, &tmp_record);
- if (_pkginfo_add_description_info_into_list(locale, tmp_record,
- &info->description)) {
- ret = PMINFO_R_ERROR;
- goto catch;
- }
- }
-
- if (flag & PMINFO_PKGINFO_GET_PRIVILEGE) {
- if (_pkginfo_get_privilege(db, info->package,
- &info->privileges)) {
- ret = PMINFO_R_ERROR;
- goto catch;
- }
- }
-
- if (is_check_storage &&
- __pkginfo_check_installed_storage(info) != PMINFO_R_OK) {
- ret = PMINFO_R_ERROR;
- pkgmgrinfo_basic_free_package(info);
- info = NULL;
- continue;
- }
-
- g_hash_table_insert(packages, (gpointer)info->package,
- (gpointer)info);
- }
-
- ret = PMINFO_R_OK;
-
-catch:
- if (constraints)
- free(constraints);
-
- if (ret != PMINFO_R_OK && info != NULL)
- pkgmgrinfo_basic_free_package(info);
-
- if (filter == NULL)
- pkgmgrinfo_pkginfo_filter_destroy(tmp_filter);
-
- g_list_free_full(bind_params, free);
- sqlite3_close_v2(db);
- sqlite3_finalize(stmt);
-
- return ret;
-}
-