Show total of conversations as well in the empty view
[conv-inbox] / src / el-home-applet.c
index 9e06024..582d635 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "config.h"
 #include "el-home-applet.h"
-
+#include <libintl.h>
 #include <hildon/hildon.h>
 #include <rtcom-eventlogger/eventlogger.h>
 #include <sqlite3.h>
 #define NOTIFICATION_UI_DBUS_PATH     "/org/freedesktop/Telepathy/Client/NotificationUI"
 #define NOTIFICATION_UI_DBUS_IFACE    "com.nokia.RtcomNotificationUi"
 
+#define CONVERSATIONS_UI_DBUS_NAME     "com.nokia.MessagingUI"
+#define CONVERSATIONS_UI_DBUS_PATH     "/com/nokia/MessagingUI"
+#define CONVERSATIONS_UI_DBUS_IFACE    "com.nokia.MessagingUI"
+
 static const gchar *conv_services[] = {"RTCOM_EL_SERVICE_SMS",
                                        "RTCOM_EL_SERVICE_CHAT",
                                        NULL};
@@ -100,10 +104,14 @@ struct _ELHomeAppletPrivate
         GtkWidget *icon;
         GtkWidget *unread;
         GtkWidget *received;
-        GtkWidget *empty;
         GtkWidget *cut_message;
         GtkWidget *avatar;
 
+        /* empty view*/
+        GtkWidget *empty;
+        GtkWidget *sms_total;
+        GtkWidget *chat_total;
+
         gchar *message;
         gint event_id;
 
@@ -509,7 +517,6 @@ expose_event (GtkWidget *self, GdkEventExpose *event)
         }
 
         if (priv->message) {
-
                 /* draw footer unread part bg */
                 rounded_rectangle (cr,
                                    0, C_HEIGHT - FOOTER_HEIGHT,
@@ -580,23 +587,45 @@ expose_event (GtkWidget *self, GdkEventExpose *event)
 }
 
 static void
+resize_sender (ELHomeAppletPrivate *priv)
+{
+        guint width = C_WIDTH;
+
+        if (priv->avatar_pixbuf) {
+                width -= AVATAR_SIZE + HILDON_MARGIN_DEFAULT;
+        }
+
+        if (priv->presence_pixbuf) {
+                width -= HILDON_ICON_PIXEL_SIZE_XSMALL + HILDON_MARGIN_DEFAULT;
+        }
+
+        gtk_widget_set_size_request (priv->sender,
+                                     width,
+                                     HILDON_ICON_PIXEL_SIZE_THUMB);
+}
+
+static void
 update_presence_pixbuf (ELHomeApplet *self,
                         OssoABookPresence *presence)
 {
         ELHomeAppletPrivate *priv = self->priv;
         const gchar *icon_name = osso_abook_presence_get_icon_name (presence);
+        gboolean resize = !!priv->presence_pixbuf ^ !!icon_name; /* logical via bit XOR */
 
         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);
+        if (resize)
+                resize_sender (priv);
 
         gtk_widget_queue_draw (GTK_WIDGET (self));
 }
@@ -614,14 +643,79 @@ 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;
 
-        gtk_widget_set_size_request (priv->sender,
-                                     C_WIDTH,
-                                     HILDON_ICON_PIXEL_SIZE_THUMB);
+        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);
+
+                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));
+        }
+
+}
+
+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;
 
         gtk_widget_hide (priv->icon);
 
@@ -635,6 +729,53 @@ 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;
+        }
+
+        resize_sender (priv);
+}
+
+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 +798,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 +851,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 +861,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;
@@ -1001,6 +1097,49 @@ query_unread_events (RTComEl *el)
         return count;
 }
 
+static gboolean
+query_read_events (RTComEl *el, const gchar *service, gint *events, gint *conversations)
+{
+        sqlite3 *db;
+        sqlite3_stmt *stmt;
+        int ret;
+        gboolean result = TRUE;
+
+        g_object_get (el, "db", &db, NULL);
+
+        if (sqlite3_prepare_v2 (db,
+                                "SELECT SUM(total_events), COUNT(group_uid) FROM GroupCache, Services "
+                                "WHERE GroupCache.service_id=Services.id AND Services.name=?;",
+                                -1,
+                                &stmt,
+                                NULL) != SQLITE_OK) {
+                g_error ("%s: can't compile SQL", G_STRFUNC);
+                return FALSE;
+        }
+        if (sqlite3_bind_text (stmt, 1, service, -1, SQLITE_STATIC) != SQLITE_OK)  {
+                g_error ("Failed to bind %s to SQL stmt", service);
+                result = FALSE;
+                goto DONE;
+        }
+
+        while (SQLITE_BUSY == (ret = sqlite3_step (stmt)));
+
+        if (ret == SQLITE_ROW) {
+                *events = sqlite3_column_int (stmt, 0);
+                *conversations = sqlite3_column_int (stmt, 1);
+        }
+        else {
+                g_error ("%s: error while executing SQL", G_STRFUNC);
+                result = FALSE;
+                goto DONE;
+        }
+
+ DONE:
+        sqlite3_finalize (stmt);
+
+        return result;
+}
+
 static void
 read_event (ELHomeApplet *self)
 {
@@ -1012,29 +1151,57 @@ read_event (ELHomeApplet *self)
 
         it = make_query (priv->eventlogger, -1);
         show_event (self, it);
-        resolve_contact (self);
 
-        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 (it) g_object_unref (it);
+
+        if (priv->event_id >= 0) {
+                start_aggregator (self);
+
+                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);
+                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 {
+                gchar *text;
+                gint n_sms_events = 0, n_sms_convs = 0;
+                gint n_chat_events = 0, n_chat_convs = 0;
+                const gchar *fmt = "%d <span size=\"small\">(%d)</span>";
+
+                query_read_events (priv->eventlogger,
+                                   "RTCOM_EL_SERVICE_SMS",
+                                   &n_sms_events, &n_sms_convs);
+                query_read_events (priv->eventlogger,
+                                   "RTCOM_EL_SERVICE_CHAT",
+                                   &n_chat_events, &n_chat_convs);
+
+                text = g_strdup_printf (fmt, n_sms_convs, n_sms_events);
+                gtk_label_set_markup (GTK_LABEL (priv->sms_total), text);
+                g_free (text);
 
-        if (it) g_object_unref (it);
+                text = g_strdup_printf (fmt, n_chat_convs, n_chat_events);
+                gtk_label_set_markup (GTK_LABEL (priv->chat_total), text);
+                g_free (text);
+
+                gtk_label_set_text (GTK_LABEL (priv->sender),
+                                    dgettext ("rtcom-messaging-ui",
+                                              "messaging_ap_conversations"));
+        }
 }
 
 static void
@@ -1042,7 +1209,7 @@ remove_notification (ELHomeApplet *self)
 {
         ELHomeAppletPrivate *priv = self->priv;
 
-        DBusGConnection* conn;
+        DBusGConnection *conn;
         GError *error;
         DBusGProxy *proxy;
         GPtrArray *conv_structs;
@@ -1118,6 +1285,38 @@ mark_as_read (ELHomeApplet *self)
 }
 
 static void
+launch_conversations (ELHomeApplet *self)
+{
+        DBusConnection *conn;
+        DBusMessage *message;
+        DBusError error;
+
+        dbus_error_init (&error);
+        conn = hd_home_plugin_item_get_dbus_connection (HD_HOME_PLUGIN_ITEM (self),
+                                                        DBUS_BUS_SESSION,
+                                                        &error);
+        if (!conn) {
+                if (dbus_error_is_set (&error)) {
+                        g_error ("Failed to get dbus connection %s", error.message);
+                        dbus_error_free (&error);
+                }
+                return;
+        }
+
+        message = dbus_message_new_method_call (CONVERSATIONS_UI_DBUS_NAME,
+                                                CONVERSATIONS_UI_DBUS_PATH,
+                                                CONVERSATIONS_UI_DBUS_IFACE,
+                                                "top_application");
+        dbus_message_set_no_reply (message, TRUE);
+
+        if (dbus_connection_send (conn, message, NULL))
+                dbus_connection_flush (conn);
+        dbus_message_unref (message);
+
+        dbus_connection_close (conn);
+}
+
+static void
 open_conversation (ELHomeApplet *self)
 {
         ELHomeAppletPrivate *priv = self->priv;
@@ -1258,7 +1457,7 @@ button_press_event_cb (GtkWidget      *widget,
 {
         ELHomeAppletPrivate *priv = self->priv;
 
-        if (priv->event_id > 0) {
+        if (priv->event_id >= 0) {
                 if (event->y < CONTENT_OFFSET_Y_TOP + HEADER_HEIGHT) {
                         if (priv->aggregator &&
                             osso_abook_waitable_is_ready
@@ -1270,9 +1469,12 @@ button_press_event_cb (GtkWidget      *widget,
                         priv->active = SELECTED_FOOTER;
                 else
                         priv->active = SELECTED_BODY;
-
-                gtk_widget_queue_draw (widget);
         }
+        else {
+                priv->active = SELECTED_BODY;
+        }
+
+        gtk_widget_queue_draw (widget);
 
         return TRUE;
 }
@@ -1339,8 +1541,12 @@ button_release_event_cb (GtkWidget      *widget,
 
         switch (priv->active) {
         case SELECTED_BODY:
-                reset_scroll (self);
-                open_conversation (self);
+                if (priv->event_id >= 0) {
+                        reset_scroll (self);
+                        open_conversation (self);
+                }
+                else
+                        launch_conversations (self);
                 break;
         case SELECTED_HEADER: {
                 GtkWidget *dialog = NULL;
@@ -1410,6 +1616,7 @@ el_home_applet_init (ELHomeApplet *self)
         ELHomeAppletPrivate *priv;
         GtkWidget *event_box;
         GtkWidget *hbox, *vbox, *align, *footer;
+        GtkWidget *w;
 
         self->priv = EL_HOME_APPLET_GET_PRIVATE (self);
         priv = self->priv;
@@ -1446,9 +1653,33 @@ el_home_applet_init (ELHomeApplet *self)
 
         priv->message = g_strdup ("One two three four five six seven eight nine ten");
 
-        /* TODO: l10n */
-        priv->empty = gtk_label_new ("No new messages");
-        gtk_widget_set_name (priv->empty, "hildon-shadow-label");
+        /* construt empty table */
+        priv->empty = gtk_fixed_new ();
+
+        w = gtk_image_new_from_icon_name ("general_sms", HILDON_ICON_SIZE_FINGER);
+        gtk_fixed_put (GTK_FIXED (priv->empty), w,
+                       4*HILDON_MARGIN_DOUBLE,
+                       2*HILDON_MARGIN_DOUBLE);
+
+        w = gtk_image_new_from_icon_name ("general_chat", HILDON_ICON_SIZE_FINGER);
+        gtk_fixed_put (GTK_FIXED (priv->empty), w,
+                       4*HILDON_MARGIN_DOUBLE,
+                       3*HILDON_MARGIN_DOUBLE + HILDON_ICON_PIXEL_SIZE_FINGER);
+
+        priv->sms_total = gtk_label_new (NULL);
+        gtk_widget_set_name (priv->sms_total, "hildon-shadow-label");
+        gtk_fixed_put (GTK_FIXED (priv->empty), priv->sms_total,
+                       5*HILDON_MARGIN_DOUBLE  + HILDON_ICON_PIXEL_SIZE_FINGER,
+                       2*HILDON_MARGIN_DOUBLE + HILDON_MARGIN_HALF);
+
+        priv->chat_total = gtk_label_new (NULL);
+        gtk_widget_set_name (priv->chat_total, "hildon-shadow-label");
+        gtk_fixed_put (GTK_FIXED (priv->empty), priv->chat_total,
+                       5*HILDON_MARGIN_DOUBLE  + HILDON_ICON_PIXEL_SIZE_FINGER,
+                       3*HILDON_MARGIN_DOUBLE + HILDON_MARGIN_HALF + HILDON_ICON_PIXEL_SIZE_FINGER);
+
+        gtk_widget_show_all (GTK_WIDGET (priv->empty));
+        gtk_widget_hide (GTK_WIDGET (priv->empty));
         GTK_WIDGET_SET_FLAGS (priv->empty, GTK_NO_SHOW_ALL);
 
         priv->received = gtk_label_new ("aewf aewf aewf awef");