5 #include <telepathy-glib/telepathy-glib.h>
7 #include <libaccounts-glib/ag-account.h>
9 #include "empathy-webcredentials-monitor.h"
11 G_DEFINE_TYPE (EmpathyWebcredentialsMonitor, empathy_webcredentials_monitor, G_TYPE_OBJECT)
13 #define WEBCRED_BUS_NAME "com.canonical.indicators.webcredentials"
14 #define WEBCRED_PATH "/com/canonical/indicators/webcredentials"
15 #define WEBCRED_IFACE "com.canonical.indicators.webcredentials"
17 #define FAILURES_PROP "Failures"
32 static guint signals[LAST_SIGNAL];
34 struct _EmpathyWebcredentialsMonitorPriv
39 /* array of owned AgAccount */
44 empathy_webcredentials_monitor_get_property (GObject *object,
49 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
54 g_value_set_object (value, self->priv->manager);
57 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
63 empathy_webcredentials_monitor_set_property (GObject *object,
68 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
73 g_assert (self->priv->manager == NULL); /* construct only */
74 self->priv->manager = g_value_dup_object (value);
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83 empathy_webcredentials_monitor_constructed (GObject *object)
85 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
86 void (*chain_up) (GObject *) =
87 ((GObjectClass *) empathy_webcredentials_monitor_parent_class)->constructed;
91 g_assert (AG_IS_MANAGER (self->priv->manager));
95 empathy_webcredentials_monitor_dispose (GObject *object)
97 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
98 void (*chain_up) (GObject *) =
99 ((GObjectClass *) empathy_webcredentials_monitor_parent_class)->dispose;
101 g_clear_object (&self->priv->manager);
102 g_clear_object (&self->priv->proxy);
108 empathy_webcredentials_monitor_finalize (GObject *object)
110 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
111 void (*chain_up) (GObject *) =
112 ((GObjectClass *) empathy_webcredentials_monitor_parent_class)->finalize;
114 g_ptr_array_unref (self->priv->failures);
120 empathy_webcredentials_monitor_class_init (
121 EmpathyWebcredentialsMonitorClass *klass)
123 GObjectClass *oclass = G_OBJECT_CLASS (klass);
126 oclass->get_property = empathy_webcredentials_monitor_get_property;
127 oclass->set_property = empathy_webcredentials_monitor_set_property;
128 oclass->constructed = empathy_webcredentials_monitor_constructed;
129 oclass->dispose = empathy_webcredentials_monitor_dispose;
130 oclass->finalize = empathy_webcredentials_monitor_finalize;
132 spec = g_param_spec_object ("manager", "Manager",
135 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
136 g_object_class_install_property (oclass, PROP_MANAGER, spec);
138 signals[SIG_FAILURE_ADDED] = g_signal_new ("failure-added",
139 G_OBJECT_CLASS_TYPE (klass),
145 signals[SIG_FAILURE_REMOVED] = g_signal_new ("failure-removed",
146 G_OBJECT_CLASS_TYPE (klass),
152 g_type_class_add_private (klass, sizeof (EmpathyWebcredentialsMonitorPriv));
156 update_failures (EmpathyWebcredentialsMonitor *self)
158 GVariant *failures, *f;
160 GList *new_list = NULL;
163 failures = g_dbus_proxy_get_cached_property (self->priv->proxy,
165 if (failures == NULL)
167 g_debug ("Does not implement Failures property");
171 g_variant_iter_init (&iter, failures);
172 while ((f = g_variant_iter_next_value (&iter)) != NULL)
177 id = g_variant_get_uint32 (f);
179 account = ag_manager_get_account (self->priv->manager, id);
183 /* Pass ownership of 'account' to the list */
184 new_list = g_list_append (new_list, account);
186 if (!tp_g_ptr_array_contains (self->priv->failures, account))
188 g_ptr_array_add (self->priv->failures, g_object_ref (account));
190 g_signal_emit (self, signals[SIG_FAILURE_ADDED], 0, account);
196 g_variant_unref (failures);
198 for (i = 0; i < self->priv->failures->len; i++)
200 AgAccount *account = g_ptr_array_index (self->priv->failures, i);
202 if (g_list_find (new_list, account) == NULL)
204 g_object_ref (account);
205 g_ptr_array_remove (self->priv->failures, account);
207 g_signal_emit (self, signals[SIG_FAILURE_REMOVED], 0, account);
208 g_object_unref (account);
212 g_list_free_full (new_list, g_object_unref);
216 properties_changed_cb (GDBusProxy *proxy,
217 GVariant *changed_properties,
218 GStrv invalidated_properties,
219 EmpathyWebcredentialsMonitor *self)
221 if (g_variant_lookup_value (changed_properties, FAILURES_PROP, NULL) == NULL)
224 update_failures (self);
228 proxy_new_cb (GObject *source,
229 GAsyncResult *result,
232 EmpathyWebcredentialsMonitor *self;
233 TpWeakRef *wr = user_data;
234 GError *error = NULL;
236 self = tp_weak_ref_dup_object (wr);
240 self->priv->proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
241 if (self->priv->proxy == NULL)
243 g_debug ("Failed to create webcredentials proxy: %s", error->message);
244 g_error_free (error);
248 update_failures (self);
250 g_signal_connect (self->priv->proxy, "g-properties-changed",
251 G_CALLBACK (properties_changed_cb), self);
254 tp_weak_ref_destroy (wr);
255 g_clear_object (&self);
259 empathy_webcredentials_monitor_init (EmpathyWebcredentialsMonitor *self)
261 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
262 EMPATHY_TYPE_WEBCREDENTIALS_MONITOR, EmpathyWebcredentialsMonitorPriv);
264 self->priv->failures = g_ptr_array_new_with_free_func (g_object_unref);
266 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL,
267 WEBCRED_BUS_NAME, WEBCRED_PATH, WEBCRED_IFACE,
268 NULL, proxy_new_cb, tp_weak_ref_new (self, NULL, NULL));
271 EmpathyWebcredentialsMonitor *
272 empathy_webcredentials_monitor_new (AgManager *manager)
274 return g_object_new (EMPATHY_TYPE_WEBCREDENTIALS_MONITOR,
280 empathy_webcredentials_get_failures (EmpathyWebcredentialsMonitor *self)
282 return self->priv->failures;