GList *
e_select_names_editable_get_names (ESelectNamesEditable *esne)
{
	EDestinationStore *destination_store;
	GList *destinations;
	EDestination *destination;
	GList *result = NULL;

	g_return_val_if_fail (E_SELECT_NAMES_EDITABLE (esne), NULL);

	destination_store = e_name_selector_entry_peek_destination_store (E_NAME_SELECTOR_ENTRY (esne));
	destinations = e_destination_store_list_destinations (destination_store);
	if (!destinations)
		return NULL;

	destination = destinations->data;
	if (e_destination_is_evolution_list (destination)) {
		const GList *list_dests, *l;

		list_dests = e_destination_list_get_dests (destination);
		for (l = list_dests; l != NULL; l = g_list_next (l)) {
			result = g_list_append (result, g_strdup (e_destination_get_name (l->data)));
		}
	} else {
		result = g_list_append (result, g_strdup (e_destination_get_name (destination)));
	}

	g_list_free (destinations);

	return result;
}
void
contact_list_editor_email_entry_updated_cb (GtkWidget *widget,
                                            EDestination *destination)
{
	EContactListEditor *editor;
	ENameSelectorEntry *entry;
	EContactListModel *model;
	EDestinationStore *store;
	gchar *email;

	editor = contact_list_editor_extract (widget);

	entry = E_NAME_SELECTOR_ENTRY (widget);
	model = E_CONTACT_LIST_MODEL (editor->priv->model);

	email = g_strdup (e_destination_get_address (destination));
	store = e_name_selector_entry_peek_destination_store (entry);
	e_destination_store_remove_destination (store, destination);
	gtk_entry_set_text (GTK_ENTRY (WIDGET (EMAIL_ENTRY)), "");	

	if (email && *email) {
		e_contact_list_model_add_email (model, email);
		contact_list_editor_scroll_to_end (editor);
		editor->priv->changed = TRUE;
	}

	g_free (email);
	contact_list_editor_update (editor);
}
gchar *
e_select_names_editable_get_name (ESelectNamesEditable *esne)
{
	EDestinationStore *destination_store;
	GList *destinations;
	EDestination *destination;
	gchar *result = NULL;

	g_return_val_if_fail (E_SELECT_NAMES_EDITABLE (esne), NULL);

	destination_store = e_name_selector_entry_peek_destination_store (E_NAME_SELECTOR_ENTRY (esne));
	destinations = e_destination_store_list_destinations (destination_store);
	if (!destinations)
		return NULL;

	destination = destinations->data;
	result = g_strdup (e_destination_get_name (destination));
	g_list_free (destinations);
	return result;
}
GList *
e_select_names_editable_get_emails (ESelectNamesEditable *esne)
{
	EDestinationStore *destination_store;
	GList *destinations;
	EDestination *destination;
	GList *result = NULL;

	g_return_val_if_fail (E_SELECT_NAMES_EDITABLE (esne), NULL);

	destination_store = e_name_selector_entry_peek_destination_store (E_NAME_SELECTOR_ENTRY (esne));
	destinations = e_destination_store_list_destinations (destination_store);
	if (!destinations)
		return NULL;

	destination = destinations->data;
	if (e_destination_is_evolution_list (destination)) {
		const GList *list_dests, *l;

		list_dests = e_destination_list_get_dests (destination);
		for (l = list_dests; l != NULL; l = g_list_next (l)) {
			result = g_list_append (result, g_strdup (e_destination_get_email (l->data)));
		}
	} else {
		/* check if the contact is contact list, it does not contain all the email ids  */
		/* we dont expand it currently, TODO do we need to expand it by getting it from addressbook*/
		if (e_destination_get_contact (destination) &&
		    e_contact_get (e_destination_get_contact (destination), E_CONTACT_IS_LIST)) {
			/* If its a contact_list which is not expanded, it wont have a email id,
			   so we can use the name as the email id */

			 result = g_list_append (result, g_strdup (e_destination_get_name (destination)));
		} else
			 result = g_list_append (result, g_strdup (e_destination_get_email (destination)));
	}

	g_list_free (destinations);

	return result;
}
void
e_select_names_editable_set_address (ESelectNamesEditable *esne, const gchar *name, const gchar *email)
{
	EDestinationStore *destination_store;
	GList *destinations;
	EDestination *destination;

	g_return_if_fail (E_IS_SELECT_NAMES_EDITABLE (esne));

	destination_store = e_name_selector_entry_peek_destination_store (E_NAME_SELECTOR_ENTRY (esne));
	destinations = e_destination_store_list_destinations (destination_store);

	if (!destinations)
		destination = e_destination_new ();
	else
		destination = g_object_ref (destinations->data);

	e_destination_set_name (destination, name);
	e_destination_set_email (destination, email);

	if (!destinations)
		e_destination_store_append_destination (destination_store, destination);
	g_object_unref (destination);
}
static void
subscribe_to_folder (GtkWidget *dialog,
                     gint response,
                     gpointer data)
{
	SubscriptionInfo *subscription_info = data;
	gchar *user_email_address = NULL, *folder_name = NULL, *path = NULL;
	gchar *subscriber_email;
	EFolder *folder = NULL;
	EDestinationStore *destination_store;
	GList *destinations;
	EDestination *destination;
	ExchangeAccountFolderResult result;

	if (response == GTK_RESPONSE_CANCEL) {
		gtk_widget_destroy (dialog);
		destroy_subscription_info (subscription_info);
	}
	else if (response == GTK_RESPONSE_OK) {
		while (TRUE) {
			destination_store = e_name_selector_entry_peek_destination_store (
						E_NAME_SELECTOR_ENTRY (GTK_ENTRY (subscription_info->name_selector_widget)));
			destinations = e_destination_store_list_destinations (destination_store);
			if (!destinations)
				break;
			destination = destinations->data;
			user_email_address = g_strdup (e_destination_get_email (destination));
			g_list_free (destinations);

			if (user_email_address != NULL && *user_email_address != '\0')
				break;

			/* check if user is trying to subscribe to his own folder */
			subscriber_email = exchange_account_get_email_id (subscription_info->account);
			if (subscriber_email != NULL && *subscriber_email != '\0') {
				if (g_str_equal (subscriber_email, user_email_address)) {
					e_alert_run_dialog_for_args (GTK_WINDOW (dialog), ERROR_DOMAIN ":folder-exists-error", NULL);
					g_free (user_email_address);
					gtk_widget_destroy (dialog);
					destroy_subscription_info (subscription_info);
					return;
				}
			}

			/* It would be nice to insensitivize the OK button appropriately
			instead of doing this, but unfortunately we can't do this for the
			control.  */
			e_alert_run_dialog_for_args (GTK_WINDOW (dialog), ERROR_DOMAIN ":select-user", NULL);
		}

		folder_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (subscription_info->folder_name_entry)));
		if (user_email_address && folder_name) {
			const gchar *err_msg = NULL;
			result = exchange_account_discover_shared_folder (subscription_info->account,
									  user_email_address,
									  folder_name, &folder);
			g_free (folder_name);
			gtk_widget_hide (dialog);
			switch (result) {
				case EXCHANGE_ACCOUNT_FOLDER_OK:
					exchange_account_rescan_tree (subscription_info->account);
					if (!g_ascii_strcasecmp (e_folder_get_type_string (folder), "mail"))
						err_msg = ERROR_DOMAIN ":folder-restart-evo";
					break;
				case EXCHANGE_ACCOUNT_FOLDER_ALREADY_EXISTS:
					err_msg = ERROR_DOMAIN ":folder-exists-error";
					break;
				case EXCHANGE_ACCOUNT_FOLDER_DOES_NOT_EXIST:
					err_msg = ERROR_DOMAIN ":folder-doesnt-exist-error";
					break;
				case EXCHANGE_ACCOUNT_FOLDER_UNKNOWN_TYPE:
					err_msg = ERROR_DOMAIN ":folder-unknown-type";
					break;
				case EXCHANGE_ACCOUNT_FOLDER_PERMISSION_DENIED:
					err_msg = ERROR_DOMAIN ":folder-perm-error";
					break;
				case EXCHANGE_ACCOUNT_FOLDER_OFFLINE:
					err_msg = ERROR_DOMAIN ":folder-offline-error";
					break;
				case EXCHANGE_ACCOUNT_FOLDER_UNSUPPORTED_OPERATION:
					err_msg = ERROR_DOMAIN ":folder-unsupported-error";
					break;
				case EXCHANGE_ACCOUNT_FOLDER_GC_NOTREACHABLE:
					err_msg = ERROR_DOMAIN ":folder-no-gc-error";
					break;
				case EXCHANGE_ACCOUNT_FOLDER_NO_SUCH_USER:
					e_alert_run_dialog_for_args (GTK_WINDOW (dialog), ERROR_DOMAIN ":no-user-error", user_email_address, NULL);
					break;
				case EXCHANGE_ACCOUNT_FOLDER_GENERIC_ERROR:
					err_msg = ERROR_DOMAIN ":folder-generic-error";
					break;
				default:
					break;
			}

			if (err_msg)
				e_alert_run_dialog_for_args (GTK_WINDOW (dialog), err_msg, NULL);
		}

		if (!folder) {
			g_free (user_email_address);
			gtk_widget_destroy (dialog);
			return;
		}

		g_object_unref (folder);
		path = g_strdup_printf ("/%s", user_email_address);
		exchange_account_open_folder (subscription_info->account, path);
		g_free (path);
		g_free (user_email_address);
		gtk_widget_destroy (dialog);
		destroy_subscription_info (subscription_info);
	}
}