Index: plugins/imap-features/imap-headers.c
===================================================================
--- plugins/imap-features/imap-headers.c	(revision 36808)
+++ plugins/imap-features/imap-headers.c	(working copy)
@@ -92,9 +92,10 @@ imap_headers_commit (EPlugin *efp, EConf
 		GString *str;
 		gchar *header = NULL;
 
-		str = g_string_new("");
-
 		temp = mail_config_get_account_by_source_url (account->source->url);
+		g_return_if_fail (temp != NULL);
+
+		str = g_string_new("");
 
 		url = camel_url_new (e_account_get_string(account, E_ACCOUNT_SOURCE_URL), &ex);
 
Index: plugins/imap-features/ChangeLog
===================================================================
--- plugins/imap-features/ChangeLog	(revision 36808)
+++ plugins/imap-features/ChangeLog	(working copy)
@@ -1,3 +1,10 @@
+2008-11-20  Milan Crha  <mcrha@redhat.com>
+
+	** Part of fix for bug #560188
+
+	* imap-headers.c: (imap_headers_commit):
+	Sanity check for non-NULL account returned from mail-config.
+
 2008-10-03  Sankar P  <psankar@novell.com>
 
 License Changes
Index: mail/ChangeLog
===================================================================
--- mail/ChangeLog	(revision 36808)
+++ mail/ChangeLog	(working copy)
@@ -1,3 +1,15 @@
+2008-11-20  Milan Crha  <mcrha@redhat.com>
+
+	** Fix for bug #560188
+
+	* em-folder-tree-model.c: (account_changed):
+	Sanity check for non-NULL account passed in.
+	* mail-config.c: (mc_find_account_by_url), (get_service_source),
+	(get_service_transport), (mail_config_get_account_by_source_url),
+	(mail_config_get_account_by_transport_url):
+	Use the same function for similar things and let compare URLs
+	the respective provider as Sankar suggested.
+
 2008-11-19  Srinivasa Ragavan  <sragavan@novell.com>
 
 	** Fix for bug #555276
Index: mail/em-folder-tree-model.c
===================================================================
--- mail/em-folder-tree-model.c	(revision 36808)
+++ mail/em-folder-tree-model.c	(working copy)
@@ -393,6 +393,8 @@ account_changed (EAccountList *accounts,
 	CamelException ex;
 	char *uri;
 
+	g_return_if_fail (account != NULL);
+
 	if (!(si = g_hash_table_lookup (model->account_hash, account)))
 		return;
 
Index: mail/mail-config.c
===================================================================
--- mail/mail-config.c	(revision 36808)
+++ mail/mail-config.c	(working copy)
@@ -800,102 +800,98 @@ mail_config_get_account_by_uid (const ch
 	return (EAccount *) e_account_list_find (config->accounts, E_ACCOUNT_FIND_UID, uid);
 }
 
-EAccount *
-mail_config_get_account_by_source_url (const char *source_url)
+static EAccount *
+mc_find_account_by_url (const char *test_url, const EAccountService * (*get_service) (EAccount *account))
 {
 	EAccount *account = NULL;
 	EIterator *iter;
+	CamelURL *url1, *url2;
 
-	g_return_val_if_fail (source_url != NULL, NULL);
+	g_return_val_if_fail (test_url != NULL, NULL);
+	g_return_val_if_fail (get_service != NULL, NULL);
+
+	url1 = camel_url_new (test_url, NULL);
+	g_return_val_if_fail (url1 != NULL, NULL);
 
 	iter = e_list_get_iterator ((EList *) config->accounts);
 	while (e_iterator_is_valid (iter)) {
-		CamelURL *url;
-		gchar *string;
+		const EAccountService *service;
+		CamelProvider *provider;
 
 		account = (EAccount *) e_iterator_get (iter);
+		service = get_service (account);
 
 		e_iterator_next (iter);
 
-		if (account->source == NULL)
+		if (service == NULL)
+			continue;
+
+		else if (service->url == NULL)
 			continue;
 
-		else if (account->source->url == NULL)
+		else if (*service->url == '\0')
 			continue;
 
-		else if (*account->source->url == '\0')
+		url2 = camel_url_new (service->url, NULL);
+		if (!url2)
 			continue;
 
-		url = camel_url_new (account->source->url, NULL);
-		if (url == NULL)
+		provider = camel_provider_get (service->url, NULL);
+		if (provider == NULL) {
+			camel_url_free (url2);
 			continue;
+		}
 
-		/* Simplify the account URL for comparison. */
-		string = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
-		if (string == NULL || strcmp (string, source_url) != 0)
+		if (!provider->url_equal (url1, url2))
 			account = NULL;  /* not a match */
 
-		camel_url_free (url);
-		g_free (string);
+		camel_url_free (url2);
 
 		if (account != NULL) {
+			camel_url_free (url1);
 			g_object_unref (iter);
 			return account;
 		}
 	}
 
 	g_object_unref (iter);
+	camel_url_free (url1);
 
 	return NULL;
 }
 
-EAccount *
-mail_config_get_account_by_transport_url (const char *transport_url)
+static const EAccountService *
+get_service_source (EAccount *account)
 {
-	EAccount *account = NULL;
-	EIterator *iter;
-
-	g_return_val_if_fail (transport_url != NULL, NULL);
-
-	iter = e_list_get_iterator ((EList *) config->accounts);
-	while (e_iterator_is_valid (iter)) {
-		CamelURL *url;
-		gchar *string;
-
-		account = (EAccount *) e_iterator_get (iter);
-
-		e_iterator_next (iter);
-
-		if (account->transport == NULL)
-			continue;
-
-		else if (account->transport->url == NULL)
-			continue;
+	if (!account)
+		return NULL;
 
-		else if (*account->transport->url == '\0')
-			continue;
+	return account->source;
+}
 
-		url = camel_url_new (account->transport->url, NULL);
-		if (url == NULL)
-			continue;
+static const EAccountService *
+get_service_transport (EAccount *account)
+{
+	if (!account)
+		return NULL;
 
-		/* Simplify the account URL for comparison. */
-		string = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
-		if (string == NULL || strcmp (string, transport_url) != 0)
-			account = NULL;  /* not a match */
+	return account->transport;
+}
 
-		camel_url_free (url);
-		g_free (string);
+EAccount *
+mail_config_get_account_by_source_url (const char *source_url)
+{
+	g_return_val_if_fail (source_url != NULL, NULL);
 
-		if (account != NULL) {
-			g_object_unref (iter);
-			return account;
-		}
-	}
+	return mc_find_account_by_url (source_url, get_service_source);
+}
 
-	g_object_unref (iter);
+EAccount *
+mail_config_get_account_by_transport_url (const char *transport_url)
+{
+	g_return_val_if_fail (transport_url != NULL, NULL);
 
-	return NULL;
+	return mc_find_account_by_url (transport_url, get_service_transport);
 }
 
 int
