2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/types.h>
25 #include "rua_internal.h"
26 #include "db-schema.h"
29 #include "rua_private.h"
31 static int __exec(sqlite3 *db, char *query)
39 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
49 static int __create_table(sqlite3 *db)
53 r = __exec(db, CREATE_RUA_HISTORY_TABLE);
60 static sqlite3 *__db_init(uid_t uid)
65 r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
70 r = __create_table(db);
79 static int __delete_history_with_pkg_name(sqlite3 *db, const char *pkg_name,
80 const char *instance_id)
82 static const char query[] =
83 "DELETE FROM rua_history WHERE pkg_name=? AND instance_id=?";
88 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
90 LOGE("prepare failed: %s", sqlite3_errmsg(db));
95 __BIND_TEXT(db, stmt, idx++, pkg_name);
96 __BIND_TEXT(db, stmt, idx++, instance_id);
98 r = sqlite3_step(stmt);
99 if (r != SQLITE_DONE) {
100 LOGE("step failed: %s", sqlite3_errmsg(db));
101 sqlite3_finalize(stmt);
105 sqlite3_finalize(stmt);
110 static int __delete_history_with_app_path(sqlite3 *db, const char *app_path,
111 const char *instance_id)
113 static const char query[] =
114 "DELETE FROM rua_history WHERE app_path=? AND instance_id=?";
119 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
120 if (r != SQLITE_OK) {
121 LOGE("prepare failed: %s", sqlite3_errmsg(db));
122 sqlite3_close_v2(db);
126 __BIND_TEXT(db, stmt, idx++, app_path);
127 __BIND_TEXT(db, stmt, idx++, instance_id);
129 r = sqlite3_step(stmt);
130 if (r != SQLITE_DONE) {
131 LOGE("step failed: %s", sqlite3_errmsg(db));
132 sqlite3_finalize(stmt);
136 sqlite3_finalize(stmt);
141 API int rua_usr_db_delete_history(bundle *b, uid_t uid)
145 char *pkg_name = NULL;
146 char *app_path = NULL;
147 char *instance_id = NULL;
151 LOGE("Error db null");
156 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
157 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
158 bundle_get_str(b, AUL_K_RUA_INSTANCE_ID, &instance_id);
162 LOGI("rua_delete_history_from_db : %s", pkg_name);
163 r = __delete_history_with_pkg_name(db, pkg_name, instance_id);
164 } else if (app_path) {
165 LOGI("rua_delete_history_from_db : %s", app_path);
166 r = __delete_history_with_app_path(db, app_path, instance_id);
168 LOGE("No data to delete history");
172 sqlite3_close_v2(db);
174 r = rua_dbus_send_update_signal(DELETE);
176 LOGE("[RUA SEND SIGNAL ERROR] \n");
183 API int rua_db_delete_history(bundle *b)
185 return rua_usr_db_delete_history(b, getuid());
188 static int __insert_history(sqlite3 *db, struct rua_rec *rec)
190 static const char query[] =
191 "INSERT OR REPLACE INTO rua_history ("
192 " pkg_name, app_path, arg, launch_time,"
193 " instance_id, instance_name, icon, uri) "
194 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
199 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
200 if (r != SQLITE_OK) {
201 LOGE("prepare failed: %s", sqlite3_errmsg(db));
205 __BIND_TEXT(db, stmt, idx++, rec->pkg_name);
206 __BIND_TEXT(db, stmt, idx++, rec->app_path);
207 __BIND_TEXT(db, stmt, idx++, rec->arg ? rec->arg : "");
208 __BIND_INT(db, stmt, idx++, (int)rec->launch_time);
209 __BIND_TEXT(db, stmt, idx++, rec->instance_id ? rec->instance_id : "");
210 __BIND_TEXT(db, stmt, idx++,
211 rec->instance_name ? rec->instance_name : "");
212 __BIND_TEXT(db, stmt, idx++, rec->icon ? rec->icon : "");
213 __BIND_TEXT(db, stmt, idx++, rec->uri ? rec->uri : "");
215 r = sqlite3_step(stmt);
216 if (r != SQLITE_DONE) {
217 LOGE("step failed: %s", sqlite3_errmsg(db));
218 sqlite3_finalize(stmt);
222 sqlite3_finalize(stmt);
227 static int __update_history(sqlite3 *db, struct rua_rec *rec)
229 static const char query[] =
230 "UPDATE rua_history SET launch_time=? "
231 "WHERE pkg_name=? AND instance_id=?";
236 r = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
237 if (r != SQLITE_OK) {
238 LOGE("prepare failed: %s", sqlite3_errmsg(db));
242 __BIND_INT(db, stmt, idx++, (int)rec->launch_time);
243 __BIND_TEXT(db, stmt, idx++, rec->pkg_name);
244 __BIND_TEXT(db, stmt, idx++, rec->instance_id);
246 r = sqlite3_step(stmt);
247 if (r != SQLITE_DONE) {
248 LOGE("step failed: %s", sqlite3_errmsg(db));
249 sqlite3_finalize(stmt);
253 sqlite3_finalize(stmt);
258 API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
263 if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
264 LOGE("Invalid parameter");
270 LOGE("Error db null");
274 if (rec->instance_id &&
275 (rec->instance_name == NULL || rec->icon == NULL ||
277 r = __update_history(db, rec);
279 r = __insert_history(db, rec);
281 r = rua_dbus_send_update_signal(ADD);
283 LOGE("[RUA SEND SIGNAL ERROR] \n");
284 sqlite3_close_v2(db);
288 sqlite3_close_v2(db);
292 API int rua_db_add_history(struct rua_rec *rec)
294 return rua_usr_db_add_history(rec, getuid());