root / trunk / Pithos.Client.WPF / SelectiveSynch / DirectoryRecord.cs @ 759bd3c4
History | View | Annotate | Download (7 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.IO; |
47 |
using System.Linq; |
48 |
using System.Text; |
49 |
using Caliburn.Micro; |
50 |
using Pithos.Core.Agents; |
51 |
using Pithos.Interfaces; |
52 |
|
53 |
namespace Pithos.Client.WPF.SelectiveSynch |
54 |
{ |
55 |
public class DirectoryRecord : PropertyChangedBase,IEnumerable<DirectoryRecord> |
56 |
{ |
57 |
private ObjectInfo _objectInfo; |
58 |
public ObjectInfo ObjectInfo |
59 |
{ |
60 |
get { return _objectInfo; } |
61 |
set |
62 |
{ |
63 |
_objectInfo = value; |
64 |
Uri = value.Uri; |
65 |
} |
66 |
} |
67 |
|
68 |
public Uri Uri { get; set; } |
69 |
//public DirectoryInfo LocalInfo { get; set; } |
70 |
|
71 |
DirectoryRecord _parent; |
72 |
|
73 |
public bool Added { get; set; } |
74 |
public bool Removed { get; set; } |
75 |
|
76 |
private bool? _isChecked=true; |
77 |
#region IsChecked |
78 |
|
79 |
/// <summary> |
80 |
/// Gets/sets the state of the associated UI toggle (ex. CheckBox). |
81 |
/// The return value is calculated based on the check state of all |
82 |
/// child FooViewModels. Setting this property to true or false |
83 |
/// will set all children to the same check state, and setting it |
84 |
/// to any value will cause the parent to verify its check state. |
85 |
/// </summary> |
86 |
public bool? IsChecked |
87 |
{ |
88 |
get { return _isChecked; } |
89 |
set { this.SetIsChecked(value, true, true); } |
90 |
} |
91 |
|
92 |
void SetIsChecked(bool? value, bool updateChildren, bool updateParent) |
93 |
{ |
94 |
if (value == _isChecked) |
95 |
return; |
96 |
|
97 |
_isChecked = value; |
98 |
|
99 |
//If the value is null both Added and Removed should be False |
100 |
Added = _isChecked??false; |
101 |
Removed = !(_isChecked??true); |
102 |
|
103 |
if (updateChildren && _isChecked.HasValue) |
104 |
this.Directories.Apply(c => c.SetIsChecked(_isChecked, true, false)); |
105 |
|
106 |
if (updateParent && _parent != null) |
107 |
_parent.VerifyCheckState(); |
108 |
|
109 |
this.RaisePropertyChangedEventImmediately("IsChecked"); |
110 |
} |
111 |
|
112 |
void VerifyCheckState() |
113 |
{ |
114 |
bool? state = null; |
115 |
for (var i = 0; i < this.Directories.Count(); ++i) |
116 |
{ |
117 |
bool? current = this.Directories.ElementAt(i).IsChecked; |
118 |
if (i == 0) |
119 |
{ |
120 |
state = current; |
121 |
} |
122 |
else if (state != current) |
123 |
{ |
124 |
state = null; |
125 |
break; |
126 |
} |
127 |
} |
128 |
this.SetIsChecked(state, false, true); |
129 |
} |
130 |
|
131 |
#endregion // IsChecked |
132 |
|
133 |
|
134 |
public bool IsInitiallySelected { get; private set; } |
135 |
|
136 |
/*readonly Lazy<List<DirectoryRecord>> _directories = new Lazy<List<DirectoryRecord>>(); |
137 |
|
138 |
public List<DirectoryRecord> Directories |
139 |
{ |
140 |
get |
141 |
{ |
142 |
return _directories.Value; |
143 |
} |
144 |
}*/ |
145 |
|
146 |
private IEnumerable<DirectoryRecord> _directories=new List<DirectoryRecord>(); |
147 |
public IEnumerable<DirectoryRecord> Directories |
148 |
{ |
149 |
get { return _directories; } |
150 |
set { _directories = value; } |
151 |
} |
152 |
|
153 |
public DirectoryRecord() |
154 |
{ |
155 |
|
156 |
/* |
157 |
_directories = new Lazy<List<DirectoryRecord>>(() => |
158 |
new List<DirectoryRecord>()); |
159 |
*/ |
160 |
/* |
161 |
_directories = new Lazy<List<DirectoryRecord>>(() => |
162 |
(from directory in Info.EnumerateDirectories("*", SearchOption.TopDirectoryOnly) |
163 |
where !directory.FullName.StartsWith(ignorePath) |
164 |
select new DirectoryRecord(ignorePath) { Info = directory }).ToList()); |
165 |
*/ |
166 |
} |
167 |
|
168 |
private string _displayName; |
169 |
public string DisplayName |
170 |
{ |
171 |
get |
172 |
{ |
173 |
if (ObjectInfo != null) |
174 |
return ObjectInfo.Name; |
175 |
return _displayName; |
176 |
} |
177 |
set { _displayName = value; } |
178 |
} |
179 |
|
180 |
public DirectoryRecord(string rootPath,ObjectInfo info) |
181 |
{ |
182 |
var relativePath = info.RelativeUrlToFilePath(info.Account); |
183 |
//LocalInfo = new DirectoryInfo(Path.Combine(rootPath, relativePath)); |
184 |
ObjectInfo = info; |
185 |
} |
186 |
|
187 |
|
188 |
/* |
189 |
public IEnumerable<DirectoryInfo> GetCheckedDirectories() |
190 |
{ |
191 |
var q = from record in this |
192 |
where record.IsChecked==true |
193 |
select record.Info; |
194 |
return q; |
195 |
} |
196 |
*/ |
197 |
|
198 |
/* |
199 |
public void SetSelections(StringCollection selections) |
200 |
{ |
201 |
IsChecked=selections.Contains(Info.FullName); |
202 |
foreach (var children in Directories) |
203 |
{ |
204 |
children.SetSelections(selections); |
205 |
} |
206 |
} |
207 |
*/ |
208 |
|
209 |
public IEnumerator<DirectoryRecord> GetEnumerator() |
210 |
{ |
211 |
yield return this; |
212 |
foreach (var children in Directories) |
213 |
foreach (var info in children) |
214 |
{ |
215 |
yield return info; |
216 |
} |
217 |
} |
218 |
|
219 |
IEnumerator IEnumerable.GetEnumerator() |
220 |
{ |
221 |
return GetEnumerator(); |
222 |
} |
223 |
} |
224 |
} |