Increased the number of retries before abandoning StartMonitor
[pithos-ms-client] / trunk / Pithos.Client.WPF / Shell / ShellViewModel.cs
index 7700a81..897d7d7 100644 (file)
@@ -47,9 +47,11 @@ using System.Net;
 using System.Reflection;
 using System.Runtime.InteropServices;
 using System.ServiceModel;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls.Primitives;
+using System.Windows.Input;
 using AppLimit.NetSparkle;
 using Caliburn.Micro;
 using Hardcodet.Wpf.TaskbarNotification;
@@ -71,7 +73,27 @@ using StatusService = Pithos.Client.WPF.Services.StatusService;
 namespace Pithos.Client.WPF {
        using System.ComponentModel.Composition;
 
-       
+       public class ToggleStatusCommand:ICommand
+       {
+           private readonly ShellViewModel _model;
+           public ToggleStatusCommand(ShellViewModel model)
+           {
+               _model = model;
+           }
+           public void Execute(object parameter)
+           {
+               _model.CurrentSyncStatus();
+           }
+
+           public bool CanExecute(object parameter)
+           {
+               return true;
+           }
+
+           public event EventHandler CanExecuteChanged;
+       }
+
+
        ///<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
@@ -84,14 +106,11 @@ 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)), Export(typeof(ShellViewModel))]
+    [Export(typeof(IShell)), Export(typeof(ShellViewModel)),Export(typeof(IStatusNotification))]
        public class ShellViewModel : Screen, IStatusNotification, IShell,
                IHandle<Notification>, IHandle<SelectiveSynchChanges>, IHandle<ShowFilePropertiesEvent>
        {
-
-               //The Status Checker provides the current synch state
-               //TODO: Could we remove the status checker and use events in its place?
-               private readonly IStatusChecker _statusChecker;
+               
                private readonly IEventAggregator _events;
 
                public PithosSettings Settings { get; private set; }
@@ -122,12 +141,18 @@ namespace Pithos.Client.WPF {
                //Logging in the Pithos client is provided by log4net
         private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
 
-           private readonly PollAgent _pollAgent;
-           private readonly NetworkAgent _networkAgent;
+        [Import]
+           private PollAgent _pollAgent;
+
+        [Import]
+           private NetworkAgent _networkAgent;
 
            [Import]
            public Selectives Selectives { get; set; }
 
+
+           public ToggleStatusCommand ToggleMiniStatusCommand { get; set; }
+
            private MiniStatusViewModel _miniStatus;
 
            [Import]
@@ -153,21 +178,22 @@ namespace Pithos.Client.WPF {
                /// The PithosSettings class encapsulates the app's settings to abstract their storage mechanism (App settings, a database or registry)
                ///</remarks>
                [ImportingConstructor]          
-               public ShellViewModel(IWindowManager windowManager, IEventAggregator events, IStatusChecker statusChecker, PithosSettings settings,PollAgent pollAgent,NetworkAgent networkAgent)
+               public ShellViewModel(IWindowManager windowManager, IEventAggregator events, PithosSettings settings/*,PollAgent pollAgent,NetworkAgent networkAgent*/)
                {
                        try
                        {
 
                                _windowManager = windowManager;
                                //CHECK: Caliburn doesn't need explicit command construction
-                               //OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
-                               _statusChecker = statusChecker;
+                               //CurrentSyncStatusCommand = new PithosCommand(OpenPithosFolder);
                                //The event subst
                                _events = events;
                                _events.Subscribe(this);
 
+/*
                            _pollAgent = pollAgent;
                            _networkAgent = networkAgent;
+*/
                                Settings = settings;
 
                                Proxy.SetFromSettings(settings);
@@ -183,6 +209,8 @@ namespace Pithos.Client.WPF {
                                                                                                   };
 
                 SetVersionMessage();
+
+                ToggleMiniStatusCommand=new ToggleStatusCommand(this);
                        }
                        catch (Exception exc)
                        {
@@ -208,13 +236,16 @@ namespace Pithos.Client.WPF {
             else
             {
                 if (!_statusVisible)
+                {
                     _windowManager.ShowWindow(MiniStatus);
+                    _statusVisible = true;
+                }
                 else
                 {
                     if (MiniStatus.IsActive)
                         MiniStatus.TryClose();
+                    _statusVisible = false;
                 }
-                _statusVisible = !_statusVisible;
 
                 NotifyOfPropertyChange(() => MiniStatusCaption);
             }
@@ -286,7 +317,7 @@ namespace Pithos.Client.WPF {
                 }
                     
                                var accounts = Settings.Accounts.Select(MonitorAccount);
-                               await TaskEx.WhenAll(accounts);
+                await TaskEx.WhenAll(accounts).ConfigureAwait(false);
                                _statusService = StatusService.Start();
 
                        }
@@ -422,17 +453,17 @@ namespace Pithos.Client.WPF {
         {
             get
             {
-                return String.Format("{0}\r\n{1}", "Status Window", "Enable / Dissable the stuatus window");
+                return String.Format("{0}\r\n{1}", "Status Window", "Enable / Disable the status window");
             }
         }
 
-        public string ToggleStatusWindowMessage
+        /*public string ToggleStatusWindowMessage
         {
             get
             {
                 return String.Format("{0}" + Environment.NewLine + "{1} Toggle Mini Status");
             }
-        }
+        }*/
 
            private readonly ObservableConcurrentCollection<AccountInfo> _accounts = new ObservableConcurrentCollection<AccountInfo>();
                public ObservableConcurrentCollection<AccountInfo> Accounts
@@ -504,9 +535,13 @@ namespace Pithos.Client.WPF {
                {
                        //Settings.Reload();
             
-                   var preferences = new PreferencesViewModel(_windowManager, _events, this, Settings,currentTab);
-                   _windowManager.ShowDialog(preferences);
-                       
+                   var preferences = IoC.Get<PreferencesViewModel>();//??new PreferencesViewModel(_windowManager, _events, this, Settings,currentTab);
+            if (!String.IsNullOrWhiteSpace(currentTab))
+                preferences.SelectedTab = currentTab;
+            if (!preferences.IsActive)
+                       _windowManager.ShowWindow(preferences);
+            var view = (Window)preferences.GetView();
+            view.NullSafe(v=>v.Activate());
                }
 
                public void AboutPithos()
@@ -868,7 +903,7 @@ namespace Pithos.Client.WPF {
 
                private bool AbandonRetry(PithosMonitor monitor, int retries)
                {
-                       if (retries > 1)
+                       if (retries > 3)
                        {
                                var message = String.Format("Monitoring of account {0} has failed too many times. Will not retry",
                                                                                        monitor.UserName);
@@ -953,7 +988,7 @@ namespace Pithos.Client.WPF {
 
                public void Notify(Notification notification)
                {
-                       _events.Publish(notification);
+            TaskEx.Run(()=> _events.Publish(notification));
                }
 
 
@@ -1024,8 +1059,8 @@ namespace Pithos.Client.WPF {
                        var account = Accounts.FirstOrDefault(acc => acc.AccountKey == message.Account.AccountKey);
                        if (account != null)
                        {
-                           _pollAgent.SetSelectivePaths(account, message.Added, message.Removed);
-                    _pollAgent.SynchNow();
+                           var added=monitor.UrisToFilePaths(message.Added);
+                    _pollAgent.SynchNow(added);
                        }
                    });
 
@@ -1076,7 +1111,7 @@ namespace Pithos.Client.WPF {
                    {
                        StatusMessage = String.Format("{0} {1:p2} of {2} - {3}",                                                      
                                               progress.Action,
-                                                     progress.Block/(double)progress.TotalBlocks,
+                                                     (progress.Block + progress.BlockPercentage/100.0)/(double)progress.TotalBlocks,
                                                      progress.FileSize.ToByteSize(),
                                                      progress.FileName);
                        return;