summaryrefslogtreecommitdiff
path: root/parser/widget_plugin_parser_db.c
blob: 2173b649c4cde55d424e72eb3fe7f7c4d041f84e (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright 2015  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * 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 <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <linux/limits.h>

#include <glib.h>
#include <sqlite3.h>

#include <dlog.h>
#include <tzplatform_config.h>
#include <pkgmgr_installer_info.h>

#include "widget_plugin_parser_internal.h"

static int target_uid_initialized;
static uid_t target_uid;

static uid_t __get_target_uid(void)
{
	int ret;

	if (target_uid_initialized)
		return target_uid;

	ret = pkgmgr_installer_info_get_target_uid(&target_uid);
	if (ret < 0)
		LOGE("Failed to get target uid - %d", ret);

	return target_uid;
}

static int _bind_text(sqlite3_stmt *stmt, int idx, const char *text)
{
	if (text)
		return sqlite3_bind_text(stmt, idx, text, -1, SQLITE_STATIC);
	else
		return sqlite3_bind_null(stmt, idx);
}

static const char *_get_root_path(const char *pkgid)
{
	const char *path;
	uid_t uid = __get_target_uid();

	if (uid == 0) {
		path = tzplatform_mkpath(TZ_SYS_RO_APP, pkgid);
	} else if (uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)) {
		path = tzplatform_mkpath(TZ_SYS_RW_APP, pkgid);
	} else {
		tzplatform_set_user(uid);
		path = tzplatform_mkpath(TZ_USER_APP, pkgid);
		tzplatform_reset_user();
	}

	return path;
}

static int _insert_support_size(sqlite3 *db, const char *pkgid,
		const char *classid, GList *sizes)
{
	int ret;
	static const char query[] =
		"INSERT INTO support_size "
		"(classid, preview, frame, width, height) "
		"VALUES (?, ?, ?, ?, ?)";
	GList *tmp;
	struct support_size *size;
	sqlite3_stmt *stmt = NULL;
	int idx;
	char buf[PATH_MAX];

	for (tmp = sizes; tmp; tmp = tmp->next) {
		size = (struct support_size *)tmp->data;
		ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
		if (ret != SQLITE_OK) {
			LOGE("prepare error: %s", sqlite3_errmsg(db));
			return -1;
		}

		idx = 1;
		_bind_text(stmt, idx++, classid);
		/* adjust preview image path */
		if (size->preview != NULL) {
			if (size->preview[0] == '/')
				snprintf(buf, sizeof(buf), "%s", size->preview);
			else
				snprintf(buf, sizeof(buf), "%s/shared/res/%s",
						_get_root_path(pkgid),
						size->preview);
			_bind_text(stmt, idx++, buf);
		} else {
			_bind_text(stmt, idx++, NULL);
		}
		sqlite3_bind_int(stmt, idx++, size->frame);
		sqlite3_bind_int(stmt, idx++, size->width);
		sqlite3_bind_int(stmt, idx++, size->height);

		ret = sqlite3_step(stmt);
		if (ret != SQLITE_DONE) {
			LOGE("step error: %s", sqlite3_errmsg(db));
			sqlite3_finalize(stmt);
			return -1;
		}

		sqlite3_reset(stmt);
		sqlite3_clear_bindings(stmt);
	}

	if (stmt)
		sqlite3_finalize(stmt);

	return 0;
}

static int _insert_label(sqlite3 *db, const char *classid, GList *labels)
{
	int ret;
	static const char query[] =
		"INSERT INTO label (classid, locale, label) "
		"VALUES (?, ?, ?)";
	GList *tmp;
	struct label *label;
	sqlite3_stmt *stmt = NULL;
	int idx;

	for (tmp = labels; tmp; tmp = tmp->next) {
		label = (struct label *)tmp->data;
		ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
		if (ret != SQLITE_OK) {
			LOGE("prepare error: %s", sqlite3_errmsg(db));
			return -1;
		}

		idx = 1;
		_bind_text(stmt, idx++, classid);
		_bind_text(stmt, idx++, label->lang);
		_bind_text(stmt, idx++, label->label);

		ret = sqlite3_step(stmt);
		if (ret != SQLITE_DONE) {
			LOGE("step error: %s", sqlite3_errmsg(db));
			sqlite3_finalize(stmt);
			return -1;
		}

		sqlite3_reset(stmt);
		sqlite3_clear_bindings(stmt);
	}

	if (stmt)
		sqlite3_finalize(stmt);

	return 0;
}

static int _insert_icon(sqlite3 *db, const char *pkgid,
		const char *classid, GList *icons)
{
	int ret;
	static const char query[] =
		"INSERT INTO icon (classid, locale, icon) "
		"VALUES (?, ?, ?)";
	GList *tmp;
	struct icon *icon;
	sqlite3_stmt *stmt = NULL;
	int idx;
	char buf[PATH_MAX];

	for (tmp = icons; tmp; tmp = tmp->next) {
		icon = (struct icon *)tmp->data;
		ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
		if (ret != SQLITE_OK) {
			LOGE("prepare error: %s", sqlite3_errmsg(db));
			return -1;
		}

		idx = 1;
		_bind_text(stmt, idx++, classid);
		_bind_text(stmt, idx++, icon->lang);
		/* adjust icon path */
		if (icon->icon[0] == '/')
			snprintf(buf, sizeof(buf), "%s", icon->icon);
		else
			snprintf(buf, sizeof(buf), "%s/shared/res/%s",
					_get_root_path(pkgid), icon->icon);
		_bind_text(stmt, idx++, buf);

		ret = sqlite3_step(stmt);
		if (ret != SQLITE_DONE) {
			LOGE("step error: %s", sqlite3_errmsg(db));
			sqlite3_finalize(stmt);
			return -1;
		}

		sqlite3_reset(stmt);
		sqlite3_clear_bindings(stmt);
	}

	if (stmt)
		sqlite3_finalize(stmt);

	return 0;
}

static int _insert_widget_class(sqlite3 *db, const char *pkgid, GList *wcs)
{
	int ret;
	static const char query[] =
		"INSERT INTO widget_class (classid, update_period, "
		"setup_appid, appid, pkgid, nodisplay, max_instance) "
		"VALUES (?, ?, ?, ?, ?, ?, ?)";
	GList *tmp;
	struct widget_class *wc;
	sqlite3_stmt *stmt = NULL;
	int idx;

	for (tmp = wcs; tmp; tmp = tmp->next) {
		wc = (struct widget_class *)tmp->data;
		ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
		if (ret != SQLITE_OK) {
			LOGE("prepare error: %s", sqlite3_errmsg(db));
			return -1;
		}

		idx = 1;
		_bind_text(stmt, idx++, wc->classid);
		sqlite3_bind_int(stmt, idx++, wc->update_period);
		_bind_text(stmt, idx++, wc->setup_appid);
		_bind_text(stmt, idx++, wc->appid);
		_bind_text(stmt, idx++, pkgid);
		sqlite3_bind_int(stmt, idx++, wc->nodisplay);
		sqlite3_bind_int(stmt, idx++, wc->max_instance);

		ret = sqlite3_step(stmt);
		if (ret != SQLITE_DONE) {
			LOGE("step error: %s", sqlite3_errmsg(db));
			break;
		}

		sqlite3_reset(stmt);
		sqlite3_clear_bindings(stmt);

		if (_insert_support_size(db, pkgid, wc->classid,
					wc->support_size))
			return -1;
		if (_insert_label(db, wc->classid, wc->label))
			return -1;
		if (_insert_icon(db, pkgid, wc->classid, wc->icon))
			return -1;
	}

	if (stmt)
		sqlite3_finalize(stmt);

	return 0;
}

int widget_parser_db_insert_widget_class(const char *pkgid, GList *widget_list)
{
	int ret;
	sqlite3 *db;

	db = _open_db(__get_target_uid(), false);
	if (db == NULL)
		return -1;

	if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL)) {
		LOGE("begin transaction error");
		sqlite3_close_v2(db);
		return -1;
	}

	ret = _insert_widget_class(db, pkgid, widget_list);
	if (ret) {
		LOGE("failed to insert widget class data");
		sqlite3_close_v2(db);
		return -1;
	}

	if (sqlite3_exec(db, "END TRANSACTION", NULL, NULL, NULL)) {
		LOGE("begin transaction error");
		sqlite3_close_v2(db);
		return -1;
	}

	_close_db(db);

	return 0;
}

static int _remove_widget_class(sqlite3 *db, const char *pkgid)
{
	int ret;
	static const char query[] =
		"DELETE FROM widget_class WHERE pkgid=?";
	sqlite3_stmt *stmt = NULL;

	ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL);
	if (ret != SQLITE_OK) {
		LOGE("prepare error: %s", sqlite3_errmsg(db));
		return -1;
	}

	_bind_text(stmt, 1, pkgid);

	ret = sqlite3_step(stmt);
	if (ret != SQLITE_DONE) {
		LOGE("step error: %s", sqlite3_errmsg(db));
		sqlite3_finalize(stmt);
		return -1;
	}

	sqlite3_finalize(stmt);

	return 0;
}

int widget_parser_db_remove_widget_class(const char *pkgid)
{
	int ret;
	sqlite3 *db;

	db = _open_db(__get_target_uid(), false);
	if (db == NULL)
		return -1;

	if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL)) {
		LOGE("begin transaction error");
		sqlite3_close_v2(db);
		return -1;
	}

	ret = _remove_widget_class(db, pkgid);
	if (ret) {
		LOGE("failed to remove widget class data");
		sqlite3_close_v2(db);
		return -1;
	}

	if (sqlite3_exec(db, "END TRANSACTION", NULL, NULL, NULL)) {
		LOGE("begin transaction error");
		sqlite3_close_v2(db);
		return -1;
	}

	_close_db(db);

	return 0;
}