Changed MonitorAccount to call StartMonitor which handles reauthentication
[pithos-ms-client] / trunk / Pithos.Client.WPF / Shell / ShellViewModel.cs
index f3900b2..da7cf3c 100644 (file)
@@ -5,6 +5,7 @@ using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.IO;
 using System.Net;
+using System.Reflection;
 using System.Runtime.InteropServices;
 using System.ServiceModel;
 using System.ServiceModel.Description;
@@ -30,7 +31,7 @@ using StatusService = Pithos.Client.WPF.Services.StatusService;
 namespace Pithos.Client.WPF {
     using System.ComponentModel.Composition;
 
-    [Export(typeof(IShell))]
+    
        ///<summary>
        /// The "shell" of the Pithos application displays the taskbar  icon, menu and notifications.
        /// The shell also hosts the status service called by shell extensions to retrieve file info
@@ -43,6 +44,7 @@ namespace Pithos.Client.WPF {
        /// * ShowFilePropertiesEvent: Raised when a shell command requests the display of the file/container properties dialog
        ///</remarks>           
        //TODO: CODE SMELL Why does the shell handle the SelectiveSynchChanges?
+    [Export(typeof(IShell))]
     public class ShellViewModel : Screen, IStatusNotification, IShell,
         IHandle<Notification>, IHandle<SelectiveSynchChanges>, IHandle<ShowFilePropertiesEvent>
     {
@@ -103,6 +105,12 @@ namespace Pithos.Client.WPF {
 
                 StatusMessage = "In Synch";
 
+                _accounts.CollectionChanged += (sender, e) =>
+                                                   {
+                                                       NotifyOfPropertyChange(() => OpenFolderCaption);
+                                                       NotifyOfPropertyChange(() => HasAccounts);
+                                                   };
+
             }
             catch (Exception exc)
             {
@@ -111,6 +119,7 @@ namespace Pithos.Client.WPF {
             }
         }
 
+
         protected override void OnActivate()
         {
             base.OnActivate();
@@ -162,8 +171,8 @@ namespace Pithos.Client.WPF {
                     //If the account is active
                     if (account.IsActive)
                         //Start the monitor. It's OK to start an already started monitor,
-                        //it will just ignore the call
-                        monitor.Start();
+                        //it will just ignore the call                        
+                        StartMonitor(monitor).Wait();                        
                     else
                     {
                         //If the account is inactive
@@ -206,9 +215,9 @@ namespace Pithos.Client.WPF {
 
         protected override void OnViewLoaded(object view)
         {
-            var window = (Window)view;
-            window.Hide();
             UpdateStatus();
+            var window = (Window)view;            
+            TaskEx.Delay(1000).ContinueWith(t => Execute.OnUIThread(window.Hide));
             base.OnViewLoaded(view);
         }
 
@@ -226,14 +235,29 @@ namespace Pithos.Client.WPF {
             }
         }
 
-        private ObservableConcurrentCollection<AccountInfo> _accounts = new ObservableConcurrentCollection<AccountInfo>();
+        private readonly ObservableConcurrentCollection<AccountInfo> _accounts = new ObservableConcurrentCollection<AccountInfo>();
         public ObservableConcurrentCollection<AccountInfo> Accounts
         {
             get { return _accounts; }
         }
 
+           public bool HasAccounts
+           {
+            get { return _accounts.Count > 0; }
+           }
 
-        private string _pauseSyncCaption="Pause Syncing";
+
+        public string OpenFolderCaption
+        {
+            get
+            {
+                return (_accounts.Count == 0)
+                        ? "No Accounts Defined"
+                        : "Open Pithos Folder";
+            }
+        }
+
+        private string _pauseSyncCaption="Pause Synching";
         public string PauseSyncCaption
         {
             get { return _pauseSyncCaption; }
@@ -251,13 +275,13 @@ namespace Pithos.Client.WPF {
         }
 
 
-        private string _statusIcon="../Images/Tray.ico";
+        private string _statusIcon="../Images/Pithos.ico";
         public string StatusIcon
         {
             get { return _statusIcon; }
             set
             {
-                _statusIcon = value;
+                //_statusIcon = value;
                 NotifyOfPropertyChange(() => StatusIcon);
             }
         }
@@ -302,6 +326,12 @@ namespace Pithos.Client.WPF {
         }
 
         
+        public void GoToSite()
+        {            
+            var site = Properties.Settings.Default.PithosSite;
+            Process.Start(site);            
+        }
+
         public void GoToSite(AccountInfo account)
         {
             var site = String.Format("{0}/ui/?token={1}&user={2}",
@@ -334,11 +364,14 @@ namespace Pithos.Client.WPF {
             var account = pair.Key;
             var accountMonitor = pair.Value;
 
-            ObjectInfo info = accountMonitor.GetObjectInfo(filePath);
+            if (accountMonitor == null)
+                return;
+
+            var infoTask=Task.Factory.StartNew(()=>accountMonitor.GetObjectInfo(filePath));
 
             
 
-            var fileProperties = new FilePropertiesViewModel(this, info,filePath);
+            var fileProperties = new FilePropertiesViewModel(this, infoTask,filePath);
             _windowManager.ShowWindow(fileProperties);
         } 
         
@@ -365,7 +398,7 @@ namespace Pithos.Client.WPF {
                                    select monitor).FirstOrDefault();
             var account = pair.Key;
             var accountMonitor = pair.Value;            
-            ContainerInfo info = accountMonitor.GetContainerInfo(filePath);
+            var info = accountMonitor.GetContainerInfo(filePath);
 
             
 
@@ -445,7 +478,12 @@ namespace Pithos.Client.WPF {
             {
                 var info = iconNames[pithosStatus];
                 StatusIcon = String.Format(@"../Images/{0}.ico", info.IconName);
-                StatusMessage = String.Format("Pithos 1.0\r\n{0}", info.StatusText);
+
+                Assembly assembly = Assembly.GetExecutingAssembly();                               
+                var fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location);
+
+
+                StatusMessage = String.Format("Pithos {0}\r\n{1}", fileVersion.FileVersion,info.StatusText);
             }
             
             _events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info});
@@ -513,7 +551,7 @@ namespace Pithos.Client.WPF {
             try
             {
 
-                var credentials = await PithosAccount.RetrieveCredentialsAsync(Settings.PithosLoginUrl);
+                var credentials = await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl);
 
                 var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
                 account.ApiKey = credentials.Password;
@@ -521,6 +559,7 @@ namespace Pithos.Client.WPF {
                 Settings.Save();
                 await TaskEx.Delay(10000);
                 StartMonitor(monitor, retries + 1);
+                NotifyOfPropertyChange(()=>Accounts);
             }
             catch (AggregateException exc)
             {
@@ -610,6 +649,9 @@ namespace Pithos.Client.WPF {
             if (String.IsNullOrWhiteSpace(accountName))
                 return;
 
+            var accountInfo=_accounts.FirstOrDefault(account => account.UserName == accountName);
+            _accounts.TryRemove(accountInfo);
+
             PithosMonitor monitor;
             if (Monitors.TryGetValue(accountName, out monitor))
             {