Moved SQLite dlls to separate 32/64 folders in Libraries
[pithos-ms-client] / trunk / Pithos.Client.WPF / TaskbarViewModel.cs
index 97affe0..9454bbb 100644 (file)
@@ -4,8 +4,11 @@
 // </copyright>
 // -----------------------------------------------------------------------
 
+using System.Collections.Concurrent;
 using System.ComponentModel.Composition;
 using System.Diagnostics;
+using System.IO;
+using System.Threading.Tasks;
 using System.Windows;
 using Caliburn.Micro;
 using Pithos.Client.WPF.Properties;
@@ -23,9 +26,10 @@ namespace Pithos.Client.WPF
     /// TODO: Update summary.
     /// </summary>
     [Export]
-    public class TaskbarViewModel:ViewAware
+    public class TaskbarViewModel:ViewAware,IStatusNotification
     {
         private IStatusChecker _statusChecker;
+        private IEventAggregator _events;
 
         public PithosMonitor Monitor { get; private set; }
 
@@ -34,12 +38,29 @@ namespace Pithos.Client.WPF
         public IScreen Parent { get; set; }
 
         [ImportingConstructor]
-        public TaskbarViewModel(IStatusChecker statusChecker,PithosMonitor monitor,IPithosSettings settings)
+        public TaskbarViewModel(IEventAggregator events, IStatusChecker statusChecker,PithosMonitor monitor,IPithosSettings settings)
         {
             OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
             _statusChecker = statusChecker;
+            _events = events;            
             Settings = settings;
             Monitor = monitor;
+            Monitor.StatusNotification = this;
+
+            
+
+            var account=settings.Accounts.FirstOrDefault(act => act.IsActive);
+            if (account != null)
+            {
+                Monitor.UserName = account.AccountName;
+                Monitor.ApiKey = account.ApiKey;
+                Monitor.UsePithos = account.UsePithos;
+                var appSettings = Properties.Settings.Default;
+                Monitor.AuthenticationUrl = account.UsePithos
+                                                ? appSettings.PithosAuthenticationUrl
+                                                : appSettings.CloudfilesAuthenticationUrl;
+                Monitor.RootPath = Path.Combine(Settings.PithosPath, account.RootPath??"");
+            }
 
         }
         #region Status Properties
@@ -78,14 +99,14 @@ namespace Pithos.Client.WPF
             }
         }
 
-        private readonly IObservableCollection<FileEntry> _recentFiles = new BindableCollection<FileEntry>();
-        public IObservableCollection<FileEntry> RecentFiles
+        private readonly ObservableConcurrentCollection<FileEntry> _recentFiles = new ObservableConcurrentCollection<FileEntry>();
+        public ObservableConcurrentCollection<FileEntry> RecentFiles
         {
             get { return _recentFiles; }
         }
 
 
-        private string _statusIcon;
+        private string _statusIcon="Images/Tray.ico";
         public string StatusIcon
         {
             get { return _statusIcon; }
@@ -153,9 +174,47 @@ namespace Pithos.Client.WPF
                 StatusIcon = String.Format(@"Images/{0}.ico", info.IconName);
                 StatusMessage = String.Format("Pithos 1.0\r\n{0}", info.StatusText);
             }
-            if (!String.IsNullOrWhiteSpace(Settings.UserName) &&
-                !String.IsNullOrWhiteSpace(Settings.ApiKey))
-                Monitor.Start();
+
+            var tv=this.GetView();
+            _events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info});
+            if (!String.IsNullOrWhiteSpace(Monitor.UserName) &&
+                !String.IsNullOrWhiteSpace(Monitor.ApiKey))
+                StartMonitor();                
+        }
+
+        private void StartMonitor()
+        {
+            Task.Factory.StartNew(() =>
+            {
+                try
+                {
+                    Monitor.Start();
+                }
+                catch(Exception exc)
+                {
+                    var message = String.Format("An exception occured. Can't start monitoring\nWill retry in 10 seconds\n{0}",exc);
+                    _events.Publish(new Notification{Title = "Error", Message = message, Level = TraceLevel.Error});
+                    Task.Factory.StartNewDelayed(10000, StartMonitor);                    
+                }
+            });
+        }
+
+
+        public void NotifyChange(string status, TraceLevel level=TraceLevel.Info)
+        {
+            this.StatusMessage = status;
+            
+            _events.Publish(new Notification { Title = "Pithos", Message = status, Level = level });
+        }
+
+        public void NotifyChangedFile(string filePath)
+        {
+            var entry = new FileEntry {FullPath=filePath};
+            IProducerConsumerCollection<FileEntry> files=this.RecentFiles;
+            FileEntry popped;
+            while (files.Count > 5)
+                files.TryTake(out popped);
+            files.TryAdd(entry);
         }
     }
 }