X-Git-Url: https://code.grnet.gr/git/pithos-ms-client/blobdiff_plain/42af59dcf12e2b6f97e396653f295d99243bd782..9ad59de30e7a0cf6c3341ea1b5d195150f19aba8:/trunk/Pithos.Client.WPF/Preferences/AddAccountViewModel.cs diff --git a/trunk/Pithos.Client.WPF/Preferences/AddAccountViewModel.cs b/trunk/Pithos.Client.WPF/Preferences/AddAccountViewModel.cs index 7a24433..f4f43b1 100644 --- a/trunk/Pithos.Client.WPF/Preferences/AddAccountViewModel.cs +++ b/trunk/Pithos.Client.WPF/Preferences/AddAccountViewModel.cs @@ -1,350 +1,395 @@ -#region -/* ----------------------------------------------------------------------- - * - * - * Copyright 2011-2012 GRNET S.A. All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the following - * disclaimer. - * - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * - * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and - * documentation are those of the authors and should not be - * interpreted as representing official policies, either expressed - * or implied, of GRNET S.A. - * - * ----------------------------------------------------------------------- - */ -#endregion -using System; -using System.Collections.Generic; -using System.ComponentModel.Composition; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Forms; -using Pithos.Client.WPF.Properties; -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 Settings.Default.Servers.Select(server => server.ServerUri).ToList(); } - } - - 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); - } - } - - - private bool _shouldCreateOkeanosFolder; - public bool ShouldCreateOkeanosFolder - { - get { return _shouldCreateOkeanosFolder; } - set - { - _shouldCreateOkeanosFolder = value; - NotifyOfPropertyChange(()=>ShouldCreateOkeanosFolder); - } - } - - public void SelectAccount() - { - using (var dlg = new FolderBrowserDialog{Description=Resources.AddAccountViewModel_SelectAccount_Please_select_a_folder}) - { - //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; - - ShouldCreateOkeanosFolder=Directory.EnumerateFileSystemEntries(AccountPath).Any(); - } - } - - - public void RetrieveCredentials() - { - SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page"); - IsConfirmed = false; - - try - { - var loginUri=new Uri(new Uri(CurrentServer) , "login"); - var credentials = PithosAccount.RetrieveCredentials(loginUri.ToString()); - if (credentials == null) - return; - AccountName = credentials.UserName; - Token = credentials.Password; - - IsConfirmed = true; - - } - catch (PithosException exc) - { - ClearBusy(); - MessageBox.Show(exc.Message, "Unable to retrieve credentials"); - } - catch (Exception exc) - { - IsConfirmed = false; - MessageBox.Show(exc.ToString(), "Error"); - throw; - } - finally - { - ClearBusy(); - - ((Window) GetView()).Activate(); - } - - if (IsConfirmed) - TaskEx.Run(TestAccount); - - } - - public AddAccountViewModel() - { - _servers=new List - { - Settings.Default.ProductionServer, - Settings.Default.DevelopmentServer - }; - CurrentServer = _servers[0]; - DisplayName = "Add Pithos+ Account"; - } - - 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,/*Proxy=Proxy */}; - await TaskEx.Run(() => - { - client.Authenticate(); - return client.ListContainers(AccountName); - }).ConfigureAwait(false); - HasValidCredentials = true; - ValidationMessage = "Credentials Validated"; - } - catch - { - HasValidCredentials = false; - MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop); - ValidationMessage = "Credentials validation failed"; - } - finally - { - ClearBusy(); - } - } - - } -} +#region +/* ----------------------------------------------------------------------- + * + * + * Copyright 2011-2012 GRNET S.A. All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * + * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and + * documentation are those of the authors and should not be + * interpreted as representing official policies, either expressed + * or implied, of GRNET S.A. + * + * ----------------------------------------------------------------------- + */ +#endregion +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Forms; +using Pithos.Client.WPF.Properties; +using Pithos.Interfaces; +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 Settings.Default.Servers.Select(server => server.ServerUri).ToList(); } + } + + 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); + } + } + + + private bool _shouldCreateOkeanosFolder; + public bool ShouldCreateOkeanosFolder + { + get { return _shouldCreateOkeanosFolder; } + set + { + _shouldCreateOkeanosFolder = value; + NotifyOfPropertyChange(()=>ShouldCreateOkeanosFolder); + } + } + + bool HasNoParentWithCache(DirectoryInfo info) + { + if (info == null) + return false; + var parent = info.Parent; + if (parent == null) + return true; + if (parent.EnumerateDirectories(FolderConstants.CacheFolder).Any()) + { + return false; + } + return HasNoParentWithCache(parent); + } + + string GetLowestAboveCache(string path) + { + if (String.IsNullOrWhiteSpace(path)) + return null; + var info = new DirectoryInfo(path); + if (!info.Exists) + return path; + while (!HasNoParentWithCache(info)) + { + info = info.Parent; + } + if (info == null) + return null; + else + return info.FullName; + } + + public void SelectAccount() + { + using (var dlg = new FolderBrowserDialog{Description=Resources.AddAccountViewModel_SelectAccount_Please_select_a_folder}) + { + //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; + + //If the selected path is under an account folder, ie a folder containint a .pithos.cache subfolder, try to locate + //the cache's parent folder + var selectedPath = GetLowestAboveCache(dlg.SelectedPath); + + //If the path ends with /okeanos#/pithos, replace with /okeanos# + var match = _okeanosRegex.Match(selectedPath); + if (match.Success) + { + selectedPath = match.Groups[1].Value; + } + + AccountPath= selectedPath; + + + + ShouldCreateOkeanosFolder=Directory.EnumerateFileSystemEntries(AccountPath).Any(); + } + } + + + public void RetrieveCredentials() + { + SetBusy("Waiting for credentials.", "Please enter your credentials in the Pithos logon page"); + IsConfirmed = false; + + try + { + var loginUri=new Uri(CurrentServer).Combine("login"); + var credentials = PithosAccount.RetrieveCredentials(loginUri.ToString()); + if (credentials == null) + return; + AccountName = credentials.UserName; + Token = credentials.Password; + + IsConfirmed = true; + + } + catch (PithosException exc) + { + ClearBusy(); + MessageBox.Show(exc.Message, "Unable to retrieve credentials"); + } + catch (Exception exc) + { + IsConfirmed = false; + MessageBox.Show(exc.ToString(), "Error"); + throw; + } + finally + { + ClearBusy(); + + ((Window) GetView()).Activate(); + } + + if (IsConfirmed) + TaskEx.Run(()=>TestAccount()); + + } + + public AddAccountViewModel() + { + _servers=new List + { + Settings.Default.ProductionServer, + Settings.Default.DevelopmentServer + }; + CurrentServer = _servers[0]; + DisplayName = "Add Pithos+ Account"; + } + + 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; + private Regex _okeanosRegex = new Regex(@"(.*okeanos[0-9]*)\\pithos$"); + + 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 Task TestAccount() + { + try + { + SetBusy("Validating Credentials", ""); + var client = new CloudFilesClient(AccountName, Token) { AuthenticationUrl = CurrentServer,/*Proxy=Proxy */}; + await client.Authenticate().ConfigureAwait(false); + await client.ListContainers(AccountName).ConfigureAwait(false); + HasValidCredentials = true; + ValidationMessage = "Credentials Validated"; + } + catch + { + HasValidCredentials = false; + MessageBox.Show("The account is not valid", "Account Error", MessageBoxButton.OK, MessageBoxImage.Stop); + ValidationMessage = "Credentials validation failed"; + } + finally + { + ClearBusy(); + } + } + + } +}