Implement remote appcontrol feature
[platform/core/appfw/pkgmgr-info.git] / parser / src / pkgmgr_parser_db.c
index 0163fae..a5f992d 100644 (file)
@@ -186,19 +186,36 @@ static const char *__get_cert_db_path(void)
        return tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_cert.db");
 }
 
+#define DB_VERSION_PATH SYSCONFDIR "/package-manager/pkg_db_version.txt"
 static int __set_db_version(sqlite3 *db)
 {
-       static const char query_raw[] = "PRAGMA user_version=";
-       char query[BUFSIZE];
        int ret;
+       FILE *fp = NULL;
+       char version[PKG_STRING_LEN_MAX] = { 0 };
+       char *query = NULL;
 
-       snprintf(query, sizeof(query), "%s%d", query_raw, PKG_DB_VERSION);
+       fp = fopen(DB_VERSION_PATH, "r");
+       retvm_if(fp == NULL, -1, "Failed to open db version file");
+       if (fgets(version, sizeof(version), fp) == NULL) {
+               _LOGE("Failed to get version information");
+               fclose(fp);
+               return -1;
+       }
+       fclose(fp);
+
+       query = sqlite3_mprintf("PRAGMA user_version=%Q", version);
+       if (!query) {
+               _LOGE("Out of memory");
+               return -1;
+       }
 
        ret = sqlite3_exec(db, query, NULL, NULL, NULL);
        if (ret != SQLITE_OK) {
                _LOGE("exec failed: %s", sqlite3_errmsg(db));
+               sqlite3_free(query);
                return -1;
        }
+       sqlite3_free(query);
 
        return 0;
 }
@@ -480,7 +497,7 @@ static int __open_db(uid_t uid, const char *path, sqlite3 **db, int flags)
        if (flags & SQLITE_OPEN_CREATE) {
                ret = __initialize_db(*db, path, uid);
                if (ret) {
-                       _LOGE("failed to initialize db: %s\n");
+                       _LOGE("failed to initialize db: %s", path);
                        sqlite3_close_v2(*db);
                        return -1;
                }
@@ -609,8 +626,9 @@ static int __insert_appcontrol_privilege_info(sqlite3 *db, const char *appid,
 static int __insert_appcontrol_info(sqlite3 *db, application_x *app)
 {
        static const char query[] =
-               "INSERT INTO package_app_app_control (app_id, app_control) "
-               "VALUES (?, ?)";
+               "INSERT INTO package_app_app_control (app_id, app_control,"
+               "  visibility) "
+               "VALUES (?, ?, ?)";
        int ret;
        sqlite3_stmt *stmt;
        int idx;
@@ -641,6 +659,7 @@ static int __insert_appcontrol_info(sqlite3 *db, application_x *app)
                                        ac->mime : "NULL") : "NULL");
                __BIND_TEXT(db, stmt, idx++, app->appid);
                __BIND_TEXT(db, stmt, idx++, app_control);
+               __BIND_TEXT(db, stmt, idx++, ac->visibility);
 
                ret = sqlite3_step(stmt);
                if (ret != SQLITE_DONE) {
@@ -988,6 +1007,9 @@ static GList *__find_splashscreens(GList *splashscreens)
        GList *list = NULL;
        splashscreen_x *ss;
 
+       if (splashscreens == NULL)
+               return NULL;
+
        g_list_foreach(splashscreens,
                        __find_appcontrol_splashscreen_with_dpi, &list);
        g_list_foreach(splashscreens,
@@ -1003,7 +1025,8 @@ static GList *__find_splashscreens(GList *splashscreens)
        return list;
 }
 
-static int __insert_splashscreen_info(sqlite3 *db, application_x *app)
+static int __insert_splashscreen_info(sqlite3 *db, application_x *app,
+               GList *ss_list)
 {
        static const char query[] =
                "INSERT INTO package_app_splash_screen (app_id, src, type,"
@@ -1014,12 +1037,10 @@ static int __insert_splashscreen_info(sqlite3 *db, application_x *app)
        int idx;
        GList *tmp;
        splashscreen_x *ss;
-       GList *ss_list;
 
        if (app->splashscreens == NULL)
                return 0;
 
-       ss_list = __find_splashscreens(app->splashscreens);
        if (ss_list == NULL)
                return 0;
 
@@ -1627,7 +1648,7 @@ static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
                "  app_effective_appid, app_splash_screen_display,"
                "  app_package_system, app_removable,"
                "  app_package_installed_time, app_support_ambient,"
-               "  app_setup_appid) "
+               "  app_external_path, app_setup_appid) "
                "VALUES (?, ?, "
                "  ?, LOWER(?), ?, LOWER(?), LOWER(?),"
                "  LOWER(?), LOWER(?), ?,"
@@ -1643,7 +1664,7 @@ static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
                "  ?, LOWER(?),"
                "  LOWER(?), LOWER(?),"
                "  ?, LOWER(?),"
-               "  ?)";
+               "  ?, ?)";
        int ret;
        sqlite3_stmt *stmt;
        int idx;
@@ -1651,6 +1672,7 @@ static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
        application_x *app;
        int bg_category;
        const char *effective_appid;
+       GList *ss_list;
 
        ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
        if (ret != SQLITE_OK) {
@@ -1718,6 +1740,7 @@ static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
                __BIND_TEXT(db, stmt, idx++, mfx->installed_time);
                __BIND_TEXT(db, stmt, idx++,
                                __get_bool(app->support_ambient, false));
+               __BIND_TEXT(db, stmt, idx++, mfx->external_path);
                __BIND_TEXT(db, stmt, idx++, app->setup_appid);
 
                ret = sqlite3_step(stmt);
@@ -1745,10 +1768,13 @@ static int __insert_application_info(sqlite3 *db, manifest_x *mfx)
                        sqlite3_finalize(stmt);
                        return -1;
                }
-               if (__insert_splashscreen_info(db, app)) {
+               ss_list = __find_splashscreens(app->splashscreens);
+               if (__insert_splashscreen_info(db, app, ss_list)) {
+                       g_list_free(ss_list);
                        sqlite3_finalize(stmt);
                        return -1;
                }
+               g_list_free(ss_list);
                if (__insert_app_localized_info(db, app)) {
                        sqlite3_finalize(stmt);
                        return -1;
@@ -2473,6 +2499,72 @@ API int pkgmgr_parser_update_app_label_info_in_db(const char *appid,
                        label);
 }
 
+static int __set_app_icon(sqlite3 *db, const char *appid, const char *icon_path)
+{
+       static const char query[] =
+               "UPDATE package_app_localized_info SET app_icon=? "
+               "WHERE app_id=? AND app_icon IS NOT NULL";
+       int ret;
+       sqlite3_stmt *stmt;
+       int idx = 1;
+
+       ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               _LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, idx++, icon_path);
+       __BIND_TEXT(db, stmt, idx++, appid);
+
+       ret = sqlite3_step(stmt);
+       if (ret != SQLITE_DONE) {
+               _LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
+API int pkgmgr_parser_update_app_icon_info_in_usr_db(const char *appid,
+               uid_t uid, const char *icon_path)
+{
+       int ret;
+       const char *dbpath;
+       sqlite3 *db;
+
+       if (appid == NULL) {
+               _LOGE("invalid parameter");
+               return PM_PARSER_R_EINVAL;
+       }
+
+       dbpath = __get_parser_db_path(uid);
+
+       ret = __open_db(uid, dbpath, &db, SQLITE_OPEN_READWRITE);
+       if (ret != SQLITE_OK) {
+               _LOGE("open db failed: %d", ret);
+               return PM_PARSER_R_ERROR;
+       }
+
+       __BEGIN_TRANSACTION(db);
+       __DO_TRANSACTION(db, __set_app_icon(db, appid, icon_path));
+       __END_TRANSACTION(db);
+
+       sqlite3_close_v2(db);
+
+       return PM_PARSER_R_OK;
+}
+
+API int pkgmgr_parser_update_app_icon_info_in_db(const char *appid,
+               const char *icon_path)
+{
+       return pkgmgr_parser_update_app_icon_info_in_usr_db(appid, __getuid(),
+                       icon_path);
+}
+
 static int __set_tep_path(sqlite3 *db, const char *pkgid, const char *tep_path)
 {
        static const char query[] =