1 // -----------------------------------------------------------------------
2 // <copyright file="FilePropertiesViewModel.cs" company="GRNet">
3 // Copyright 2011 GRNET S.A. All rights reserved.
5 // Redistribution and use in source and binary forms, with or
6 // without modification, are permitted provided that the following
9 // 1. Redistributions of source code must retain the above
10 // copyright notice, this list of conditions and the following
13 // 2. Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials
16 // provided with the distribution.
18 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
31 // The views and conclusions contained in the software and
32 // documentation are those of the authors and should not be
33 // interpreted as representing official policies, either expressed
34 // or implied, of GRNET S.A.
36 // -----------------------------------------------------------------------
38 using System.Collections;
39 using System.Collections.Concurrent;
40 using System.Collections.ObjectModel;
41 using System.Collections.Specialized;
42 using System.ComponentModel.Composition;
43 using System.Diagnostics;
44 using System.Diagnostics.Contracts;
48 using System.Threading.Tasks;
50 using System.Windows.Interop;
51 using System.Windows.Media.Imaging;
53 using Pithos.Client.WPF.FileProperties;
54 using Pithos.Client.WPF.Properties;
55 using Pithos.Interfaces;
58 namespace Pithos.Client.WPF
61 using System.Collections.Generic;
66 /// TODO: Update summary.
68 [Export(typeof(FilePropertiesViewModel))]
69 public class FilePropertiesViewModel : Screen
71 private string _title;
74 get { return _title; }
78 NotifyOfPropertyChange(()=>Title);
83 private bool _isPublic;
86 get { return _isPublic; }
90 NotifyOfPropertyChange(()=>IsPublic);
94 private string _contentDisposition;
95 public string ContentDisposition
97 get { return _contentDisposition; }
100 _contentDisposition = value;
101 NotifyOfPropertyChange(() => ContentDisposition);
105 private string _contentEncoding;
106 public string ContentEncoding
108 get { return _contentEncoding; }
111 _contentEncoding = value;
112 NotifyOfPropertyChange(() => ContentEncoding);
117 private string _manifest;
118 public string Manifest
120 get { return _manifest; }
124 NotifyOfPropertyChange(() => Manifest);
128 private string _kind;
131 get { return _kind; }
135 NotifyOfPropertyChange(() => Kind);
139 private string _size;
142 get { return _size; }
146 NotifyOfPropertyChange(() => Size);
150 private string _shortSize;
151 public string ShortSize
153 get { return _shortSize; }
157 NotifyOfPropertyChange(() => ShortSize);
161 private string _where;
164 get { return _where; }
168 NotifyOfPropertyChange(() => Where);
172 private DateTime _modified;
173 public DateTime Modified
175 get { return _modified; }
179 NotifyOfPropertyChange(() => Modified);
183 private string _modifiedBy;
184 public string ModifiedBy
186 get { return _modifiedBy; }
190 NotifyOfPropertyChange(() => ModifiedBy);
194 private long _version;
197 get { return _version; }
201 NotifyOfPropertyChange(() => Version);
205 private string _localFileName;
206 protected string LocalFileName
208 get { return _localFileName; }
211 _localFileName = value;
212 NotifyOfPropertyChange(() => LocalFileName);
216 private BitmapSource _fileIcon;
217 public BitmapSource FileIcon
219 get { return _fileIcon; }
223 NotifyOfPropertyChange(() => FileIcon);
227 private string _publicUrl;
228 public string PublicUrl
230 get { return _publicUrl; }
234 NotifyOfPropertyChange(() => PublicUrl);
238 private string _fileName;
239 public string FileName
241 get { return _fileName; }
245 NotifyOfPropertyChange(() => FileName);
249 private string _container;
250 public string Container
252 get { return _container; }
256 NotifyOfPropertyChange(() => Container);
260 public bool TagsChanged { get; private set; }
261 public bool PermissionsChanged { get; private set; }
263 private bool _isBusy = true;
266 get { return _isBusy; }
270 NotifyOfPropertyChange(() => IsBusy);
275 public FilePropertiesViewModel(ShellViewModel shell,Task<ObjectInfo> pithosFile,string localFileName)
278 throw new ArgumentNullException("shell");
279 if (pithosFile==null)
280 throw new ArgumentNullException("pithosFile");
281 if (String.IsNullOrWhiteSpace(localFileName))
282 throw new ArgumentNullException("localFileName");
283 Contract.EndContractBlock();
286 _tags = new ObservableCollection<MetaValue>();
287 _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
288 _permissions = new ObservableCollection<Permission>();
289 _permissions.CollectionChanged += (sender, evt) => { PermissionsChanged = true; };
292 LocalFileName = localFileName;
293 pithosFile.ContinueWith(t =>
298 Execute.OnUIThread(()=>ShowError(t.Exception));
303 Execute.OnUIThread(() => PithosFile = t.Result);
307 private void ShowError(AggregateException exception)
309 MessageView view = null;
310 if (exception.InnerException is RetryException)
311 view = new MessageView(exception.InnerException as RetryException);
312 else if (exception.InnerException is WebException)
313 view = new MessageView(exception.InnerException as WebException);
315 view = new MessageView(exception.InnerException);
320 protected ShellViewModel Shell { get; set; }
322 private ObjectInfo _pithosFile;
323 public ObjectInfo PithosFile
325 get { return _pithosFile; }
333 var perms=from permission in value.Permissions
334 select new Permission(permission.Key, permission.Value);
335 perms.Apply(perm=>Permissions.Add(perm));
337 var tags=from tag in value.Tags
338 select new MetaValue(tag.Key, tag.Value);
339 tags.Apply(tag=>Tags.Add(tag));
341 Title = String.Format("{0} Properties", value.Name);
342 Kind=value.Content_Type;
343 ShortSize = value.Bytes.ToByteSize();
344 Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
345 Where = Uri.UnescapeDataString(value.Name);
346 FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
347 Container = value.Container;
348 Modified = value.Last_Modified;
349 ModifiedBy = value.ModifiedBy;
350 Version = value.Version??0;
352 ContentDisposition = value.ContendDisposition;
353 ContentEncoding = value.ContentEncoding;
354 Manifest = value.Manifest;
355 IsPublic = value.IsPublic;
357 PublicUrl = String.Format("{0}/v1{1}", Shell.Accounts.First(account=>account.UserName==PithosFile.Account).SiteUri,value.PublicUrl);
359 if (Directory.Exists(LocalFileName))
361 FileIcon= new BitmapImage(new Uri("../Images/Folder.ico",UriKind.Relative));
363 else if (File.Exists(LocalFileName))
365 using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
367 FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
368 BitmapSizeOptions.FromEmptyOptions());
371 NotifyOfPropertyChange(()=>PithosFile);
377 private readonly ObservableCollection<MetaValue> _tags ;
378 public ObservableCollection<MetaValue> Tags
383 private readonly ObservableCollection<Permission> _permissions ;
386 public ObservableCollection<Permission> Permissions
388 get { return _permissions; }
393 PithosFile=Shell.RefreshObjectInfo(PithosFile);
396 public override void CanClose(Action<bool> callback)
398 base.CanClose(callback);
401 public void SaveChanges()
407 public void RejectChanges()
412 public void ApplyChanges()
417 private void DoSave()
421 PithosFile.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
424 if (PermissionsChanged)
426 PithosFile.Permissions = this.Permissions.ToDictionary(perm => perm.UserName, perm => perm.Value);
429 PithosFile.ContendDisposition = ContentDisposition;
430 PithosFile.ContentEncoding = ContentEncoding;
431 PithosFile.Manifest = Manifest;
432 PithosFile.IsPublic = IsPublic;
434 var monitor = Shell.Monitors[PithosFile.Account];
435 monitor.CloudClient.UpdateMetadata(PithosFile);
439 PermissionsChanged = false;