~unity8-desktop-session-team/indicator-session/indicator-session-using-upstart

« back to all changes in this revision

Viewing changes to src/session-dbus.c

  • Committer: Charles Kerr
  • Date: 2012-07-05 15:45:10 UTC
  • Revision ID: charles.kerr@canonical.com-20120705154510-q6dhcpn2lrosssv7
copyediting

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
The Dbus object on the bus for the indicator.
 
3
 
 
4
Copyright 2010 Canonical Ltd.
 
5
 
 
6
Authors:
 
7
    Ted Gould <ted@canonical.com>
 
8
    Conor Curran <conor.curran@canonical.com>
 
9
 
 
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.
 
13
 
 
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.
 
18
 
 
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/>.
 
21
*/
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include "config.h"
 
25
#endif
 
26
 
 
27
#include <gio/gio.h>
 
28
 
 
29
#include "session-dbus.h"
 
30
#include "shared-names.h"
 
31
 
 
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);
 
35
 
 
36
#include "gen-session-dbus.xml.h"
 
37
 
 
38
typedef struct _SessionDbusPrivate SessionDbusPrivate;
 
39
struct _SessionDbusPrivate {
 
40
        gchar * name;
 
41
        GDBusConnection * bus;
 
42
        GCancellable * bus_cancel;
 
43
        guint dbus_registration;
 
44
};
 
45
 
 
46
/* GDBus Stuff */
 
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 */
 
53
};
 
54
 
 
55
#define SESSION_DBUS_GET_PRIVATE(o) \
 
56
(G_TYPE_INSTANCE_GET_PRIVATE ((o), SESSION_DBUS_TYPE, SessionDbusPrivate))
 
57
 
 
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);
 
62
 
 
63
G_DEFINE_TYPE (SessionDbus, session_dbus, G_TYPE_OBJECT);
 
64
 
 
65
static void
 
66
session_dbus_class_init (SessionDbusClass *klass)
 
67
{
 
68
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
69
 
 
70
        g_type_class_add_private (klass, sizeof (SessionDbusPrivate));
 
71
 
 
72
        object_class->dispose = session_dbus_dispose;
 
73
        object_class->finalize = session_dbus_finalize;
 
74
 
 
75
        /* Setting up the DBus interfaces */
 
76
        if (node_info == NULL) {
 
77
                GError * error = NULL;
 
78
 
 
79
                node_info = g_dbus_node_info_new_for_xml(_session_dbus, &error);
 
80
                if (error != NULL) {
 
81
                        g_error("Unable to parse Session Service Interface description: %s", error->message);
 
82
                        g_error_free(error);
 
83
                }
 
84
        }
 
85
 
 
86
        if (interface_info == NULL) {
 
87
                interface_info = g_dbus_node_info_lookup_interface(node_info, INDICATOR_SESSION_SERVICE_DBUS_IFACE);
 
88
 
 
89
                if (interface_info == NULL) {
 
90
                        g_error("Unable to find interface '" INDICATOR_SESSION_SERVICE_DBUS_IFACE "'");
 
91
                }
 
92
        }
 
93
 
 
94
        return;
 
95
}
 
96
 
 
97
static void
 
98
session_dbus_init (SessionDbus *self)
 
99
{
 
100
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(self);
 
101
 
 
102
        priv->name = NULL;
 
103
        priv->bus = NULL;
 
104
        priv->bus_cancel = NULL;
 
105
        priv->dbus_registration = 0;
 
106
 
 
107
        priv->bus_cancel = g_cancellable_new();
 
108
        g_bus_get(G_BUS_TYPE_SESSION,
 
109
                  priv->bus_cancel,
 
110
                  bus_get_cb,
 
111
                  self);
 
112
 
 
113
        return;
 
114
}
 
115
 
 
116
static void
 
117
bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data)
 
118
{
 
119
        GError * error = NULL;
 
120
        GDBusConnection * connection = g_bus_get_finish(res, &error);
 
121
 
 
122
        if (error != NULL) {
 
123
                g_error("OMG! Unable to get a connection to DBus: %s", error->message);
 
124
                g_error_free(error);
 
125
                return;
 
126
        }
 
127
 
 
128
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(user_data);
 
129
 
 
130
        g_warn_if_fail(priv->bus == NULL);
 
131
        priv->bus = connection;
 
132
 
 
133
        if (priv->bus_cancel != NULL) {
 
134
                g_object_unref(priv->bus_cancel);
 
135
                priv->bus_cancel = NULL;
 
136
        }
 
137
 
 
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,
 
141
                                                                    interface_info,
 
142
                                                                    &interface_table,
 
143
                                                                    user_data,
 
144
                                                                    NULL,
 
145
                                                                    &error);
 
146
 
 
147
        if (error != NULL) {
 
148
                g_error("Unable to register the object to DBus: %s", error->message);
 
149
                g_error_free(error);
 
150
                return;
 
151
        }
 
152
 
 
153
        return; 
 
154
}
 
155
 
 
156
/* A method has been called from our dbus inteface.  Figure out what it
 
157
   is and dispatch it. */
 
158
static void
 
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)
 
163
{
 
164
        SessionDbus * service = SESSION_DBUS (user_data);
 
165
 
 
166
        GVariant * retval = NULL;
 
167
 
 
168
        if (g_strcmp0(method, "GetUserRealName") == 0) {
 
169
                retval = get_users_real_name (service);
 
170
        }
 
171
  else {
 
172
    g_warning("Calling method '%s' on the indicator service and it's unknown", method);
 
173
        }
 
174
        g_dbus_method_invocation_return_value(invocation, retval);
 
175
        return;
 
176
}
 
177
 
 
178
static void
 
179
session_dbus_dispose (GObject *object)
 
180
{
 
181
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);
 
182
 
 
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;
 
187
        }
 
188
 
 
189
        if (priv->bus != NULL) {
 
190
                g_object_unref(priv->bus);
 
191
                priv->bus = NULL;
 
192
        }
 
193
 
 
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;
 
198
        }
 
199
 
 
200
        G_OBJECT_CLASS (session_dbus_parent_class)->dispose (object);
 
201
        return;
 
202
}
 
203
 
 
204
static void
 
205
session_dbus_finalize (GObject *object)
 
206
{
 
207
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);
 
208
 
 
209
        g_clear_pointer (&priv->name, g_free);
 
210
 
 
211
        G_OBJECT_CLASS (session_dbus_parent_class)->finalize (object);
 
212
        return;
 
213
}
 
214
 
 
215
static GVariant *
 
216
get_users_real_name (SessionDbus * service)
 
217
{
 
218
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(service);
 
219
        return g_variant_new ("(s)", priv->name ? priv->name : "");
 
220
}
 
221
 
 
222
SessionDbus *
 
223
session_dbus_new (void)
 
224
{
 
225
        return SESSION_DBUS(g_object_new(SESSION_DBUS_TYPE, NULL));
 
226
}
 
227
 
 
228
void
 
229
session_dbus_set_name (SessionDbus * session, const gchar * name)
 
230
{
 
231
}
 
232
 
 
233
void
 
234
session_dbus_set_users_real_name (SessionDbus * session, const gchar * name)
 
235
{
 
236
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
 
237
        GError * error = NULL;
 
238
 
 
239
        g_free (priv->name);
 
240
        priv->name = g_strdup(name);
 
241
 
 
242
        if (priv->bus != NULL) {
 
243
                g_dbus_connection_emit_signal (priv->bus,
 
244
                                   NULL,
 
245
                                   INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
 
246
                                   INDICATOR_SESSION_SERVICE_DBUS_IFACE,
 
247
                                   "UserRealNameUpdated",
 
248
                                   g_variant_new ("(s)", priv->name, NULL),
 
249
                                   &error);
 
250
 
 
251
                if (error != NULL) {
 
252
                        g_warning("Unable to send UserRealNameUpdated signal: %s", error->message);
 
253
                        g_error_free(error);
 
254
                        return;
 
255
                }
 
256
        }
 
257
        return;
 
258
}
 
259
 
 
260
void session_dbus_restart_required (SessionDbus* session)
 
261
{
 
262
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
 
263
        GError * error = NULL;
 
264
 
 
265
        if (priv->bus != NULL) {
 
266
    g_debug("About to send RebootRequired signal");
 
267
 
 
268
                g_dbus_connection_emit_signal (priv->bus,
 
269
                                   NULL,
 
270
                                   INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
 
271
                                   INDICATOR_SESSION_SERVICE_DBUS_IFACE,
 
272
                                   "RestartRequired",
 
273
                                   NULL,
 
274
                                   &error);
 
275
 
 
276
                if (error != NULL) {
 
277
                        g_warning("Unable to send reboot-required signal: %s", error->message);
 
278
                        g_error_free(error);
 
279
                }
 
280
        }
 
281
 
 
282
}