2 /* -----------------------------------------------------------------------
3 * <copyright file="SelectiveSynchViewModel.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.Generic;
44 using System.Collections.ObjectModel;
45 using System.Collections.Specialized;
50 using Pithos.Client.WPF.Properties;
51 using Pithos.Interfaces;
54 namespace Pithos.Client.WPF.SelectiveSynch
56 class SelectiveSynchViewModel:Screen
58 private IEventAggregator _events ;
60 private string _rootPath;
61 private string _cachePath;
62 public string RootPath
64 get { return _rootPath; }
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);
76 private string _title;
79 get { return _title; }
83 NotifyOfPropertyChange(() => Title);
87 public AccountSettings Account { get; set; }
89 private ObservableCollection<DirectoryRecord> _pithosDirectory;
90 public ObservableCollection<DirectoryRecord> PithosDirectory
92 get { return _pithosDirectory; }
95 private ObservableCollection<DirectoryInfo> _checks;
96 public ObservableCollection<DirectoryInfo> Checks
98 get { return _checks; }
101 public void GetChecks()
103 var root = PithosDirectory[0];
104 _checks = new ObservableCollection<DirectoryInfo>(
106 where record.IsChecked==true
108 NotifyOfPropertyChange(() => Checks);
111 public SelectiveSynchViewModel(IEnumerable<string> folders, IEventAggregator events, AccountSettings account)
114 AccountName = account.AccountName;
115 Title = account.AccountName;
116 RootPath = account.RootPath;
118 SetInitialSelections(account);
121 private void SetInitialSelections(AccountSettings account)
123 var selections = account.SelectiveFolders;
124 if (selections.Count == 0)
126 var root = PithosDirectory[0];
127 var selects= from record in root
128 where selections.Contains(record.Info.FullName)
130 selects.Apply(record=>record.IsChecked=true);
133 protected string AccountName { get; set; }
135 public void SaveChanges()
137 var selections = GetSelectedFolderNames();
139 SaveSettings(selections);
140 var root = PithosDirectory[0];
142 var added= (from record in root
144 select record.Info.FullName.ToLower()).ToArray();
145 var removed= (from record in root
147 select record.Info.FullName.ToLower()).ToArray();
149 _events.Publish(new SelectiveSynchChanges{Account=Account,Added=added,Removed=removed});
158 private void SaveSettings(string[] selections)
160 Account.SelectiveFolders.Clear();
161 Account.SelectiveFolders.AddRange(selections);
162 Settings.Default.Save();
165 private string[] GetSelectedFolderNames()
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();
174 public void RejectChanges()