Added header to all files. Closes #2064
[pithos-ms-client] / trunk / Pithos.Client.WPF / SelectiveSynch / SelectiveSynchViewModel.cs
1 #region
2 /* -----------------------------------------------------------------------
3  * <copyright file="SelectiveSynchViewModel.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.Generic;
44 using System.Collections.ObjectModel;
45 using System.Collections.Specialized;
46 using System.IO;
47 using System.Linq;
48 using System.Text;
49 using Caliburn.Micro;
50 using Pithos.Client.WPF.Properties;
51 using Pithos.Interfaces;
52 using Pithos.Network;
53
54 namespace Pithos.Client.WPF.SelectiveSynch
55 {
56     class SelectiveSynchViewModel:Screen
57     {
58         private IEventAggregator _events ;
59
60         private string _rootPath;
61         private string _cachePath;
62         public string RootPath
63         {
64             get { return _rootPath; }
65             set
66             {
67                 _rootPath = value;
68                 _cachePath = Path.Combine(_rootPath, FolderConstants.CacheFolder);
69                 _pithosDirectory = new ObservableCollection<DirectoryRecord>{
70                         new DirectoryRecord(_cachePath) {Info = new DirectoryInfo(value)}};
71                 NotifyOfPropertyChange(() => RootPath);
72                 NotifyOfPropertyChange(()=>PithosDirectory);
73             }
74         }
75
76         private string _title;
77         public string Title
78         {
79             get { return _title; }
80             set
81             {
82                 _title = value;
83                 NotifyOfPropertyChange(() => Title);
84             }
85         }
86
87         public AccountSettings Account { get; set; }
88
89         private ObservableCollection<DirectoryRecord> _pithosDirectory;
90         public ObservableCollection<DirectoryRecord> PithosDirectory
91         {
92             get { return _pithosDirectory; }
93         }
94
95         private ObservableCollection<DirectoryInfo> _checks;
96         public ObservableCollection<DirectoryInfo> Checks
97         {
98             get { return _checks; }
99         }
100
101         public void GetChecks()
102         {
103             var root = PithosDirectory[0];            
104             _checks = new ObservableCollection<DirectoryInfo>(
105                 from record in root
106                 where record.IsChecked==true
107                 select record.Info);
108             NotifyOfPropertyChange(() => Checks);
109         }
110
111         public SelectiveSynchViewModel(IEnumerable<string> folders, IEventAggregator events, AccountSettings account)
112         {
113             Account = account;
114             AccountName = account.AccountName;
115             Title = account.AccountName;
116             RootPath = account.RootPath;
117
118             SetInitialSelections(account);
119         }
120
121         private void SetInitialSelections(AccountSettings account)
122         {
123             var selections = account.SelectiveFolders;
124             if (selections.Count == 0)
125                 return;
126             var root = PithosDirectory[0];
127             var selects= from record in root
128                              where selections.Contains(record.Info.FullName)
129                              select record;
130             selects.Apply(record=>record.IsChecked=true);            
131         }
132
133         protected string AccountName { get; set; }
134
135         public void SaveChanges()
136         {
137             var selections = GetSelectedFolderNames();
138
139             SaveSettings(selections);
140             var root = PithosDirectory[0];
141             
142             var added= (from record in root
143                          where record.Added
144                          select record.Info.FullName.ToLower()).ToArray();
145             var removed= (from record in root
146                          where record.Removed
147                          select record.Info.FullName.ToLower()).ToArray();            
148
149             _events.Publish(new SelectiveSynchChanges{Account=Account,Added=added,Removed=removed});
150             
151
152             
153
154             TryClose(true);
155         }
156
157         
158         private void SaveSettings(string[] selections)
159         {
160             Account.SelectiveFolders.Clear();
161             Account.SelectiveFolders.AddRange(selections);
162             Settings.Default.Save();            
163         }
164
165         private string[] GetSelectedFolderNames()
166         {
167             var root = PithosDirectory[0];
168             var selections = from record in root
169                          where record.IsChecked == true
170                          select record.Info.FullName;
171             return selections.ToArray();
172         }
173
174         public void RejectChanges()
175         {
176             TryClose(false);
177         }
178     }
179 }