Release version 0.5.11
[platform/core/appfw/librua.git] / src / rua.c
index 7fef47a..527c66a 100644 (file)
--- a/src/rua.c
+++ b/src/rua.c
@@ -19,7 +19,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <db-util.h>
+#include <sqlite3.h>
 #include <aul.h>
 #include <dlog.h>
 
@@ -133,18 +133,19 @@ API int rua_clear_history(void)
 
 API int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
 {
+       static const char query[] = "SELECT "
+               "pkg_name, app_path, arg, launch_time, instance_id, "
+               "instance_name, icon, uri, image, comp_id "
+               "FROM rua_history ORDER BY launch_time DESC";
        int r;
-       char query[QUERY_MAXLEN];
        char *db_err = NULL;
        char **db_result = NULL;
        sqlite3 *db = NULL;
 
-       if (table == NULL)
-               return -1;
-       if (nrows == NULL)
-               return -1;
-       if (ncols == NULL)
+       if (table == NULL || nrows == NULL || ncols == NULL) {
+               LOGE("invalid parameter");
                return -1;
+       }
 
        r = _rua_util_check_uid(uid);
        if (r == -1)
@@ -154,19 +155,17 @@ API int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t
        if (r != SQLITE_OK)
                return -1;
 
-       snprintf(query, sizeof(query),
-                "SELECT pkg_name, app_path, arg, launch_time, instance_id, "
-                "instance_name, icon, uri FROM %s ORDER BY launch_time DESC;",
-                RUA_HISTORY);
-
        r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
+       if (r != SQLITE_OK) {
+               LOGE("get table failed: %s", db_err);
+               sqlite3_free(db_err);
+               sqlite3_close_v2(db);
+               return -1;
+       }
 
-       if (r == SQLITE_OK)
-               *table = db_result;
-       else
-               sqlite3_free_table(db_result);
+       *table = db_result;
 
-       db_util_close(db);
+       sqlite3_close_v2(db);
 
        return r;
 }
@@ -231,10 +230,11 @@ API int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int nc
        db_result = table + ((row + 1) * ncols);
 
        tmp = db_result[RUA_COL_PKGNAME];
-       if (tmp)
+       if (tmp) {
                rec->pkg_name = tmp;
+               LOGI("get rec pkg_name %s", rec->pkg_name);
+       }
 
-       LOGI("get rec pkg_name %s", rec->pkg_name);
        tmp = db_result[RUA_COL_APPPATH];
        if (tmp)
                rec->app_path = tmp;
@@ -247,6 +247,12 @@ API int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int nc
        if (tmp)
                rec->launch_time = atoi(tmp);
 
+       tmp = db_result[RUA_COL_COMP_ID];
+       if (tmp && tmp[0] != '\0')
+               rec->comp_id = tmp;
+       else
+               rec->comp_id = NULL;
+
        tmp = db_result[RUA_COL_INSTANCE_ID];
        if (tmp && tmp[0] != '\0')
                rec->instance_id = tmp;
@@ -271,6 +277,18 @@ API int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int nc
        else
                rec->uri = NULL;
 
+       tmp = db_result[RUA_COL_IMAGE];
+       if (tmp && tmp[0] != '\0')
+               rec->image = tmp;
+       else
+               rec->image = NULL;
+
+       tmp = db_result[RUA_COL_COMP_ID];
+       if (tmp && tmp[0] != '\0')
+               rec->comp_id = tmp;
+       else
+               rec->comp_id = NULL;
+
        return 0;
 }
 
@@ -286,11 +304,13 @@ API int rua_history_unload_db(char ***table)
 
 API int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
 {
+       static const char query[] =
+               "SELECT pkg_name FROM rua_history "
+               "ORDER BY launch_time DESC LIMIT 1";
        int r = -1;
        sqlite3_stmt *stmt;
        const unsigned char *ct;
        sqlite3 *db = NULL;
-       char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";
 
        if (!pkg_name)
                return -1;
@@ -303,31 +323,31 @@ API int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
        if (r != SQLITE_OK)
                return -1;
 
-       r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
+       r = sqlite3_prepare(db, query, strlen(query), &stmt, NULL);
        if (r != SQLITE_OK) {
-               db_util_close(db);
+               sqlite3_close_v2(db);
                return -1;
        }
 
        r = sqlite3_step(stmt);
-       if (r == SQLITE_ROW) {
-               ct = sqlite3_column_text(stmt, 0);
-               if (ct == NULL || ct[0] == '\0') {
-                       r = -1;
-                       goto out;
-               }
-
-               if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
-                       r = 0;
-                       goto out;
-               }
+       if (r != SQLITE_ROW) {
+               if (r != SQLITE_DONE)
+                       LOGE("step failed: %s", sqlite3_errmsg(db));
+               sqlite3_finalize(stmt);
+               sqlite3_close_v2(db);
+               return -1;
        }
 
-out:
-       if (stmt)
-               sqlite3_finalize(stmt);
-       if (db)
-               db_util_close(db);
+       ct = sqlite3_column_text(stmt, 0);
+       if (ct == NULL || ct[0] == '\0')
+               r = -1;
+       else if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0)
+               r = 0;
+       else
+               r = -1;
+
+       sqlite3_finalize(stmt);
+       sqlite3_close_v2(db);
 
        return r;
 }