Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / Preferences / AddAccountViewModel.cs @ 5e31048f

History | View | Annotate | Download (8.2 kB)

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.ComponentModel.Composition;
5
using System.IO;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows;
10
using System.Windows.Controls;
11
using System.Windows.Forms;
12
using Caliburn.Micro;
13
using Pithos.Client.WPF.Properties;
14
using Pithos.Core;
15
using Pithos.Network;
16
using MessageBox = System.Windows.MessageBox;
17
using Screen = Caliburn.Micro.Screen;
18

    
19
namespace Pithos.Client.WPF.Preferences
20
{
21
    [Export(typeof(AddAccountViewModel))]
22
    public class AddAccountViewModel:Screen
23
    {
24

    
25
        private readonly List<string> _servers;
26

    
27
        public List<string> Servers
28
        {
29
            get { return _servers; }
30
        }
31

    
32
        private bool _isValidServer;
33
        public bool IsValidServer
34
        {
35
            get { return _isValidServer; }
36
            set
37
            {
38
                _isValidServer = value;
39
                NotifyOfPropertyChange(()=>IsValidServer);
40
            }
41
        }
42

    
43

    
44
        private string _currentServer;
45
        public string CurrentServer
46
        {
47
            get { return _currentServer; }
48
            set
49
            {
50
                if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
51
                {
52
                    IsValidServer = false;
53
                    throw new UriFormatException();
54
                }
55
                _currentServer = value;
56
                IsValidServer = true;
57
                HasValidCredentials = false;
58
                IsConfirmed = false;
59
                NotifyOfPropertyChange(()=>CurrentServer);
60
            }
61
        }
62

    
63
        private string _accountName;
64
        public string AccountName
65
        {
66
            get { return _accountName; }
67
            set
68
            {
69
                _accountName = value;
70
                NotifyOfPropertyChange(()=>AccountName);
71
                NotifyOfPropertyChange(() => HasCredentials);
72
            }
73
        }
74

    
75
        private string _token;
76
        public string Token
77
        {
78
            get { return _token; }
79
            set
80
            {
81
                _token = value;
82
                NotifyOfPropertyChange(()=>Token);
83
                NotifyOfPropertyChange(() => HasCredentials);
84
            }
85
        }
86

    
87
        private string _accountPath;
88
        public string AccountPath
89
        {
90
            get { return _accountPath; }
91
            set
92
            {
93
                _accountPath = value;
94
                NotifyOfPropertyChange(() => AccountPath);
95
                NotifyOfPropertyChange(() => HasAccountPath);
96
            }
97
        }
98

    
99

    
100
        public bool HasAccountPath
101
        {
102
            get { return !String.IsNullOrWhiteSpace(AccountPath); }
103
        }
104

    
105
        public bool HasCredentials
106
        {
107
            get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
108
        }
109

    
110

    
111
        private bool  _isConfirmed;
112

    
113
        public bool IsConfirmed
114
        {
115
            get { return _isConfirmed; }
116
            set
117
            {
118
                _isConfirmed = value;
119
                HasValidCredentials = false;
120
                NotifyOfPropertyChange(() => IsConfirmed);
121
            }
122
        }
123

    
124

    
125
        private bool _isAccountActive;
126

    
127
        public bool IsAccountActive
128
        {
129
            get { return _isAccountActive; }
130
            set
131
            {
132
                _isAccountActive = value;
133
                NotifyOfPropertyChange(() => IsAccountActive);
134
            }
135
        }
136

    
137
        public void SelectAccount()
138
        {
139
            using (var dlg = new FolderBrowserDialog())
140
            {
141
                //Ask the user to select a folder
142
                //Note: We need a parent window here, which we retrieve with GetView            
143
                var view = (Window)GetView();
144
                if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
145
                    return;
146

    
147
                AccountPath= dlg.SelectedPath;
148
            }
149
        }
150

    
151

    
152
        public async void RetrieveCredentials()
153
        {
154
            SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page");
155
            IsConfirmed = false;
156

    
157
            try
158
            {
159
                var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl);
160
                AccountName = credentials.UserName;
161
                Token = credentials.Password;
162

    
163
                IsConfirmed = true;
164
            }
165
            catch (Exception exc)
166
            {
167
                IsConfirmed = false;
168
                MessageBox.Show(exc.ToString(), "Error");
169
                throw;
170
            }
171
            finally
172
            {
173
                ClearBusy();
174
                
175
                (this.GetView() as Window).Activate();
176
            }
177

    
178
        }
179

    
180
        public AddAccountViewModel()
181
        {
182
            _servers=new List<string>
183
                         {
184
                             Settings.Default.ProductionServer, 
185
                             Settings.Default.DevelopmentServer
186
                         };
187
            CurrentServer = _servers[0];
188
        }
189

    
190
        private bool _hasValidCredentials;
191
        public bool HasValidCredentials
192
        {
193
            get { return _hasValidCredentials; }
194
            set
195
            {
196
                _hasValidCredentials = value;
197
                NotifyOfPropertyChange(()=>HasValidCredentials);
198
            }
199
        }
200

    
201
        private string _validationMessage;
202
        public string ValidationMessage
203
        {
204
            get { return _validationMessage; }
205
            set
206
            {
207
                _validationMessage = value;
208
                NotifyOfPropertyChange(()=>ValidationMessage);
209
            }
210
        }
211

    
212
        private bool _isWorking;
213
        public bool IsWorking
214
        {
215
            get { return _isWorking; }
216
            set
217
            {
218
                _isWorking = value;
219
                NotifyOfPropertyChange(()=>IsWorking);
220
            }
221
        }
222

    
223
        private string _busyTitle;
224
        public string BusyTitle
225
        {
226
            get { return _busyTitle; }
227
            set
228
            {
229
                _busyTitle = value;
230
                NotifyOfPropertyChange(()=>BusyTitle);
231
            }
232
        }
233

    
234
        private string _busyDetail;
235
        public string BusyDetail
236
        {
237
            get { return _busyDetail; }
238
            set
239
            {
240
                _busyDetail = value;
241
                NotifyOfPropertyChange(()=>BusyDetail);
242
            }
243
        }
244

    
245
        private void SetBusy(string title,string detail)
246
        {
247
            IsWorking = true;
248
            BusyTitle = title;
249
            BusyDetail = detail;
250
        }
251

    
252
        private void ClearBusy()
253
        {
254
            IsWorking = false;
255
            BusyTitle = "";
256
            BusyDetail = "";
257
            
258
        }
259

    
260
        public async void TestAccount()
261
        {
262
            try
263
            {
264
                SetBusy("Validating Credentials", "");
265
                var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer };
266
                var containers = await TaskEx.Run(() =>
267
                                                      {
268
                                                          client.Authenticate();
269
                                                          return client.ListContainers(AccountName);
270
                                                      });
271
                HasValidCredentials = true;
272
                ValidationMessage = "Credentials Validated";
273
            }
274
/*
275
            catch (AggregateException exc)
276
            {
277
                exc.Handle(ex=>
278
                               {
279
                                   HasValidCredentials = false;
280
                                   MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);                                                   
281
                               });
282
            }
283
*/
284
            catch
285
            {
286
                HasValidCredentials = false;
287
                MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
288
                ValidationMessage = "Credentials validation failed";
289
            }
290
            finally
291
            {
292
                ClearBusy();
293
            }
294
        }
295

    
296
    }
297
}