Merge "Fix static analysis" into tizen
[platform/core/appfw/librua.git] / src / rua_internal.c
index 4aa2250..0bb4305 100644 (file)
@@ -18,7 +18,6 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <db-util.h>
 #include <aul.h>
 #include <dlog.h>
 #include <sqlite3.h>
@@ -70,28 +69,104 @@ static sqlite3 *__db_init(uid_t uid)
 
        r = __create_table(db);
        if (r) {
-               db_util_close(db);
+               sqlite3_close_v2(db);
                return NULL;
        }
 
        return db;
 }
 
-API int rua_db_delete_history(bundle *b)
+static int __delete_history_with_pkg_name(sqlite3 *db, const char *pkg_name,
+               const char *instance_id)
 {
-       return rua_usr_db_delete_history(b, getuid());
+       static const char query[] =
+               "DELETE FROM rua_history WHERE pkg_name=? AND instance_id=?";
+       sqlite3_stmt *stmt;
+       int r;
+       int idx = 1;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, idx++, pkg_name);
+       __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
+static int __delete_history_with_app_path(sqlite3 *db, const char *app_path,
+               const char *instance_id)
+{
+       static const char query[] =
+               "DELETE FROM rua_history WHERE app_path=? AND instance_id=?";
+       sqlite3_stmt *stmt;
+       int r;
+       int idx = 1;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, idx++, app_path);
+       __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
+static int __clear_history(sqlite3 *db)
+{
+       static const char query[] = "DELETE FROM rua_history";
+       sqlite3_stmt *stmt;
+       int r;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
 }
 
 API int rua_usr_db_delete_history(bundle *b, uid_t uid)
 {
        int r;
-       sqlite3 *db = NULL;
-       char query[QUERY_MAXLEN];
+       sqlite3 *db;
        char *pkg_name = NULL;
        char *app_path = NULL;
        char *instance_id = NULL;
-       char *errmsg = NULL;
-       int result = 0;
 
        db = __db_init(uid);
        if (db == NULL) {
@@ -106,50 +181,114 @@ API int rua_usr_db_delete_history(bundle *b, uid_t uid)
        }
 
        if (pkg_name) {
-               snprintf(query, sizeof(query),
-                               "DELETE FROM rua_history WHERE pkg_name = '%s' "
-                               "AND instance_id = '%s';",
-                               pkg_name, instance_id ? instance_id : "");
+               LOGI("rua_delete_history_from_db : %s", pkg_name);
+               r = __delete_history_with_pkg_name(db, pkg_name, instance_id);
        } else if (app_path) {
-               snprintf(query, sizeof(query),
-                               "DELETE FROM rua_history WHERE app_path = '%s' "
-                               "AND instance_id = '%s';",
-                               app_path, instance_id ? instance_id : "");
+               LOGI("rua_delete_history_from_db : %s", app_path);
+               r = __delete_history_with_app_path(db, app_path, instance_id);
        } else {
-               snprintf(query, sizeof(query), "DELETE FROM rua_history;");
+               LOGI("rua clear history");
+               r = __clear_history(db);
        }
 
-       LOGI("rua_delete_history_from_db : %s", query);
-       r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
+       sqlite3_close_v2(db);
 
-       if (r != SQLITE_OK) {
-               LOGE("fail to exec delete query %s : %s", query, errmsg);
-               sqlite3_free(errmsg);
-               result = -1;
+       if (r == -1) {
+               LOGE("Failed to delete history");
+               return -1;
        }
 
        r = rua_dbus_send_update_signal(DELETE);
        if (r == -1) {
                LOGE("[RUA SEND SIGNAL ERROR] \n");
-               db_util_close(db);
                return -1;
        }
 
-       if (db != NULL)
-               db_util_close(db);
+       return 0;
+}
 
-       return result;
+API int rua_db_delete_history(bundle *b)
+{
+       return rua_usr_db_delete_history(b, getuid());
 }
 
-API int rua_db_add_history(struct rua_rec *rec)
+static int __insert_history(sqlite3 *db, struct rua_rec *rec)
 {
-       return rua_usr_db_add_history(rec, getuid());
+       static const char query[] =
+               "INSERT OR REPLACE INTO rua_history ("
+               "  pkg_name, app_path, arg, launch_time,"
+               "  instance_id, instance_name, icon, uri,"
+               "  image, comp_id) "
+               "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
+       int r;
+       sqlite3_stmt *stmt;
+       int idx = 1;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, idx++, rec->pkg_name);
+       __BIND_TEXT(db, stmt, idx++, rec->app_path);
+       __BIND_TEXT(db, stmt, idx++, rec->arg ? rec->arg : "");
+       __BIND_INT(db, stmt, idx++, (int)rec->launch_time);
+       __BIND_TEXT(db, stmt, idx++, rec->instance_id ? rec->instance_id : "");
+       __BIND_TEXT(db, stmt, idx++,
+                       rec->instance_name ? rec->instance_name : "");
+       __BIND_TEXT(db, stmt, idx++, rec->icon ? rec->icon : "");
+       __BIND_TEXT(db, stmt, idx++, rec->uri ? rec->uri : "");
+       __BIND_TEXT(db, stmt, idx++, rec->image ? rec->image : "");
+       __BIND_TEXT(db, stmt, idx++, rec->comp_id ? rec->comp_id : "");
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
+static int __update_history(sqlite3 *db, struct rua_rec *rec)
+{
+       static const char query[] =
+               "UPDATE rua_history SET launch_time=? "
+               "WHERE pkg_name=? AND comp_id=? AND instance_id=?";
+       int r;
+       sqlite3_stmt *stmt;
+       int idx = 1;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_INT(db, stmt, idx++, (int)rec->launch_time);
+       __BIND_TEXT(db, stmt, idx++, rec->pkg_name);
+       __BIND_TEXT(db, stmt, idx++, rec->comp_id);
+       __BIND_TEXT(db, stmt, idx++, rec->instance_id);
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+
+       sqlite3_finalize(stmt);
+
+       return 0;
 }
 
 API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
 {
        int r;
-       char query[QUERY_MAXLEN];
        sqlite3 *db;
 
        if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
@@ -165,46 +304,101 @@ API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
 
        if (rec->instance_id &&
                        (rec->instance_name == NULL || rec->icon == NULL ||
-                        rec->uri == NULL)) {
-               snprintf(query, sizeof(query),
-                               "UPDATE %s SET launch_time = %d "
-                               "WHERE pkg_name = %s AND instance_id = %s;",
-                               RUA_HISTORY,
-                               (int)rec->launch_time,
-                               rec->pkg_name,
-                               rec->instance_id);
-       } else {
-               snprintf(query, sizeof(query),
-                               "INSERT OR REPLACE INTO %s "
-                               "(pkg_name, app_path, arg, launch_time, "
-                               "instance_id, instance_name, icon, uri) "
-                               "VALUES (\"%s\", \"%s\", \"%s\", %d, "
-                               "\"%s\", \"%s\", \"%s\", \"%s\");",
-                               RUA_HISTORY,
-                               rec->pkg_name,
-                               rec->app_path,
-                               rec->arg ? rec->arg : "",
-                               (int)rec->launch_time,
-                               rec->instance_id ? rec->instance_id : "",
-                               rec->instance_name ? rec->instance_name : "",
-                               rec->icon ? rec->icon : "",
-                               rec->uri ? rec->uri : "");
-       }
-
-       r = __exec(db, query);
+                        rec->uri == NULL))
+               r = __update_history(db, rec);
+       else
+               r = __insert_history(db, rec);
        if (r == -1) {
-               LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
-               db_util_close(db);
+               LOGE("Failed insert or update history");
+               sqlite3_close_v2(db);
                return -1;
        }
 
        r = rua_dbus_send_update_signal(ADD);
        if (r == -1) {
                LOGE("[RUA SEND SIGNAL ERROR] \n");
-               db_util_close(db);
+               sqlite3_close_v2(db);
                return -1;
        }
 
-       db_util_close(db);
+       sqlite3_close_v2(db);
        return r;
 }
+
+API int rua_db_add_history(struct rua_rec *rec)
+{
+       return rua_usr_db_add_history(rec, getuid());
+}
+
+static int __update_image(sqlite3 *db, const char *pkg_name, const char *comp_id,
+               const char *instance_id, const char *image)
+{
+       static const char query[] =
+               "UPDATE rua_history SET image=? "
+               "WHERE pkg_name=? AND comp_id=? AND instance_id=?";
+       sqlite3_stmt *stmt;
+       int idx = 1;
+       int r;
+
+       r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
+       if (r != SQLITE_OK) {
+               LOGE("Prepare failed: %s", sqlite3_errmsg(db));
+               return -1;
+       }
+
+       __BIND_TEXT(db, stmt, idx++, image);
+       __BIND_TEXT(db, stmt, idx++, pkg_name);
+       __BIND_TEXT(db, stmt, idx++, comp_id ? comp_id : "");
+       __BIND_TEXT(db, stmt, idx++, instance_id ? instance_id : "");
+
+       r = sqlite3_step(stmt);
+       if (r != SQLITE_DONE) {
+               LOGE("Step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               return -1;
+       }
+       sqlite3_finalize(stmt);
+
+       return 0;
+}
+
+API int rua_usr_db_update_image(const char *pkg_name, const char *comp_id,
+               const char *instance_id, const char *image, uid_t uid)
+{
+       int r;
+       sqlite3 *db;
+
+       if (pkg_name == NULL || image == NULL) {
+               LOGE("Invalid parameter");
+               return -1;
+       }
+
+       db = __db_init(uid);
+       if (db == NULL) {
+               LOGE("Error db null");
+               return -1;
+       }
+
+       r = __update_image(db, pkg_name, comp_id, instance_id, image);
+       if (r < 0) {
+               LOGE("Failed to update image - appid(%s)", pkg_name);
+               sqlite3_close_v2(db);
+               return -1;
+       }
+       sqlite3_close_v2(db);
+
+       r = rua_dbus_send_update_signal(UPDATE);
+       if (r < 0) {
+               LOGE("[RUA SEND SIGNAL ERROR]");
+               return -1;
+       }
+
+       return r;
+}
+
+API int rua_db_update_image(const char *pkg_name, const char *comp_id,
+               const char *instance_id, const char *image)
+{
+       return rua_usr_db_update_image(
+                       pkg_name, comp_id, instance_id, image, getuid());
+}