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;
49 using Pithos.Client.WPF.Utils;
51 using Pithos.Interfaces;
54 namespace Pithos.Client.WPF.SelectiveSynch
56 class SelectiveSynchViewModel:Screen
58 private readonly IEventAggregator _events ;
61 public AccountSettings Account { get; set; }
63 private readonly ObservableCollection<DirectoryRecord> _rootNodes=new ObservableCollection<DirectoryRecord>();
64 public ObservableCollection<DirectoryRecord> RootNodes
66 get { return _rootNodes; }
69 private ObservableCollection<ObjectInfo> _checks;
70 //private readonly PithosMonitor _monitor;
71 private bool _isBusy=true;
72 private string _apiKey;
74 public ObservableCollection<ObjectInfo> Checks
76 get { return _checks; }
79 public void GetChecks()
81 var root = RootNodes[0];
82 _checks = new ObservableCollection<ObjectInfo>(
83 from DirectoryRecord record in root
84 where record.IsChecked==true
85 select record.ObjectInfo);
86 NotifyOfPropertyChange(() => Checks);
89 public SelectiveSynchViewModel(/*PithosMonitor monitor,*/ IEventAggregator events, AccountSettings account, string apiKey)
92 AccountName = account.AccountName;
93 DisplayName = String.Format("Selective folder synchronization for {0}",account.AccountName);
97 TaskEx.Run(LoadRootNode);
100 private void LoadRootNode()
103 var client = new CloudFilesClient(AccountName,_apiKey){AuthenticationUrl=Account.ServerUrl,UsePithos=true};
104 client.Authenticate();
107 var dirs = from container in client.ListContainers(AccountName)
108 select new DirectoryRecord
110 DisplayName = container.Name,
111 Uri=new Uri(client.StorageUrl,String.Format(@"{0}/{1}",Account.AccountName, container.Name)),
112 Directories = (from dir in client.ListObjects(AccountName, container.Name)
115 var ownFolders = dirs.ToList();
117 var accountNodes=from account in client.ListSharingAccounts()
118 select new DirectoryRecord
120 DisplayName=account.name,
121 Uri=new Uri(client.StorageUrl,"../"+ account.name),
122 Directories=(from container in client.ListContainers(account.name)
123 select new DirectoryRecord
125 DisplayName=container.Name,
126 Uri = new Uri(client.StorageUrl, "../" + account.name + "/" + container.Name),
127 Directories=(from folder in client.ListObjects(account.name,container.Name)
128 select folder).ToTree()
132 var othersNode = new DirectoryRecord
134 DisplayName = "Shared to me",
135 Directories=accountNodes.ToList()
139 var rootItem = new DirectoryRecord
141 DisplayName = AccountName ,
142 Directories = ownFolders.ToList()
145 Execute.OnUIThread(() =>
147 RootNodes.Add(rootItem);
148 RootNodes.Add(othersNode);
151 SetInitialSelections(Account);
163 NotifyOfPropertyChange(()=>IsBusy);
167 private void SetInitialSelections(AccountSettings account)
169 var selections = account.SelectiveFolders;
173 //Initially, all nodes are checked
174 //We need to *uncheck* the nodes that are not selected
176 var allNodes = (from DirectoryRecord rootRecord in RootNodes
177 from DirectoryRecord record in rootRecord
178 select record).ToList();
180 allNodes.Apply(record => record.IsChecked = false);
182 if (selections.Count == 0)
184 // allNodes.Apply(record => record.IsChecked = false);
188 var selects = (from DirectoryRecord rootRecord in RootNodes
189 from DirectoryRecord record in rootRecord
190 where record.Uri !=null && selections.Contains(record.Uri.ToString())
191 select record).ToList();
192 //var shouldBeChecked = allNodes.Except(selects).ToList();
194 selects.Apply(record=>record.IsExplicitlyChecked=true);
196 //shouldBeChecked.Apply(record => record.IsChecked = true);
202 protected string AccountName { get; set; }
204 public void SaveChanges()
206 var uris = (from DirectoryRecord root in RootNodes
207 from DirectoryRecord record in root
208 where record.IsChecked == true && record.Uri != null
209 select record.Uri).ToArray();
213 //RootNodes is an ObservableCollection, it can't be enumerated iterativelly
215 var added = (from DirectoryRecord root in RootNodes
216 from DirectoryRecord record in root
217 where record.Added && record.Uri != null
218 select record.Uri).ToArray();
219 var removed = (from DirectoryRecord root in RootNodes
220 from DirectoryRecord record in root
221 where record.Removed && record.Uri != null
222 select record.Uri).ToArray();
223 //TODO: Include Uris for the containers as well
224 _events.Publish(new SelectiveSynchChanges{Account=Account,Uris=uris,Added=added,Removed=removed});
233 private void SaveSettings(IEnumerable<Uri> uris)
235 var selections = uris.Select(uri => uri.ToString()).ToArray();
237 Account.SelectiveFolders.Clear();
238 Account.SelectiveFolders.AddRange(selections);
239 Settings.Default.Save();
242 public void RejectChanges()