Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (15.1 kB)

1
#region
2
/* -----------------------------------------------------------------------
3
 * <copyright file="FilePropertiesViewModel.cs" company="GRNet">
4
 * 
5
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or
8
 * without modification, are permitted provided that the following
9
 * conditions are met:
10
 *
11
 *   1. Redistributions of source code must retain the above
12
 *      copyright notice, this list of conditions and the following
13
 *      disclaimer.
14
 *
15
 *   2. Redistributions in binary form must reproduce the above
16
 *      copyright notice, this list of conditions and the following
17
 *      disclaimer in the documentation and/or other materials
18
 *      provided with the distribution.
19
 *
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 * The views and conclusions contained in the software and
35
 * documentation are those of the authors and should not be
36
 * interpreted as representing official policies, either expressed
37
 * or implied, of GRNET S.A.
38
 * </copyright>
39
 * -----------------------------------------------------------------------
40
 */
41
#endregion
42
using System.Collections;
43
using System.Collections.Concurrent;
44
using System.Collections.ObjectModel;
45
using System.Collections.Specialized;
46
using System.ComponentModel.Composition;
47
using System.Diagnostics;
48
using System.Diagnostics.Contracts;
49
using System.Drawing;
50
using System.IO;
51
using System.Net;
52
using System.Threading.Tasks;
53
using System.Windows;
54
using System.Windows.Interop;
55
using System.Windows.Media.Imaging;
56
using Caliburn.Micro;
57
using Pithos.Client.WPF.FileProperties;
58
using Pithos.Client.WPF.Properties;
59
using Pithos.Core;
60
using Pithos.Interfaces;
61
using Pithos.Network;
62

    
63
namespace Pithos.Client.WPF
64
{
65
    using System;
66
    using System.Collections.Generic;
67
    using System.Linq;
68
    using System.Text;
69

    
70
    /// <summary>
71
    /// TODO: Update summary.
72
    /// </summary>
73
    [Export(typeof(FilePropertiesViewModel))]
74
    public class FilePropertiesViewModel : Screen
75
    {
76
        private string _title;
77
        public string Title
78
        {
79
            get { return _title; }
80
            set
81
            {
82
                _title = value;
83
                NotifyOfPropertyChange(()=>Title);
84
            }
85
        }
86

    
87

    
88
        private bool _isPublic;
89
        public bool IsPublic
90
        {
91
            get { return _isPublic; }
92
            set
93
            {
94
                _isPublic = value;
95
                NotifyOfPropertyChange(()=>IsPublic);
96
            }
97
        }
98

    
99
        private string _contentDisposition;
100
        public string ContentDisposition
101
        {
102
            get { return _contentDisposition; }
103
            set
104
            {
105
                _contentDisposition = value;
106
                NotifyOfPropertyChange(() => ContentDisposition);
107
            }
108
        }
109

    
110
        private string _contentEncoding;
111
        public string ContentEncoding
112
        {
113
            get { return _contentEncoding; }
114
            set
115
            {
116
                _contentEncoding = value;
117
                NotifyOfPropertyChange(() => ContentEncoding);
118
            }
119
        }
120

    
121

    
122
        private string _manifest;
123
        public string Manifest
124
        {
125
            get { return _manifest; }
126
            set
127
            {
128
                _manifest = value;
129
                NotifyOfPropertyChange(() => Manifest);
130
            }
131
        }
132

    
133
        private string _kind;
134
        public string Kind
135
        {
136
            get { return _kind; }
137
            set
138
            {
139
                _kind = value;
140
                NotifyOfPropertyChange(() => Kind);
141
            }
142
        }
143

    
144
        private string _size;
145
        public string Size
146
        {
147
            get { return _size; }
148
            set
149
            {
150
                _size = value;
151
                NotifyOfPropertyChange(() => Size);
152
            }
153
        }
154

    
155
        private string _shortSize;
156
        public string ShortSize
157
        {
158
            get { return _shortSize; }
159
            set
160
            {
161
                _shortSize = value;
162
                NotifyOfPropertyChange(() => ShortSize);
163
            }
164
        }
165

    
166
        private string _where;
167
        public string Where
168
        {
169
            get { return _where; }
170
            set
171
            {
172
                _where = value;
173
                NotifyOfPropertyChange(() => Where);
174
            }
175
        }
176

    
177
        private DateTime _modified;
178
        public DateTime Modified
179
        {
180
            get { return _modified; }
181
            set
182
            {
183
                _modified = value;
184
                NotifyOfPropertyChange(() => Modified);
185
            }
186
        }
187

    
188
        private string _modifiedBy;
189
        public string ModifiedBy
190
        {
191
            get { return _modifiedBy; }
192
            set
193
            {
194
                _modifiedBy = value;
195
                NotifyOfPropertyChange(() => ModifiedBy);
196
            }
197
        }
198

    
199
        private long _version;
200
        public long Version
201
        {
202
            get { return _version; }
203
            set
204
            {
205
                _version = value;
206
                NotifyOfPropertyChange(() => Version);
207
            }
208
        }
209

    
210
        private string _localFileName;
211
        protected string LocalFileName
212
        {
213
            get { return _localFileName; }
214
            set
215
            {
216
                _localFileName = value;
217
                NotifyOfPropertyChange(() => LocalFileName);
218
            }
219
        }
220

    
221
        private BitmapSource _fileIcon;
222
        public BitmapSource FileIcon
223
        {
224
            get { return _fileIcon; }
225
            set
226
            {
227
                _fileIcon = value;
228
                NotifyOfPropertyChange(() => FileIcon);
229
            }
230
        }
231

    
232
        private string _publicUrl;
233
        public string PublicUrl
234
        {
235
            get { return _publicUrl; }
236
            set
237
            {
238
                _publicUrl = value;
239
                NotifyOfPropertyChange(() => PublicUrl);
240
            }
241
        }
242

    
243
        private string _fileName;
244
        public string FileName
245
        {
246
            get { return _fileName; }
247
            set
248
            {
249
                _fileName = value;
250
                NotifyOfPropertyChange(() => FileName);
251
            }
252
        }
253

    
254
        private string _container;
255
        public string Container
256
        {
257
            get { return _container; }
258
            set
259
            {
260
                _container = value;
261
                NotifyOfPropertyChange(() => Container);
262
            }
263
        }
264

    
265
        private string _synchStatus;
266
        public string SynchStatus
267
        {
268
            get { return _synchStatus; }
269
            set
270
            {
271
                _synchStatus = value;
272
                NotifyOfPropertyChange(()=>SynchStatus);
273
            }
274
        }
275

    
276
        private string _permissionName;
277
        public string PermissionName
278
        {
279
            get { return _permissionName; }
280
            set
281
            {
282
                _permissionName = value;
283
                NotifyOfPropertyChange(()=>PermissionName);
284
            }
285
        }
286

    
287
        private Permission _currentPermission;
288
        public Permission CurrentPermission
289
        {
290
            get { return _currentPermission; }
291
            set
292
            {
293
                _currentPermission = value;
294
                NotifyOfPropertyChange(()=>CurrentPermission);
295
            }
296
        }
297

    
298

    
299
        private bool _permissionRead;
300
        public bool PermissionRead
301
        {
302
            get { return _permissionRead; }
303
            set
304
            {
305
                _permissionRead = value;
306
                NotifyOfPropertyChange(()=>PermissionRead);
307
                if (CurrentPermission != null)
308
                {
309
                    CurrentPermission.Read = value;
310
                }
311
            }
312
        }
313
        public bool PermissionWrite
314
        {
315
            get { return CurrentPermission.Write; }
316
            set
317
            {
318
                CurrentPermission.Write = value;
319
                NotifyOfPropertyChange(()=>PermissionWrite);
320
            }
321
        }
322

    
323
        public bool CanAddPermission
324
        {
325
            get { return !String.IsNullOrWhiteSpace(PermissionName); }
326
        }
327

    
328
        public void AddPermission()
329
        {
330
            Permissions.Add(new Permission{Read=PermissionRead,UserName=PermissionName,Write=!PermissionRead});   
331
        }
332

    
333

    
334
        public bool TagsChanged { get; private set; }
335
        public bool PermissionsChanged { get; private set; }
336

    
337
        private bool _isBusy = true;
338
        public bool IsBusy
339
        {
340
            get { return _isBusy; }
341
            set
342
            {
343
                _isBusy = value;
344
                NotifyOfPropertyChange(() => IsBusy);
345
            }
346
        }
347

    
348

    
349
        public FilePropertiesViewModel(ShellViewModel shell,Task<ObjectInfo> pithosFile,string localFileName)
350
        {
351
            if (shell==null)
352
                throw new ArgumentNullException("shell");
353
            if (pithosFile==null)
354
                throw new ArgumentNullException("pithosFile");
355
            if (String.IsNullOrWhiteSpace(localFileName))
356
                throw new ArgumentNullException("localFileName");
357
            Contract.EndContractBlock();
358

    
359

    
360
            _tags = new ObservableCollection<MetaValue>();
361
            _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
362
            _permissions = new ObservableCollection<Permission>();
363
            _permissions.CollectionChanged += (sender, evt) => { PermissionsChanged = true; };
364
            
365
            Shell = shell;
366
            LocalFileName = localFileName;
367
            pithosFile.ContinueWith(t =>
368
            {
369
                if (t.IsFaulted)
370
                {
371
                    IsBusy = false;
372
                    Execute.OnUIThread(()=>ShowError(t.Exception));
373
                    this.TryClose();
374

    
375
                }
376
                else
377
                    Execute.OnUIThread(() => PithosFile = t.Result);
378
            });       
379
                 
380
        }
381

    
382
        private void ShowError(AggregateException exception)
383
        {
384
            MessageView view = null;
385
            if (exception.InnerException is RetryException)
386
                view = new MessageView(exception.InnerException as RetryException);
387
            else if (exception.InnerException is WebException)
388
                view = new MessageView(exception.InnerException as WebException);
389
            else
390
                view = new MessageView(exception.InnerException);
391
            view.ShowDialog();
392
        }
393

    
394

    
395
        protected ShellViewModel Shell { get; set; }
396

    
397
        private ObjectInfo _pithosFile;
398
        public ObjectInfo PithosFile
399
        {
400
            get { return _pithosFile; }
401
            set
402
            {
403
                _pithosFile = value;
404
                
405
                Permissions.Clear();
406
                Tags.Clear();
407

    
408
                var perms=from permission in value.Permissions
409
                            select new Permission(permission.Key, permission.Value);
410
                perms.Apply(perm=>Permissions.Add(perm));
411
                
412
                var tags=from tag in value.Tags
413
                             select new MetaValue(tag.Key, tag.Value);
414
                tags.Apply(tag=>Tags.Add(tag));
415

    
416
                Title = String.Format("{0} Properties", value.Name);
417
                Kind=value.Content_Type;
418
                ShortSize = value.Bytes.ToByteSize();
419
                Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
420
                Where = Uri.UnescapeDataString(value.Name);
421
                FileName = Uri.UnescapeDataString(value.Name.Split('/').Last());
422
                Container = value.Container;
423
                Modified = value.Last_Modified;
424
                ModifiedBy = value.ModifiedBy;
425
                Version = value.Version??0;
426

    
427
                ContentDisposition = value.ContendDisposition;
428
                ContentEncoding = value.ContentEncoding;
429
                Manifest = value.Manifest;
430
                IsPublic = value.IsPublic;
431
                if (IsPublic)
432
                    PublicUrl = String.Format("{0}/v1{1}", Shell.Accounts.First(account=>account.UserName==PithosFile.Account).SiteUri,value.PublicUrl);
433

    
434
                if (Directory.Exists(LocalFileName))
435
                {
436
                    FileIcon= new BitmapImage(new Uri("../Images/Folder.ico",UriKind.Relative));
437
                }
438
                else if (File.Exists(LocalFileName))
439
                {
440
                    using (var icon = Icon.ExtractAssociatedIcon(LocalFileName))
441
                    {
442
                        FileIcon = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty,
443
                                                                       BitmapSizeOptions.FromEmptyOptions());
444
                    }
445
                }
446

    
447
                var status=Shell.GetFileStatus(LocalFileName);
448
                SynchStatus = Enum.GetName(typeof (FileStatus), status);
449

    
450

    
451
                NotifyOfPropertyChange(()=>PithosFile);
452
                IsBusy = false;
453
            }
454
        }
455

    
456

    
457
        private readonly ObservableCollection<MetaValue> _tags ;
458
        public ObservableCollection<MetaValue> Tags
459
        {
460
            get { return _tags;}
461
        }
462

    
463
        private readonly ObservableCollection<Permission> _permissions ;
464
        
465

    
466
        public ObservableCollection<Permission> Permissions
467
        {
468
            get { return _permissions; }
469
        }
470

    
471
        public void Reload()
472
        {
473
            PithosFile=Shell.RefreshObjectInfo(PithosFile);
474
        }
475

    
476
        public override void CanClose(Action<bool> callback)
477
        {
478
            base.CanClose(callback);
479
        }
480

    
481
        public void SaveChanges()
482
        {
483
            DoSave();
484
            TryClose();
485
        }
486

    
487
        public void RejectChanges()
488
        {
489
            TryClose();
490
        }
491

    
492
        public void ApplyChanges()
493
        {
494
            DoSave();
495
        }
496

    
497
        private void DoSave()
498
        {
499
            if (TagsChanged)
500
            {
501
                PithosFile.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
502
            }
503
            
504
            if (PermissionsChanged)
505
            {
506
                PithosFile.Permissions = this.Permissions.ToDictionary(perm => perm.UserName, perm => 
507
                    (perm.Value.Trim()));
508
            }
509

    
510
            PithosFile.ContendDisposition = ContentDisposition;
511
            PithosFile.ContentEncoding = ContentEncoding;
512
            PithosFile.Manifest = Manifest;
513
            PithosFile.IsPublic = IsPublic;
514
            
515
            var monitor = Shell.Monitors[PithosFile.AccountKey];
516
            monitor.CloudClient.UpdateMetadata(PithosFile);
517

    
518

    
519
            TagsChanged = false;
520
            PermissionsChanged = false;
521
        }
522

    
523

    
524
    }
525
}