2 /* -----------------------------------------------------------------------
3 * <copyright file="ContainerPropertiesViewModel.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.Concurrent;
44 using System.Collections.Generic;
45 using System.Collections.ObjectModel;
46 using System.ComponentModel.Composition;
47 using System.Diagnostics.Contracts;
51 using System.Threading.Tasks;
56 namespace Pithos.Client.WPF.FileProperties
58 [Export(typeof(ContainerPropertiesViewModel))]
59 public class ContainerPropertiesViewModel:Screen
61 private string _containerName;
64 /// Name of the displayed container
66 public string ContainerName
68 get { return _containerName; }
71 _containerName = value;
72 NotifyOfPropertyChange(()=>ContainerName);
76 private string _title;
82 get { return _title; }
86 NotifyOfPropertyChange(() => Title);
93 /// Count of files in the container
97 get { return _count; }
101 NotifyOfPropertyChange(() => Count);
105 private DateTime _modified;
108 /// Date of last modification
110 public DateTime Modified
112 get { return _modified; }
116 NotifyOfPropertyChange(() => Modified);
120 private string _size;
123 /// Total size of the container in bytes
127 get { return _size; }
131 NotifyOfPropertyChange(() => Size);
135 private string _shortSize;
138 /// Total size of the container formatted in KB,MB etc
140 public string ShortSize
142 get { return _shortSize; }
146 NotifyOfPropertyChange(() => ShortSize);
150 private int _blockSize;
153 /// Block size used by the container
157 get { return _blockSize; }
161 NotifyOfPropertyChange(() => BlockSize);
165 private string _blockHash;
168 /// Hash algorithm used to calculate block hashes
170 public string BlockHash
172 get { return _blockHash; }
176 NotifyOfPropertyChange(() => BlockHash);
180 private ShellViewModel _shell;
183 /// Reference to the parent Shell
185 protected ShellViewModel Shell
187 get { return _shell; }
191 NotifyOfPropertyChange(() => Shell);
195 private ContainerInfo _container;
197 /// The displayed ContainerInfo
199 protected ContainerInfo Container
201 get { return _container; }
208 LoadFromDictionary(Tags, value.Tags);
210 //LoadFromDictionary(Policies, value.Policies);
212 if (value.Policies.ContainsKey("Versioning"))
215 if (Enum.TryParse(value.Policies["Versioning"], out version))
217 SelectedVersion = version;
221 if (value.Policies.ContainsKey("Quota"))
224 if (int.TryParse(value.Policies["Quota"], out quota))
230 Title = String.Format("{0} Properties", _container.Name);
232 ShortSize = value.Bytes.ToByteSize();
233 Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
234 Modified = value.Last_Modified;
235 BlockSize = value.BlockSize;
236 BlockHash = value.BlockHash;
237 ContainerName = Uri.UnescapeDataString(value.Name.Split('/').Last());
239 NotifyOfPropertyChange(() => Container);
247 get { return _quota; }
251 throw new ArgumentOutOfRangeException("Quota");
254 NotifyOfPropertyChange(() => Quota);
260 private Versioning[] _versions={Versioning.manual,Versioning.auto,Versioning.none};
261 public Versioning[] Versions
263 get { return _versions; }
267 private Versioning _selectedVersion;
268 public Versioning SelectedVersion
270 get { return _selectedVersion; }
272 _selectedVersion = value;
273 NotifyOfPropertyChange(()=>SelectedVersion);
277 private void LoadFromDictionary(ICollection<MetaValue> collection,Dictionary<string,string> source )
280 var items = from item in source
281 select new MetaValue(item.Key, item.Value);
283 items.Apply(collection.Add);
286 private readonly ObservableCollection<MetaValue> _tags;
287 public ObservableCollection<MetaValue> Tags
289 get { return _tags; }
292 /* private readonly ObservableCollection<MetaValue> _policies;
293 public ObservableCollection<MetaValue> Policies
295 get { return _policies; }
298 private bool _tagsChanged;
299 public bool TagsChanged
301 get { return _tagsChanged; }
304 _tagsChanged = value;
305 NotifyOfPropertyChange(()=>TagsChanged);
306 NotifyOfPropertyChange(() => CanApplyChanges);
310 //public bool PoliciesChanged { get; private set; }
312 private bool _isBusy=true;
315 get { return _isBusy; }
319 NotifyOfPropertyChange(()=>IsBusy);
323 public ContainerPropertiesViewModel(ShellViewModel shell, Task<ContainerInfo> container, string localFolderName)
326 throw new ArgumentNullException("shell");
328 throw new ArgumentNullException("container");
329 if (String.IsNullOrWhiteSpace(localFolderName))
330 throw new ArgumentNullException("localFolderName");
331 Contract.EndContractBlock();
333 _tags = new ObservableCollection<MetaValue>();
334 _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
336 /*_policies = new ObservableCollection<MetaValue>();
337 _policies.CollectionChanged += (sender, evt) => { PoliciesChanged = true; };
341 ContainerName = localFolderName;
342 container.ContinueWith(t =>{
346 Execute.OnUIThread(() => ShowError(t.Exception));
351 Execute.OnUIThread(()=>Container = t.Result);
355 private void ShowError(AggregateException exception)
357 MessageView view=null;
358 if (exception.InnerException is RetryException)
359 view=new MessageView(exception.InnerException as RetryException);
360 else if (exception.InnerException is WebException)
361 view = new MessageView(exception.InnerException as WebException);
363 view = new MessageView(exception.InnerException);
369 Container = Shell.RefreshContainerInfo(Container);
372 public override void CanClose(Action<bool> callback)
374 base.CanClose(callback);
377 public void SaveChanges()
383 public void RejectChanges()
388 public void ApplyChanges()
393 public bool CanApplyChanges
395 get { return TagsChanged; }
398 private void DoSave()
403 Container.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
404 var monitor = Shell.Monitors[Container.AccountKey];
405 monitor.CloudClient.UpdateMetadata(Container);
408 //Container.Policies["Quota"] = Quota.ToString();
409 //Container.Policies["Versioning"] = Enum.GetName(typeof (Versioning), SelectedVersion);
414 Container.Policies = this.Policies.ToDictionary(policy=> policy.Name, tag => tag.Value);