Clean up repository
[platform/core/appfw/librua.git] / src / rua_internal.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #include <db-util.h>
22 #include <aul.h>
23 #include <dlog.h>
24 #include <sqlite3.h>
25
26 #include "rua_internal.h"
27 #include "db-schema.h"
28 #include "rua_util.h"
29 #include "rua_dbus.h"
30 #include "rua_private.h"
31
32 static int __exec(sqlite3 *db, char *query)
33 {
34         int r;
35         char *errmsg = NULL;
36
37         if (db == NULL)
38                 return -1;
39
40         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
41
42         if (r != SQLITE_OK) {
43                 sqlite3_free(errmsg);
44                 return -1;
45         }
46
47         return 0;
48 }
49
50 static int __create_table(sqlite3 *db)
51 {
52         int r;
53
54         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
55         if (r == -1)
56                 return -1;
57
58         return 0;
59 }
60
61 static sqlite3 *__db_init(uid_t uid)
62 {
63         int r;
64         sqlite3 *db = NULL;
65
66         r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
67                         uid, RUA_DB_NAME);
68         if (r != SQLITE_OK)
69                 return NULL;
70
71         r = __create_table(db);
72         if (r) {
73                 db_util_close(db);
74                 return NULL;
75         }
76
77         return db;
78 }
79
80 API int rua_db_delete_history(bundle *b)
81 {
82         return rua_usr_db_delete_history(b, getuid());
83 }
84
85 API int rua_usr_db_delete_history(bundle *b, uid_t uid)
86 {
87         int r;
88         sqlite3 *db = NULL;
89         char query[QUERY_MAXLEN];
90         char *pkg_name = NULL;
91         char *app_path = NULL;
92         char *instance_id = NULL;
93         char *errmsg = NULL;
94         int result = 0;
95
96         db = __db_init(uid);
97         if (db == NULL) {
98                 LOGE("Error db null");
99                 return -1;
100         }
101
102         if (b != 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);
106         }
107
108         if (pkg_name) {
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 : "");
118         } else {
119                 snprintf(query, sizeof(query), "DELETE FROM rua_history;");
120         }
121
122         LOGI("rua_delete_history_from_db : %s", query);
123         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
124
125         if (r != SQLITE_OK) {
126                 LOGE("fail to exec delete query %s : %s", query, errmsg);
127                 sqlite3_free(errmsg);
128                 result = -1;
129         }
130
131         r = rua_dbus_send_update_signal(DELETE);
132         if (r == -1) {
133                 LOGE("[RUA SEND SIGNAL ERROR] \n");
134                 db_util_close(db);
135                 return -1;
136         }
137
138         if (db != NULL)
139                 db_util_close(db);
140
141         return result;
142 }
143
144 API int rua_db_add_history(struct rua_rec *rec)
145 {
146         return rua_usr_db_add_history(rec, getuid());
147 }
148
149 API int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
150 {
151         int r;
152         char query[QUERY_MAXLEN];
153         sqlite3 *db;
154
155         if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
156                 LOGE("Invalid parameter");
157                 return -1;
158         }
159
160         db = __db_init(uid);
161         if (db == NULL) {
162                 LOGE("Error db null");
163                 return -1;
164         }
165
166         if (rec->instance_id &&
167                         (rec->instance_name == NULL || rec->icon == NULL ||
168                          rec->uri == NULL)) {
169                 snprintf(query, sizeof(query),
170                                 "UPDATE %s SET launch_time = %d "
171                                 "WHERE pkg_name = %s AND instance_id = %s;",
172                                 RUA_HISTORY,
173                                 (int)rec->launch_time,
174                                 rec->pkg_name,
175                                 rec->instance_id);
176         } else {
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\");",
183                                 RUA_HISTORY,
184                                 rec->pkg_name,
185                                 rec->app_path,
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 : "");
192         }
193
194         r = __exec(db, query);
195         if (r == -1) {
196                 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
197                 db_util_close(db);
198                 return -1;
199         }
200
201         r = rua_dbus_send_update_signal(ADD);
202         if (r == -1) {
203                 LOGE("[RUA SEND SIGNAL ERROR] \n");
204                 db_util_close(db);
205                 return -1;
206         }
207
208         db_util_close(db);
209         return r;
210 }