Add multi-user feature
[platform/core/appfw/librua.git] / src / rua_internal.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <tzplatform_config.h>
18 #include <db-util.h>
19 #include <aul.h>
20
21 #include "rua_internal.h"
22 #include "db-schema.h"
23 #include "rua_util.h"
24
25 static int __exec(sqlite3 *db, char *query)
26 {
27         int r;
28         char *errmsg = NULL;
29
30         if (db == NULL)
31                 return -1;
32
33         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
34
35         if (r != SQLITE_OK) {
36                 sqlite3_free(errmsg);
37                 return -1;
38         }
39
40         return 0;
41 }
42
43 static int __create_table(sqlite3 *db)
44 {
45         int r;
46
47         r = __exec(db, CREATE_RUA_HISTORY_TABLE);
48         if (r == -1)
49                 return -1;
50
51         return 0;
52 }
53
54 static sqlite3 *__db_init()
55 {
56         int r;
57         sqlite3 *db = NULL;
58
59         r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, getuid(), RUA_DB_NAME);
60         if (r != SQLITE_OK)
61                 return NULL;
62
63         r = __create_table(db);
64         if (r) {
65                 db_util_close(db);
66                 return NULL;
67         }
68
69         return db;
70 }
71
72 int rua_db_delete_history(bundle *b)
73 {
74         int r;
75         sqlite3 *db = NULL;
76         char query[QUERY_MAXLEN];
77         char *pkg_name = NULL;
78         char *app_path = NULL;
79         char *errmsg = NULL;
80         int result = 0;
81
82         db = __db_init();
83         if (db == NULL) {
84                 LOGE("Error db null");
85                 return -1;
86         }
87
88         if (b != NULL) {
89                 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
90                 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
91         }
92
93         if (pkg_name != NULL)
94                 snprintf(query, QUERY_MAXLEN, "delete from rua_history where pkg_name = '%s';", pkg_name);
95         else if (app_path != NULL)
96                 snprintf(query, QUERY_MAXLEN, "delete from rua_history where app_path = '%s';", app_path);
97         else
98                 snprintf(query, QUERY_MAXLEN, "delete from rua_history;");
99
100         LOGI("rua_delete_history_from_db : %s", query);
101         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
102
103         if (r != SQLITE_OK) {
104                 LOGE("fail to exec delete query %s : %s", query, errmsg);
105                 sqlite3_free(errmsg);
106                 result = -1;
107         }
108
109         if (db != NULL)
110                 db_util_close(db);
111
112         return result;
113 }
114
115 int rua_db_add_history(struct rua_rec *rec)
116 {
117         int r;
118         char query[QUERY_MAXLEN];
119         sqlite3 *db = NULL;
120
121         db = __db_init();
122         if (db == NULL) {
123                 LOGE("Error db null");
124                 return -1;
125         }
126
127         if (rec == NULL) {
128                 LOGE("Error rec null");
129                 db_util_close(db);
130                 return -1;
131         }
132
133         snprintf(query, QUERY_MAXLEN,
134                 "insert or replace into %s ( pkg_name, app_path, arg, launch_time) "
135                 " values ( \"%s\", \"%s\", \"%s\", %d) ",
136                 RUA_HISTORY,
137                 rec->pkg_name ? rec->pkg_name : "",
138                 rec->app_path ? rec->app_path : "",
139                 rec->arg ? rec->arg : "", (int)rec->launch_time);
140
141         r = __exec(db, query);
142         if (r == -1) {
143                 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
144                 db_util_close(db);
145                 return -1;
146         }
147
148         db_util_close(db);
149         return r;
150 }