aboutsummaryrefslogtreecommitdiff
path: root/src/session-dbus.c
blob: 4b9d45f447d09caa33bac81909881ea4c3c09c1c (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
/*
The Dbus object on the bus for the indicator.

Copyright 2010 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>
    Conor Curran <conor.curran@canonical.com>

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gio/gio.h>

#include "session-dbus.h"
#include "shared-names.h"

static GVariant * get_users_real_name (SessionDbus * service);
static void bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data);
static void bus_method_call (GDBusConnection * connection, const gchar * sender, const gchar * path, const gchar * interface, const gchar * method, GVariant * params, GDBusMethodInvocation * invocation, gpointer user_data);

#include "gen-session-dbus.xml.h"

typedef struct _SessionDbusPrivate SessionDbusPrivate;
struct _SessionDbusPrivate {
	gchar * name;
	GDBusConnection * bus;
	GCancellable * bus_cancel;
	guint dbus_registration;
};

/* GDBus Stuff */
static GDBusNodeInfo *      node_info = NULL;
static GDBusInterfaceInfo * interface_info = NULL;
static GDBusInterfaceVTable interface_table = {
       method_call:    bus_method_call,
       get_property:   NULL, /* No properties */
       set_property:   NULL  /* No properties */
};

#define SESSION_DBUS_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), SESSION_DBUS_TYPE, SessionDbusPrivate))

static void session_dbus_class_init (SessionDbusClass *klass);
static void session_dbus_init       (SessionDbus *self);
static void session_dbus_dispose    (GObject *object);
static void session_dbus_finalize   (GObject *object);

G_DEFINE_TYPE (SessionDbus, session_dbus, G_TYPE_OBJECT);

static void
session_dbus_class_init (SessionDbusClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (SessionDbusPrivate));

	object_class->dispose = session_dbus_dispose;
	object_class->finalize = session_dbus_finalize;

	/* Setting up the DBus interfaces */
	if (node_info == NULL) {
		GError * error = NULL;

		node_info = g_dbus_node_info_new_for_xml(_session_dbus, &error);
		if (error != NULL) {
			g_error("Unable to parse Session Service Interface description: %s", error->message);
			g_error_free(error);
		}
	}

	if (interface_info == NULL) {
		interface_info = g_dbus_node_info_lookup_interface(node_info, INDICATOR_SESSION_SERVICE_DBUS_IFACE);

		if (interface_info == NULL) {
			g_error("Unable to find interface '" INDICATOR_SESSION_SERVICE_DBUS_IFACE "'");
		}
	}

	return;
}

static void
session_dbus_init (SessionDbus *self)
{
	SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(self);

	priv->name = NULL;
	priv->bus = NULL;
	priv->bus_cancel = NULL;
	priv->dbus_registration = 0;

	priv->bus_cancel = g_cancellable_new();
	g_bus_get(G_BUS_TYPE_SESSION,
	          priv->bus_cancel,
	          bus_get_cb,
	          self);

	return;
}

static void
bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data)
{
	GError * error = NULL;
	GDBusConnection * connection = g_bus_get_finish(res, &error);

	if (error != NULL) {
		g_error("OMG! Unable to get a connection to DBus: %s", error->message);
		g_error_free(error);
		return;
	}

	SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(user_data);

	g_warn_if_fail(priv->bus == NULL);
	priv->bus = connection;

	if (priv->bus_cancel != NULL) {
		g_object_unref(priv->bus_cancel);
		priv->bus_cancel = NULL;
	}

	/* Now register our object on our new connection */
	priv->dbus_registration = g_dbus_connection_register_object(priv->bus,
	                                                            INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
	                                                            interface_info,
	                                                            &interface_table,
	                                                            user_data,
	                                                            NULL,
	                                                            &error);

	if (error != NULL) {
		g_error("Unable to register the object to DBus: %s", error->message);
		g_error_free(error);
		return;
	}

	return;	
}

/* A method has been called from our dbus inteface.  Figure out what it
   is and dispatch it. */
static void
bus_method_call (GDBusConnection * connection, const gchar * sender,
                 const gchar * path, const gchar * interface,
                 const gchar * method, GVariant * params,
                 GDBusMethodInvocation * invocation, gpointer user_data)
{
	SessionDbus * service = SESSION_DBUS (user_data);

	GVariant * retval = NULL;

	if (g_strcmp0(method, "GetUserRealName") == 0) {
		retval = get_users_real_name (service);
	}
  else {
    g_warning("Calling method '%s' on the indicator service and it's unknown", method);
	}
	g_dbus_method_invocation_return_value(invocation, retval);
	return;
}

static void
session_dbus_dispose (GObject *object)
{
	SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);

	if (priv->dbus_registration != 0) {
		g_dbus_connection_unregister_object(priv->bus, priv->dbus_registration);
		/* Don't care if it fails, there's nothing we can do */
		priv->dbus_registration = 0;
	}

	if (priv->bus != NULL) {
		g_object_unref(priv->bus);
		priv->bus = NULL;
	}

	if (priv->bus_cancel != NULL) {
		g_cancellable_cancel(priv->bus_cancel);
		g_object_unref(priv->bus_cancel);
		priv->bus_cancel = NULL;
	}

	G_OBJECT_CLASS (session_dbus_parent_class)->dispose (object);
	return;
}

static void
session_dbus_finalize (GObject *object)
{
	SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);

	g_clear_pointer (&priv->name, g_free);

	G_OBJECT_CLASS (session_dbus_parent_class)->finalize (object);
	return;
}

static GVariant *
get_users_real_name (SessionDbus * service)
{
	SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(service);
	return g_variant_new ("(s)", priv->name ? priv->name : "");
}

SessionDbus *
session_dbus_new (void)
{
	return SESSION_DBUS(g_object_new(SESSION_DBUS_TYPE, NULL));
}

void
session_dbus_set_name (SessionDbus * session, const gchar * name)
{
}

void
session_dbus_set_users_real_name (SessionDbus * session, const gchar * name)
{
	SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
	GError * error = NULL;
	if (priv->name != NULL) {
		g_free(priv->name);
		priv->name = NULL;
	}

	priv->name = g_strdup(name);

	if (priv->bus != NULL) {
		g_dbus_connection_emit_signal (priv->bus,
                                   NULL,
                                   INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
                                   INDICATOR_SESSION_SERVICE_DBUS_IFACE,
                                   "UserRealNameUpdated",
                                   g_variant_new ("(s)", priv->name, NULL),
                                   &error);

		if (error != NULL) {
			g_warning("Unable to send UserRealNameUpdated signal: %s", error->message);
			g_error_free(error);
			return;
		}
	}
	return;
}

void session_dbus_restart_required (SessionDbus* session)
{
	SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
	GError * error = NULL;

	if (priv->bus != NULL) {
    g_debug("About to send RebootRequired signal");

		g_dbus_connection_emit_signal (priv->bus,
                                   NULL,
                                   INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
                                   INDICATOR_SESSION_SERVICE_DBUS_IFACE,
                                   "RestartRequired",
                                   NULL,
                                   &error);

		if (error != NULL) {
			g_warning("Unable to send reboot-required signal: %s", error->message);
			g_error_free(error);
		}
	}

}