Пример #1
0
        /// <summary>
        /// Deletes a sub-folder under this folder. All messages contained in this folder will be deleted.
        /// </summary>
        /// <param name="name">Name of folder to delete</param>
        public void DeleteFolder(string name)
        {
            if (_client.OfflineMode)
            {
                _client.Log(IMAPBase.LogTypeEnum.WARN, "Cannot delete folders in offline mode.");
                return;
            }

            string cmd = "DELETE \"{0}\"\r\n";

            IMAPFolder folderToDelete = null;

            foreach (IMAPFolder f in _subFolders)
            {
                if (f.FolderName.Equals(name))
                {
                    folderToDelete = f;
                    break;
                }
            }

            if (folderToDelete != null)
            {
                ArrayList result = new ArrayList();
                _client._imap.SendAndReceive(String.Format(cmd, folderToDelete.FolderPath), ref result);
                if (result[0].ToString().Contains("OK"))
                {
                    // TODO: check if this folder is selected. if so, select its parent instead

                    _subFolders.Remove(folderToDelete);
                    _client.UpdateCache(true);
                }
            }
        }
Пример #2
0
        /// <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");
        }