Reuse contact object if it's the same for a new message
[conv-inbox] / src / el-home-applet.c
index 582d635..c21f681 100644 (file)
@@ -32,7 +32,6 @@
 #include <libosso-abook/osso-abook-contact.h>
 #include <libosso-abook/osso-abook-waitable.h>
 #include <libosso-abook/osso-abook-presence.h>
-#include <libosso-abook/osso-abook-avatar.h>
 #include <libosso-abook/osso-abook-touch-contact-starter.h>
 #include <libosso-abook/osso-abook-temporary-contact-dialog.h>
 #include <libosso-abook/osso-abook-account-manager.h>
@@ -105,7 +104,6 @@ struct _ELHomeAppletPrivate
         GtkWidget *unread;
         GtkWidget *received;
         GtkWidget *cut_message;
-        GtkWidget *avatar;
 
         /* empty view*/
         GtkWidget *empty;
@@ -643,6 +641,16 @@ presence_updated (OssoABookPresence *presence,
         update_presence_pixbuf (self, presence);
 }
 
+static void
+show_contact (ELHomeApplet *self)
+{
+        ELHomeAppletPrivate *priv = self->priv;
+
+        gtk_label_set_text (GTK_LABEL (priv->sender),
+                            osso_abook_contact_get_display_name (priv->contact));
+        resize_sender (priv);
+        gtk_widget_queue_draw (GTK_WIDGET (self));
+}
 
 static void
 resolve_contact (ELHomeApplet *self)
@@ -678,26 +686,19 @@ resolve_contact (ELHomeApplet *self)
 
         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));
-
+                g_signal_connect (priv->contact,
+                                  "notify::presence-status",
+                                  G_CALLBACK (presence_updated),
+                                  self);
                 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);
-
                 update_presence_pixbuf (self,
                                         OSSO_ABOOK_PRESENCE (priv->contact));
-                g_signal_connect (priv->contact,
-                                  "notify::presence-status",
-                                  G_CALLBACK (presence_updated),
-                                  self);
-
-                resize_sender (priv);
-                gtk_widget_queue_draw (GTK_WIDGET (self));
+                show_contact (self);
         }
-
 }
 
 static void
@@ -717,8 +718,6 @@ reset_contact (ELHomeApplet *self)
 {
         ELHomeAppletPrivate *priv = self->priv;
 
-        gtk_widget_hide (priv->icon);
-
         if (priv->avatar_pixbuf) {
                 g_object_unref (priv->avatar_pixbuf);
                 priv->avatar_pixbuf = NULL;
@@ -770,12 +769,35 @@ contacts_removed (OssoABookRoster *roster,
 }
 
 static void
-clean_state (ELHomeApplet *self)
+clean_contact (ELHomeApplet *self)
 {
         ELHomeAppletPrivate *priv = self->priv;
 
         reset_contact (self);
 
+        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;
+        }
+}
+
+static void
+clean_state (ELHomeApplet *self)
+{
+        ELHomeAppletPrivate *priv = self->priv;
+
         if (priv->message) {
                 g_free (priv->message);
                 priv->message = NULL;
@@ -797,23 +819,6 @@ clean_state (ELHomeApplet *self)
                 g_free (priv->group_uid);
                 priv->group_uid = 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;
-        }
 }
 
 static void
@@ -836,6 +841,7 @@ dispose (GObject *self)
         }
 
         clean_state (EL_HOME_APPLET (self));
+        clean_contact (EL_HOME_APPLET (self));
 
         G_OBJECT_CLASS (el_home_applet_parent_class)->dispose (self);
 }
@@ -990,8 +996,6 @@ show_event (ELHomeApplet *self, RTComElIter *it)
                 priv->event_id = -1;
         }
 
-        gtk_widget_hide (priv->avatar);
-
         if (priv->message) {
                 gtk_widget_hide (priv->empty);
         }
@@ -1146,6 +1150,11 @@ read_event (ELHomeApplet *self)
         ELHomeAppletPrivate *priv = self->priv;
         RTComElIter *it = NULL;
         const gchar *icon_name = NULL;
+        gchar *remote_id;
+        gchar *local_id;
+
+        remote_id = g_strdup (priv->remote_id);
+        local_id = g_strdup (priv->local_id);
 
         clean_state (self);
 
@@ -1155,26 +1164,39 @@ read_event (ELHomeApplet *self)
         if (it) g_object_unref (it);
 
         if (priv->event_id >= 0) {
-                start_aggregator (self);
+                gboolean new_account = g_strcmp0 (priv->local_id, local_id);
 
-                if (g_strcmp0 (priv->local_id, "ring/tel/ring") == 0) {
-                        icon_name = "general_sms";
+                if (g_strcmp0 (priv->remote_id, remote_id) || new_account) {
+                        clean_contact (self);
+                        start_aggregator (self);
                 }
-                else{
-                        McAccount *account;
-                        account = osso_abook_account_manager_lookup_by_name (NULL,
-                                                                             priv->local_id);
-                        if (account) {
-                                McProfile *profile = mc_profile_lookup (mc_account_compat_get_profile (account));
-                                icon_name = mc_profile_get_icon_name (profile);
-                        }
+                else {
+                        show_contact (self);
                 }
 
-                if (icon_name) {
-                        gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
-                                                      icon_name,
-                                                      HILDON_ICON_SIZE_XSMALL);
-                        gtk_widget_show (priv->icon);
+                if (new_account) {
+                        g_warning ("Update service icon");
+                        if (g_strcmp0 (priv->local_id, "ring/tel/ring") == 0) {
+                                icon_name = "general_sms";
+                        }
+                        else{
+                                McAccount *account;
+                                account = osso_abook_account_manager_lookup_by_name (NULL,
+                                                                                     priv->local_id);
+                                if (account) {
+                                        McProfile *profile = mc_profile_lookup (mc_account_compat_get_profile (account));
+                                        icon_name = mc_profile_get_icon_name (profile);
+                                }
+                        }
+
+                        if (icon_name) {
+                                gtk_image_set_from_icon_name (GTK_IMAGE (priv->icon),
+                                                              icon_name,
+                                                              HILDON_ICON_SIZE_XSMALL);
+                                gtk_widget_show (priv->icon);
+                        }
+                        else
+                                gtk_widget_hide (priv->icon);
                 }
         }
         else {
@@ -1201,7 +1223,13 @@ read_event (ELHomeApplet *self)
                 gtk_label_set_text (GTK_LABEL (priv->sender),
                                     dgettext ("rtcom-messaging-ui",
                                               "messaging_ap_conversations"));
+
+                clean_contact (self);
+                gtk_widget_hide (priv->icon);
         }
+
+        g_free (local_id);
+        g_free (remote_id);
 }
 
 static void
@@ -1634,11 +1662,6 @@ el_home_applet_init (ELHomeApplet *self)
                                 0.5f,
                                 0.5f);
 
-        priv->avatar = gtk_image_new ();
-        gtk_misc_set_alignment (GTK_MISC (priv->avatar),
-                                0.5f,
-                                0.5f);
-
         priv->sender = gtk_label_new ("asdf asdf asdf asdf asdf");
         gtk_misc_set_alignment (GTK_MISC (priv->sender),
                                 0.5f,