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.ComponentModel.Composition;
11 using System.Diagnostics;
12 using System.Diagnostics.Contracts;
15 using System.Windows.Interop;
16 using System.Windows.Media.Imaging;
18 using Pithos.Interfaces;
20 namespace Pithos.Client.WPF
23 using System.Collections.Generic;
28 /// TODO: Update summary.
30 [Export(typeof(FilePropertiesViewModel))]
31 public class FilePropertiesViewModel : Screen, IShell
33 private string _title;
36 get { return _title; }
40 NotifyOfPropertyChange(()=>Title);
44 public string Kind { get; set; }
45 public string Size { get; set; }
46 public string ShortSize { get; set; }
47 public string Where { get; set; }
48 public DateTime Modified { get; set; }
49 public string ModifiedBy { get; set; }
50 public long Version { get; set; }
51 protected string LocalFileName { get; set; }
52 public BitmapSource FileIcon { get; set; }
54 public string FileName { get; set; }
55 public string Container { get; set; }
57 public FilePropertiesViewModel(ShellViewModel shell,ObjectInfo pithosFile,string localFileName)
60 throw new ArgumentNullException("shell");
62 throw new ArgumentNullException("pithosFile");
63 if (String.IsNullOrWhiteSpace(localFileName))
64 throw new ArgumentNullException("localFileName");
65 Contract.EndContractBlock();
68 LocalFileName = localFileName;
69 PithosFile = pithosFile;
70 Title = String.Format("{0} Properties", pithosFile.Name);
74 protected ShellViewModel Shell { get; set; }
76 private ObjectInfo _pithosFile;
77 public ObjectInfo PithosFile
79 get { return _pithosFile; }
84 if (Permissions!=null)
85 ((IDictionary<string,string>)Permissions).Clear();
86 value.Permissions.Apply(perm=>Permissions.Add(perm.Key,perm.Value));
87 Kind=value.Content_Type;
88 ShortSize = ByteSize(value.Bytes);
89 Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
90 Where = Uri.UnescapeDataString(value.Name);
91 FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
92 Container = value.Container;
93 Modified = value.Last_Modified;
94 ModifiedBy = value.ModifiedBy;
95 Version = value.Version??0;
97 using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
99 FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
100 BitmapSizeOptions.FromEmptyOptions());
102 NotifyOfPropertyChange(()=>PithosFile);
108 private ObservableConcurrentDictionary<string, string> _permissions;
109 public ObservableConcurrentDictionary<string, string> Permissions
111 get { return _permissions; }
114 _permissions = value;
119 public void Refresh()
121 PithosFile=Shell.RefreshObjectInfo(PithosFile);
124 public override void CanClose(Action<bool> callback)
126 base.CanClose(callback);
129 public void SaveChanges()
135 public void RejectChanges()
140 public void ApplyChanges()
145 private void DoSave()
150 static string[] sizeSuffixes = {
151 "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
153 public string ByteSize(long size)
156 const string formatTemplate = "{0}{1:0.#} {2}";
160 return string.Format(formatTemplate, null, 0, sizeSuffixes[0]);
163 var absSize = Math.Abs((double)size);
164 var fpPower = Math.Log(absSize, 1000);
165 var intPower = (int)fpPower;
166 var iUnit = intPower >= sizeSuffixes.Length
167 ? sizeSuffixes.Length - 1
169 var normSize = absSize / Math.Pow(1000, iUnit);
171 return string.Format(
173 size < 0 ? "-" : null, normSize, sizeSuffixes[iUnit]);