* Fix some detected memory leaks
[modest] / src / widgets / modest-folder-view.c
index cf1acc2..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)                      \
@@ -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;
@@ -448,9 +451,10 @@ modest_folder_view_init (ModestFolderView *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;
+       priv->cur_folder_store   = NULL;
+       priv->visible_account_id = NULL;
 
        /* Initialize the local account name */
        conf = modest_runtime_get_conf();
@@ -462,6 +466,9 @@ modest_folder_view_init (ModestFolderView *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", 
@@ -521,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);
 }
@@ -640,24 +648,16 @@ 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 = NULL;
 
@@ -667,22 +667,22 @@ 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);
 
@@ -730,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);
                }
 
@@ -927,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
@@ -972,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;
@@ -990,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));
@@ -1044,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",
@@ -1395,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 
@@ -1428,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;
+}