Resolve remove history APIs permission problem
[platform/core/appfw/librua.git] / src / rua.c
1 /*
2  *  RUA
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 /*
23  * @file    rua.c
24  * @version 0.1
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <db-util.h>
31 #include <aul.h>
32
33 /* For multi-user support */
34 #include <tzplatform_config.h>
35 #include <dlog.h>
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #endif
40
41 #define LOG_TAG "RUA"
42
43 #include "rua.h"
44 #include "db-schema.h"
45 #include "perf-measure.h"
46
47 #define RUA_DB_NAME     ".rua.db"
48 #define RUA_HISTORY     "rua_history"
49 #define QUERY_MAXLEN    4096
50 #define Q_LATEST \
51         "select pkg_name from rua_history " \
52         "order by launch_time desc limit 1 "
53
54 static int __exec(sqlite3 *db, char *query);
55 static int __create_table(sqlite3 *db);
56 static sqlite3 *__db_init();
57
58 int rua_delete_history_from_db(bundle *b)
59 {
60         int r;
61         sqlite3 *db = NULL;
62         char query[QUERY_MAXLEN];
63
64         char *pkg_name = NULL;
65         char *app_path = NULL;
66         char *errmsg = NULL;
67         int result = 0;
68
69         db = __db_init();
70         if (db == NULL) {
71                 LOGE("Error db null");
72                 return -1;
73         }
74
75         if (b != NULL) {
76                 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
77                 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
78         }
79
80         if (pkg_name != NULL)
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);
84         else
85                 snprintf(query, QUERY_MAXLEN, "delete from rua_history;");
86
87         LOGI("rua_delete_history_from_db : %s", query);
88         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
89
90         if (r != SQLITE_OK) {
91                 LOGE("fail to exec delete query %s : %s", query, errmsg);
92                 sqlite3_free(errmsg);
93                 result = -1;
94         }
95
96         if (db != NULL)
97                 db_util_close(db);
98
99         return result;
100
101 }
102
103 int rua_clear_history(void)
104 {
105         int r;
106         r = aul_delete_rua_history(NULL);
107         LOGI("rua_clear_history result : %d ", r);
108         return r;
109 }
110
111 int rua_delete_history_with_pkgname(char *pkg_name)
112 {
113         int r;
114         bundle *b = bundle_create();
115         if (b == NULL) {
116                 LOGE("bundle_create fail out of memory.");
117                 return -1;
118         }
119
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);
123         bundle_free(b);
124         return r;
125 }
126
127 int rua_delete_history_with_apppath(char *app_path)
128 {
129         int r;
130         bundle *b = bundle_create();
131         if (b == NULL) {
132                 LOGE("bundle_create fail out of memory.");
133                 return -1;
134         }
135
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);
139         bundle_free(b);
140
141         return r;
142 }
143
144 int rua_add_history(struct rua_rec *rec)
145 {
146         int r;
147         int cnt = 0;
148         char query[QUERY_MAXLEN];
149         sqlite3_stmt *stmt;
150         sqlite3 *db = NULL;
151
152         unsigned int timestamp;
153         timestamp = PERF_MEASURE_START("RUA");
154
155         db = __db_init();
156         if (db == NULL) {
157                 LOGE("Error db null");
158                 return -1;
159         }
160
161         if (rec == NULL) {
162                 LOGE("Error rec null");
163                 db_util_close(db);
164                 return -1;
165         }
166
167         snprintf(query, QUERY_MAXLEN,
168                 "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
169                 rec->pkg_name);
170
171         r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
172         if (r != SQLITE_OK) {
173                 LOGE("Error sqlite3_prepare fail");
174                 db_util_close(db);
175                 return -1;
176         }
177
178         r = sqlite3_step(stmt);
179         if (r == SQLITE_ROW)
180                 cnt = sqlite3_column_int(stmt, 0);
181
182         sqlite3_finalize(stmt);
183
184         if (cnt == 0)
185                 /* insert */
186                 snprintf(query, QUERY_MAXLEN,
187                         "insert into %s ( pkg_name, app_path, arg, launch_time ) "
188                         " values ( \"%s\", \"%s\", \"%s\", %d ) ",
189                         RUA_HISTORY,
190                         rec->pkg_name ? rec->pkg_name : "",
191                         rec->app_path ? rec->app_path : "",
192                         rec->arg ? rec->arg : "", time(NULL));
193         else
194                 /* update */
195                 snprintf(query, QUERY_MAXLEN,
196                         "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
197                         RUA_HISTORY,
198                         rec->arg ? rec->arg : "", time(NULL), rec->pkg_name);
199
200         r = __exec(db, query);
201         if (r == -1) {
202                 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
203                 db_util_close(db);
204                 return -1;
205         }
206
207         PERF_MEASURE_END("RUA", timestamp);
208         db_util_close(db);
209         return r;
210 }
211
212 int rua_history_load_db(char ***table, int *nrows, int *ncols)
213 {
214         int r;
215         char query[QUERY_MAXLEN];
216         char *db_err = NULL;
217         char **db_result = NULL;
218         sqlite3 *db = NULL;
219
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");
224                 return -1;
225         }
226         snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
227
228         if (table == NULL)
229                 return -1;
230         if (nrows == NULL)
231                 return -1;
232         if (ncols == NULL)
233                 return -1;
234
235         r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
236         if (r) {
237                 db_util_close(db);
238                 return -1;
239         }
240
241         snprintf(query, QUERY_MAXLEN,
242                  "select * from %s order by launch_time desc;", RUA_HISTORY);
243
244         r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
245
246         if (r == SQLITE_OK)
247                 *table = db_result;
248         else
249                 sqlite3_free_table(db_result);
250
251         db_util_close(db);
252
253         return r;
254 }
255
256 int rua_history_unload_db(char ***table)
257 {
258         if (*table) {
259                 sqlite3_free_table(*table);
260                 *table = NULL;
261                 return 0;
262         }
263         return -1;
264 }
265
266 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
267                         int row)
268 {
269         char **db_result = NULL;
270         char *tmp = NULL;
271
272         if (rec == NULL)
273                 return -1;
274         if (table == NULL)
275                 return -1;
276         if (row >= nrows)
277                 return -1;
278
279         db_result = table + ((row + 1) * ncols);
280
281         tmp = db_result[RUA_COL_ID];
282         if (tmp) {
283                 rec->id = atoi(tmp);
284         }
285
286         tmp = db_result[RUA_COL_PKGNAME];
287         if (tmp) {
288                 rec->pkg_name = tmp;
289         }
290
291         tmp = db_result[RUA_COL_APPPATH];
292         if (tmp) {
293                 rec->app_path = tmp;
294         }
295
296         tmp = db_result[RUA_COL_ARG];
297         if (tmp) {
298                 rec->arg = tmp;
299         }
300
301         tmp = db_result[RUA_COL_LAUNCHTIME];
302         if (tmp) {
303                 rec->launch_time = atoi(tmp);
304         }
305
306         return 0;
307 }
308
309 int rua_is_latest_app(const char *pkg_name)
310 {
311         int r = -1;
312         sqlite3_stmt *stmt;
313         const unsigned char *ct;
314         sqlite3 *db;
315
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");
320                 return -1;
321         }
322         snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
323
324         if (!pkg_name)
325                 return -1;
326
327         r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
328         if (r) {
329                 db_util_close(db);
330                 return -1;
331         }
332
333         r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
334         if (r != SQLITE_OK) {
335                 db_util_close(db);
336                 return -1;
337         }
338
339         r = sqlite3_step(stmt);
340         if (r == SQLITE_ROW) {
341                 ct = sqlite3_column_text(stmt, 0);
342                 if (ct == NULL || ct[0] == '\0') {
343                         r = -1;
344                         goto out;
345                 }
346
347                 if (strncmp(pkg_name, ct, strlen(pkg_name)) == 0) {
348                         r = 0;
349                         goto out;
350                 }
351         }
352
353 out:
354         if (stmt)
355                 sqlite3_finalize(stmt);
356         if (db)
357                 db_util_close(db);
358
359         return r;
360 }
361
362 int rua_init(void)
363 {
364         return 0;
365 }
366
367 int rua_fini(void)
368 {
369         return 0;
370 }
371
372 static int __exec(sqlite3 *db, char *query)
373 {
374         int r;
375         char *errmsg = NULL;
376
377         if (db == NULL)
378                 return -1;
379
380         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
381
382         if (r != SQLITE_OK) {
383                 sqlite3_free(errmsg);
384                 return -1;
385         }
386
387         return 0;
388 }
389
390 static int __create_table(sqlite3 *db)
391 {
392         int r;
393
394         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
395         if (r == -1)
396                 return -1;
397
398         return 0;
399 }
400
401 static sqlite3 *__db_init()
402 {
403         int r;
404         sqlite3 *db = NULL;
405
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");
410                 return NULL;
411         }
412         snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
413
414         r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
415         if (r) {
416                 db_util_close(db);
417                 return NULL;
418         }
419
420         r = __create_table(db);
421         if (r) {
422                 db_util_close(db);
423                 return NULL;
424         }
425
426         return db;
427 }