commit 31c97141112604ef6dc1e4ffe5e25035d5fdb184
Author: Panagiotis Kanavos <pkanavos@gmail.com>
Date:   Wed Feb 29 22:16:02 2012 +0200

    Changes to hide the warning image when an account token is refreshed

diff --git a/trunk/Pithos.Client.WPF/Preferences/PreferencesViewModel.cs b/trunk/Pithos.Client.WPF/Preferences/PreferencesViewModel.cs
index 8ece12c..7017cf7 100644
--- a/trunk/Pithos.Client.WPF/Preferences/PreferencesViewModel.cs
+++ b/trunk/Pithos.Client.WPF/Preferences/PreferencesViewModel.cs
@@ -45,6 +45,7 @@
 
 using System.Collections.Concurrent;
 using System.ComponentModel.Composition;
+using System.Diagnostics;
 using System.IO;
 using System.Net;
 using System.Threading.Tasks;
@@ -69,6 +70,8 @@ namespace Pithos.Client.WPF.Preferences
     {
         private IEventAggregator _events;
 
+        //Logging in the Pithos client is provided by log4net
+        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos");
 
         private PithosSettings _settings;
         public PithosSettings Settings
@@ -81,8 +84,8 @@ namespace Pithos.Client.WPF.Preferences
             }
         }
 
-        private ObservableConcurrentCollection<AccountSettings> _accounts;
-        public ObservableConcurrentCollection<AccountSettings> Accounts
+        private ObservableConcurrentCollection<AccountViewModel> _accounts;
+        public ObservableConcurrentCollection<AccountViewModel> Accounts
         {
             get { return _accounts; }
             set 
@@ -106,13 +109,16 @@ namespace Pithos.Client.WPF.Preferences
             Shell = shell;
             
             Settings=settings;
-            Accounts = new ObservableConcurrentCollection<AccountSettings>();
+            Accounts = new ObservableConcurrentCollection<AccountViewModel>();
             if (settings.Accounts == null)
             {
                 settings.Accounts=new AccountsCollection();
                 settings.Save();
             }
-            Accounts.AddFromEnumerable(settings.Accounts);
+            var accountVMs = from account in settings.Accounts
+                             select new AccountViewModel(account);
+
+            Accounts.AddFromEnumerable(accountVMs);
             
             var startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
             _shortcutPath = Path.Combine(startupPath, "Pithos.lnk");
@@ -205,7 +211,7 @@ namespace Pithos.Client.WPF.Preferences
             var monitor = Shell.Monitors[CurrentAccount.AccountName];
             
 
-            var model = new SelectiveSynchViewModel(monitor,_events,CurrentAccount);
+            var model = new SelectiveSynchViewModel(monitor,_events,CurrentAccount.Account);
             if (_windowManager.ShowDialog(model) == true)
             {
                 
@@ -213,10 +219,40 @@ namespace Pithos.Client.WPF.Preferences
         }
 
         public async Task RefreshApiKey()
-        {            
-            await Shell.TryAuthorize(CurrentAccount.AccountName, 3);
-            NotifyOfPropertyChange(()=>CurrentAccount);
+        {
+            _events.Publish(new Notification { Title = "Authorization failed", Message = "Your API Key has probably expired. You will be directed to a page where you can renew it", Level = TraceLevel.Error });
+
+            //var userName = CurrentAccount.AccountName;
+            try
+            {
+
+                var credentials = await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl);
+
+                var account = Accounts.First(act => act.AccountName == credentials.UserName);
+                //The server may return credentials for a different account
+                var monitor =  Shell.Monitors[account.AccountName];
+                account.ApiKey = credentials.Password;
+                monitor.ApiKey = credentials.Password;
+                account.IsExpired = false;
+                Settings.Save();
+                TaskEx.Delay(10000).ContinueWith(_ =>Shell.MonitorAccount(account.Account));
+                NotifyOfPropertyChange(() => Accounts);
+            }
+            catch (AggregateException exc)
+            {
+                string message = String.Format("API Key retrieval failed");
+                Log.Error(message, exc.InnerException);
+                _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error });
+            }
+            catch (Exception exc)
+            {
+                string message = String.Format("API Key retrieval failed");
+                Log.Error(message, exc);
+                _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error });
+            }
+
         }
+
     
         public void SaveChanges()
         {
@@ -298,8 +334,9 @@ namespace Pithos.Client.WPF.Preferences
                                         IsActive=wizard.IsAccountActive
                                     };
                Settings.Accounts.Add(newAccount);
-               (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(newAccount);
-               CurrentAccount = newAccount;
+               var accountVM = new AccountViewModel(newAccount);
+               (Accounts as IProducerConsumerCollection<AccountViewModel>).TryAdd(accountVM);
+               CurrentAccount = accountVM;
                NotifyOfPropertyChange(() => Accounts);
                NotifyOfPropertyChange(() => Settings);   
            }
@@ -312,6 +349,7 @@ namespace Pithos.Client.WPF.Preferences
        {
             var credentials=await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl);
             var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
+            var accountVM = new AccountViewModel(account);
             if (account == null)
             {
                 account=new AccountSettings{
@@ -319,14 +357,15 @@ namespace Pithos.Client.WPF.Preferences
                     ApiKey=credentials.Password
                 };
                 Settings.Accounts.Add(account);
-                (Accounts as IProducerConsumerCollection<AccountSettings>).TryAdd(account);
+                accountVM = new AccountViewModel(account);
+                (Accounts as IProducerConsumerCollection<AccountViewModel>).TryAdd(accountVM);
             }
             else
             {
                 account.ApiKey=credentials.Password;
             }
             //SelectedAccountIndex= Settings.Accounts.IndexOf(account);
-            CurrentAccount = account;
+            CurrentAccount = accountVM;
             NotifyOfPropertyChange(() => Accounts);
             NotifyOfPropertyChange(()=>Settings);                       
        }
@@ -334,7 +373,7 @@ namespace Pithos.Client.WPF.Preferences
         public void RemoveAccount()
         {
             var accountName = CurrentAccount.AccountName;
-            Settings.Accounts.Remove(CurrentAccount);
+            Settings.Accounts.Remove(CurrentAccount.Account);
 
             Accounts.TryRemove(CurrentAccount);
             
@@ -401,13 +440,13 @@ namespace Pithos.Client.WPF.Preferences
             }
         }*/
 
-        private AccountSettings _currentAccount;
+        private AccountViewModel _currentAccount;
         private IWindowManager _windowManager;
         private string _shortcutPath;
 
 
         
-        public AccountSettings CurrentAccount
+        public AccountViewModel CurrentAccount
         {
             get { return _currentAccount; }
             set
@@ -494,7 +533,7 @@ namespace Pithos.Client.WPF.Preferences
                     monitor.Start();
             }
             else
-                Shell.MonitorAccount(CurrentAccount);
+                Shell.MonitorAccount(CurrentAccount.Account);
             //Finally, notify that the Settings, CurrentAccount have changed
             NotifyOfPropertyChange(() => CurrentAccount);
             NotifyOfPropertyChange(() => Settings);
