* Partially fixes NB#85461, do not unload the folder before synchronizing it
[modest] / src / widgets / modest-header-view.c
index 9b169b8..aeddf0c 100644 (file)
@@ -32,6 +32,7 @@
 #include <tny-simple-list.h>
 #include <tny-folder-monitor.h>
 #include <tny-folder-change.h>
+#include <tny-error.h>
 #include <string.h>
 
 #include <modest-header-view.h>
@@ -154,6 +155,7 @@ struct _ModestHeaderViewPrivate {
 
        HeaderViewStatus status;
        guint status_timeout;
+       gboolean notify_status; /* whether or not the filter_row should notify about changes in the filtering */
 };
 
 typedef struct _HeadersCountChangedHelper HeadersCountChangedHelper;
@@ -388,12 +390,15 @@ modest_header_view_set_columns (ModestHeaderView *self, const GList *columns, Tn
        g_object_set (G_OBJECT (renderer_subject),
                      "ellipsize", PANGO_ELLIPSIZE_END, "yalign", 1.0,
                      NULL);
+       gtk_cell_renderer_text_set_fixed_height_from_font (GTK_CELL_RENDERER_TEXT (renderer_subject), 1);
        g_object_set (G_OBJECT (renderer_recpt),
                      "ellipsize", PANGO_ELLIPSIZE_END, "yalign", 0.0,
                      NULL);
+       gtk_cell_renderer_text_set_fixed_height_from_font (GTK_CELL_RENDERER_TEXT (renderer_recpt), 1);
        g_object_set(G_OBJECT(renderer_compact_date_or_status),
                     "xalign", 1.0, "yalign", 0.0,
                     NULL);
+       gtk_cell_renderer_text_set_fixed_height_from_font (GTK_CELL_RENDERER_TEXT (renderer_compact_date_or_status), 1);
        g_object_set (G_OBJECT (renderer_priority),
                      "yalign", 1.0, NULL);
        g_object_set (G_OBJECT (renderer_attach),
@@ -561,6 +566,7 @@ modest_header_view_init (ModestHeaderView *obj)
 
        priv->status  = HEADER_VIEW_INIT;
        priv->status_timeout = 0;
+       priv->notify_status = TRUE;
 
        priv->observer_list_lock = g_mutex_new();
        priv->observer_list = NULL;
@@ -983,22 +989,19 @@ modest_header_view_get_folder (ModestHeaderView *self)
 }
 
 static void
-modest_header_view_set_folder_intern (ModestHeaderView *self, TnyFolder *folder)
+set_folder_intern_get_headers_async_cb (TnyFolder *folder, 
+                                       gboolean cancelled, 
+                                       TnyList *headers, 
+                                       GError *err, 
+                                       gpointer user_data)
 {
-       TnyFolderType type;
-       TnyList *headers;
+       ModestHeaderView *self;
        ModestHeaderViewPrivate *priv;
-       GList *cols, *cursor;
-       GtkTreeModel *filter_model, *sortable; 
-       guint sort_colid;
-       GtkSortType sort_type;
-
-       priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
 
-       headers = TNY_LIST (tny_gtk_header_list_model_new ());
+       g_return_if_fail (MODEST_IS_HEADER_VIEW (user_data));
 
-       tny_gtk_header_list_model_set_folder (TNY_GTK_HEADER_LIST_MODEL(headers),
-                                             folder, FALSE, NULL, NULL, NULL);
+       self = MODEST_HEADER_VIEW (user_data);
+       priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
 
        /* Add IDLE observer (monitor) and another folder observer for
           new messages (self) */
@@ -1011,6 +1014,39 @@ modest_header_view_set_folder_intern (ModestHeaderView *self, TnyFolder *folder)
        tny_folder_monitor_add_list (priv->monitor, TNY_LIST (headers));
        tny_folder_monitor_start (priv->monitor);
        g_mutex_unlock (priv->observers_lock);
+}
+
+static void
+modest_header_view_set_folder_intern (ModestHeaderView *self, TnyFolder *folder)
+{
+       TnyFolderType type;
+       TnyList *headers;
+       ModestHeaderViewPrivate *priv;
+       GList *cols, *cursor;
+       GtkTreeModel *filter_model, *sortable; 
+       guint sort_colid;
+       GtkSortType sort_type;
+
+       priv = MODEST_HEADER_VIEW_GET_PRIVATE(self);
+
+       headers = TNY_LIST (tny_gtk_header_list_model_new ());
+
+       /* Start the monitor in the callback of the
+          tny_gtk_header_list_model_set_folder call. It's crucial to
+          do it there and not just after the call because we want the
+          monitor to observe only the headers returned by the
+          tny_folder_get_headers_async call that it's inside the
+          tny_gtk_header_list_model_set_folder call. This way the
+          monitor infrastructure could successfully cope with
+          duplicates. For example if a tny_folder_add_msg_async is
+          happening while tny_gtk_header_list_model_set_folder is
+          invoked, then the first call could add a header that will
+          be added again by tny_gtk_header_list_model_set_folder, so
+          we'd end up with duplicate headers. sergio */
+       tny_gtk_header_list_model_set_folder (TNY_GTK_HEADER_LIST_MODEL(headers),
+                                             folder, FALSE, 
+                                             set_folder_intern_get_headers_async_cb, 
+                                             NULL, self);
 
        sortable = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL(headers));
        g_object_unref (G_OBJECT (headers));
@@ -1179,11 +1215,37 @@ folder_refreshed_cb (ModestMailOperation *mail_op,
        g_signal_emit (G_OBJECT (info->header_view), 
                       signals[UPDATING_MSG_LIST_SIGNAL], 0, FALSE, NULL);
 
+       /* Allow filtering notifications from now on if the current
+          folder is still the same (if not then the user has selected
+          another one to refresh, we should wait until that refresh
+          finishes) */
+       if (priv->folder == folder)
+               priv->notify_status = TRUE;
+
        /* Frees */
        g_object_unref (info->header_view);
        g_free (info);
 }
 
+static void
+refresh_folder_error_handler (ModestMailOperation *mail_op, 
+                             gpointer user_data)
+{
+       const GError *error = modest_mail_operation_get_error (mail_op);
+
+       if (error->code == TNY_SYSTEM_ERROR_MEMORY ||
+           error->code == TNY_IO_ERROR_WRITE ||
+           error->code == TNY_IO_ERROR_READ) {
+               ModestMailOperationStatus st = modest_mail_operation_get_status (mail_op);
+               /* If the mail op has been cancelled then it's not an error: don't show any message */
+               if (st != MODEST_MAIL_OPERATION_STATUS_CANCELED) {
+                       modest_platform_information_banner (NULL, NULL,
+                                                           dgettext("ke-recv",
+                                                                    "cerm_device_memory_full"));
+               }
+       }
+}
+
 void
 modest_header_view_set_folder (ModestHeaderView *self, 
                               TnyFolder *folder,
@@ -1206,8 +1268,10 @@ modest_header_view_set_folder (ModestHeaderView *self,
        }
                                                      
        if (priv->folder) {
-               if (priv->status_timeout)
+               if (priv->status_timeout) {
                        g_source_remove (priv->status_timeout);
+                       priv->status_timeout = 0;
+               }
 
                g_mutex_lock (priv->observers_lock);
                tny_folder_remove_observer (priv->folder, TNY_FOLDER_OBSERVER (self));
@@ -1226,6 +1290,9 @@ modest_header_view_set_folder (ModestHeaderView *self,
                /* Pick my reference. Nothing to do with the mail operation */
                priv->folder = g_object_ref (folder);
 
+               /* Do not notify about filterings until the refresh finishes */
+               priv->notify_status = FALSE;
+
                /* Clear the selection if exists */
                selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
                gtk_tree_selection_unselect_all(selection);
@@ -1242,7 +1309,9 @@ modest_header_view_set_folder (ModestHeaderView *self,
                info->user_data = user_data;
 
                /* Create the mail operation (source will be the parent widget) */
-               mail_op = modest_mail_operation_new (G_OBJECT(main_win));
+               mail_op = modest_mail_operation_new_with_error_handling (G_OBJECT(main_win),
+                                                                        refresh_folder_error_handler,
+                                                                        NULL, NULL);
                modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
                                                 mail_op);
 
@@ -1715,22 +1784,40 @@ on_button_press_event(GtkWidget * self, GdkEventButton * event, gpointer userdat
        GtkTreeSelection *selection = NULL;
        GtkTreePath *path = NULL;
        gboolean already_selected = FALSE;
+       ModestTnySendQueueStatus status = MODEST_TNY_SEND_QUEUE_UNKNOWN;
 
        if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(self), event->x, event->y, &path, NULL, NULL, NULL)) {
+               GtkTreeIter iter;
+               GtkTreeModel *model;
+
                selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
                already_selected = gtk_tree_selection_path_is_selected (selection, path);
+
+               /* Get header from model */
+               model = gtk_tree_view_get_model (GTK_TREE_VIEW (self));
+               if (gtk_tree_model_get_iter (model, &iter, path)) {
+                       GValue value = {0,};
+                       TnyHeader *header;
+
+                       gtk_tree_model_get_value (model, &iter, 
+                                                 TNY_GTK_HEADER_LIST_MODEL_INSTANCE_COLUMN, 
+                                                 &value);
+                       header = (TnyHeader *) g_value_get_object (&value);
+                       if (TNY_IS_HEADER (header))
+                               status = modest_tny_all_send_queues_get_msg_status (header);
+                       g_value_unset (&value);
+               }
        }
 
        /* Enable drag and drop onlly if the user clicks on a row that
           it's already selected. If not, let him select items using
-          the pointer */
-       if (!already_selected) {
+          the pointer. If the message is in an outbos and in sending
+          status disable drag and drop as well */
+       if (!already_selected || status == MODEST_TNY_SEND_QUEUE_SENDING)
                disable_drag_and_drop(self);
-       }
 
-       if (path != NULL) {
+       if (path != NULL)
                gtk_tree_path_free(path);
-       }
 
        return FALSE;
 }
@@ -1904,6 +1991,10 @@ static void
 notify_filter_change_destroy (gpointer data)
 {
        NotifyFilterInfo *info = (NotifyFilterInfo *) data;
+       ModestHeaderViewPrivate *priv;
+
+       priv = MODEST_HEADER_VIEW_GET_PRIVATE (info->self);
+       priv->status_timeout = 0;
 
        g_object_unref (info->self);
        g_object_unref (info->folder);
@@ -1957,7 +2048,7 @@ filter_row (GtkTreeModel *model,
        
        /* Check hiding */
        if (priv->hidding_ids != NULL) {
-               id = g_strdup(tny_header_get_message_id (header));
+               id = tny_header_dup_message_id (header);
                for (i=0; i < priv->n_selected && !found; i++)
                        if (priv->hidding_ids[i] != NULL && id != NULL)
                                found = (!strcmp (priv->hidding_ids[i], id));
@@ -1969,7 +2060,7 @@ filter_row (GtkTreeModel *model,
  frees:
        old_status = priv->status;
        priv->status = ((gboolean) priv->status) && !visible;
-       if (priv->status != old_status) {
+       if ((priv->notify_status) && (priv->status != old_status)) {
                NotifyFilterInfo *info;
 
                if (priv->status_timeout)
@@ -2043,7 +2134,7 @@ on_account_removed (TnyAccountStore *self,
                my_account = tny_folder_get_account (priv->folder);
                if (my_account == account)
                        modest_header_view_clear (MODEST_HEADER_VIEW (user_data));
-               g_object_unref (account);
+               g_object_unref (my_account);
        }
 }