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