~indicator-applet-developers/indicator-session/trunk.0.1

« back to all changes in this revision

Viewing changes to src/status-provider-mc5.c

  • Committer: Ted Gould
  • Date: 2009-10-10 12:41:23 UTC
  • mfrom: (55.1.7 mc5fix)
  • Revision ID: ted@canonical.com-20091010124123-xqr113phm185xga3
Listening for MC5 coming on and off the bus as EmpathyAccountManager was doing that for us before.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include "status-provider-mc5-marshal.h"
32
32
 
33
33
#include <dbus/dbus-glib.h>
 
34
#include <dbus/dbus-glib-bindings.h>
34
35
 
35
36
static gchar * sp_to_mc_map[STATUS_PROVIDER_STATUS_LAST] = {
36
37
        /* STATUS_PROVIDER_STATUS_ONLINE,  */  "available",
66
67
struct _StatusProviderMC5Private {
67
68
        EmpathyAccountManager * manager;
68
69
        StatusProviderStatus status;
 
70
        DBusGProxy * dbus_proxy;
69
71
};
70
72
 
71
73
#define STATUS_PROVIDER_MC5_GET_PRIVATE(o) \
72
74
(G_TYPE_INSTANCE_GET_PRIVATE ((o), STATUS_PROVIDER_MC5_TYPE, StatusProviderMC5Private))
 
75
#define MC5_WELL_KNOWN_NAME  "org.freedesktop.Telepathy.MissionControl5"
73
76
 
74
77
/* Prototypes */
75
78
/* GObject stuff */
81
84
static void set_status (StatusProvider * sp, StatusProviderStatus status);
82
85
static StatusProviderStatus get_status (StatusProvider * sp);
83
86
static void presence_changed (EmpathyAccountManager * eam, guint type, const gchar * type_str, const gchar * message, StatusProviderMC5 * sp);
 
87
static void dbus_namechange (DBusGProxy * proxy, const gchar * name, const gchar * prev, const gchar * new, StatusProviderMC5 * self);
 
88
static void mc5_exists_cb (DBusGProxy * proxy, gboolean exists, GError * error, gpointer userdata);
84
89
 
85
90
G_DEFINE_TYPE (StatusProviderMC5, status_provider_mc5, STATUS_PROVIDER_TYPE);
86
91
 
104
109
        return;
105
110
}
106
111
 
 
112
/* Build our empathy account manager instance if we don't
 
113
   have one. */
 
114
static void
 
115
build_eam (StatusProviderMC5 * self)
 
116
{
 
117
        StatusProviderMC5Private * priv = STATUS_PROVIDER_MC5_GET_PRIVATE(self);
 
118
 
 
119
        if (priv->manager != NULL) {
 
120
                return;
 
121
        }
 
122
 
 
123
        priv->manager = EMPATHY_ACCOUNT_MANAGER(g_object_new(EMPATHY_TYPE_ACCOUNT_MANAGER, NULL));
 
124
        g_signal_connect(G_OBJECT(priv->manager), "global-presence-changed", G_CALLBACK(presence_changed), self);
 
125
 
 
126
        return;
 
127
}
 
128
 
107
129
/* Creating an instance of the status provider.  We set the variables
108
130
   and create an EmpathyAccountManager object.  It does all the hard
109
131
   work in this module of tracking MissionControl and enumerating the
116
138
        priv->status = STATUS_PROVIDER_STATUS_DISCONNECTED;
117
139
        priv->manager = NULL;
118
140
 
119
 
        g_signal_connect(G_OBJECT(priv->manager), "global-presence-changed", G_CALLBACK(presence_changed), self);
 
141
        DBusGConnection * bus = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
 
142
        g_return_if_fail(bus != NULL); /* Can't do anymore DBus stuff without this,
 
143
                                          all non-DBus stuff should be done */
 
144
 
 
145
        GError * error = NULL;
 
146
 
 
147
        /* Set up the dbus Proxy */
 
148
        priv->dbus_proxy = dbus_g_proxy_new_for_name_owner (bus,
 
149
                                                            DBUS_SERVICE_DBUS,
 
150
                                                            DBUS_PATH_DBUS,
 
151
                                                            DBUS_INTERFACE_DBUS,
 
152
                                                            &error);
 
153
        if (error != NULL) {
 
154
                g_warning("Unable to connect to DBus events: %s", error->message);
 
155
                g_error_free(error);
 
156
                return;
 
157
        }
 
158
 
 
159
        /* Configure the name owner changing */
 
160
        dbus_g_proxy_add_signal(priv->dbus_proxy, "NameOwnerChanged",
 
161
                                G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
 
162
                                                        G_TYPE_INVALID);
 
163
        dbus_g_proxy_connect_signal(priv->dbus_proxy, "NameOwnerChanged",
 
164
                                G_CALLBACK(dbus_namechange),
 
165
                                self, NULL);
 
166
 
 
167
        org_freedesktop_DBus_name_has_owner_async(priv->dbus_proxy, MC5_WELL_KNOWN_NAME, mc5_exists_cb, self);
120
168
 
121
169
        return;
122
170
}
133
181
                priv->manager = NULL;
134
182
        }
135
183
 
 
184
        if (priv->dbus_proxy != NULL) {
 
185
                g_object_unref(priv->dbus_proxy);
 
186
                priv->dbus_proxy = NULL;
 
187
        }
 
188
 
136
189
        G_OBJECT_CLASS (status_provider_mc5_parent_class)->dispose (object);
137
190
        return;
138
191
}
146
199
        return;
147
200
}
148
201
 
 
202
/* Watch for MC5 Coming on and off the bus. */
 
203
static void
 
204
dbus_namechange (DBusGProxy * proxy, const gchar * name, const gchar * prev, const gchar * new, StatusProviderMC5 * self)
 
205
{
 
206
        /* g_debug("DBUS NAMECHANGE: %s %s %s", name, prev, new); */
 
207
 
 
208
        if (prev[0] == '\0' && g_strcmp0(name, MC5_WELL_KNOWN_NAME) == 0) {
 
209
                g_debug("MC5 Coming online");
 
210
                build_eam(self);
 
211
        }
 
212
        if (new[0] == '\0' && g_strcmp0(name, MC5_WELL_KNOWN_NAME) == 0) {
 
213
                g_debug("MC5 going offline");
 
214
                StatusProviderMC5Private * priv = STATUS_PROVIDER_MC5_GET_PRIVATE(self);
 
215
                if (priv->manager != NULL) {
 
216
                        g_object_unref(priv->manager);
 
217
                        priv->manager = NULL;
 
218
                }
 
219
 
 
220
                priv->status = STATUS_PROVIDER_STATUS_DISCONNECTED;
 
221
                g_signal_emit(G_OBJECT(self), STATUS_PROVIDER_SIGNAL_STATUS_CHANGED_ID, 0, priv->status, TRUE);
 
222
        }
 
223
 
 
224
        return;
 
225
}
 
226
 
 
227
/* Callback for the Dbus command to do HasOwner on
 
228
   the MC5 service.  If it exists, we want to have an
 
229
   account manager. */
 
230
static void
 
231
mc5_exists_cb (DBusGProxy * proxy, gboolean exists, GError * error, gpointer userdata)
 
232
{
 
233
        if (error) {
 
234
                g_warning("Unable to check if MC5 is running: %s", error->message);
 
235
                return;
 
236
        }
 
237
 
 
238
        if (exists) {
 
239
                build_eam(STATUS_PROVIDER_MC5(userdata));
 
240
        }
 
241
 
 
242
        return;
 
243
}
 
244
 
149
245
/**
150
246
        status_provider_mc5_new:
151
247
 
168
264
set_status (StatusProvider * sp, StatusProviderStatus status)
169
265
{
170
266
        StatusProviderMC5Private * priv = STATUS_PROVIDER_MC5_GET_PRIVATE(sp);
171
 
        if (priv->manager == NULL) {
172
 
                priv->manager = EMPATHY_ACCOUNT_MANAGER(g_object_new(EMPATHY_TYPE_ACCOUNT_MANAGER, NULL));
173
 
        }
 
267
 
 
268
        build_eam(STATUS_PROVIDER_MC5(sp));
174
269
 
175
270
        empathy_account_manager_request_global_presence(priv->manager, sp_to_tp_map[status], sp_to_mc_map[status], "");
176
271