2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.ComponentModel.Composition;
8 using System.Threading.Tasks;
10 using System.Windows.Controls;
11 using System.Windows.Forms;
13 using Pithos.Client.WPF.Properties;
16 using MessageBox = System.Windows.MessageBox;
17 using Screen = Caliburn.Micro.Screen;
19 namespace Pithos.Client.WPF.Preferences
21 [Export(typeof(AddAccountViewModel))]
22 public class AddAccountViewModel:Screen
25 private readonly List<string> _servers;
27 public List<string> Servers
29 get { return _servers; }
32 private bool _isValidServer;
33 public bool IsValidServer
35 get { return _isValidServer; }
38 _isValidServer = value;
39 NotifyOfPropertyChange(()=>IsValidServer);
44 private string _currentServer;
45 public string CurrentServer
47 get { return _currentServer; }
50 if (!Uri.IsWellFormedUriString(value, UriKind.Absolute))
52 IsValidServer = false;
53 throw new UriFormatException();
55 _currentServer = value;
57 HasValidCredentials = false;
59 NotifyOfPropertyChange(()=>CurrentServer);
63 private string _accountName;
64 public string AccountName
66 get { return _accountName; }
70 NotifyOfPropertyChange(()=>AccountName);
71 NotifyOfPropertyChange(() => HasCredentials);
75 private string _token;
78 get { return _token; }
82 NotifyOfPropertyChange(()=>Token);
83 NotifyOfPropertyChange(() => HasCredentials);
87 private string _accountPath;
88 public string AccountPath
90 get { return _accountPath; }
94 NotifyOfPropertyChange(() => AccountPath);
95 NotifyOfPropertyChange(() => HasAccountPath);
100 public bool HasAccountPath
102 get { return !String.IsNullOrWhiteSpace(AccountPath); }
105 public bool HasCredentials
107 get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; }
111 private bool _isConfirmed;
113 public bool IsConfirmed
115 get { return _isConfirmed; }
118 _isConfirmed = value;
119 HasValidCredentials = false;
120 NotifyOfPropertyChange(() => IsConfirmed);
125 private bool _isAccountActive;
127 public bool IsAccountActive
129 get { return _isAccountActive; }
132 _isAccountActive = value;
133 NotifyOfPropertyChange(() => IsAccountActive);
137 public void SelectAccount()
139 using (var dlg = new FolderBrowserDialog())
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)))
147 AccountPath= dlg.SelectedPath;
152 public async void RetrieveCredentials()
154 SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page");
159 var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl);
160 AccountName = credentials.UserName;
161 Token = credentials.Password;
165 catch (Exception exc)
168 MessageBox.Show(exc.ToString(), "Error");
175 (this.GetView() as Window).Activate();
180 public AddAccountViewModel()
182 _servers=new List<string>
184 Settings.Default.ProductionServer,
185 Settings.Default.DevelopmentServer
187 CurrentServer = _servers[0];
190 private bool _hasValidCredentials;
191 public bool HasValidCredentials
193 get { return _hasValidCredentials; }
196 _hasValidCredentials = value;
197 NotifyOfPropertyChange(()=>HasValidCredentials);
202 private bool _isWorking;
203 public bool IsWorking
205 get { return _isWorking; }
209 NotifyOfPropertyChange(()=>IsWorking);
213 private string _busyTitle;
214 public string BusyTitle
216 get { return _busyTitle; }
220 NotifyOfPropertyChange(()=>BusyTitle);
224 private string _busyDetail;
225 public string BusyDetail
227 get { return _busyDetail; }
231 NotifyOfPropertyChange(()=>BusyDetail);
235 private void SetBusy(string title,string detail)
242 private void ClearBusy()
250 public async void TestAccount()
254 SetBusy("Validating Credentials","");
255 var client = new CloudFilesClient(AccountName, Token) {AuthenticationUrl = CurrentServer};
256 var containers = await TaskEx.Run(()=>
258 client.Authenticate();
259 return client.ListContainers(AccountName);
261 HasValidCredentials = true;
265 HasValidCredentials = false;
266 MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop);