/// <summary> /// Creates a new sub-folder under this folder /// </summary> /// <param name="name">Name of folder to create</param> /// <param name="autoSelect">Automatically select this folder upon successful creation</param> public void CreateFolder(string name, bool autoSelect) { if (_client.OfflineMode) { _client.Log(IMAPBase.LogTypeEnum.WARN, "Cannot create folders in offline mode."); return; } string cmd = "CREATE \"{0}\"\r\n"; ArrayList result = new ArrayList(); string newFolder = String.Format("{0}/{1}", this.FolderPath, name); _client._imap.SendAndReceive(String.Format(cmd, newFolder), ref result); if (result[0].ToString().Contains("OK")) { IMAPFolder oNewFolder = new IMAPFolder(); oNewFolder.FolderName = name; oNewFolder.FolderPath = newFolder; oNewFolder.ParentFolder = this; oNewFolder.ParentFolderName = this.FolderName; _subFolders.Add(oNewFolder); if (autoSelect) { oNewFolder.Select(); } _client.UpdateCache(true); } else { _client.Log(IMAPBase.LogTypeEnum.ERROR, "Folder could not be created." + result[0].ToString()); } }
/// <summary> /// Helper method for recursively adding all folders from the source folder structure into the flat list /// </summary> /// <param name="currentFolder"></param> /// <param name="master"></param> private void FolderRecurser(IMAPFolder currentFolder, ref IMAPFolderCollection master) { if (currentFolder.SubFolders.Count > 0) { foreach (IMAPFolder f in currentFolder.SubFolders) { master.Add(f); FolderRecurser(f, ref master); } } }
/// <summary> /// Takes the multi-level folder structure and flattens it into a single list of all folders /// </summary> /// <param name="list"></param> /// <returns></returns> private IMAPFolderCollection FlattenFolderList(IMAPFolderCollection list) { IMAPFolderCollection newList = new IMAPFolderCollection(); foreach (IMAPFolder f in list) { FolderRecurser(f, ref newList); newList.Add(f); } return(newList); }
/// <summary> /// Synchronizes the local cache with the server /// </summary> public void SyncCache() { if (this.OfflineMode) { return; } if (!this.UsingCache) { return; } // to synchronize the cache without having to download everything all over again, // we first get the folder list from the server. We then look at each folder in the server list // and see if it already exists in the client list. if it does not, we add it and pull // the message UIDs for it. // then we check the client list and see if all of those folders are still on the server // if not, the folder on the client side is removed, all with all of its messages // keep track of newly added folders so their contents can be downloaded. // next we iterate through all the existing folders and check for any new messages. // this is accomplished by simply calling the GetMessageIDs method on the folder. this will // update the folder with any UIDs that dont already exist. // the messages content will be loaded automatically when it is serialized. Log(IMAPBase.LogTypeEnum.INFO, "Synching Cache..."); IMAPFolderCollection serverFolderList = _imap.RawFolderList; IMAPFolderCollection clientFolderList = FlattenFolderList(_folders); IMAPFolderCollection newFolderList = new IMAPFolderCollection(); IMAPFolderCollection oldFolderList = new IMAPFolderCollection(); foreach (IMAPFolder f in serverFolderList) { bool found = false; foreach (IMAPFolder cf in clientFolderList) { if (cf.FolderPath.Equals(f.FolderPath)) { found = true; } } if (!found) { newFolderList.Add(f); } } foreach (IMAPFolder f in clientFolderList) { bool found = false; foreach (IMAPFolder sf in serverFolderList) { if (sf.FolderPath.Equals(f.FolderPath)) { found = true; } } if (!found) { oldFolderList.Add(f); } } if (oldFolderList.Count > 0) { Log(IMAPBase.LogTypeEnum.INFO, String.Format("{0} old folders found", newFolderList.Count)); foreach (IMAPFolder f in oldFolderList) { IMAPFolder temp = null; FindFolder(f.FolderPath, ref _folders, ref temp); if (temp != null) { if (temp.ParentFolder == null) { _folders.Remove(temp); } else { temp.ParentFolder.SubFolders.Remove(temp); } } } } if (newFolderList.Count > 0) { Log(IMAPBase.LogTypeEnum.INFO, String.Format("{0} new folders found", newFolderList.Count)); // now we need to put these new folders into the proper locations in the tree. foreach (IMAPFolder f in newFolderList) { f.GetMessageIDs(false); foreach (IMAPFolder sf in serverFolderList) { if (sf.FolderName.Equals(f.ParentFolderName)) { f.ParentFolder = sf; break; } } // if the new folder has no parent assigned to it then we just add it to the root folders if (f.ParentFolderName == String.Empty) { _folders.Add(f); } else { // otherwise we just loop through the flat list we created // and find the folder that is the parent of the current new folder // we then add the new folder to the sub folders of its parent foreach (IMAPFolder cf in clientFolderList) { if (cf.FolderPath.Equals(f.ParentFolder.FolderPath)) { cf.SubFolders.Add(f); f.ParentFolder = cf; break; } } } } } foreach (IMAPFolder f in clientFolderList) { // this will get the UIDs of any new messages that have been added to the folder on the server f.GetMessageIDs(false); } UpdateCache(false); Log(IMAPBase.LogTypeEnum.INFO, "Cache Synchronization Complete"); }