1 using System.Collections.Concurrent;
2 using System.ComponentModel;
3 using System.ComponentModel.Composition;
4 using System.Diagnostics;
5 using System.Diagnostics.Contracts;
8 using System.Reflection;
9 using System.Runtime.InteropServices;
10 using System.ServiceModel;
11 using System.ServiceModel.Description;
12 using System.Threading.Tasks;
15 using Hardcodet.Wpf.TaskbarNotification;
16 using Pithos.Client.WPF.Configuration;
17 using Pithos.Client.WPF.FileProperties;
18 using Pithos.Client.WPF.Properties;
19 using Pithos.Client.WPF.SelectiveSynch;
20 using Pithos.Client.WPF.Services;
21 using Pithos.Client.WPF.Shell;
23 using Pithos.Interfaces;
25 using System.Collections.Generic;
29 using StatusService = Pithos.Client.WPF.Services.StatusService;
31 namespace Pithos.Client.WPF {
32 using System.ComponentModel.Composition;
34 [Export(typeof(IShell))]
36 /// The "shell" of the Pithos application displays the taskbar icon, menu and notifications.
37 /// The shell also hosts the status service called by shell extensions to retrieve file info
40 /// It is a strange "shell" as its main visible element is an icon instead of a window
41 /// The shell subscribes to the following events:
42 /// * Notification: Raised by components that want to notify the user. Usually displayed in a balloon
43 /// * 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
44 /// * ShowFilePropertiesEvent: Raised when a shell command requests the display of the file/container properties dialog
46 //TODO: CODE SMELL Why does the shell handle the SelectiveSynchChanges?
47 public class ShellViewModel : Screen, IStatusNotification, IShell,
48 IHandle<Notification>, IHandle<SelectiveSynchChanges>, IHandle<ShowFilePropertiesEvent>
50 //The Status Checker provides the current synch state
51 //TODO: Could we remove the status checker and use events in its place?
52 private IStatusChecker _statusChecker;
53 private IEventAggregator _events;
55 public PithosSettings Settings { get; private set; }
58 private Dictionary<string, PithosMonitor> _monitors = new Dictionary<string, PithosMonitor>();
60 /// Dictionary of account monitors, keyed by account
63 /// One monitor class is created for each account. The Shell needs access to the monitors to execute start/stop/pause commands,
64 /// retrieve account and boject info
66 // TODO: Does the Shell REALLY need access to the monitors? Could we achieve the same results with a better design?
67 // TODO: The monitors should be internal to Pithos.Core, even though exposing them makes coding of the Object and Container windows easier
68 public Dictionary<string, PithosMonitor> Monitors
70 get { return _monitors; }
75 /// The status service is used by Shell extensions to retrieve file status information
77 //TODO: CODE SMELL! This is the shell! While hosting in the shell makes executing start/stop commands easier, it is still a smell
78 private ServiceHost _statusService { get; set; }
80 //Logging in the Pithos client is provided by log4net
81 private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos");
84 /// The Shell depends on MEF to provide implementations for windowManager, events, the status checker service and the settings
87 /// The PithosSettings class encapsulates the app's settings to abstract their storage mechanism (App settings, a database or registry)
89 [ImportingConstructor]
90 public ShellViewModel(IWindowManager windowManager, IEventAggregator events, IStatusChecker statusChecker, PithosSettings settings)
95 _windowManager = windowManager;
96 //CHECK: Caliburn doesn't need explicit command construction
97 //OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
98 _statusChecker = statusChecker;
101 _events.Subscribe(this);
105 StatusMessage = "In Synch";
108 catch (Exception exc)
110 Log.Error("Error while starting the ShellViewModel",exc);
115 protected override void OnActivate()
123 private async Task StartMonitoring()
127 foreach (var account in Settings.Accounts)
129 await MonitorAccount(account);
131 _statusService = StatusService.Start();
133 catch (AggregateException exc)
137 Log.Error("Error while starting monitoring", e);
144 protected override void OnDeactivate(bool close)
146 base.OnDeactivate(close);
149 StatusService.Stop(_statusService);
150 _statusService = null;
154 public Task MonitorAccount(AccountSettings account)
156 return Task.Factory.StartNew(() =>
158 PithosMonitor monitor = null;
159 var accountName = account.AccountName;
161 if (_monitors.TryGetValue(accountName, out monitor))
163 //If the account is active
164 if (account.IsActive)
165 //Start the monitor. It's OK to start an already started monitor,
166 //it will just ignore the call
170 //If the account is inactive
171 //Stop and remove the monitor
172 RemoveMonitor(accountName);
177 //Create a new monitor/ Can't use MEF here, it would return a single instance for all monitors
178 monitor = new PithosMonitor
180 UserName = accountName,
181 ApiKey = account.ApiKey,
182 UsePithos = account.UsePithos,
183 StatusNotification = this,
184 RootPath = account.RootPath
186 //PithosMonitor uses MEF so we need to resolve it
187 IoC.BuildUp(monitor);
189 var appSettings = Properties.Settings.Default;
190 monitor.AuthenticationUrl = account.UsePithos
191 ? appSettings.PithosAuthenticationUrl
192 : appSettings.CloudfilesAuthenticationUrl;
194 _monitors[accountName] = monitor;
196 if (account.IsActive)
198 //Don't start a monitor if it doesn't have an account and ApiKey
199 if (String.IsNullOrWhiteSpace(monitor.UserName) ||
200 String.IsNullOrWhiteSpace(monitor.ApiKey))
202 StartMonitor(monitor);
208 protected override void OnViewLoaded(object view)
210 var window = (Window)view;
213 base.OnViewLoaded(view);
217 #region Status Properties
219 private string _statusMessage;
220 public string StatusMessage
222 get { return _statusMessage; }
225 _statusMessage = value;
226 NotifyOfPropertyChange(() => StatusMessage);
230 private ObservableConcurrentCollection<AccountInfo> _accounts = new ObservableConcurrentCollection<AccountInfo>();
231 public ObservableConcurrentCollection<AccountInfo> Accounts
233 get { return _accounts; }
237 private string _pauseSyncCaption="Pause Syncing";
238 public string PauseSyncCaption
240 get { return _pauseSyncCaption; }
243 _pauseSyncCaption = value;
244 NotifyOfPropertyChange(() => PauseSyncCaption);
248 private readonly ObservableConcurrentCollection<FileEntry> _recentFiles = new ObservableConcurrentCollection<FileEntry>();
249 public ObservableConcurrentCollection<FileEntry> RecentFiles
251 get { return _recentFiles; }
255 private string _statusIcon="../Images/Tray.ico";
256 public string StatusIcon
258 get { return _statusIcon; }
262 NotifyOfPropertyChange(() => StatusIcon);
270 public void ShowPreferences()
273 var preferences = new PreferencesViewModel(_windowManager,_events, this,Settings);
274 _windowManager.ShowDialog(preferences);
278 public void AboutPithos()
280 var about = new AboutViewModel();
281 _windowManager.ShowWindow(about);
284 public void SendFeedback()
286 var feedBack = IoC.Get<FeedbackViewModel>();
287 _windowManager.ShowWindow(feedBack);
290 //public PithosCommand OpenPithosFolderCommand { get; private set; }
292 public void OpenPithosFolder()
294 var account = Settings.Accounts.FirstOrDefault(acc => acc.IsActive);
297 Process.Start(account.RootPath);
300 public void OpenPithosFolder(AccountInfo account)
302 Process.Start(account.AccountPath);
306 public void GoToSite(AccountInfo account)
308 var site = String.Format("{0}/ui/?token={1}&user={2}",
309 Properties.Settings.Default.PithosSite,account.Token,
314 public void ShowFileProperties()
316 var account = Settings.Accounts.First(acc => acc.IsActive);
317 var dir = new DirectoryInfo(account.RootPath + @"\pithos");
318 var files=dir.GetFiles();
320 var idx=r.Next(0, files.Length);
321 ShowFileProperties(files[idx].FullName);
324 public void ShowFileProperties(string filePath)
326 if (String.IsNullOrWhiteSpace(filePath))
327 throw new ArgumentNullException("filePath");
328 if (!File.Exists(filePath))
329 throw new ArgumentException(String.Format("Non existent file {0}",filePath),"filePath");
330 Contract.EndContractBlock();
332 var pair=(from monitor in Monitors
333 where filePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase)
334 select monitor).FirstOrDefault();
335 var account = pair.Key;
336 var accountMonitor = pair.Value;
338 if (accountMonitor == null)
341 var infoTask=Task.Factory.StartNew(()=>accountMonitor.GetObjectInfo(filePath));
345 var fileProperties = new FilePropertiesViewModel(this, infoTask,filePath);
346 _windowManager.ShowWindow(fileProperties);
349 public void ShowContainerProperties()
351 var account = Settings.Accounts.First(acc => acc.IsActive);
352 var dir = new DirectoryInfo(account.RootPath);
353 var fullName = (from folder in dir.EnumerateDirectories()
354 where (folder.Attributes & FileAttributes.Hidden) == 0
355 select folder.FullName).First();
356 ShowContainerProperties(fullName);
359 public void ShowContainerProperties(string filePath)
361 if (String.IsNullOrWhiteSpace(filePath))
362 throw new ArgumentNullException("filePath");
363 if (!Directory.Exists(filePath))
364 throw new ArgumentException(String.Format("Non existent file {0}",filePath),"filePath");
365 Contract.EndContractBlock();
367 var pair=(from monitor in Monitors
368 where filePath.StartsWith(monitor.Value.RootPath, StringComparison.InvariantCultureIgnoreCase)
369 select monitor).FirstOrDefault();
370 var account = pair.Key;
371 var accountMonitor = pair.Value;
372 var info = accountMonitor.GetContainerInfo(filePath);
376 var containerProperties = new ContainerPropertiesViewModel(this, info,filePath);
377 _windowManager.ShowWindow(containerProperties);
380 public ObjectInfo RefreshObjectInfo(ObjectInfo currentInfo)
382 if (currentInfo==null)
383 throw new ArgumentNullException("currentInfo");
384 Contract.EndContractBlock();
386 var monitor = Monitors[currentInfo.Account];
387 var newInfo=monitor.CloudClient.GetObjectInfo(currentInfo.Account, currentInfo.Container, currentInfo.Name);
391 public ContainerInfo RefreshContainerInfo(ContainerInfo container)
393 if (container == null)
394 throw new ArgumentNullException("container");
395 Contract.EndContractBlock();
397 var monitor = Monitors[container.Account];
398 var newInfo = monitor.CloudClient.GetContainerInfo(container.Account, container.Name);
403 public void ToggleSynching()
406 foreach (var pair in Monitors)
408 var monitor = pair.Value;
409 monitor.Pause = !monitor.Pause;
410 isPaused = monitor.Pause;
413 PauseSyncCaption = isPaused ? "Resume syncing" : "Pause syncing";
414 var iconKey = isPaused? "TraySyncPaused" : "TrayInSynch";
415 StatusIcon = String.Format(@"../Images/{0}.ico", iconKey);
418 public void ExitPithos()
420 foreach (var pair in Monitors)
422 var monitor = pair.Value;
426 ((Window)GetView()).Close();
431 private Dictionary<PithosStatus, StatusInfo> iconNames = new List<StatusInfo>
433 new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
434 new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching"),
435 new StatusInfo(PithosStatus.SyncPaused, "Sync Paused", "TraySyncPaused")
436 }.ToDictionary(s => s.Status);
438 readonly IWindowManager _windowManager;
442 /// Updates the visual status indicators of the application depending on status changes, e.g. icon, stat
444 public void UpdateStatus()
446 var pithosStatus = _statusChecker.GetPithosStatus();
448 if (iconNames.ContainsKey(pithosStatus))
450 var info = iconNames[pithosStatus];
451 StatusIcon = String.Format(@"../Images/{0}.ico", info.IconName);
453 Assembly assembly = Assembly.GetExecutingAssembly();
454 var fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location);
457 StatusMessage = String.Format("Pithos {0}\r\n{1}", fileVersion.FileVersion,info.StatusText);
460 _events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info});
465 private Task StartMonitor(PithosMonitor monitor,int retries=0)
467 return Task.Factory.StartNew(() =>
469 using (log4net.ThreadContext.Stacks["Monitor"].Push("Start"))
473 Log.InfoFormat("Start Monitoring {0}", monitor.UserName);
477 catch (WebException exc)
479 if (AbandonRetry(monitor, retries))
482 if (IsUnauthorized(exc))
484 var message = String.Format("API Key Expired for {0}. Starting Renewal",monitor.UserName);
485 Log.Error(message,exc);
486 TryAuthorize(monitor,retries).Wait();
490 TryLater(monitor, exc,retries);
493 catch (Exception exc)
495 if (AbandonRetry(monitor, retries))
498 TryLater(monitor,exc,retries);
504 private bool AbandonRetry(PithosMonitor monitor, int retries)
508 var message = String.Format("Monitoring of account {0} has failed too many times. Will not retry",
510 _events.Publish(new Notification
511 {Title = "Account monitoring failed", Message = message, Level = TraceLevel.Error});
518 private async Task TryAuthorize(PithosMonitor monitor,int retries)
520 _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 });
525 var credentials = await PithosAccount.RetrieveCredentialsAsync(Settings.PithosLoginUrl);
527 var account = Settings.Accounts.FirstOrDefault(act => act.AccountName == credentials.UserName);
528 account.ApiKey = credentials.Password;
529 monitor.ApiKey = credentials.Password;
531 await TaskEx.Delay(10000);
532 StartMonitor(monitor, retries + 1);
533 NotifyOfPropertyChange(()=>Accounts);
535 catch (AggregateException exc)
537 string message = String.Format("API Key retrieval for {0} failed", monitor.UserName);
538 Log.Error(message, exc.InnerException);
539 _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error });
542 catch (Exception exc)
544 string message = String.Format("API Key retrieval for {0} failed", monitor.UserName);
545 Log.Error(message, exc);
546 _events.Publish(new Notification { Title = "Authorization failed", Message = message, Level = TraceLevel.Error });
553 private static bool IsUnauthorized(WebException exc)
556 throw new ArgumentNullException("exc");
557 Contract.EndContractBlock();
559 var response = exc.Response as HttpWebResponse;
560 if (response == null)
562 return (response.StatusCode == HttpStatusCode.Unauthorized);
565 private void TryLater(PithosMonitor monitor, Exception exc,int retries)
567 var message = String.Format("An exception occured. Can't start monitoring\nWill retry in 10 seconds");
568 Task.Factory.StartNewDelayed(10000, () => StartMonitor(monitor,retries+1));
569 _events.Publish(new Notification
570 {Title = "Error", Message = message, Level = TraceLevel.Error});
571 Log.Error(message, exc);
575 public void NotifyChange(string status, TraceLevel level=TraceLevel.Info)
577 this.StatusMessage = status;
579 _events.Publish(new Notification { Title = "Pithos", Message = status, Level = level });
582 public void NotifyChangedFile(string filePath)
584 var entry = new FileEntry {FullPath=filePath};
585 IProducerConsumerCollection<FileEntry> files=this.RecentFiles;
587 while (files.Count > 5)
588 files.TryTake(out popped);
592 public void NotifyAccount(AccountInfo account)
597 account.SiteUri= String.Format("{0}/ui/?token={1}&user={2}",
598 Properties.Settings.Default.PithosSite, account.Token,
601 IProducerConsumerCollection<AccountInfo> accounts = Accounts;
602 for (var i = 0; i < _accounts.Count; i++)
605 if (accounts.TryTake(out item))
607 if (item.UserName!=account.UserName)
609 accounts.TryAdd(item);
614 accounts.TryAdd(account);
618 public void RemoveMonitor(string accountName)
620 if (String.IsNullOrWhiteSpace(accountName))
623 PithosMonitor monitor;
624 if (Monitors.TryGetValue(accountName, out monitor))
626 Monitors.Remove(accountName);
631 public void RefreshOverlays()
633 foreach (var pair in Monitors)
635 var monitor = pair.Value;
637 var path = monitor.RootPath;
639 if (String.IsNullOrWhiteSpace(path))
642 if (!Directory.Exists(path) && !File.Exists(path))
645 IntPtr pathPointer = Marshal.StringToCoTaskMemAuto(path);
649 NativeMethods.SHChangeNotify(HChangeNotifyEventID.SHCNE_UPDATEITEM,
650 HChangeNotifyFlags.SHCNF_PATHW | HChangeNotifyFlags.SHCNF_FLUSHNOWAIT,
651 pathPointer, IntPtr.Zero);
655 Marshal.FreeHGlobal(pathPointer);
660 #region Event Handlers
662 public void Handle(SelectiveSynchChanges message)
664 var accountName = message.Account.AccountName;
665 PithosMonitor monitor;
666 if (_monitors.TryGetValue(accountName, out monitor))
668 monitor.AddSelectivePaths(message.Added);
669 monitor.RemoveSelectivePaths(message.Removed);
676 public void Handle(Notification notification)
678 if (!Settings.ShowDesktopNotifications)
680 BalloonIcon icon = BalloonIcon.None;
681 switch (notification.Level)
683 case TraceLevel.Error:
684 icon = BalloonIcon.Error;
686 case TraceLevel.Info:
687 case TraceLevel.Verbose:
688 icon = BalloonIcon.Info;
690 case TraceLevel.Warning:
691 icon = BalloonIcon.Warning;
694 icon = BalloonIcon.None;
698 if (Settings.ShowDesktopNotifications)
700 var tv = (ShellView) this.GetView();
701 tv.TaskbarView.ShowBalloonTip(notification.Title, notification.Message, icon);
706 public void Handle(ShowFilePropertiesEvent message)
709 throw new ArgumentNullException("message");
710 if (String.IsNullOrWhiteSpace(message.FileName) )
711 throw new ArgumentException("message");
712 Contract.EndContractBlock();
714 var fileName = message.FileName;
716 if (File.Exists(fileName))
717 this.ShowFileProperties(fileName);
718 else if (Directory.Exists(fileName))
719 this.ShowContainerProperties(fileName);