Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (11.3 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.Net;
16
using System.Threading.Tasks;
17
using System.Windows;
18
using System.Windows.Interop;
19
using System.Windows.Media.Imaging;
20
using Caliburn.Micro;
21
using Pithos.Client.WPF.FileProperties;
22
using Pithos.Client.WPF.Properties;
23
using Pithos.Interfaces;
24
using Pithos.Network;
25

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

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

    
50

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

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

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

    
84

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

    
96
        private string _kind;
97
        public string Kind
98
        {
99
            get { return _kind; }
100
            set
101
            {
102
                _kind = value;
103
                NotifyOfPropertyChange(() => Kind);
104
            }
105
        }
106

    
107
        private string _size;
108
        public string Size
109
        {
110
            get { return _size; }
111
            set
112
            {
113
                _size = value;
114
                NotifyOfPropertyChange(() => Size);
115
            }
116
        }
117

    
118
        private string _shortSize;
119
        public string ShortSize
120
        {
121
            get { return _shortSize; }
122
            set
123
            {
124
                _shortSize = value;
125
                NotifyOfPropertyChange(() => ShortSize);
126
            }
127
        }
128

    
129
        private string _where;
130
        public string Where
131
        {
132
            get { return _where; }
133
            set
134
            {
135
                _where = value;
136
                NotifyOfPropertyChange(() => Where);
137
            }
138
        }
139

    
140
        private DateTime _modified;
141
        public DateTime Modified
142
        {
143
            get { return _modified; }
144
            set
145
            {
146
                _modified = value;
147
                NotifyOfPropertyChange(() => Modified);
148
            }
149
        }
150

    
151
        private string _modifiedBy;
152
        public string ModifiedBy
153
        {
154
            get { return _modifiedBy; }
155
            set
156
            {
157
                _modifiedBy = value;
158
                NotifyOfPropertyChange(() => ModifiedBy);
159
            }
160
        }
161

    
162
        private long _version;
163
        public long Version
164
        {
165
            get { return _version; }
166
            set
167
            {
168
                _version = value;
169
                NotifyOfPropertyChange(() => Version);
170
            }
171
        }
172

    
173
        private string _localFileName;
174
        protected string LocalFileName
175
        {
176
            get { return _localFileName; }
177
            set
178
            {
179
                _localFileName = value;
180
                NotifyOfPropertyChange(() => LocalFileName);
181
            }
182
        }
183

    
184
        private BitmapSource _fileIcon;
185
        public BitmapSource FileIcon
186
        {
187
            get { return _fileIcon; }
188
            set
189
            {
190
                _fileIcon = value;
191
                NotifyOfPropertyChange(() => FileIcon);
192
            }
193
        }
194

    
195
        private string _publicUrl;
196
        public string PublicUrl
197
        {
198
            get { return _publicUrl; }
199
            set
200
            {
201
                _publicUrl = value;
202
                NotifyOfPropertyChange(() => PublicUrl);
203
            }
204
        }
205

    
206
        private string _fileName;
207
        public string FileName
208
        {
209
            get { return _fileName; }
210
            set
211
            {
212
                _fileName = value;
213
                NotifyOfPropertyChange(() => FileName);
214
            }
215
        }
216

    
217
        private string _container;
218
        public string Container
219
        {
220
            get { return _container; }
221
            set
222
            {
223
                _container = value;
224
                NotifyOfPropertyChange(() => Container);
225
            }
226
        }
227

    
228
        public bool TagsChanged { get; private set; }
229
        public bool PermissionsChanged { get; private set; }
230

    
231
        private bool _isBusy = true;
232
        public bool IsBusy
233
        {
234
            get { return _isBusy; }
235
            set
236
            {
237
                _isBusy = value;
238
                NotifyOfPropertyChange(() => IsBusy);
239
            }
240
        }
241

    
242

    
243
        public FilePropertiesViewModel(ShellViewModel shell,Task<ObjectInfo> pithosFile,string localFileName)
244
        {
245
            if (shell==null)
246
                throw new ArgumentNullException("shell");
247
            if (pithosFile==null)
248
                throw new ArgumentNullException("pithosFile");
249
            if (String.IsNullOrWhiteSpace(localFileName))
250
                throw new ArgumentNullException("localFileName");
251
            Contract.EndContractBlock();
252

    
253

    
254
            _tags = new ObservableCollection<MetaValue>();
255
            _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
256
            _permissions = new ObservableCollection<Permission>();
257
            _permissions.CollectionChanged += (sender, evt) => { PermissionsChanged = true; };
258
            
259
            Shell = shell;
260
            LocalFileName = localFileName;
261
            pithosFile.ContinueWith(t =>
262
            {
263
                if (t.IsFaulted)
264
                {
265
                    IsBusy = false;
266
                    Execute.OnUIThread(()=>ShowError(t.Exception));
267
                    this.TryClose();
268

    
269
                }
270
                else
271
                    Execute.OnUIThread(() => PithosFile = t.Result);
272
            });                        
273
        }
274

    
275
        private void ShowError(AggregateException exception)
276
        {
277
            MessageView view = null;
278
            if (exception.InnerException is RetryException)
279
                view = new MessageView(exception.InnerException as RetryException);
280
            else if (exception.InnerException is WebException)
281
                view = new MessageView(exception.InnerException as WebException);
282
            else
283
                view = new MessageView(exception.InnerException);
284
            view.ShowDialog();
285
        }
286

    
287

    
288
        protected ShellViewModel Shell { get; set; }
289

    
290
        private ObjectInfo _pithosFile;
291
        public ObjectInfo PithosFile
292
        {
293
            get { return _pithosFile; }
294
            set
295
            {
296
                _pithosFile = value;
297
                
298
                Permissions.Clear();
299
                Tags.Clear();
300

    
301
                var perms=from permission in value.Permissions
302
                            select new Permission(permission.Key, permission.Value);
303
                perms.Apply(perm=>Permissions.Add(perm));
304
                
305
                var tags=from tag in value.Tags
306
                             select new MetaValue(tag.Key, tag.Value);
307
                tags.Apply(tag=>Tags.Add(tag));
308

    
309
                Title = String.Format("{0} Properties", value.Name);
310
                Kind=value.Content_Type;
311
                ShortSize = value.Bytes.ToByteSize();
312
                Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
313
                Where = Uri.UnescapeDataString(value.Name);
314
                FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
315
                Container = value.Container;
316
                Modified = value.Last_Modified;
317
                ModifiedBy = value.ModifiedBy;
318
                Version = value.Version??0;
319

    
320
                ContentDisposition = value.ContendDisposition;
321
                ContentEncoding = value.ContentEncoding;
322
                Manifest = value.Manifest;
323
                IsPublic = value.IsPublic;
324
                if (IsPublic)
325
                    PublicUrl = String.Format("{0}/v1{1}", Shell.Accounts.First(account=>account.UserName==PithosFile.Account).SiteUri,value.PublicUrl);
326

    
327
                using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
328
                {
329
                    FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
330
                                                                   BitmapSizeOptions.FromEmptyOptions());
331
                }
332
                NotifyOfPropertyChange(()=>PithosFile);
333
                IsBusy = false;
334
            }
335
        }
336

    
337

    
338
        private readonly ObservableCollection<MetaValue> _tags ;
339
        public ObservableCollection<MetaValue> Tags
340
        {
341
            get { return _tags;}
342
        }
343

    
344
        private readonly ObservableCollection<Permission> _permissions ;
345
        
346

    
347
        public ObservableCollection<Permission> Permissions
348
        {
349
            get { return _permissions; }
350
        }
351

    
352
        public void Reload()
353
        {
354
            PithosFile=Shell.RefreshObjectInfo(PithosFile);
355
        }
356

    
357
        public override void CanClose(Action<bool> callback)
358
        {
359
            base.CanClose(callback);
360
        }
361

    
362
        public void SaveChanges()
363
        {
364
            DoSave();
365
            TryClose();
366
        }
367

    
368
        public void RejectChanges()
369
        {
370
            TryClose();
371
        }
372

    
373
        public void ApplyChanges()
374
        {
375
            DoSave();
376
        }
377

    
378
        private void DoSave()
379
        {
380
            if (TagsChanged)
381
            {
382
                PithosFile.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
383
            }
384
            
385
            if (PermissionsChanged)
386
            {
387
                PithosFile.Permissions = this.Permissions.ToDictionary(perm => perm.UserName, perm => perm.Value);
388
            }
389

    
390
            PithosFile.ContendDisposition = ContentDisposition;
391
            PithosFile.ContentEncoding = ContentEncoding;
392
            PithosFile.Manifest = Manifest;
393
            PithosFile.IsPublic = IsPublic;
394

    
395
            var monitor = Shell.Monitors[PithosFile.Account];
396
            monitor.CloudClient.UpdateMetadata(PithosFile);
397

    
398

    
399
            TagsChanged = false;
400
            PermissionsChanged = false;
401
        }
402

    
403

    
404
    }
405
}