summaryrefslogtreecommitdiff
path: root/syspopup-caller/syspopup_caller.c
blob: 0695b63438483537270d0122caeb6dae82903c75 (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
/*
 * Copyright (c) 2000 - 2017 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 <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <glib.h>
#include <gio/gio.h>
#include <bundle.h>
#include <aul.h>

#include "syspopup_core.h"
#include "syspopup_db.h"
#include "syspopup_api.h"
#include "simple_util.h"
#include "syspopup_caller.h"

#define REGULAR_UID_MIN 5000

struct cb_info {
	syspopup_info_cb callback;
	void *user_data;
};

API int syspopup_launch_for_uid(char *popup_name, bundle *b, uid_t uid)
{
	syspopup_info_t *info;
	int ret = -1;
	int is_bundle = 0;

	if (popup_name == NULL) {
		_E("popup_name is NULL");
		return -1;
	}

	info = _syspopup_info_get(popup_name);
	if (info == NULL || info->pkgname == NULL) {
		_E("info or info->pkgname is NULL");
		if (info)
			_syspopup_info_free(info);
		return -1;
	}

	if (b == NULL) {
		b = bundle_create();
		if (b == NULL) {
			_E("out of memory");
			_syspopup_info_free(info);
			return -1;
		}
		is_bundle = 1;
	}

	if (_syspopup_set_name_to_bundle(b, popup_name) < 0) {
		_E("bundle set error\n");
		_syspopup_info_free(info);
		if (is_bundle)
			bundle_free(b);
		return -1;
	}

	ret = aul_launch_app_async_for_uid(info->pkgname, b, uid);
	if (ret < 0) {
		_E("syspopup launch error - %d", ret);
		_syspopup_info_free(info);
		if (is_bundle)
			bundle_free(b);
		return -1;
	}

	if (is_bundle)
		bundle_free(b);

	_syspopup_info_free(info);

	return 0;
}

API int syspopup_launch(char *popup_name, bundle *b)
{
	return syspopup_launch_for_uid(popup_name, b, getuid());
}

API int syspopup_destroy_all(void)
{
	GDBusConnection *conn = NULL;
	GError *err = NULL;
	int ret = 0;

#if !(GLIB_CHECK_VERSION(2, 36, 0))
	g_type_init();
#endif

	conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
	if (err) {
		_E("gdbus connection error: %s", err->message);
		g_error_free(err);
		return -1;
	}

	if (g_dbus_connection_emit_signal(conn,
					NULL,
					SYSPOPUP_DBUS_PATH,
					SYSPOPUP_DBUS_SIGNAL_INTERFACE,
					SYSPOPUP_DBUS_SP_TERM_SIGNAL,
					NULL,
					&err) == FALSE) {
		_E("emitting the signal error: %s", err->message);
		ret = -1;
	} else {
		_D("send signal done");
	}

	if (err)
		g_error_free(err);
	if (conn)
		g_object_unref(conn);

	return ret;
}

static int __foreach_cb(syspopup_info_t *sp_info, void *user_data)
{
	struct cb_info *info = (struct cb_info *)user_data;

	if (!info)
		return -1;

	return info->callback(sp_info->name, sp_info->pkgname, info->user_data);
}

API int syspopup_foreach_info(syspopup_info_cb callback, void *user_data)
{
	struct cb_info info = {
		.callback = callback,
		.user_data = user_data
	};

	if (!callback) {
		_E("Invalid parameter");
		return -1;
	}

	return _syspopup_info_foreach(__foreach_cb, &info);
}