Revision 71374945 trunk/Pithos.OFM/FileManagerViewModel.cs

b/trunk/Pithos.OFM/FileManagerViewModel.cs
14 14
    [Export, PartCreationPolicy(CreationPolicy.Shared)]
15 15
    public class FileManagerViewModel:Screen
16 16
    {
17
        [Import]
18
        public CloudFilesViewModel CloudFiles { get; set; }
17 19
        
20
        [Import]
21
        public LocalFilesViewModel LocalFiles { get; set; }
22

  
18 23
        public IEnumerable<AccountSettings> Accounts
19 24
        {
20 25
            get { return _accounts; }
......
37 42
            }
38 43
        }
39 44

  
45
        public bool IsCloudSelected
46
        {
47
            get { return CloudFiles.IsCloudSelected; }
48
            set
49
            {
50
                if (value.Equals(CloudFiles.IsCloudSelected)) return;
51
                CloudFiles.IsCloudSelected = value;
52
                LocalFiles.IsLocalSelected = !value;
53
                NotifyOfPropertyChange(() => IsCloudSelected);
54
            }
55
        }
56

  
57
        public bool IsLocalSelected
58
        {
59
            get { return LocalFiles.IsLocalSelected; }
60
            set
61
            {
62
                if (value.Equals(LocalFiles.IsLocalSelected)) return;
63
                LocalFiles.IsLocalSelected = value;
64
                IsCloudSelected = !value;
65
                NotifyOfPropertyChange(() => IsLocalSelected);
66
            }
67
        }
68

  
69
        private IEnumerable<AccountSettings> _accounts;
70
        private string _title;
71

  
40 72
        private AccountSettings _currentAccount;
41 73
        
42 74
        private AccountSettings _currentLoadedAccount;
......
65 97
                    this.Title = String.Format("Pithos+ OFM - {0}", _currentAccount.AccountName);
66 98
                    
67 99

  
68
                    TaskEx.Run(() => LoadLocalFiles(_currentAccount.RootPath));
100
                    
101
                    TaskEx.Run(() => LocalFiles.LoadLocalFiles(_currentAccount.RootPath));
69 102
                    TaskEx.Run(
70 103
                        () =>
71
                        LoadCloudFiles(_currentAccount.AccountName, _currentAccount.ApiKey, _currentAccount.ServerUrl));
104
                        CloudFiles.LoadCloudFiles(_currentAccount.AccountName, _currentAccount.ApiKey, _currentAccount.ServerUrl));
72 105
                }
73 106
            }
74 107
        }
75 108

  
76
        public IEnumerable<ObjectRecord> CloudInfos
77
        {
78
            get { return _cloudInfos; }
79
            set
80
            {
81
                if (Equals(value, _cloudInfos)) return;
82
                _cloudInfos = value;
83
                NotifyOfPropertyChange(() => CloudInfos);
84
            }
85
        }
86 109

  
87 110

  
88
        public bool IsLocalSelected
89
        {
90
            get { return _isLocalSelected; }
91
            set
92
            {
93
                if (value.Equals(_isLocalSelected)) return;
94
                _isLocalSelected = value;
95
                IsCloudSelected = !value;
96
                NotifyOfPropertyChange(() => IsLocalSelected);
97
            }
98
        }
99

  
100
        public FileSystemInfo CurrentLocalItem
101
        {
102
            get { return _currentLocalItem; }
103
            set
104
            {
105
                if (Equals(value, _currentLocalItem)) return;
106
                _currentLocalItem = value;
107
                NotifyOfPropertyChange(() => CurrentLocalItem);
108
                
109
            }
110
        }
111

  
112 111
        public FileManagerViewModel()
113 112
        {
114 113
            DisplayName = "Pithos+ OFM";
115
            _watcher = new FileSystemWatcher();            
116 114
          //  _watcher.Created += (o, e) => NotifyOfPropertyChange(()=>LocalItems);
117 115
          //  _watcher.Deleted += (o, e) => NotifyOfPropertyChange(() => LocalItems);
118 116
          //  _watcher.Renamed += (o, e) => NotifyOfPropertyChange(() => LocalItems);
119 117
        }
120 118

  
121
        readonly FileSystemWatcher _watcher;
122
        private IEnumerable<DirectoryRecord> _localFiles;
123
        private FileSystemInfo _currentLocalItem;
124
        private IEnumerable<ObjectRecord> _cloudInfos;
125

  
126

  
127

  
128
        public IEnumerable<DirectoryRecord> LocalFiles
129
        {
130
            get { return _localFiles; }
131
            set
132
            {
133
                if (Equals(value, _localFiles)) return;
134
                _localFiles = value;
135
                NotifyOfPropertyChange(() => LocalFiles);
136
            }
137
        }
138

  
139
        private IInfoRecord _selectedLocalFile;
140

  
141
        public IInfoRecord SelectedLocalFile
142
        {
143
            get { return _selectedLocalFile; }
144
            set
145
            {
146
                if (Equals(value, _selectedLocalFile)) return;
147
                _selectedLocalFile = value;
148
                NotifyOfPropertyChange(() => SelectedLocalFile);
149
                NotifyOfPropertyChange(() => CanUpload);
150
                IsLocalSelected = true;
151
            }
152
        }
153

  
154
        public void SelectLocalFile(RoutedPropertyChangedEventArgs<object> file)
155
        {
156
            SelectedLocalFile=(IInfoRecord) file.NewValue;
157
        }
158

  
159
        private void LoadLocalFiles(string path)
160
        {
161
            IsLocalBusy = true;
162
            LocalBusyMessage = "Reading local files ...";
163
            if (_watcher.Path != path)
164
            {
165
                _watcher.Path = path;
166
                _watcher.EnableRaisingEvents = true;
167
            }
168
            
169
            var folder = new DirectoryInfo(Path.Combine(path,"pithos"));
170
            LocalFiles = new List<DirectoryRecord>
171
                {
172
                    new DirectoryRecord{DirInfo=folder}
173
                };
174
            IsLocalBusy = false;
175

  
176
        }
177

  
178

  
179
        public bool IsCloudSelected
180
        {
181
            get { return _isCloudSelected; }
182
            set
183
            {
184
                if (value.Equals(_isCloudSelected)) return;
185
                _isCloudSelected = value;
186
                IsLocalSelected = !value;
187
                NotifyOfPropertyChange(() => IsCloudSelected);
188
            }
189
        }
190

  
191
        private ObjectRecord _selectedCloudFile;
192

  
193
        public ObjectRecord SelectedCloudFile
194
        {
195
            get { return _selectedCloudFile; }
196
            set
197
            {
198
                if (Equals(value, _selectedCloudFile)) return;
199
                _selectedCloudFile = value;
200
                NotifyOfPropertyChange(() => SelectedCloudFile);
201
                NotifyOfPropertyChange(() => CanDownload);
202
                IsCloudSelected = true;
203
            }
204
        }
205

  
206
        public void SelectCloudFile(RoutedPropertyChangedEventArgs<object> file)
207
        {
208
            SelectedCloudFile = (ObjectRecord)file.NewValue;
209
        }
210

  
211

  
212

  
213
        private async Task LoadCloudFiles(string accountName, string apiKey, string serverUrl)
214
        {
215
            await TaskEx.Yield();
216
            IsCloudBusy = true;
217
            CloudBusyMessage = "Checking Pithos ...";
218
            var client = new CloudFilesClient(accountName, apiKey) { AuthenticationUrl = serverUrl, UsePithos = true };
219
            await client.Authenticate().ConfigureAwait(false);
220 119

  
221
            var containerInfos = await client.ListContainers(accountName).ConfigureAwait(false);
222
            var infos = from container in containerInfos                        
223
                       from info in client.ListObjects(accountName, container.Name)
224
                       select info;
225
            //Force enumeration here to get all items. Otherwise we get only the containers
226
            //and the objects will be retrieved by the UI thread during binding
227
            var rootItem = new ObjectRecord()
228
            {
229
                DisplayName = accountName,
230
                Directories = infos.ToTree()
231
            };
232

  
233
            CloudInfos = rootItem;
234
            IsCloudBusy = false;
235
        }
236

  
237
        private bool _isLocalBusy;
238
        private bool _isCloudBusy;
239
        private string _localBusyMessage;
240
        private string _cloudBusyMessage;
241
        private IEnumerable<AccountSettings> _accounts;
242
        private string _title;
243
        private bool _isLocalSelected;
244
        private bool _isCloudSelected;
245

  
246
        public bool IsCloudBusy
247
        {
248
            get { return _isCloudBusy; }
249
            set
250
            {
251
                if (value.Equals(_isCloudBusy)) return;
252
                _isCloudBusy = value;
253
                NotifyOfPropertyChange(() => IsCloudBusy);
254
            }
255
        }
256

  
257
        public bool IsLocalBusy
258
        {
259
            get { return _isLocalBusy; }
260
            set
261
            {
262
                if (value.Equals(_isLocalBusy)) return;
263
                _isLocalBusy = value;
264
                NotifyOfPropertyChange(() => IsLocalBusy);
265
            }
266
        }
267

  
268
        public string LocalBusyMessage
269
        {
270
            get { return _localBusyMessage; }
271
            set
272
            {
273
                if (value == _localBusyMessage) return;
274
                _localBusyMessage = value;
275
                NotifyOfPropertyChange(() => LocalBusyMessage);
276
            }
277
        }
278

  
279
        public string CloudBusyMessage
280
        {
281
            get { return _cloudBusyMessage; }
282
            set
283
            {
284
                if (value == _cloudBusyMessage) return;
285
                _cloudBusyMessage = value;
286
                NotifyOfPropertyChange(() => CloudBusyMessage);
287
            }
288
        }
289 120

  
290
        public bool CanUpload { get { return SelectedLocalFile != null; } }
121
        public bool CanUpload { get { return LocalFiles.CanUpload; } }
291 122

  
292 123
        public void Upload()
293 124
        {
294 125
            
295 126
        }
296 127

  
297
        public bool CanDownload { get { return SelectedCloudFile != null; } }
128
        public bool CanDownload { get { return CloudFiles.CanDownload; } }
298 129

  
299 130
        public void Download()
300 131
        {

Also available in: Unified diff