// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; namespace Spludlow { /// /// EWS Folder ID lookup with caching. Used by Spludlow.Exchange /// public class ExchangeFolders { private DataSet DataSet = null; private Spludlow.Exchange ExchangeService; private string CacheDirectory; public ExchangeFolders(Spludlow.Exchange exchangeService, string cacheDirectory) { this.ExchangeService = exchangeService; this.CacheDirectory = cacheDirectory; this.DataSet = Spludlow.Data.TextTable.ReadDirectory(this.CacheDirectory); foreach (DataTable table in this.DataSet.Tables) table.CaseSensitive = true; } private void SaveChanges(string userEmailAddress) { DataTable table = this.DataSet.Tables[userEmailAddress]; Spludlow.Data.TextTable.Write(this.CacheDirectory + @"\" + table.TableName + ".txt", table); } public string FolderId(string userEmailAddress, string path) { DataTable table; if (this.DataSet.Tables.Contains(userEmailAddress) == false) { string rootFolderId = this.ExchangeService.MailBoxRootUniqueFolderId(userEmailAddress); table = this.ExchangeService.QueryFolders(rootFolderId).Tables[0]; table.TableName = userEmailAddress; table.DataSet.Tables.Remove(table); table.CaseSensitive = true; this.DataSet.Tables.Add(table); this.SaveChanges(userEmailAddress); } table = this.DataSet.Tables[userEmailAddress]; DataRow[] rows = table.Select("Path = '" + path.Replace("'", "''") + "'"); if (rows.Length == 0) return null; if (rows.Length > 1) throw new ApplicationException("ExchangeFolders; Looking up FolderId, more that one folder with same path: " + userEmailAddress + ", " + path); return (string)rows[0]["UniqueId"]; } public bool FolderExists(string userEmailAddress, string path) { return (this.FolderId(userEmailAddress, path) != null); } // UniqueId DisplayName Depth Path FolderType ChildFolderCount TotalCount public string CreateFolder(string userEmailAddress, string path) { string folderId = this.FolderId(userEmailAddress, path); if (folderId != null) return folderId; string parentPath = Path.GetDirectoryName(path).Replace("\\", "/"); string name = Path.GetFileName(path); //throw new ApplicationException(">" + parentPath + "<"); string parentFolderId = this.FolderId(userEmailAddress, parentPath); if (parentFolderId == null) throw new ApplicationException("ExchangeFolders; Create Folder, Can't find parent folder: " + userEmailAddress + ", " + path); folderId = this.ExchangeService.CreateFolder(parentFolderId, name); int depth = 0; foreach (char ch in path) if (ch == '/') ++depth; DataTable table = this.DataSet.Tables[userEmailAddress]; table.Rows.Add(folderId, name, depth, path, "Folder", 0, 0); // Add to parent's ChildFolderCount DataRow[] rows = table.Select("UniqueId = '" + parentFolderId + "'"); if (rows.Length != 1) throw new ApplicationException("ExchangeFolders; Create Folder, Did not find one parent row: " + userEmailAddress + ", " + path + ", " + rows.Length); DataRow parentRow = rows[0]; parentRow["ChildFolderCount"] = (int)parentRow["ChildFolderCount"] + 1; this.SaveChanges(userEmailAddress); return folderId; } public static void DeleteFolder(string userEmailAddress, string path) { throw new NotImplementedException("Delete not implimentd yet!"); } } }