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;
56 using Pithos.Interfaces;
59 namespace Pithos.Client.WPF
62 using System.Collections.Generic;
67 /// TODO: Update summary.
69 [Export(typeof(FilePropertiesViewModel))]
70 public class FilePropertiesViewModel : Screen
72 private string _title;
75 get { return _title; }
79 NotifyOfPropertyChange(()=>Title);
84 private bool _isPublic;
87 get { return _isPublic; }
91 NotifyOfPropertyChange(()=>IsPublic);
95 private string _contentDisposition;
96 public string ContentDisposition
98 get { return _contentDisposition; }
101 _contentDisposition = value;
102 NotifyOfPropertyChange(() => ContentDisposition);
106 private string _contentEncoding;
107 public string ContentEncoding
109 get { return _contentEncoding; }
112 _contentEncoding = value;
113 NotifyOfPropertyChange(() => ContentEncoding);
118 private string _manifest;
119 public string Manifest
121 get { return _manifest; }
125 NotifyOfPropertyChange(() => Manifest);
129 private string _kind;
132 get { return _kind; }
136 NotifyOfPropertyChange(() => Kind);
140 private string _size;
143 get { return _size; }
147 NotifyOfPropertyChange(() => Size);
151 private string _shortSize;
152 public string ShortSize
154 get { return _shortSize; }
158 NotifyOfPropertyChange(() => ShortSize);
162 private string _where;
165 get { return _where; }
169 NotifyOfPropertyChange(() => Where);
173 private DateTime _modified;
174 public DateTime Modified
176 get { return _modified; }
180 NotifyOfPropertyChange(() => Modified);
184 private string _modifiedBy;
185 public string ModifiedBy
187 get { return _modifiedBy; }
191 NotifyOfPropertyChange(() => ModifiedBy);
195 private long _version;
198 get { return _version; }
202 NotifyOfPropertyChange(() => Version);
206 private string _localFileName;
207 protected string LocalFileName
209 get { return _localFileName; }
212 _localFileName = value;
213 NotifyOfPropertyChange(() => LocalFileName);
217 private BitmapSource _fileIcon;
218 public BitmapSource FileIcon
220 get { return _fileIcon; }
224 NotifyOfPropertyChange(() => FileIcon);
228 private string _publicUrl;
229 public string PublicUrl
231 get { return _publicUrl; }
235 NotifyOfPropertyChange(() => PublicUrl);
239 private string _fileName;
240 public string FileName
242 get { return _fileName; }
246 NotifyOfPropertyChange(() => FileName);
250 private string _container;
251 public string Container
253 get { return _container; }
257 NotifyOfPropertyChange(() => Container);
261 private string _synchStatus;
262 public string SynchStatus
264 get { return _synchStatus; }
267 _synchStatus = value;
268 NotifyOfPropertyChange(()=>SynchStatus);
272 public bool TagsChanged { get; private set; }
273 public bool PermissionsChanged { get; private set; }
275 private bool _isBusy = true;
278 get { return _isBusy; }
282 NotifyOfPropertyChange(() => IsBusy);
287 public FilePropertiesViewModel(ShellViewModel shell,Task<ObjectInfo> pithosFile,string localFileName)
290 throw new ArgumentNullException("shell");
291 if (pithosFile==null)
292 throw new ArgumentNullException("pithosFile");
293 if (String.IsNullOrWhiteSpace(localFileName))
294 throw new ArgumentNullException("localFileName");
295 Contract.EndContractBlock();
298 _tags = new ObservableCollection<MetaValue>();
299 _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
300 _permissions = new ObservableCollection<Permission>();
301 _permissions.CollectionChanged += (sender, evt) => { PermissionsChanged = true; };
304 LocalFileName = localFileName;
305 pithosFile.ContinueWith(t =>
310 Execute.OnUIThread(()=>ShowError(t.Exception));
315 Execute.OnUIThread(() => PithosFile = t.Result);
320 private void ShowError(AggregateException exception)
322 MessageView view = null;
323 if (exception.InnerException is RetryException)
324 view = new MessageView(exception.InnerException as RetryException);
325 else if (exception.InnerException is WebException)
326 view = new MessageView(exception.InnerException as WebException);
328 view = new MessageView(exception.InnerException);
333 protected ShellViewModel Shell { get; set; }
335 private ObjectInfo _pithosFile;
336 public ObjectInfo PithosFile
338 get { return _pithosFile; }
346 var perms=from permission in value.Permissions
347 select new Permission(permission.Key, permission.Value);
348 perms.Apply(perm=>Permissions.Add(perm));
350 var tags=from tag in value.Tags
351 select new MetaValue(tag.Key, tag.Value);
352 tags.Apply(tag=>Tags.Add(tag));
354 Title = String.Format("{0} Properties", value.Name);
355 Kind=value.Content_Type;
356 ShortSize = value.Bytes.ToByteSize();
357 Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
358 Where = Uri.UnescapeDataString(value.Name);
359 FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
360 Container = value.Container;
361 Modified = value.Last_Modified;
362 ModifiedBy = value.ModifiedBy;
363 Version = value.Version??0;
365 ContentDisposition = value.ContendDisposition;
366 ContentEncoding = value.ContentEncoding;
367 Manifest = value.Manifest;
368 IsPublic = value.IsPublic;
370 PublicUrl = String.Format("{0}/v1{1}", Shell.Accounts.First(account=>account.UserName==PithosFile.Account).SiteUri,value.PublicUrl);
372 if (Directory.Exists(LocalFileName))
374 FileIcon= new BitmapImage(new Uri("../Images/Folder.ico",UriKind.Relative));
376 else if (File.Exists(LocalFileName))
378 using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
380 FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
381 BitmapSizeOptions.FromEmptyOptions());
385 var status=Shell.GetFileStatus(LocalFileName);
386 SynchStatus = Enum.GetName(typeof (FileStatus), status);
389 NotifyOfPropertyChange(()=>PithosFile);
395 private readonly ObservableCollection<MetaValue> _tags ;
396 public ObservableCollection<MetaValue> Tags
401 private readonly ObservableCollection<Permission> _permissions ;
404 public ObservableCollection<Permission> Permissions
406 get { return _permissions; }
411 PithosFile=Shell.RefreshObjectInfo(PithosFile);
414 public override void CanClose(Action<bool> callback)
416 base.CanClose(callback);
419 public void SaveChanges()
425 public void RejectChanges()
430 public void ApplyChanges()
435 private void DoSave()
439 PithosFile.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
442 if (PermissionsChanged)
444 PithosFile.Permissions = this.Permissions.ToDictionary(perm => perm.UserName, perm => perm.Value);
447 PithosFile.ContendDisposition = ContentDisposition;
448 PithosFile.ContentEncoding = ContentEncoding;
449 PithosFile.Manifest = Manifest;
450 PithosFile.IsPublic = IsPublic;
452 var monitor = Shell.Monitors[PithosFile.Account];
453 monitor.CloudClient.UpdateMetadata(PithosFile);
457 PermissionsChanged = false;