root / trunk / Pithos.Client.WPF / SelectiveSynch / DirectoryRecord.cs @ 73f7c768
History | View | Annotate | Download (8.8 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 { get; set; } |
72 |
//public DirectoryInfo LocalInfo { get; set; } |
73 |
|
74 |
DirectoryRecord _parent; |
75 |
|
76 |
public bool Added { get; set; } |
77 |
public bool Removed { get; set; } |
78 |
|
79 |
private bool? _isChecked=true; |
80 |
#region IsChecked |
81 |
|
82 |
/// <summary> |
83 |
/// Gets/sets the state of the associated UI toggle (ex. CheckBox). |
84 |
/// The return value is calculated based on the check state of all |
85 |
/// child FooViewModels. Setting this property to true or false |
86 |
/// will set all children to the same check state, and setting it |
87 |
/// to any value will cause the parent to verify its check state. |
88 |
/// </summary> |
89 |
public bool? IsChecked |
90 |
{ |
91 |
get |
92 |
{ |
93 |
if (_directories.Count==0) |
94 |
return _isChecked; |
95 |
|
96 |
if (_isChecked == true) |
97 |
return _isChecked; |
98 |
//Check children to decide whether to display a clear (false) or gray (null) |
99 |
return _directories.Any(d => d.IsChecked == true) ? null : (bool?)false; |
100 |
} |
101 |
set { this.SetIsChecked(value, true, true); } |
102 |
} |
103 |
|
104 |
void SetIsChecked(bool? value, bool updateChildren, bool updateParent) |
105 |
{ |
106 |
if (value == _isChecked) |
107 |
return; |
108 |
|
109 |
_isChecked = value; |
110 |
|
111 |
//If the value is null both Added and Removed should be False |
112 |
Added = _isChecked??false; |
113 |
Removed = !(_isChecked??true); |
114 |
|
115 |
if (updateChildren && _isChecked.HasValue) |
116 |
this.Directories.Apply(c => c.SetIsChecked(_isChecked, true, false)); |
117 |
|
118 |
if (updateParent && _parent != null) |
119 |
_parent.VerifyCheckState(); |
120 |
|
121 |
this.RaisePropertyChangedEventImmediately("IsChecked"); |
122 |
} |
123 |
|
124 |
void VerifyCheckState() |
125 |
{ |
126 |
bool? state = null; |
127 |
for (var i = 0; i < this.Directories.Count(); ++i) |
128 |
{ |
129 |
bool? current = this.Directories.ElementAt(i).IsChecked; |
130 |
if (i == 0) |
131 |
{ |
132 |
state = current; |
133 |
} |
134 |
else if (state != current) |
135 |
{ |
136 |
state = null; |
137 |
break; |
138 |
} |
139 |
} |
140 |
this.SetIsChecked(state, false, true); |
141 |
} |
142 |
|
143 |
#endregion // IsChecked |
144 |
|
145 |
|
146 |
public bool IsInitiallySelected { get; private set; } |
147 |
|
148 |
private List<DirectoryRecord> _directories=new List<DirectoryRecord>(); |
149 |
public List<DirectoryRecord> Directories |
150 |
{ |
151 |
get { return _directories; } |
152 |
set { _directories= value; } |
153 |
} |
154 |
|
155 |
public DirectoryRecord() |
156 |
{ |
157 |
|
158 |
/* |
159 |
_directories = new Lazy<List<DirectoryRecord>>(() => |
160 |
new List<DirectoryRecord>()); |
161 |
*/ |
162 |
/* |
163 |
_directories = new Lazy<List<DirectoryRecord>>(() => |
164 |
(from directory in Info.EnumerateDirectories("*", SearchOption.TopDirectoryOnly) |
165 |
where !directory.FullName.StartsWith(ignorePath) |
166 |
select new DirectoryRecord(ignorePath) { Info = directory }).ToList()); |
167 |
*/ |
168 |
} |
169 |
|
170 |
private string _displayName; |
171 |
|
172 |
public string DisplayName |
173 |
{ |
174 |
get |
175 |
{ |
176 |
if (ObjectInfo != null) |
177 |
return ObjectInfo.Name.Split('/').Last(); |
178 |
return _displayName; |
179 |
} |
180 |
set { _displayName = value; } |
181 |
} |
182 |
|
183 |
public bool IsExplicitlyChecked |
184 |
{ |
185 |
set |
186 |
{ |
187 |
_isChecked=value; |
188 |
this.RaisePropertyChangedEventImmediately("IsChecked"); |
189 |
} |
190 |
} |
191 |
|
192 |
public DirectoryRecord(ObjectInfo info) |
193 |
{ |
194 |
ObjectInfo = info; |
195 |
} |
196 |
|
197 |
|
198 |
/* |
199 |
public IEnumerable<DirectoryInfo> GetCheckedDirectories() |
200 |
{ |
201 |
var q = from record in this |
202 |
where record.IsChecked==true |
203 |
select record.Info; |
204 |
return q; |
205 |
} |
206 |
*/ |
207 |
|
208 |
/* |
209 |
public void SetSelections(StringCollection selections) |
210 |
{ |
211 |
IsChecked=selections.Contains(Info.FullName); |
212 |
foreach (var children in Directories) |
213 |
{ |
214 |
children.SetSelections(selections); |
215 |
} |
216 |
} |
217 |
*/ |
218 |
|
219 |
|
220 |
public IEnumerator<DirectoryRecord> GetEnumerator() |
221 |
{ |
222 |
yield return this; |
223 |
foreach (var children in Directories) |
224 |
foreach (var info in children) |
225 |
{ |
226 |
yield return info; |
227 |
} |
228 |
} |
229 |
|
230 |
IEnumerator IEnumerable.GetEnumerator() |
231 |
{ |
232 |
return GetEnumerator(); |
233 |
} |
234 |
|
235 |
public override int GetHashCode() |
236 |
{ |
237 |
return ObjectInfo == null |
238 |
? (Uri == null ? DisplayName.GetHashCode() : Uri.GetHashCode()) |
239 |
: ObjectInfo.GetHashCode(); |
240 |
} |
241 |
|
242 |
public override bool Equals(object obj) |
243 |
{ |
244 |
if (!(obj is DirectoryRecord)) |
245 |
return false; |
246 |
var other = (DirectoryRecord)obj; |
247 |
if (Uri != other.Uri) |
248 |
return false; |
249 |
if (ObjectInfo== null ^ other.ObjectInfo== null) |
250 |
return false; |
251 |
|
252 |
if (ObjectInfo!= null && !ObjectInfo.Equals(other.ObjectInfo)) |
253 |
return false; |
254 |
var thisEnum = GetEnumerator(); |
255 |
var otherEnum = other.GetEnumerator(); |
256 |
//Skipt the first item, it is the current node itself |
257 |
thisEnum.MoveNext(); |
258 |
otherEnum.MoveNext(); |
259 |
while (true) |
260 |
{ |
261 |
var thisMove = thisEnum.MoveNext(); |
262 |
var otherMove = otherEnum.MoveNext(); |
263 |
|
264 |
if (thisMove ^ otherMove) |
265 |
return false; |
266 |
if (!thisMove) |
267 |
return true; |
268 |
|
269 |
if (!thisEnum.Current.Equals(otherEnum.Current)) |
270 |
return false; |
271 |
} |
272 |
} |
273 |
} |
274 |
} |