2 /* -----------------------------------------------------------------------
3 * <copyright file="DirectoryRecord.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
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.
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.
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.
39 * -----------------------------------------------------------------------
43 using System.Collections;
44 using System.Collections.Generic;
45 using System.Collections.Specialized;
46 using System.Diagnostics;
51 using Pithos.Client.WPF.Utils;
52 using Pithos.Core.Agents;
53 using Pithos.Interfaces;
55 namespace Pithos.Client.WPF.SelectiveSynch
57 [DebuggerDisplay("{Uri} : {IsChecked}")]
58 public class DirectoryRecord :PropertyChangedBase, IEnumerable<DirectoryRecord>
60 private ObjectInfo _objectInfo;
61 public ObjectInfo ObjectInfo
63 get { return _objectInfo; }
71 public Uri Uri { get; set; }
72 //public DirectoryInfo LocalInfo { get; set; }
74 DirectoryRecord _parent;
76 public bool Added { get; set; }
77 public bool Removed { get; set; }
79 private bool? _isChecked=true;
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.
89 public bool? IsChecked
91 get { return _isChecked; }
92 set { this.SetIsChecked(value, true, true); }
95 void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
97 if (value == _isChecked)
102 //If the value is null both Added and Removed should be False
103 Added = _isChecked??false;
104 Removed = !(_isChecked??true);
106 if (updateChildren && _isChecked.HasValue)
107 this.Directories.Apply(c => c.SetIsChecked(_isChecked, true, false));
109 if (updateParent && _parent != null)
110 _parent.VerifyCheckState();
112 this.RaisePropertyChangedEventImmediately("IsChecked");
115 void VerifyCheckState()
118 for (var i = 0; i < this.Directories.Count(); ++i)
120 bool? current = this.Directories.ElementAt(i).IsChecked;
125 else if (state != current)
131 this.SetIsChecked(state, false, true);
134 #endregion // IsChecked
137 public bool IsInitiallySelected { get; private set; }
139 private List<DirectoryRecord> _directories=new List<DirectoryRecord>();
140 public List<DirectoryRecord> Directories
142 get { return _directories; }
143 set { _directories= value; }
146 public DirectoryRecord()
150 _directories = new Lazy<List<DirectoryRecord>>(() =>
151 new List<DirectoryRecord>());
154 _directories = new Lazy<List<DirectoryRecord>>(() =>
155 (from directory in Info.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
156 where !directory.FullName.StartsWith(ignorePath)
157 select new DirectoryRecord(ignorePath) { Info = directory }).ToList());
161 private string _displayName;
163 public string DisplayName
167 if (ObjectInfo != null)
168 return ObjectInfo.Name.Split('/').Last();
171 set { _displayName = value; }
174 public bool IsExplicitlyChecked
179 this.RaisePropertyChangedEventImmediately("IsChecked");
\r
183 public DirectoryRecord(ObjectInfo info)
190 public IEnumerable<DirectoryInfo> GetCheckedDirectories()
192 var q = from record in this
193 where record.IsChecked==true
200 public void SetSelections(StringCollection selections)
202 IsChecked=selections.Contains(Info.FullName);
203 foreach (var children in Directories)
205 children.SetSelections(selections);
211 public IEnumerator<DirectoryRecord> GetEnumerator()
214 foreach (var children in Directories)
215 foreach (var info in children)
221 IEnumerator IEnumerable.GetEnumerator()
223 return GetEnumerator();
226 public override int GetHashCode()
228 return ObjectInfo == null
229 ? (Uri == null ? DisplayName.GetHashCode() : Uri.GetHashCode())
230 : ObjectInfo.GetHashCode();
233 public override bool Equals(object obj)
235 if (!(obj is DirectoryRecord))
237 var other = (DirectoryRecord)obj;
238 if (Uri != other.Uri)
240 if (ObjectInfo== null ^ other.ObjectInfo== null)
243 if (ObjectInfo!= null && !ObjectInfo.Equals(other.ObjectInfo))
245 var thisEnum = GetEnumerator();
246 var otherEnum = other.GetEnumerator();
247 //Skipt the first item, it is the current node itself
249 otherEnum.MoveNext();
252 var thisMove = thisEnum.MoveNext();
253 var otherMove = otherEnum.MoveNext();
255 if (thisMove ^ otherMove)
260 if (!thisEnum.Current.Equals(otherEnum.Current))