Statistics
| Branch: | Revision:

root / trunk / Pithos.OFM / CloudFilesViewModel.cs @ 71374945

History | View | Annotate | Download (3.6 kB)

1
´╗┐using System;
2
using System.Collections.Generic;
3
using System.ComponentModel.Composition;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Windows;
8
using Caliburn.Micro;
9
using Pithos.Network;
10

    
11
namespace Pithos.OFM
12
{
13
    [Export]
14
    public class CloudFilesViewModel:Screen
15
    {
16
        public bool IsCloudBusy
17
        {
18
            get { return _isCloudBusy; }
19
            set
20
            {
21
                if (value.Equals(_isCloudBusy)) return;
22
                _isCloudBusy = value;
23
                NotifyOfPropertyChange(() => IsCloudBusy);
24
            }
25
        }
26

    
27
        public bool IsCloudSelected
28
        {
29
            get { return _isCloudSelected; }
30
            set
31
            {
32
                if (value.Equals(_isCloudSelected)) return;
33
                _isCloudSelected = value;
34
                NotifyOfPropertyChange(() => IsCloudSelected);
35
            }
36
        }
37

    
38
        private ObjectRecord _selectedCloudFile;
39

    
40
        public ObjectRecord SelectedCloudFile
41
        {
42
            get { return _selectedCloudFile; }
43
            set
44
            {
45
                if (Equals(value, _selectedCloudFile)) return;
46
                _selectedCloudFile = value;
47
                NotifyOfPropertyChange(() => SelectedCloudFile);
48
                NotifyOfPropertyChange(() => CanDownload);
49
                IsCloudSelected = true;
50
            }
51
        }
52

    
53
        public bool CanDownload { get { return SelectedCloudFile != null; } }
54

    
55
        public void SelectCloudFile(RoutedPropertyChangedEventArgs<object> file)
56
        {
57
            SelectedCloudFile = (ObjectRecord)file.NewValue;
58
        }
59

    
60

    
61
        public async Task LoadCloudFiles(string accountName, string apiKey, string serverUrl)
62
        {
63
            await TaskEx.Yield();
64
            IsCloudBusy = true;
65
            CloudBusyMessage = "Checking Pithos ...";
66
            var client = new CloudFilesClient(accountName, apiKey) { AuthenticationUrl = serverUrl, UsePithos = true };
67
            await client.Authenticate().ConfigureAwait(false);
68

    
69
            var containerInfos = await client.ListContainers(accountName).ConfigureAwait(false);
70
            var infos = from container in containerInfos
71
                        from info in client.ListObjects(accountName, container.Name)
72
                        select info;
73
            //Force enumeration here to get all items. Otherwise we get only the containers
74
            //and the objects will be retrieved by the UI thread during binding
75
            var rootItem = new ObjectRecord()
76
            {
77
                DisplayName = accountName,
78
                Directories = infos.ToTree()
79
            };
80

    
81
            CloudInfos = rootItem;
82
            IsCloudBusy = false;
83
        }
84

    
85

    
86
        public string CloudBusyMessage
87
        {
88
            get { return _cloudBusyMessage; }
89
            set
90
            {
91
                if (value == _cloudBusyMessage) return;
92
                _cloudBusyMessage = value;
93
                NotifyOfPropertyChange(() => CloudBusyMessage);
94
            }
95
        }
96

    
97
        public IEnumerable<ObjectRecord> CloudInfos
98
        {
99
            get { return _cloudInfos; }
100
            set
101
            {
102
                if (Equals(value, _cloudInfos)) return;
103
                _cloudInfos = value;
104
                NotifyOfPropertyChange(() => CloudInfos);
105
            }
106
        }
107

    
108

    
109
        private IEnumerable<ObjectRecord> _cloudInfos;
110

    
111
        private bool _isCloudBusy;
112
        private string _cloudBusyMessage;
113
        private bool _isCloudSelected;
114

    
115

    
116
    }
117
}