2
The Dbus object on the bus for the indicator.
4
Copyright 2010 Canonical Ltd.
7
Ted Gould <ted@canonical.com>
8
Conor Curran <conor.curran@canonical.com>
10
This program is free software: you can redistribute it and/or modify it
11
under the terms of the GNU General Public License version 3, as published
12
by the Free Software Foundation.
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranties of
16
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
17
PURPOSE. See the GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License along
20
with this program. If not, see <http://www.gnu.org/licenses/>.
29
#include "session-dbus.h"
30
#include "shared-names.h"
32
static GVariant * get_users_real_name (SessionDbus * service);
33
static void bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data);
34
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);
36
#include "gen-session-dbus.xml.h"
38
typedef struct _SessionDbusPrivate SessionDbusPrivate;
39
struct _SessionDbusPrivate {
41
GDBusConnection * bus;
42
GCancellable * bus_cancel;
43
guint dbus_registration;
47
static GDBusNodeInfo * node_info = NULL;
48
static GDBusInterfaceInfo * interface_info = NULL;
49
static GDBusInterfaceVTable interface_table = {
50
method_call: bus_method_call,
51
get_property: NULL, /* No properties */
52
set_property: NULL /* No properties */
55
#define SESSION_DBUS_GET_PRIVATE(o) \
56
(G_TYPE_INSTANCE_GET_PRIVATE ((o), SESSION_DBUS_TYPE, SessionDbusPrivate))
58
static void session_dbus_class_init (SessionDbusClass *klass);
59
static void session_dbus_init (SessionDbus *self);
60
static void session_dbus_dispose (GObject *object);
61
static void session_dbus_finalize (GObject *object);
63
G_DEFINE_TYPE (SessionDbus, session_dbus, G_TYPE_OBJECT);
66
session_dbus_class_init (SessionDbusClass *klass)
68
GObjectClass *object_class = G_OBJECT_CLASS (klass);
70
g_type_class_add_private (klass, sizeof (SessionDbusPrivate));
72
object_class->dispose = session_dbus_dispose;
73
object_class->finalize = session_dbus_finalize;
75
/* Setting up the DBus interfaces */
76
if (node_info == NULL) {
77
GError * error = NULL;
79
node_info = g_dbus_node_info_new_for_xml(_session_dbus, &error);
81
g_error("Unable to parse Session Service Interface description: %s", error->message);
86
if (interface_info == NULL) {
87
interface_info = g_dbus_node_info_lookup_interface(node_info, INDICATOR_SESSION_SERVICE_DBUS_IFACE);
89
if (interface_info == NULL) {
90
g_error("Unable to find interface '" INDICATOR_SESSION_SERVICE_DBUS_IFACE "'");
98
session_dbus_init (SessionDbus *self)
100
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(self);
104
priv->bus_cancel = NULL;
105
priv->dbus_registration = 0;
107
priv->bus_cancel = g_cancellable_new();
108
g_bus_get(G_BUS_TYPE_SESSION,
117
bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data)
119
GError * error = NULL;
120
GDBusConnection * connection = g_bus_get_finish(res, &error);
123
g_error("OMG! Unable to get a connection to DBus: %s", error->message);
128
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(user_data);
130
g_warn_if_fail(priv->bus == NULL);
131
priv->bus = connection;
133
if (priv->bus_cancel != NULL) {
134
g_object_unref(priv->bus_cancel);
135
priv->bus_cancel = NULL;
138
/* Now register our object on our new connection */
139
priv->dbus_registration = g_dbus_connection_register_object(priv->bus,
140
INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
148
g_error("Unable to register the object to DBus: %s", error->message);
156
/* A method has been called from our dbus inteface. Figure out what it
157
is and dispatch it. */
159
bus_method_call (GDBusConnection * connection, const gchar * sender,
160
const gchar * path, const gchar * interface,
161
const gchar * method, GVariant * params,
162
GDBusMethodInvocation * invocation, gpointer user_data)
164
SessionDbus * service = SESSION_DBUS (user_data);
166
GVariant * retval = NULL;
168
if (g_strcmp0(method, "GetUserRealName") == 0) {
169
retval = get_users_real_name (service);
172
g_warning("Calling method '%s' on the indicator service and it's unknown", method);
174
g_dbus_method_invocation_return_value(invocation, retval);
179
session_dbus_dispose (GObject *object)
181
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);
183
if (priv->dbus_registration != 0) {
184
g_dbus_connection_unregister_object(priv->bus, priv->dbus_registration);
185
/* Don't care if it fails, there's nothing we can do */
186
priv->dbus_registration = 0;
189
if (priv->bus != NULL) {
190
g_object_unref(priv->bus);
194
if (priv->bus_cancel != NULL) {
195
g_cancellable_cancel(priv->bus_cancel);
196
g_object_unref(priv->bus_cancel);
197
priv->bus_cancel = NULL;
200
G_OBJECT_CLASS (session_dbus_parent_class)->dispose (object);
205
session_dbus_finalize (GObject *object)
207
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);
209
g_clear_pointer (&priv->name, g_free);
211
G_OBJECT_CLASS (session_dbus_parent_class)->finalize (object);
216
get_users_real_name (SessionDbus * service)
218
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(service);
219
return g_variant_new ("(s)", priv->name ? priv->name : "");
223
session_dbus_new (void)
225
return SESSION_DBUS(g_object_new(SESSION_DBUS_TYPE, NULL));
229
session_dbus_set_name (SessionDbus * session, const gchar * name)
234
session_dbus_set_users_real_name (SessionDbus * session, const gchar * name)
236
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
237
GError * error = NULL;
240
priv->name = g_strdup(name);
242
if (priv->bus != NULL) {
243
g_dbus_connection_emit_signal (priv->bus,
245
INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
246
INDICATOR_SESSION_SERVICE_DBUS_IFACE,
247
"UserRealNameUpdated",
248
g_variant_new ("(s)", priv->name, NULL),
252
g_warning("Unable to send UserRealNameUpdated signal: %s", error->message);
260
void session_dbus_restart_required (SessionDbus* session)
262
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
263
GError * error = NULL;
265
if (priv->bus != NULL) {
266
g_debug("About to send RebootRequired signal");
268
g_dbus_connection_emit_signal (priv->bus,
270
INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
271
INDICATOR_SESSION_SERVICE_DBUS_IFACE,
277
g_warning("Unable to send reboot-required signal: %s", error->message);