2 /* -----------------------------------------------------------------------
3 * <copyright file="FilePropertiesViewModel.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 * -----------------------------------------------------------------------
42 using System.Collections;
43 using System.Collections.Concurrent;
44 using System.Collections.ObjectModel;
45 using System.Collections.Specialized;
46 using System.ComponentModel.Composition;
47 using System.Diagnostics;
48 using System.Diagnostics.Contracts;
52 using System.Threading.Tasks;
54 using System.Windows.Interop;
55 using System.Windows.Media.Imaging;
57 using Pithos.Client.WPF.FileProperties;
58 using Pithos.Client.WPF.Properties;
60 using Pithos.Interfaces;
63 namespace Pithos.Client.WPF
66 using System.Collections.Generic;
71 /// TODO: Update summary.
73 [Export(typeof(FilePropertiesViewModel))]
74 public class FilePropertiesViewModel : Screen
76 private string _title;
79 get { return _title; }
83 NotifyOfPropertyChange(()=>Title);
88 private bool _isPublic;
91 get { return _isPublic; }
95 NotifyOfPropertyChange(()=>IsPublic);
99 private string _contentDisposition;
100 public string ContentDisposition
102 get { return _contentDisposition; }
105 _contentDisposition = value;
106 NotifyOfPropertyChange(() => ContentDisposition);
110 private string _contentEncoding;
111 public string ContentEncoding
113 get { return _contentEncoding; }
116 _contentEncoding = value;
117 NotifyOfPropertyChange(() => ContentEncoding);
122 private string _manifest;
123 public string Manifest
125 get { return _manifest; }
129 NotifyOfPropertyChange(() => Manifest);
133 private string _kind;
136 get { return _kind; }
140 NotifyOfPropertyChange(() => Kind);
144 private string _size;
147 get { return _size; }
151 NotifyOfPropertyChange(() => Size);
155 private string _shortSize;
156 public string ShortSize
158 get { return _shortSize; }
162 NotifyOfPropertyChange(() => ShortSize);
166 private string _where;
169 get { return _where; }
173 NotifyOfPropertyChange(() => Where);
177 private DateTime _modified;
178 public DateTime Modified
180 get { return _modified; }
184 NotifyOfPropertyChange(() => Modified);
188 private string _modifiedBy;
189 public string ModifiedBy
191 get { return _modifiedBy; }
195 NotifyOfPropertyChange(() => ModifiedBy);
199 private long _version;
202 get { return _version; }
206 NotifyOfPropertyChange(() => Version);
210 private string _localFileName;
211 protected string LocalFileName
213 get { return _localFileName; }
216 _localFileName = value;
217 NotifyOfPropertyChange(() => LocalFileName);
221 private BitmapSource _fileIcon;
222 public BitmapSource FileIcon
224 get { return _fileIcon; }
228 NotifyOfPropertyChange(() => FileIcon);
232 private string _publicUrl;
233 public string PublicUrl
235 get { return _publicUrl; }
239 NotifyOfPropertyChange(() => PublicUrl);
243 private string _fileName;
244 public string FileName
246 get { return _fileName; }
250 NotifyOfPropertyChange(() => FileName);
254 private string _container;
255 public string Container
257 get { return _container; }
261 NotifyOfPropertyChange(() => Container);
265 private string _synchStatus;
266 public string SynchStatus
268 get { return _synchStatus; }
271 _synchStatus = value;
272 NotifyOfPropertyChange(()=>SynchStatus);
276 private string _permissionName;
277 public string PermissionName
279 get { return _permissionName; }
282 _permissionName = value;
283 NotifyOfPropertyChange(()=>PermissionName);
284 NotifyOfPropertyChange(() => CanAddPermission);
288 private Permission _currentPermission;
289 public Permission CurrentPermission
291 get { return _currentPermission; }
294 _currentPermission = value;
295 PermissionName = CurrentPermission.UserName;
296 PermissionRead = CurrentPermission.Read;
298 NotifyOfPropertyChange(()=>CurrentPermission);
299 NotifyOfPropertyChange(() => PermissionName);
304 private bool _permissionRead=true;
305 public bool PermissionRead
307 get { return _permissionRead; }
310 _permissionRead = value;
311 NotifyOfPropertyChange(()=>PermissionRead);
312 NotifyOfPropertyChange(() => PermissionWrite);
316 public bool PermissionWrite
320 return !PermissionRead;
324 _permissionRead = !value;
325 NotifyOfPropertyChange(() => PermissionRead);
326 NotifyOfPropertyChange(()=>PermissionWrite);
330 public bool CanAddPermission
332 get { return !String.IsNullOrWhiteSpace(PermissionName); }
335 public void AddPermission()
337 Permissions.Add(new Permission{Read=PermissionRead,UserName=PermissionName,Write=!PermissionRead});
340 public bool CanAddTag
342 get { return CurrentTag!=null && !String.IsNullOrWhiteSpace(CurrentTag.Name); }
347 Tags.Add(CurrentTag);
351 public bool TagsChanged { get; private set; }
352 public bool PermissionsChanged { get; private set; }
354 private bool _isBusy = true;
357 get { return _isBusy; }
361 NotifyOfPropertyChange(() => IsBusy);
366 public FilePropertiesViewModel(ShellViewModel shell,Task<ObjectInfo> pithosFile,string localFileName)
369 throw new ArgumentNullException("shell");
370 if (pithosFile==null)
371 throw new ArgumentNullException("pithosFile");
372 if (String.IsNullOrWhiteSpace(localFileName))
373 throw new ArgumentNullException("localFileName");
374 Contract.EndContractBlock();
377 _tags = new ObservableCollection<MetaValue>();
378 _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
379 _permissions = new ObservableCollection<Permission>();
380 _permissions.CollectionChanged += (sender, evt) => { PermissionsChanged = true; };
383 LocalFileName = localFileName;
384 pithosFile.ContinueWith(t =>
389 Execute.OnUIThread(()=>ShowError(t.Exception));
394 Execute.OnUIThread(() => PithosFile = t.Result);
399 private void ShowError(AggregateException exception)
401 MessageView view = null;
402 if (exception.InnerException is RetryException)
403 view = new MessageView(exception.InnerException as RetryException);
404 else if (exception.InnerException is WebException)
405 view = new MessageView(exception.InnerException as WebException);
407 view = new MessageView(exception.InnerException);
412 protected ShellViewModel Shell { get; set; }
414 private ObjectInfo _pithosFile;
415 public ObjectInfo PithosFile
417 get { return _pithosFile; }
425 var perms=from permission in value.Permissions
426 select new Permission(permission.Key, permission.Value);
427 perms.Apply(perm=>Permissions.Add(perm));
429 var tags=from tag in value.Tags
430 select new MetaValue(tag.Key, tag.Value);
431 tags.Apply(tag=>Tags.Add(tag));
433 Title = String.Format("{0} Properties", value.Name);
434 Kind=value.Content_Type;
435 ShortSize = value.Bytes.ToByteSize();
436 Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
437 Where = Uri.UnescapeDataString(value.Name);
438 FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
439 Container = value.Container;
440 Modified = value.Last_Modified;
441 ModifiedBy = value.ModifiedBy;
442 Version = value.Version??0;
444 ContentDisposition = value.ContendDisposition;
445 ContentEncoding = value.ContentEncoding;
446 Manifest = value.Manifest;
447 IsPublic = value.IsPublic;
449 PublicUrl = String.Format("{0}/v1{1}", Shell.Accounts.First(account=>account.UserName==PithosFile.Account).SiteUri,value.PublicUrl);
451 if (Directory.Exists(LocalFileName))
453 FileIcon= new BitmapImage(new Uri("../Images/Folder.ico",UriKind.Relative));
455 else if (File.Exists(LocalFileName))
457 using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
459 FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
460 BitmapSizeOptions.FromEmptyOptions());
464 var status=Shell.GetFileStatus(LocalFileName);
465 SynchStatus = Enum.GetName(typeof (FileStatus), status);
468 NotifyOfPropertyChange(()=>PithosFile);
473 private MetaValue _currentTag;
474 public MetaValue CurrentTag
476 get { return _currentTag; }
480 NotifyOfPropertyChange(()=>CurrentTag);
484 private readonly ObservableCollection<MetaValue> _tags ;
485 public ObservableCollection<MetaValue> Tags
490 private readonly ObservableCollection<Permission> _permissions ;
493 public ObservableCollection<Permission> Permissions
495 get { return _permissions; }
500 PithosFile=Shell.RefreshObjectInfo(PithosFile);
503 public override void CanClose(Action<bool> callback)
505 base.CanClose(callback);
508 public void SaveChanges()
514 public void RejectChanges()
519 public void ApplyChanges()
524 private void DoSave()
528 PithosFile.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
531 if (PermissionsChanged)
533 PithosFile.Permissions = this.Permissions.ToDictionary(perm => perm.UserName, perm =>
534 (perm.Value.Trim()));
537 PithosFile.ContendDisposition = ContentDisposition;
538 PithosFile.ContentEncoding = ContentEncoding;
539 PithosFile.Manifest = Manifest;
540 PithosFile.IsPublic = IsPublic;
542 var monitor = Shell.Monitors[PithosFile.AccountKey];
543 monitor.CloudClient.UpdateMetadata(PithosFile);
547 PermissionsChanged = false;