[evolution/account-mgmt: 42/49] Adapt mail-to-task plugin to the new ESource API.



commit 3b827b5aa359b4041e822e8b8bb2b0dc8e3cc136
Author: Matthew Barnes <mbarnes redhat com>
Date:   Mon Mar 28 11:15:04 2011 -0400

    Adapt mail-to-task plugin to the new ESource API.

 plugins/mail-to-task/mail-to-task.c |  162 +++++++++++++++++++---------------
 1 files changed, 90 insertions(+), 72 deletions(-)
---
diff --git a/plugins/mail-to-task/mail-to-task.c b/plugins/mail-to-task/mail-to-task.c
index a4713c5..5722e1a 100644
--- a/plugins/mail-to-task/mail-to-task.c
+++ b/plugins/mail-to-task/mail-to-task.c
@@ -35,9 +35,10 @@
 #include <gtkhtml/gtkhtml.h>
 #include <libecal/e-cal-client.h>
 #include <libecal/e-cal-component.h>
-#include <libedataserver/e-account.h>
-#include <libedataserver/e-data-server-util.h>
 #include <libedataserver/e-flag.h>
+#include <libedataserver/e-data-server-util.h>
+#include <libedataserver/e-source-calendar.h>
+#include <libedataserver/e-source-mail-identity.h>
 #include <libedataserverui/e-source-selector-dialog.h>
 #include <libedataserverui/e-client-utils.h>
 
@@ -45,7 +46,6 @@
 #include <mail/em-utils.h>
 #include <mail/em-format-html.h>
 #include <mail/message-list.h>
-#include <e-util/e-account-utils.h>
 #include <e-util/e-dialog-utils.h>
 #include <misc/e-popup-action.h>
 #include <shell/e-shell-view.h>
@@ -80,11 +80,14 @@ get_component_editor (EShell *shell,
 	ECalComponentId *id;
 	CompEditorFlags flags = 0;
 	CompEditor *editor = NULL;
+	ESourceRegistry *registry;
 
 	g_return_val_if_fail (E_IS_SHELL (shell), NULL);
 	g_return_val_if_fail (E_IS_CAL_CLIENT (client), NULL);
 	g_return_val_if_fail (E_IS_CAL_COMPONENT (comp), NULL);
 
+	registry = e_shell_get_registry (shell);
+
 	id = e_cal_component_get_id (comp);
 	g_return_val_if_fail (id != NULL, NULL);
 	g_return_val_if_fail (id->uid != NULL, NULL);
@@ -96,7 +99,7 @@ get_component_editor (EShell *shell,
 	}
 
 	if (!editor) {
-		if (itip_organizer_is_user (comp, client))
+		if (itip_organizer_is_user (registry, comp, client))
 			flags |= COMP_EDITOR_USER_ORG;
 
 		switch (e_cal_component_get_vtype (comp)) {
@@ -315,38 +318,47 @@ static gchar *
 set_organizer (ECalComponent *comp,
                CamelFolder *folder)
 {
-	EAccount *account = NULL;
-	const gchar *str, *name;
+	EShell *shell;
+	ESource *source = NULL;
+	ESourceRegistry *registry;
+	ESourceMailIdentity *extension;
+	const gchar *extension_name;
+	const gchar *address, *name;
 	ECalComponentOrganizer organizer = {NULL, NULL, NULL, NULL};
-	gchar *res;
+	gchar *mailto;
 
-	if (folder) {
+	shell = e_shell_get_default ();
+	registry = e_shell_get_registry (shell);
+
+	if (folder != NULL) {
 		CamelStore *store;
 		const gchar *uid;
 
 		store = camel_folder_get_parent_store (folder);
 		uid = camel_service_get_uid (CAMEL_SERVICE (store));
-
-		account = e_get_account_by_uid (uid);
+		source = e_source_registry_lookup_by_uid (registry, uid);
 	}
 
-	if (!account)
-		account = e_get_default_account ();
-	if (!account)
-		return NULL;
+	if (source == NULL)
+		source = e_source_registry_get_default_mail_account (registry);
+
+	g_return_val_if_fail (E_IS_SOURCE (source), NULL);
+
+	extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
+	extension = e_source_get_extension (source, extension_name);
 
-	str = e_account_get_string (account, E_ACCOUNT_ID_ADDRESS);
-	name = e_account_get_string (account, E_ACCOUNT_ID_NAME);
+	name = e_source_mail_identity_get_name (extension);
+	address = e_source_mail_identity_get_address (extension);
 
-	if (!str)
+	if (address == NULL)
 		return NULL;
 
-	res = g_strconcat ("mailto:";, str, NULL);
-	organizer.value = res;
+	mailto = g_strconcat ("mailto:";, address, NULL);
+	organizer.value = mailto;
 	organizer.cn = name;
 	e_cal_component_set_organizer (comp, &organizer);
 
-	return res;
+	return mailto;
 }
 
 struct _att_async_cb_data {
@@ -1071,65 +1083,84 @@ mail_to_event (ECalClientSourceType source_type,
                gboolean with_attendees,
                EMailReader *reader)
 {
+	EShell *shell;
+	EMailBackend *backend;
+	ESourceRegistry *registry;
 	CamelFolder *folder;
 	GPtrArray *uids;
-	ESourceList *source_list = NULL;
-	GSList *groups, *p;
-	ESource *source = NULL, *default_source = NULL;
+	ESource *source = NULL;
+	ESource *default_source;
+	GList *list, *iter;
+	GtkWindow *parent;
+	const gchar *extension_name;
 	GError *error = NULL;
-	gint writable_sources = 0;
 
 	folder = e_mail_reader_get_folder (reader);
+	parent = e_mail_reader_get_window (reader);
 	uids = e_mail_reader_get_selected_uids (reader);
 
-	if (!e_cal_client_get_sources (&source_list, source_type, &error)) {
-		e_notice (NULL, GTK_MESSAGE_ERROR, _("Cannot get source list. %s"), error ? error->message : _("Unknown error."));
-		if (error)
-			g_error_free (error);
-		em_utils_uids_free (uids);
-		g_object_unref (folder);
-		return;
-	}
-
-	/* Ask before converting 10 or more mails to events */
+	/* Ask before converting 10 or more mails to events. */
 	if (uids->len > 10) {
-		gchar *question = g_strdup_printf (get_question_add_all_mails (source_type), uids->len);
-		if (do_ask (question, FALSE) == GTK_RESPONSE_NO) {
-			g_free (question);
-			g_object_unref (source_list);
+		gchar *question;
+		gint response;
+
+		question = g_strdup_printf (
+			get_question_add_all_mails (source_type), uids->len);
+		response = do_ask (question, FALSE);
+		g_free (question);
+
+		if (response == GTK_RESPONSE_NO) {
 			em_utils_uids_free (uids);
 			g_object_unref (folder);
 			return;
 		}
-		g_free (question);
 	}
 
-	/* Find 'Default' source. When no source is default, ask user to pick one */
-	groups = e_source_list_peek_groups (source_list);
-	for (p = groups; p != NULL; p = p->next) {
-		ESourceGroup *group = E_SOURCE_GROUP (p->data);
-		GSList *sources, *q;
+	backend = e_mail_reader_get_backend (reader);
+	shell = e_shell_backend_get_shell (E_SHELL_BACKEND (backend));
+	registry = e_shell_get_registry (shell);
+
+	switch (source_type) {
+		case E_CAL_CLIENT_SOURCE_TYPE_EVENTS:
+			extension_name = E_SOURCE_EXTENSION_CALENDAR;
+			default_source = e_source_registry_get_default_calendar (registry);
+			break;
+		case E_CAL_CLIENT_SOURCE_TYPE_MEMOS:
+			extension_name = E_SOURCE_EXTENSION_MEMO_LIST;
+			default_source = e_source_registry_get_default_memo_list (registry);
+			break;
+		case E_CAL_CLIENT_SOURCE_TYPE_TASKS:
+			extension_name = E_SOURCE_EXTENSION_TASK_LIST;
+			default_source = e_source_registry_get_default_task_list (registry);
+			break;
+		default:
+			g_return_if_reached ();
+	}
 
-		sources = e_source_group_peek_sources (group);
-		for (q = sources; q != NULL; q = q->next) {
-			ESource *s = E_SOURCE (q->data);
+	list = e_source_registry_list_sources (registry, extension_name);
 
-			if (s && e_source_get_property (s, "default") && !e_source_get_readonly (s)) {
-				default_source = s;
-			}
+	/* If there is only one writable source, no need to prompt the user. */
+	for (iter = list; iter != NULL; iter = g_list_next (iter)) {
+		ESource *candidate = E_SOURCE (iter->data);
 
-			if (s && !e_source_get_readonly (s)) {
-				writable_sources++;
-				source = s;
+		if (e_source_get_writable (candidate)) {
+			if (source == NULL)
+				source = candidate;
+			else {
+				source = NULL;
+				break;
 			}
 		}
 	}
 
-	if (writable_sources > 1) {
+	g_list_free (list);
+
+	if (source == NULL) {
 		GtkWidget *dialog;
 
 		/* ask the user which tasks list to save to */
-		dialog = e_source_selector_dialog_new (NULL, source_list);
+		dialog = e_source_selector_dialog_new (
+			parent, registry, extension_name);
 
 		e_source_selector_dialog_select_default_source (
 			E_SOURCE_SELECTOR_DIALOG (dialog));
@@ -1137,8 +1168,6 @@ mail_to_event (ECalClientSourceType source_type,
 		if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
 			source = e_source_selector_dialog_peek_primary_selection (
 				E_SOURCE_SELECTOR_DIALOG (dialog));
-		else
-			source = NULL;
 
 		gtk_widget_destroy (dialog);
 	} else if (!source && default_source) {
@@ -1146,7 +1175,6 @@ mail_to_event (ECalClientSourceType source_type,
 	} else if (!source) {
 		e_notice (NULL, GTK_MESSAGE_ERROR, _("No writable calendar is available."));
 
-		g_object_unref (source_list);
 		em_utils_uids_free (uids);
 		g_object_unref (folder);
 		if (error)
@@ -1163,21 +1191,13 @@ mail_to_event (ECalClientSourceType source_type,
 
 		client = e_cal_client_new (source, source_type, &error);
 		if (!client) {
-			gchar *uri = e_source_get_uri (source);
-
-			e_notice (NULL, GTK_MESSAGE_ERROR, "Could not create the client '%s': %s", uri, error ? error->message : "Unknown error");
-
-			g_free (uri);
-			g_object_unref (source_list);
-			em_utils_uids_free (uids);
-			g_object_unref (folder);
-			if (error)
-				g_error_free (error);
+			e_notice (
+				parent, GTK_MESSAGE_ERROR,
+				"Could not connect to '%s'",
+				e_source_get_display_name (source));
 			return;
 		}
 
-		g_signal_connect (client, "authenticate", G_CALLBACK (e_client_utils_authenticate_handler), NULL);
-
 		/* Fill the elements in AsynData */
 		data = g_new0 (AsyncData, 1);
 		data->client = client;
@@ -1196,8 +1216,6 @@ mail_to_event (ECalClientSourceType source_type,
 			g_error_free (error);
 		}
 	}
-
-	g_object_unref (source_list);
 }
 
 static void



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