Merge "Fix static analysis" into tizen
[platform/core/appfw/librua.git] / src / rua_internal.c
index 6438983..0bb4305 100644 (file)
  * limitations under the License.
  */
 
-#include <tzplatform_config.h>
-#include <db-util.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+
 #include <aul.h>
+#include <dlog.h>
+#include <sqlite3.h>
 
 #include "rua_internal.h"
 #include "db-schema.h"
 #include "rua_util.h"
+#include "rua_dbus.h"
+#include "rua_private.h"
 
 static int __exec(sqlite3 *db, char *query)
 {
@@ -51,35 +57,118 @@ static int __create_table(sqlite3 *db)
        return 0;
 }
 
-static sqlite3 *__db_init()
+static sqlite3 *__db_init(uid_t uid)
 {
        int r;
        sqlite3 *db = NULL;
 
-       r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, getuid(), RUA_DB_NAME);
+       r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
+                       uid, RUA_DB_NAME);
        if (r != SQLITE_OK)
                return NULL;
 
        r = __create_table(db);
        if (r) {
-               db_util_close(db);
+               sqlite3_close_v2(db);
                return NULL;
        }
 
        return db;
 }
 
-int rua_db_delete_history(bundle *b)
+static int __delete_history_with_pkg_name(sqlite3 *db, const char *pkg_name,
+               const char *instance_id)
 {
+       static const char query[] =
+               "DELETE FROM rua_history WHERE pkg_name=? AND instance_id=?";
+       sqlite3_stmt *stmt;
        int r;
-       sqlite3 *db = NULL;
-       char query[QUERY_MAXLEN];
+       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;
        char *pkg_name = NULL;
        char *app_path = NULL;
-       char *errmsg = NULL;
-       int result = 0;
+       char *instance_id = NULL;
 
-       db = __db_init();
+       db = __db_init(uid);
        if (db == NULL) {
                LOGE("Error db null");
                return -1;
@@ -88,63 +177,228 @@ int rua_db_delete_history(bundle *b)
        if (b != NULL) {
                bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
                bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
+               bundle_get_str(b, AUL_K_RUA_INSTANCE_ID, &instance_id);
        }
 
-       if (pkg_name != NULL)
-               snprintf(query, QUERY_MAXLEN, "delete from rua_history where pkg_name = '%s';", pkg_name);
-       else if (app_path != NULL)
-               snprintf(query, QUERY_MAXLEN, "delete from rua_history where app_path = '%s';", app_path);
-       else
-               snprintf(query, QUERY_MAXLEN, "delete from rua_history;");
+       if (pkg_name) {
+               LOGI("rua_delete_history_from_db : %s", pkg_name);
+               r = __delete_history_with_pkg_name(db, pkg_name, instance_id);
+       } else if (app_path) {
+               LOGI("rua_delete_history_from_db : %s", app_path);
+               r = __delete_history_with_app_path(db, app_path, instance_id);
+       } else {
+               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 == -1) {
+               LOGE("Failed to delete history");
+               return -1;
+       }
 
+       r = rua_dbus_send_update_signal(DELETE);
+       if (r == -1) {
+               LOGE("[RUA SEND SIGNAL ERROR] \n");
+               return -1;
+       }
+
+       return 0;
+}
+
+API int rua_db_delete_history(bundle *b)
+{
+       return rua_usr_db_delete_history(b, getuid());
+}
+
+static int __insert_history(sqlite3 *db, struct rua_rec *rec)
+{
+       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("fail to exec delete query %s : %s", query, errmsg);
-               sqlite3_free(errmsg);
-               result = -1;
+               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;
        }
 
-       if (db != NULL)
-               db_util_close(db);
+       sqlite3_finalize(stmt);
 
-       return result;
+       return 0;
 }
 
-int rua_db_add_history(struct rua_rec *rec)
+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;
-       char query[QUERY_MAXLEN];
-       sqlite3 *db = NULL;
+       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;
+       sqlite3 *db;
 
-       db = __db_init();
+       if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
+               LOGE("Invalid parameter");
+               return -1;
+       }
+
+       db = __db_init(uid);
        if (db == NULL) {
                LOGE("Error db null");
                return -1;
        }
 
-       if (rec == NULL) {
-               LOGE("Error rec null");
-               db_util_close(db);
+       if (rec->instance_id &&
+                       (rec->instance_name == NULL || rec->icon == NULL ||
+                        rec->uri == NULL))
+               r = __update_history(db, rec);
+       else
+               r = __insert_history(db, rec);
+       if (r == -1) {
+               LOGE("Failed insert or update history");
+               sqlite3_close_v2(db);
                return -1;
        }
 
-       snprintf(query, QUERY_MAXLEN,
-               "insert or replace into %s ( pkg_name, app_path, arg, launch_time) "
-               " values ( \"%s\", \"%s\", \"%s\", %d) ",
-               RUA_HISTORY,
-               rec->pkg_name ? rec->pkg_name : "",
-               rec->app_path ? rec->app_path : "",
-               rec->arg ? rec->arg : "", (int)rec->launch_time);
-
-       r = __exec(db, query);
+       r = rua_dbus_send_update_signal(ADD);
        if (r == -1) {
-               LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
-               db_util_close(db);
+               LOGE("[RUA SEND SIGNAL ERROR] \n");
+               sqlite3_close_v2(db);
+               return -1;
+       }
+
+       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;
        }
 
-       db_util_close(db);
        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());
+}