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