Add new api for subscribing db update callback
[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 <db-util.h>
18 #include <aul.h>
19
20 #include "rua_internal.h"
21 #include "db-schema.h"
22 #include "rua_util.h"
23 #include "rua_dbus.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(uid_t uid)
55 {
56         int r;
57         sqlite3 *db = NULL;
58
59         r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
60                         uid, RUA_DB_NAME);
61         if (r != SQLITE_OK)
62                 return NULL;
63
64         r = __create_table(db);
65         if (r) {
66                 db_util_close(db);
67                 return NULL;
68         }
69
70         return db;
71 }
72
73 int rua_db_delete_history(bundle *b)
74 {
75         return rua_usr_db_delete_history(b, getuid());
76 }
77
78 int rua_usr_db_delete_history(bundle *b, uid_t uid)
79 {
80         int r;
81         sqlite3 *db = NULL;
82         char query[QUERY_MAXLEN];
83         char *pkg_name = NULL;
84         char *app_path = NULL;
85         char *instance_id = NULL;
86         char *errmsg = NULL;
87         int result = 0;
88
89         db = __db_init(uid);
90         if (db == NULL) {
91                 LOGE("Error db null");
92                 return -1;
93         }
94
95         if (b != NULL) {
96                 bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name);
97                 bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path);
98                 bundle_get_str(b, AUL_K_RUA_INSTANCE_ID, &instance_id);
99         }
100
101         if (pkg_name) {
102                 snprintf(query, sizeof(query),
103                                 "DELETE FROM rua_history WHERE pkg_name = '%s' "
104                                 "AND instance_id = '%s';",
105                                 pkg_name, instance_id ? instance_id : "");
106         } else if (app_path) {
107                 snprintf(query, sizeof(query),
108                                 "DELETE FROM rua_history WHERE app_path = '%s' "
109                                 "AND instance_id = '%s';",
110                                 app_path, instance_id ? instance_id : "");
111         } else {
112                 snprintf(query, sizeof(query), "DELETE FROM rua_history;");
113         }
114
115         LOGI("rua_delete_history_from_db : %s", query);
116         r = sqlite3_exec(db, query, NULL, NULL, &errmsg);
117
118         if (r != SQLITE_OK) {
119                 LOGE("fail to exec delete query %s : %s", query, errmsg);
120                 sqlite3_free(errmsg);
121                 result = -1;
122         }
123
124         r = rua_dbus_send_update_signal(DELETE);
125         if (r == -1) {
126                 LOGE("[RUA SEND SIGNAL ERROR] \n");
127                 db_util_close(db);
128                 return -1;
129         }
130
131         if (db != NULL)
132                 db_util_close(db);
133
134         return result;
135 }
136
137 int rua_db_add_history(struct rua_rec *rec)
138 {
139         return rua_usr_db_add_history(rec, getuid());
140 }
141
142 int rua_usr_db_add_history(struct rua_rec *rec, uid_t uid)
143 {
144         int r;
145         char query[QUERY_MAXLEN];
146         sqlite3 *db;
147
148         if (rec == NULL || rec->pkg_name == NULL || rec->app_path == NULL) {
149                 LOGE("Invalid parameter");
150                 return -1;
151         }
152
153         db = __db_init(uid);
154         if (db == NULL) {
155                 LOGE("Error db null");
156                 return -1;
157         }
158
159         if (rec->instance_id &&
160                         (rec->instance_name == NULL || rec->icon == NULL ||
161                          rec->uri == NULL)) {
162                 snprintf(query, sizeof(query),
163                                 "UPDATE %s SET launch_time = %d "
164                                 "WHERE pkg_name = %s AND instance_id = %s;",
165                                 RUA_HISTORY,
166                                 (int)rec->launch_time,
167                                 rec->pkg_name,
168                                 rec->instance_id);
169         } else {
170                 snprintf(query, sizeof(query),
171                                 "INSERT OR REPLACE INTO %s "
172                                 "(pkg_name, app_path, arg, launch_time, "
173                                 "instance_id, instance_name, icon, uri) "
174                                 "VALUES (\"%s\", \"%s\", \"%s\", %d, "
175                                 "\"%s\", \"%s\", \"%s\", \"%s\");",
176                                 RUA_HISTORY,
177                                 rec->pkg_name,
178                                 rec->app_path,
179                                 rec->arg ? rec->arg : "",
180                                 (int)rec->launch_time,
181                                 rec->instance_id ? rec->instance_id : "",
182                                 rec->instance_name ? rec->instance_name : "",
183                                 rec->icon ? rec->icon : "",
184                                 rec->uri ? rec->uri : "");
185         }
186
187         r = __exec(db, query);
188         if (r == -1) {
189                 LOGE("[RUA ADD HISTORY ERROR] %s\n", query);
190                 db_util_close(db);
191                 return -1;
192         }
193
194         r = rua_dbus_send_update_signal(ADD);
195         if (r == -1) {
196                 LOGE("[RUA SEND SIGNAL ERROR] \n");
197                 db_util_close(db);
198                 return -1;
199         }
200
201         db_util_close(db);
202         return r;
203 }