Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Preferences / AddAccountViewModel.cs @ fe62b7f4

History | View | Annotate | Download (10.1 kB)

1
#region
2
/* -----------------------------------------------------------------------
3
 * <copyright file="AddAccountViewModel.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;
43
using System.Collections.Generic;
44
using System.ComponentModel.Composition;
45
using System.IO;
46
using System.Linq;
47
using System.Threading.Tasks;
48
using System.Windows;
49
using System.Windows.Forms;
50
using Pithos.Client.WPF.Properties;
51
using Pithos.Network;
52
using MessageBox = System.Windows.MessageBox;
53
using Screen = Caliburn.Micro.Screen;
54

    
55
namespace Pithos.Client.WPF.Preferences
56
{
57
    [Export(typeof(AddAccountViewModel))]
58
    public class AddAccountViewModel:Screen
59
    {
60

    
61
        private readonly List<string> _servers;
62

    
63
        public List<string> Servers
64
        {
65
            get { return _servers; }
66
        }
67

    
68
        private bool _isValidServer;
69
        public bool IsValidServer
70
        {
71
            get { return _isValidServer; }
72
            set
73
            {
74
                _isValidServer = value;
75
                NotifyOfPropertyChange(()=>IsValidServer);
76
            }
77
        }
78

    
79

    
80
        private string _currentServer;
81
        public string CurrentServer
82
        {
83
            get { return _currentServer; }
84
            set
85
            {
86
                if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
87
                {
88
                    IsValidServer = false;
89
                    throw new UriFormatException();
90
                }
91
                _currentServer = value;
92
                IsValidServer = true;
93
                HasValidCredentials = false;
94
                IsConfirmed = false;
95
                NotifyOfPropertyChange(()=>CurrentServer);
96
            }
97
        }
98

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

    
111
        private string _token;
112
        public string Token
113
        {
114
            get { return _token; }
115
            set
116
            {
117
                _token = value;
118
                NotifyOfPropertyChange(()=>Token);
119
                NotifyOfPropertyChange(() => HasCredentials);
120
            }
121
        }
122

    
123
        private string _accountPath;
124
        public string AccountPath
125
        {
126
            get { return _accountPath; }
127
            set
128
            {
129
                _accountPath = value;
130
                NotifyOfPropertyChange(() => AccountPath);
131
                NotifyOfPropertyChange(() => HasAccountPath);
132
            }
133
        }
134

    
135

    
136
        public bool HasAccountPath
137
        {
138
            get { return !String.IsNullOrWhiteSpace(AccountPath); }
139
        }
140

    
141
        public bool HasCredentials
142
        {
143
            get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
144
        }
145

    
146

    
147
        private bool  _isConfirmed;
148

    
149
        public bool IsConfirmed
150
        {
151
            get { return _isConfirmed; }
152
            set
153
            {
154
                _isConfirmed = value;
155
                HasValidCredentials = false;
156
                NotifyOfPropertyChange(() => IsConfirmed);
157
            }
158
        }
159

    
160

    
161
        private bool _isAccountActive;
162

    
163
        public bool IsAccountActive
164
        {
165
            get { return _isAccountActive; }
166
            set
167
            {
168
                _isAccountActive = value;
169
                NotifyOfPropertyChange(() => IsAccountActive);
170
            }
171
        }
172

    
173

    
174
        private bool _shouldCreateOkeanosFolder;
175
        public bool ShouldCreateOkeanosFolder
176
        {
177
            get { return _shouldCreateOkeanosFolder; }
178
            set
179
            {
180
                _shouldCreateOkeanosFolder = value;
181
                NotifyOfPropertyChange(()=>ShouldCreateOkeanosFolder);
182
            }
183
        }
184

    
185
        public void SelectAccount()
186
        {
187
            using (var dlg = new FolderBrowserDialog{Description=Resources.AddAccountViewModel_SelectAccount_Please_select_a_folder})
188
            {
189
                //Ask the user to select a folder
190
                //Note: We need a parent window here, which we retrieve with GetView            
191
                var view = (Window)GetView();
192
                if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
193
                    return;
194

    
195
                AccountPath= dlg.SelectedPath;
196

    
197
                ShouldCreateOkeanosFolder=Directory.EnumerateFileSystemEntries(AccountPath).Any();                
198
            }
199
        }
200

    
201

    
202
        public async void RetrieveCredentials()
203
        {
204
            SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page");
205
            IsConfirmed = false;
206

    
207
            try
208
            {
209
                var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl);
210
                AccountName = credentials.UserName;
211
                Token = credentials.Password;
212

    
213
                IsConfirmed = true;
214
            }
215
            catch (PithosException exc)
216
            {
217
                ClearBusy();
218
                MessageBox.Show(exc.Message, "Unable to retrieve credentials");
219
            }
220
            catch (Exception exc)
221
            {
222
                IsConfirmed = false;
223
                MessageBox.Show(exc.ToString(), "Error");
224
                throw;
225
            }
226
            finally
227
            {
228
                ClearBusy();
229
                
230
                ((Window) GetView()).Activate();
231
            }
232

    
233
        }
234

    
235
        public AddAccountViewModel()
236
        {
237
            _servers=new List<string>
238
                         {
239
                             Settings.Default.ProductionServer, 
240
                             Settings.Default.DevelopmentServer
241
                         };
242
            CurrentServer = _servers[0];
243
        }
244

    
245
        private bool _hasValidCredentials;
246
        public bool HasValidCredentials
247
        {
248
            get { return _hasValidCredentials; }
249
            set
250
            {
251
                _hasValidCredentials = value;
252
                NotifyOfPropertyChange(()=>HasValidCredentials);
253
            }
254
        }
255

    
256
        private string _validationMessage;
257
        public string ValidationMessage
258
        {
259
            get { return _validationMessage; }
260
            set
261
            {
262
                _validationMessage = value;
263
                NotifyOfPropertyChange(()=>ValidationMessage);
264
            }
265
        }
266

    
267
        private bool _isWorking;
268
        public bool IsWorking
269
        {
270
            get { return _isWorking; }
271
            set
272
            {
273
                _isWorking = value;
274
                NotifyOfPropertyChange(()=>IsWorking);
275
            }
276
        }
277

    
278
        private string _busyTitle;
279
        public string BusyTitle
280
        {
281
            get { return _busyTitle; }
282
            set
283
            {
284
                _busyTitle = value;
285
                NotifyOfPropertyChange(()=>BusyTitle);
286
            }
287
        }
288

    
289
        private string _busyDetail;
290
        public string BusyDetail
291
        {
292
            get { return _busyDetail; }
293
            set
294
            {
295
                _busyDetail = value;
296
                NotifyOfPropertyChange(()=>BusyDetail);
297
            }
298
        }
299

    
300
        private void SetBusy(string title,string detail)
301
        {
302
            IsWorking = true;
303
            BusyTitle = title;
304
            BusyDetail = detail;
305
        }
306

    
307
        private void ClearBusy()
308
        {
309
            IsWorking = false;
310
            BusyTitle = "";
311
            BusyDetail = "";
312
            
313
        }
314

    
315
        public async void TestAccount()
316
        {
317
            try
318
            {
319
                SetBusy("Validating Credentials", "");
320
                var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer,/*Proxy=Proxy */};                
321
                await TaskEx.Run(() =>
322
                                     {
323
                                         client.Authenticate();
324
                                         return client.ListContainers(AccountName);
325
                                     });
326
                HasValidCredentials = true;
327
                ValidationMessage = "Credentials Validated";
328
            }
329
            catch
330
            {
331
                HasValidCredentials = false;
332
                MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
333
                ValidationMessage = "Credentials validation failed";
334
            }
335
            finally
336
            {
337
                ClearBusy();
338
            }
339
        }
340

    
341
    }
342
}