// // FolderNamespaceCollection.cs // // Author: Jeffrey Stedfast // // Copyright (c) 2013-2020 .NET Foundation and Contributors // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // using System; using System.Text; using System.Collections; using System.Collections.Generic; using MimeKit.Utils; namespace MailKit { /// /// A read-only collection of folder namespaces. /// /// /// A read-only collection of folder namespaces. /// public class FolderNamespaceCollection : IEnumerable { readonly List namespaces; /// /// Initializes a new instance of the class. /// /// /// Creates a new . /// public FolderNamespaceCollection () { namespaces = new List (); } #region ICollection implementation /// /// Gets the number of folder namespaces contained in the collection. /// /// /// Gets the number of folder namespaces contained in the collection. /// /// The count. public int Count { get { return namespaces.Count; } } /// /// Adds the specified namespace. /// /// /// Adds the specified namespace. /// /// The namespace to add. /// /// is null. /// public void Add (FolderNamespace @namespace) { if (@namespace == null) throw new ArgumentNullException (nameof (@namespace)); namespaces.Add (@namespace); } /// /// Removes all namespaces from the collection. /// /// /// Removes all namespaces from the collection. /// public void Clear () { namespaces.Clear (); } /// /// Checks if the collection contains the specified namespace. /// /// /// Checks if the collection contains the specified namespace. /// /// true if the specified namespace exists; /// otherwise false. /// The namespace. /// /// is null. /// public bool Contains (FolderNamespace @namespace) { if (@namespace == null) throw new ArgumentNullException (nameof (@namespace)); return namespaces.Contains (@namespace); } /// /// Removes the first occurance of the specified namespace. /// /// /// Removes the first occurance of the specified namespace. /// /// true if the frst occurance of the specified /// namespace was removed; otherwise false. /// The namespace. /// /// is null. /// public bool Remove (FolderNamespace @namespace) { if (@namespace == null) throw new ArgumentNullException (nameof (@namespace)); return namespaces.Remove (@namespace); } /// /// Gets the at the specified index. /// /// /// Gets the at the specified index. /// /// The folder namespace at the specified index. /// The index. /// /// is null. /// /// /// is out of range. /// public FolderNamespace this [int index] { get { if (index < 0 || index >= namespaces.Count) throw new ArgumentOutOfRangeException (nameof (index)); return namespaces[index]; } set { if (index < 0 || index >= namespaces.Count) throw new ArgumentOutOfRangeException (nameof (index)); if (value == null) throw new ArgumentNullException (nameof (value)); namespaces[index] = value; } } #endregion #region IEnumerable implementation /// /// Gets the enumerator. /// /// /// Gets the enumerator. /// /// The enumerator. public IEnumerator GetEnumerator () { return namespaces.GetEnumerator (); } #endregion #region IEnumerable implementation /// /// Gets the enumerator. /// /// /// Gets the enumerator. /// /// The enumerator. IEnumerator IEnumerable.GetEnumerator () { return namespaces.GetEnumerator (); } #endregion static bool Escape (char directorySeparator) { return directorySeparator == '\\' || directorySeparator == '"'; } /// /// Returns a that represents the current . /// /// /// Returns a that represents the current . /// /// A that represents the current . public override string ToString () { var builder = new StringBuilder (); builder.Append ('('); for (int i = 0; i < namespaces.Count; i++) { builder.Append ("(\""); if (Escape (namespaces[i].DirectorySeparator)) builder.Append ('\\'); builder.Append (namespaces[i].DirectorySeparator); builder.Append ("\" "); builder.Append (MimeUtils.Quote (namespaces[i].Path)); builder.Append (")"); } builder.Append (')'); return builder.ToString (); } } }