ews_store_subscribe_folder_sync (CamelSubscribable *subscribable,
                                 const gchar *folder_name,
                                 GCancellable *cancellable,
                                 GError **error)
{
	CamelEwsStore *ews_store = CAMEL_EWS_STORE (subscribable);
	EEwsFolder *folder;
	const EwsFolderId *fid;
	gboolean res = TRUE;
	gchar *tmp;

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

	/* Folder's name is not stored with the "/" in the beginning, even when the
	   folder is in the root (same level as "Inbox", "Drafts", etc. */
	if (*folder_name == '/')
		folder_name++;

	/* can subscribe only public folders,
	   thus skip anything known */
	tmp = camel_ews_store_summary_get_folder_id_from_name (ews_store->summary, folder_name);
	if (tmp) {
		g_free (tmp);
		return TRUE;
	}

	g_mutex_lock (&ews_store->priv->get_finfo_lock);
	if (!ews_store->priv->public_folders) {
		g_mutex_unlock (&ews_store->priv->get_finfo_lock);

		g_set_error (
			error, CAMEL_STORE_ERROR, CAMEL_STORE_ERROR_NO_FOLDER,
			_("Cannot subscribe folder “%s”, no public folder available"), folder_name);
		return FALSE;
	}

	folder = ews_store_find_public_folder (ews_store, folder_name);
	if (!folder) {
		g_mutex_unlock (&ews_store->priv->get_finfo_lock);

		g_set_error (
			error, CAMEL_STORE_ERROR, CAMEL_STORE_ERROR_NO_FOLDER,
			_("Cannot subscribe folder “%s”, folder not found"), folder_name);
		return FALSE;
	}

	fid = e_ews_folder_get_id (folder);

	g_return_val_if_fail (fid != NULL, FALSE);

	if (camel_ews_store_summary_has_folder (ews_store->summary, EWS_PUBLIC_FOLDER_ROOT_ID)) {
		gchar *parent_name = camel_ews_store_summary_get_folder_name (ews_store->summary, EWS_PUBLIC_FOLDER_ROOT_ID, NULL);

		g_return_val_if_fail (parent_name != NULL, FALSE);

		tmp = g_strconcat (parent_name, "/", e_ews_folder_get_escaped_name (folder), NULL);
		g_free (parent_name);
	} else {
		tmp = g_strconcat (EWS_PUBLIC_FOLDER_ROOT_DISPLAY_NAME, "/", e_ews_folder_get_escaped_name (folder), NULL);
	}

	if (e_ews_folder_get_folder_type (folder) != 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_add_as_esource (registry,
			camel_ews_settings_get_hosturl (ews_settings),
			camel_network_settings_get_user (CAMEL_NETWORK_SETTINGS (ews_settings)),
			folder,
			E_EWS_ESOURCE_FLAG_OFFLINE_SYNC | E_EWS_ESOURCE_FLAG_PUBLIC_FOLDER,
			0,
			cancellable,
			error);

		g_object_unref (session);
		g_object_unref (settings);
	}

	if (res) {
		ews_store_ensure_unique_path (ews_store, &tmp);

		camel_ews_store_summary_new_folder (ews_store->summary, fid->id, EWS_PUBLIC_FOLDER_ROOT_ID,
			NULL,
			strrchr (tmp, '/') + 1,
			e_ews_folder_get_folder_type (folder),
			CAMEL_FOLDER_SUBSCRIBED,
			e_ews_folder_get_total_count (folder),
			FALSE, TRUE);

		if (e_ews_folder_get_folder_type (folder) == E_EWS_FOLDER_TYPE_MAILBOX) {
			CamelFolderInfo *fi;

			camel_ews_store_ensure_virtual_folders (ews_store);

			fi = camel_ews_utils_build_folder_info (ews_store, fid->id);
			camel_store_folder_created (CAMEL_STORE (ews_store), fi);
			camel_subscribable_folder_subscribed (CAMEL_SUBSCRIBABLE (ews_store), fi);
			camel_folder_info_free (fi);
		}
	}

	camel_ews_store_summary_save (ews_store->summary, NULL);

	g_free (tmp);
	g_mutex_unlock (&ews_store->priv->get_finfo_lock);

	return res;
}