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;
49 using System.Threading.Tasks;
51 using Pithos.Client.WPF.Properties;
53 using Pithos.Core.Agents;
54 using Pithos.Interfaces;
57 namespace Pithos.Client.WPF.SelectiveSynch
59 class SelectiveSynchViewModel:Screen
61 private const string DirectoryType = "application/directory";
62 private IEventAggregator _events ;
64 private string _title;
67 get { return _title; }
71 NotifyOfPropertyChange(() => Title);
75 public AccountSettings Account { get; set; }
77 private readonly ObservableCollection<DirectoryRecord> _rootNodes=new ObservableCollection<DirectoryRecord>();
78 public ObservableCollection<DirectoryRecord> RootNodes
80 get { return _rootNodes; }
83 private ObservableCollection<ObjectInfo> _checks;
84 private PithosMonitor _monitor;
85 private bool _isBusy=true;
87 public ObservableCollection<ObjectInfo> Checks
89 get { return _checks; }
92 public void GetChecks()
94 var root = RootNodes[0];
95 _checks = new ObservableCollection<ObjectInfo>(
97 where record.IsChecked==true
98 select record.ObjectInfo);
99 NotifyOfPropertyChange(() => Checks);
102 public SelectiveSynchViewModel(PithosMonitor monitor, IEventAggregator events, AccountSettings account)
105 AccountName = account.AccountName;
106 Title = account.AccountName;
109 TaskEx.Run(LoadRootNode);
112 private void LoadRootNode()
114 var client = _monitor.CloudClient;
116 var dirs = from container in client.ListContainers(_monitor.UserName)
117 select new DirectoryRecord
119 DisplayName = container.Name,
120 Uri=new Uri(client.StorageUrl,container.Name),
121 Directories = (from dir in client.ListObjects(_monitor.UserName, container.Name, "")
122 where dir.Content_Type == DirectoryType
123 select new DirectoryRecord { DisplayName = dir.Name, ObjectInfo = dir }).ToList()
125 var ownFolders = dirs.ToList();
127 var accounts = client.ListSharingAccounts();
129 var accountNodes=from account in client.ListSharingAccounts()
130 select new DirectoryRecord
132 DisplayName=account.name,
133 Directories=(from container in client.ListContainers(account.name)
134 select new DirectoryRecord
136 DisplayName=container.Name,
137 Directories=(from folder in client.ListObjects(account.name,container.Name,"")
138 where folder.Content_Type==DirectoryType
139 select new DirectoryRecord{DisplayName=folder.Name,ObjectInfo=folder}).ToList()
143 var othersNode = new DirectoryRecord
145 DisplayName = "Others",
146 Directories=accountNodes.ToList()
150 var rootItem = new DirectoryRecord
152 DisplayName = AccountName ,
153 Directories = ownFolders.ToList()
156 Execute.OnUIThread(() =>
158 this.RootNodes.Add(rootItem);
159 this.RootNodes.Add(othersNode);
162 SetInitialSelections(Account);
174 NotifyOfPropertyChange(()=>IsBusy);
178 private void SetInitialSelections(AccountSettings account)
180 var selections = account.SelectiveFolders;
182 if (selections.Count == 0)
184 //Initially, all nodes are checked
185 //We need to *uncheck* the nodes that are not selected
187 var selects = from rootRecord in RootNodes
188 from record in rootRecord
189 where record.Uri !=null && !selections.Contains(record.Uri.ToString())
192 selects.Apply(record=>record.IsChecked=false);
195 protected string AccountName { get; set; }
197 public void SaveChanges()
199 var uris = (from root in RootNodes
201 where record.IsChecked == true && record.Uri != null
202 select record.Uri).ToArray();
206 //RootNodes is an ObservableCollection, it can't be enumerated iterativelly
208 var added= (from root in RootNodes
210 where record.Added && record.Uri != null
211 select record.Uri).ToArray();
212 var removed = (from root in RootNodes
214 where record.Removed && record.Uri != null
215 select record.Uri).ToArray();
216 //TODO: Include Uris for the containers as well
217 _events.Publish(new SelectiveSynchChanges{Account=Account,Uris=uris,Added=added,Removed=removed});
226 private void SaveSettings(IEnumerable<Uri> uris)
228 var selections = uris.Select(uri => uri.ToString()).ToArray();
230 Account.SelectiveFolders.Clear();
231 Account.SelectiveFolders.AddRange(selections);
232 Settings.Default.Save();
235 public void RejectChanges()