aboutsummaryrefslogtreecommitdiff
path: root/src/status-provider-telepathy.c
blob: 2c6453f0d21c2b6df4a8c0877a8cdbe77fc7c617 (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
/*
A small wrapper utility to load indicators and put them as menu items
into the gnome-panel using it's applet interface.

Copyright 2009 Canonical Ltd.

Authors:
    Ted Gould <ted@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 "status-provider.h"
#include "status-provider-telepathy.h"
#include "status-provider-telepathy-marshal.h"

#include <dbus/dbus-glib.h>

typedef enum {
	MC_STATUS_UNSET,
	MC_STATUS_OFFLINE,
	MC_STATUS_AVAILABLE,
	MC_STATUS_AWAY,
	MC_STATUS_EXTENDED_AWAY,
	MC_STATUS_HIDDEN,
	MC_STATUS_DND
} mc_status_t;

static StatusProviderStatus mc_to_sp_map[] = {
	/* MC_STATUS_UNSET,         */  STATUS_PROVIDER_STATUS_OFFLINE,
	/* MC_STATUS_OFFLINE,       */  STATUS_PROVIDER_STATUS_OFFLINE,
	/* MC_STATUS_AVAILABLE,     */  STATUS_PROVIDER_STATUS_ONLINE,
	/* MC_STATUS_AWAY,          */  STATUS_PROVIDER_STATUS_AWAY,
	/* MC_STATUS_EXTENDED_AWAY, */  STATUS_PROVIDER_STATUS_AWAY,
	/* MC_STATUS_HIDDEN,        */  STATUS_PROVIDER_STATUS_INVISIBLE,
	/* MC_STATUS_DND            */  STATUS_PROVIDER_STATUS_DND
};

static mc_status_t sp_to_mc_map[] = {
	/* STATUS_PROVIDER_STATUS_ONLINE,  */  MC_STATUS_AVAILABLE,
	/* STATUS_PROVIDER_STATUS_AWAY,    */  MC_STATUS_AWAY,
	/* STATUS_PROVIDER_STATUS_DND      */  MC_STATUS_DND,
	/* STATUS_PROVIDER_STATUS_INVISIBLE*/  MC_STATUS_HIDDEN,
	/* STATUS_PROVIDER_STATUS_OFFLINE  */  MC_STATUS_OFFLINE
};

typedef struct _StatusProviderTelepathyPrivate StatusProviderTelepathyPrivate;
struct _StatusProviderTelepathyPrivate {
	DBusGProxy * proxy;
	mc_status_t  mc_status;
};

#define STATUS_PROVIDER_TELEPATHY_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), STATUS_PROVIDER_TELEPATHY_TYPE, StatusProviderTelepathyPrivate))

/* Prototypes */
/* GObject stuff */
static void status_provider_telepathy_class_init (StatusProviderTelepathyClass *klass);
static void status_provider_telepathy_init       (StatusProviderTelepathy *self);
static void status_provider_telepathy_dispose    (GObject *object);
static void status_provider_telepathy_finalize   (GObject *object);
/* Internal Funcs */
static void set_status (StatusProvider * sp, StatusProviderStatus status);
static StatusProviderStatus get_status (StatusProvider * sp);
static void changed_status (DBusGProxy * proxy, guint status, gchar * message, StatusProvider * sp);
static void proxy_destroy (DBusGProxy * proxy, StatusProvider * sp);

G_DEFINE_TYPE (StatusProviderTelepathy, status_provider_telepathy, STATUS_PROVIDER_TYPE);

static void
status_provider_telepathy_class_init (StatusProviderTelepathyClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (StatusProviderTelepathyPrivate));

	object_class->dispose = status_provider_telepathy_dispose;
	object_class->finalize = status_provider_telepathy_finalize;

	StatusProviderClass * spclass = STATUS_PROVIDER_CLASS(klass);

	spclass->set_status = set_status;
	spclass->get_status = get_status;

	return;
}


static void
status_provider_telepathy_init (StatusProviderTelepathy *self)
{
	StatusProviderTelepathyPrivate * priv = STATUS_PROVIDER_TELEPATHY_GET_PRIVATE(self);

	priv->proxy = NULL;
	priv->mc_status = MC_STATUS_OFFLINE;

	GError * error = NULL;

	DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);

	priv->proxy = NULL;
	priv->proxy = dbus_g_proxy_new_for_name_owner(session_bus,
	                         "org.freedesktop.Telepathy.MissionControl",
	                        "/org/freedesktop/Telepathy/MissionControl",
	                         "org.freedesktop.Telepathy.MissionControl",
							 &error);

	if (priv->proxy != NULL) {
		g_object_add_weak_pointer (G_OBJECT(priv->proxy), (gpointer *)&priv->proxy);
		g_signal_connect(G_OBJECT(priv->proxy), "destroy",
		                 G_CALLBACK(proxy_destroy), self);

		dbus_g_object_register_marshaller(_status_provider_telepathy_marshal_VOID__UINT_STRING,
		                            G_TYPE_NONE,
		                            G_TYPE_UINT,
		                            G_TYPE_STRING,
		                            G_TYPE_INVALID);
		dbus_g_proxy_add_signal    (priv->proxy,
		                            "PresenceChanged",
		                            G_TYPE_UINT,
		                            G_TYPE_STRING,
		                            G_TYPE_INVALID);
		dbus_g_proxy_connect_signal(priv->proxy,
		                            "PresenceChanged",
		                            G_CALLBACK(changed_status),
		                            (void *)self,
		                            NULL);
	} else {
		g_warning("Unable to connect to Mission Control");
		if (error != NULL) {
			g_error_free(error);
		}
	}

	return;
}

static void
status_provider_telepathy_dispose (GObject *object)
{
	StatusProviderTelepathyPrivate * priv = STATUS_PROVIDER_TELEPATHY_GET_PRIVATE(object);

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

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

static void
status_provider_telepathy_finalize (GObject *object)
{

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

/**
	status_provider_telepathy_new:

	Creates a new #StatusProviderTelepathy object.  No parameters or anything
	like that.  Just a convience function.

	Return value: A new instance of #StatusProviderTelepathy
*/
StatusProvider *
status_provider_telepathy_new (void)
{
	return STATUS_PROVIDER(g_object_new(STATUS_PROVIDER_TELEPATHY_TYPE, NULL));
}

static void
set_status (StatusProvider * sp, StatusProviderStatus status)
{
	StatusProviderTelepathyPrivate * priv = STATUS_PROVIDER_TELEPATHY_GET_PRIVATE(sp);
	if (priv->proxy == NULL) {
		priv->mc_status = MC_STATUS_OFFLINE;
		return;
	}

	priv->mc_status = sp_to_mc_map[status];	

	guint mcstatus = MC_STATUS_UNSET;
	gboolean ret = FALSE;
	GError * error = NULL;

	ret = dbus_g_proxy_call(priv->proxy,
	                        "GetPresence", &error,
	                        G_TYPE_INVALID,
	                        G_TYPE_UINT, &priv->mc_status,
	                        G_TYPE_INVALID);

	/* If we can't get the  get call to work, let's not set */
	if (!ret) {
		if (error != NULL) {
			g_error_free(error);
		}
		return;
	}
	
	/* If the get call doesn't return a status, that means that there
	   are no clients connected.  We don't want to connect them by telling
	   MC that we're going online -- we'd like to be more passive than that. */
	if (mcstatus == MC_STATUS_UNSET) {
		return;
	}

	ret = dbus_g_proxy_call(priv->proxy,
	                        "SetPresence", &error,
	                        G_TYPE_UINT, priv->mc_status,
	                        G_TYPE_STRING, "",
	                        G_TYPE_INVALID,
	                        G_TYPE_INVALID);

	if (!ret) {
		if (error != NULL) {
			g_warning("Unable to set Mission Control Presence: %s", error->message);
			g_error_free(error);
		} else {
			g_warning("Unable to set Mission Control Presence");
		}
		return;
	}

	return;
}

static StatusProviderStatus
get_status (StatusProvider * sp)
{
	StatusProviderTelepathyPrivate * priv = STATUS_PROVIDER_TELEPATHY_GET_PRIVATE(sp);

	if (priv->proxy == NULL) {
		return mc_to_sp_map[MC_STATUS_OFFLINE];
	}

	return mc_to_sp_map[priv->mc_status];
}

static void
changed_status (DBusGProxy * proxy, guint status, gchar * message, StatusProvider * sp)
{
	StatusProviderTelepathyPrivate * priv = STATUS_PROVIDER_TELEPATHY_GET_PRIVATE(sp);
	priv->mc_status = status;
	g_signal_emit(G_OBJECT(sp), STATUS_PROVIDER_SIGNAL_STATUS_CHANGED_ID, 0, mc_to_sp_map[priv->mc_status], TRUE);
}

static void
proxy_destroy (DBusGProxy * proxy, StatusProvider * sp)
{
	g_debug("Signal: Mission Control proxy destroyed");
	g_signal_emit(G_OBJECT(sp), STATUS_PROVIDER_SIGNAL_STATUS_CHANGED_ID, 0, STATUS_PROVIDER_STATUS_OFFLINE, TRUE);
	return;
}