summaryrefslogtreecommitdiff
path: root/src/rua.c
blob: f8bab6ac543e0bd3952214ddffc958cec3634d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <db-util.h>
#include <aul.h>

#include "rua_util.h"
#include "rua_internal.h"
#include "rua.h"
#include "db-schema.h"

int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid)
{
	int r;
	char time_str[32] = {0,};
	bundle *b = NULL;

	if (pkg_name == NULL || app_path == NULL) {
		LOGE("invalid param");
		return -1;
	}

	r = _rua_util_check_uid(uid);
	if (r == -1)
		return r;

	b = bundle_create();
	if (b == NULL) {
		LOGE("bundle_create fail out of memory.");
		return -1;
	}
	snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL));
	bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
	bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
	bundle_add_str(b, AUL_K_RUA_ARG, arg);
	bundle_add_str(b, AUL_K_RUA_TIME, time_str);

	r = aul_add_rua_history_for_uid(b, uid);
	LOGI("rua_add_history_for_uid result : %d ", r);
	bundle_free(b);
	return r;
}

int rua_delete_history_with_pkgname(char *pkg_name)
{
	return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid());
}

int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid)
{
	int r;
	bundle *b;

	r = _rua_util_check_uid(uid);
	if (r == -1)
		return r;

	b = bundle_create();
	if (b == NULL) {
		LOGE("bundle_create fail out of memory.");
		return -1;
	}
	bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name);
	r = aul_delete_rua_history_for_uid(b, uid);
	LOGI("rua_delete_history_with_pkgname result : %d ", r);
	bundle_free(b);
	return r;
}

int rua_delete_history_with_apppath(char *app_path)
{
	return rua_delete_history_with_apppath_for_uid(app_path, getuid());
}

int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid)
{
	int r;
	bundle *b;

	r = _rua_util_check_uid(uid);
	if (r == -1)
		return r;

	b = bundle_create();
	if (b == NULL) {
		LOGE("bundle_create fail out of memory.");
		return -1;
	}
	bundle_add_str(b, AUL_K_RUA_APPPATH, app_path);
	r = aul_delete_rua_history_for_uid(b, uid);
	LOGI("rua_delete_history_with_apppath result : %d ", r);
	bundle_free(b);
	return r;
}

int rua_clear_history(void)
{
	return rua_clear_history_for_uid(getuid());
}

int rua_clear_history_for_uid(uid_t uid)
{
	int r;

	r = _rua_util_check_uid(uid);
	if (r == -1)
		return r;

	r = aul_delete_rua_history_for_uid(NULL, uid);
	LOGI("rua_clear_history result : %d ", r);
	return r;
}

int rua_history_load_db(char ***table, int *nrows, int *ncols)
{
	return rua_history_load_db_for_uid(table, nrows, ncols, getuid());
}

int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid)
{
	int r;
	char query[QUERY_MAXLEN];
	char *db_err = NULL;
	char **db_result = NULL;
	sqlite3 *db = NULL;

	if (table == NULL)
		return -1;
	if (nrows == NULL)
		return -1;
	if (ncols == NULL)
		return -1;

	r = _rua_util_check_uid(uid);
	if (r == -1)
		return r;

	r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
	if (r != SQLITE_OK)
		return -1;

	snprintf(query, sizeof(query),
		 "SELECT pkg_name, app_path, arg, launch_time, instance_id, "
		 "instance_name, icon, uri FROM %s ORDER BY launch_time DESC;",
		 RUA_HISTORY);

	r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err);

	if (r == SQLITE_OK)
		*table = db_result;
	else
		sqlite3_free_table(db_result);

	db_util_close(db);

	return r;
}

int rua_history_unload_db(char ***table)
{
	if (*table) {
		sqlite3_free_table(*table);
		*table = NULL;
		return 0;
	}
	return -1;
}

int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols,
			int row)
{
	char **db_result = NULL;
	char *tmp = NULL;

	if (rec == NULL)
		return -1;
	if (table == NULL)
		return -1;
	if (row >= nrows)
		return -1;

	db_result = table + ((row + 1) * ncols);

	tmp = db_result[RUA_COL_PKGNAME];
	if (tmp)
		rec->pkg_name = tmp;

	LOGI("get rec pkg_name %s", rec->pkg_name);
	tmp = db_result[RUA_COL_APPPATH];
	if (tmp)
		rec->app_path = tmp;

	tmp = db_result[RUA_COL_ARG];
	if (tmp)
		rec->arg = tmp;

	tmp = db_result[RUA_COL_LAUNCHTIME];
	if (tmp)
		rec->launch_time = atoi(tmp);

	tmp = db_result[RUA_COL_INSTANCE_ID];
	if (tmp && tmp[0] != '\0')
		rec->instance_id = tmp;
	else
		rec->instance_id = NULL;

	tmp = db_result[RUA_COL_INSTANCE_NAME];
	if (tmp && tmp[0] != '\0')
		rec->instance_name = tmp;
	else
		rec->instance_name = NULL;

	tmp = db_result[RUA_COL_ICON];
	if (tmp && tmp[0] != '\0')
		rec->icon = tmp;
	else
		rec->icon = NULL;

	tmp = db_result[RUA_COL_URI];
	if (tmp && tmp[0] != '\0')
		rec->uri = tmp;
	else
		rec->uri = NULL;

	return 0;
}

int rua_is_latest_app(const char *pkg_name)
{
	return rua_is_latest_app_for_uid(pkg_name, getuid());
}

int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid)
{
	int r = -1;
	sqlite3_stmt *stmt;
	const unsigned char *ct;
	sqlite3 *db = NULL;
	char *query = "select pkg_name from rua_history order by launch_time desc limit 1;";

	if (!pkg_name)
		return -1;

	r = _rua_util_check_uid(uid);
	if (r == -1)
		return r;

	r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME);
	if (r != SQLITE_OK)
		return -1;

	r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL);
	if (r != SQLITE_OK) {
		db_util_close(db);
		return -1;
	}

	r = sqlite3_step(stmt);
	if (r == SQLITE_ROW) {
		ct = sqlite3_column_text(stmt, 0);
		if (ct == NULL || ct[0] == '\0') {
			r = -1;
			goto out;
		}

		if (strncmp(pkg_name, (const char *)ct, strlen(pkg_name)) == 0) {
			r = 0;
			goto out;
		}
	}

out:
	if (stmt)
		sqlite3_finalize(stmt);
	if (db)
		db_util_close(db);

	return r;
}

int rua_init(void)
{
	return 0;
}

int rua_fini(void)
{
	return 0;
}

int rua_delete_history_with_instance_id(const char *app_id,
		const char *instance_id)
{
	int ret;
	bundle *b;

	if (app_id == NULL) {
		LOGE("Invalid parameter");
		return -1;
	}

	b = bundle_create();
	if (b == NULL) {
		LOGE("Out of memory");
		return -1;
	}

	bundle_add_str(b, AUL_K_RUA_PKGNAME, app_id);
	if (instance_id)
		bundle_add_str(b, AUL_K_RUA_INSTANCE_ID, instance_id);

	ret = aul_delete_rua_history_for_uid(b, getuid());
	bundle_free(b);
	if (ret < 0) {
		LOGE("Failed to delete rua history - result(%d)", ret);
		return -1;
	}

	return 0;
}