blob: f8ff8580a470fb7d192575012c37ff9f32774133 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
using System;
using System.Collections.Generic;
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; }
/// <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;
}
/// <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);
}
/// <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;
}
/// <summary>
/// Returns true if the item exists.
/// </summary>
/// <returns></returns>
public abstract bool Exists();
/// <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;
}
}
}
|