* src/widgets/modest-header-view.c:
[modest] / src / modest-widget-memory.c
index 5fe3735..be9c42a 100644 (file)
@@ -33,6 +33,7 @@
 #include <modest-account-mgr-helpers.h>
 #include <modest-tny-platform-factory.h>
 #include <modest-tny-folder.h>
+#include <modest-init.h>
 #include <widgets/modest-header-view.h>
 #include <widgets/modest-msg-view.h>
 #include <widgets/modest-folder-view.h>
@@ -241,6 +242,9 @@ restore_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
        if (modest_conf_key_exists (conf, key, NULL)) {
                pos = modest_conf_get_int (conf, key, NULL);
                gtk_paned_set_position (paned, pos);
+       } else {
+               /* The initial position must follow the 30/70 rule */
+               gtk_paned_set_position (paned, GTK_WIDGET(paned)->requisition.width/3);
        }
 
        g_free (key);
@@ -253,6 +257,8 @@ save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
                           const gchar *name)
 {
        gchar *key;
+       gchar *sort_key;
+       gchar *sort_value;
        GString *str;
        GList *cols, *cursor;
        TnyFolder *folder;
@@ -260,18 +266,29 @@ save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
        ModestHeaderViewStyle style;
        gint sort_colid;
        GtkSortType sort_type;
+       gint sort_flag_id = 0;
        
        folder = modest_header_view_get_folder (header_view);
-       if (!folder || modest_header_view_is_empty (header_view))
+       if (!folder || modest_header_view_is_empty (header_view)) {
+               if (folder)
+                       g_object_unref (folder);
                return TRUE; /* no non-empty folder: no settings */
+       }
        
        type  = modest_tny_folder_guess_folder_type (folder);
        style = modest_header_view_get_style   (header_view);
        
        key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
                                                                  MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
+       sort_key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
+                                                                      MODEST_WIDGET_MEMORY_PARAM_COLUMN_SORT);
 
        cursor = cols = modest_header_view_get_columns (header_view);
+       if (!cols) {
+               g_warning ("DEBUG: %s: modest_header_view_get_columns() returned NULL.",
+                        __FUNCTION__);
+       }
+       
        str = g_string_new (NULL);
 
        /* NOTE: the exact details of this format are important, as they
@@ -279,10 +296,12 @@ save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
         */
        sort_colid = modest_header_view_get_sort_column_id (header_view, type); 
        sort_type = modest_header_view_get_sort_type (header_view, type); 
+
        while (cursor) {
 
                int col_id, width, sort;
                GtkTreeViewColumn *col;
+               int column_sort_flag;
                
                col    = GTK_TREE_VIEW_COLUMN (cursor->data);
                col_id = GPOINTER_TO_INT(g_object_get_data (G_OBJECT(col),
@@ -290,13 +309,31 @@ save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
                width = gtk_tree_view_column_get_width (col);
                sort = 0;
                if (sort_colid == col_id)
-                       sort = (sort_type == GTK_SORT_ASCENDING) ? 1:-1;
-                       
+                       sort = (sort_type == GTK_SORT_ASCENDING) ? 1:0;
+               column_sort_flag = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (col), MODEST_HEADER_VIEW_FLAG_SORT));
+               if (column_sort_flag != 0)
+                       sort_flag_id = column_sort_flag;
+               
                g_string_append_printf (str, "%d:%d:%d ", col_id, width, sort);
                cursor = g_list_next (cursor);
        }
 
-       modest_conf_set_string (conf, key, str->str, NULL);
+       if ((str->str == NULL) || (strlen(str->str) == 0)) {
+               /* TODO: Find out why this happens sometimes. */
+               g_warning ("DEBUG: %s: Attempting to write an empty value to "
+                       "gconf key %s. Preventing.", __FUNCTION__, key);
+       }
+       else
+               modest_conf_set_string (conf, key, str->str, NULL);
+
+       /* store current sort column for compact view */
+       if (sort_colid >= 0) {
+               sort_value = g_strdup_printf("%d:%d:%d", sort_colid, sort_type, sort_flag_id);
+               modest_conf_set_string (conf, sort_key, sort_value, NULL);
+               g_free (sort_value);
+       }
+
+
 
        g_free (key);   
        g_string_free (str, TRUE);
@@ -314,19 +351,30 @@ restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
        guint col, width;
        gint sort;
        gchar *key;
+       gchar *sort_key;
        TnyFolder *folder;
        TnyFolderType type;
        ModestHeaderViewStyle style;
-
+       gint sort_flag_id = 0;
+       gint sort_colid = -1, sort_type;
+       
        folder = modest_header_view_get_folder (header_view);
-       if (!folder || modest_header_view_is_empty (header_view))
-               return TRUE; /* no non-empty folder: no settings */
+       if (!folder)
+               return TRUE; /* no folder: no settings */
        
-       type = modest_tny_folder_guess_folder_type (folder);
-       style = modest_header_view_get_style   (header_view);
+       type = modest_tny_folder_guess_folder_type (folder);    
+       style = modest_header_view_get_style (header_view);
 
        key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
                                                                  MODEST_WIDGET_MEMORY_PARAM_COLUMN_WIDTH);
+       sort_key = _modest_widget_memory_get_keyname_with_double_type (name, type, style,
+                                                                      MODEST_WIDGET_MEMORY_PARAM_COLUMN_SORT);
+
+       if (modest_conf_key_exists (conf, sort_key, NULL)) {
+               gchar *value = modest_conf_get_string (conf, sort_key, NULL);
+               sscanf (value, "%d:%d:%d", &sort_colid, &sort_type, &sort_flag_id);
+       }
+
        if (modest_conf_key_exists (conf, key, NULL)) {
                
                gchar *data, *cursor;
@@ -337,6 +385,7 @@ restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
 
                cursor = data = modest_conf_get_string (conf, key, NULL);
                while (cursor && sscanf (cursor, "%d:%d:%d ", &col, &width, &sort) == 3) {
+
                        cols      = g_list_append (cols, GINT_TO_POINTER(col));
                        colwidths = g_list_append (colwidths, GINT_TO_POINTER(width));
                        colsortables = g_list_append (colsortables, GINT_TO_POINTER(sort));
@@ -344,32 +393,40 @@ restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
                }
                g_free (data);  
                
+               /* Use defaults if gconf has no, or empty information: */
+               /* We don't know why the value is empty sometimes. */
+               if (g_list_length(cols) == 0) {
+                       g_warning("%s: gconf key %s was empty. Using default column IDs.\n", 
+                               __FUNCTION__, key);
+                       g_list_free (cols);
+                       cols = NULL;
+               }
+               
+               if (!cols)
+                       cols = modest_init_get_default_header_view_column_ids (type, style);
+               
                if (cols) {
-                       GList *viewcolumns, *colcursor, *widthcursor, *sortablecursor;
+                       GList *viewcolumns, *colcursor, *widthcursor;
                        modest_header_view_set_columns (header_view, cols, type);
                        sortable = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
 
                        widthcursor = colwidths;
-                       sortablecursor = colsortables;
                        colcursor = viewcolumns = gtk_tree_view_get_columns (GTK_TREE_VIEW(header_view));
-                       while (colcursor && widthcursor && sortablecursor) {
+                       while (colcursor && widthcursor) {
                                int width = GPOINTER_TO_INT(widthcursor->data);
-                               int sort = GPOINTER_TO_INT(sortablecursor->data);
+                               int view_column_id = GPOINTER_TO_INT (g_object_get_data (
+                                                                             G_OBJECT (colcursor->data), 
+                                                                             MODEST_HEADER_VIEW_COLUMN));
                                if (width > 0)
                                        gtk_tree_view_column_set_max_width(GTK_TREE_VIEW_COLUMN(colcursor->data),
                                                                           width);
-                               if (sort != 0) {
-                                       int colid = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(colcursor->data), MODEST_HEADER_VIEW_COLUMN));
-                                       GtkSortType sort_type = (sort == 1) ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING; 
-                                       modest_header_view_set_sort_params (header_view, colid, sort_type, type);
-                                       gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
-                                                                             colid,
-                                                                             sort_type);
-                                       gtk_tree_sortable_sort_column_changed (GTK_TREE_SORTABLE(sortable));
-                               }
+                               if (((view_column_id == MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN) ||
+                                    (view_column_id == MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT)) &&
+                                   (sort_flag_id != 0))
+                                       g_object_set_data (G_OBJECT (colcursor->data), 
+                                                          MODEST_HEADER_VIEW_FLAG_SORT, GINT_TO_POINTER (sort_flag_id));
                                colcursor = g_list_next (colcursor);
                                widthcursor = g_list_next (widthcursor);
-                               sortablecursor = g_list_next (sortablecursor);
                        }
 
                        g_list_free (cols);
@@ -379,7 +436,22 @@ restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
                }
        }
 
+       if (sort_colid >= 0) {
+         GtkTreeModel *sortable = 
+                 gtk_tree_model_filter_get_model (
+                         GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (header_view))));
+               if (sort_colid == TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN)
+                       modest_header_view_sort_by_column_id (header_view, 0, sort_type);
+               gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(sortable),
+                                                     sort_colid,
+                                                     sort_type);
+               modest_header_view_sort_by_column_id (header_view, sort_colid, sort_type);
+               gtk_tree_sortable_sort_column_changed (GTK_TREE_SORTABLE(sortable));
+       }
+
        g_free (key);
+       g_free (sort_key);
+       
        g_object_unref (G_OBJECT (folder));
 
        return TRUE;
@@ -394,13 +466,14 @@ save_settings_folder_view (ModestConf *conf, ModestFolderView *folder_view,
        gchar *key;
        const gchar* account_id;
 
-       /* Restore the visible account */
+       /* Save the visible account */
        key = _modest_widget_memory_get_keyname (name, "visible_server_account_id");
 
-       account_id = modest_folder_view_get_visible_server_account_id (folder_view);
+       account_id = modest_folder_view_get_account_id_of_visible_server_account (folder_view);
        if (account_id)
                modest_conf_set_string (conf, key, account_id, NULL);
-
+       else
+               modest_conf_remove_key (conf, key, NULL);
        g_free (key);
 
        return TRUE;
@@ -418,8 +491,8 @@ restore_settings_folder_view (ModestConf *conf,
 
        if (modest_conf_key_exists (conf, key, NULL)) {
                account_id = modest_conf_get_string (conf, key, NULL);
-               modest_folder_view_set_visible_server_account_id (folder_view, 
-                                                                 (const gchar *) account_id);
+               modest_folder_view_set_account_id_of_visible_server_account (folder_view, 
+                                                                            (const gchar *) account_id);
                g_free (account_id);
        } else {
                ModestAccountMgr *mgr;
@@ -438,7 +511,7 @@ restore_settings_folder_view (ModestConf *conf,
                        server_acc_id = (const gchar *) acc_data->store_account->account_name;
 
                        modest_conf_set_string (conf, key, server_acc_id, NULL);
-                       modest_folder_view_set_visible_server_account_id (folder_view, server_acc_id);
+                       modest_folder_view_set_account_id_of_visible_server_account (folder_view, server_acc_id);
 
                        g_free (default_acc);
                }
@@ -499,7 +572,7 @@ modest_widget_memory_restore (ModestConf *conf, GObject *widget, const gchar *na
        g_return_val_if_fail (conf, FALSE);
        g_return_val_if_fail (widget, FALSE);
        g_return_val_if_fail (name, FALSE);
-       
+
        if (GTK_IS_WINDOW(widget))
                return restore_settings_window (conf, GTK_WINDOW(widget), name);
        else if (GTK_IS_PANED(widget))