<commit_before>/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* camel-ews-utils.c
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <glib/gstdio.h>

#include "camel-ews-utils.h"
#include "ews-esource-utils.h"

#define SUBFOLDER_DIR_NAME     "subfolders"
#define SUBFOLDER_DIR_NAME_LEN 10

/**
 * e_path_to_physical:
 * @prefix: a prefix to prepend to the path, or %NULL
 * @path: the virtual path to convert to a filesystem path.
 *
 * This converts the "virtual" path @path into an expanded form that
 * allows a given name to refer to both a file and a directory. The
 * expanded path will have a "subfolders" directory inserted between
 * each path component. If the path ends with "/", the returned
 * physical path will end with "/subfolders"
 *
 * If @prefix is non-%NULL, it will be prepended to the returned path.
 *
 * Returns: the expanded path
 **/
gchar *
e_path_to_physical (const gchar *prefix, const gchar *vpath)
{
	const gchar *p, *newp;
	gchar *dp;
	gchar *ppath;
	gint ppath_len;
	gint prefix_len;

	while (*vpath == '/')
		vpath++;
	if (!prefix)
		prefix = "";

	/* Calculate the length of the real path. */
	ppath_len = strlen (vpath);
	ppath_len++;	/* For the ending zero.  */

	prefix_len = strlen (prefix);
	ppath_len += prefix_len;
	ppath_len++;	/* For the separating slash.  */

	/* Take account of the fact that we need to translate every
	 * separator into 'subfolders/'.
	 */
	p = vpath;
	while (1) {
		newp = strchr (p, '/');
		if (newp == NULL)
			break;

		ppath_len += SUBFOLDER_DIR_NAME_LEN;
		ppath_len++; /* For the separating slash.  */

		/* Skip consecutive slashes.  */
		while (*newp == '/')
			newp++;

		p = newp;
	};

	ppath = g_malloc (ppath_len);
	dp = ppath;

	memcpy (dp, prefix, prefix_len);
	dp += prefix_len;
	*(dp++) = '/';

	/* Copy the mangled path.  */
	p = vpath;
	while (1) {
		newp = strchr (p, '/');
		if (newp == NULL) {
			strcpy (dp, p);
			break;
		}

		memcpy (dp, p, newp - p + 1); /* '+ 1' to copy the slash too.  */
		dp += newp - p + 1;

		memcpy (dp, SUBFOLDER_DIR_NAME, SUBFOLDER_DIR_NAME_LEN);
		dp += SUBFOLDER_DIR_NAME_LEN;

		*(dp++) = '/';

		/* Skip consecutive slashes.  */
		while (*newp == '/')
			newp++;

		p = newp;
	}

	return ppath;
}

static gboolean
find_folders_recursive (const gchar *physical_path, const gchar *path,
			EPathFindFoldersCallback callback, gpointer data)
{
	GDir *dir;
	gchar *subfolder_directory_path;
	gboolean ok;

	if (*path) {
		if (!callback (physical_path, path, data))
			return FALSE;

		subfolder_directory_path = g_strdup_printf ("%s/%s", physical_path, SUBFOLDER_DIR_NAME);
	} else {
		/* On the top level, we have no folders and,
		 * consequently, no subfolder directory.
		 */

		subfolder_directory_path = g_strdup (physical_path);
	}

	/* Now scan the subfolders and load them. */
	dir = g_dir_open (subfolder_directory_path, 0, NULL);
	if (dir == NULL) {
		g_free (subfolder_directory_path);
		return TRUE;
	}

	ok = TRUE;
	while (ok) {
		struct stat file_stat;
		const gchar *dirent;
		gchar *file_path;
		gchar *new_path;

		dirent = g_dir_read_name (dir);
		if (dirent == NULL)
			break;

		file_path = g_strdup_printf ("%s/%s", subfolder_directory_path, dirent);

		if (g_stat (file_path, &file_stat) < 0 ||
		    !S_ISDIR (file_stat.st_mode)) {
			g_free (file_path);
			continue;
		}

		new_path = g_strdup_printf ("%s/%s", path, dirent);

		ok = find_folders_recursive (file_path, new_path, callback, data);

		g_free (file_path);
		g_free (new_path);
	}

	g_dir_close (dir);
	g_free (subfolder_directory_path);

	return ok;
}

/**
 * e_path_find_folders:
 * @prefix: directory to start from
 * @callback: Callback to invoke on each folder
 * @data: Data for @callback
 *
 * Walks the folder tree starting at @prefix and calls @callback
 * on each folder.
 *
 * Returns: %TRUE on success, %FALSE if an error occurs at any point
 **/
gboolean
e_path_find_folders (const gchar *prefix,
		     EPathFindFoldersCallback callback,
		     gpointer data)
{
	return find_folders_recursive (prefix, "", callback, data);
}

/**
 * e_path_rmdir:
 * @prefix: a prefix to prepend to the path, or %NULL
 * @path: the virtual path to convert to a filesystem path.
 *
 * This removes the directory pointed to by @prefix and @path
 * and attempts to remove its parent "subfolders" directory too
 * if it's empty.
 *
 * Returns: -1 (with errno set) if it failed to rmdir the
 * specified directory. 0 otherwise, whether or not it removed
 * the parent directory.
 **/
gint
e_path_rmdir (const gchar *prefix, const gchar *vpath)
{
	gchar *physical_path, *p;

	/* Remove the directory itself */
	physical_path = e_path_to_physical (prefix, vpath);
	if (g_rmdir (physical_path) == -1) {
		g_free (physical_path);
		return -1;
	}

	/* Attempt to remove its parent "subfolders" directory,
	 * ignoring errors since it might not be empty.
	 */

	p = strrchr (physical_path, '/');
	if (p[1] == '\0') {
		g_free (physical_path);
		return 0;
	}
	*p = '\0';
	p = strrchr (physical_path, '/');
	if (!p || strcmp (p + 1, SUBFOLDER_DIR_NAME) != 0) {
		g_free (physical_path);
		return 0;
	}

	g_rmdir (physical_path);
	g_free (physical_path);
	return 0;
}

void
do_flags_diff (flags_diff_t *diff, guint32 old, guint32 _new)
{
	diff->changed = old ^ _new;
	diff->bits = _new & diff->changed;
}

gchar *
ews_concat ( const gchar *prefix, const gchar *suffix)
{
	gsize len;

	len = strlen (prefix);
	if (len == 0 || prefix[len - 1] == '/')
		return g_strdup_printf ("%s%s", prefix, suffix);
	else
		return g_strdup_printf ("%s%c%s", prefix, '/', suffix);
}

void
strip_lt_gt (gchar **string, gint s_offset, gint e_offset)
{
	gchar *temp = NULL;
	gint len;

	temp = g_strdup (*string);
	len = strlen (*string);

	*string = (gchar *)g_malloc0 (len-1);
	*string = memcpy(*string, temp+s_offset, len-e_offset);
	g_free (temp);
}

CamelFolderInfo *
camel_ews_utils_build_folder_info (CamelEwsStore *store, const gchar *fname)
{
	CamelEwsStoreSummary *ews_summary = store->summary;
	CamelFolderInfo *fi;
	gchar *url;

	url = camel_url_to_string (CAMEL_SERVICE (store)->url,
			(CAMEL_URL_HIDE_PASSWORD|
			 CAMEL_URL_HIDE_PARAMS|
			 CAMEL_URL_HIDE_AUTH) );

	if ( url[strlen (url) - 1] != '/') {
		gchar *temp_url;

		temp_url = g_strconcat (url, "/", NULL);
		g_free ((gchar *)url);
		url = temp_url;
	}

	fi = camel_folder_info_new ();
	fi->full_name = g_strdup (fname);
	fi->name = g_strdup (camel_ews_store_summary_get_folder_name	(ews_summary,
				fi->full_name,
				NULL));
	fi->uri = g_strconcat (url, fi->full_name, NULL);
	fi->flags = camel_ews_store_summary_get_folder_flags	(ews_summary,
			fi->full_name,
			NULL);
	
	fi->unread = camel_ews_store_summary_get_folder_unread	(ews_summary,
			fi->full_name,
			NULL);
	fi->total = camel_ews_store_summary_get_folder_total	(ews_summary,
			fi->full_name,
			NULL);


	g_free (url);

	return fi;
}

struct remove_esrc_data {
	gchar *fid;
	gchar *account_name;
	EwsFolderType ftype;
};

static gboolean ews_do_remove_esource (gpointer user_data)
{
	struct remove_esrc_data *remove_data = user_data;


	ews_esource_utils_remove_esource (remove_data->fid,
					  remove_data->account_name,
					  remove_data->ftype);
	g_free (remove_data->fid);
	g_free (remove_data->account_name);
	g_free (remove_data);

	return FALSE;
}

static void
sync_deleted_folders (CamelEwsStore *store, GSList *deleted_folders)
{
	CamelEwsStoreSummary *ews_summary = store->summary;
	GSList *l;

	for (l = deleted_folders; l != NULL; l = g_slist_next (l)) {
		const gchar *fid = l->data;
		const gchar *folder_name;
		EwsFolderType ftype;
		CamelFolderInfo *fi;
		GError *error = NULL;

		folder_name = camel_ews_store_summary_get_folder_name_from_id (ews_summary, fid);
		if (!folder_name) {
			g_warning ("Folder unavailable for deletion");
			continue;
		}
		
		ftype = camel_ews_store_summary_get_folder_type (ews_summary, folder_name, NULL);
		if (ftype == EWS_FOLDER_TYPE_MAILBOX) {
			fi = camel_ews_utils_build_folder_info (store, folder_name);

			camel_ews_store_summary_remove_folder (ews_summary, folder_name, &error);
			camel_store_folder_deleted ((CamelStore *) store, fi);
			
			g_clear_error (&error);
		} else {
			struct remove_esrc_data *remove_data = g_new0(struct remove_esrc_data, 1);
			CamelURL *url = CAMEL_SERVICE (store)->url;

			remove_data->fid = g_strdup (fid);
			remove_data->account_name = g_strdup (camel_url_get_param (url, "email"));
			remove_data->ftype = ftype;

			/* This uses GConf so has to be done in the main thread */
			g_idle_add_full (G_PRIORITY_DEFAULT, ews_do_remove_esource, remove_data, NULL);
		}
	}
}

gboolean ews_utils_rename_folder (CamelEwsStore *store, EwsFolderType ftype,
				  const gchar *fid, const gchar *changekey,
				  const gchar *new_fname,
				  const gchar *folder_name, const gchar *display_name,
				  GError **error)
{
	CamelEwsStoreSummary *ews_summary = store->summary;
	guint64 flags;
	CamelFolderInfo *fi;

	camel_ews_store_summary_new_folder (ews_summary, new_fname, fid);
	camel_ews_store_summary_set_change_key (ews_summary, new_fname, changekey);
	camel_ews_store_summary_set_folder_name (ews_summary, new_fname, display_name);

	flags = camel_ews_store_summary_get_folder_flags (ews_summary, folder_name, NULL);
	camel_ews_store_summary_set_folder_flags (ews_summary, new_fname, flags);
		
	if (ftype == EWS_FOLDER_TYPE_MAILBOX) {
		fi = camel_ews_utils_build_folder_info (store, new_fname);
		camel_store_folder_renamed ((CamelStore *) store, folder_name, fi);
	}

	/* TODO set total and unread count. Check if server returns all properties on update */

	/* Discard error if removal fails; it's not the end of the world */
	camel_ews_store_summary_remove_folder (ews_summary, folder_name, NULL);

	return TRUE;
}

static void
sync_updated_folders (CamelEwsStore *store, GSList *updated_folders)
{
	CamelEwsStoreSummary *ews_summary = store->summary;
	GSList *l;
	
	for (l = updated_folders; l != NULL; l = g_slist_next (l)) {
		EEwsFolder *ews_folder = (EEwsFolder *)	l->data;
		EwsFolderType ftype;
		gchar *folder_name;
		const gchar *display_name;
		const EwsFolderId *fid, *pfid;

		ftype = e_ews_folder_get_folder_type (ews_folder);
		if (ftype == EWS_FOLDER_TYPE_CALENDAR ||
		    ftype == EWS_FOLDER_TYPE_TASKS ||
		    ftype == EWS_FOLDER_TYPE_CONTACTS) {
			/* TODO Update esource */
		} else 	if (ftype != EWS_FOLDER_TYPE_MAILBOX)
			continue;

		fid = e_ews_folder_get_id (ews_folder);
		folder_name = g_strdup(camel_ews_store_summary_get_folder_name_from_id (ews_summary, fid->id));

		pfid = e_ews_folder_get_parent_id (ews_folder);
		display_name = e_ews_folder_get_name (ews_folder);

		/* If the folder is moved or renamed (which are separate
		   operations in Exchange, unfortunately, then the name
		   or parent folder will change. Handle both... */
		if (pfid || display_name) {
			GError *error = NULL;
			gchar *new_fname = NULL;

			if (pfid) {
				const gchar *pfname;

				/* If the display name wasn't changed, its basename is still
				   the same as it was before... */
				if (!display_name)
					display_name = camel_ews_store_summary_get_folder_name (ews_summary,
										folder_name, NULL);
				if (!display_name)
					goto done;

				pfname = camel_ews_store_summary_get_folder_name_from_id (ews_summary, pfid->id);

				/* If the lookup failed, it'll be because the new parent folder
				   is the message folder root. */
				if (pfname)
					new_fname = g_strconcat (pfname, "/", display_name, NULL);
				else
					new_fname = g_strdup (display_name);
			} else {
				/* Parent folder not changed; just basename */
				const gchar *last_slash;

				/* Append new display_name to old parent directory name... */
				last_slash = g_strrstr (folder_name, "/");
				if (last_slash)
					new_fname = g_strdup_printf ("%.*s/%s", (int)(last_slash - folder_name),
								     folder_name, display_name);
				else /* ...unless it was a child of the root folder */
					new_fname = g_strdup (display_name);
			}

			ews_utils_rename_folder (store, ftype, fid->id, fid->change_key,
						 new_fname, folder_name, display_name, &error);
			g_free (new_fname);
			g_clear_error (&error);
		}
 done:
		g_free (folder_name);
	}
}
<commit_msg>Don't call ews_utils_rename_folder() if the folder didn't move.<commit_after>/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* camel-ews-utils.c
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.<EMAIL>)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <glib/gstdio.h>

#include "camel-ews-utils.h"
#include "ews-esource-utils.h"

#define SUBFOLDER_DIR_NAME     "subfolders"
#define SUBFOLDER_DIR_NAME_LEN 10

/**
 * e_path_to_physical:
 * @prefix: a prefix to prepend to the path, or %NULL
 * @path: the virtual path to convert to a filesystem path.
 *
 * This converts the "virtual" path @path into an expanded form that
 * allows a given name to refer to both a file and a directory. The
 * expanded path will have a "subfolders" directory inserted between
 * each path component. If the path ends with "/", the returned
 * physical path will end with "/subfolders"
 *
 * If @prefix is non-%NULL, it will be prepended to the returned path.
 *
 * Returns: the expanded path
 **/
gchar *
e_path_to_physical (const gchar *prefix, const gchar *vpath)
{
	const gchar *p, *newp;
	gchar *dp;
	gchar *ppath;
	gint ppath_len;
	gint prefix_len;

	while (*vpath == '/')
		vpath++;
	if (!prefix)
		prefix = "";

	/* Calculate the length of the real path. */
	ppath_len = strlen (vpath);
	ppath_len++;	/* For the ending zero.  */

	prefix_len = strlen (prefix);
	ppath_len += prefix_len;
	ppath_len++;	/* For the separating slash.  */

	/* Take account of the fact that we need to translate every
	 * separator into 'subfolders/'.
	 */
	p = vpath;
	while (1) {
		newp = strchr (p, '/');
		if (newp == NULL)
			break;

		ppath_len += SUBFOLDER_DIR_NAME_LEN;
		ppath_len++; /* For the separating slash.  */

		/* Skip consecutive slashes.  */
		while (*newp == '/')
			newp++;

		p = newp;
	};

	ppath = g_malloc (ppath_len);
	dp = ppath;

	memcpy (dp, prefix, prefix_len);
	dp += prefix_len;
	*(dp++) = '/';

	/* Copy the mangled path.  */
	p = vpath;
	while (1) {
		newp = strchr (p, '/');
		if (newp == NULL) {
			strcpy (dp, p);
			break;
		}

		memcpy (dp, p, newp - p + 1); /* '+ 1' to copy the slash too.  */
		dp += newp - p + 1;

		memcpy (dp, SUBFOLDER_DIR_NAME, SUBFOLDER_DIR_NAME_LEN);
		dp += SUBFOLDER_DIR_NAME_LEN;

		*(dp++) = '/';

		/* Skip consecutive slashes.  */
		while (*newp == '/')
			newp++;

		p = newp;
	}

	return ppath;
}

static gboolean
find_folders_recursive (const gchar *physical_path, const gchar *path,
			EPathFindFoldersCallback callback, gpointer data)
{
	GDir *dir;
	gchar *subfolder_directory_path;
	gboolean ok;

	if (*path) {
		if (!callback (physical_path, path, data))
			return FALSE;

		subfolder_directory_path = g_strdup_printf ("%s/%s", physical_path, SUBFOLDER_DIR_NAME);
	} else {
		/* On the top level, we have no folders and,
		 * consequently, no subfolder directory.
		 */

		subfolder_directory_path = g_strdup (physical_path);
	}

	/* Now scan the subfolders and load them. */
	dir = g_dir_open (subfolder_directory_path, 0, NULL);
	if (dir == NULL) {
		g_free (subfolder_directory_path);
		return TRUE;
	}

	ok = TRUE;
	while (ok) {
		struct stat file_stat;
		const gchar *dirent;
		gchar *file_path;
		gchar *new_path;

		dirent = g_dir_read_name (dir);
		if (dirent == NULL)
			break;

		file_path = g_strdup_printf ("%s/%s", subfolder_directory_path, dirent);

		if (g_stat (file_path, &file_stat) < 0 ||
		    !S_ISDIR (file_stat.st_mode)) {
			g_free (file_path);
			continue;
		}

		new_path = g_strdup_printf ("%s/%s", path, dirent);

		ok = find_folders_recursive (file_path, new_path, callback, data);

		g_free (file_path);
		g_free (new_path);
	}

	g_dir_close (dir);
	g_free (subfolder_directory_path);

	return ok;
}

/**
 * e_path_find_folders:
 * @prefix: directory to start from
 * @callback: Callback to invoke on each folder
 * @data: Data for @callback
 *
 * Walks the folder tree starting at @prefix and calls @callback
 * on each folder.
 *
 * Returns: %TRUE on success, %FALSE if an error occurs at any point
 **/
gboolean
e_path_find_folders (const gchar *prefix,
		     EPathFindFoldersCallback callback,
		     gpointer data)
{
	return find_folders_recursive (prefix, "", callback, data);
}

/**
 * e_path_rmdir:
 * @prefix: a prefix to prepend to the path, or %NULL
 * @path: the virtual path to convert to a filesystem path.
 *
 * This removes the directory pointed to by @prefix and @path
 * and attempts to remove its parent "subfolders" directory too
 * if it's empty.
 *
 * Returns: -1 (with errno set) if it failed to rmdir the
 * specified directory. 0 otherwise, whether or not it removed
 * the parent directory.
 **/
gint
e_path_rmdir (const gchar *prefix, const gchar *vpath)
{
	gchar *physical_path, *p;

	/* Remove the directory itself */
	physical_path = e_path_to_physical (prefix, vpath);
	if (g_rmdir (physical_path) == -1) {
		g_free (physical_path);
		return -1;
	}

	/* Attempt to remove its parent "subfolders" directory,
	 * ignoring errors since it might not be empty.
	 */

	p = strrchr (physical_path, '/');
	if (p[1] == '\0') {
		g_free (physical_path);
		return 0;
	}
	*p = '\0';
	p = strrchr (physical_path, '/');
	if (!p || strcmp (p + 1, SUBFOLDER_DIR_NAME) != 0) {
		g_free (physical_path);
		return 0;
	}

	g_rmdir (physical_path);
	g_free (physical_path);
	return 0;
}

void
do_flags_diff (flags_diff_t *diff, guint32 old, guint32 _new)
{
	diff->changed = old ^ _new;
	diff->bits = _new & diff->changed;
}

gchar *
ews_concat ( const gchar *prefix, const gchar *suffix)
{
	gsize len;

	len = strlen (prefix);
	if (len == 0 || prefix[len - 1] == '/')
		return g_strdup_printf ("%s%s", prefix, suffix);
	else
		return g_strdup_printf ("%s%c%s", prefix, '/', suffix);
}

void
strip_lt_gt (gchar **string, gint s_offset, gint e_offset)
{
	gchar *temp = NULL;
	gint len;

	temp = g_strdup (*string);
	len = strlen (*string);

	*string = (gchar *)g_malloc0 (len-1);
	*string = memcpy(*string, temp+s_offset, len-e_offset);
	g_free (temp);
}

CamelFolderInfo *
camel_ews_utils_build_folder_info (CamelEwsStore *store, const gchar *fname)
{
	CamelEwsStoreSummary *ews_summary = store->summary;
	CamelFolderInfo *fi;
	gchar *url;

	url = camel_url_to_string (CAMEL_SERVICE (store)->url,
			(CAMEL_URL_HIDE_PASSWORD|
			 CAMEL_URL_HIDE_PARAMS|
			 CAMEL_URL_HIDE_AUTH) );

	if ( url[strlen (url) - 1] != '/') {
		gchar *temp_url;

		temp_url = g_strconcat (url, "/", NULL);
		g_free ((gchar *)url);
		url = temp_url;
	}

	fi = camel_folder_info_new ();
	fi->full_name = g_strdup (fname);
	fi->name = g_strdup (camel_ews_store_summary_get_folder_name	(ews_summary,
				fi->full_name,
				NULL));
	fi->uri = g_strconcat (url, fi->full_name, NULL);
	fi->flags = camel_ews_store_summary_get_folder_flags	(ews_summary,
			fi->full_name,
			NULL);
	
	fi->unread = camel_ews_store_summary_get_folder_unread	(ews_summary,
			fi->full_name,
			NULL);
	fi->total = camel_ews_store_summary_get_folder_total	(ews_summary,
			fi->full_name,
			NULL);


	g_free (url);

	return fi;
}

struct remove_esrc_data {
	gchar *fid;
	gchar *account_name;
	EwsFolderType ftype;
};

static gboolean ews_do_remove_esource (gpointer user_data)
{
	struct remove_esrc_data *remove_data = user_data;


	ews_esource_utils_remove_esource (remove_data->fid,
					  remove_data->account_name,
					  remove_data->ftype);
	g_free (remove_data->fid);
	g_free (remove_data->account_name);
	g_free (remove_data);

	return FALSE;
}

static void
sync_deleted_folders (CamelEwsStore *store, GSList *deleted_folders)
{
	CamelEwsStoreSummary *ews_summary = store->summary;
	GSList *l;

	for (l = deleted_folders; l != NULL; l = g_slist_next (l)) {
		const gchar *fid = l->data;
		const gchar *folder_name;
		EwsFolderType ftype;
		CamelFolderInfo *fi;
		GError *error = NULL;

		folder_name = camel_ews_store_summary_get_folder_name_from_id (ews_summary, fid);
		if (!folder_name) {
			g_warning ("Folder unavailable for deletion");
			continue;
		}
		
		ftype = camel_ews_store_summary_get_folder_type (ews_summary, folder_name, NULL);
		if (ftype == EWS_FOLDER_TYPE_MAILBOX) {
			fi = camel_ews_utils_build_folder_info (store, folder_name);

			camel_ews_store_summary_remove_folder (ews_summary, folder_name, &error);
			camel_store_folder_deleted ((CamelStore *) store, fi);
			
			g_clear_error (&error);
		} else {
			struct remove_esrc_data *remove_data = g_new0(struct remove_esrc_data, 1);
			CamelURL *url = CAMEL_SERVICE (store)->url;

			remove_data->fid = g_strdup (fid);
			remove_data->account_name = g_strdup (camel_url_get_param (url, "email"));
			remove_data->ftype = ftype;

			/* This uses GConf so has to be done in the main thread */
			g_idle_add_full (G_PRIORITY_DEFAULT, ews_do_remove_esource, remove_data, NULL);
		}
	}
}

gboolean ews_utils_rename_folder (CamelEwsStore *store, EwsFolderType ftype,
				  const gchar *fid, const gchar *changekey,
				  const gchar *new_fname,
				  const gchar *folder_name, const gchar *display_name,
				  GError **error)
{
	CamelEwsStoreSummary *ews_summary = store->summary;
	guint64 flags;
	CamelFolderInfo *fi;

	camel_ews_store_summary_new_folder (ews_summary, new_fname, fid);
	camel_ews_store_summary_set_change_key (ews_summary, new_fname, changekey);
	camel_ews_store_summary_set_folder_name (ews_summary, new_fname, display_name);

	flags = camel_ews_store_summary_get_folder_flags (ews_summary, folder_name, NULL);
	camel_ews_store_summary_set_folder_flags (ews_summary, new_fname, flags);
		
	if (ftype == EWS_FOLDER_TYPE_MAILBOX) {
		fi = camel_ews_utils_build_folder_info (store, new_fname);
		camel_store_folder_renamed ((CamelStore *) store, folder_name, fi);
	}

	/* TODO set total and unread count. Check if server returns all properties on update */

	/* Discard error if removal fails; it's not the end of the world */
	camel_ews_store_summary_remove_folder (ews_summary, folder_name, NULL);

	return TRUE;
}

static void
sync_updated_folders (CamelEwsStore *store, GSList *updated_folders)
{
	CamelEwsStoreSummary *ews_summary = store->summary;
	GSList *l;
	
	for (l = updated_folders; l != NULL; l = g_slist_next (l)) {
		EEwsFolder *ews_folder = (EEwsFolder *)	l->data;
		EwsFolderType ftype;
		gchar *folder_name;
		const gchar *display_name;
		const EwsFolderId *fid, *pfid;

		ftype = e_ews_folder_get_folder_type (ews_folder);
		if (ftype == EWS_FOLDER_TYPE_CALENDAR ||
		    ftype == EWS_FOLDER_TYPE_TASKS ||
		    ftype == EWS_FOLDER_TYPE_CONTACTS) {
			/* TODO Update esource */
		} else 	if (ftype != EWS_FOLDER_TYPE_MAILBOX)
			continue;

		fid = e_ews_folder_get_id (ews_folder);
		folder_name = g_strdup(camel_ews_store_summary_get_folder_name_from_id (ews_summary, fid->id));

		pfid = e_ews_folder_get_parent_id (ews_folder);
		display_name = e_ews_folder_get_name (ews_folder);

		/* If the folder is moved or renamed (which are separate
		   operations in Exchange, unfortunately, then the name
		   or parent folder will change. Handle both... */
		if (pfid || display_name) {
			GError *error = NULL;
			gchar *new_fname = NULL;

			if (pfid) {
				const gchar *pfname;

				/* If the display name wasn't changed, its basename is still
				   the same as it was before... */
				if (!display_name)
					display_name = camel_ews_store_summary_get_folder_name (ews_summary,
										folder_name, NULL);
				if (!display_name)
					goto done;

				pfname = camel_ews_store_summary_get_folder_name_from_id (ews_summary, pfid->id);

				/* If the lookup failed, it'll be because the new parent folder
				   is the message folder root. */
				if (pfname)
					new_fname = g_strconcat (pfname, "/", display_name, NULL);
				else
					new_fname = g_strdup (display_name);
			} else {
				/* Parent folder not changed; just basename */
				const gchar *last_slash;

				/* Append new display_name to old parent directory name... */
				last_slash = g_strrstr (folder_name, "/");
				if (last_slash)
					new_fname = g_strdup_printf ("%.*s/%s", (int)(last_slash - folder_name),
								     folder_name, display_name);
				else /* ...unless it was a child of the root folder */
					new_fname = g_strdup (display_name);
			}

			if (strcmp(new_fname, folder_name))
				ews_utils_rename_folder (store, ftype, fid->id, fid->change_key,
							 new_fname, folder_name, display_name, &error);
			g_free (new_fname);
			g_clear_error (&error);
		}
 done:
		g_free (folder_name);
	}
}

{'fraction_non_alphanumeric': 0.11564028117359414, 'fraction_numerical': 0.004278728606356968, 'mean_word_length': 3.9427870090634443, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2984025', 'n_tokens_mistral': 10531, 'n_tokens_neox': 9562, 'n_words': 2616}