ews_store_unsubscribe_folder_sync (CamelSubscribable *subscribable,
                                   const gchar *folder_name,
                                   GCancellable *cancellable,
                                   GError **error)
{
	CamelEwsStore *ews_store = CAMEL_EWS_STORE (subscribable);
	EEwsFolderType folder_type;
	EEwsFolder *folder;
	gboolean is_public;
	gboolean res = TRUE;
	gchar *fid = NULL;

	if (!camel_offline_store_get_online (CAMEL_OFFLINE_STORE (ews_store))) {
		g_set_error_literal (
			error, CAMEL_SERVICE_ERROR, CAMEL_SERVICE_ERROR_UNAVAILABLE,
			_("Cannot unsubscribe EWS folders in offline mode"));
		return FALSE;
	}

	folder = ews_store_find_public_folder (ews_store, folder_name);
	if (folder) {
		const EwsFolderId *folder_id = e_ews_folder_get_id (folder);

		if (folder_id) {
			fid = g_strdup (folder_id->id);
			folder_type = e_ews_folder_get_folder_type (folder);
		}
	}

	if (!fid) {
		fid = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, folder_name);
		if (fid)
			folder_type = camel_ews_store_summary_get_folder_type (ews_store->summary, fid, NULL);
	}

	if (!fid) {
		/* no such folder in the cache, might be unsubscribed already */
		return TRUE;
	}

	is_public = camel_ews_store_summary_get_public (ews_store->summary, fid, NULL);
	if (!is_public && !camel_ews_store_summary_get_foreign (ews_store->summary, fid, NULL)) {
		/* nothing to do for regular folders */
		res = TRUE;
	} else {
		CamelFolderInfo *fi;

		if (!is_public && camel_ews_store_summary_get_foreign_subfolders (ews_store->summary, fid, NULL)) {
			/* when subscribed with subfolders, then unsubscribe with subfolders as well */
			GSList *local_folders = NULL, *ii;
			gchar *full_name = camel_ews_store_summary_get_folder_full_name (ews_store->summary, fid, NULL);
			if (full_name) {
				local_folders = camel_ews_store_summary_get_folders (ews_store->summary, full_name);
			}
			g_free (full_name);

			for (ii = local_folders; ii != NULL; ii = ii->next) {
				const gchar *lfid = ii->data;
				EEwsFolderType ftype;

				if (g_strcmp0 (lfid, fid) == 0)
					continue;

				ftype = camel_ews_store_summary_get_folder_type (ews_store->summary, lfid, NULL);
				if (ftype != E_EWS_FOLDER_TYPE_MAILBOX)
					continue;

				fi = camel_ews_utils_build_folder_info (ews_store, lfid);
				camel_ews_store_summary_remove_folder (ews_store->summary, lfid, NULL);

				camel_subscribable_folder_unsubscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
				camel_store_folder_deleted (CAMEL_STORE (ews_store), fi);
				camel_folder_info_free (fi);
			}

			g_slist_free_full (local_folders, g_free);
		}

		if (folder_type != E_EWS_FOLDER_TYPE_MAILBOX) {
			CamelSession *session;
			CamelSettings *settings;
			CamelEwsSettings *ews_settings;
			ESourceRegistry *registry = NULL;

			settings = camel_service_ref_settings (CAMEL_SERVICE (ews_store));
			ews_settings = CAMEL_EWS_SETTINGS (settings);
			session = camel_service_ref_session (CAMEL_SERVICE (ews_store));
			if (E_IS_MAIL_SESSION (session))
				registry = e_mail_session_get_registry (E_MAIL_SESSION (session));

			res = e_ews_folder_utils_remove_as_esource (registry,
				camel_ews_settings_get_hosturl (ews_settings),
				camel_network_settings_get_user (CAMEL_NETWORK_SETTINGS (ews_settings)),
				fid,
				cancellable,
				error);

			g_object_unref (session);
			g_object_unref (settings);
		}

		if (res) {
			fi = camel_ews_utils_build_folder_info (ews_store, fid);
			camel_ews_store_summary_remove_folder (ews_store->summary, fid, error);

			if (folder_type == E_EWS_FOLDER_TYPE_MAILBOX) {
				camel_subscribable_folder_unsubscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
				camel_store_folder_deleted (CAMEL_STORE (ews_store), fi);
				camel_folder_info_free (fi);

				camel_ews_store_ensure_virtual_folders (ews_store);
			}
		}

		camel_ews_store_summary_save (ews_store->summary, NULL);
	}

	g_free (fid);

	return res;
}