f806ad64e04e646b2537c4c6eb94bc85d47cc6b4
[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
31 #include <db-util.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
44 #include "rua.h"
45 #include "db-schema.h"
46 #include "perf-measure.h"
47
48 #define RUA_DB_NAME     ".rua.db"
49 #define RUA_HISTORY     "rua_history"
50 #define QUERY_MAXLEN    4096
51 #define Q_LATEST \
52         "select pkg_name from rua_history " \
53         "order by launch_time desc limit 1 "
54
55 static sqlite3 *_db = NULL;
56
57 static int __exec(sqlite3 *db, char *query);
58 static int __create_table(sqlite3 *db);
59 static sqlite3 *__db_init(char *root);
60
61 int rua_clear_history(void)
62 {
63         int r;
64         char query[QUERY_MAXLEN];
65
66         if (_db == NULL)
67                 return -1;
68
69         snprintf(query, QUERY_MAXLEN, "delete from %s;", RUA_HISTORY);
70
71         r = __exec(_db, query);
72
73         return r;
74 }
75
76 int rua_delete_history_with_pkgname(char *pkg_name)
77 {
78         int r;
79         char query[QUERY_MAXLEN];
80
81         if (_db == NULL)
82                 return -1;
83
84         if (pkg_name == NULL)
85                 return -1;
86
87         snprintf(query, QUERY_MAXLEN, "delete from %s where pkg_name = '%s';",
88                 RUA_HISTORY, pkg_name);
89
90         r = __exec(_db, query);
91
92         return r;
93 }
94
95 int rua_delete_history_with_apppath(char *app_path)
96 {
97         int r;
98         char query[QUERY_MAXLEN];
99
100         if (_db == NULL)
101                 return -1;
102
103         if (app_path == NULL)
104                 return -1;
105
106         snprintf(query, QUERY_MAXLEN, "delete from %s where app_path = '%s';",
107                 RUA_HISTORY, app_path);
108
109         r = __exec(_db, query);
110
111         return r;
112 }
113
114 int rua_add_history(struct rua_rec *rec)
115 {
116         int r;
117         int cnt = 0;
118         char query[QUERY_MAXLEN];
119         sqlite3_stmt *stmt;
120
121         unsigned int timestamp;
122         timestamp = PERF_MEASURE_START("RUA");
123
124         if (_db == NULL) {
125                 LOGE("Error db null");
126                 return -1;
127         }
128
129         if (rec == NULL) {
130                 LOGE("Error rec null");
131                 return -1;
132         }
133
134         snprintf(query, QUERY_MAXLEN,
135                 "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
136                 rec->pkg_name);
137
138         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
139         if (r != SQLITE_OK) {
140                 LOGE("Error sqlite3_prepare fail");
141                 return -1;
142         }
143
144         r = sqlite3_step(stmt);
145         if (r == SQLITE_ROW) {
146                 cnt = sqlite3_column_int(stmt, 0);
147         }
148         sqlite3_finalize(stmt);
149
150         if (cnt == 0)
151                 /* insert */
152                 snprintf(query, QUERY_MAXLEN,
153                         "insert into %s ( pkg_name, app_path, arg, launch_time ) "
154                         " values ( \"%s\", \"%s\", \"%s\", %d ) ",
155                         RUA_HISTORY,
156                         rec->pkg_name ? rec->pkg_name : "",
157                         rec->app_path ? rec->app_path : "",
158                         rec->arg ? rec->arg : "", time(NULL));
159         else
160                 /* update */
161                 snprintf(query, QUERY_MAXLEN,
162                         "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
163                         RUA_HISTORY,
164                         rec->arg ? rec->arg : "", time(NULL), rec->pkg_name);
165
166         r = __exec(_db, query);
167         if (r == -1) {
168                 printf("[RUA ADD HISTORY ERROR] %s\n", query);
169                 return -1;
170         }
171
172         PERF_MEASURE_END("RUA", timestamp);
173
174         return r;
175 }
176
177 int rua_history_load_db(char ***table, int *nrows, int *ncols)
178 {
179         int r;
180         char query[QUERY_MAXLEN];
181         char *db_err = NULL;
182         char **db_result = NULL;
183         sqlite3 *db = NULL;
184
185         char defname[FILENAME_MAX];
186         const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
187         snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
188
189         if (table == NULL)
190                 return -1;
191         if (nrows == NULL)
192                 return -1;
193         if (ncols == NULL)
194                 return -1;
195
196         r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
197         if (r) {
198                 db_util_close(db);
199                 return -1;
200         }
201
202         snprintf(query, QUERY_MAXLEN,
203                  "select * from %s order by launch_time desc;", RUA_HISTORY);
204
205         r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);
206
207         if (r == SQLITE_OK)
208                 *table = db_result;
209         else
210                 sqlite3_free_table(db_result);
211
212         if (db)
213                 db_util_close(db);
214
215         return r;
216 }
217
218 int rua_history_unload_db(char ***table)
219 {
220         if (*table) {
221                 sqlite3_free_table(*table);
222                 *table = NULL;
223                 return 0;
224         }
225         return -1;
226 }
227
228 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
229                         int row)
230 {
231         char **db_result = NULL;
232         char *tmp = NULL;
233
234         if (rec == NULL)
235                 return -1;
236         if (table == NULL)
237                 return -1;
238         if (row >= nrows)
239                 return -1;
240
241         db_result = table + ((row + 1) * ncols);
242
243         tmp = db_result[RUA_COL_ID];
244         if (tmp) {
245                 rec->id = atoi(tmp);
246         }
247
248         tmp = db_result[RUA_COL_PKGNAME];
249         if (tmp) {
250                 rec->pkg_name = tmp;
251         }
252
253         tmp = db_result[RUA_COL_APPPATH];
254         if (tmp) {
255                 rec->app_path = tmp;
256         }
257
258         tmp = db_result[RUA_COL_ARG];
259         if (tmp) {
260                 rec->arg = tmp;
261         }
262
263         tmp = db_result[RUA_COL_LAUNCHTIME];
264         if (tmp) {
265                 rec->launch_time = atoi(tmp);
266         }
267
268         return 0;
269 }
270
271 int rua_is_latest_app(const char *pkg_name)
272 {
273         int r = -1;
274         sqlite3_stmt *stmt;
275         const unsigned char *ct;
276         sqlite3 *db;
277
278         char defname[FILENAME_MAX];
279         const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
280         snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
281
282         if (!pkg_name)
283                 return -1;
284
285         r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL);
286         if (r) {
287                 db_util_close(db);
288                 return -1;
289         }
290
291         r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
292         if (r != SQLITE_OK) {
293                 return -1;
294         }
295
296         r = sqlite3_step(stmt);
297         if (r == SQLITE_ROW) {
298                 ct = sqlite3_column_text(stmt, 0);
299                 if (ct == NULL || ct[0] == '\0') {
300                         r = -1;
301                         goto out;
302                 }
303
304                 if (strncmp(pkg_name, ct, strlen(pkg_name)) == 0) {
305                         r = 0;
306                         goto out;
307                 }
308         }
309
310 out:
311         if (stmt)
312                 sqlite3_finalize(stmt);
313         if (db)
314                 db_util_close(db);
315
316         return r;
317 }
318
319 int rua_init(void)
320 {
321         unsigned int timestamp;
322         timestamp = PERF_MEASURE_START("RUA");
323
324         if (_db) {
325                 return 0;
326         }
327
328         char defname[FILENAME_MAX];
329         const char *rua_db_path = tzplatform_getenv(TZ_USER_DB);
330         snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME);
331         _db = __db_init(defname);
332
333         if (_db == NULL)
334                 return -1;
335
336         PERF_MEASURE_END("RUA", timestamp);
337
338         return 0;
339 }
340
341 int rua_fini(void)
342 {
343         unsigned int timestamp;
344         timestamp = PERF_MEASURE_START("RUA");
345
346         if (_db) {
347                 db_util_close(_db);
348                 _db = NULL;
349         }
350
351         PERF_MEASURE_END("RUA", timestamp);
352         return 0;
353 }
354
355 static int __exec(sqlite3 *db, char *query)
356 {
357         int r;
358         char *errmsg = NULL;
359
360         if (db == NULL)
361                 return -1;
362
363         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
364
365         if (r != SQLITE_OK) {
366                 sqlite3_free(errmsg);
367                 return -1;
368         }
369
370         return 0;
371 }
372
373 static int __create_table(sqlite3 *db)
374 {
375         int r;
376
377         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
378         if (r == -1)
379                 return -1;
380
381         return 0;
382 }
383
384 static sqlite3 *__db_init(char *root)
385 {
386         int r;
387         sqlite3 *db = NULL;
388
389         r = db_util_open_with_options(root, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
390         if (r) {
391                 db_util_close(db);
392                 return NULL;
393         }
394
395         r = __create_table(db);
396         if (r) {
397                 db_util_close(db);
398                 return NULL;
399         }
400
401         return db;
402 }