summaryrefslogtreecommitdiff
path: root/src/data/data_user.c
blob: 9d90c9f3288e49a74c172720f8db70a43262614b (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Copyright (c) 2015 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 <app_debug.h>
#include <Eina.h>
#include <Evas.h>
#include <stdbool.h>
#include <viewmgr.h>
#include <stdlib.h>
#include <gum-user.h>
#include <gum-user-service.h>
#include <common/gum-user-types.h>
#include <gio/gio.h>

#include "data_user.h"
#include "datamgr.h"
#include "defs.h"
#include "utils.h"

#define ADDRESS_BUF_MAX 128
#define USER_SWITCH_TLM_SEAT_ID "seat0"
#define USER_SWITCH_DBUS_SOCKET_PATH "/var/run/tlm/"
#define USER_SWITCH_TLM_BUS_NAME "org.O1.Tlm.Login"
#define USER_SWITCH_TLM_INTERFACE USER_SWITCH_TLM_BUS_NAME
#define USER_SWITCH_TLM_OBJECT_PATH "/org/O1/Tlm/Login"
#define USER_SWITCH_METHOD "switchUser"

#define PINCODE "tizen"

static GVariant *_build_env_param(const char *username, const char *password)
{
	GVariantBuilder *builder;
	GVariant *param, *env;

	builder = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
	if (!builder) {
		_ERR("failed to new builder");
		return NULL;
	}

	g_variant_builder_add(builder, "{ss}", "", "");
	env = g_variant_builder_end(builder);
	g_variant_builder_unref(builder);

	param = g_variant_new("(sss@a{ss})", USER_SWITCH_TLM_SEAT_ID,
			username, password, env);

	return param;
}

static GDBusConnection *_get_bus_connection(GError **error)
{
	gchar address[ADDRESS_BUF_MAX];

	g_snprintf(address, ADDRESS_BUF_MAX - 1, "unix:path=%s%s-%u",
			USER_SWITCH_DBUS_SOCKET_PATH, USER_SWITCH_TLM_SEAT_ID,
			getuid());

	return g_dbus_connection_new_for_address_sync(address,
			G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
			NULL, NULL, error);
}

static int _switch(char *name, char *password)
{
	GDBusConnection *conn;
	GError *error;
	GVariant *param;
	GDBusMessage *msg, *res;
	guint32 serial;

	if (!name || !password)
		return ITEM_SELECT_ERROR_INVALID_PARAMETER;

	error = NULL;
	conn = _get_bus_connection(&error);
	if (!conn) {
		_ERR("failed to get connection, %s",
				error ? error->message : "");
		g_error_free(error);
		return ITEM_SELECT_ERROR_INVALID_OPERATION;
	}

	param = _build_env_param(name, password);
	if (!param) {
		_ERR("failed to build env param");
		g_object_unref(conn);
		g_error_free(error);
		return ITEM_SELECT_ERROR_INVALID_OPERATION;
	}

	msg = g_dbus_message_new_method_call(USER_SWITCH_TLM_BUS_NAME,
			USER_SWITCH_TLM_OBJECT_PATH, USER_SWITCH_TLM_INTERFACE,
			USER_SWITCH_METHOD);
	g_dbus_message_set_body(msg, param);

	res = g_dbus_connection_send_message_with_reply_sync(conn, msg,
			G_DBUS_SEND_MESSAGE_FLAGS_NONE, 1000, &serial, NULL,
			&error);
	if (!res) {
		_ERR("failed to send message, %s", error ? error->message : "");
		g_object_unref(msg);
		g_object_unref(conn);
		g_error_free(error);
		return ITEM_SELECT_ERROR_SWITCH;
	}

	g_object_unref(res);
	g_object_unref(msg);
	g_object_unref(conn);

	viewmgr_hide_view(VIEW_USER);

	return ITEM_SELECT_ERROR_NONE;
}

static int _update(struct datamgr_item *di)
{
	GumUser *user;
	gboolean r;

	user = gum_user_get_by_name_sync(di->title, FALSE);
	if (!user) {
		_ERR("failed to get user");
		return ITEM_SELECT_ERROR_INVALID_OPERATION;
	}

	g_object_set(G_OBJECT(user), GUM_ATTR_ICON, di->icon, NULL);

	r = gum_user_update_sync(user);
	if (!r) {
		_ERR("failed to update user");
		g_object_unref(user);
		return ITEM_SELECT_ERROR_INVALID_OPERATION;
	}

	g_object_unref(user);

	return ITEM_SELECT_ERROR_NONE;
}

static int _delete(struct datamgr_item *di)
{
	GumUser *user;
	gboolean r;

	user = gum_user_get_by_name_sync(di->title, FALSE);
	if (!user) {
		_ERR("failed to get user");
		return ITEM_SELECT_ERROR_INVALID_OPERATION;
	}

	r = gum_user_delete_sync(user, FALSE);
	if (!r) {
		_ERR("failed to delete user");
		g_object_unref(user);
		return ITEM_SELECT_ERROR_INVALID_OPERATION;
	}

	g_object_unref(user);

	return ITEM_SELECT_ERROR_NONE;
}

static int _select(struct datamgr_item *di)
{
	int r;

	if (!di) {
		_ERR("Invalid argument");
		return ITEM_SELECT_ERROR_INVALID_PARAMETER;
	}

	r = ITEM_SELECT_ERROR_INVALID_OPERATION;
	switch (di->action) {
	case ITEM_SELECT_ACTION_SWITCH:
		/* FIXME: set pincode "tizen" */
		r = _switch(di->title, PINCODE);
		break;
	case ITEM_SELECT_ACTION_PUSH:
		if (viewmgr_push_view(di->parameter))
			r = ITEM_SELECT_ERROR_NONE;
		break;
	case ITEM_SELECT_ACTION_POP:
		if (viewmgr_pop_view())
			r = ITEM_SELECT_ERROR_NONE;
		break;
	case ITEM_SELECT_ACTION_UPDATE:
		r = _update(di);
		break;
	case ITEM_SELECT_ACTION_DELETE:
		r = _delete(di);
		break;
	default:
		_ERR("Invalid state");
		break;
	}

	return r;
}

static void _unload_user(struct datamgr *dm)
{
	struct datamgr_item *di;

	EINA_LIST_FREE(dm->list, di) {
		free(di->title);
		free(di->icon);
		free(di->focus_icon);
		free(di->parameter);

		free(di);
	}

	dm->list = NULL;
}

static struct datamgr_item *_pack_user(char *name, char *icon, char *focus_icon,
		enum datamgr_item_select_action action, char *parameter,
		int type)
{
	struct datamgr_item *di;

	di = calloc(1, sizeof(*di));
	if (!di) {
		_ERR("failed calloc user item");
		return false;
	}

	if (!icon || strlen(icon) == 0)
		icon = IMAGE_USER_DEFAULT;

	if (!focus_icon || strlen(focus_icon) == 0)
		focus_icon = IMAGE_USER_DEFAULT_FOCUS;

	if (name)
		di->title = strdup(name);
	if (parameter)
		di->parameter = strdup(parameter);
	di->icon = strdup(icon);
	di->focus_icon = strdup(focus_icon);
	di->action = action;
	di->type = type;

	return di;
}

static bool _load_login_user(Eina_List **list)
{
	struct datamgr_item *di;
	GumUser *user;
	uid_t uid;
	char *name, *icon;
	GumUserType type;

	uid = getuid();
	user = gum_user_get_sync(uid, FALSE);
	if (!user) {
		_ERR("failed to get user service");
		return false;
	}

	name = NULL;
	icon = NULL;
	g_object_get(G_OBJECT(user), GUM_ATTR_NAME, &name, GUM_ATTR_ICON, &icon,
			GUM_ATTR_USERTYPE, &type, NULL);
	di = _pack_user(name, icon,
			(char *)utils_get_focus_icon_from_icon(icon),
			ITEM_SELECT_ACTION_POP, NULL, type);
	if (!di) {
		g_free(name);
		g_free(icon);
		g_object_unref(user);
		return false;
	}

	*list = eina_list_append(*list, di);

	g_free(name);
	g_free(icon);
	g_object_unref(user);

	return true;
}

static bool _load_users(Eina_List **list)
{
	GumUserList *ulist;
	GumUser *user;
	GumUserService *service;
	GumUserType type;
	struct datamgr_item *di;
	char *name, *icon;
	uid_t uid, user_uid;
	int i;

	static const gchar * const strv[] = {
		GUM_LIST_USERTYPE_NORMAL,
		GUM_LIST_USERTYPE_ADMIN,
		NULL
	};

	service = gum_user_service_create_sync(FALSE);
	if (!service) {
		_ERR("failed to create service");
		return false;
	}

	ulist = gum_user_service_get_user_list_sync(service, strv);
	if (!ulist) {
		_ERR("failed to get user list");
		g_object_unref(service);
		return false;
	}

	user_uid = getuid();
	for (i = 0; i < g_list_length(ulist); i++) {
		user = g_list_nth_data(ulist, i);
		if (!user)
			continue;

		g_object_get(G_OBJECT(user), GUM_ATTR_NAME, &name,
				GUM_ATTR_ICON, &icon, GUM_ATTR_UID, &uid,
				GUM_ATTR_USERTYPE, &type, NULL);
		if (uid == user_uid) {
			g_free(name);
			g_free(icon);
			continue;
		}

		di = _pack_user(name, icon,
				(char *)utils_get_focus_icon_from_icon(icon),
				ITEM_SELECT_ACTION_SWITCH, NULL, type);
		g_free(name);
		g_free(icon);

		if (!di)
			continue;

		*list = eina_list_append(*list, di);
	}

	gum_user_service_list_free(ulist);
	g_object_unref(service);

	return true;
}

static bool _load_add_user(Eina_List **list)
{
	struct datamgr_item *di;

	di = _pack_user(NULL, IMAGE_USER_ADD, IMAGE_USER_ADD_FOCUS,
			ITEM_SELECT_ACTION_PUSH, VIEW_USER_EDIT, 1);
	if (!di)
		return false;

	*list = eina_list_append(*list, di);

	return true;
}

static bool _load_user(struct datamgr *dm)
{
	if (!_load_login_user(&dm->list)) {
		_ERR("failed to load login user");
		return false;
	}

	if (!_load_users(&dm->list))
		_ERR("failed to load users");

	if (!_load_add_user(&dm->list))
		_ERR("failed to load add user");

	return true;
}

static Eina_List *_get_items(struct datamgr *dm)
{
	if (!dm) {
		_ERR("Invalid argument");
		return NULL;
	}

	_unload_user(dm);
	_load_user(dm);

	return dm->list;
}

static void _fini(struct datamgr *dm)
{
	if (!dm) {
		_ERR("Invalid argument");
		return;
	}

	_unload_user(dm);
}

static bool _init(struct datamgr *dm)
{
	if (!dm) {
		_ERR("Invalid argument");
		return false;
	}

	return _load_user(dm);
}

static bool _add(struct datamgr *dm, const char *title, const char *icon,
		const char *parameter)
{
	GumUser *user;
	gboolean r;

	if (!dm || !title || !icon) {
		_ERR("Invalid argument");
		return false;
	}

	user = gum_user_create_sync(FALSE);
	if (!user) {
		_ERR("failed to get user");
		return false;
	}

	/* FIXME: set pincode "tizen" */
	parameter = PINCODE;

	g_object_set(G_OBJECT(user), GUM_ATTR_NAME, title,
			GUM_ATTR_ICON, icon,
			GUM_ATTR_PASSWORD, parameter,
			GUM_ATTR_USERTYPE, GUM_USERTYPE_NORMAL, NULL);

	r = gum_user_add_sync(user);
	if (!r) {
		_ERR("failed to add user");
		g_object_unref(user);
		return false;
	}

	g_object_unref(user);

	return true;
}

static struct data_class dclass = {
	.init = _init,
	.fini = _fini,
	.get_items = _get_items,
	.select = _select,
	.add = _add
};

struct data_class *datamgr_user_get_dclass(void)
{
	return &dclass;
}