Revision 42800be8 trunk/Pithos.Client.WPF/FileProperties/FilePropertiesViewModel.cs

b/trunk/Pithos.Client.WPF/FileProperties/FilePropertiesViewModel.cs
15 15
using System.Windows.Interop;
16 16
using System.Windows.Media.Imaging;
17 17
using Caliburn.Micro;
18
using Pithos.Client.WPF.FileProperties;
18 19
using Pithos.Interfaces;
19 20

  
20 21
namespace Pithos.Client.WPF
......
28 29
    /// TODO: Update summary.
29 30
    /// </summary>
30 31
    [Export(typeof(FilePropertiesViewModel))]
31
    public class FilePropertiesViewModel : Screen, IShell
32
    public class FilePropertiesViewModel : Screen
32 33
    {
33 34
        private string _title;
34 35
        public string Title
......
85 86
                    ((IDictionary<string,string>)Permissions).Clear();                
86 87
                value.Permissions.Apply(perm=>Permissions.Add(perm.Key,perm.Value));
87 88
                Kind=value.Content_Type;
88
                ShortSize = ByteSize(value.Bytes);
89
                ShortSize = value.Bytes.ToByteSize();
89 90
                Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
90 91
                Where = Uri.UnescapeDataString(value.Name);
91 92
                FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
......
147 148
            
148 149
        }
149 150

  
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 151

  
177 152
    }
178 153
}

Also available in: Unified diff