diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-05-09 18:07:56 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-05-09 18:07:56 +0300 |
| commit | e1236499b485a69648bea4d7871aa185e5dae745 (patch) | |
| tree | 8cd4ccd7d272c8c4e61d4bd6bcba434eb9522b85 /Software/Visual_Studio/Tango.Core/IO | |
| parent | 2a459c79e90a2ade3f1dac1ae4541c0f4d74965c (diff) | |
| download | Tango-e1236499b485a69648bea4d7871aa185e5dae745.tar.gz Tango-e1236499b485a69648bea4d7871aa185e5dae745.zip | |
New TemporaryManager !
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/IO')
6 files changed, 525 insertions, 48 deletions
diff --git a/Software/Visual_Studio/Tango.Core/IO/ITemporaryItem.cs b/Software/Visual_Studio/Tango.Core/IO/ITemporaryItem.cs index ebfc70e60..cf1045a60 100644 --- a/Software/Visual_Studio/Tango.Core/IO/ITemporaryItem.cs +++ b/Software/Visual_Studio/Tango.Core/IO/ITemporaryItem.cs @@ -6,13 +6,58 @@ using System.Threading.Tasks; namespace Tango.Core.IO { + /// <summary> + /// Represents a <see cref="TemporaryManager"/> item. + /// </summary> public interface ITemporaryItem { + /// <summary> + /// Occurs when this item has been deleted. + /// </summary> + event EventHandler Deleted; + /// <summary> + /// Gets the creation date of the temporary item. + /// </summary> DateTime Date { get; } + /// <summary> + /// Gets the path to the temporary item. + /// </summary> String Path { get; } + /// <summary> + /// Gets the optional item tag. + /// </summary> + Object Tag { get; } + /// <summary> + /// Gets or sets a value indicating whether this <see cref="ITemporaryItem"/> should not be deleted by a parent item. + /// </summary> + bool Persist { get; set; } + /// <summary> + /// Gets the collection of child items. + /// </summary> IReadOnlyCollection<ITemporaryItem> Items { get; } - void AddItem(ITemporaryItem item); + /// <summary> + /// Adds a child item. + /// </summary> + /// <param name="item">The item.</param> + ITemporaryItem AddItem(ITemporaryItem item); + /// <summary> + /// Removes the specified child item. + /// </summary> + /// <param name="item">The item.</param> void RemoveItem(ITemporaryItem item); - void Delete(); + /// <summary> + /// Deletes the temporary item. + /// </summary> + /// <returns>True is deletion was successful.</returns> + bool Delete(); + /// <summary> + /// Deletes the temporary item asynchronously. + /// </summary> + /// <returns>True is deletion was successful.</returns> + Task<bool> DeleteAsync(); + /// <summary> + /// Makes the temporary item visible to the user somehow. + /// </summary> + void Display(); } } diff --git a/Software/Visual_Studio/Tango.Core/IO/TemporaryFile.cs b/Software/Visual_Studio/Tango.Core/IO/TemporaryFile.cs index 4e119875e..3e5bd579e 100644 --- a/Software/Visual_Studio/Tango.Core/IO/TemporaryFile.cs +++ b/Software/Visual_Studio/Tango.Core/IO/TemporaryFile.cs @@ -1,13 +1,104 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tango.Core.IO { - public class TemporaryFile + /// <summary> + /// Represents a temporary file. + /// </summary> + /// <seealso cref="Tango.Core.IO.TemporaryItem" /> + public class TemporaryFile : TemporaryItem { + /// <summary> + /// Initializes a new instance of the <see cref="TemporaryFile"/> class. + /// </summary> + /// <param name="path">The temporary item path.</param> + public TemporaryFile(string path) : base(path) + { + Create(); + } + /// <summary> + /// Initializes a new instance of the <see cref="TemporaryFile"/> class. + /// </summary> + /// <param name="path">The temporary item path.</param> + /// <param name="tag">The item tag.</param> + public TemporaryFile(string path, object tag) : base(path, tag) + { + Create(); + } + + /// <summary> + /// Deletes the temporary item. + /// </summary> + /// <returns> + /// True is deletion was successful. + /// </returns> + public override bool Delete() + { + base.Delete(); + + try + { + if (File.Exists(Path)) + { + File.Delete(Path); + } + + return true; + } + catch + { + return false; + } + } + + /// <summary> + /// Creates/Overwrites a new file in the specified path. + /// </summary> + public virtual void Create() + { + File.Create(Path).Dispose(); + } + + /// <summary> + /// Creates a stream to the file path. + /// </summary> + /// <returns></returns> + public virtual FileStream CreateStream() + { + return new FileStream(Path, FileMode.OpenOrCreate); + } + + /// <summary> + /// Writes the specified text to the file. + /// </summary> + /// <param name="text">The text.</param> + public virtual void WriteAllText(String text) + { + File.WriteAllText(Path, text); + } + + /// <summary> + /// Writes the specified bytes to the file. + /// </summary> + /// <param name="data">The data.</param> + public virtual void WriteAllBytes(byte[] data) + { + File.WriteAllBytes(Path, data); + } + + /// <summary> + /// Makes the temporary item visible to the user somehow. + /// </summary> + public override void Display() + { + Process.Start("explorer.exe", string.Format("/select,\"{0}\"", Path)); + } } } diff --git a/Software/Visual_Studio/Tango.Core/IO/TemporaryFileManager.cs b/Software/Visual_Studio/Tango.Core/IO/TemporaryFileManager.cs deleted file mode 100644 index a4ff8fa27..000000000 --- a/Software/Visual_Studio/Tango.Core/IO/TemporaryFileManager.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Tango.Core.IO -{ - /// <summary> - /// Represents the Tango system temporary file manager. - /// </summary> - public class TemporaryFileManager - { - #region Singleton - - private static TemporaryFileManager _default; - public static TemporaryFileManager Default - { - get - { - if (_default == null) - { - _default = new TemporaryFileManager(); - } - - return _default; - } - } - - private TemporaryFileManager() - { - - } - - #endregion - - - } -} diff --git a/Software/Visual_Studio/Tango.Core/IO/TemporaryFolder.cs b/Software/Visual_Studio/Tango.Core/IO/TemporaryFolder.cs new file mode 100644 index 000000000..f9cc4a088 --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/IO/TemporaryFolder.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Core.IO +{ + /// <summary> + /// Represents a temporary folder. + /// </summary> + /// <seealso cref="Tango.Core.IO.TemporaryItem" /> + public class TemporaryFolder : TemporaryItem + { + /// <summary> + /// Initializes a new instance of the <see cref="TemporaryFolder"/> class. + /// </summary> + /// <param name="path">The temporary item path.</param> + public TemporaryFolder(string path) : base(path) + { + Directory.CreateDirectory(path); + } + + /// <summary> + /// Initializes a new instance of the <see cref="TemporaryFolder"/> class. + /// </summary> + /// <param name="path">The temporary item path.</param> + /// <param name="tag">The item tag.</param> + public TemporaryFolder(string path, object tag) : base(path, tag) + { + Directory.CreateDirectory(path); + } + + /// <summary> + /// Deletes the temporary item. + /// </summary> + /// <returns> + /// True is deletion was successful. + /// </returns> + public override bool Delete() + { + base.Delete(); + + try + { + if (Directory.Exists(Path)) + { + Directory.Delete(Path); + } + + return true; + } + catch + { + return false; + } + } + + /// <summary> + /// Creates a new temporary file inside the temp folder and returns the file item. + /// </summary> + /// <returns></returns> + public virtual TemporaryFile CreateFile(String extension = null) + { + return AddItem(new TemporaryFile(System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName() + extension))) as TemporaryFile; + } + + /// <summary> + /// Creates a new temporary folder inside the temp folder and returns the folder item. + /// </summary> + /// <returns></returns> + public virtual TemporaryFolder CreateFolder() + { + return AddItem(new TemporaryFolder(System.IO.Path.Combine(Path, System.IO.Path.GetRandomFileName()))) as TemporaryFolder; + } + + /// <summary> + /// Makes the temporary item visible to the user somehow. + /// </summary> + public override void Display() + { + Process.Start("explorer.exe", string.Format("/select,\"{0}\"", Path)); + } + } +} diff --git a/Software/Visual_Studio/Tango.Core/IO/TemporaryItem.cs b/Software/Visual_Studio/Tango.Core/IO/TemporaryItem.cs index 46464bf15..662a0ad58 100644 --- a/Software/Visual_Studio/Tango.Core/IO/TemporaryItem.cs +++ b/Software/Visual_Studio/Tango.Core/IO/TemporaryItem.cs @@ -4,31 +4,168 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections.ObjectModel; +using System.IO; namespace Tango.Core.IO { + /// <summary> + /// Represents a temporary item base class. + /// </summary> + /// <seealso cref="Tango.Core.IO.ITemporaryItem" /> public abstract class TemporaryItem : ITemporaryItem { + /// <summary> + /// Occurs when this item has been deleted. + /// </summary> + public event EventHandler Deleted; + + /// <summary> + /// Gets the creation date of the temporary item. + /// </summary> public DateTime Date { get; protected set; } - public abstract string Path { get; protected set; } - public abstract IReadOnlyCollection<ITemporaryItem> Items { get; protected set; } - public virtual void AddItem(ITemporaryItem item) + /// <summary> + /// Gets the path to the temporary item. + /// </summary> + public string Path { get; protected set; } + + /// <summary> + /// Gets the optional item tag. + /// </summary> + public Object Tag { get; protected set; } + + /// <summary> + /// Gets or sets a value indicating whether this <see cref="ITemporaryItem" /> should not be deleted by a parent item. + /// </summary> + public bool Persist { get; set; } + + /// <summary> + /// Gets the collection of child items. + /// </summary> + public IReadOnlyCollection<ITemporaryItem> Items { get; private set; } + + /// <summary> + /// Initializes a new instance of the <see cref="TemporaryItem"/> class. + /// </summary> + /// <param name="path">The temporary item path.</param> + /// <param name="tag">The item tag.</param> + public TemporaryItem(String path, Object tag) + { + Items = new ReadOnlyCollection<ITemporaryItem>(new List<ITemporaryItem>()); + Date = DateTime.Now; + Path = path; + Tag = tag; + } + + /// <summary> + /// Initializes a new instance of the <see cref="TemporaryItem"/> class. + /// </summary> + /// <param name="path">The temporary item path.</param> + public TemporaryItem(String path) : this(path, null) { + + } + + /// <summary> + /// Adds a child item. + /// </summary> + /// <param name="item">The item.</param> + public ITemporaryItem AddItem(ITemporaryItem item) + { + item.Deleted += Item_Deleted; var list = Items.ToList(); list.Add(item); + Items = list.ToReadOnlyCollection(); + return item; + } - //IReadOnlyCollection<String> s = new ReadOnlyCollection<String>(); + /// <summary> + /// Removes the specified child item. + /// </summary> + public void RemoveItem(ITemporaryItem item) + { + item.Deleted -= Item_Deleted; + var list = Items.ToList(); + list.Remove(item); + Items = list.ToReadOnlyCollection(); + } + + /// <summary> + /// Handles the Deleted event of the child Item. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> + private void Item_Deleted(object sender, EventArgs e) + { + RemoveItem(sender as ITemporaryItem); } - public virtual void Delete() + /// <summary> + /// Deletes the temporary item. + /// </summary> + /// <returns> + /// True is deletion was successful. + /// </returns> + public virtual bool Delete() { + List<ITemporaryItem> remained = new List<ITemporaryItem>(); + + var items = Items; + + foreach (var item in items.Where(x => !x.Persist)) + { + if (!item.Delete()) + { + remained.Add(item); + } + else + { + Deleted?.Invoke(this, new EventArgs()); + } + } + + Items = new ReadOnlyCollection<ITemporaryItem>(remained); + return remained.Count == 0; } - public virtual void RemoveItem(ITemporaryItem item) + /// <summary> + /// Deletes the temporary item asynchronously. + /// </summary> + /// <returns> + /// True is deletion was successful. + /// </returns> + public Task<bool> DeleteAsync() { + return Task.Factory.StartNew<bool>(() => Delete()); + } + + /// <summary> + /// Makes the temporary item visible to the user somehow. + /// </summary> + public abstract void Display(); + + /// <summary> + /// Returns a <see cref="System.String" /> that represents this instance. + /// </summary> + /// <returns> + /// A <see cref="System.String" /> that represents this instance. + /// </returns> + public override string ToString() + { + return Path; + } + /// <summary> + /// Performs an explicit conversion from <see cref="TemporaryItem"/> to <see cref="String"/>. + /// </summary> + /// <param name="item">The item.</param> + /// <returns> + /// The result of the conversion. + /// </returns> + public static implicit operator String(TemporaryItem item) + { + return item.Path; } } } diff --git a/Software/Visual_Studio/Tango.Core/IO/TemporaryManager.cs b/Software/Visual_Studio/Tango.Core/IO/TemporaryManager.cs new file mode 100644 index 000000000..27702bd7f --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/IO/TemporaryManager.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Core.IO +{ + /// <summary> + /// Represents the Tango system temporary file manager. + /// </summary> + public class TemporaryManager : IDisposable + { + #region Singleton + + private static TemporaryManager _default; + /// <summary> + /// Gets the default temporary manager instance. + /// </summary> + public static TemporaryManager Default + { + get + { + if (_default == null) + { + _default = new TemporaryManager(); + } + + return _default; + } + } + + /// <summary> + /// Prevents a default instance of the <see cref="TemporaryManager"/> class from being created. + /// </summary> + private TemporaryManager() + { + Items = new List<ITemporaryItem>().ToReadOnlyCollection(); + RootPath = Path.Combine(Path.GetTempPath(), "Twine"); + Directory.CreateDirectory(RootPath); + } + + #endregion + + private bool _disposed; + + /// <summary> + /// Gets or sets the temporary root path. + /// </summary> + public String RootPath { get; protected set; } + + /// <summary> + /// Gets the collection of temporary items. + /// </summary> + public IReadOnlyCollection<ITemporaryItem> Items { get; protected set; } + + /// <summary> + /// Adds the item. + /// </summary> + /// <param name="item">The item.</param> + private void AddItem(ITemporaryItem item) + { + var list = Items.ToList(); + list.Add(item); + Items = list.ToReadOnlyCollection(); + item.Deleted += Item_Deleted; + } + + /// <summary> + /// Removes the item. + /// </summary> + /// <param name="item">The item.</param> + private void RemoveItem(ITemporaryItem item) + { + var list = Items.ToList(); + list.Remove(item); + Items = list.ToReadOnlyCollection(); + item.Deleted -= Item_Deleted; + } + + /// <summary> + /// Handles the Deleted event of the item. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> + private void Item_Deleted(object sender, EventArgs e) + { + RemoveItem(sender as ITemporaryItem); + } + + /// <summary> + /// Creates a new temporary folder and returns the folder item. + /// </summary> + /// <returns></returns> + public virtual TemporaryFolder CreateFolder() + { + TemporaryFolder folder = new TemporaryFolder(Path.Combine(RootPath, Path.GetRandomFileName())); + AddItem(folder); + return folder; + } + + /// <summary> + /// Creates a new temporary file and returns the file item. + /// </summary> + /// <returns></returns> + public virtual TemporaryFile CreateFile(String extension = null) + { + TemporaryFile file = new TemporaryFile(Path.Combine(RootPath, Path.GetRandomFileName() + extension)); + AddItem(file); + return file; + } + + /// <summary> + /// Deletes all items. + /// </summary> + public virtual void Clean() + { + var items = Items; + + foreach (var item in items.Where(x => !x.Persist)) + { + item.Delete(); + } + } + + /// <summary> + /// Deletes all items. + /// </summary> + public virtual Task CleanAsync() + { + return Task.Factory.StartNew(() => Clean()); + } + + /// <summary> + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// </summary> + public void Dispose() + { + if (!_disposed) + { + Clean(); + _disposed = true; + } + } + + /// <summary> + /// Finalizes an instance of the <see cref="TemporaryManager"/> class. + /// </summary> + ~TemporaryManager() + { + Dispose(); + } + } +} |
