2 #include "empathy-webcredentials-monitor.h"
5 #include <telepathy-glib/telepathy-glib.h>
6 #include <libaccounts-glib/ag-account.h>
8 G_DEFINE_TYPE (EmpathyWebcredentialsMonitor, empathy_webcredentials_monitor, G_TYPE_OBJECT)
10 #define WEBCRED_BUS_NAME "com.canonical.indicators.webcredentials"
11 #define WEBCRED_PATH "/com/canonical/indicators/webcredentials"
12 #define WEBCRED_IFACE "com.canonical.indicators.webcredentials"
14 #define FAILURES_PROP "Failures"
29 static guint signals[LAST_SIGNAL];
31 struct _EmpathyWebcredentialsMonitorPriv
36 /* array of owned AgAccount */
41 empathy_webcredentials_monitor_get_property (GObject *object,
46 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
51 g_value_set_object (value, self->priv->manager);
54 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
60 empathy_webcredentials_monitor_set_property (GObject *object,
65 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
70 g_assert (self->priv->manager == NULL); /* construct only */
71 self->priv->manager = g_value_dup_object (value);
74 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
80 empathy_webcredentials_monitor_constructed (GObject *object)
82 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
83 void (*chain_up) (GObject *) =
84 ((GObjectClass *) empathy_webcredentials_monitor_parent_class)->constructed;
88 g_assert (AG_IS_MANAGER (self->priv->manager));
92 empathy_webcredentials_monitor_dispose (GObject *object)
94 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
95 void (*chain_up) (GObject *) =
96 ((GObjectClass *) empathy_webcredentials_monitor_parent_class)->dispose;
98 g_clear_object (&self->priv->manager);
99 g_clear_object (&self->priv->proxy);
105 empathy_webcredentials_monitor_finalize (GObject *object)
107 EmpathyWebcredentialsMonitor *self = EMPATHY_WEBCREDENTIALS_MONITOR (object);
108 void (*chain_up) (GObject *) =
109 ((GObjectClass *) empathy_webcredentials_monitor_parent_class)->finalize;
111 g_ptr_array_unref (self->priv->failures);
117 empathy_webcredentials_monitor_class_init (
118 EmpathyWebcredentialsMonitorClass *klass)
120 GObjectClass *oclass = G_OBJECT_CLASS (klass);
123 oclass->get_property = empathy_webcredentials_monitor_get_property;
124 oclass->set_property = empathy_webcredentials_monitor_set_property;
125 oclass->constructed = empathy_webcredentials_monitor_constructed;
126 oclass->dispose = empathy_webcredentials_monitor_dispose;
127 oclass->finalize = empathy_webcredentials_monitor_finalize;
129 spec = g_param_spec_object ("manager", "Manager",
132 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
133 g_object_class_install_property (oclass, PROP_MANAGER, spec);
135 signals[SIG_FAILURE_ADDED] = g_signal_new ("failure-added",
136 G_OBJECT_CLASS_TYPE (klass),
142 signals[SIG_FAILURE_REMOVED] = g_signal_new ("failure-removed",
143 G_OBJECT_CLASS_TYPE (klass),
149 g_type_class_add_private (klass, sizeof (EmpathyWebcredentialsMonitorPriv));
153 update_failures (EmpathyWebcredentialsMonitor *self)
155 GVariant *failures, *f;
157 GList *new_list = NULL;
160 failures = g_dbus_proxy_get_cached_property (self->priv->proxy,
162 if (failures == NULL)
164 g_debug ("Does not implement Failures property");
168 g_variant_iter_init (&iter, failures);
169 while ((f = g_variant_iter_next_value (&iter)) != NULL)
174 id = g_variant_get_uint32 (f);
176 account = ag_manager_get_account (self->priv->manager, id);
180 /* Pass ownership of 'account' to the list */
181 new_list = g_list_append (new_list, account);
183 if (!tp_g_ptr_array_contains (self->priv->failures, account))
185 g_ptr_array_add (self->priv->failures, g_object_ref (account));
187 g_signal_emit (self, signals[SIG_FAILURE_ADDED], 0, account);
193 g_variant_unref (failures);
195 for (i = 0; i < self->priv->failures->len; i++)
197 AgAccount *account = g_ptr_array_index (self->priv->failures, i);
199 if (g_list_find (new_list, account) == NULL)
201 g_object_ref (account);
202 g_ptr_array_remove (self->priv->failures, account);
204 g_signal_emit (self, signals[SIG_FAILURE_REMOVED], 0, account);
205 g_object_unref (account);
209 g_list_free_full (new_list, g_object_unref);
213 properties_changed_cb (GDBusProxy *proxy,
214 GVariant *changed_properties,
215 GStrv invalidated_properties,
216 EmpathyWebcredentialsMonitor *self)
218 if (g_variant_lookup_value (changed_properties, FAILURES_PROP, NULL) == NULL)
221 update_failures (self);
225 proxy_new_cb (GObject *source,
226 GAsyncResult *result,
229 EmpathyWebcredentialsMonitor *self;
230 TpWeakRef *wr = user_data;
231 GError *error = NULL;
233 self = tp_weak_ref_dup_object (wr);
237 self->priv->proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
238 if (self->priv->proxy == NULL)
240 g_debug ("Failed to create webcredentials proxy: %s", error->message);
241 g_error_free (error);
245 update_failures (self);
247 g_signal_connect (self->priv->proxy, "g-properties-changed",
248 G_CALLBACK (properties_changed_cb), self);
251 tp_weak_ref_destroy (wr);
252 g_clear_object (&self);
256 empathy_webcredentials_monitor_init (EmpathyWebcredentialsMonitor *self)
258 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
259 EMPATHY_TYPE_WEBCREDENTIALS_MONITOR, EmpathyWebcredentialsMonitorPriv);
261 self->priv->failures = g_ptr_array_new_with_free_func (g_object_unref);
263 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL,
264 WEBCRED_BUS_NAME, WEBCRED_PATH, WEBCRED_IFACE,
265 NULL, proxy_new_cb, tp_weak_ref_new (self, NULL, NULL));
268 EmpathyWebcredentialsMonitor *
269 empathy_webcredentials_monitor_new (AgManager *manager)
271 return g_object_new (EMPATHY_TYPE_WEBCREDENTIALS_MONITOR,
277 empathy_webcredentials_get_failures (EmpathyWebcredentialsMonitor *self)
279 return self->priv->failures;