using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.Composition; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using Caliburn.Micro; using Pithos.Client.WPF.Properties; using Pithos.Core; using Pithos.Network; using MessageBox = System.Windows.MessageBox; using Screen = Caliburn.Micro.Screen; namespace Pithos.Client.WPF.Preferences { [Export(typeof(AddAccountViewModel))] public class AddAccountViewModel:Screen { private readonly List _servers; public List Servers { get { return _servers; } } private bool _isValidServer; public bool IsValidServer { get { return _isValidServer; } set { _isValidServer = value; NotifyOfPropertyChange(()=>IsValidServer); } } private string _currentServer; public string CurrentServer { get { return _currentServer; } set { if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) { IsValidServer = false; throw new UriFormatException(); } _currentServer = value; IsValidServer = true; HasValidCredentials = false; IsConfirmed = false; NotifyOfPropertyChange(()=>CurrentServer); } } private string _accountName; public string AccountName { get { return _accountName; } set { _accountName = value; NotifyOfPropertyChange(()=>AccountName); NotifyOfPropertyChange(() => HasCredentials); } } private string _token; public string Token { get { return _token; } set { _token = value; NotifyOfPropertyChange(()=>Token); NotifyOfPropertyChange(() => HasCredentials); } } private string _accountPath; public string AccountPath { get { return _accountPath; } set { _accountPath = value; NotifyOfPropertyChange(() => AccountPath); NotifyOfPropertyChange(() => HasAccountPath); } } public bool HasAccountPath { get { return !String.IsNullOrWhiteSpace(AccountPath); } } public bool HasCredentials { get { return !(String.IsNullOrWhiteSpace(AccountName) || String.IsNullOrWhiteSpace(Token) ) ; } } private bool _isConfirmed; public bool IsConfirmed { get { return _isConfirmed; } set { _isConfirmed = value; HasValidCredentials = false; NotifyOfPropertyChange(() => IsConfirmed); } } private bool _isAccountActive; public bool IsAccountActive { get { return _isAccountActive; } set { _isAccountActive = value; NotifyOfPropertyChange(() => IsAccountActive); } } public void SelectAccount() { using (var dlg = new FolderBrowserDialog()) { //Ask the user to select a folder //Note: We need a parent window here, which we retrieve with GetView var view = (Window)GetView(); if (DialogResult.OK != dlg.ShowDialog(new Wpf32Window(view))) return; AccountPath= dlg.SelectedPath; } } public async void RetrieveCredentials() { SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page"); IsConfirmed = false; try { var credentials = await PithosAccount.RetrieveCredentials(Settings.Default.PithosLoginUrl); AccountName = credentials.UserName; Token = credentials.Password; IsConfirmed = true; } catch (Exception exc) { IsConfirmed = false; MessageBox.Show(exc.ToString(), "Error"); throw; } finally { ClearBusy(); (this.GetView() as Window).Activate(); } } public AddAccountViewModel() { _servers=new List { Settings.Default.ProductionServer, Settings.Default.DevelopmentServer }; CurrentServer = _servers[0]; } private bool _hasValidCredentials; public bool HasValidCredentials { get { return _hasValidCredentials; } set { _hasValidCredentials = value; NotifyOfPropertyChange(()=>HasValidCredentials); } } private string _validationMessage; public string ValidationMessage { get { return _validationMessage; } set { _validationMessage = value; NotifyOfPropertyChange(()=>ValidationMessage); } } private bool _isWorking; public bool IsWorking { get { return _isWorking; } set { _isWorking = value; NotifyOfPropertyChange(()=>IsWorking); } } private string _busyTitle; public string BusyTitle { get { return _busyTitle; } set { _busyTitle = value; NotifyOfPropertyChange(()=>BusyTitle); } } private string _busyDetail; public string BusyDetail { get { return _busyDetail; } set { _busyDetail = value; NotifyOfPropertyChange(()=>BusyDetail); } } private void SetBusy(string title,string detail) { IsWorking = true; BusyTitle = title; BusyDetail = detail; } private void ClearBusy() { IsWorking = false; BusyTitle = ""; BusyDetail = ""; } public async void TestAccount() { try { SetBusy("Validating Credentials", ""); var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer }; var containers = await TaskEx.Run(() => { client.Authenticate(); return client.ListContainers(AccountName); }); HasValidCredentials = true; ValidationMessage = "Credentials Validated"; } /* catch (AggregateException exc) { exc.Handle(ex=> { HasValidCredentials = false; MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop); }); } */ catch { HasValidCredentials = false; MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop); ValidationMessage = "Credentials validation failed"; } finally { ClearBusy(); } } } }