upload tizen1.0 source
[framework/appfw/librua.git] / 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  * @author  Noha Park (noha.park@samsung.com)
25  * @version 0.1
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <db-util.h>
33
34 #include "rua.h"
35 #include "db-schema.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         if (_db == NULL)
112                 return -1;
113
114         if (rec == NULL)
115                 return -1;
116
117         snprintf(query, QUERY_MAXLEN,
118                 "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY,
119                 rec->pkg_name);
120
121         r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL);
122         if (r != SQLITE_OK) {
123                 return -1;
124         }
125
126         r = sqlite3_step(stmt);
127         if (r == SQLITE_ROW) {
128                 cnt = sqlite3_column_int(stmt, 0);
129         }
130         sqlite3_finalize(stmt);
131
132         if (cnt == 0)
133                 /* insert */
134                 snprintf(query, QUERY_MAXLEN,
135                         "insert into %s ( pkg_name, app_path, arg, launch_time ) "
136                         " values ( \"%s\", \"%s\", \"%s\", %d ) ",
137                         RUA_HISTORY,
138                         rec->pkg_name ? rec->pkg_name : "",
139                         rec->app_path ? rec->app_path : "",
140                         rec->arg ? rec->arg : "", time(NULL));
141         else
142                 /* update */
143                 snprintf(query, QUERY_MAXLEN,
144                         "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';",
145                         RUA_HISTORY,
146                         rec->arg ? rec->arg : "", time(NULL), rec->pkg_name);
147
148         r = __exec(_db, query);
149         if (r == -1) {
150                 printf("[RUA ADD HISTORY ERROR] %s\n", query);
151                 return -1;
152         }
153
154         return r;
155 }
156
157 int rua_history_load_db(char ***table, int *nrows, int *ncols)
158 {
159         int r;
160         char query[QUERY_MAXLEN];
161         char *db_err = NULL;
162         char **db_result = NULL;
163
164         if (table == NULL)
165                 return -1;
166         if (nrows == NULL)
167                 return -1;
168         if (ncols == NULL)
169                 return -1;
170
171         snprintf(query, QUERY_MAXLEN,
172                  "select * from %s order by launch_time desc;", RUA_HISTORY);
173
174         r = sqlite3_get_table(_db, query, &db_result, nrows, ncols, &db_err);
175
176         if (r == SQLITE_OK)
177                 *table = db_result;
178         else
179                 sqlite3_free_table(db_result);
180
181         return r;
182 }
183
184 int rua_history_unload_db(char ***table)
185 {
186         if (*table) {
187                 sqlite3_free_table(*table);
188                 *table = NULL;
189                 return 0;
190         }
191         return -1;
192 }
193
194 int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
195                         int row)
196 {
197         char **db_result = NULL;
198         char *tmp = NULL;
199
200         if (rec == NULL)
201                 return -1;
202         if (table == NULL)
203                 return -1;
204         if (row >= nrows)
205                 return -1;
206
207         db_result = table + ((row + 1) * ncols);
208
209         tmp = db_result[RUA_COL_ID];
210         if (tmp) {
211                 rec->id = atoi(tmp);
212         }
213
214         tmp = db_result[RUA_COL_PKGNAME];
215         if (tmp) {
216                 rec->pkg_name = tmp;
217         }
218
219         tmp = db_result[RUA_COL_APPPATH];
220         if (tmp) {
221                 rec->app_path = tmp;
222         }
223
224         tmp = db_result[RUA_COL_ARG];
225         if (tmp) {
226                 rec->arg = tmp;
227         }
228
229         tmp = db_result[RUA_COL_LAUNCHTIME];
230         if (tmp) {
231                 rec->launch_time = atoi(tmp);
232         }
233
234         return 0;
235 }
236
237 int rua_is_latest_app(const char *pkg_name)
238 {
239         int r;
240         sqlite3_stmt *stmt;
241         const unsigned char *ct;
242
243         if (!pkg_name || !_db)
244                 return -1;
245
246         r = sqlite3_prepare(_db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL);
247         if (r != SQLITE_OK) {
248                 return -1;
249         }
250
251         r = sqlite3_step(stmt);
252         if (r == SQLITE_ROW) {
253                 ct = sqlite3_column_text(stmt, 0);
254                 if (ct == NULL || ct[0] == '\0') {
255                         sqlite3_finalize(stmt);
256                         return -1;
257                 }
258
259                 if (strncmp(pkg_name, ct, strlen(pkg_name)) == 0) {
260                         sqlite3_finalize(stmt);
261                         return 0;
262                 }
263         }
264
265         sqlite3_finalize(stmt);
266         return -1;
267 }
268
269 int rua_init(void)
270 {
271         if (_db) {
272                 return 0;
273         }
274
275         char defname[FILENAME_MAX];
276         snprintf(defname, sizeof(defname), "%s/%s", RUA_DB_PATH, RUA_DB_NAME);
277         _db = __db_init(defname);
278
279         if (_db == NULL)
280                 return -1;
281
282         return 0;
283 }
284
285 int rua_fini(void)
286 {
287         if (_db) {
288                 db_util_close(_db);
289                 _db = NULL;
290         }
291
292         return 0;
293 }
294
295 static int __exec(sqlite3 *db, char *query)
296 {
297         int r;
298         char *errmsg = NULL;
299
300         if (db == NULL)
301                 return -1;
302
303         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
304
305         if (r != SQLITE_OK) {
306                 sqlite3_free(errmsg);
307                 return -1;
308         }
309
310         return 0;
311 }
312
313 static int __create_table(sqlite3 *db)
314 {
315         int r;
316
317         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
318         if (r == -1)
319                 return -1;
320
321         return 0;
322 }
323
324 static sqlite3 *__db_init(char *root)
325 {
326         int r;
327         sqlite3 *db = NULL;
328
329         r = db_util_open(root, &db, 0);
330         if (r) {
331                 db_util_close(db);
332                 return NULL;
333         }
334
335         r = __create_table(db);
336         if (r) {
337                 db_util_close(db);
338                 return NULL;
339         }
340
341         return db;
342 }