1 // -----------------------------------------------------------------------
2 // <copyright file="FilePropertiesViewModel.cs" company="Microsoft">
3 // TODO: Update copyright text.
5 // -----------------------------------------------------------------------
7 using System.Collections;
8 using System.Collections.Concurrent;
9 using System.Collections.ObjectModel;
10 using System.Collections.Specialized;
11 using System.ComponentModel.Composition;
12 using System.Diagnostics;
13 using System.Diagnostics.Contracts;
16 using System.Threading.Tasks;
18 using System.Windows.Interop;
19 using System.Windows.Media.Imaging;
21 using Pithos.Client.WPF.FileProperties;
22 using Pithos.Client.WPF.Properties;
23 using Pithos.Interfaces;
26 namespace Pithos.Client.WPF
29 using System.Collections.Generic;
34 /// TODO: Update summary.
36 [Export(typeof(FilePropertiesViewModel))]
37 public class FilePropertiesViewModel : Screen
39 private string _title;
42 get { return _title; }
46 NotifyOfPropertyChange(()=>Title);
51 private bool _isPublic;
54 get { return _isPublic; }
58 NotifyOfPropertyChange(()=>IsPublic);
62 private string _contentDisposition;
63 public string ContentDisposition
65 get { return _contentDisposition; }
68 _contentDisposition = value;
69 NotifyOfPropertyChange(() => ContentDisposition);
73 private string _contentEncoding;
74 public string ContentEncoding
76 get { return _contentEncoding; }
79 _contentEncoding = value;
80 NotifyOfPropertyChange(() => ContentEncoding);
85 private string _manifest;
86 public string Manifest
88 get { return _manifest; }
92 NotifyOfPropertyChange(() => Manifest);
103 NotifyOfPropertyChange(() => Kind);
107 private string _size;
110 get { return _size; }
114 NotifyOfPropertyChange(() => Size);
118 private string _shortSize;
119 public string ShortSize
121 get { return _shortSize; }
125 NotifyOfPropertyChange(() => ShortSize);
129 private string _where;
132 get { return _where; }
136 NotifyOfPropertyChange(() => Where);
140 private DateTime _modified;
141 public DateTime Modified
143 get { return _modified; }
147 NotifyOfPropertyChange(() => Modified);
151 private string _modifiedBy;
152 public string ModifiedBy
154 get { return _modifiedBy; }
158 NotifyOfPropertyChange(() => ModifiedBy);
162 private long _version;
165 get { return _version; }
169 NotifyOfPropertyChange(() => Version);
173 private string _localFileName;
174 protected string LocalFileName
176 get { return _localFileName; }
179 _localFileName = value;
180 NotifyOfPropertyChange(() => LocalFileName);
184 private BitmapSource _fileIcon;
185 public BitmapSource FileIcon
187 get { return _fileIcon; }
191 NotifyOfPropertyChange(() => FileIcon);
195 private string _publicUrl;
196 public string PublicUrl
198 get { return _publicUrl; }
202 NotifyOfPropertyChange(() => PublicUrl);
206 private string _fileName;
207 public string FileName
209 get { return _fileName; }
213 NotifyOfPropertyChange(() => FileName);
217 private string _container;
218 public string Container
220 get { return _container; }
224 NotifyOfPropertyChange(() => Container);
228 public bool TagsChanged { get; private set; }
229 public bool PermissionsChanged { get; private set; }
231 private bool _isBusy = true;
234 get { return _isBusy; }
238 NotifyOfPropertyChange(() => IsBusy);
243 public FilePropertiesViewModel(ShellViewModel shell,Task<ObjectInfo> pithosFile,string localFileName)
246 throw new ArgumentNullException("shell");
247 if (pithosFile==null)
248 throw new ArgumentNullException("pithosFile");
249 if (String.IsNullOrWhiteSpace(localFileName))
250 throw new ArgumentNullException("localFileName");
251 Contract.EndContractBlock();
254 _tags = new ObservableCollection<MetaValue>();
255 _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
256 _permissions = new ObservableCollection<Permission>();
257 _permissions.CollectionChanged += (sender, evt) => { PermissionsChanged = true; };
260 LocalFileName = localFileName;
261 pithosFile.ContinueWith(t =>
266 Execute.OnUIThread(()=>ShowError(t.Exception));
271 Execute.OnUIThread(() => PithosFile = t.Result);
275 private void ShowError(AggregateException exception)
277 MessageView view = null;
278 if (exception.InnerException is RetryException)
279 view = new MessageView(exception.InnerException as RetryException);
280 else if (exception.InnerException is WebException)
281 view = new MessageView(exception.InnerException as WebException);
283 view = new MessageView(exception.InnerException);
288 protected ShellViewModel Shell { get; set; }
290 private ObjectInfo _pithosFile;
291 public ObjectInfo PithosFile
293 get { return _pithosFile; }
301 var perms=from permission in value.Permissions
302 select new Permission(permission.Key, permission.Value);
303 perms.Apply(perm=>Permissions.Add(perm));
305 var tags=from tag in value.Tags
306 select new MetaValue(tag.Key, tag.Value);
307 tags.Apply(tag=>Tags.Add(tag));
309 Title = String.Format("{0} Properties", value.Name);
310 Kind=value.Content_Type;
311 ShortSize = value.Bytes.ToByteSize();
312 Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
313 Where = Uri.UnescapeDataString(value.Name);
314 FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
315 Container = value.Container;
316 Modified = value.Last_Modified;
317 ModifiedBy = value.ModifiedBy;
318 Version = value.Version??0;
320 ContentDisposition = value.ContendDisposition;
321 ContentEncoding = value.ContentEncoding;
322 Manifest = value.Manifest;
323 IsPublic = value.IsPublic;
325 PublicUrl = String.Format("{0}/v1{1}", Settings.Default.PithosSite ,value.PublicUrl);
327 using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
329 FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
330 BitmapSizeOptions.FromEmptyOptions());
332 NotifyOfPropertyChange(()=>PithosFile);
338 private readonly ObservableCollection<MetaValue> _tags ;
339 public ObservableCollection<MetaValue> Tags
344 private readonly ObservableCollection<Permission> _permissions ;
347 public ObservableCollection<Permission> Permissions
349 get { return _permissions; }
354 PithosFile=Shell.RefreshObjectInfo(PithosFile);
357 public override void CanClose(Action<bool> callback)
359 base.CanClose(callback);
362 public void SaveChanges()
368 public void RejectChanges()
373 public void ApplyChanges()
378 private void DoSave()
382 PithosFile.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
385 if (PermissionsChanged)
387 PithosFile.Permissions = this.Permissions.ToDictionary(perm => perm.UserName, perm => perm.Value);
390 PithosFile.ContendDisposition = ContentDisposition;
391 PithosFile.ContentEncoding = ContentEncoding;
392 PithosFile.Manifest = Manifest;
393 PithosFile.IsPublic = IsPublic;
395 var monitor = Shell.Monitors[PithosFile.Account];
396 monitor.CloudClient.UpdateMetadata(PithosFile);
400 PermissionsChanged = false;