Track contacts added/removed in aggregator
[conv-inbox] / src / el-home-applet.c
index a2f2923..4e950cf 100644 (file)
@@ -31,7 +31,8 @@
 #include <libosso-abook/osso-abook-aggregator.h>
 #include <libosso-abook/osso-abook-contact.h>
 #include <libosso-abook/osso-abook-waitable.h>
-#include <libosso-abook/osso-abook-contact.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>
@@ -62,7 +63,6 @@
 #define MESSAGE_HEIGHT (C_HEIGHT - HEADER_HEIGHT - FOOTER_HEIGHT)
 #define MESSAGE_WIDTH (C_WIDTH - 2*HILDON_MARGIN_DEFAULT)
 
-#define SERVICE_ICON_SIZE 64
 #define AVATAR_SIZE HILDON_ICON_PIXEL_SIZE_THUMB
 
 #define AVATAR_X (C_WIDTH - AVATAR_SIZE - HILDON_MARGIN_DEFAULT)
@@ -97,7 +97,7 @@ struct _ELHomeAppletPrivate
         RTComEl *eventlogger;
 
         GtkWidget *sender;
-        /* GtkWidget *icon; */
+        GtkWidget *icon;
         GtkWidget *unread;
         GtkWidget *received;
         GtkWidget *empty;
@@ -116,11 +116,10 @@ struct _ELHomeAppletPrivate
                 float green;
                 float blue;
         } active_color;
-        guint8 border_color[4];
         PangoFontDescription *font_desc;
 
         GdkPixbuf *avatar_pixbuf;
-        GdkPixbuf *service_pixbuf;
+        GdkPixbuf *presence_pixbuf;
 
         guint idle_id;
 
@@ -336,11 +335,6 @@ style_set_cb (GtkWidget *widget,
                 priv->active_color.red = color.red/(float)G_MAXUINT16;
                 priv->active_color.green = color.green/(float)G_MAXUINT16;
                 priv->active_color.blue = color.blue/(float)G_MAXUINT16;
-
-                priv->border_color[0] = color.red;
-                priv->border_color[1] = color.green;
-                priv->border_color[2] = color.blue;
-                priv->border_color[3] = 255;
         }
 }
 
@@ -499,16 +493,22 @@ expose_event (GtkWidget *self, GdkEventExpose *event)
                                        1.0f);
                 cairo_stroke (cr);
         }
+        if (priv->presence_pixbuf) {
+                guint x = C_WIDTH - HILDON_ICON_PIXEL_SIZE_XSMALL - HILDON_MARGIN_DEFAULT;
+                guint y = (HEADER_HEIGHT - HILDON_ICON_PIXEL_SIZE_XSMALL)/2;
+
+                if (priv->avatar_pixbuf)
+                        x -= AVATAR_SIZE + HILDON_MARGIN_DEFAULT;
+
+                cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+                gdk_cairo_set_source_pixbuf (cr,
+                                             priv->presence_pixbuf,
+                                             x,
+                                             y);
+                cairo_paint (cr);
+        }
 
         if (priv->message) {
-                if (priv->service_pixbuf) {
-                        cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
-                        gdk_cairo_set_source_pixbuf (cr,
-                                                     priv->service_pixbuf,
-                                                     C_WIDTH - SERVICE_ICON_SIZE,
-                                                     C_HEIGHT - FOOTER_HEIGHT - SERVICE_ICON_SIZE);
-                        cairo_paint_with_alpha (cr, 0.3);
-                }
 
                 /* draw footer unread part bg */
                 rounded_rectangle (cr,
@@ -579,9 +579,118 @@ expose_event (GtkWidget *self, GdkEventExpose *event)
         return GTK_WIDGET_CLASS (el_home_applet_parent_class)->expose_event (self, event);
 }
 
+static void
+update_presence_pixbuf (ELHomeApplet *self,
+                        OssoABookPresence *presence)
+{
+        ELHomeAppletPrivate *priv = self->priv;
+        const gchar *icon_name = osso_abook_presence_get_icon_name (presence);
+
+        if (priv->presence_pixbuf) {
+                g_object_unref (priv->presence_pixbuf);
+                priv->presence_pixbuf = NULL;
+        }
+        g_warning ("presence %s", icon_name);
+        if (icon_name)
+                priv->presence_pixbuf = gtk_icon_theme_load_icon
+                        (gtk_icon_theme_get_default (),
+                         icon_name,
+                         HILDON_ICON_PIXEL_SIZE_XSMALL,
+                         0, NULL);
+
+        gtk_widget_queue_draw (GTK_WIDGET (self));
+}
 
 static void
-clean_state (ELHomeApplet *self)
+presence_updated (OssoABookPresence *presence,
+                  GParamSpec *spec,
+                  gpointer *user_data)
+{
+        ELHomeApplet *self = EL_HOME_APPLET(user_data);
+
+        if (!OSSO_ABOOK_IS_CONTACT(self->priv->contact))
+                return;
+
+        update_presence_pixbuf (self, presence);
+}
+
+
+static void
+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;
 
@@ -589,16 +698,63 @@ clean_state (ELHomeApplet *self)
                                      C_WIDTH,
                                      HILDON_ICON_PIXEL_SIZE_THUMB);
 
-        if (priv->service_pixbuf) {
-                g_object_unref (priv->service_pixbuf);
-                priv->service_pixbuf = NULL;
-        }
+        gtk_widget_hide (priv->icon);
 
         if (priv->avatar_pixbuf) {
                 g_object_unref (priv->avatar_pixbuf);
                 priv->avatar_pixbuf = NULL;
         }
 
+        if (priv->presence_pixbuf) {
+                g_object_unref (priv->presence_pixbuf);
+                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;
@@ -621,16 +777,18 @@ clean_state (ELHomeApplet *self)
                 priv->group_uid = NULL;
         }
 
-        if (priv->contact) {
-                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;
@@ -672,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) {
@@ -683,70 +840,25 @@ 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 /*rounded*/
-                        (OSSO_ABOOK_AVATAR (priv->contact),
-                         HILDON_ICON_PIXEL_SIZE_THUMB,
-                         HILDON_ICON_PIXEL_SIZE_THUMB,
-                         TRUE);
-                         /* -1, */
-                         /* priv->border_color); */
-                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);
 
-                }
-#if 0
-                if (priv->avatar_pixbuf) {
-                        gtk_image_set_from_pixbuf (GTK_IMAGE (priv->avatar),
-                                                   priv->avatar_pixbuf);
-                        gtk_widget_show (priv->avatar);
-                        /* g_object_unref (avatar_image); */
-                }
-#endif
-                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;
         GError *error = NULL;
-
+        g_warning ("%s rid %s lid %s", G_STRFUNC, priv->remote_id, priv->local_id);
         if (priv->local_id && priv->remote_id) {
                 const gchar *vcard = osso_abook_account_manager_get_vcard_field
                         (NULL, priv->local_id);
@@ -969,34 +1081,32 @@ read_event (ELHomeApplet *self)
 {
         ELHomeAppletPrivate *priv = self->priv;
         RTComElIter *it = NULL;
+        const gchar *icon_name = NULL;
 
         clean_state (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)) {
+        if (g_strcmp0 (priv->local_id, "ring/tel/ring") == 0) {
+                icon_name = "general_sms";
+        }
+        else{
                 McAccount *account;
-                const gchar *icon_name = NULL;
                 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) {
-                        GdkScreen *screen;
-                        GtkIconTheme *icon_theme;
-
-                        screen = gtk_widget_get_screen (GTK_WIDGET (self));
-                        icon_theme = gtk_icon_theme_get_for_screen (screen);
+        }
 
-                        priv->service_pixbuf = gtk_icon_theme_load_icon (icon_theme,
-                                                                         icon_name,
-                                                                         SERVICE_ICON_SIZE,
-                                                                         0, NULL);
-                }
+        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 (it) g_object_unref (it);
@@ -1386,16 +1496,12 @@ el_home_applet_init (ELHomeApplet *self)
                                 0.0f,
                                 0.5f);
         hildon_helper_set_logical_font (priv->unread, "SmallSystemFont");
-        /* gtk_widget_set_size_request (priv->unread, */
-        /*                              -1, */
-        /*                              HEADER_HEIGHT); */
-#if 0
-        priv->icon = gtk_image_new_from_icon_name ("chat_unread_sms",
-                                                   HILDON_ICON_SIZE_FINGER);
+
+        priv->icon = gtk_image_new ();
         gtk_misc_set_alignment (GTK_MISC (priv->icon),
                                 0.5f,
                                 0.5f);
-#endif
+
         priv->avatar = gtk_image_new ();
         gtk_misc_set_alignment (GTK_MISC (priv->avatar),
                                 0.5f,
@@ -1437,13 +1543,12 @@ el_home_applet_init (ELHomeApplet *self)
         GTK_WIDGET_SET_FLAGS (priv->cut_message, GTK_NO_SHOW_ALL);
 
         hbox = gtk_hbox_new (FALSE, 0);
-        /* gtk_box_pack_start (GTK_BOX (hbox), priv->icon, FALSE, FALSE, 0); */
         gtk_box_pack_start (GTK_BOX (hbox), priv->sender, FALSE, FALSE, 0);
-        /* gtk_box_pack_start (GTK_BOX (hbox), priv->avatar, FALSE, FALSE, 0); */
 
         footer = gtk_hbox_new (FALSE, HILDON_MARGIN_DEFAULT);
         gtk_box_pack_start (GTK_BOX (footer), priv->unread, FALSE, FALSE, 0);
         gtk_box_pack_start (GTK_BOX (footer), priv->cut_message, TRUE, TRUE, 0);
+        gtk_box_pack_end (GTK_BOX (footer), priv->icon, FALSE, FALSE, 0);
         gtk_box_pack_end (GTK_BOX (footer), priv->received, FALSE, FALSE, 0);
 
         vbox = gtk_vbox_new (FALSE, 0);
@@ -1502,7 +1607,7 @@ el_home_applet_init (ELHomeApplet *self)
 
         osso_abook_init_with_name (PACKAGE, NULL);
 
-        read_new_event (self);
+        add_new_idle (self);
 #endif
 }