4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
33 /* For multi-user support */
34 #include <tzplatform_config.h>
44 #include "db-schema.h"
45 #include "perf-measure.h"
47 #define RUA_DB_NAME ".rua.db"
48 #define RUA_HISTORY "rua_history"
49 #define QUERY_MAXLEN 4096
51 "select pkg_name from rua_history " \
52 "order by launch_time desc limit 1 "
54 static int __exec(sqlite3 *db, char *query);
55 static int __create_table(sqlite3 *db);
56 static sqlite3 *__db_init();
58 int rua_delete_history_from_db(bundle *b)
62 char query[QUERY_MAXLEN];
64 char *pkg_name = NULL;
65 char *app_path = NULL;
71 LOGE("Error db null");
76 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
77 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
81 snprintf(query, QUERY_MAXLEN, "delete from rua_history where pkg_name = '%s';", pkg_name);
82 else if (app_path != NULL)
83 snprintf(query, QUERY_MAXLEN, "delete from rua_history where app_path = '%s';", app_path);
85 snprintf(query, QUERY_MAXLEN, "delete from rua_history;");
87 LOGI("rua_delete_history_from_db : %s", query);
88 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
91 LOGE("fail to exec delete query %s : %s", query, errmsg);
103 int rua_clear_history(void)
106 r = aul_delete_rua_history(NULL);
107 LOGI("rua_clear_history result : %d ", r);
111 int rua_delete_history_with_pkgname(char *pkg_name)
114 bundle *b = bundle_create();
116 LOGE("bundle_create fail out of memory.");
120 bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
121 r = aul_delete_rua_history(b);
122 LOGI("rua_delete_history_with_pkgname result : %d ", r);
127 int rua_delete_history_with_apppath(char *app_path)
130 bundle *b = bundle_create();
132 LOGE("bundle_create fail out of memory.");
136 bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
137 r = aul_delete_rua_history(b);
138 LOGI("rua_delete_history_with_apppath result : %d ", r);
144 int rua_add_history(struct rua_rec *rec)
148 char query[QUERY_MAXLEN];
152 unsigned int timestamp;
153 timestamp = PERF_MEASURE_START("RUA");
157 LOGE("Error db null");
162 LOGE("Error rec null");
167 snprintf(query, QUERY_MAXLEN,
168 "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
171 r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
172 if (r != SQLITE_OK) {
173 LOGE("Error sqlite3_prepare fail");
178 r = sqlite3_step(stmt);
180 cnt = sqlite3_column_int(stmt, 0);
182 sqlite3_finalize(stmt);
186 snprintf(query, QUERY_MAXLEN,
187 "insert into %s ( pkg_name, app_path, arg, launch_time ) "
188 " values ( \"%s\", \"%s\", \"%s\", %d ) ",
190 rec->pkg_name ? rec->pkg_name : "",
191 rec->app_path ? rec->app_path : "",
192 rec->arg ? rec->arg : "", time(NULL));
195 snprintf(query, QUERY_MAXLEN,
196 "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
198 rec->arg ? rec->arg : "", time(NULL), rec->pkg_name);
200 r = __exec(db, query);
202 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
207 PERF_MEASURE_END("RUA", timestamp);
212 int rua_history_load_db(char ***table, int *nrows, int *ncols)
215 char query[QUERY_MAXLEN];
217 char **db_result = NULL;
220 char defname[FILENAME_MAX];
221 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
222 if (rua_db_path == NULL) {
223 LOGE("fail to get rua_db_path");
226 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
235 r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
241 snprintf(query, QUERY_MAXLEN,
242 "select * from %s order by launch_time desc;", RUA_HISTORY);
244 r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
249 sqlite3_free_table(db_result);
256 int rua_history_unload_db(char ***table)
259 sqlite3_free_table(*table);
266 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
269 char **db_result = NULL;
279 db_result = table + ((row + 1) * ncols);
281 tmp = db_result[RUA_COL_ID];
286 tmp = db_result[RUA_COL_PKGNAME];
291 tmp = db_result[RUA_COL_APPPATH];
296 tmp = db_result[RUA_COL_ARG];
301 tmp = db_result[RUA_COL_LAUNCHTIME];
303 rec->launch_time = atoi(tmp);
309 int rua_is_latest_app(const char *pkg_name)
313 const unsigned char *ct;
316 char defname[FILENAME_MAX];
317 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
318 if (rua_db_path == NULL) {
319 LOGE("fail to get rua_db_path");
322 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
327 r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
333 r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
334 if (r != SQLITE_OK) {
339 r = sqlite3_step(stmt);
340 if (r == SQLITE_ROW) {
341 ct = sqlite3_column_text(stmt, 0);
342 if (ct == NULL || ct[0] == '\0') {
347 if (strncmp(pkg_name, ct, strlen(pkg_name)) == 0) {
355 sqlite3_finalize(stmt);
372 static int __exec(sqlite3 *db, char *query)
380 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
382 if (r != SQLITE_OK) {
383 sqlite3_free(errmsg);
390 static int __create_table(sqlite3 *db)
394 r = __exec(db, CREATE_RUA_HISTORY_TABLE);
401 static sqlite3 *__db_init()
406 char defname[FILENAME_MAX];
407 const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
408 if (rua_db_path == NULL) {
409 LOGE("fail to get rua_db_path");
412 snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
414 r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
420 r = __create_table(db);