* Fix some detected memory leaks
[modest] / src / widgets / modest-folder-view.c
index d2705a3..46f87cb 100644 (file)
@@ -49,6 +49,8 @@
 #include "modest-folder-view.h"
 #include <modest-dnd.h>
 #include <modest-platform.h>
+#include <modest-account-mgr-helpers.h>
+#include <modest-widget-memory.h>
 
 /* 'private'/'protected' functions */
 static void modest_folder_view_class_init  (ModestFolderViewClass *klass);
@@ -139,6 +141,7 @@ struct _ModestFolderViewPrivate {
        guint                 timer_expander;
 
        gchar                *local_account_name;
+       gchar                *visible_account_id;
        ModestFolderViewStyle style;
 };
 #define MODEST_FOLDER_VIEW_GET_PRIVATE(o)                      \
@@ -280,19 +283,19 @@ text_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                else
                        number = unread;
 
-               /* Use bold font style if there are unread messages */                  
+               /* Use bold font style if there are unread messages */
                if (unread > 0) {
                        gchar *folder_title = g_strdup_printf ("%s (%d)", fname, unread);
                        g_object_set (rendobj,"text", folder_title,  "weight", 800, NULL);
                        if (G_OBJECT (priv->cur_folder_store) == instance)
-                               g_signal_emit (G_OBJECT(data), 
+                               g_signal_emit (G_OBJECT(data),
                                               signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
                                               folder_title);
                        g_free (folder_title);
                } else {
                        g_object_set (rendobj,"text", fname, "weight", 400, NULL);
                        if (G_OBJECT (priv->cur_folder_store) == instance)
-                               g_signal_emit (G_OBJECT(data), 
+                               g_signal_emit (G_OBJECT(data),
                                               signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
                                               fname);
                }
@@ -333,8 +336,8 @@ static void
 icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
                 GtkTreeModel *tree_model,  GtkTreeIter *iter, gpointer data)
 {
-       GObject *rendobj, *instance;
-       GdkPixbuf *pixbuf;
+       GObject *rendobj = NULL, *instance = NULL;
+       GdkPixbuf *pixbuf = NULL;
        TnyFolderType type;
        gchar *fname = NULL;
        const gchar *account_id = NULL;
@@ -344,7 +347,7 @@ icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
        gtk_tree_model_get (tree_model, iter,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN, &fname,
-                           TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread, 
+                           TNY_GTK_FOLDER_STORE_TREE_MODEL_UNREAD_COLUMN, &unread,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_INSTANCE_COLUMN, &instance,
                            -1);
 
@@ -403,53 +406,69 @@ icon_cell_data  (GtkTreeViewColumn *column,  GtkCellRenderer *renderer,
 }
 
 static void
-modest_folder_view_init (ModestFolderView *obj)
+add_columns (GtkWidget *treeview)
 {
-       ModestFolderViewPrivate *priv;
        GtkTreeViewColumn *column;
        GtkCellRenderer *renderer;
        GtkTreeSelection *sel;
-       ModestConf *conf;
-       
-       priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
-       
-       priv->timer_expander = 0;
-       priv->account_store  = NULL;
-       priv->cur_folder_store     = NULL;
-       priv->query          = NULL;
-       priv->style          = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
-
-       /* Initialize the local account name */
-       conf = modest_runtime_get_conf();
-       priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
 
-       /* Build treeview */
+       /* Create column */
        column = gtk_tree_view_column_new ();   
        
+       /* Set icon and text render function */
        renderer = gtk_cell_renderer_pixbuf_new();
        gtk_tree_view_column_pack_start (column, renderer, FALSE);
        gtk_tree_view_column_set_cell_data_func(column, renderer,
-                                               icon_cell_data, obj, NULL);
+                                               icon_cell_data, treeview, NULL);
        
        renderer = gtk_cell_renderer_text_new();
        gtk_tree_view_column_pack_start (column, renderer, FALSE);
        gtk_tree_view_column_set_cell_data_func(column, renderer,
-                                               text_cell_data, obj, NULL);
+                                               text_cell_data, treeview, NULL);
        
-       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(obj));
+       /* Set selection mode */
+       sel = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeview));
        gtk_tree_selection_set_mode (sel, GTK_SELECTION_SINGLE);
 
+       /* Set treeview appearance */
        gtk_tree_view_column_set_spacing (column, 2);
        gtk_tree_view_column_set_resizable (column, TRUE);
        gtk_tree_view_column_set_fixed_width (column, TRUE);            
-       gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(obj), FALSE);
-       gtk_tree_view_set_enable_search     (GTK_TREE_VIEW(obj), FALSE);
+       gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(treeview), FALSE);
+       gtk_tree_view_set_enable_search (GTK_TREE_VIEW(treeview), FALSE);
+
+       /* Add column */
+       gtk_tree_view_append_column (GTK_TREE_VIEW(treeview),column);
+}
 
-       gtk_tree_view_append_column (GTK_TREE_VIEW(obj),column);
+static void
+modest_folder_view_init (ModestFolderView *obj)
+{
+       ModestFolderViewPrivate *priv;
+       ModestConf *conf;
+       
+       priv =  MODEST_FOLDER_VIEW_GET_PRIVATE(obj);
+       
+       priv->timer_expander = 0;
+       priv->account_store  = NULL;
+       priv->query          = NULL;
+       priv->style          = MODEST_FOLDER_VIEW_STYLE_SHOW_ALL;
+       priv->cur_folder_store   = NULL;
+       priv->visible_account_id = NULL;
+
+       /* Initialize the local account name */
+       conf = modest_runtime_get_conf();
+       priv->local_account_name = modest_conf_get_string (conf, MODEST_CONF_DEVICE_NAME, NULL);
+
+       /* Build treeview */
+       add_columns (GTK_WIDGET (obj));
 
        /* Setup drag and drop */
        setup_drag_and_drop (GTK_TREE_VIEW(obj));
 
+       /* Restore conf */
+       modest_widget_memory_restore (conf, G_OBJECT (obj), MODEST_CONF_FOLDER_VIEW_KEY);
+
        /* Connect signals */
        g_signal_connect (G_OBJECT (obj), 
                          "key-press-event", 
@@ -509,6 +528,7 @@ modest_folder_view_finalize (GObject *obj)
                g_signal_handler_disconnect (G_OBJECT(sel), priv->changed_signal);
 
        g_free (priv->local_account_name);
+       g_free (priv->visible_account_id);
        
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
@@ -603,7 +623,8 @@ modest_folder_view_new (TnyFolderStoreQuery *query)
        self = G_OBJECT (g_object_new (MODEST_TYPE_FOLDER_VIEW, NULL));
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE (self);
 
-       priv->query = g_object_ref (query);
+       if (query)
+               priv->query = g_object_ref (query);
        
        sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(self));
        priv->changed_signal = g_signal_connect (sel, "changed",
@@ -627,26 +648,18 @@ expand_root_items (ModestFolderView *self)
 }
 
 /*
- * HACK: we use this function to implement the
- * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. This implementation
- * assumes that the model has the following order (which is currently
- * true) Remote folders->Local folders->MMC folders, so the rows of
- * the first level (that represent the accounts) are received in the
- * same order. So basically it uses a static variable to register that
- * a remote account has already being shown to hide the others
- * (returning NULL). When the function evaluates the local or the MMC
- * accounts is time to reset the static variable in order to get it
- * ready for the next time the tree model needs to be shown.
+ * We use this function to implement the
+ * MODEST_FOLDER_VIEW_STYLE_SHOW_ONE style. We only show the default
+ * account in this case
  */
 static gboolean 
 filter_row (GtkTreeModel *model,
            GtkTreeIter *iter,
            gpointer data)
 {
-       static gboolean found = FALSE;
-       gboolean retval;
+       gboolean retval = TRUE;
        gint type;
-       GObject *instance;
+       GObject *instance = NULL;
 
        gtk_tree_model_get (model, iter,
                            TNY_GTK_FOLDER_STORE_TREE_MODEL_TYPE_COLUMN, &type,
@@ -654,22 +667,24 @@ filter_row (GtkTreeModel *model,
                            -1);
 
        if (type == TNY_FOLDER_TYPE_ROOT) {
-               const gchar *account_id = tny_account_get_id (TNY_ACCOUNT (instance));
+               TnyAccount *acc;
+               const gchar *account_id;
 
-               if (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) &&
-                   strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) {
+               acc = TNY_ACCOUNT (instance);
+               account_id = tny_account_get_id (acc);
 
-                       if (!found) {
-                               found = TRUE;
-                               retval = TRUE;
-                       } else
+               if (strcmp (account_id, MODEST_LOCAL_FOLDERS_ACCOUNT_ID) &&
+                   strcmp (account_id, MODEST_MMC_ACCOUNT_ID)) { 
+                       ModestFolderViewPrivate *priv;
+                       
+                       /* Show only the visible account id */
+                       priv = MODEST_FOLDER_VIEW_GET_PRIVATE (data);
+                       if (priv->visible_account_id && strcmp (account_id, priv->visible_account_id))
                                retval = FALSE;
-               } else {
-                       found = FALSE;
-                       retval = TRUE;
                }
-       } else
-               retval = TRUE;
+       }
+
+       g_object_unref (instance);
 
        return retval;
 }
@@ -715,7 +730,7 @@ update_model (ModestFolderView *self, ModestTnyAccountStore *account_store)
                        filter_model = gtk_tree_model_filter_new (sortable, NULL);
                        gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter_model),
                                                                filter_row,
-                                                               NULL,
+                                                               self,
                                                                NULL);
                }
 
@@ -753,10 +768,10 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
                priv->cur_folder_store = NULL;
 
                /* Notify the display name observers */
-               g_signal_emit (G_OBJECT(user_data), 
+               g_signal_emit (G_OBJECT(user_data),
                               signals[FOLDER_DISPLAY_NAME_CHANGED_SIGNAL], 0,
                               NULL);
-               return; 
+               return;
        }
 
        tree_view = MODEST_FOLDER_VIEW (user_data);
@@ -783,9 +798,9 @@ on_selection_changed (GtkTreeSelection *sel, gpointer user_data)
        priv->cur_folder_store = folder;
 
        /* New folder has been selected */
-       g_signal_emit (G_OBJECT(tree_view), 
-                      signals[FOLDER_SELECTION_CHANGED_SIGNAL], 
-                      0, folder, TRUE); 
+       g_signal_emit (G_OBJECT(tree_view),
+                      signals[FOLDER_SELECTION_CHANGED_SIGNAL],
+                      0, folder, TRUE);
 }
 
 TnyFolderStore *
@@ -912,7 +927,7 @@ typedef struct _DndHelper {
 
 /*
  * This function is the callback of the
- * modest_mail_operation_xfer_msg() and
+ * modest_mail_operation_xfer_msgs () and
  * modest_mail_operation_xfer_folder() calls. We check here if the
  * message/folder was correctly asynchronously transferred. The reason
  * to use the same callback is that the code is the same, it only has
@@ -957,6 +972,7 @@ drag_and_drop_from_header_view (GtkTreeModel *source_model,
                                GtkTreePath  *dest_row,
                                DndHelper    *helper)
 {
+       TnyList *headers;
        TnyHeader *header;
        TnyFolder *folder;
        ModestMailOperation *mail_op;
@@ -975,13 +991,19 @@ drag_and_drop_from_header_view (GtkTreeModel *source_model,
                            &folder, -1);
 
        /* Transfer message */
-       mail_op = modest_mail_operation_new ();
-       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
+       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, NULL);
+
+       modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
                                         mail_op);
        g_signal_connect (G_OBJECT (mail_op), "progress-changed",
                          G_CALLBACK (on_progress_changed), helper);
 
-       modest_mail_operation_xfer_msg (mail_op, header, folder, helper->delete_source);
+       /* FIXME: I replaced this because the API changed, but D&D
+          should be reviewed in order to allow multiple drags*/
+       headers = tny_simple_list_new ();
+       tny_list_append (headers, G_OBJECT (header));
+       g_object_unref (header);
+       modest_mail_operation_xfer_msgs (mail_op, headers, folder, helper->delete_source);
 
        /* Frees */
        g_object_unref (G_OBJECT (mail_op));
@@ -1029,7 +1051,7 @@ drag_and_drop_from_folder_view (GtkTreeModel     *source_model,
                            &folder, -1);
 
        /* Do the mail operation */
-       mail_op = modest_mail_operation_new ();
+       mail_op = modest_mail_operation_new (MODEST_MAIL_OPERATION_ID_RECEIVE, NULL);
        modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (), 
                                         mail_op);
        g_signal_connect (G_OBJECT (mail_op), "progress-changed",
@@ -1380,25 +1402,27 @@ on_configuration_key_changed (ModestConf* conf,
 {
        ModestFolderViewPrivate *priv;
 
-       if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME))
+       if (!key)
                return;
 
        priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
 
-       g_free (priv->local_account_name);
+       if (!strcmp (key, MODEST_CONF_DEVICE_NAME)) {
+               g_free (priv->local_account_name);
 
-       if (event == MODEST_CONF_EVENT_KEY_UNSET)
-               priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
-       else
-               priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
-                                                                  MODEST_CONF_DEVICE_NAME, NULL);
+               if (event == MODEST_CONF_EVENT_KEY_UNSET)
+                       priv->local_account_name = g_strdup (MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
+               else
+                       priv->local_account_name = modest_conf_get_string (modest_runtime_get_conf(),
+                                                                          MODEST_CONF_DEVICE_NAME, NULL);
 
-       /* Force a redraw */
+               /* Force a redraw */
 #if GTK_CHECK_VERSION(2, 8, 0) /* gtk_tree_view_column_queue_resize is only available in GTK+ 2.8 */
-       GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
-                                               TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
-       gtk_tree_view_column_queue_resize (tree_column);
+               GtkTreeViewColumn * tree_column = gtk_tree_view_get_column (GTK_TREE_VIEW (self), 
+                                                                           TNY_GTK_FOLDER_STORE_TREE_MODEL_NAME_COLUMN);
+               gtk_tree_view_column_queue_resize (tree_column);
 #endif
+       }
 }
 
 void 
@@ -1413,3 +1437,35 @@ modest_folder_view_set_style (ModestFolderView *self,
 
        priv->style = style;
 }
+
+void
+modest_folder_view_set_account_id_of_visible_server_account (ModestFolderView *self,
+                                                            const gchar *account_id)
+{
+       ModestFolderViewPrivate *priv;
+       ModestConf *conf;
+
+       g_return_if_fail (self);
+       
+       priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
+
+       if (priv->visible_account_id)
+               g_free (priv->visible_account_id);
+       priv->visible_account_id = g_strdup (account_id);
+
+       /* Save preferences */
+       conf = modest_runtime_get_conf ();
+       modest_widget_memory_save (conf, G_OBJECT (self), MODEST_CONF_FOLDER_VIEW_KEY);
+}
+
+const gchar *
+modest_folder_view_get_account_id_of_visible_server_account (ModestFolderView *self)
+{
+       ModestFolderViewPrivate *priv;
+
+       g_return_val_if_fail (self, NULL);
+       
+       priv = MODEST_FOLDER_VIEW_GET_PRIVATE(self);
+
+       return (const gchar *) priv->visible_account_id;
+}