Simplified proxy management code
[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.Net;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Windows;
11 using System.Windows.Controls;
12 using System.Windows.Forms;
13 using Caliburn.Micro;
14 using Pithos.Client.WPF.Properties;
15 using Pithos.Core;
16 using Pithos.Network;
17 using MessageBox = System.Windows.MessageBox;
18 using Screen = Caliburn.Micro.Screen;
19
20 namespace Pithos.Client.WPF.Preferences
21 {
22     [Export(typeof(AddAccountViewModel))]
23     public class AddAccountViewModel:Screen
24     {
25
26         private readonly List<string> _servers;
27
28         public List<string> Servers
29         {
30             get { return _servers; }
31         }
32
33         private bool _isValidServer;
34         public bool IsValidServer
35         {
36             get { return _isValidServer; }
37             set
38             {
39                 _isValidServer = value;
40                 NotifyOfPropertyChange(()=>IsValidServer);
41             }
42         }
43
44
45         private string _currentServer;
46         public string CurrentServer
47         {
48             get { return _currentServer; }
49             set
50             {
51                 if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
52                 {
53                     IsValidServer = false;
54                     throw new UriFormatException();
55                 }
56                 _currentServer = value;
57                 IsValidServer = true;
58                 HasValidCredentials = false;
59                 IsConfirmed = false;
60                 NotifyOfPropertyChange(()=>CurrentServer);
61             }
62         }
63
64         private string _accountName;
65         public string AccountName
66         {
67             get { return _accountName; }
68             set
69             {
70                 _accountName = value;
71                 NotifyOfPropertyChange(()=>AccountName);
72                 NotifyOfPropertyChange(() => HasCredentials);
73             }
74         }
75
76         private string _token;
77         public string Token
78         {
79             get { return _token; }
80             set
81             {
82                 _token = value;
83                 NotifyOfPropertyChange(()=>Token);
84                 NotifyOfPropertyChange(() => HasCredentials);
85             }
86         }
87
88         private string _accountPath;
89         public string AccountPath
90         {
91             get { return _accountPath; }
92             set
93             {
94                 _accountPath = value;
95                 NotifyOfPropertyChange(() => AccountPath);
96                 NotifyOfPropertyChange(() => HasAccountPath);
97             }
98         }
99
100
101         public bool HasAccountPath
102         {
103             get { return !String.IsNullOrWhiteSpace(AccountPath); }
104         }
105
106         public bool HasCredentials
107         {
108             get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
109         }
110
111
112         private bool  _isConfirmed;
113
114         public bool IsConfirmed
115         {
116             get { return _isConfirmed; }
117             set
118             {
119                 _isConfirmed = value;
120                 HasValidCredentials = false;
121                 NotifyOfPropertyChange(() => IsConfirmed);
122             }
123         }
124
125
126         private bool _isAccountActive;
127
128         public bool IsAccountActive
129         {
130             get { return _isAccountActive; }
131             set
132             {
133                 _isAccountActive = value;
134                 NotifyOfPropertyChange(() => IsAccountActive);
135             }
136         }
137
138         public void SelectAccount()
139         {
140             using (var dlg = new FolderBrowserDialog{Description="Please select a folder to store local files. Pithos will create a new folder called Okeanos under the folder you specify."})
141             {
142                 //Ask the user to select a folder
143                 //Note: We need a parent window here, which we retrieve with GetView            
144                 var view = (Window)GetView();
145                 if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view)))
146                     return;
147
148                 AccountPath= dlg.SelectedPath;
149             }
150         }
151
152
153         public async void RetrieveCredentials()
154         {
155             SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page");
156             IsConfirmed = false;
157
158             try
159             {
160                 var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl);
161                 AccountName = credentials.UserName;
162                 Token = credentials.Password;
163
164                 IsConfirmed = true;
165             }
166             catch (PithosException exc)
167             {
168                 ClearBusy();
169                 MessageBox.Show(exc.Message, "Unable to retrieve credentials");
170             }
171             catch (Exception exc)
172             {
173                 IsConfirmed = false;
174                 MessageBox.Show(exc.ToString(), "Error");
175                 throw;
176             }
177             finally
178             {
179                 ClearBusy();
180                 
181                 (this.GetView() as Window).Activate();
182             }
183
184         }
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         private string _validationMessage;
208         public string ValidationMessage
209         {
210             get { return _validationMessage; }
211             set
212             {
213                 _validationMessage = value;
214                 NotifyOfPropertyChange(()=>ValidationMessage);
215             }
216         }
217
218         private bool _isWorking;
219         public bool IsWorking
220         {
221             get { return _isWorking; }
222             set
223             {
224                 _isWorking = value;
225                 NotifyOfPropertyChange(()=>IsWorking);
226             }
227         }
228
229         private string _busyTitle;
230         public string BusyTitle
231         {
232             get { return _busyTitle; }
233             set
234             {
235                 _busyTitle = value;
236                 NotifyOfPropertyChange(()=>BusyTitle);
237             }
238         }
239
240         private string _busyDetail;
241         public string BusyDetail
242         {
243             get { return _busyDetail; }
244             set
245             {
246                 _busyDetail = value;
247                 NotifyOfPropertyChange(()=>BusyDetail);
248             }
249         }
250
251         private void SetBusy(string title,string detail)
252         {
253             IsWorking = true;
254             BusyTitle = title;
255             BusyDetail = detail;
256         }
257
258         private void ClearBusy()
259         {
260             IsWorking = false;
261             BusyTitle = "";
262             BusyDetail = "";
263             
264         }
265
266         public async void TestAccount()
267         {
268             try
269             {
270                 SetBusy("Validating Credentials", "");
271                 var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer,/*Proxy=Proxy */};                
272                 var containers = await TaskEx.Run(() =>
273                                                       {
274                                                           client.Authenticate();
275                                                           return client.ListContainers(AccountName);
276                                                       });
277                 HasValidCredentials = true;
278                 ValidationMessage = "Credentials Validated";
279             }
280 /*
281             catch (AggregateException exc)
282             {
283                 exc.Handle(ex=>
284                                {
285                                    HasValidCredentials = false;
286                                    MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);                                                   
287                                });
288             }
289 */
290             catch
291             {
292                 HasValidCredentials = false;
293                 MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
294                 ValidationMessage = "Credentials validation failed";
295             }
296             finally
297             {
298                 ClearBusy();
299             }
300         }
301
302     }
303 }