Fix #2121, manual changes to the API Key were not passed to the appropriate PithosMon...
[pithos-ms-client] / trunk / Pithos.Client.WPF / Shell / ShellViewModel.cs
1 #region
2 /* -----------------------------------------------------------------------
3  * <copyright file="ShellViewModel.cs" company="GRNet">
4  * 
5  * Copyright 2011-2012 GRNET S.A. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or
8  * without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  *   1. Redistributions of source code must retain the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer.
14  *
15  *   2. Redistributions in binary form must reproduce the above
16  *      copyright notice, this list of conditions and the following
17  *      disclaimer in the documentation and/or other materials
18  *      provided with the distribution.
19  *
20  *
21  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * The views and conclusions contained in the software and
35  * documentation are those of the authors and should not be
36  * interpreted as representing official policies, either expressed
37  * or implied, of GRNET S.A.
38  * </copyright>
39  * -----------------------------------------------------------------------
40  */
41 #endregion
42 using System.Collections.Concurrent;
43 using System.Diagnostics;
44 using System.Diagnostics.Contracts;
45 using System.IO;
46 using System.Net;
47 using System.Reflection;
48 using System.Runtime.InteropServices;
49 using System.ServiceModel;
50 using System.Threading.Tasks;
51 using System.Windows;
52 using System.Windows.Controls.Primitives;
53 using Caliburn.Micro;
54 using Hardcodet.Wpf.TaskbarNotification;
55 using Pithos.Client.WPF.Configuration;
56 using Pithos.Client.WPF.FileProperties;
57 using Pithos.Client.WPF.Preferences;
58 using Pithos.Client.WPF.Properties;
59 using Pithos.Client.WPF.SelectiveSynch;
60 using Pithos.Client.WPF.Services;
61 using Pithos.Client.WPF.Shell;
62 using Pithos.Core;
63 using Pithos.Core.Agents;
64 using Pithos.Interfaces;
65 using System;
66 using System.Collections.Generic;
67 using System.Linq;
68 using Pithos.Network;
69 using StatusService = Pithos.Client.WPF.Services.StatusService;
70
71 namespace Pithos.Client.WPF {
72         using System.ComponentModel.Composition;
73
74         
75         ///<summary>
76         /// The "shell" of the Pithos application displays the taskbar  icon, menu and notifications.
77         /// The shell also hosts the status service called by shell extensions to retrieve file info
78         ///</summary>
79         ///<remarks>
80         /// It is a strange "shell" as its main visible element is an icon instead of a window
81         /// The shell subscribes to the following events:
82         /// * Notification:  Raised by components that want to notify the user. Usually displayed in a balloon
83         /// * SelectiveSynchChanges: Notifies that the user made changes to the selective synch folders for an account. Raised by the Selective Synch dialog. Located here because the monitors are here
84         /// * ShowFilePropertiesEvent: Raised when a shell command requests the display of the file/container properties dialog
85         ///</remarks>           
86         //TODO: CODE SMELL Why does the shell handle the SelectiveSynchChanges?
87         [Export(typeof(IShell))]
88         public class ShellViewModel : Screen, IStatusNotification, IShell,
89                 IHandle<Notification>, IHandle<SelectiveSynchChanges>, IHandle<ShowFilePropertiesEvent>
90         {
91                 //The Status Checker provides the current synch state
92                 //TODO: Could we remove the status checker and use events in its place?
93                 private readonly IStatusChecker _statusChecker;
94                 private readonly IEventAggregator _events;
95
96                 public PithosSettings Settings { get; private set; }
97
98
99                 private readonly ConcurrentDictionary<string, PithosMonitor> _monitors = new ConcurrentDictionary<string, PithosMonitor>();
100                 ///<summary>
101                 /// Dictionary of account monitors, keyed by account
102                 ///</summary>
103                 ///<remarks>
104                 /// One monitor class is created for each account. The Shell needs access to the monitors to execute start/stop/pause commands,
105                 /// retrieve account and boject info            
106                 ///</remarks>
107                 // TODO: Does the Shell REALLY need access to the monitors? Could we achieve the same results with a better design?
108                 // TODO: The monitors should be internal to Pithos.Core, even though exposing them makes coding of the Object and Container windows easier
109                 public ConcurrentDictionary<string, PithosMonitor> Monitors
110                 {
111                         get { return _monitors; }
112                 }
113
114
115                 ///<summary>
116                 /// The status service is used by Shell extensions to retrieve file status information
117                 ///</summary>
118                 //TODO: CODE SMELL! This is the shell! While hosting in the shell makes executing start/stop commands easier, it is still a smell
119                 private ServiceHost _statusService;
120
121                 //Logging in the Pithos client is provided by log4net
122                 private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos");
123
124                 //Lazily initialized File Version info. This is done once and lazily to avoid blocking the UI
125                 private Lazy<FileVersionInfo> _fileVersion;
126
127             private PollAgent _pollAgent;
128
129                 ///<summary>
130                 /// The Shell depends on MEF to provide implementations for windowManager, events, the status checker service and the settings
131                 ///</summary>
132                 ///<remarks>
133                 /// The PithosSettings class encapsulates the app's settings to abstract their storage mechanism (App settings, a database or registry)
134                 ///</remarks>
135                 [ImportingConstructor]          
136                 public ShellViewModel(IWindowManager windowManager, IEventAggregator events, IStatusChecker statusChecker, PithosSettings settings,PollAgent pollAgent)
137                 {
138                         try
139                         {
140
141                                 _windowManager = windowManager;
142                                 //CHECK: Caliburn doesn't need explicit command construction
143                                 //OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
144                                 _statusChecker = statusChecker;
145                                 //The event subst
146                                 _events = events;
147                                 _events.Subscribe(this);
148
149                             _pollAgent = pollAgent;
150                                 Settings = settings;
151
152                                 Proxy.SetFromSettings(settings);
153
154                                 StatusMessage = "In Synch";
155
156                                 _fileVersion=  new Lazy<FileVersionInfo>(() =>
157                                 {
158                                         Assembly assembly = Assembly.GetExecutingAssembly();
159                                         var fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location);
160                                         return fileVersion;
161                                 });
162                                 _accounts.CollectionChanged += (sender, e) =>
163                                                                                                    {
164                                                                                                            NotifyOfPropertyChange(() => OpenFolderCaption);
165                                                                                                            NotifyOfPropertyChange(() => HasAccounts);
166                                                                                                    };
167
168                         }
169                         catch (Exception exc)
170                         {
171                                 Log.Error("Error while starting the ShellViewModel",exc);
172                                 throw;
173                         }
174                 }
175
176
177                 protected override void OnActivate()
178                 {
179                         base.OnActivate();
180
181                         
182
183                         StartMonitoring();                    
184                 }
185
186
187
188                 private async void StartMonitoring()
189                 {
190                         try
191                         {
192                                 var accounts = Settings.Accounts.Select(MonitorAccount);
193                                 await TaskEx.WhenAll(accounts);
194                                 _statusService = StatusService.Start();
195
196 /*
197                                 foreach (var account in Settings.Accounts)
198                                 {
199                                         await MonitorAccount(account);
200                                 }
201 */
202                                 
203                         }
204                         catch (AggregateException exc)
205                         {
206                                 exc.Handle(e =>
207                                 {
208                                         Log.Error("Error while starting monitoring", e);
209                                         return true;
210                                 });
211                                 throw;
212                         }
213                 }
214
215                 protected override void OnDeactivate(bool close)
216                 {
217                         base.OnDeactivate(close);
218                         if (close)
219                         {
220                                 StatusService.Stop(_statusService);
221                                 _statusService = null;
222                         }
223                 }
224
225                 public Task MonitorAccount(AccountSettings account)
226                 {
227                         return Task.Factory.StartNew(() =>
228                         {                                                
229                                 PithosMonitor monitor;
230                                 var accountName = account.AccountName;
231
232                                 if (_monitors.TryGetValue(accountName, out monitor))
233                                 {
234                                         //If the account is active
235                     if (account.IsActive)
236                     {
237                         //The Api Key may have changed throuth the Preferences dialog
238                         monitor.ApiKey = account.ApiKey;
239                                                 Debug.Assert(monitor.StatusNotification == this,"An existing monitor should already have a StatusNotification service object");
240                         monitor.RootPath = account.RootPath;
241                         //Start the monitor. It's OK to start an already started monitor,
242                         //it will just ignore the call                        
243                         StartMonitor(monitor).Wait();
244                     }
245                     else
246                     {
247                         //If the account is inactive
248                         //Stop and remove the monitor
249                         RemoveMonitor(accountName);
250                     }
251                                         return;
252                                 }
253
254                                 
255                                 //Create a new monitor/ Can't use MEF here, it would return a single instance for all monitors
256                                 monitor = new PithosMonitor
257                                                           {
258                                                                   UserName = accountName,
259                                                                   ApiKey = account.ApiKey,                                  
260                                                                   StatusNotification = this,
261                                                                   RootPath = account.RootPath
262                                                           };
263                                 //PithosMonitor uses MEF so we need to resolve it
264                                 IoC.BuildUp(monitor);
265
266                                 monitor.AuthenticationUrl = account.ServerUrl;
267
268                                 _monitors[accountName] = monitor;
269
270                                 if (account.IsActive)
271                                 {
272                                         //Don't start a monitor if it doesn't have an account and ApiKey
273                                         if (String.IsNullOrWhiteSpace(monitor.UserName) ||
274                                                 String.IsNullOrWhiteSpace(monitor.ApiKey))
275                                                 return;
276                                         StartMonitor(monitor);
277                                 }
278                         });
279                 }
280
281
282                 protected override void OnViewLoaded(object view)
283                 {
284                         UpdateStatus();
285                         var window = (Window)view;            
286                         TaskEx.Delay(1000).ContinueWith(t => Execute.OnUIThread(window.Hide));
287                         base.OnViewLoaded(view);
288                 }
289
290
291                 #region Status Properties
292
293                 private string _statusMessage;
294                 public string StatusMessage
295                 {
296                         get { return _statusMessage; }
297                         set
298                         {
299                                 _statusMessage = value;
300                                 NotifyOfPropertyChange(() => StatusMessage);
301                         }
302                 }
303
304                 private readonly ObservableConcurrentCollection<AccountInfo> _accounts = new ObservableConcurrentCollection<AccountInfo>();
305                 public ObservableConcurrentCollection<AccountInfo> Accounts
306                 {
307                         get { return _accounts; }
308                 }
309
310                 public bool HasAccounts
311                 {
312                         get { return _accounts.Count > 0; }
313                 }
314
315
316                 public string OpenFolderCaption
317                 {
318                         get
319                         {
320                                 return (_accounts.Count == 0)
321                                                 ? "No Accounts Defined"
322                                                 : "Open Pithos Folder";
323                         }
324                 }
325
326                 private string _pauseSyncCaption="Pause Synching";
327                 public string PauseSyncCaption
328                 {
329                         get { return _pauseSyncCaption; }
330                         set
331                         {
332                                 _pauseSyncCaption = value;
333                                 NotifyOfPropertyChange(() => PauseSyncCaption);
334                         }
335                 }
336
337                 private readonly ObservableConcurrentCollection<FileEntry> _recentFiles = new ObservableConcurrentCollection<FileEntry>();
338                 public ObservableConcurrentCollection<FileEntry> RecentFiles
339                 {
340                         get { return _recentFiles; }
341                 }
342
343
344                 private string _statusIcon="../Images/Pithos.ico";
345                 public string StatusIcon
346                 {
347                         get { return _statusIcon; }
348                         set
349                         {
350                                 //TODO: Ensure all status icons use the Pithos logo
351                                 _statusIcon = value;
352                                 NotifyOfPropertyChange(() => StatusIcon);
353                         }
354                 }
355
356                 #endregion
357
358                 #region Commands
359
360         public void ShowPreferences()
361         {
362             ShowPreferences(null);
363         }
364
365                 public void ShowPreferences(string currentTab)
366                 {
367                         //Settings.Reload();
368                     var preferences = new PreferencesViewModel(_windowManager, _events, this, Settings,currentTab);
369                     _windowManager.ShowDialog(preferences);
370                         
371                 }
372
373                 public void AboutPithos()
374                 {
375                         var about = new AboutViewModel();
376                         _windowManager.ShowWindow(about);
377                 }
378
379                 public void SendFeedback()
380                 {
381                         var feedBack =  IoC.Get<FeedbackViewModel>();
382                         _windowManager.ShowWindow(feedBack);
383                 }
384
385                 //public PithosCommand OpenPithosFolderCommand { get; private set; }
386
387                 public void OpenPithosFolder()
388                 {
389                         var account = Settings.Accounts.FirstOrDefault(acc => acc.IsActive);
390                         if (account == null)
391                                 return;
392                         Process.Start(account.RootPath);
393                 }
394
395                 public void OpenPithosFolder(AccountInfo account)
396                 {
397                         Process.Start(account.AccountPath);
398                 }
399
400                 
401 /*
402                 public void GoToSite()
403                 {            
404                         var site = Properties.Settings.Default.PithosSite;
405                         Process.Start(site);            
406                 }
407 */
408
409                 public void GoToSite(AccountInfo account)
410                 {
411                         /*var site = String.Format("{0}/ui/?token={1}&user={2}",
412                                 account.SiteUri,account.Token,
413                                 account.UserName);*/
414                         Process.Start(account.SiteUri);
415                 }
416
417                 public void ShowFileProperties()
418                 {
419                         var account = Settings.Accounts.First(acc => acc.IsActive);            
420                         var dir = new DirectoryInfo(account.RootPath + @"\pithos");
421                         var files=dir.GetFiles();
422                         var r=new Random();
423                         var idx=r.Next(0, files.Length);
424                         ShowFileProperties(files[idx].FullName);            
425                 }
426
427                 public void ShowFileProperties(string filePath)
428                 {
429                         if (String.IsNullOrWhiteSpace(filePath))
430                                 throw new ArgumentNullException("filePath");
431                         if (!File.Exists(filePath) && !Directory.Exists(filePath))
432                                 throw new ArgumentException(String.Format("Non existent file {0}",filePath),"filePath");
433                         Contract.EndContractBlock();
434
435                         var pair=(from monitor in  Monitors
436                                                            where filePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase)
437                                                                    select monitor).FirstOrDefault();
438                         var accountMonitor = pair.Value;
439
440                         if (accountMonitor == null)
441                                 return;
442
443                         var infoTask=Task.Factory.StartNew(()=>accountMonitor.GetObjectInfo(filePath));
444
445                         
446
447                         var fileProperties = new FilePropertiesViewModel(this, infoTask,filePath);
448                         _windowManager.ShowWindow(fileProperties);
449                 } 
450                 
451                 public void ShowContainerProperties()
452                 {
453                         var account = Settings.Accounts.First(acc => acc.IsActive);            
454                         var dir = new DirectoryInfo(account.RootPath);
455                         var fullName = (from folder in dir.EnumerateDirectories()
456                                                         where (folder.Attributes & FileAttributes.Hidden) == 0
457                                                         select folder.FullName).First();
458                         ShowContainerProperties(fullName);            
459                 }
460
461                 public void ShowContainerProperties(string filePath)
462                 {
463                         if (String.IsNullOrWhiteSpace(filePath))
464                                 throw new ArgumentNullException("filePath");
465                         if (!Directory.Exists(filePath))
466                                 throw new ArgumentException(String.Format("Non existent file {0}",filePath),"filePath");
467                         Contract.EndContractBlock();
468
469                         var pair=(from monitor in  Monitors
470                                                            where filePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase)
471                                                                    select monitor).FirstOrDefault();
472                         var accountMonitor = pair.Value;            
473                         var info = accountMonitor.GetContainerInfo(filePath);
474
475                         
476
477                         var containerProperties = new ContainerPropertiesViewModel(this, info,filePath);
478                         _windowManager.ShowWindow(containerProperties);
479                 }
480
481                 public void SynchNow()
482                 {
483                         _pollAgent.SynchNow();
484                 }
485
486                 public ObjectInfo RefreshObjectInfo(ObjectInfo currentInfo)
487                 {
488                         if (currentInfo==null)
489                                 throw new ArgumentNullException("currentInfo");
490                         Contract.EndContractBlock();
491
492                         var monitor = Monitors[currentInfo.Account];
493                         var newInfo=monitor.CloudClient.GetObjectInfo(currentInfo.Account, currentInfo.Container, currentInfo.Name);
494                         return newInfo;
495                 }
496
497                 public ContainerInfo RefreshContainerInfo(ContainerInfo container)
498                 {
499                         if (container == null)
500                                 throw new ArgumentNullException("container");
501                         Contract.EndContractBlock();
502
503                         var monitor = Monitors[container.Account];
504                         var newInfo = monitor.CloudClient.GetContainerInfo(container.Account, container.Name);
505                         return newInfo;
506                 }
507
508
509                 public void ToggleSynching()
510                 {
511                         bool isPaused=false;
512                         foreach (var pair in Monitors)
513                         {
514                                 var monitor = pair.Value;
515                                 monitor.Pause = !monitor.Pause;
516                                 isPaused = monitor.Pause;
517                         }
518
519                         PauseSyncCaption = isPaused ? "Resume syncing" : "Pause syncing";
520                         var iconKey = isPaused? "TraySyncPaused" : "TrayInSynch";
521                         StatusIcon = String.Format(@"../Images/{0}.ico", iconKey);
522                 }
523
524                 public void ExitPithos()
525                 {
526                         foreach (var pair in Monitors)
527                         {
528                                 var monitor = pair.Value;
529                                 monitor.Stop();
530                         }
531
532                         ((Window)GetView()).Close();
533                 }
534                 #endregion
535
536
537                 private readonly Dictionary<PithosStatus, StatusInfo> _iconNames = new List<StatusInfo>
538                         {
539                                 new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
540                                 new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching"),
541                                 new StatusInfo(PithosStatus.SyncPaused, "Sync Paused", "TraySyncPaused")
542                         }.ToDictionary(s => s.Status);
543
544                 readonly IWindowManager _windowManager;
545                 
546
547                 ///<summary>
548                 /// Updates the visual status indicators of the application depending on status changes, e.g. icon, stat                
549                 ///</summary>
550                 public void UpdateStatus()
551                 {
552                         var pithosStatus = _statusChecker.GetPithosStatus();
553
554                         if (_iconNames.ContainsKey(pithosStatus))
555                         {
556                                 var info = _iconNames[pithosStatus];
557                                 StatusIcon = String.Format(@"../Images/{0}.ico", info.IconName);
558
559
560
561                                 StatusMessage = String.Format("Pithos {0}\r\n{1}", _fileVersion.Value.FileVersion,info.StatusText);
562                         }
563                         
564                         //_events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info});
565                 }
566
567
568            
569                 private Task StartMonitor(PithosMonitor monitor,int retries=0)
570                 {
571                         return Task.Factory.StartNew(() =>
572                         {
573                                 using (log4net.ThreadContext.Stacks["Monitor"].Push("Start"))
574                                 {
575                                         try
576                                         {
577                                                 Log.InfoFormat("Start Monitoring {0}", monitor.UserName);
578
579                                                 monitor.Start();
580                                         }
581                                         catch (WebException exc)
582                                         {
583                                                 if (AbandonRetry(monitor, retries))
584                                                         return;
585
586                                                 HttpStatusCode statusCode =HttpStatusCode.OK;
587                                                 var response = exc.Response as HttpWebResponse;
588                                                 if(response!=null)
589                                                         statusCode = response.StatusCode;
590
591                                                 switch (statusCode)
592                                                 {
593                                                         case HttpStatusCode.Unauthorized:
594                                                                 var message = String.Format("API Key Expired for {0}. Starting Renewal",
595                                                                                                                         monitor.UserName);
596                                                                 Log.Error(message, exc);
597                                                         var account = Settings.Accounts.Find(acc => acc.AccountName == monitor.UserName);                                
598                                                         account.IsExpired = true;
599                                 Notify(new ExpirationNotification(account));
600                                                                 //TryAuthorize(monitor.UserName, retries).Wait();
601                                                                 break;
602                                                         case HttpStatusCode.ProxyAuthenticationRequired:
603                                                                 TryAuthenticateProxy(monitor,retries);
604                                                                 break;
605                                                         default:
606                                                                 TryLater(monitor, exc, retries);
607                                                                 break;
608                                                 }
609                                         }
610                                         catch (Exception exc)
611                                         {
612                                                 if (AbandonRetry(monitor, retries)) 
613                                                         return;
614
615                                                 TryLater(monitor,exc,retries);
616                                         }
617                                 }
618                         });
619                 }
620
621                 private void TryAuthenticateProxy(PithosMonitor monitor,int retries)
622                 {
623                         Execute.OnUIThread(() =>
624                                                                    {                                       
625                                                                            var proxyAccount = IoC.Get<ProxyAccountViewModel>();
626                                                                                 proxyAccount.Settings = this.Settings;
627                                                                            if (true != _windowManager.ShowDialog(proxyAccount)) 
628                                                                                    return;
629                                                                            StartMonitor(monitor, retries);
630                                                                            NotifyOfPropertyChange(() => Accounts);
631                                                                    });
632                 }
633
634                 private bool AbandonRetry(PithosMonitor monitor, int retries)
635                 {
636                         if (retries > 1)
637                         {
638                                 var message = String.Format("Monitoring of account {0} has failed too many times. Will not retry",
639                                                                                         monitor.UserName);
640                                 _events.Publish(new Notification
641                                                                         {Title = "Account monitoring failed", Message = message, Level = TraceLevel.Error});
642                                 return true;
643                         }
644                         return false;
645                 }
646
647
648
649                 private static bool IsUnauthorized(WebException exc)
650                 {
651                         if (exc==null)
652                                 throw new ArgumentNullException("exc");
653                         Contract.EndContractBlock();
654
655                         var response = exc.Response as HttpWebResponse;
656                         if (response == null)
657                                 return false;
658                         return (response.StatusCode == HttpStatusCode.Unauthorized);
659                 }
660
661                 private void TryLater(PithosMonitor monitor, Exception exc,int retries)
662                 {
663                         var message = String.Format("An exception occured. Can't start monitoring\nWill retry in 10 seconds");
664                         Task.Factory.StartNewDelayed(10000, () => StartMonitor(monitor,retries+1));
665                         _events.Publish(new Notification
666                                                                 {Title = "Error", Message = message, Level = TraceLevel.Error});
667                         Log.Error(message, exc);
668                 }
669
670
671                 public void NotifyChange(string status, TraceLevel level=TraceLevel.Info)
672                 {
673                         StatusMessage = status;
674                         
675                         _events.Publish(new Notification { Title = "Pithos", Message = status, Level = level });
676                 }
677
678                 public void NotifyChangedFile(string filePath)
679                 {
680                         var entry = new FileEntry {FullPath=filePath};
681                         IProducerConsumerCollection<FileEntry> files=RecentFiles;
682                         FileEntry popped;
683                         while (files.Count > 5)
684                                 files.TryTake(out popped);
685                         files.TryAdd(entry);
686                 }
687
688                 public void NotifyAccount(AccountInfo account)
689                 {
690                         if (account== null)
691                                 return;
692                         //TODO: What happens to an existing account whose Token has changed?
693                         account.SiteUri= String.Format("{0}/ui/?token={1}&user={2}",
694                                 account.SiteUri, Uri.EscapeDataString(account.Token),
695                                 Uri.EscapeDataString(account.UserName));
696
697                         if (Accounts.All(item => item.UserName != account.UserName))
698                                 Accounts.TryAdd(account);
699
700                 }
701
702                 public void NotifyConflicts(IEnumerable<FileSystemInfo> conflictFiles, string message)
703                 {
704                         if (conflictFiles == null)
705                                 return;
706                         if (!conflictFiles.Any())
707                                 return;
708
709                         UpdateStatus();
710                         //TODO: Create a more specific message. For now, just show a warning
711                         NotifyForFiles(conflictFiles,message,TraceLevel.Warning);
712
713                 }
714
715                 public void NotifyForFiles(IEnumerable<FileSystemInfo> files, string message,TraceLevel level=TraceLevel.Info)
716                 {
717                         if (files == null)
718                                 return;
719                         if (!files.Any())
720                                 return;
721
722                         StatusMessage = message;
723
724                         _events.Publish(new Notification { Title = "Pithos", Message = message, Level = level});
725                 }
726
727                 public void Notify(Notification notification)
728                 {
729                         _events.Publish(notification);
730                 }
731
732
733                 public void RemoveMonitor(string accountName)
734                 {
735                         if (String.IsNullOrWhiteSpace(accountName))
736                                 return;
737
738                         var accountInfo=_accounts.FirstOrDefault(account => account.UserName == accountName);
739                         _accounts.TryRemove(accountInfo);
740
741                         PithosMonitor monitor;
742                         if (Monitors.TryRemove(accountName, out monitor))
743                         {
744                                 monitor.Stop();
745                         }
746                 }
747
748                 public void RefreshOverlays()
749                 {
750                         foreach (var pair in Monitors)
751                         {
752                                 var monitor = pair.Value;
753
754                                 var path = monitor.RootPath;
755
756                                 if (String.IsNullOrWhiteSpace(path))
757                                         continue;
758
759                                 if (!Directory.Exists(path) && !File.Exists(path))
760                                         continue;
761
762                                 IntPtr pathPointer = Marshal.StringToCoTaskMemAuto(path);
763
764                                 try
765                                 {
766                                         NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM,
767                                                                                                  HChangeNotifyFlags.SHCNF_PATHW | HChangeNotifyFlags.SHCNF_FLUSHNOWAIT,
768                                                                                                  pathPointer, IntPtr.Zero);
769                                 }
770                                 finally
771                                 {
772                                         Marshal.FreeHGlobal(pathPointer);
773                                 }
774                         }
775                 }
776
777                 #region Event Handlers
778                 
779                 public void Handle(SelectiveSynchChanges message)
780                 {
781                         var accountName = message.Account.AccountName;
782                         PithosMonitor monitor;
783                         if (_monitors.TryGetValue(accountName, out monitor))
784                         {
785                                 monitor.SetSelectivePaths(message.Uris,message.Added,message.Removed);
786
787                         }
788                         
789                 }
790
791
792                 private bool _pollStarted = false;
793
794                 //SMELL: Doing so much work for notifications in the shell is wrong
795                 //The notifications should be moved to their own view/viewmodel pair
796                 //and different templates should be used for different message types
797                 //This will also allow the addition of extra functionality, eg. actions
798                 //
799                 public void Handle(Notification notification)
800                 {
801                         UpdateStatus();
802
803                         if (!Settings.ShowDesktopNotifications)
804                                 return;
805
806                         if (notification is PollNotification)
807                         {
808                                 _pollStarted = true;
809                                 return;
810                         }
811                         if (notification is CloudNotification)
812                         {
813                                 if (!_pollStarted) 
814                                         return;
815                                 _pollStarted= false;
816                                 notification.Title = "Pithos";
817                                 notification.Message = "Start Synchronisation";
818                         }
819
820                         if (String.IsNullOrWhiteSpace(notification.Message) && String.IsNullOrWhiteSpace(notification.Title))
821                                 return;
822
823                         BalloonIcon icon;
824                         switch (notification.Level)
825                         {
826                                 case TraceLevel.Error:
827                                         icon = BalloonIcon.Error;
828                                         break;
829                                 case TraceLevel.Info:
830                                 case TraceLevel.Verbose:
831                                         icon = BalloonIcon.Info;
832                                         break;
833                                 case TraceLevel.Warning:
834                                         icon = BalloonIcon.Warning;
835                                         break;
836                                 default:
837                                         icon = BalloonIcon.None;
838                                         break;
839                         }
840                         
841                         if (Settings.ShowDesktopNotifications)
842                         {
843                                 var tv = (ShellView) GetView();
844                             System.Action clickAction = null;
845                 if (notification is ExpirationNotification)
846                 {
847                     clickAction = ()=>ShowPreferences("AccountTab");
848                 }
849                                 var balloon=new PithosBalloon{Title=notification.Title,Message=notification.Message,Icon=icon,ClickAction=clickAction};
850                                 tv.TaskbarView.ShowCustomBalloon(balloon,PopupAnimation.Fade,4000);
851 //                              tv.TaskbarView.ShowBalloonTip(notification.Title, notification.Message, icon);
852                         }
853                 }
854                 #endregion
855
856                 public void Handle(ShowFilePropertiesEvent message)
857                 {
858                         if (message == null)
859                                 throw new ArgumentNullException("message");
860                         if (String.IsNullOrWhiteSpace(message.FileName) )
861                                 throw new ArgumentException("message");
862                         Contract.EndContractBlock();
863
864                         var fileName = message.FileName;
865                         //TODO: Display file properties for non-container folders
866                         if (File.Exists(fileName))
867                                 //Retrieve the full name with exact casing. Pithos names are case sensitive                             
868                                 ShowFileProperties(FileInfoExtensions.GetProperFilePathCapitalization(fileName));
869                         else if (Directory.Exists(fileName))
870                                 //Retrieve the full name with exact casing. Pithos names are case sensitive
871                         {
872                                 var path = FileInfoExtensions.GetProperDirectoryCapitalization(fileName);
873                                 if (IsContainer(path))
874                                         ShowContainerProperties(path);
875                                 else
876                                         ShowFileProperties(path);
877                         }
878                 }
879
880                 private bool IsContainer(string path)
881                 {
882                         var matchingFolders = from account in _accounts
883                                                                   from rootFolder in Directory.GetDirectories(account.AccountPath)
884                                                                   where rootFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase)
885                                                                   select rootFolder;
886                         return matchingFolders.Any();
887                 }
888
889                 public FileStatus GetFileStatus(string localFileName)
890                 {
891                         if (String.IsNullOrWhiteSpace(localFileName))
892                                 throw new ArgumentNullException("localFileName");
893                         Contract.EndContractBlock();
894                         
895                         var statusKeeper = IoC.Get<IStatusKeeper>();
896                         var status=statusKeeper.GetFileStatus(localFileName);
897                         return status;
898                 }
899         }
900 }