Fixed error that caused continuous credential retrieval in Auto account page
[pithos-ms-client] / trunk / Pithos.Client.WPF / Preferences / AddAccountViewModel.cs
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
202         private bool _isWorking;
203         public bool IsWorking
204         {
205             get { return _isWorking; }
206             set
207             {
208                 _isWorking = value;
209                 NotifyOfPropertyChange(()=>IsWorking);
210             }
211         }
212
213         private string _busyTitle;
214         public string BusyTitle
215         {
216             get { return _busyTitle; }
217             set
218             {
219                 _busyTitle = value;
220                 NotifyOfPropertyChange(()=>BusyTitle);
221             }
222         }
223
224         private string _busyDetail;
225         public string BusyDetail
226         {
227             get { return _busyDetail; }
228             set
229             {
230                 _busyDetail = value;
231                 NotifyOfPropertyChange(()=>BusyDetail);
232             }
233         }
234
235         private void SetBusy(string title,string detail)
236         {
237             IsWorking = true;
238             BusyTitle = title;
239             BusyDetail = detail;
240         }
241
242         private void ClearBusy()
243         {
244             IsWorking = false;
245             BusyTitle = "";
246             BusyDetail = "";
247             
248         }
249
250         public async void TestAccount()
251         {
252             try
253             {
254                 SetBusy("Validating Credentials","");
255                 var client = new CloudFilesClient(AccountName, Token) {AuthenticationUrl = CurrentServer};                
256                 var containers = await TaskEx.Run(()=>
257                                                       {
258                                                           client.Authenticate();
259                                                           return client.ListContainers(AccountName);
260                                                       });
261                 HasValidCredentials = true;                
262             }
263             catch (Exception ex)
264             {
265                 HasValidCredentials = false;
266                 MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
267                 throw;
268             }
269             finally
270             {
271                 ClearBusy();
272             }
273         }
274
275     }
276 }