Added action to the PithosBalloon
[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                 ///<summary>
128                 /// The Shell depends on MEF to provide implementations for windowManager, events, the status checker service and the settings
129                 ///</summary>
130                 ///<remarks>
131                 /// The PithosSettings class encapsulates the app's settings to abstract their storage mechanism (App settings, a database or registry)
132                 ///</remarks>
133                 [ImportingConstructor]          
134                 public ShellViewModel(IWindowManager windowManager, IEventAggregator events, IStatusChecker statusChecker, PithosSettings settings)
135                 {
136                         try
137                         {
138
139                                 _windowManager = windowManager;
140                                 //CHECK: Caliburn doesn't need explicit command construction
141                                 //OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
142                                 _statusChecker = statusChecker;
143                                 //The event subst
144                                 _events = events;
145                                 _events.Subscribe(this);
146
147                                 Settings = settings;
148
149                                 Proxy.SetFromSettings(settings);
150
151                                 StatusMessage = "In Synch";
152
153                                 _fileVersion=  new Lazy<FileVersionInfo>(() =>
154                                 {
155                                         Assembly assembly = Assembly.GetExecutingAssembly();
156                                         var fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location);
157                                         return fileVersion;
158                                 });
159                                 _accounts.CollectionChanged += (sender, e) =>
160                                                                                                    {
161                                                                                                            NotifyOfPropertyChange(() => OpenFolderCaption);
162                                                                                                            NotifyOfPropertyChange(() => HasAccounts);
163                                                                                                    };
164
165                         }
166                         catch (Exception exc)
167                         {
168                                 Log.Error("Error while starting the ShellViewModel",exc);
169                                 throw;
170                         }
171                 }
172
173
174                 protected override void OnActivate()
175                 {
176                         base.OnActivate();
177
178                         
179
180                         StartMonitoring();                    
181                 }
182
183
184
185                 private async void StartMonitoring()
186                 {
187                         try
188                         {
189                                 var accounts = Settings.Accounts.Select(MonitorAccount);
190                                 await TaskEx.WhenAll(accounts);
191                                 _statusService = StatusService.Start();
192
193 /*
194                                 foreach (var account in Settings.Accounts)
195                                 {
196                                         await MonitorAccount(account);
197                                 }
198 */
199                                 
200                         }
201                         catch (AggregateException exc)
202                         {
203                                 exc.Handle(e =>
204                                 {
205                                         Log.Error("Error while starting monitoring", e);
206                                         return true;
207                                 });
208                                 throw;
209                         }
210                 }
211
212                 protected override void OnDeactivate(bool close)
213                 {
214                         base.OnDeactivate(close);
215                         if (close)
216                         {
217                                 StatusService.Stop(_statusService);
218                                 _statusService = null;
219                         }
220                 }
221
222                 public Task MonitorAccount(AccountSettings account)
223                 {
224                         return Task.Factory.StartNew(() =>
225                         {                                                
226                                 PithosMonitor monitor;
227                                 var accountName = account.AccountName;
228
229                                 if (_monitors.TryGetValue(accountName, out monitor))
230                                 {
231                                         //If the account is active
232                                         if (account.IsActive)
233                                                 //Start the monitor. It's OK to start an already started monitor,
234                                                 //it will just ignore the call                        
235                                                 StartMonitor(monitor).Wait();                        
236                                         else
237                                         {
238                                                 //If the account is inactive
239                                                 //Stop and remove the monitor
240                                                 RemoveMonitor(accountName);
241                                         }
242                                         return;
243                                 }
244
245                                 
246                                 //Create a new monitor/ Can't use MEF here, it would return a single instance for all monitors
247                                 monitor = new PithosMonitor
248                                                           {
249                                                                   UserName = accountName,
250                                                                   ApiKey = account.ApiKey,                                  
251                                                                   StatusNotification = this,
252                                                                   RootPath = account.RootPath
253                                                           };
254                                 //PithosMonitor uses MEF so we need to resolve it
255                                 IoC.BuildUp(monitor);
256
257                                 monitor.AuthenticationUrl = account.ServerUrl;
258
259                                 _monitors[accountName] = monitor;
260
261                                 if (account.IsActive)
262                                 {
263                                         //Don't start a monitor if it doesn't have an account and ApiKey
264                                         if (String.IsNullOrWhiteSpace(monitor.UserName) ||
265                                                 String.IsNullOrWhiteSpace(monitor.ApiKey))
266                                                 return;
267                                         StartMonitor(monitor);
268                                 }
269                         });
270                 }
271
272
273                 protected override void OnViewLoaded(object view)
274                 {
275                         UpdateStatus();
276                         var window = (Window)view;            
277                         TaskEx.Delay(1000).ContinueWith(t => Execute.OnUIThread(window.Hide));
278                         base.OnViewLoaded(view);
279                 }
280
281
282                 #region Status Properties
283
284                 private string _statusMessage;
285                 public string StatusMessage
286                 {
287                         get { return _statusMessage; }
288                         set
289                         {
290                                 _statusMessage = value;
291                                 NotifyOfPropertyChange(() => StatusMessage);
292                         }
293                 }
294
295                 private readonly ObservableConcurrentCollection<AccountInfo> _accounts = new ObservableConcurrentCollection<AccountInfo>();
296                 public ObservableConcurrentCollection<AccountInfo> Accounts
297                 {
298                         get { return _accounts; }
299                 }
300
301                 public bool HasAccounts
302                 {
303                         get { return _accounts.Count > 0; }
304                 }
305
306
307                 public string OpenFolderCaption
308                 {
309                         get
310                         {
311                                 return (_accounts.Count == 0)
312                                                 ? "No Accounts Defined"
313                                                 : "Open Pithos Folder";
314                         }
315                 }
316
317                 private string _pauseSyncCaption="Pause Synching";
318                 public string PauseSyncCaption
319                 {
320                         get { return _pauseSyncCaption; }
321                         set
322                         {
323                                 _pauseSyncCaption = value;
324                                 NotifyOfPropertyChange(() => PauseSyncCaption);
325                         }
326                 }
327
328                 private readonly ObservableConcurrentCollection<FileEntry> _recentFiles = new ObservableConcurrentCollection<FileEntry>();
329                 public ObservableConcurrentCollection<FileEntry> RecentFiles
330                 {
331                         get { return _recentFiles; }
332                 }
333
334
335                 private string _statusIcon="../Images/Pithos.ico";
336                 public string StatusIcon
337                 {
338                         get { return _statusIcon; }
339                         set
340                         {
341                                 //TODO: Ensure all status icons use the Pithos logo
342                                 _statusIcon = value;
343                                 NotifyOfPropertyChange(() => StatusIcon);
344                         }
345                 }
346
347                 #endregion
348
349                 #region Commands
350
351         public void ShowPreferences()
352         {
353             ShowPreferences(null);
354         }
355
356                 public void ShowPreferences(string currentTab)
357                 {
358                         //Settings.Reload();
359                     var preferences = new PreferencesViewModel(_windowManager, _events, this, Settings,currentTab);
360                     _windowManager.ShowDialog(preferences);
361                         
362                 }
363
364                 public void AboutPithos()
365                 {
366                         var about = new AboutViewModel();
367                         _windowManager.ShowWindow(about);
368                 }
369
370                 public void SendFeedback()
371                 {
372                         var feedBack =  IoC.Get<FeedbackViewModel>();
373                         _windowManager.ShowWindow(feedBack);
374                 }
375
376                 //public PithosCommand OpenPithosFolderCommand { get; private set; }
377
378                 public void OpenPithosFolder()
379                 {
380                         var account = Settings.Accounts.FirstOrDefault(acc => acc.IsActive);
381                         if (account == null)
382                                 return;
383                         Process.Start(account.RootPath);
384                 }
385
386                 public void OpenPithosFolder(AccountInfo account)
387                 {
388                         Process.Start(account.AccountPath);
389                 }
390
391                 
392 /*
393                 public void GoToSite()
394                 {            
395                         var site = Properties.Settings.Default.PithosSite;
396                         Process.Start(site);            
397                 }
398 */
399
400                 public void GoToSite(AccountInfo account)
401                 {
402                         /*var site = String.Format("{0}/ui/?token={1}&user={2}",
403                                 account.SiteUri,account.Token,
404                                 account.UserName);*/
405                         Process.Start(account.SiteUri);
406                 }
407
408                 public void ShowFileProperties()
409                 {
410                         var account = Settings.Accounts.First(acc => acc.IsActive);            
411                         var dir = new DirectoryInfo(account.RootPath + @"\pithos");
412                         var files=dir.GetFiles();
413                         var r=new Random();
414                         var idx=r.Next(0, files.Length);
415                         ShowFileProperties(files[idx].FullName);            
416                 }
417
418                 public void ShowFileProperties(string filePath)
419                 {
420                         if (String.IsNullOrWhiteSpace(filePath))
421                                 throw new ArgumentNullException("filePath");
422                         if (!File.Exists(filePath) && !Directory.Exists(filePath))
423                                 throw new ArgumentException(String.Format("Non existent file {0}",filePath),"filePath");
424                         Contract.EndContractBlock();
425
426                         var pair=(from monitor in  Monitors
427                                                            where filePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase)
428                                                                    select monitor).FirstOrDefault();
429                         var accountMonitor = pair.Value;
430
431                         if (accountMonitor == null)
432                                 return;
433
434                         var infoTask=Task.Factory.StartNew(()=>accountMonitor.GetObjectInfo(filePath));
435
436                         
437
438                         var fileProperties = new FilePropertiesViewModel(this, infoTask,filePath);
439                         _windowManager.ShowWindow(fileProperties);
440                 } 
441                 
442                 public void ShowContainerProperties()
443                 {
444                         var account = Settings.Accounts.First(acc => acc.IsActive);            
445                         var dir = new DirectoryInfo(account.RootPath);
446                         var fullName = (from folder in dir.EnumerateDirectories()
447                                                         where (folder.Attributes & FileAttributes.Hidden) == 0
448                                                         select folder.FullName).First();
449                         ShowContainerProperties(fullName);            
450                 }
451
452                 public void ShowContainerProperties(string filePath)
453                 {
454                         if (String.IsNullOrWhiteSpace(filePath))
455                                 throw new ArgumentNullException("filePath");
456                         if (!Directory.Exists(filePath))
457                                 throw new ArgumentException(String.Format("Non existent file {0}",filePath),"filePath");
458                         Contract.EndContractBlock();
459
460                         var pair=(from monitor in  Monitors
461                                                            where filePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase)
462                                                                    select monitor).FirstOrDefault();
463                         var accountMonitor = pair.Value;            
464                         var info = accountMonitor.GetContainerInfo(filePath);
465
466                         
467
468                         var containerProperties = new ContainerPropertiesViewModel(this, info,filePath);
469                         _windowManager.ShowWindow(containerProperties);
470                 }
471
472                 public void SynchNow()
473                 {
474                         var agent = IoC.Get<PollAgent>();
475                         agent.SynchNow();
476                 }
477
478                 public ObjectInfo RefreshObjectInfo(ObjectInfo currentInfo)
479                 {
480                         if (currentInfo==null)
481                                 throw new ArgumentNullException("currentInfo");
482                         Contract.EndContractBlock();
483
484                         var monitor = Monitors[currentInfo.Account];
485                         var newInfo=monitor.CloudClient.GetObjectInfo(currentInfo.Account, currentInfo.Container, currentInfo.Name);
486                         return newInfo;
487                 }
488
489                 public ContainerInfo RefreshContainerInfo(ContainerInfo container)
490                 {
491                         if (container == null)
492                                 throw new ArgumentNullException("container");
493                         Contract.EndContractBlock();
494
495                         var monitor = Monitors[container.Account];
496                         var newInfo = monitor.CloudClient.GetContainerInfo(container.Account, container.Name);
497                         return newInfo;
498                 }
499
500
501                 public void ToggleSynching()
502                 {
503                         bool isPaused=false;
504                         foreach (var pair in Monitors)
505                         {
506                                 var monitor = pair.Value;
507                                 monitor.Pause = !monitor.Pause;
508                                 isPaused = monitor.Pause;
509                         }
510
511                         PauseSyncCaption = isPaused ? "Resume syncing" : "Pause syncing";
512                         var iconKey = isPaused? "TraySyncPaused" : "TrayInSynch";
513                         StatusIcon = String.Format(@"../Images/{0}.ico", iconKey);
514                 }
515
516                 public void ExitPithos()
517                 {
518                         foreach (var pair in Monitors)
519                         {
520                                 var monitor = pair.Value;
521                                 monitor.Stop();
522                         }
523
524                         ((Window)GetView()).Close();
525                 }
526                 #endregion
527
528
529                 private readonly Dictionary<PithosStatus, StatusInfo> _iconNames = new List<StatusInfo>
530                         {
531                                 new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
532                                 new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching"),
533                                 new StatusInfo(PithosStatus.SyncPaused, "Sync Paused", "TraySyncPaused")
534                         }.ToDictionary(s => s.Status);
535
536                 readonly IWindowManager _windowManager;
537                 
538
539                 ///<summary>
540                 /// Updates the visual status indicators of the application depending on status changes, e.g. icon, stat                
541                 ///</summary>
542                 public void UpdateStatus()
543                 {
544                         var pithosStatus = _statusChecker.GetPithosStatus();
545
546                         if (_iconNames.ContainsKey(pithosStatus))
547                         {
548                                 var info = _iconNames[pithosStatus];
549                                 StatusIcon = String.Format(@"../Images/{0}.ico", info.IconName);
550
551
552
553                                 StatusMessage = String.Format("Pithos {0}\r\n{1}", _fileVersion.Value.FileVersion,info.StatusText);
554                         }
555                         
556                         //_events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info});
557                 }
558
559
560            
561                 private Task StartMonitor(PithosMonitor monitor,int retries=0)
562                 {
563                         return Task.Factory.StartNew(() =>
564                         {
565                                 using (log4net.ThreadContext.Stacks["Monitor"].Push("Start"))
566                                 {
567                                         try
568                                         {
569                                                 Log.InfoFormat("Start Monitoring {0}", monitor.UserName);
570
571                                                 monitor.Start();
572                                         }
573                                         catch (WebException exc)
574                                         {
575                                                 if (AbandonRetry(monitor, retries))
576                                                         return;
577
578                                                 HttpStatusCode statusCode =HttpStatusCode.OK;
579                                                 var response = exc.Response as HttpWebResponse;
580                                                 if(response!=null)
581                                                         statusCode = response.StatusCode;
582
583                                                 switch (statusCode)
584                                                 {
585                                                         case HttpStatusCode.Unauthorized:
586                                                                 var message = String.Format("API Key Expired for {0}. Starting Renewal",
587                                                                                                                         monitor.UserName);
588                                                                 Log.Error(message, exc);
589                                                         var account = Settings.Accounts.Find(acc => acc.AccountName == monitor.UserName);                                
590                                                         account.IsExpired = true;
591                                 Notify(new ExpirationNotification(account));
592                                                                 //TryAuthorize(monitor.UserName, retries).Wait();
593                                                                 break;
594                                                         case HttpStatusCode.ProxyAuthenticationRequired:
595                                                                 TryAuthenticateProxy(monitor,retries);
596                                                                 break;
597                                                         default:
598                                                                 TryLater(monitor, exc, retries);
599                                                                 break;
600                                                 }
601                                         }
602                                         catch (Exception exc)
603                                         {
604                                                 if (AbandonRetry(monitor, retries)) 
605                                                         return;
606
607                                                 TryLater(monitor,exc,retries);
608                                         }
609                                 }
610                         });
611                 }
612
613                 private void TryAuthenticateProxy(PithosMonitor monitor,int retries)
614                 {
615                         Execute.OnUIThread(() =>
616                                                                    {                                       
617                                                                            var proxyAccount = IoC.Get<ProxyAccountViewModel>();
618                                                                                 proxyAccount.Settings = this.Settings;
619                                                                            if (true != _windowManager.ShowDialog(proxyAccount)) 
620                                                                                    return;
621                                                                            StartMonitor(monitor, retries);
622                                                                            NotifyOfPropertyChange(() => Accounts);
623                                                                    });
624                 }
625
626                 private bool AbandonRetry(PithosMonitor monitor, int retries)
627                 {
628                         if (retries > 1)
629                         {
630                                 var message = String.Format("Monitoring of account {0} has failed too many times. Will not retry",
631                                                                                         monitor.UserName);
632                                 _events.Publish(new Notification
633                                                                         {Title = "Account monitoring failed", Message = message, Level = TraceLevel.Error});
634                                 return true;
635                         }
636                         return false;
637                 }
638
639
640                 public async Task TryAuthorize(string userName, int retries)
641                 {
642                         _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 });
643
644                         try
645                         {
646
647                                 var credentials = await PithosAccount.RetrieveCredentials(Settings.PithosLoginUrl);
648
649                                 var account = Settings.Accounts.First(act => act.AccountName == credentials.UserName);
650                 //The server may return credentials for a different account
651                             var monitor = _monitors[account.AccountName];
652                                 account.ApiKey = credentials.Password;
653                 monitor.ApiKey = credentials.Password;
654                             account.IsExpired = false;
655                                 Settings.Save();
656                                 TaskEx.Delay(10000).ContinueWith(_=>
657                             StartMonitor(monitor, retries + 1));
658                                 NotifyOfPropertyChange(()=>Accounts);
659                         }
660                         catch (AggregateException exc)
661                         {
662                                 string message = String.Format("API Key retrieval for {0} failed", userName);
663                                 Log.Error(message, exc.InnerException);
664                                 _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error });
665                         }
666                         catch (Exception exc)
667                         {
668                                 string message = String.Format("API Key retrieval for {0} failed", userName);
669                                 Log.Error(message, exc);
670                                 _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error });
671                         }
672
673                 }
674
675                 private static bool IsUnauthorized(WebException exc)
676                 {
677                         if (exc==null)
678                                 throw new ArgumentNullException("exc");
679                         Contract.EndContractBlock();
680
681                         var response = exc.Response as HttpWebResponse;
682                         if (response == null)
683                                 return false;
684                         return (response.StatusCode == HttpStatusCode.Unauthorized);
685                 }
686
687                 private void TryLater(PithosMonitor monitor, Exception exc,int retries)
688                 {
689                         var message = String.Format("An exception occured. Can't start monitoring\nWill retry in 10 seconds");
690                         Task.Factory.StartNewDelayed(10000, () => StartMonitor(monitor,retries+1));
691                         _events.Publish(new Notification
692                                                                 {Title = "Error", Message = message, Level = TraceLevel.Error});
693                         Log.Error(message, exc);
694                 }
695
696
697                 public void NotifyChange(string status, TraceLevel level=TraceLevel.Info)
698                 {
699                         StatusMessage = status;
700                         
701                         _events.Publish(new Notification { Title = "Pithos", Message = status, Level = level });
702                 }
703
704                 public void NotifyChangedFile(string filePath)
705                 {
706                         var entry = new FileEntry {FullPath=filePath};
707                         IProducerConsumerCollection<FileEntry> files=RecentFiles;
708                         FileEntry popped;
709                         while (files.Count > 5)
710                                 files.TryTake(out popped);
711                         files.TryAdd(entry);
712                 }
713
714                 public void NotifyAccount(AccountInfo account)
715                 {
716                         if (account== null)
717                                 return;
718                         //TODO: What happens to an existing account whose Token has changed?
719                         account.SiteUri= String.Format("{0}/ui/?token={1}&user={2}",
720                                 account.SiteUri, Uri.EscapeDataString(account.Token),
721                                 Uri.EscapeDataString(account.UserName));
722
723                         if (Accounts.All(item => item.UserName != account.UserName))
724                                 Accounts.TryAdd(account);
725
726                 }
727
728                 public void NotifyConflicts(IEnumerable<FileSystemInfo> conflictFiles, string message)
729                 {
730                         if (conflictFiles == null)
731                                 return;
732                         if (!conflictFiles.Any())
733                                 return;
734
735                         UpdateStatus();
736                         //TODO: Create a more specific message. For now, just show a warning
737                         NotifyForFiles(conflictFiles,message,TraceLevel.Warning);
738
739                 }
740
741                 public void NotifyForFiles(IEnumerable<FileSystemInfo> files, string message,TraceLevel level=TraceLevel.Info)
742                 {
743                         if (files == null)
744                                 return;
745                         if (!files.Any())
746                                 return;
747
748                         StatusMessage = message;
749
750                         _events.Publish(new Notification { Title = "Pithos", Message = message, Level = level});
751                 }
752
753                 public void Notify(Notification notification)
754                 {
755                         _events.Publish(notification);
756                 }
757
758
759                 public void RemoveMonitor(string accountName)
760                 {
761                         if (String.IsNullOrWhiteSpace(accountName))
762                                 return;
763
764                         var accountInfo=_accounts.FirstOrDefault(account => account.UserName == accountName);
765                         _accounts.TryRemove(accountInfo);
766
767                         PithosMonitor monitor;
768                         if (Monitors.TryRemove(accountName, out monitor))
769                         {
770                                 monitor.Stop();
771                         }
772                 }
773
774                 public void RefreshOverlays()
775                 {
776                         foreach (var pair in Monitors)
777                         {
778                                 var monitor = pair.Value;
779
780                                 var path = monitor.RootPath;
781
782                                 if (String.IsNullOrWhiteSpace(path))
783                                         continue;
784
785                                 if (!Directory.Exists(path) && !File.Exists(path))
786                                         continue;
787
788                                 IntPtr pathPointer = Marshal.StringToCoTaskMemAuto(path);
789
790                                 try
791                                 {
792                                         NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM,
793                                                                                                  HChangeNotifyFlags.SHCNF_PATHW | HChangeNotifyFlags.SHCNF_FLUSHNOWAIT,
794                                                                                                  pathPointer, IntPtr.Zero);
795                                 }
796                                 finally
797                                 {
798                                         Marshal.FreeHGlobal(pathPointer);
799                                 }
800                         }
801                 }
802
803                 #region Event Handlers
804                 
805                 public void Handle(SelectiveSynchChanges message)
806                 {
807                         var accountName = message.Account.AccountName;
808                         PithosMonitor monitor;
809                         if (_monitors.TryGetValue(accountName, out monitor))
810                         {
811                                 monitor.SetSelectivePaths(message.Uris,message.Added,message.Removed);
812
813                         }
814                         
815                 }
816
817
818                 private bool _pollStarted = false;
819
820                 //SMELL: Doing so much work for notifications in the shell is wrong
821                 //The notifications should be moved to their own view/viewmodel pair
822                 //and different templates should be used for different message types
823                 //This will also allow the addition of extra functionality, eg. actions
824                 //
825                 public void Handle(Notification notification)
826                 {
827                         UpdateStatus();
828
829                         if (!Settings.ShowDesktopNotifications)
830                                 return;
831
832                         if (notification is PollNotification)
833                         {
834                                 _pollStarted = true;
835                                 return;
836                         }
837                         if (notification is CloudNotification)
838                         {
839                                 if (!_pollStarted) 
840                                         return;
841                                 _pollStarted= false;
842                                 notification.Title = "Pithos";
843                                 notification.Message = "Start Synchronisation";
844                         }
845
846                         if (String.IsNullOrWhiteSpace(notification.Message) && String.IsNullOrWhiteSpace(notification.Title))
847                                 return;
848
849                         BalloonIcon icon;
850                         switch (notification.Level)
851                         {
852                                 case TraceLevel.Error:
853                                         icon = BalloonIcon.Error;
854                                         break;
855                                 case TraceLevel.Info:
856                                 case TraceLevel.Verbose:
857                                         icon = BalloonIcon.Info;
858                                         break;
859                                 case TraceLevel.Warning:
860                                         icon = BalloonIcon.Warning;
861                                         break;
862                                 default:
863                                         icon = BalloonIcon.None;
864                                         break;
865                         }
866                         
867                         if (Settings.ShowDesktopNotifications)
868                         {
869                                 var tv = (ShellView) GetView();
870                             System.Action clickAction = null;
871                 if (notification is ExpirationNotification)
872                 {
873                     clickAction = ()=>ShowPreferences("AccountTab");
874                 }
875                                 var balloon=new PithosBalloon{Title=notification.Title,Message=notification.Message,Icon=icon,ClickAction=clickAction};
876                                 tv.TaskbarView.ShowCustomBalloon(balloon,PopupAnimation.Fade,4000);
877 //                              tv.TaskbarView.ShowBalloonTip(notification.Title, notification.Message, icon);
878                         }
879                 }
880                 #endregion
881
882                 public void Handle(ShowFilePropertiesEvent message)
883                 {
884                         if (message == null)
885                                 throw new ArgumentNullException("message");
886                         if (String.IsNullOrWhiteSpace(message.FileName) )
887                                 throw new ArgumentException("message");
888                         Contract.EndContractBlock();
889
890                         var fileName = message.FileName;
891                         //TODO: Display file properties for non-container folders
892                         if (File.Exists(fileName))
893                                 //Retrieve the full name with exact casing. Pithos names are case sensitive                             
894                                 ShowFileProperties(FileInfoExtensions.GetProperFilePathCapitalization(fileName));
895                         else if (Directory.Exists(fileName))
896                                 //Retrieve the full name with exact casing. Pithos names are case sensitive
897                         {
898                                 var path = FileInfoExtensions.GetProperDirectoryCapitalization(fileName);
899                                 if (IsContainer(path))
900                                         ShowContainerProperties(path);
901                                 else
902                                         ShowFileProperties(path);
903                         }
904                 }
905
906                 private bool IsContainer(string path)
907                 {
908                         var matchingFolders = from account in _accounts
909                                                                   from rootFolder in Directory.GetDirectories(account.AccountPath)
910                                                                   where rootFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase)
911                                                                   select rootFolder;
912                         return matchingFolders.Any();
913                 }
914
915                 public FileStatus GetFileStatus(string localFileName)
916                 {
917                         if (String.IsNullOrWhiteSpace(localFileName))
918                                 throw new ArgumentNullException("localFileName");
919                         Contract.EndContractBlock();
920                         
921                         var statusKeeper = IoC.Get<IStatusKeeper>();
922                         var status=statusKeeper.GetFileStatus(localFileName);
923                         return status;
924                 }
925         }
926 }