Index: mail/ChangeLog
===================================================================
--- mail/ChangeLog	(revision 34394)
+++ mail/ChangeLog	(working copy)
@@ -1,3 +1,19 @@
+2007-10-17  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #343011
+	(restore collapse state of selected folder after start)
+
+	* em-folder-tree.c: (emft_maybe_expand_row): Do not expand subtree of
+	a node, only make it visible.
+	* em-folder-tree-model.h: (em_folder_tree_model_get_expanded_uri),
+	(em_folder_tree_model_set_expanded_uri):
+	* em-folder-tree-model.c: (em_folder_tree_model_get_expanded_uri),
+	(em_folder_tree_model_set_expanded_uri):
+	Same as ..._get/_set_expanded, but using 'uri', instead of 'key'.
+	* em-folder-tree-model.c: (emftm_uri_to_key): New helper function.
+	* mail-component.c: (impl_createView): Restore collapsed state after
+	selecting last selected folder, if necessary.
+
 2007-10-15  Matthew Barnes  <mbarnes@redhat.com>
 
 	* em-folder-view.c:
Index: mail/em-folder-tree.c
===================================================================
--- mail/em-folder-tree.c	(revision 34394)
+++ mail/em-folder-tree.c	(working copy)
@@ -671,6 +671,7 @@
 	char *full_name;
 	char *key;
 	struct _selected_uri *u;
+	gboolean is_expanded;
 
 	gtk_tree_model_get ((GtkTreeModel *) model, iter,
 			    COL_STRING_FULL_NAME, &full_name,
@@ -689,11 +690,19 @@
 		key = g_strdup_printf ("local/%s", full_name ? full_name : "");
 	}
 
+	is_expanded = em_folder_tree_model_get_expanded (model, key);
 	u = g_hash_table_lookup(priv->select_uris_table, key);
-	if (em_folder_tree_model_get_expanded (model, key) || u) {
-		gtk_tree_view_expand_to_path (priv->treeview, tree_path);
-		gtk_tree_view_expand_row (priv->treeview, tree_path, FALSE);
+	if (is_expanded || u) {
+		if (is_expanded) {
+			gtk_tree_view_expand_to_path (priv->treeview, tree_path);
+			gtk_tree_view_expand_row (priv->treeview, tree_path, FALSE);
+		} else {
+			char *c = strrchr (key, '/');
 
+			*c = '\0';
+			emft_expand_node (model, key, emft);
+		}
+
 		if (u)
 			emft_select_uri(emft, tree_path, u);
 	}
Index: mail/em-folder-tree-model.c
===================================================================
--- mail/em-folder-tree-model.c	(revision 34394)
+++ mail/em-folder-tree-model.c	(working copy)
@@ -55,6 +55,7 @@
 #include "em-utils.h"
 
 #include <camel/camel-folder.h>
+#include <camel/camel-vee-store.h>
 
 #include "em-marshal.h"
 #include "em-folder-tree-model.h"
@@ -1021,7 +1022,100 @@
 	} while (name);
 }
 
+/**
+ * emftm_uri_to_key
+ * Converts uri to key used in functions like em_folder_tree_model_[s/g]et_expanded.
+ * @param uri Uri to be converted.
+ * @return Key of the uri or NULL, if failed. Returned value should be clear by g_free.
+ **/
+static gchar *
+emftm_uri_to_key (const char *uri)
+{
+	CamelException ex = { 0 };
+	CamelStore *store;
+	CamelURL *url;
+	gchar *key;
+
+	if (!uri)
+		return NULL;
+
+	store = (CamelStore *)camel_session_get_service (session, uri, CAMEL_PROVIDER_STORE, &ex);
+	camel_exception_clear(&ex);
+
+	url = camel_url_new (uri, NULL);
+
+	if (store == NULL || url == NULL) {
+		key = NULL;
+	} else {
+		const char *path;
+		EAccount *account;
+	
+		if (((CamelService *)store)->provider->url_flags & CAMEL_URL_FRAGMENT_IS_PATH)
+			path = url->fragment;
+		else
+			path = url->path && url->path[0]=='/' ? url->path+1:url->path;
+
+		if (path == NULL)
+			path = "";
+
+		if ( (account = mail_config_get_account_by_source_url (uri)) )
+			key = g_strdup_printf ("%s/%s", account->uid, path);
+		else if (CAMEL_IS_VEE_STORE (store))
+			key = g_strdup_printf ("vfolder/%s", path);
+		else
+			key = g_strdup_printf ("local/%s", path);
+	}
+
+	if (url)
+		camel_url_free (url);
+
+	if (store)
+		camel_object_unref (store);
+
+	return key;
+}
+
+/**
+ * em_folder_tree_model_get_expanded_uri
+ * Same as @ref em_folder_tree_model_get_expanded, but here we use uri, not key for node.
+ **/
+gboolean
+em_folder_tree_model_get_expanded_uri (EMFolderTreeModel *model, const char *uri)
+{
+	gchar *key;
+	gboolean expanded;
+
+	g_return_val_if_fail (model != NULL, FALSE);
+	g_return_val_if_fail (uri != NULL, FALSE);
+
+	key = emftm_uri_to_key (uri);
+	expanded = key && em_folder_tree_model_get_expanded (model, key);
+
+	g_free (key);
+
+	return expanded;
+}
+
+/**
+ * em_folder_tree_model_set_expanded_uri
+ * Same as @ref em_folder_tree_model_set_expanded, but here we use uri, not key for node.
+ **/
 void
+em_folder_tree_model_set_expanded_uri (EMFolderTreeModel *model, const char *uri, gboolean expanded)
+{
+	gchar *key;
+
+	g_return_if_fail (model != NULL);
+	g_return_if_fail (uri != NULL);
+
+	key = emftm_uri_to_key (uri);
+	if (key)
+		em_folder_tree_model_set_expanded (model, key, expanded);
+
+	g_free (key);
+}
+
+void
 em_folder_tree_model_save_state (EMFolderTreeModel *model)
 {
 	char *dirname;
Index: mail/em-folder-tree-model.h
===================================================================
--- mail/em-folder-tree-model.h	(revision 34394)
+++ mail/em-folder-tree-model.h	(working copy)
@@ -137,6 +137,9 @@
 gboolean em_folder_tree_model_get_expanded (EMFolderTreeModel *model, const char *key);
 void em_folder_tree_model_set_expanded (EMFolderTreeModel *model, const char *key, gboolean expanded);
 
+gboolean em_folder_tree_model_get_expanded_uri (EMFolderTreeModel *model, const char *uri);
+void em_folder_tree_model_set_expanded_uri (EMFolderTreeModel *model, const char *uri, gboolean expanded);
+
 void em_folder_tree_model_save_state (EMFolderTreeModel *model);
 
 typedef void (* EMFTModelExpandFunc) (EMFolderTreeModel *model, const char *path, void *user_data);
Index: mail/mail-component.c
===================================================================
--- mail/mail-component.c	(revision 34394)
+++ mail/mail-component.c	(working copy)
@@ -700,8 +700,15 @@
 	em_folder_tree_enable_drag_and_drop ((EMFolderTree *) tree_widget);
 	
 	if ((uri = em_folder_tree_model_get_selected (priv->model))) {
+		gboolean expanded;
+
+		expanded = em_folder_tree_model_get_expanded_uri (priv->model, uri);
 		em_folder_tree_set_selected ((EMFolderTree *) tree_widget, uri, FALSE);
 		em_folder_view_set_folder_uri ((EMFolderView *) view_widget, uri);
+
+		if (!expanded)
+			em_folder_tree_model_set_expanded_uri (priv->model, uri, expanded);
+
 		g_free (uri);
 	}
 	
