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>
26 #include "rua_internal.h"
27 #include "db-schema.h"
30 #include "rua_private.h"
32 static int __exec(sqlite3 *db, char *query)
40 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
50 static int __create_table(sqlite3 *db)
54 r = __exec(db, CREATE_RUA_HISTORY_TABLE);
61 static sqlite3 *__db_init(uid_t uid)
66 r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
71 r = __create_table(db);
80 API int rua_db_delete_history(bundle *b)
82 return rua_usr_db_delete_history(b, getuid());
85 API int rua_usr_db_delete_history(bundle *b, uid_t uid)
89 char query[QUERY_MAXLEN];
90 char *pkg_name = NULL;
91 char *app_path = NULL;
92 char *instance_id = NULL;
98 LOGE("Error db null");
103 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
104 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
105 bundle_get_str(b, AUL_K_RUA_INSTANCE_ID, &instance_id);
109 snprintf(query, sizeof(query),
110 "DELETE FROM rua_history WHERE pkg_name = '%s' "
111 "AND instance_id = '%s';",
112 pkg_name, instance_id ? instance_id : "");
113 } else if (app_path) {
114 snprintf(query, sizeof(query),
115 "DELETE FROM rua_history WHERE app_path = '%s' "
116 "AND instance_id = '%s';",
117 app_path, instance_id ? instance_id : "");
119 snprintf(query, sizeof(query), "DELETE FROM rua_history;");
122 LOGI("rua_delete_history_from_db : %s", query);
123 r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
125 if (r != SQLITE_OK) {
126 LOGE("fail to exec delete query %s : %s", query, errmsg);
127 sqlite3_free(errmsg);
131 r = rua_dbus_send_update_signal(DELETE);
133 LOGE("[RUA SEND SIGNAL ERROR] \n");
144 API int rua_db_add_history(struct rua_rec *rec)
146 return rua_usr_db_add_history(rec, getuid());
149 API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
152 char query[QUERY_MAXLEN];
155 if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
156 LOGE("Invalid parameter");
162 LOGE("Error db null");
166 if (rec->instance_id &&
167 (rec->instance_name == NULL || rec->icon == NULL ||
169 snprintf(query, sizeof(query),
170 "UPDATE %s SET launch_time = %d "
171 "WHERE pkg_name = %s AND instance_id = %s;",
173 (int)rec->launch_time,
177 snprintf(query, sizeof(query),
178 "INSERT OR REPLACE INTO %s "
179 "(pkg_name, app_path, arg, launch_time, "
180 "instance_id, instance_name, icon, uri) "
181 "VALUES (\"%s\", \"%s\", \"%s\", %d, "
182 "\"%s\", \"%s\", \"%s\", \"%s\");",
186 rec->arg ? rec->arg : "",
187 (int)rec->launch_time,
188 rec->instance_id ? rec->instance_id : "",
189 rec->instance_name ? rec->instance_name : "",
190 rec->icon ? rec->icon : "",
191 rec->uri ? rec->uri : "");
194 r = __exec(db, query);
196 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
201 r = rua_dbus_send_update_signal(ADD);
203 LOGE("[RUA SEND SIGNAL ERROR] \n");