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;
46 using System.Threading.Tasks;
48 using Pithos.Client.WPF.Properties;
50 using Pithos.Interfaces;
52 namespace Pithos.Client.WPF.SelectiveSynch
54 class SelectiveSynchViewModel:Screen
56 private const string DirectoryType = "application/directory";
57 private readonly IEventAggregator _events ;
60 public AccountSettings Account { get; set; }
62 private readonly ObservableCollection<DirectoryRecord> _rootNodes=new ObservableCollection<DirectoryRecord>();
63 public ObservableCollection<DirectoryRecord> RootNodes
65 get { return _rootNodes; }
68 private ObservableCollection<ObjectInfo> _checks;
69 private readonly PithosMonitor _monitor;
70 private bool _isBusy=true;
72 public ObservableCollection<ObjectInfo> Checks
74 get { return _checks; }
77 public void GetChecks()
79 var root = RootNodes[0];
80 _checks = new ObservableCollection<ObjectInfo>(
82 where record.IsChecked==true
83 select record.ObjectInfo);
84 NotifyOfPropertyChange(() => Checks);
87 public SelectiveSynchViewModel(PithosMonitor monitor, IEventAggregator events, AccountSettings account)
90 AccountName = account.AccountName;
91 DisplayName = account.AccountName;
94 TaskEx.Run(LoadRootNode);
97 private void LoadRootNode()
99 var client = _monitor.CloudClient;
101 var dirs = from container in client.ListContainers(_monitor.UserName)
102 select new DirectoryRecord
104 DisplayName = container.Name,
105 Uri=new Uri(client.StorageUrl,container.Name),
106 Directories = (from dir in client.ListObjects(_monitor.UserName, container.Name, "")
107 where dir.Content_Type == DirectoryType
108 select new DirectoryRecord { DisplayName = dir.Name, ObjectInfo = dir }).ToList()
110 var ownFolders = dirs.ToList();
112 var accountNodes=from account in client.ListSharingAccounts()
113 select new DirectoryRecord
115 DisplayName=account.name,
116 Uri=new Uri(client.StorageUrl,"../"+ account.name),
117 Directories=(from container in client.ListContainers(account.name)
118 select new DirectoryRecord
120 DisplayName=container.Name,
121 Uri = new Uri(client.StorageUrl, "../" + account.name + "/" + container.Name),
122 Directories=(from folder in client.ListObjects(account.name,container.Name,"")
123 where folder.Content_Type==DirectoryType
124 select new DirectoryRecord{DisplayName=folder.Name,ObjectInfo=folder}).ToList()
128 var othersNode = new DirectoryRecord
130 DisplayName = "Others",
131 Directories=accountNodes.ToList()
135 var rootItem = new DirectoryRecord
137 DisplayName = AccountName ,
138 Directories = ownFolders.ToList()
141 Execute.OnUIThread(() =>
143 RootNodes.Add(rootItem);
144 RootNodes.Add(othersNode);
147 SetInitialSelections(Account);
159 NotifyOfPropertyChange(()=>IsBusy);
163 private void SetInitialSelections(AccountSettings account)
165 var selections = account.SelectiveFolders;
167 if (selections.Count == 0)
169 //Initially, all nodes are checked
170 //We need to *uncheck* the nodes that are not selected
172 var selects = from rootRecord in RootNodes
173 from record in rootRecord
174 where record.Uri !=null && !selections.Contains(record.Uri.ToString())
177 selects.Apply(record=>record.IsChecked=false);
180 protected string AccountName { get; set; }
182 public void SaveChanges()
184 var uris = (from root in RootNodes
186 where record.IsChecked == true && record.Uri != null
187 select record.Uri).ToArray();
191 //RootNodes is an ObservableCollection, it can't be enumerated iterativelly
193 var added= (from root in RootNodes
195 where record.Added && record.Uri != null
196 select record.Uri).ToArray();
197 var removed = (from root in RootNodes
199 where record.Removed && record.Uri != null
200 select record.Uri).ToArray();
201 //TODO: Include Uris for the containers as well
202 _events.Publish(new SelectiveSynchChanges{Account=Account,Uris=uris,Added=added,Removed=removed});
211 private void SaveSettings(IEnumerable<Uri> uris)
213 var selections = uris.Select(uri => uri.ToString()).ToArray();
215 Account.SelectiveFolders.Clear();
216 Account.SelectiveFolders.AddRange(selections);
217 Settings.Default.Save();
220 public void RejectChanges()