[evolution] Bug #682425 - Can do network operations on disabled accounts



commit 511acab89a83114aad35e9c6f76a8f284907dab9
Author: Milan Crha <mcrha redhat com>
Date:   Wed Sep 5 15:41:16 2012 +0200

    Bug #682425 - Can do network operations on disabled accounts

 libemail-engine/e-mail-folder-utils.c |    7 ++-
 libemail-engine/e-mail-utils.c        |   38 +++++++++++++-
 libemail-engine/e-mail-utils.h        |    3 +
 mail/e-mail-account-store.c           |   41 +++++++++++++++
 mail/e-mail-account-store.h           |    3 +
 mail/e-mail-backend.c                 |   91 +++++++++++++++------------------
 mail/e-mail-reader.c                  |   21 ++-----
 7 files changed, 136 insertions(+), 68 deletions(-)
---
diff --git a/libemail-engine/e-mail-folder-utils.c b/libemail-engine/e-mail-folder-utils.c
index 5d4fa2e..971a672 100644
--- a/libemail-engine/e-mail-folder-utils.c
+++ b/libemail-engine/e-mail-folder-utils.c
@@ -29,6 +29,8 @@
 #include <libemail-engine/e-mail-session.h>
 #include <libemail-engine/mail-tools.h>
 
+#include "e-mail-utils.h"
+
 /* X-Mailer header value */
 #define X_MAILER ("Evolution " VERSION SUB_VERSION " " VERSION_COMMENT)
 
@@ -298,15 +300,14 @@ mail_folder_expunge_pop3_stores (CamelFolder *folder,
 		gboolean any_found = FALSE;
 		gboolean delete_expunged = FALSE;
 		gboolean keep_on_server = FALSE;
-		gboolean enabled;
 
 		source_uid = e_source_get_uid (source);
-		enabled = e_source_get_enabled (source);
 
 		extension = e_source_get_extension (source, extension_name);
 		backend_name = e_source_backend_get_backend_name (extension);
 
-		if (!enabled || g_strcmp0 (backend_name, "pop") != 0)
+		if (!em_utils_is_source_enabled_with_parents (registry, source) ||
+		    g_strcmp0 (backend_name, "pop") != 0)
 			continue;
 
 		service = camel_session_ref_service (
diff --git a/libemail-engine/e-mail-utils.c b/libemail-engine/e-mail-utils.c
index d842b35..8f91971 100644
--- a/libemail-engine/e-mail-utils.c
+++ b/libemail-engine/e-mail-utils.c
@@ -1176,7 +1176,8 @@ second_preference:
 	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
 		ESource *temp = E_SOURCE (iter->data);
 
-		if (mail_account_in_recipients (registry, temp, recipients)) {
+		if (em_utils_is_source_enabled_with_parents (registry, temp) &&
+		    mail_account_in_recipients (registry, temp, recipients)) {
 			source = g_object_ref (temp);
 			break;
 		}
@@ -1272,6 +1273,41 @@ em_utils_ref_mail_identity_for_store (ESourceRegistry *registry,
 	return source;
 }
 
+gboolean
+em_utils_is_source_enabled_with_parents (ESourceRegistry *registry,
+					 ESource *source)
+{
+	ESource *parent;
+	const gchar *parent_uid;
+
+	g_return_val_if_fail (registry != NULL, FALSE);
+	g_return_val_if_fail (source != NULL, FALSE);
+
+	if (!e_source_get_enabled (source))
+		return FALSE;
+
+	parent = g_object_ref (source);
+	while (parent_uid = e_source_get_parent (parent), parent_uid) {
+		ESource *next = e_source_registry_ref_source (registry, parent_uid);
+
+		if (!next)
+			break;
+
+		g_object_unref (parent);
+
+		if (!e_source_get_enabled (next)) {
+			g_object_unref (next);
+			return FALSE;
+		}
+
+		parent = next;
+	}
+
+	g_object_unref (parent);
+
+	return TRUE;
+}
+
 /**
  * em_utils_uids_free:
  * @uids: array of uids
diff --git a/libemail-engine/e-mail-utils.h b/libemail-engine/e-mail-utils.h
index 1844b49..e7521d6 100644
--- a/libemail-engine/e-mail-utils.h
+++ b/libemail-engine/e-mail-utils.h
@@ -62,6 +62,9 @@ ESource *	em_utils_guess_mail_identity_with_recipients
 ESource *	em_utils_ref_mail_identity_for_store
 						(ESourceRegistry *registry,
 						 CamelStore *store);
+gboolean	em_utils_is_source_enabled_with_parents
+						(ESourceRegistry *registry,
+						 ESource *source);
 void		emu_remove_from_mail_cache	(const GSList *addresses);
 void		emu_remove_from_mail_cache_1	(const gchar *address);
 void		emu_free_mail_cache		(GDestroyNotify done_cb,
diff --git a/mail/e-mail-account-store.c b/mail/e-mail-account-store.c
index a78034a..9c777b5 100644
--- a/mail/e-mail-account-store.c
+++ b/mail/e-mail-account-store.c
@@ -1365,6 +1365,47 @@ e_mail_account_store_queue_enabled_services (EMailAccountStore *store,
 	}
 }
 
+gboolean
+e_mail_account_store_have_enabled_service (EMailAccountStore *store,
+                                           GType service_type)
+{
+	GtkTreeModel *tree_model;
+	GtkTreeIter iter;
+	gboolean iter_set;
+	gint column;
+	gboolean found = FALSE;
+
+	g_return_val_if_fail (E_IS_MAIL_ACCOUNT_STORE (store), FALSE);
+
+	tree_model = GTK_TREE_MODEL (store);
+
+	iter_set = gtk_tree_model_get_iter_first (tree_model, &iter);
+
+	while (iter_set && !found) {
+		GValue value = G_VALUE_INIT;
+		gboolean enabled;
+
+		column = E_MAIL_ACCOUNT_STORE_COLUMN_ENABLED;
+		gtk_tree_model_get_value (tree_model, &iter, column, &value);
+		enabled = g_value_get_boolean (&value);
+		g_value_unset (&value);
+
+		if (enabled) {
+			CamelService *service;
+
+			column = E_MAIL_ACCOUNT_STORE_COLUMN_SERVICE;
+			gtk_tree_model_get_value (tree_model, &iter, column, &value);
+			service = g_value_get_object (&value);
+			found = service && G_TYPE_CHECK_INSTANCE_TYPE (service, service_type);
+			g_value_unset (&value);
+		}
+
+		iter_set = gtk_tree_model_iter_next (tree_model, &iter);
+	}
+
+	return found;
+}
+
 void
 e_mail_account_store_reorder_services (EMailAccountStore *store,
                                        GQueue *ordered_services)
diff --git a/mail/e-mail-account-store.h b/mail/e-mail-account-store.h
index 063f6c0..5bd696e 100644
--- a/mail/e-mail-account-store.h
+++ b/mail/e-mail-account-store.h
@@ -134,6 +134,9 @@ void		e_mail_account_store_queue_services
 void		e_mail_account_store_queue_enabled_services
 						(EMailAccountStore *store,
 						 GQueue *out_queue);
+gboolean	e_mail_account_store_have_enabled_service
+						(EMailAccountStore *store,
+						 GType service_type);
 void		e_mail_account_store_reorder_services
 						(EMailAccountStore *store,
 						 GQueue *ordered_services);
diff --git a/mail/e-mail-backend.c b/mail/e-mail-backend.c
index 81a16ed..251bce9 100644
--- a/mail/e-mail-backend.c
+++ b/mail/e-mail-backend.c
@@ -137,14 +137,25 @@ mail_backend_prepare_for_offline_cb (EShell *shell,
 {
 	GtkWindow *window;
 	EMailSession *session;
-	ESourceRegistry *registry;
-	GList *list, *link;
-	const gchar *extension_name;
+	EMailAccountStore *account_store;
+	GQueue queue = G_QUEUE_INIT;
 	gboolean synchronize = FALSE;
 
+	if (e_shell_backend_is_started (E_SHELL_BACKEND (backend))) {
+		if (!e_activity_get_cancellable (activity)) {
+			GCancellable *cancellable;
+
+			cancellable = camel_operation_new ();
+			e_activity_set_cancellable (activity, cancellable);
+			g_object_unref (cancellable);
+		}
+
+		e_shell_backend_add_activity (E_SHELL_BACKEND (backend), activity);
+	}
+
 	window = e_shell_get_active_window (shell);
 	session = e_mail_backend_get_session (backend);
-	registry = e_mail_session_get_registry (session);
+	account_store = e_mail_ui_session_get_account_store (E_MAIL_UI_SESSION (session));
 
 	if (e_shell_get_network_available (shell) &&
 		e_shell_backend_is_started (E_SHELL_BACKEND (backend)))
@@ -157,34 +168,21 @@ mail_backend_prepare_for_offline_cb (EShell *shell,
 			CAMEL_SESSION (session), FALSE);
 	}
 
-	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
-	list = e_source_registry_list_sources (registry, extension_name);
-
-	for (link = list; link != NULL; link = g_list_next (link)) {
-		ESource *source = E_SOURCE (link->data);
+	e_mail_account_store_queue_enabled_services (account_store, &queue);
+	while (!g_queue_is_empty (&queue)) {
 		CamelService *service;
-		const gchar *uid;
-
-		uid = e_source_get_uid (source);
-		service = camel_session_ref_service (
-			CAMEL_SESSION (session), uid);
 
+		service = g_queue_pop_head (&queue);
 		if (service == NULL)
 			continue;
 
-		/* FIXME Not passing a GCancellable. */
 		if (CAMEL_IS_STORE (service))
 			e_mail_store_go_offline (
-				CAMEL_STORE (service),
-				G_PRIORITY_DEFAULT,
-				NULL, (GAsyncReadyCallback)
-				mail_backend_store_operation_done_cb,
+				CAMEL_STORE (service), G_PRIORITY_DEFAULT,
+				e_activity_get_cancellable (activity),
+				(GAsyncReadyCallback) mail_backend_store_operation_done_cb,
 				g_object_ref (activity));
-
-		g_object_unref (service);
 	}
-
-	g_list_free_full (list, (GDestroyNotify) g_object_unref);
 }
 
 static void
@@ -193,46 +191,41 @@ mail_backend_prepare_for_online_cb (EShell *shell,
                                     EMailBackend *backend)
 {
 	EMailSession *session;
-	ESourceRegistry *registry;
-	GList *list, *link;
-	const gchar *extension_name;
+	EMailAccountStore *account_store;
+	GQueue queue = G_QUEUE_INIT;
 
-	session = e_mail_backend_get_session (backend);
-	registry = e_mail_session_get_registry (session);
+	if (e_shell_backend_is_started (E_SHELL_BACKEND (backend))) {
+		if (!e_activity_get_cancellable (activity)) {
+			GCancellable *cancellable;
 
-	camel_session_set_online (CAMEL_SESSION (session), TRUE);
+			cancellable = camel_operation_new ();
+			e_activity_set_cancellable (activity, cancellable);
+			g_object_unref (cancellable);
+		}
 
-	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
-	list = e_source_registry_list_sources (registry, extension_name);
+		e_shell_backend_add_activity (E_SHELL_BACKEND (backend), activity);
+	}
 
-	for (link = list; link != NULL; link = g_list_next (link)) {
-		ESource *source = E_SOURCE (link->data);
-		CamelService *service;
-		const gchar *uid;
+	session = e_mail_backend_get_session (backend);
+	account_store = e_mail_ui_session_get_account_store (E_MAIL_UI_SESSION (session));
 
-		if (!e_source_get_enabled (source))
-			continue;
+	camel_session_set_online (CAMEL_SESSION (session), TRUE);
 
-		uid = e_source_get_uid (source);
-		service = camel_session_ref_service (
-			CAMEL_SESSION (session), uid);
+	e_mail_account_store_queue_enabled_services (account_store, &queue);
+	while (!g_queue_is_empty (&queue)) {
+		CamelService *service;
 
+		service = g_queue_pop_head (&queue);
 		if (service == NULL)
 			continue;
 
-		/* FIXME Not passing a GCancellable. */
 		if (CAMEL_IS_STORE (service))
 			e_mail_store_go_online (
-				CAMEL_STORE (service),
-				G_PRIORITY_DEFAULT,
-				NULL, (GAsyncReadyCallback)
-				mail_backend_store_operation_done_cb,
+				CAMEL_STORE (service), G_PRIORITY_DEFAULT,
+				e_activity_get_cancellable (activity),
+				(GAsyncReadyCallback) mail_backend_store_operation_done_cb,
 				g_object_ref (activity));
-
-		g_object_unref (service);
 	}
-
-	g_list_free_full (list, (GDestroyNotify) g_object_unref);
 }
 
 /* Helper for mail_backend_prepare_for_quit_cb() */
diff --git a/mail/e-mail-reader.c b/mail/e-mail-reader.c
index 659e569..963b9e0 100644
--- a/mail/e-mail-reader.c
+++ b/mail/e-mail-reader.c
@@ -48,6 +48,7 @@
 #include "mail/e-mail-backend.h"
 #include "mail/e-mail-browser.h"
 #include "mail/e-mail-reader-utils.h"
+#include "mail/e-mail-ui-session.h"
 #include "mail/e-mail-view.h"
 #include "mail/em-composer-utils.h"
 #include "mail/em-event.h"
@@ -4118,8 +4119,8 @@ e_mail_reader_check_state (EMailReader *reader)
 	CamelStore *store = NULL;
 	EMailBackend *backend;
 	ESourceRegistry *registry;
-	GList *list, *iter;
-	const gchar *extension_name;
+	EMailSession *mail_session;
+	EMailAccountStore *account_store;
 	const gchar *tag;
 	gboolean can_clear_flags = FALSE;
 	gboolean can_flag_completed = FALSE;
@@ -4146,6 +4147,8 @@ e_mail_reader_check_state (EMailReader *reader)
 	backend = e_mail_reader_get_backend (reader);
 	shell = e_shell_backend_get_shell (E_SHELL_BACKEND (backend));
 	registry = e_shell_get_registry (shell);
+	mail_session = e_mail_backend_get_session (backend);
+	account_store = e_mail_ui_session_get_account_store (E_MAIL_UI_SESSION (mail_session));
 
 	folder = e_mail_reader_get_folder (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
@@ -4242,19 +4245,7 @@ e_mail_reader_check_state (EMailReader *reader)
 		camel_folder_free_message_info (folder, info);
 	}
 
-	extension_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
-	list = e_source_registry_list_sources (registry, extension_name);
-
-	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
-		ESource *source = E_SOURCE (iter->data);
-
-		if (e_source_get_enabled (source)) {
-			have_enabled_account = TRUE;
-			break;
-		}
-	}
-
-	g_list_free_full (list, (GDestroyNotify) g_object_unref);
+	have_enabled_account = e_mail_account_store_have_enabled_service (account_store, CAMEL_TYPE_STORE);
 
 	if (have_enabled_account)
 		state |= E_MAIL_READER_HAVE_ENABLED_ACCOUNT;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]