* don't depend on hildon when it's not there
[modest] / src / modest-tny-header-tree-view.c
index 8ef2a85..7e88c5e 100644 (file)
@@ -1,6 +1,35 @@
-
-/* modest-tny-header-tree-view.c 
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+
+
+/* modest-tny-header-tree-view.c */
+
 #include <glib/gi18n.h>
 #include "modest-tny-header-tree-view.h"
 #include <tny-list-iface.h>
 #include <modest-icon-names.h>
 #include "modest-icon-factory.h"
 
-/* 'private'/'protected' functions */
 static void modest_tny_header_tree_view_class_init  (ModestTnyHeaderTreeViewClass *klass);
 static void modest_tny_header_tree_view_init        (ModestTnyHeaderTreeView *obj);
 static void modest_tny_header_tree_view_finalize    (GObject *obj);
 
-static void selection_changed (GtkTreeSelection *sel, gpointer user_data);
-static void column_clicked (GtkTreeViewColumn *treeviewcolumn, gpointer user_data);
+static void     on_selection_changed (GtkTreeSelection *sel, gpointer user_data);
+static void     on_column_clicked (GtkTreeViewColumn *treeviewcolumn, gpointer user_data);
 static gboolean refresh_folder_finish_status_update (gpointer user_data);
 
 enum {
@@ -33,6 +61,8 @@ struct _ModestTnyHeaderTreeViewPrivate {
 
        gint              status_id;
        GSList            *columns;
+
+       GMutex            *lock;
        
        ModestTnyHeaderTreeViewStyle style;
 };
@@ -107,7 +137,7 @@ msgtype_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
                   GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
 {
        TnyMsgHeaderFlags flags;
-       GdkPixbuf *pixbuf;
+       GdkPixbuf *pixbuf = NULL;
 
        gtk_tree_model_get (tree_model, iter, TNY_MSG_HEADER_LIST_MODEL_FLAGS_COLUMN,
                            &flags, -1);
@@ -118,8 +148,8 @@ msgtype_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
                pixbuf = modest_icon_factory_get_icon (MODEST_HEADER_ICON_READ);
        else
                pixbuf = modest_icon_factory_get_icon (MODEST_HEADER_ICON_UNREAD);
-       if (pixbuf)
-               g_object_set (G_OBJECT (renderer), "pixbuf", pixbuf, NULL);
+               
+       g_object_set (G_OBJECT (renderer), "pixbuf", pixbuf, NULL);
 }
 
 static void
@@ -132,13 +162,11 @@ attach_cell_data (GtkTreeViewColumn *column, GtkCellRenderer *renderer,
        gtk_tree_model_get (tree_model, iter, TNY_MSG_HEADER_LIST_MODEL_FLAGS_COLUMN,
                            &flags, -1);
 
-       if (flags & TNY_MSG_HEADER_FLAG_ATTACHMENTS) {
+       if (flags & TNY_MSG_HEADER_FLAG_ATTACHMENTS)
                pixbuf = modest_icon_factory_get_icon (MODEST_HEADER_ICON_ATTACH);
-               if (pixbuf)
-                       g_object_set (G_OBJECT (renderer), "pixbuf", pixbuf, NULL);
-       }
-}
 
+       g_object_set (G_OBJECT (renderer), "pixbuf", pixbuf, NULL);
+}
 
 
 static void
@@ -159,12 +187,36 @@ header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
 
 
 
+/* try to make a shorter display address; changes it arg in-place */
+static gchar*
+display_address (gchar *address)
+{
+       gchar *cursor;
+
+       if (!address)
+               return;
+       
+       /* simplistic --> remove <email@address> from display name */
+       cursor = g_strstr_len (address, strlen(address), "<");
+       if (cursor) 
+               cursor[0]='\0';
+
+       /* simplistic --> remove (bla bla) from display name */
+       cursor = g_strstr_len (address, strlen(address), "(");
+       if (cursor) 
+               cursor[0]='\0';
+
+       return address;
+}
+
+
+
 static void
 sender_receiver_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                            GtkTreeModel *tree_model,  GtkTreeIter *iter,  gboolean is_sender)
 {
        TnyMsgHeaderFlags flags;
-       gchar *addr1, *addr2;
+       gchar *address;
        gint sender_receiver_col;
 
        if (is_sender)
@@ -173,22 +225,20 @@ sender_receiver_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *rendere
                sender_receiver_col = TNY_MSG_HEADER_LIST_MODEL_TO_COLUMN;
                
        gtk_tree_model_get (tree_model, iter,
-                           sender_receiver_col,  &addr1,
+                           sender_receiver_col,  &address,
                            TNY_MSG_HEADER_LIST_MODEL_FLAGS_COLUMN, &flags,
                            -1);
-       
-       /* simplistic --> remove <email@address> from display */
-       addr2 = g_strstr_len (addr1, strlen(addr1), "<");
-       if (addr2) 
-               addr2[0]='\0';
 
        g_object_set (G_OBJECT(renderer),
-                     "text", addr1,
-                     "weight", (flags & TNY_MSG_HEADER_FLAG_SEEN) ? 400 : 800,
-                     "style",  (flags & TNY_MSG_HEADER_FLAG_DELETED) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL,
+                     "text",
+                     display_address (address),
+                     "weight",
+                     (flags & TNY_MSG_HEADER_FLAG_SEEN) ? 400 : 800,
+                     "style",
+                     (flags & TNY_MSG_HEADER_FLAG_DELETED)?PANGO_STYLE_ITALIC:PANGO_STYLE_NORMAL,
                      NULL);
 
-       g_free (addr1); 
+       g_free (address);       
 }
 
 
@@ -227,7 +277,6 @@ compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer
        GObject *rendobj;
        TnyMsgHeaderFlags flags;
        gchar *from, *subject;
-       gchar *address;
        gchar *header;
        time_t date;
                
@@ -238,14 +287,12 @@ compact_header_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer
                            TNY_MSG_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN, &date,   
                            -1);
        rendobj = G_OBJECT(renderer);           
-       
-       /* simplistic --> remove <email@address> from display */
-       address = g_strstr_len (from, strlen(from), "<");
-       if (address)
-               address[0]='\0'; /* set a new endpoint */
-       
-       header = g_strdup_printf ("%s %s\n%s", from,
-                                 display_date(date), subject);
+
+       header = g_strdup_printf ("%s %s\n%s",
+                                 display_address (from),
+                                 display_date(date),
+                                 subject);
+
        g_object_set (G_OBJECT(renderer),
                      "text",  header,
                      "weight", (flags & TNY_MSG_HEADER_FLAG_SEEN) ? 400: 800,
@@ -405,6 +452,7 @@ modest_tny_header_tree_view_init (ModestTnyHeaderTreeView *obj)
        priv = MODEST_TNY_HEADER_TREE_VIEW_GET_PRIVATE(obj); 
 
        priv->status_id = 0;
+       priv->lock = g_mutex_new ();
 }
 
 static void
@@ -418,9 +466,17 @@ modest_tny_header_tree_view_finalize (GObject *obj)
 
        if (priv->headers)      
                g_object_unref (G_OBJECT(priv->headers));
+
        
+       if (priv->lock) {
+               g_mutex_free (priv->lock);
+               priv->lock = NULL;
+       }
+
        priv->headers = NULL;
        priv->tny_msg_folder    = NULL;
+
+       G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
 GtkWidget*
@@ -453,7 +509,7 @@ modest_tny_header_tree_view_new (TnyMsgFolderIface *folder,
 
        sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
        g_signal_connect (sel, "changed",
-                         G_CALLBACK(selection_changed), self);
+                         G_CALLBACK(on_selection_changed), self);
        
        return GTK_WIDGET(self);
 }
@@ -523,11 +579,11 @@ modest_tny_header_tree_view_get_style (ModestTnyHeaderTreeView *self)
 
 
 /* get the length of any prefix that should be ignored for sorting */
-static int 
+static inline int 
 get_prefix_len (const gchar *sub)
 {
-       int i = 0;
-       const static gchar* prefix[] = {"Re:", "RE:", "Fwd:", "FWD:", "FW:", NULL};
+       gint i = 0;
+       const static gchar* prefix[] = {"Re:", "RE:", "Fwd:", "FWD:", "FW:", "AW:", NULL};
 
        if (sub[0] != 'R' && sub[0] != 'F') /* optimization */
                return 0;
@@ -545,12 +601,20 @@ get_prefix_len (const gchar *sub)
 }
 
 
-static gint
+static inline gint
 cmp_normalized_subject (const gchar* s1, const gchar *s2)
 {
-       /* FIXME: utf8 */
-       return strcmp (s1 + get_prefix_len(s1),
-                      s2 + get_prefix_len(s2));
+       gint result = 0;
+       register gchar *n1, *n2;
+       
+       n1 = g_utf8_collate_key (s1 + get_prefix_len(s1), -1);
+       n2 = g_utf8_collate_key (s2 + get_prefix_len(s2), -1);
+       
+       result = strcmp (n1, n2);
+       g_free (n1);
+       g_free (n2);
+       
+       return result;
 }
 
 
@@ -571,16 +635,20 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
                /* first one, we decide based on the time */
        case MODEST_TNY_HEADER_TREE_VIEW_COLUMN_COMPACT_HEADER:
        case MODEST_TNY_HEADER_TREE_VIEW_COLUMN_RECEIVED_DATE:
-               gtk_tree_model_get (tree_model, iter1, TNY_MSG_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN,
+               gtk_tree_model_get (tree_model, iter1,
+                                   TNY_MSG_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN,
                                    &t1,-1);
-               gtk_tree_model_get (tree_model, iter2, TNY_MSG_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN,
+               gtk_tree_model_get (tree_model, iter2,
+                                   TNY_MSG_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN,
                                    &t2,-1);
                return t1 - t2;
                
        case MODEST_TNY_HEADER_TREE_VIEW_COLUMN_SENT_DATE:
-               gtk_tree_model_get (tree_model, iter1, TNY_MSG_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN,
+               gtk_tree_model_get (tree_model, iter1,
+                                   TNY_MSG_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN,
                                    &t1,-1);
-               gtk_tree_model_get (tree_model, iter2, TNY_MSG_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN,
+               gtk_tree_model_get (tree_model, iter2,
+                                   TNY_MSG_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN,
                                    &t2,-1);
                return t1 - t2;
 
@@ -644,7 +712,8 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
                gtk_tree_model_get (tree_model, iter2, TNY_MSG_HEADER_LIST_MODEL_FLAGS_COLUMN, &val2,
                                    TNY_MSG_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN, &t2, -1);
                
-               cmp = (val1 & TNY_MSG_HEADER_FLAG_ATTACHMENTS) - (val2 & TNY_MSG_HEADER_FLAG_ATTACHMENTS);
+               cmp = (val1 & TNY_MSG_HEADER_FLAG_ATTACHMENTS) -
+                       (val2 & TNY_MSG_HEADER_FLAG_ATTACHMENTS);
 
                return cmp ? cmp : t1 - t2;
                
@@ -665,7 +734,7 @@ cmp_rows (GtkTreeModel *tree_model, GtkTreeIter *iter1, GtkTreeIter *iter2,
 
 static void
 refresh_folder (TnyMsgFolderIface *folder, gboolean cancelled,
-                                      gpointer user_data)
+               gpointer user_data)
 {
        GtkTreeModel *oldsortable, *sortable;
        ModestTnyHeaderTreeView *self =
@@ -685,11 +754,9 @@ refresh_folder (TnyMsgFolderIface *folder, gboolean cancelled,
        else { /* it's a new one or a refresh */
                GSList *col;
 
-               #warning Looks like a memory leak.              
                priv->headers = TNY_LIST_IFACE(tny_msg_header_list_model_new ());
-                       
-               tny_msg_folder_iface_get_headers (folder, priv->headers,
-                                                 FALSE);
+               
+               tny_msg_folder_iface_get_headers (folder, priv->headers, FALSE);
                tny_msg_header_list_model_set_folder (TNY_MSG_HEADER_LIST_MODEL(priv->headers),
                                                      folder, TRUE); /* async */
                
@@ -707,7 +774,7 @@ refresh_folder (TnyMsgFolderIface *folder, gboolean cancelled,
                /* install our special sorting functions */
                col = priv->columns;
                while (col) {
-                       int col_id = GPOINTER_TO_INT (col->data);
+                       gint col_id = GPOINTER_TO_INT (col->data);
                        gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE(sortable), col_id,
                                                         (GtkTreeIterCompareFunc)cmp_rows,
                                                         GINT_TO_POINTER(col_id), NULL);
@@ -717,8 +784,8 @@ refresh_folder (TnyMsgFolderIface *folder, gboolean cancelled,
                gtk_tree_view_set_model (GTK_TREE_VIEW (self), sortable);
                gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(self), TRUE);
                /* no need to unref sortable */
-               
-       } 
+       }
+
 }
 
 
@@ -768,36 +835,60 @@ gboolean
 modest_tny_header_tree_view_set_folder (ModestTnyHeaderTreeView *self,
                                        TnyMsgFolderIface *folder)
 {
-       
-       if (!folder)  /* when there is no folder */
+       ModestTnyHeaderTreeViewPrivate *priv;
+               
+       g_return_val_if_fail (MODEST_IS_TNY_HEADER_TREE_VIEW (self), FALSE);
+
+       priv = MODEST_TNY_HEADER_TREE_VIEW_GET_PRIVATE(self);
+
+       g_mutex_lock (priv->lock);
+
+       if (!folder)  {/* when there is no folder */
+               GtkTreeModel *model;
+               model = gtk_tree_view_get_model (GTK_TREE_VIEW(self));
                gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(self), FALSE);
-       
+               gtk_tree_view_set_model (GTK_TREE_VIEW (self), NULL);
+               if (model)
+                       g_object_unref (model);
+       }
        else { /* it's a new one or a refresh */
                tny_msg_folder_iface_refresh_async (folder,
                                            refresh_folder,
                                                    refresh_folder_status_update,
                                                    self);
        }
+
+       /* no message selected */
+       g_signal_emit (G_OBJECT(self), signals[MESSAGE_SELECTED_SIGNAL], 0,
+                      NULL);
+
+       g_mutex_unlock (priv->lock);
+
        return TRUE;
 }
 
 
 
 static void
-selection_changed (GtkTreeSelection *sel, gpointer user_data)
+on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
 {
-       GtkTreeModel            *model;
-       TnyMsgHeaderIface       *header;
-       GtkTreeIter             iter;
-       ModestTnyHeaderTreeView *tree_view;
-
+       GtkTreeModel                   *model;
+       TnyMsgHeaderIface              *header;
+       GtkTreeIter                    iter;
+       ModestTnyHeaderTreeView        *self;
+       ModestTnyHeaderTreeViewPrivate *priv;
+       
        g_return_if_fail (sel);
        g_return_if_fail (user_data);
        
+       self = MODEST_TNY_HEADER_TREE_VIEW (user_data);
+       priv = MODEST_TNY_HEADER_TREE_VIEW_GET_PRIVATE(self);   
+
+               
        if (!gtk_tree_selection_get_selected (sel, &model, &iter))
                return; /* msg was _un_selected */
-       
-       tree_view = MODEST_TNY_HEADER_TREE_VIEW (user_data);
+
+       //g_mutex_lock (priv->lock);
        
        gtk_tree_model_get (model, &iter,
                            TNY_MSG_HEADER_LIST_MODEL_INSTANCE_COLUMN,
@@ -809,28 +900,31 @@ selection_changed (GtkTreeSelection *sel, gpointer user_data)
                
                folder = tny_msg_header_iface_get_folder (TNY_MSG_HEADER_IFACE(header));
                if (!folder)
-                       g_message ("cannot find folder");
+                       g_printerr ("modest: cannot find folder\n");
                else {
                        msg = tny_msg_folder_iface_get_message (TNY_MSG_FOLDER_IFACE(folder),
                                                                header);
                        if (!msg) {
-                               g_message ("cannot find msg");
+                               g_printerr ("modest: cannot find msg\n");
                                gtk_tree_store_remove (GTK_TREE_STORE(model), 
                                                       &iter); 
                        }
                }
                                        
-               g_signal_emit (G_OBJECT(tree_view), signals[MESSAGE_SELECTED_SIGNAL], 0,
+               g_signal_emit (G_OBJECT(self), signals[MESSAGE_SELECTED_SIGNAL], 0,
                               msg);
 
                /* mark message as seen; _set_flags crashes, bug in tinymail? */
                //flags = tny_msg_header_iface_get_flags (TNY_MSG_HEADER_IFACE(header));
                //tny_msg_header_iface_set_flags (header, TNY_MSG_HEADER_FLAG_SEEN);
        }
+
+       // g_mutex_unlock (priv->lock);
+       
 }
 
 static void
-column_clicked (GtkTreeViewColumn *col, gpointer user_data)
+on_column_clicked (GtkTreeViewColumn *col, gpointer user_data)
 {
        GtkTreeView *treeview;
        gint id;