Index: mail/ChangeLog
===================================================================
--- mail/ChangeLog	(revision 37218)
+++ mail/ChangeLog	(working copy)
@@ -1,3 +1,12 @@
+2009-02-04  Milan Crha  <mcrha@redhat.com>
+
+	** Part of fix for bug #563212
+
+	* em-utils.c: (try_open_e_book_cb), (try_open_e_book),
+	(is_local), (em_utils_in_addressbook), (em_utils_contact_photo):
+	Ensure empty GError before filling it. Check if group is local based
+	on its base_uri. Adapt to new e_book_cancel behaviour.
+
 2009-02-03  Srinivasa Ragavan  <sragavan@novell.com>
 
 	** Fix for bug #569986
Index: mail/em-utils.c
===================================================================
--- mail/em-utils.c	(revision 37218)
+++ mail/em-utils.c	(working copy)
@@ -2042,8 +2042,10 @@ try_open_e_book_cb (EBook *book, EBookSt
 
 	data->result = status == E_BOOK_ERROR_OK;
 
-	if (!data->result)
+	if (!data->result) {
+		g_clear_error (data->error);
 		g_set_error (data->error, E_BOOK_ERROR, status, "EBookStatus returned %d", status);
+	}
 
 	e_flag_set (data->flag);
 }
@@ -2068,6 +2070,7 @@ try_open_e_book (EBook *book, gboolean o
 
 	if (e_book_async_open (book, only_if_exists, try_open_e_book_cb, &data) != FALSE) {
 		e_flag_free (flag);
+		g_clear_error (error);
 		g_set_error (error, E_BOOK_ERROR, E_BOOK_ERROR_OTHER_ERROR, "Failed to call e_book_async_open.");
 		return FALSE;
 	}
@@ -2082,14 +2085,25 @@ try_open_e_book (EBook *book, gboolean o
 	}
 
 	if (canceled) {
+		g_clear_error (error);
 		g_set_error (error, E_BOOK_ERROR, E_BOOK_ERROR_CANCELLED, "Operation has been canceled.");
 		e_book_cancel_async_op (book, NULL);
+		/* it had been canceled, the above callback may not be called, thus setting flag here */
+		e_flag_set (flag);
 	}
 
 	e_flag_wait (flag);
 	e_flag_free (flag);
 
-	return data.result;
+	return data.result && (!error || !*error);
+}
+
+static gboolean
+is_local (ESourceGroup *group)
+{
+	return group &&
+		e_source_group_peek_base_uri (group) &&
+		g_str_has_prefix (e_source_group_peek_base_uri (group), "file://");
 }
 
 gboolean
@@ -2144,7 +2158,7 @@ em_utils_in_addressbook (CamelInternetAd
 	/* FIXME: this aint threadsafe by any measure, but what can you do eh??? */
 
 	for (g = e_source_list_peek_groups(emu_addr_list);g;g=g_slist_next(g)) {
-		if (local_only &&  e_source_group_peek_base_uri ((ESourceGroup *)g->data) && !g_str_has_prefix (e_source_group_peek_base_uri ((ESourceGroup *)g->data), "file://"))
+		if (local_only &&  !is_local (g->data))
 			continue;
 
 		for (s = e_source_group_peek_sources((ESourceGroup *)g->data);s;s=g_slist_next(s)) {
@@ -2170,20 +2184,24 @@ em_utils_in_addressbook (CamelInternetAd
 		book = e_book_new(source, &err);
 
 		if (book == NULL) {
-			g_warning("Unable to create addressbook: %s", err->message);
+			if (err && !g_error_matches (err, E_BOOK_ERROR, E_BOOK_ERROR_CANCELLED))
+				g_warning ("%s: Unable to create addressbook: %s", G_STRFUNC, err->message);
 			g_clear_error(&err);
 			continue;
 		}
 
+		g_clear_error(&err);
+
 		hook = mail_cancel_hook_add(emu_addr_cancel_book, book);
 
 		/* ignore errors, but cancellation errors we don't try to go further either */
 		if (!try_open_e_book (book, TRUE, &err)
 		    || !e_book_get_contacts(book, query, &contacts, &err)) {
-			stop = err && err->domain == E_BOOK_ERROR && err->code == E_BOOK_ERROR_CANCELLED;
+			stop = err && g_error_matches (err, E_BOOK_ERROR, E_BOOK_ERROR_CANCELLED);
 			mail_cancel_hook_remove(hook);
 			g_object_unref(book);
-			g_warning("Can't get contacts: %s", err->message);
+			if (err && !stop)
+				g_warning ("%s: Can't get contacts: %s", G_STRFUNC, err->message);
 			g_clear_error(&err);
 			continue;
 		}
@@ -2245,7 +2263,7 @@ em_utils_contact_photo (struct _CamelInt
 
     	query = e_book_query_field_test(E_CONTACT_EMAIL, E_BOOK_QUERY_IS, addr);
 	for (g = e_source_list_peek_groups(emu_addr_list); g; g = g_slist_next(g)) {
-		if (local && strcmp (e_source_group_peek_name ((ESourceGroup *)g->data), "On This Computer"))
+		if (local && !is_local (g->data))
 			continue;
 
 		for (s = e_source_group_peek_sources((ESourceGroup *)g->data); s; s=g_slist_next(s)) {
@@ -2263,14 +2281,25 @@ em_utils_contact_photo (struct _CamelInt
 		source = s->data;
 
 		book = e_book_new(source, &err);
+		if (!book) {
+			if (err && !g_error_matches (err, E_BOOK_ERROR, E_BOOK_ERROR_CANCELLED))
+				g_warning ("%s: Unable to create addressbook: %s", G_STRFUNC, err->message);
+			g_clear_error (&err);
+			continue;
+		}
+
+		g_clear_error (&err);
+
 		if (!try_open_e_book (book, TRUE, &err)
 		    || !e_book_get_contacts(book, query, &contacts, &err)) {
-			stop = err && err->domain == E_BOOK_ERROR && err->code == E_BOOK_ERROR_CANCELLED;
+			stop = err && g_error_matches (err, E_BOOK_ERROR, E_BOOK_ERROR_CANCELLED);
 			g_object_unref(book);
-			g_warning("Can't get contacts: %s", err->message);
+			if (err && !stop)
+				g_warning ("%s: Can't get contacts: %s", G_STRFUNC, err->message);
 			g_clear_error(&err);
 			continue;
 		}
+		g_clear_error (&err);
 
 		if (contacts != NULL) {
 			found = TRUE;
