Revision f734ab5b trunk/Pithos.Client.WPF/Preferences/AddAccountViewModel.cs

b/trunk/Pithos.Client.WPF/Preferences/AddAccountViewModel.cs
5 5
using System.IO;
6 6
using System.Linq;
7 7
using System.Text;
8
using System.Threading.Tasks;
8 9
using System.Windows;
9 10
using System.Windows.Controls;
10 11
using System.Windows.Forms;
12
using Caliburn.Micro;
11 13
using Pithos.Client.WPF.Properties;
12 14
using Pithos.Core;
15
using Pithos.Network;
13 16
using MessageBox = System.Windows.MessageBox;
14 17
using Screen = Caliburn.Micro.Screen;
15 18

  
......
18 21
    [Export(typeof(AddAccountViewModel))]
19 22
    public class AddAccountViewModel:Screen
20 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

  
21 63
        private string _accountName;
22 64
        public string AccountName
23 65
        {
......
74 116
            set
75 117
            {
76 118
                _isConfirmed = value;
119
                HasValidCredentials = false;
77 120
                NotifyOfPropertyChange(() => IsConfirmed);
78 121
            }
79 122
        }
......
139 182
            IsRetrieving = false;
140 183

  
141 184
        }
142
        
185

  
186
        public AddAccountViewModel()
187
        {
188
            _servers=new List<string>
189
                         {
190
                             Settings.Default.ProductionServer, 
191
                             Settings.Default.DevelopmentServer
192
                         };
193
            CurrentServer = _servers[0];
194
        }
195

  
196
        private bool _hasValidCredentials;
197
        public bool HasValidCredentials
198
        {
199
            get { return _hasValidCredentials; }
200
            set
201
            {
202
                _hasValidCredentials = value;
203
                NotifyOfPropertyChange(()=>HasValidCredentials);
204
            }
205
        }
206

  
207

  
208
        private bool _isValidating;
209
        public bool IsValidating
210
        {
211
            get { return _isValidating; }
212
            set
213
            {
214
                _isValidating = value;
215
                NotifyOfPropertyChange(()=>IsValidating);
216
            }
217
        }
218

  
219
        public async void TestAccount()
220
        {
221
            try
222
            {
223
                IsValidating = true;
224
                var client = new CloudFilesClient(AccountName, Token) {AuthenticationUrl = CurrentServer};                
225
                var containers = await TaskEx.Run(()=>
226
                                                      {
227
                                                          client.Authenticate();
228
                                                          return client.ListContainers(AccountName);
229
                                                      });
230
                HasValidCredentials = true;                
231
            }
232
            catch (Exception ex)
233
            {
234
                HasValidCredentials = false;
235
                MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
236
                throw;
237
            }
238
            finally
239
            {
240
                IsValidating = false;
241
            }
242
        }
143 243

  
144 244
    }
145 245
}

Also available in: Unified diff