Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / FileProperties / ContainerPropertiesViewModel.cs @ c92e02f3

History | View | Annotate | Download (10.4 kB)

1
using System;
2
using System.Collections.Concurrent;
3
using System.Collections.Generic;
4
using System.Collections.ObjectModel;
5
using System.ComponentModel.Composition;
6
using System.Diagnostics.Contracts;
7
using System.Linq;
8
using System.Net;
9
using System.Text;
10
using System.Threading.Tasks;
11
using System.Windows;
12
using Caliburn.Micro;
13
using Pithos.Network;
14

    
15
namespace Pithos.Client.WPF.FileProperties
16
{
17
    [Export(typeof(ContainerPropertiesViewModel))]
18
    public class ContainerPropertiesViewModel:Screen
19
    {
20
        private string _containerName;
21

    
22
        /// <summary>
23
        /// Name of the displayed container
24
        /// </summary>
25
        public string ContainerName
26
        {
27
            get { return _containerName; }
28
            set
29
            {
30
                _containerName = value;
31
                NotifyOfPropertyChange(()=>ContainerName);
32
            }
33
        }
34

    
35
        private string _title;
36
        /// <summary>
37
        /// Window title
38
        /// </summary>
39
        public string Title
40
        {
41
            get { return _title; }
42
            set
43
            {
44
                _title = value;
45
                NotifyOfPropertyChange(() => Title);
46
            }
47
        }
48

    
49
        private long _count;
50

    
51
        /// <summary>
52
        /// Count of files in the container
53
        /// </summary>
54
        public long Count
55
        {
56
            get { return _count; }
57
            set
58
            {
59
                _count = value;
60
                NotifyOfPropertyChange(() => Count);
61
            }
62
        }
63

    
64
        private DateTime _modified;
65

    
66
        /// <summary>
67
        /// Date of last modification
68
        /// </summary>
69
        public DateTime Modified
70
        {
71
            get { return _modified; }
72
            set
73
            {
74
                _modified = value;
75
                NotifyOfPropertyChange(() => Modified);
76
            }
77
        }
78

    
79
        private string _size;
80

    
81
        /// <summary>
82
        /// Total size of the container in bytes
83
        /// </summary>
84
        public string Size
85
        {
86
            get { return _size; }
87
            set
88
            {
89
                _size = value;
90
                NotifyOfPropertyChange(() => Size);
91
            }
92
        }
93

    
94
        private string _shortSize;
95

    
96
        /// <summary>
97
        /// Total size of the container formatted in KB,MB etc
98
        /// </summary>
99
        public string ShortSize
100
        {
101
            get { return _shortSize; }
102
            set
103
            {
104
                _shortSize = value;
105
                NotifyOfPropertyChange(() => ShortSize);
106
            }
107
        }
108

    
109
        private int _blockSize;
110

    
111
        /// <summary>
112
        /// Block size used by the container
113
        /// </summary>
114
        public int BlockSize
115
        {
116
            get { return _blockSize; }
117
            set
118
            {
119
                _blockSize = value;
120
                NotifyOfPropertyChange(() => BlockSize);
121
            }
122
        }
123

    
124
        private string _blockHash;
125

    
126
        /// <summary>
127
        /// Hash algorithm used to calculate block hashes
128
        /// </summary>
129
        public string BlockHash
130
        {
131
            get { return _blockHash; }
132
            set
133
            {
134
                _blockHash = value;
135
                NotifyOfPropertyChange(() => BlockHash);
136
            }
137
        }
138

    
139
        private ShellViewModel _shell;
140

    
141
        /// <summary>
142
        /// Reference to the parent Shell
143
        /// </summary>
144
        protected ShellViewModel Shell
145
        {
146
            get { return _shell; }
147
            set
148
            {
149
                _shell = value;
150
                NotifyOfPropertyChange(() => Shell);
151
            }
152
        }
153

    
154
        private ContainerInfo _container;
155
        /// <summary>
156
        /// The displayed ContainerInfo
157
        /// </summary>
158
        protected ContainerInfo Container
159
        {
160
            get { return _container; }
161
            set
162
            {
163
                _container = value;
164

    
165
                
166

    
167
                LoadFromDictionary(Tags, value.Tags);
168
                TagsChanged = false;
169
                //LoadFromDictionary(Policies, value.Policies);
170

    
171
                if (value.Policies.ContainsKey("Versioning"))
172
                {
173
                    Versioning version;
174
                    if (Enum.TryParse(value.Policies["Versioning"], out version))
175
                    {
176
                        SelectedVersion = version;
177
                    }
178
                }
179

    
180
                if (value.Policies.ContainsKey("Quota"))
181
                {
182
                    int quota;
183
                    if (int.TryParse(value.Policies["Quota"], out quota))
184
                    {
185
                        Quota=quota;
186
                    }
187
                }
188

    
189
                Title = String.Format("{0} Properties", _container.Name);
190
                Count = value.Count;
191
                ShortSize = value.Bytes.ToByteSize();
192
                Size = String.Format("{0} ({1:N0} bytes)", ShortSize, value.Bytes);
193
                Modified = value.Last_Modified;
194
                BlockSize = value.BlockSize;
195
                BlockHash = value.BlockHash;                
196
                ContainerName = Uri.UnescapeDataString(value.Name.Split('/').Last());                                                
197

    
198
                NotifyOfPropertyChange(() => Container);
199
                IsBusy = false;
200
            }
201
        }
202

    
203
        private int _quota;
204
        public int Quota
205
        {
206
            get { return _quota; }
207
            set
208
            {
209
                if (value < 0)
210
                    throw new ArgumentOutOfRangeException("Quota");
211

    
212
                _quota = value;                
213
                NotifyOfPropertyChange(() => Quota);
214
            }
215
        }
216

    
217

    
218

    
219
        private Versioning[] _versions={Versioning.manual,Versioning.auto,Versioning.none};
220
        public Versioning[] Versions
221
        {
222
            get { return _versions; }
223
        }
224

    
225

    
226
        private Versioning _selectedVersion;
227
        public Versioning SelectedVersion
228
        {
229
            get { return _selectedVersion; }
230
            set { 
231
                _selectedVersion = value;
232
                NotifyOfPropertyChange(()=>SelectedVersion);
233
            }
234
        }
235

    
236
        private void LoadFromDictionary(ICollection<MetaValue> collection,Dictionary<string,string> source  )
237
        {
238
            collection.Clear();
239
            var items = from item in source
240
                        select new MetaValue(item.Key, item.Value);
241
            
242
            items.Apply(collection.Add);
243
        }
244

    
245
        private readonly ObservableCollection<MetaValue> _tags;
246
        public ObservableCollection<MetaValue> Tags
247
        {
248
            get { return _tags; }
249
        }
250

    
251
       /* private readonly ObservableCollection<MetaValue> _policies;
252
        public ObservableCollection<MetaValue> Policies
253
        {
254
            get { return _policies; }
255
        }*/
256

    
257
        private bool _tagsChanged;
258
        public bool TagsChanged
259
        {
260
            get { return _tagsChanged; }
261
            private set
262
            {
263
                _tagsChanged = value;
264
                NotifyOfPropertyChange(()=>TagsChanged);
265
                NotifyOfPropertyChange(() => CanApplyChanges);
266
            }
267
        }
268

    
269
        //public bool PoliciesChanged { get; private set; }
270

    
271
        private bool _isBusy=true;
272
        public bool IsBusy
273
        {
274
            get { return _isBusy; }
275
            set
276
            {
277
                _isBusy = value;
278
                NotifyOfPropertyChange(()=>IsBusy);
279
            }
280
        }
281

    
282
        public ContainerPropertiesViewModel(ShellViewModel shell, Task<ContainerInfo> container, string localFolderName)
283
        {
284
            if (shell==null)
285
                throw new ArgumentNullException("shell");
286
            if (container==null)
287
                throw new ArgumentNullException("container");
288
            if (String.IsNullOrWhiteSpace(localFolderName))
289
                throw new ArgumentNullException("localFolderName");
290
            Contract.EndContractBlock();
291
            
292
            _tags = new ObservableCollection<MetaValue>();
293
            _tags.CollectionChanged += (sender, evt) => { TagsChanged = true; };
294

    
295
            /*_policies = new ObservableCollection<MetaValue>();
296
            _policies.CollectionChanged += (sender, evt) => { PoliciesChanged = true; };
297
*/
298

    
299
            Shell = shell;
300
            ContainerName = localFolderName;
301
            container.ContinueWith(t =>{                
302
                if (t.IsFaulted)
303
                {
304
                    IsBusy = false;
305
                    Execute.OnUIThread(() => ShowError(t.Exception));
306
                    this.TryClose();
307

    
308
                }
309
                else
310
                    Execute.OnUIThread(()=>Container = t.Result);                                           
311
            });
312
        }
313

    
314
        private void ShowError(AggregateException exception)
315
        {
316
            MessageView view=null;
317
            if (exception.InnerException is RetryException)
318
                view=new MessageView(exception.InnerException as RetryException);
319
            else if (exception.InnerException is WebException)
320
                view = new MessageView(exception.InnerException as WebException);
321
            else
322
                view = new MessageView(exception.InnerException);
323
            view.ShowDialog();
324
        }
325

    
326
        public void Reload()
327
        {
328
            Container = Shell.RefreshContainerInfo(Container);
329
        }
330

    
331
        public override void CanClose(Action<bool> callback)
332
        {
333
            base.CanClose(callback);
334
        }
335

    
336
        public void SaveChanges()
337
        {
338
            DoSave();
339
            TryClose();
340
        }
341

    
342
        public void RejectChanges()
343
        {
344
            TryClose();
345
        }
346

    
347
        public void ApplyChanges()
348
        {
349
            DoSave();
350
        }
351

    
352
        public bool CanApplyChanges
353
        {
354
            get { return TagsChanged; }
355
        }
356

    
357
        private void DoSave()
358
        {
359
            
360
            if (TagsChanged)
361
            {
362
                Container.Tags = this.Tags.ToDictionary(tag => tag.Name, tag => tag.Value);
363
                var monitor = Shell.Monitors[Container.Account];
364
                monitor.CloudClient.UpdateMetadata(Container);
365
            }
366

    
367
            //Container.Policies["Quota"] = Quota.ToString();
368
            //Container.Policies["Versioning"] = Enum.GetName(typeof (Versioning), SelectedVersion);
369

    
370
/*
371
            if (PoliciesChanged)
372
            {
373
                Container.Policies = this.Policies.ToDictionary(policy=> policy.Name, tag => tag.Value);
374
            }
375
*/
376
            
377

    
378
        }
379

    
380
    }
381

    
382
    
383
}