~mefrio-g/+junk/indicator-session-pantheon-shutdown

« back to all changes in this revision

Viewing changes to src/session-dbus.c

  • Committer: cody at elementaryos
  • Date: 2012-12-10 00:13:38 UTC
  • Revision ID: cody@elementaryos.org-20121210001338-379sxx4jo6r003d6
Initial import, version 0.3.96-0ubuntu1

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 "dbus-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
  gboolean user_menu_is_visible;
 
42
        GDBusConnection * bus;
 
43
        GCancellable * bus_cancel;
 
44
        guint dbus_registration;
 
45
};
 
46
 
 
47
/* GDBus Stuff */
 
48
static GDBusNodeInfo *      node_info = NULL;
 
49
static GDBusInterfaceInfo * interface_info = NULL;
 
50
static GDBusInterfaceVTable interface_table = {
 
51
       method_call:    bus_method_call,
 
52
       get_property:   NULL, /* No properties */
 
53
       set_property:   NULL  /* No properties */
 
54
};
 
55
 
 
56
#define SESSION_DBUS_GET_PRIVATE(o) \
 
57
(G_TYPE_INSTANCE_GET_PRIVATE ((o), SESSION_DBUS_TYPE, SessionDbusPrivate))
 
58
 
 
59
static void session_dbus_class_init (SessionDbusClass *klass);
 
60
static void session_dbus_init       (SessionDbus *self);
 
61
static void session_dbus_dispose    (GObject *object);
 
62
static void session_dbus_finalize   (GObject *object);
 
63
 
 
64
G_DEFINE_TYPE (SessionDbus, session_dbus, G_TYPE_OBJECT);
 
65
 
 
66
static void
 
67
session_dbus_class_init (SessionDbusClass *klass)
 
68
{
 
69
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
70
 
 
71
        g_type_class_add_private (klass, sizeof (SessionDbusPrivate));
 
72
 
 
73
        object_class->dispose = session_dbus_dispose;
 
74
        object_class->finalize = session_dbus_finalize;
 
75
 
 
76
        /* Setting up the DBus interfaces */
 
77
        if (node_info == NULL) {
 
78
                GError * error = NULL;
 
79
 
 
80
                node_info = g_dbus_node_info_new_for_xml(_session_dbus, &error);
 
81
                if (error != NULL) {
 
82
                        g_error("Unable to parse Session Service Interface description: %s", error->message);
 
83
                        g_error_free(error);
 
84
                }
 
85
        }
 
86
 
 
87
        if (interface_info == NULL) {
 
88
                interface_info = g_dbus_node_info_lookup_interface(node_info, INDICATOR_SESSION_SERVICE_DBUS_IFACE);
 
89
 
 
90
                if (interface_info == NULL) {
 
91
                        g_error("Unable to find interface '" INDICATOR_SESSION_SERVICE_DBUS_IFACE "'");
 
92
                }
 
93
        }
 
94
 
 
95
        return;
 
96
}
 
97
 
 
98
static void
 
99
session_dbus_init (SessionDbus *self)
 
100
{
 
101
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(self);
 
102
 
 
103
        priv->name = NULL;
 
104
        priv->bus = NULL;
 
105
        priv->bus_cancel = NULL;
 
106
        priv->dbus_registration = 0;
 
107
  priv->user_menu_is_visible = FALSE;
 
108
 
 
109
        priv->bus_cancel = g_cancellable_new();
 
110
        g_bus_get(G_BUS_TYPE_SESSION,
 
111
                  priv->bus_cancel,
 
112
                  bus_get_cb,
 
113
                  self);
 
114
 
 
115
        return;
 
116
}
 
117
 
 
118
static void
 
119
bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data)
 
120
{
 
121
        GError * error = NULL;
 
122
        GDBusConnection * connection = g_bus_get_finish(res, &error);
 
123
 
 
124
        if (error != NULL) {
 
125
                g_error("OMG! Unable to get a connection to DBus: %s", error->message);
 
126
                g_error_free(error);
 
127
                return;
 
128
        }
 
129
 
 
130
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(user_data);
 
131
 
 
132
        g_warn_if_fail(priv->bus == NULL);
 
133
        priv->bus = connection;
 
134
 
 
135
        if (priv->bus_cancel != NULL) {
 
136
                g_object_unref(priv->bus_cancel);
 
137
                priv->bus_cancel = NULL;
 
138
        }
 
139
 
 
140
        /* Now register our object on our new connection */
 
141
        priv->dbus_registration = g_dbus_connection_register_object(priv->bus,
 
142
                                                                    INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
 
143
                                                                    interface_info,
 
144
                                                                    &interface_table,
 
145
                                                                    user_data,
 
146
                                                                    NULL,
 
147
                                                                    &error);
 
148
 
 
149
        if (error != NULL) {
 
150
                g_error("Unable to register the object to DBus: %s", error->message);
 
151
                g_error_free(error);
 
152
                return;
 
153
        }
 
154
 
 
155
        return; 
 
156
}
 
157
 
 
158
/* A method has been called from our dbus inteface.  Figure out what it
 
159
   is and dispatch it. */
 
160
static void
 
161
bus_method_call (GDBusConnection * connection, const gchar * sender,
 
162
                 const gchar * path, const gchar * interface,
 
163
                 const gchar * method, GVariant * params,
 
164
                 GDBusMethodInvocation * invocation, gpointer user_data)
 
165
{
 
166
        SessionDbus * service = SESSION_DBUS (user_data);
 
167
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE (service);
 
168
 
 
169
        GVariant * retval = NULL;
 
170
 
 
171
        if (g_strcmp0(method, "GetUserRealName") == 0) {
 
172
                retval = get_users_real_name (service);
 
173
        }
 
174
  else if (g_strcmp0 (method, "GetUserMenuVisibility") == 0){
 
175
    retval =  g_variant_new ("(b)", priv->user_menu_is_visible);
 
176
  }
 
177
  else {
 
178
    g_warning("Calling method '%s' on the indicator service and it's unknown", method);
 
179
        }
 
180
        g_dbus_method_invocation_return_value(invocation, retval);
 
181
        return;
 
182
}
 
183
 
 
184
static void
 
185
session_dbus_dispose (GObject *object)
 
186
{
 
187
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);
 
188
 
 
189
        if (priv->dbus_registration != 0) {
 
190
                g_dbus_connection_unregister_object(priv->bus, priv->dbus_registration);
 
191
                /* Don't care if it fails, there's nothing we can do */
 
192
                priv->dbus_registration = 0;
 
193
        }
 
194
 
 
195
        if (priv->bus != NULL) {
 
196
                g_object_unref(priv->bus);
 
197
                priv->bus = NULL;
 
198
        }
 
199
 
 
200
        if (priv->bus_cancel != NULL) {
 
201
                g_cancellable_cancel(priv->bus_cancel);
 
202
                g_object_unref(priv->bus_cancel);
 
203
                priv->bus_cancel = NULL;
 
204
        }
 
205
 
 
206
        G_OBJECT_CLASS (session_dbus_parent_class)->dispose (object);
 
207
        return;
 
208
}
 
209
 
 
210
static void
 
211
session_dbus_finalize (GObject *object)
 
212
{
 
213
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);
 
214
 
 
215
        if (priv->name != NULL) {
 
216
                g_free(priv->name);
 
217
                priv->name = NULL;
 
218
        }
 
219
 
 
220
        G_OBJECT_CLASS (session_dbus_parent_class)->finalize (object);
 
221
        return;
 
222
}
 
223
 
 
224
static GVariant *
 
225
get_users_real_name (SessionDbus * service)
 
226
{
 
227
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(service);
 
228
        return g_variant_new ("(s)", priv->name);
 
229
}
 
230
 
 
231
SessionDbus *
 
232
session_dbus_new (void)
 
233
{
 
234
        return SESSION_DBUS(g_object_new(SESSION_DBUS_TYPE, NULL));
 
235
}
 
236
 
 
237
void
 
238
session_dbus_set_name (SessionDbus * session, const gchar * name)
 
239
{
 
240
}
 
241
 
 
242
void
 
243
session_dbus_set_users_real_name (SessionDbus * session, const gchar * name)
 
244
{
 
245
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
 
246
        GError * error = NULL;
 
247
        if (priv->name != NULL) {
 
248
                g_free(priv->name);
 
249
                priv->name = NULL;
 
250
        }
 
251
    
 
252
        priv->name = g_strdup(name);
 
253
 
 
254
        if (priv->bus != NULL) {
 
255
                g_dbus_connection_emit_signal (priv->bus,
 
256
                                   NULL,
 
257
                                   INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
 
258
                                   INDICATOR_SESSION_SERVICE_DBUS_IFACE,
 
259
                                   "UserRealNameUpdated",
 
260
                                   g_variant_new ("(s)", priv->name, NULL),
 
261
                                   &error);
 
262
 
 
263
                if (error != NULL) {
 
264
                        g_warning("Unable to send UserRealNameUpdated signal: %s", error->message);
 
265
                        g_error_free(error);
 
266
                        return;
 
267
                }
 
268
        }
 
269
        return;
 
270
}
 
271
 
 
272
void 
 
273
session_dbus_set_user_menu_visibility (SessionDbus* session,
 
274
                                       gboolean visible)
 
275
{
 
276
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
 
277
        GError * error = NULL;
 
278
    
 
279
        priv->user_menu_is_visible = visible;
 
280
 
 
281
        if (priv->bus != NULL) {
 
282
                g_dbus_connection_emit_signal (priv->bus,
 
283
                                   NULL,
 
284
                                   INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
 
285
                                   INDICATOR_SESSION_SERVICE_DBUS_IFACE,
 
286
                                   "UserMenuIsVisible",
 
287
                                   g_variant_new ("(b)", priv->user_menu_is_visible),
 
288
                                   &error);
 
289
 
 
290
                if (error != NULL) {
 
291
                        g_warning("Unable to send UserMenuIsVisible signal: %s", error->message);
 
292
                        g_error_free(error);
 
293
                }
 
294
        }  
 
295
}
 
296
 
 
297
void session_dbus_restart_required (SessionDbus* session)
 
298
{
 
299
        SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
 
300
        GError * error = NULL;
 
301
    
 
302
        if (priv->bus != NULL) {
 
303
    g_debug("About to send RebootRequired signal");
 
304
 
 
305
                g_dbus_connection_emit_signal (priv->bus,
 
306
                                   NULL,
 
307
                                   INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
 
308
                                   INDICATOR_SESSION_SERVICE_DBUS_IFACE,
 
309
                                   "RestartRequired",
 
310
                                   NULL,
 
311
                                   &error);
 
312
 
 
313
                if (error != NULL) {
 
314
                        g_warning("Unable to send reboot-required signal: %s", error->message);
 
315
                        g_error_free(error);
 
316
                }
 
317
        }  
 
318
  
 
319
}