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 "dbus-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
gboolean user_menu_is_visible;
42
GDBusConnection * bus;
43
GCancellable * bus_cancel;
44
guint dbus_registration;
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 */
56
#define SESSION_DBUS_GET_PRIVATE(o) \
57
(G_TYPE_INSTANCE_GET_PRIVATE ((o), SESSION_DBUS_TYPE, SessionDbusPrivate))
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);
64
G_DEFINE_TYPE (SessionDbus, session_dbus, G_TYPE_OBJECT);
67
session_dbus_class_init (SessionDbusClass *klass)
69
GObjectClass *object_class = G_OBJECT_CLASS (klass);
71
g_type_class_add_private (klass, sizeof (SessionDbusPrivate));
73
object_class->dispose = session_dbus_dispose;
74
object_class->finalize = session_dbus_finalize;
76
/* Setting up the DBus interfaces */
77
if (node_info == NULL) {
78
GError * error = NULL;
80
node_info = g_dbus_node_info_new_for_xml(_session_dbus, &error);
82
g_error("Unable to parse Session Service Interface description: %s", error->message);
87
if (interface_info == NULL) {
88
interface_info = g_dbus_node_info_lookup_interface(node_info, INDICATOR_SESSION_SERVICE_DBUS_IFACE);
90
if (interface_info == NULL) {
91
g_error("Unable to find interface '" INDICATOR_SESSION_SERVICE_DBUS_IFACE "'");
99
session_dbus_init (SessionDbus *self)
101
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(self);
105
priv->bus_cancel = NULL;
106
priv->dbus_registration = 0;
107
priv->user_menu_is_visible = FALSE;
109
priv->bus_cancel = g_cancellable_new();
110
g_bus_get(G_BUS_TYPE_SESSION,
119
bus_get_cb (GObject * object, GAsyncResult * res, gpointer user_data)
121
GError * error = NULL;
122
GDBusConnection * connection = g_bus_get_finish(res, &error);
125
g_error("OMG! Unable to get a connection to DBus: %s", error->message);
130
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(user_data);
132
g_warn_if_fail(priv->bus == NULL);
133
priv->bus = connection;
135
if (priv->bus_cancel != NULL) {
136
g_object_unref(priv->bus_cancel);
137
priv->bus_cancel = NULL;
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,
150
g_error("Unable to register the object to DBus: %s", error->message);
158
/* A method has been called from our dbus inteface. Figure out what it
159
is and dispatch it. */
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)
166
SessionDbus * service = SESSION_DBUS (user_data);
167
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE (service);
169
GVariant * retval = NULL;
171
if (g_strcmp0(method, "GetUserRealName") == 0) {
172
retval = get_users_real_name (service);
174
else if (g_strcmp0 (method, "GetUserMenuVisibility") == 0){
175
retval = g_variant_new ("(b)", priv->user_menu_is_visible);
178
g_warning("Calling method '%s' on the indicator service and it's unknown", method);
180
g_dbus_method_invocation_return_value(invocation, retval);
185
session_dbus_dispose (GObject *object)
187
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);
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;
195
if (priv->bus != NULL) {
196
g_object_unref(priv->bus);
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;
206
G_OBJECT_CLASS (session_dbus_parent_class)->dispose (object);
211
session_dbus_finalize (GObject *object)
213
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(object);
215
if (priv->name != NULL) {
220
G_OBJECT_CLASS (session_dbus_parent_class)->finalize (object);
225
get_users_real_name (SessionDbus * service)
227
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(service);
228
return g_variant_new ("(s)", priv->name);
232
session_dbus_new (void)
234
return SESSION_DBUS(g_object_new(SESSION_DBUS_TYPE, NULL));
238
session_dbus_set_name (SessionDbus * session, const gchar * name)
243
session_dbus_set_users_real_name (SessionDbus * session, const gchar * name)
245
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
246
GError * error = NULL;
247
if (priv->name != NULL) {
252
priv->name = g_strdup(name);
254
if (priv->bus != NULL) {
255
g_dbus_connection_emit_signal (priv->bus,
257
INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
258
INDICATOR_SESSION_SERVICE_DBUS_IFACE,
259
"UserRealNameUpdated",
260
g_variant_new ("(s)", priv->name, NULL),
264
g_warning("Unable to send UserRealNameUpdated signal: %s", error->message);
273
session_dbus_set_user_menu_visibility (SessionDbus* session,
276
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
277
GError * error = NULL;
279
priv->user_menu_is_visible = visible;
281
if (priv->bus != NULL) {
282
g_dbus_connection_emit_signal (priv->bus,
284
INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
285
INDICATOR_SESSION_SERVICE_DBUS_IFACE,
287
g_variant_new ("(b)", priv->user_menu_is_visible),
291
g_warning("Unable to send UserMenuIsVisible signal: %s", error->message);
297
void session_dbus_restart_required (SessionDbus* session)
299
SessionDbusPrivate * priv = SESSION_DBUS_GET_PRIVATE(session);
300
GError * error = NULL;
302
if (priv->bus != NULL) {
303
g_debug("About to send RebootRequired signal");
305
g_dbus_connection_emit_signal (priv->bus,
307
INDICATOR_SESSION_SERVICE_DBUS_OBJECT,
308
INDICATOR_SESSION_SERVICE_DBUS_IFACE,
314
g_warning("Unable to send reboot-required signal: %s", error->message);