root / trunk / Pithos.Client.WPF / SelectiveSynch / DirectoryRecord.cs @ 2115e2a5
History | View | Annotate | Download (9.2 kB)
1 |
#region |
---|---|
2 |
/* ----------------------------------------------------------------------- |
3 |
* <copyright file="DirectoryRecord.cs" company="GRNet"> |
4 |
* |
5 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or |
8 |
* without modification, are permitted provided that the following |
9 |
* conditions are met: |
10 |
* |
11 |
* 1. Redistributions of source code must retain the above |
12 |
* copyright notice, this list of conditions and the following |
13 |
* disclaimer. |
14 |
* |
15 |
* 2. Redistributions in binary form must reproduce the above |
16 |
* copyright notice, this list of conditions and the following |
17 |
* disclaimer in the documentation and/or other materials |
18 |
* provided with the distribution. |
19 |
* |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 |
* POSSIBILITY OF SUCH DAMAGE. |
33 |
* |
34 |
* The views and conclusions contained in the software and |
35 |
* documentation are those of the authors and should not be |
36 |
* interpreted as representing official policies, either expressed |
37 |
* or implied, of GRNET S.A. |
38 |
* </copyright> |
39 |
* ----------------------------------------------------------------------- |
40 |
*/ |
41 |
#endregion |
42 |
using System; |
43 |
using System.Collections; |
44 |
using System.Collections.Generic; |
45 |
using System.Collections.Specialized; |
46 |
using System.Diagnostics; |
47 |
using System.IO; |
48 |
using System.Linq; |
49 |
using System.Text; |
50 |
using Caliburn.Micro; |
51 |
using Pithos.Client.WPF.Utils; |
52 |
using Pithos.Core.Agents; |
53 |
using Pithos.Interfaces; |
54 |
|
55 |
namespace Pithos.Client.WPF.SelectiveSynch |
56 |
{ |
57 |
[DebuggerDisplay("{Uri} : {IsChecked}")] |
58 |
public class DirectoryRecord :PropertyChangedBase, IEnumerable<DirectoryRecord> |
59 |
{ |
60 |
private ObjectInfo _objectInfo; |
61 |
public ObjectInfo ObjectInfo |
62 |
{ |
63 |
get { return _objectInfo; } |
64 |
set |
65 |
{ |
66 |
_objectInfo = value; |
67 |
Uri = value.Uri; |
68 |
} |
69 |
} |
70 |
|
71 |
public Uri Uri |
72 |
{ |
73 |
get { return _uri; } |
74 |
set { _uri = value; } |
75 |
} |
76 |
|
77 |
//public DirectoryInfo LocalInfo { get; set; } |
78 |
|
79 |
|
80 |
private DirectoryRecord _parent; |
81 |
public DirectoryRecord Parent |
82 |
{ |
83 |
get { return _parent; } |
84 |
private set { _parent = value; } |
85 |
} |
86 |
|
87 |
public bool Added { get; set; } |
88 |
public bool Removed { get; set; } |
89 |
|
90 |
private bool? _isChecked=true; |
91 |
#region IsChecked |
92 |
|
93 |
/// <summary> |
94 |
/// Gets/sets the state of the associated UI toggle (ex. CheckBox). |
95 |
/// The return value is calculated based on the check state of all |
96 |
/// child FooViewModels. Setting this property to true or false |
97 |
/// will set all children to the same check state, and setting it |
98 |
/// to any value will cause the parent to verify its check state. |
99 |
/// </summary> |
100 |
public bool? IsChecked |
101 |
{ |
102 |
get |
103 |
{ |
104 |
if (_directories.Count==0) |
105 |
return _isChecked; |
106 |
|
107 |
if (_isChecked == true) |
108 |
return _isChecked; |
109 |
//Check children to decide whether to display a clear (false) or gray (null) |
110 |
return _directories.Any(d => d.IsChecked == true) ? null : (bool?)false; |
111 |
} |
112 |
set { this.SetIsChecked(value, true, true); } |
113 |
} |
114 |
|
115 |
void SetIsChecked(bool? value, bool updateChildren, bool updateParent) |
116 |
{ |
117 |
if (value == _isChecked) |
118 |
return; |
119 |
|
120 |
_isChecked = value??false; |
121 |
|
122 |
//If the value is null both Added and Removed should be False |
123 |
Added = _isChecked.Value; |
124 |
Removed = !(_isChecked.Value); |
125 |
|
126 |
if (updateChildren ) |
127 |
this.Directories.Apply(c => c.SetIsChecked(_isChecked, true, false)); |
128 |
|
129 |
if (updateParent && _parent != null) |
130 |
_parent.VerifyCheckState(); |
131 |
|
132 |
NotifyOfPropertyChange(()=>IsChecked); |
133 |
} |
134 |
|
135 |
void VerifyCheckState() |
136 |
{ |
137 |
NotifyOfPropertyChange(() => IsChecked); |
138 |
return; |
139 |
bool? state = null; |
140 |
for (var i = 0; i < this.Directories.Count(); ++i) |
141 |
{ |
142 |
bool? current = this.Directories.ElementAt(i).IsChecked; |
143 |
if (i == 0) |
144 |
{ |
145 |
state = current; |
146 |
} |
147 |
else if (state != current) |
148 |
{ |
149 |
state = null; |
150 |
break; |
151 |
} |
152 |
} |
153 |
this.SetIsChecked(state, false, true); |
154 |
} |
155 |
|
156 |
#endregion // IsChecked |
157 |
|
158 |
|
159 |
public bool IsInitiallySelected { get; private set; } |
160 |
|
161 |
private List<DirectoryRecord> _directories=new List<DirectoryRecord>(); |
162 |
public List<DirectoryRecord> Directories |
163 |
{ |
164 |
get { return _directories; } |
165 |
set |
166 |
{ |
167 |
_directories= value; |
168 |
foreach (var dir in value) |
169 |
{ |
170 |
dir.Parent = this; |
171 |
} |
172 |
} |
173 |
} |
174 |
|
175 |
public DirectoryRecord() |
176 |
{ |
177 |
|
178 |
/* |
179 |
_directories = new Lazy<List<DirectoryRecord>>(() => |
180 |
new List<DirectoryRecord>()); |
181 |
*/ |
182 |
/* |
183 |
_directories = new Lazy<List<DirectoryRecord>>(() => |
184 |
(from directory in Info.EnumerateDirectories("*", SearchOption.TopDirectoryOnly) |
185 |
where !directory.FullName.StartsWith(ignorePath) |
186 |
select new DirectoryRecord(ignorePath) { Info = directory }).ToList()); |
187 |
*/ |
188 |
} |
189 |
|
190 |
private string _displayName; |
191 |
private Uri _uri; |
192 |
|
193 |
public string DisplayName |
194 |
{ |
195 |
get |
196 |
{ |
197 |
if (ObjectInfo != null) |
198 |
return Uri.UnescapeDataString(ObjectInfo.Name.ToString().Split('/').Last()); |
199 |
return _displayName; |
200 |
} |
201 |
set { _displayName = value; } |
202 |
} |
203 |
|
204 |
public bool IsExplicitlyChecked |
205 |
{ |
206 |
set |
207 |
{ |
208 |
_isChecked=value; |
209 |
NotifyOfPropertyChange(() => IsChecked); ; |
210 |
} |
211 |
} |
212 |
|
213 |
public DirectoryRecord(ObjectInfo info) |
214 |
{ |
215 |
ObjectInfo = info; |
216 |
} |
217 |
|
218 |
|
219 |
/* |
220 |
public IEnumerable<DirectoryInfo> GetCheckedDirectories() |
221 |
{ |
222 |
var q = from record in this |
223 |
where record.IsChecked==true |
224 |
select record.Info; |
225 |
return q; |
226 |
} |
227 |
*/ |
228 |
|
229 |
/* |
230 |
public void SetSelections(StringCollection selections) |
231 |
{ |
232 |
IsChecked=selections.Contains(Info.FullName); |
233 |
foreach (var children in Directories) |
234 |
{ |
235 |
children.SetSelections(selections); |
236 |
} |
237 |
} |
238 |
*/ |
239 |
|
240 |
|
241 |
public IEnumerator<DirectoryRecord> GetEnumerator() |
242 |
{ |
243 |
yield return this; |
244 |
foreach (var children in Directories) |
245 |
foreach (var info in children) |
246 |
{ |
247 |
yield return info; |
248 |
} |
249 |
} |
250 |
|
251 |
IEnumerator IEnumerable.GetEnumerator() |
252 |
{ |
253 |
return GetEnumerator(); |
254 |
} |
255 |
|
256 |
public override int GetHashCode() |
257 |
{ |
258 |
return ObjectInfo == null |
259 |
? (Uri == null ? DisplayName.GetHashCode() : Uri.GetHashCode()) |
260 |
: ObjectInfo.GetHashCode(); |
261 |
} |
262 |
|
263 |
public override bool Equals(object obj) |
264 |
{ |
265 |
if (!(obj is DirectoryRecord)) |
266 |
return false; |
267 |
var other = (DirectoryRecord)obj; |
268 |
if (Uri != other.Uri) |
269 |
return false; |
270 |
if (ObjectInfo== null ^ other.ObjectInfo== null) |
271 |
return false; |
272 |
|
273 |
if (ObjectInfo!= null && !ObjectInfo.Equals(other.ObjectInfo)) |
274 |
return false; |
275 |
var thisEnum = GetEnumerator(); |
276 |
var otherEnum = other.GetEnumerator(); |
277 |
//Skipt the first item, it is the current node itself |
278 |
thisEnum.MoveNext(); |
279 |
otherEnum.MoveNext(); |
280 |
while (true) |
281 |
{ |
282 |
var thisMove = thisEnum.MoveNext(); |
283 |
var otherMove = otherEnum.MoveNext(); |
284 |
|
285 |
if (thisMove ^ otherMove) |
286 |
return false; |
287 |
if (!thisMove) |
288 |
return true; |
289 |
|
290 |
if (!thisEnum.Current.Equals(otherEnum.Current)) |
291 |
return false; |
292 |
} |
293 |
} |
294 |
} |
295 |
} |