Track contacts added/removed in aggregator
authorArtem Garmash <artemgarmash@gmail.com>
Sun, 31 Jan 2010 00:00:10 +0000 (02:00 +0200)
committerArtem Garmash <artem.garmash@nokia.com>
Sun, 27 Jun 2010 19:13:45 +0000 (22:13 +0300)
src/el-home-applet.c

index 9e06024..4e950cf 100644 (file)
@@ -614,8 +614,83 @@ presence_updated (OssoABookPresence *presence,
         update_presence_pixbuf (self, presence);
 }
 
+
 static void
-clean_state (ELHomeApplet *self)
+resolve_contact (ELHomeApplet *self)
+{
+        ELHomeAppletPrivate *priv = self->priv;
+        GList *contacts = NULL;
+
+        if (priv->contact_id) {
+                contacts = osso_abook_aggregator_lookup
+                        (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
+                         priv->contact_id);
+        }
+        else if (priv->local_id && priv->remote_id) {
+                if (g_strcmp0 (priv->local_id, "ring/tel/ring" == 0)) {
+                        contacts = osso_abook_aggregator_find_contacts_for_phone_number
+                                (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
+                                 priv->remote_id,
+                                 TRUE);
+                }
+                else {
+                        McAccount *account;
+                        account = osso_abook_account_manager_lookup_by_name
+                                (NULL,
+                                 priv->local_id);
+                        if (account) {
+                                contacts = osso_abook_aggregator_find_contacts_for_im_contact
+                                        (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
+                                         priv->remote_id,
+                                         account);
+                        }
+                }
+        }
+
+        if (contacts && contacts->data) {
+                priv->contact = g_object_ref (OSSO_ABOOK_CONTACT (contacts->data));
+                gtk_label_set_text (GTK_LABEL (priv->sender),
+                                    osso_abook_contact_get_display_name (priv->contact));
+
+                priv->avatar_pixbuf = osso_abook_avatar_get_image_scaled
+                        (OSSO_ABOOK_AVATAR (priv->contact),
+                         HILDON_ICON_PIXEL_SIZE_THUMB,
+                         HILDON_ICON_PIXEL_SIZE_THUMB,
+                         TRUE);
+
+                g_warning ("HAVE avatar");
+                if (priv->avatar_pixbuf) {
+                        gtk_widget_set_size_request (priv->sender,
+                                                     C_WIDTH - AVATAR_SIZE - HILDON_MARGIN_DEFAULT,
+                                                     HILDON_ICON_PIXEL_SIZE_THUMB);
+
+                }
+
+                update_presence_pixbuf (self,
+                                        OSSO_ABOOK_PRESENCE (priv->contact));
+                g_signal_connect (priv->contact,
+                                  "notify::presence-status",
+                                  G_CALLBACK (presence_updated),
+                                  self);
+                gtk_widget_queue_draw (GTK_WIDGET (self));
+        }
+
+}
+
+static void
+contacts_added (OssoABookRoster  *roster,
+                OssoABookContact **contacts,
+                gpointer          userdata)
+{
+        ELHomeApplet *self = EL_HOME_APPLET (userdata);
+        ELHomeAppletPrivate *priv = self->priv;
+
+        if (!priv->contact)
+                resolve_contact (self);
+}
+
+static void
+reset_contact (ELHomeApplet *self)
 {
         ELHomeAppletPrivate *priv = self->priv;
 
@@ -635,6 +710,51 @@ clean_state (ELHomeApplet *self)
                 priv->presence_pixbuf = NULL;
         }
 
+        if (priv->contact) {
+                g_signal_handlers_disconnect_by_func (priv->contact,
+                                                      presence_updated,
+                                                      self);
+                g_object_unref (priv->contact);
+                priv->contact = NULL;
+        }
+}
+
+static void
+contacts_removed (OssoABookRoster *roster,
+                  const gchar     **ids,
+                  gpointer         userdata)
+{
+        ELHomeApplet *self = EL_HOME_APPLET (userdata);
+        ELHomeAppletPrivate *priv = self->priv;
+
+        if (priv->contact) {
+                const gchar **contact_id;
+                const gchar *uid = osso_abook_contact_get_uid (priv->contact);
+
+                for (contact_id = ids; *contact_id; contact_id++) {
+                        if (strcmp (*contact_id, priv->contact_id) == 0) {
+                                reset_contact (self);
+
+                                gtk_widget_queue_draw (GTK_WIDGET (self));
+                                return;
+                        }
+                        if (strcmp (*contact_id, uid) == 0) {
+                                reset_contact (self);
+                                resolve_contact (self);
+                                gtk_widget_queue_draw (GTK_WIDGET (self));
+                                return;
+                        }
+                }
+        }
+}
+
+static void
+clean_state (ELHomeApplet *self)
+{
+        ELHomeAppletPrivate *priv = self->priv;
+
+        reset_contact (self);
+
         if (priv->message) {
                 g_free (priv->message);
                 priv->message = NULL;
@@ -657,19 +777,18 @@ clean_state (ELHomeApplet *self)
                 priv->group_uid = NULL;
         }
 
-        if (priv->contact) {
-                g_signal_handlers_disconnect_by_func (priv->contact,
-                                                      presence_updated,
-                                                      self);
-                g_object_unref (priv->contact);
-                priv->contact = NULL;
-        }
         if (priv->aggregator) {
                 if (priv->aggregator_ready_closure){
                         osso_abook_waitable_cancel (OSSO_ABOOK_WAITABLE (priv->aggregator),
                                                     priv->aggregator_ready_closure);
                         priv->aggregator_ready_closure = NULL;
                 }
+                g_signal_handlers_disconnect_by_func (priv->aggregator,
+                                                      contacts_added,
+                                                      self);
+                g_signal_handlers_disconnect_by_func (priv->aggregator,
+                                                      contacts_removed,
+                                                      self);
                 osso_abook_roster_stop (priv->aggregator);
                 g_object_unref (priv->aggregator);
                 priv->aggregator = NULL;
@@ -711,10 +830,9 @@ aggregator_ready_cb (OssoABookWaitable *waitable,
                      const GError      *error,
                      gpointer           userdata)
 {
-        ELHomeApplet *self = EL_HOME_APPLET(userdata);
+        ELHomeApplet *self = EL_HOME_APPLET (userdata);
         ELHomeAppletPrivate *priv = self->priv;
-        GList *contacts = NULL;
-                g_warning (G_STRFUNC);
+
         priv->aggregator_ready_closure = NULL;
 
         if (error) {
@@ -722,63 +840,20 @@ aggregator_ready_cb (OssoABookWaitable *waitable,
                 return;
         }
 
-        if (priv->contact_id) {
-                contacts = osso_abook_aggregator_lookup
-                        (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
-                         priv->contact_id);
-        }
-        else if (priv->local_id && priv->remote_id) {
-                if (g_strcmp0 (priv->local_id, "ring/tel/ring" == 0)) {
-                        contacts = osso_abook_aggregator_find_contacts_for_phone_number
-                                (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
-                                 priv->remote_id,
-                                 TRUE);
-                }
-                else {
-                        McAccount *account;
-                        account = osso_abook_account_manager_lookup_by_name
-                                (NULL,
-                                 priv->local_id);
-                        if (account) {
-                                contacts = osso_abook_aggregator_find_contacts_for_im_contact
-                                        (OSSO_ABOOK_AGGREGATOR (priv->aggregator),
-                                         priv->remote_id,
-                                         account);
-                        }
-                }
-        }
-
-        if (contacts && contacts->data) {
-                priv->contact = g_object_ref (OSSO_ABOOK_CONTACT (contacts->data));
-                gtk_label_set_text (GTK_LABEL (priv->sender),
-                                    osso_abook_contact_get_display_name (priv->contact));
-
-                priv->avatar_pixbuf = osso_abook_avatar_get_image_scaled
-                        (OSSO_ABOOK_AVATAR (priv->contact),
-                         HILDON_ICON_PIXEL_SIZE_THUMB,
-                         HILDON_ICON_PIXEL_SIZE_THUMB,
-                         TRUE);
-
-                g_warning ("HAVE avatar");
-                if (priv->avatar_pixbuf) {
-                        gtk_widget_set_size_request (priv->sender,
-                                                     C_WIDTH - AVATAR_SIZE - HILDON_MARGIN_DEFAULT,
-                                                     HILDON_ICON_PIXEL_SIZE_THUMB);
-
-                }
+        g_signal_connect (priv->aggregator,
+                          "contacts-added",
+                          G_CALLBACK (contacts_added),
+                          self);
+        g_signal_connect (priv->aggregator,
+                          "contacts-removed",
+                          G_CALLBACK (contacts_removed),
+                          self);
 
-                update_presence_pixbuf (self,
-                                        OSSO_ABOOK_PRESENCE (priv->contact));
-                g_signal_connect (priv->contact,
-                                  "notify::presence-status",
-                                  G_CALLBACK (presence_updated),
-                                  self);
-                gtk_widget_queue_draw (GTK_WIDGET (self));
-        }
+        resolve_contact (self);
 }
 
 static void
-resolve_contact (ELHomeApplet *self)
+start_aggregator (ELHomeApplet *self)
 {
         ELHomeAppletPrivate *priv = self->priv;
         EBookQuery *query = NULL;
@@ -1012,7 +1087,7 @@ read_event (ELHomeApplet *self)
 
         it = make_query (priv->eventlogger, -1);
         show_event (self, it);
-        resolve_contact (self);
+        start_aggregator (self);
 
         if (g_strcmp0 (priv->local_id, "ring/tel/ring") == 0) {
                 icon_name = "general_sms";