+/*
+ * pkgmgr-info
+ *
+ * Copyright (c) 2000 - 2019 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Junghyun Yeon <jungh.yeon@samsung.com>, Sangyoon Jang <jeremy.jang@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <sqlite3.h>
+
+#include "pkgmgrinfo_private.h"
+#include "pkgmgrinfo_debug.h"
+#include "pkgmgr-info.h"
+
+static void _free_plugin(gpointer data)
+{
+ plugin_x *plugin = (plugin_x *)data;
+ if (plugin == NULL)
+ return;
+ if (plugin->appid)
+ free((void *)plugin->appid);
+ free((void *)plugin);
+}
+
+API int pkgmgrinfo_plugininfo_foreach_plugininfo(const char *pkgid,
+ const char *plugin_type, const char *plugin_name,
+ pkgmgrinfo_plugin_list_cb plugin_list_cb, void *user_data)
+{
+ int ret;
+ int idx = 0;
+ char *dbpath;
+ const char *appid;
+ sqlite3 *db;
+ sqlite3_stmt *stmt = NULL;
+ plugin_x *plugin;
+ GList *plugin_list = NULL;
+ GList *tmp_list;
+
+ static const char query[] =
+ "SELECT appid FROM "
+ "package_plugin_info WHERE pkgid=? AND "
+ "plugin_type=? AND plugin_name=?";
+
+ if (!pkgid || !plugin_type || !plugin_name || !plugin_list_cb) {
+ _LOGE("Invalid parameter");
+ return PMINFO_R_EINVAL;
+ }
+
+ dbpath = getUserPkgParserDBPathUID(_getuid());
+ if (dbpath == NULL) {
+ _LOGE("Failed to get db path");
+ return PMINFO_R_ERROR;
+ }
+
+ ret = __open_db(dbpath, &db, SQLITE_OPEN_READONLY);
+ if (ret != SQLITE_OK) {
+ _LOGD("failed to open db(%s): %d", dbpath, ret);
+ free(dbpath);
+ return PMINFO_R_ERROR;
+ }
+ free(dbpath);
+
+ 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 = sqlite3_bind_text(stmt, ++idx, pkgid, -1, SQLITE_STATIC);
+ if (ret != SQLITE_OK) {
+ ret = PMINFO_R_ERROR;
+ goto catch;
+ }
+
+ ret = sqlite3_bind_text(stmt, ++idx, plugin_type, -1, SQLITE_STATIC);
+ if (ret != SQLITE_OK) {
+ ret = PMINFO_R_ERROR;
+ goto catch;
+ }
+ ret = sqlite3_bind_text(stmt, ++idx, plugin_name, -1, SQLITE_STATIC);
+ if (ret != SQLITE_OK) {
+ ret = PMINFO_R_ERROR;
+ goto catch;
+ }
+
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ plugin = calloc(1, sizeof(plugin_x));
+ if (plugin == NULL) {
+ _LOGE("out of memory");
+ ret = PMINFO_R_ERROR;
+ goto catch;
+ }
+ idx = 0;
+ appid = (const char *)sqlite3_column_text(stmt, idx++);
+ if (appid) {
+ plugin->appid = strdup(appid);
+ }
+ plugin_list = g_list_append(plugin_list, plugin);
+ }
+
+ for (tmp_list = plugin_list; tmp_list != NULL; tmp_list = tmp_list->next) {
+ plugin = (plugin_x *)tmp_list->data;
+ if (!plugin)
+ continue;
+ ret = plugin_list_cb(pkgid, plugin->appid, plugin_type,
+ plugin_name, user_data);
+ if (ret != 0)
+ break;
+ }
+ g_list_free_full(plugin_list, _free_plugin);
+
+catch:
+ sqlite3_finalize(stmt);
+ sqlite3_close_v2(db);
+
+ return ret;
+
+}
\ No newline at end of file