Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / SelectiveSynch / DirectoryRecord.cs @ 255f5f86

History | View | Annotate | Download (5.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

    
51
namespace Pithos.Client.WPF.SelectiveSynch
52
{
53
    public class DirectoryRecord : PropertyChangedBase,IEnumerable<DirectoryRecord>
54
    {
55
        public DirectoryInfo Info { get; set; }
56

    
57

    
58
        DirectoryRecord _parent;
59

    
60
        public bool Added { get; set; }
61
        public bool Removed { get; set; }
62

    
63
        private bool? _isChecked;
64
        #region IsChecked
65

    
66
        /// <summary>
67
        /// Gets/sets the state of the associated UI toggle (ex. CheckBox).
68
        /// The return value is calculated based on the check state of all
69
        /// child FooViewModels.  Setting this property to true or false
70
        /// will set all children to the same check state, and setting it 
71
        /// to any value will cause the parent to verify its check state.
72
        /// </summary>
73
        public bool? IsChecked
74
        {
75
            get { return _isChecked; }
76
            set { this.SetIsChecked(value, true, true); }
77
        }
78

    
79
        void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
80
        {
81
            if (value == _isChecked)
82
                return;
83

    
84
            _isChecked = value;
85

    
86
            //If the value is null both Added and Removed should be False
87
            Added = _isChecked??false;
88
            Removed = !(_isChecked??true);
89

    
90
            if (updateChildren && _isChecked.HasValue)
91
                this.Directories.ForEach(c => c.SetIsChecked(_isChecked, true, false));
92

    
93
            if (updateParent && _parent != null)
94
                _parent.VerifyCheckState();
95

    
96
            this.RaisePropertyChangedEventImmediately("IsChecked");
97
        }
98

    
99
        void VerifyCheckState()
100
        {
101
            bool? state = null;
102
            for (int i = 0; i < this.Directories.Count; ++i)
103
            {
104
                bool? current = this.Directories[i].IsChecked;
105
                if (i == 0)
106
                {
107
                    state = current;
108
                }
109
                else if (state != current)
110
                {
111
                    state = null;
112
                    break;
113
                }
114
            }
115
            this.SetIsChecked(state, false, true);
116
        }
117

    
118
        #endregion // IsChecked
119

    
120

    
121
        public bool IsInitiallySelected { get; private set; }
122

    
123
        readonly Lazy<List<DirectoryRecord>> _directories = new Lazy<List<DirectoryRecord>>();
124

    
125
        public List<DirectoryRecord> Directories
126
        {
127
            get
128
            {
129
                return _directories.Value;
130
            }
131
        }
132

    
133
        public DirectoryRecord(string ignorePath)
134
        {
135
            _directories = new Lazy<List<DirectoryRecord>>(() => 
136
                (from directory in Info.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
137
                where !directory.FullName.StartsWith(ignorePath)
138
                select new DirectoryRecord(ignorePath) { Info = directory }).ToList());
139
        }
140

    
141

    
142

    
143
/*
144
        public IEnumerable<DirectoryInfo> GetCheckedDirectories()
145
        {
146
            var q = from record in this
147
                    where record.IsChecked==true
148
                    select record.Info;
149
            return q;
150
        }
151
*/
152

    
153
/*
154
        public void SetSelections(StringCollection selections)
155
        {
156
            IsChecked=selections.Contains(Info.FullName);                
157
            foreach (var children in Directories)
158
            {
159
                children.SetSelections(selections);
160
            }
161
        }
162
*/
163

    
164
        public IEnumerator<DirectoryRecord> GetEnumerator()
165
        {
166
            yield return this;
167
            foreach (var children in Directories)
168
                foreach (var info in children)
169
                {
170
                    yield return info;
171
                }
172
        }
173

    
174
        IEnumerator IEnumerable.GetEnumerator()
175
        {
176
            return GetEnumerator();
177
        }
178
    }
179
}