Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / FileProperties / FilePropertiesViewModel.cs @ aba9e6d9

History | View | Annotate | Download (7.5 kB)

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.Collections.Specialized;
11
using System.ComponentModel.Composition;
12
using System.Diagnostics;
13
using System.Diagnostics.Contracts;
14
using System.Drawing;
15
using System.Windows;
16
using System.Windows.Interop;
17
using System.Windows.Media.Imaging;
18
using Caliburn.Micro;
19
using Pithos.Client.WPF.FileProperties;
20
using Pithos.Client.WPF.Properties;
21
using Pithos.Interfaces;
22
using Pithos.Network;
23

    
24
namespace Pithos.Client.WPF
25
{
26
    using System;
27
    using System.Collections.Generic;
28
    using System.Linq;
29
    using System.Text;
30

    
31
    /// <summary>
32
    /// TODO: Update summary.
33
    /// </summary>
34
    [Export(typeof(FilePropertiesViewModel))]
35
    public class FilePropertiesViewModel : Screen
36
    {
37
        private string _title;
38
        public string Title
39
        {
40
            get { return _title; }
41
            set
42
            {
43
                _title = value;
44
                NotifyOfPropertyChange(()=>Title);
45
            }
46
        }
47

    
48

    
49
        private bool _isPublic;
50
        public bool IsPublic
51
        {
52
            get { return _isPublic; }
53
            set
54
            {
55
                _isPublic = value;
56
                NotifyOfPropertyChange(()=>IsPublic);
57
            }
58
        }
59

    
60
        private string _contentDisposition;
61
        public string ContentDisposition
62
        {
63
            get { return _contentDisposition; }
64
            set
65
            {
66
                _contentDisposition = value;
67
                NotifyOfPropertyChange(() => ContentDisposition);
68
            }
69
        }
70

    
71
        private string _contentEncoding;
72
        public string ContentEncoding
73
        {
74
            get { return _contentEncoding; }
75
            set
76
            {
77
                _contentEncoding = value;
78
                NotifyOfPropertyChange(() => ContentEncoding);
79
            }
80
        }
81

    
82

    
83
        private string _manifest;
84
        public string Manifest
85
        {
86
            get { return _manifest; }
87
            set
88
            {
89
                _manifest = value;
90
                NotifyOfPropertyChange(() => Manifest);
91
            }
92
        }
93

    
94
        public string Kind { get; set; }
95
        public string Size { get; set; }
96
        public string ShortSize { get; set; }
97
        public string Where { get; set; }
98
        public DateTime Modified { get; set; }
99
        public string ModifiedBy { get; set; }
100
        public long Version { get; set; }
101
        protected string LocalFileName { get; set; }
102
        public BitmapSource FileIcon { get; set; }
103
        public string PublicUrl { get; set; }
104

    
105
        public string FileName { get; set; }
106
        public string Container { get; set; }
107

    
108
        public bool TagsChanged { get; private set; }
109
        public bool PermissionsChanged { get; private set; }
110

    
111
        public FilePropertiesViewModel(ShellViewModel shell,ObjectInfo pithosFile,string localFileName)
112
        {
113
            if (shell==null)
114
                throw new ArgumentNullException("shell");
115
            if (pithosFile==null)
116
                throw new ArgumentNullException("pithosFile");
117
            if (String.IsNullOrWhiteSpace(localFileName))
118
                throw new ArgumentNullException("localFileName");
119
            Contract.EndContractBlock();
120

    
121

    
122
            _tags = new ObservableCollection<Tag>();
123
            _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
124
            _permissions = new ObservableCollection<Permission>();
125
            _permissions.CollectionChanged += (sender, evt) => { PermissionsChanged = true; };
126
            
127
            Shell = shell;
128
            LocalFileName = localFileName;
129
            PithosFile = pithosFile;
130
            Title = String.Format("{0} Properties", pithosFile.Name);
131
        }
132

    
133
        
134

    
135
        protected ShellViewModel Shell { get; set; }
136

    
137
        private ObjectInfo _pithosFile;
138
        public ObjectInfo PithosFile
139
        {
140
            get { return _pithosFile; }
141
            set
142
            {
143
                _pithosFile = value;
144
                
145
                Permissions.Clear();
146
                Tags.Clear();
147

    
148
                var perms=from permission in value.Permissions
149
                            select new Permission(permission.Key, permission.Value);
150
                perms.Apply(perm=>Permissions.Add(perm));
151
                
152
                var tags=from tag in value.Tags
153
                             select new Tag(tag.Key, tag.Value);
154
                tags.Apply(tag=>Tags.Add(tag));                                            
155

    
156
                Kind=value.Content_Type;
157
                ShortSize = value.Bytes.ToByteSize();
158
                Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
159
                Where = Uri.UnescapeDataString(value.Name);
160
                FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
161
                Container = value.Container;
162
                Modified = value.Last_Modified;
163
                ModifiedBy = value.ModifiedBy;
164
                Version = value.Version??0;
165

    
166
                ContentDisposition = value.ContendDisposition;
167
                ContentEncoding = value.ContentEncoding;
168
                Manifest = value.Manifest;
169
                IsPublic = value.IsPublic;
170
                
171
                PublicUrl = String.Format("{0}/v1{1}", Settings.Default.PithosSite ,value.PublicUrl);
172

    
173
                using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
174
                {
175
                    FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
176
                                                                   BitmapSizeOptions.FromEmptyOptions());
177
                }
178
                NotifyOfPropertyChange(()=>PithosFile);
179
            }
180
        }
181

    
182

    
183
        private readonly ObservableCollection<Tag> _tags ;
184
        public ObservableCollection<Tag> Tags
185
        {
186
            get { return _tags;}
187
        }
188

    
189
        private readonly ObservableCollection<Permission> _permissions ;
190
        
191

    
192
        public ObservableCollection<Permission> Permissions
193
        {
194
            get { return _permissions; }
195
        }
196

    
197
        public void Reload()
198
        {
199
            PithosFile=Shell.RefreshObjectInfo(PithosFile);
200
        }
201

    
202
        public override void CanClose(Action<bool> callback)
203
        {
204
            base.CanClose(callback);
205
        }
206

    
207
        public void SaveChanges()
208
        {
209
            DoSave();
210
            TryClose();
211
        }
212

    
213
        public void RejectChanges()
214
        {
215
            TryClose();
216
        }
217

    
218
        public void ApplyChanges()
219
        {
220
            DoSave();
221
        }
222

    
223
        private void DoSave()
224
        {
225
            if (TagsChanged)
226
            {
227
                PithosFile.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
228
            }
229
            
230
            if (PermissionsChanged)
231
            {
232
                PithosFile.Permissions = this.Permissions.ToDictionary(perm => perm.UserName, perm => perm.Value);
233
            }
234

    
235
            PithosFile.ContendDisposition = ContentDisposition;
236
            PithosFile.ContentEncoding = ContentEncoding;
237
            PithosFile.Manifest = Manifest;
238
            PithosFile.IsPublic = IsPublic;
239

    
240
            var monitor = Shell.Monitors[PithosFile.Account];
241
            monitor.CloudClient.UpdateMetadata(PithosFile);
242

    
243

    
244
            TagsChanged = false;
245
            PermissionsChanged = false;
246
        }
247

    
248

    
249
    }
250
}