using System;
using System.Collections.Generic;
namespace Microsoft.Win32.TaskScheduler
{
///
/// Provides information and control for a collection of folders that contain tasks.
///
public sealed class TaskFolderCollection : ICollection
{
private TaskFolder parent;
private TaskFolder[] v1FolderList = null;
private TaskScheduler.V2Interop.ITaskFolderCollection v2FolderList = null;
internal TaskFolderCollection()
{
v1FolderList = new TaskFolder[0];
}
internal TaskFolderCollection(TaskFolder v1Folder)
{
parent = v1Folder;
v1FolderList = new TaskFolder[] { v1Folder };
}
internal TaskFolderCollection(TaskFolder folder, TaskScheduler.V2Interop.ITaskFolderCollection iCollection)
{
parent = folder;
v2FolderList = iCollection;
}
///
/// Gets the number of items in the collection.
///
public int Count
{
get { return (v2FolderList != null) ? v2FolderList.Count : v1FolderList.Length; }
}
///
/// Gets a value indicating whether the is read-only.
///
bool ICollection.IsReadOnly
{
get { return false; }
}
///
/// Gets the specified folder from the collection.
///
/// The index of the folder to be retrieved.
/// A TaskFolder instance that represents the requested folder.
public TaskFolder this[int index]
{
get
{
if (v2FolderList != null)
return new TaskFolder(parent.TaskService, v2FolderList[++index]);
return v1FolderList[index];
}
}
///
/// Gets the specified folder from the collection.
///
/// The path of the folder to be retrieved.
/// A TaskFolder instance that represents the requested folder.
public TaskFolder this[string path]
{
get
{
if (v2FolderList != null)
return new TaskFolder(parent.TaskService, v2FolderList[path]);
if (v1FolderList != null && v1FolderList.Length > 0 && (path == string.Empty || path == "\\"))
return v1FolderList[0];
throw new ArgumentException("Path not found");
}
}
///
/// Adds an item to the .
///
/// The object to add to the .
/// This action is technically unfeasable due to limitations of the underlying library. Use the instead.
public void Add(TaskFolder item)
{
throw new NotImplementedException();
}
///
/// Removes all items from the .
///
public void Clear()
{
if (v2FolderList != null)
{
for (int i = v2FolderList.Count; i > 0; i--)
parent.DeleteFolder(v2FolderList[i].Name, false);
}
}
///
/// Determines whether the contains a specific value.
///
/// The object to locate in the .
///
/// true if is found in the ; otherwise, false.
///
public bool Contains(TaskFolder item)
{
if (v2FolderList != null)
{
for (int i = v2FolderList.Count; i > 0; i--)
if (string.Equals(item.Path, v2FolderList[i].Path, StringComparison.CurrentCultureIgnoreCase))
return true;
}
else
return item.Path == "\\";
return false;
}
///
/// Copies the elements of the ICollection to an Array, starting at a particular Array index.
///
/// The one-dimensional Array that is the destination of the elements copied from . The Array must have zero-based indexing.
/// The zero-based index in array at which copying begins.
public void CopyTo(TaskFolder[] array, int arrayIndex)
{
if (arrayIndex < 0) throw new ArgumentOutOfRangeException();
if (array == null) throw new ArgumentNullException();
if (v2FolderList != null)
{
if (arrayIndex + this.Count > array.Length)
throw new ArgumentException();
foreach (TaskScheduler.V2Interop.ITaskFolder f in v2FolderList)
array[arrayIndex++] = new TaskFolder(parent.TaskService, f);
}
else
{
if (arrayIndex + v1FolderList.Length > array.Length)
throw new ArgumentException();
v1FolderList.CopyTo(array, arrayIndex);
}
}
///
/// Releases all resources used by this class.
///
public void Dispose()
{
if (v1FolderList != null && v1FolderList.Length > 0)
{
v1FolderList[0].Dispose();
v1FolderList[0] = null;
}
if (v2FolderList != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(v2FolderList);
}
///
/// Determines whether the specified folder exists.
///
/// The path of the folder.
/// true if folder exists; otherwise, false.
public bool Exists(string path)
{
try
{
if (parent.GetFolder(path) != null)
return true;
}
catch { }
return false;
}
///
/// Gets a list of items in a collection.
///
/// Enumerated list of items in the collection.
public IEnumerator GetEnumerator()
{
TaskFolder[] eArray = new TaskFolder[this.Count];
this.CopyTo(eArray, 0);
return new TaskFolderEnumerator(eArray);
}
/*
///
/// Returns the index of the TaskFolder within the collection.
///
/// TaskFolder to find.
/// Index of the TaskFolder; -1 if not found.
public int IndexOf(TaskFolder item)
{
return IndexOf(item.Path);
}
///
/// Returns the index of the TaskFolder within the collection.
///
/// Path to find.
/// Index of the TaskFolder; -1 if not found.
public int IndexOf(string path)
{
if (v2FolderList != null)
{
for (int i = 0; i < v2FolderList.Count; i++)
{
if (v2FolderList[new System.Runtime.InteropServices.VariantWrapper(i)].Path == path)
return i;
}
return -1;
}
else
return (v1FolderList.Length > 0 && (path == string.Empty || path == "\\")) ? 0 : -1;
}
*/
///
/// Removes the first occurrence of a specific object from the .
///
/// The object to remove from the .
///
/// true if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original .
///
public bool Remove(TaskFolder item)
{
if (v2FolderList != null)
{
for (int i = v2FolderList.Count; i > 0; i--)
{
if (string.Equals(item.Path, v2FolderList[i].Path, StringComparison.CurrentCultureIgnoreCase))
{
try
{
parent.DeleteFolder(v2FolderList[i].Name, true);
}
catch
{
return false;
}
return true;
}
}
}
return false;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
private class TaskFolderEnumerator : IEnumerator
{
private TaskFolder[] folders = null;
private System.Collections.IEnumerator iEnum = null;
internal TaskFolderEnumerator(TaskFolder[] f)
{
folders = f;
iEnum = f.GetEnumerator();
}
public TaskFolder Current
{
get { return iEnum.Current as TaskFolder; }
}
object System.Collections.IEnumerator.Current
{
get { return this.Current; }
}
///
/// Releases all resources used by this class.
///
public void Dispose()
{
}
public bool MoveNext()
{
return iEnum.MoveNext();
}
public void Reset()
{
iEnum.Reset();
}
}
}
}