2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.ComponentModel.Composition;
9 using System.Threading.Tasks;
11 using System.Windows.Controls;
12 using System.Windows.Forms;
14 using Pithos.Client.WPF.Properties;
17 using MessageBox = System.Windows.MessageBox;
18 using Screen = Caliburn.Micro.Screen;
20 namespace Pithos.Client.WPF.Preferences
22 [Export(typeof(AddAccountViewModel))]
23 public class AddAccountViewModel:Screen
26 private readonly List<string> _servers;
28 public List<string> Servers
30 get { return _servers; }
33 private bool _isValidServer;
34 public bool IsValidServer
36 get { return _isValidServer; }
39 _isValidServer = value;
40 NotifyOfPropertyChange(()=>IsValidServer);
45 private string _currentServer;
46 public string CurrentServer
48 get { return _currentServer; }
51 if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
53 IsValidServer = false;
54 throw new UriFormatException();
56 _currentServer = value;
58 HasValidCredentials = false;
60 NotifyOfPropertyChange(()=>CurrentServer);
64 private string _accountName;
65 public string AccountName
67 get { return _accountName; }
71 NotifyOfPropertyChange(()=>AccountName);
72 NotifyOfPropertyChange(() => HasCredentials);
76 private string _token;
79 get { return _token; }
83 NotifyOfPropertyChange(()=>Token);
84 NotifyOfPropertyChange(() => HasCredentials);
88 private string _accountPath;
89 public string AccountPath
91 get { return _accountPath; }
95 NotifyOfPropertyChange(() => AccountPath);
96 NotifyOfPropertyChange(() => HasAccountPath);
101 public bool HasAccountPath
103 get { return !String.IsNullOrWhiteSpace(AccountPath); }
106 public bool HasCredentials
108 get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
112 private bool _isConfirmed;
114 public bool IsConfirmed
116 get { return _isConfirmed; }
119 _isConfirmed = value;
120 HasValidCredentials = false;
121 NotifyOfPropertyChange(() => IsConfirmed);
126 private bool _isAccountActive;
128 public bool IsAccountActive
130 get { return _isAccountActive; }
133 _isAccountActive = value;
134 NotifyOfPropertyChange(() => IsAccountActive);
138 public void SelectAccount()
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."})
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)))
148 AccountPath= dlg.SelectedPath;
153 public async void RetrieveCredentials()
155 SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page");
160 var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl);
161 AccountName = credentials.UserName;
162 Token = credentials.Password;
166 catch (PithosException exc)
169 MessageBox.Show(exc.Message, "Unable to retrieve credentials");
171 catch (Exception exc)
174 MessageBox.Show(exc.ToString(), "Error");
181 (this.GetView() as Window).Activate();
186 public AddAccountViewModel()
188 _servers=new List<string>
190 Settings.Default.ProductionServer,
191 Settings.Default.DevelopmentServer
193 CurrentServer = _servers[0];
196 private bool _hasValidCredentials;
197 public bool HasValidCredentials
199 get { return _hasValidCredentials; }
202 _hasValidCredentials = value;
203 NotifyOfPropertyChange(()=>HasValidCredentials);
207 private string _validationMessage;
208 public string ValidationMessage
210 get { return _validationMessage; }
213 _validationMessage = value;
214 NotifyOfPropertyChange(()=>ValidationMessage);
218 private bool _isWorking;
219 public bool IsWorking
221 get { return _isWorking; }
225 NotifyOfPropertyChange(()=>IsWorking);
229 private string _busyTitle;
230 public string BusyTitle
232 get { return _busyTitle; }
236 NotifyOfPropertyChange(()=>BusyTitle);
240 private string _busyDetail;
241 public string BusyDetail
243 get { return _busyDetail; }
247 NotifyOfPropertyChange(()=>BusyDetail);
251 private void SetBusy(string title,string detail)
258 private void ClearBusy()
266 public async void TestAccount()
270 SetBusy("Validating Credentials", "");
271 var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer,/*Proxy=Proxy */};
272 var containers = await TaskEx.Run(() =>
274 client.Authenticate();
275 return client.ListContainers(AccountName);
277 HasValidCredentials = true;
278 ValidationMessage = "Credentials Validated";
281 catch (AggregateException exc)
285 HasValidCredentials = false;
286 MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
292 HasValidCredentials = false;
293 MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);
294 ValidationMessage = "Credentials validation failed";