First version of File Properties window. Works with random file
[pithos-ms-client] / trunk / Pithos.Client.WPF / FileProperties / FilePropertiesViewModel.cs
1 // -----------------------------------------------------------------------
2 // <copyright file="FilePropertiesViewModel.cs" company="Microsoft">
3 // TODO: Update copyright text.
4 // </copyright>
5 // -----------------------------------------------------------------------
6
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;
13 using System.Drawing;
14 using System.Windows;
15 using System.Windows.Interop;
16 using System.Windows.Media.Imaging;
17 using Caliburn.Micro;
18 using Pithos.Interfaces;
19
20 namespace Pithos.Client.WPF
21 {
22     using System;
23     using System.Collections.Generic;
24     using System.Linq;
25     using System.Text;
26
27     /// <summary>
28     /// TODO: Update summary.
29     /// </summary>
30     [Export(typeof(FilePropertiesViewModel))]
31     public class FilePropertiesViewModel : Screen, IShell
32     {
33         private string _title;
34         public string Title
35         {
36             get { return _title; }
37             set
38             {
39                 _title = value;
40                 NotifyOfPropertyChange(()=>Title);
41             }
42         }
43
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; }
53
54         public string FileName { get; set; }
55         public string Container { get; set; }
56
57         public FilePropertiesViewModel(ShellViewModel shell,ObjectInfo pithosFile,string localFileName)
58         {
59             if (shell==null)
60                 throw new ArgumentNullException("shell");
61             if (pithosFile==null)
62                 throw new ArgumentNullException("pithosFile");
63             if (String.IsNullOrWhiteSpace(localFileName))
64                 throw new ArgumentNullException("localFileName");
65             Contract.EndContractBlock();
66
67             Shell = shell;
68             LocalFileName = localFileName;
69             PithosFile = pithosFile;
70             Title = String.Format("{0} Properties", pithosFile.Name);
71         }
72
73
74         protected ShellViewModel Shell { get; set; }
75
76         private ObjectInfo _pithosFile;
77         public ObjectInfo PithosFile
78         {
79             get { return _pithosFile; }
80             set
81             {
82                 _pithosFile = value;
83                 
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;
96
97                 using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
98                 {
99                     FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
100                                                                    BitmapSizeOptions.FromEmptyOptions());
101                 }
102                 NotifyOfPropertyChange(()=>PithosFile);
103             }
104         }
105         
106
107
108         private ObservableConcurrentDictionary<string, string> _permissions;
109         public ObservableConcurrentDictionary<string, string> Permissions
110         {
111             get { return _permissions; }
112             set
113             {
114                 _permissions = value;
115             }
116         }
117
118
119         public void Refresh()
120         {
121             PithosFile=Shell.RefreshObjectInfo(PithosFile);
122         }
123
124         public override void CanClose(Action<bool> callback)
125         {
126             base.CanClose(callback);
127         }
128
129         public void SaveChanges()
130         {
131             DoSave();
132             TryClose();
133         }
134
135         public void RejectChanges()
136         {
137             TryClose();
138         }
139
140         public void ApplyChanges()
141         {
142             DoSave();
143         }
144
145         private void DoSave()
146         {
147             
148         }
149
150         static string[] sizeSuffixes = { 
151         "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
152
153         public string ByteSize(long size)
154         {
155             
156             const string formatTemplate = "{0}{1:0.#} {2}";
157
158             if (size == 0)
159             {
160                 return string.Format(formatTemplate, null, 0, sizeSuffixes[0]);
161             }
162
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
168                 : intPower;
169             var normSize = absSize / Math.Pow(1000, iUnit);
170
171             return string.Format(
172                 formatTemplate,
173                 size < 0 ? "-" : null, normSize, sizeSuffixes[iUnit]);
174         } 
175
176
177     }
178 }