1 // -----------------------------------------------------------------------
2 // <copyright file="StatusViewModel.cs" company="Microsoft">
3 // TODO: Update copyright text.
5 // -----------------------------------------------------------------------
7 using System.Collections.Concurrent;
8 using System.ComponentModel.Composition;
9 using System.Diagnostics;
11 using System.Threading.Tasks;
14 using Pithos.Client.WPF.Properties;
16 using Pithos.Interfaces;
18 namespace Pithos.Client.WPF
21 using System.Collections.Generic;
26 /// TODO: Update summary.
29 public class TaskbarViewModel:ViewAware,IStatusNotification
31 private IStatusChecker _statusChecker;
32 private IEventAggregator _events;
34 public PithosMonitor Monitor { get; private set; }
36 public IPithosSettings Settings { get; private set; }
38 public IScreen Parent { get; set; }
40 [ImportingConstructor]
41 public TaskbarViewModel(IEventAggregator events, IStatusChecker statusChecker,PithosMonitor monitor,IPithosSettings settings)
43 OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
44 _statusChecker = statusChecker;
48 Monitor.StatusNotification = this;
52 var account=settings.Accounts.FirstOrDefault(act => act.IsActive);
55 Monitor.UserName = account.AccountName;
56 Monitor.ApiKey = account.ApiKey;
57 Monitor.UsePithos = account.UsePithos;
58 var appSettings = Properties.Settings.Default;
59 Monitor.AuthenticationUrl = account.UsePithos
60 ? appSettings.PithosAuthenticationUrl
61 : appSettings.CloudfilesAuthenticationUrl;
62 Monitor.RootPath = Path.Combine(Settings.PithosPath, account.RootPath??"");
66 #region Status Properties
68 private string _statusMessage;
69 public string StatusMessage
71 get { return _statusMessage; }
74 _statusMessage = value;
75 NotifyOfPropertyChange(() => StatusMessage);
79 private string _usageMessage;
80 public string UsageMessage
82 get { return _usageMessage; }
85 _usageMessage = value;
86 NotifyOfPropertyChange(() => UsageMessage);
91 private string _pauseSyncCaption="Pause Syncing";
92 public string PauseSyncCaption
94 get { return _pauseSyncCaption; }
97 _pauseSyncCaption = value;
98 NotifyOfPropertyChange(() => PauseSyncCaption);
102 private readonly ObservableConcurrentCollection<FileEntry> _recentFiles = new ObservableConcurrentCollection<FileEntry>();
103 public ObservableConcurrentCollection<FileEntry> RecentFiles
105 get { return _recentFiles; }
109 private string _statusIcon="Images/Tray.ico";
110 public string StatusIcon
112 get { return _statusIcon; }
116 NotifyOfPropertyChange(() => StatusIcon);
124 public void ShowPreferences()
130 public PithosCommand OpenPithosFolderCommand { get; private set; }
132 public void OpenPithosFolder()
134 Process.Start(Settings.PithosPath);
137 public void GoToSite()
139 Process.Start(Properties.Settings.Default.PithosSite);
143 public void ToggleSynching()
145 Monitor.Pause = !Monitor.Pause;
146 PauseSyncCaption = Monitor.Pause ? "Resume syncing" : "Pause syncing";
147 var iconKey = Monitor.Pause ? "TraySyncPaused" : "TrayInSynch";
148 StatusIcon = String.Format(@"Images/{0}.ico", iconKey);
151 public void ExitPithos()
159 private Dictionary<PithosStatus, StatusInfo> iconNames = new List<StatusInfo>
161 new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
162 new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching"),
163 new StatusInfo(PithosStatus.SyncPaused, "Sync Paused", "TraySyncPaused")
164 }.ToDictionary(s => s.Status);
167 public void UpdateStatus()
169 var pithosStatus = _statusChecker.GetPithosStatus();
171 if (iconNames.ContainsKey(pithosStatus))
173 var info = iconNames[pithosStatus];
174 StatusIcon = String.Format(@"Images/{0}.ico", info.IconName);
175 StatusMessage = String.Format("Pithos 1.0\r\n{0}", info.StatusText);
178 var tv=this.GetView();
179 _events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info});
180 if (!String.IsNullOrWhiteSpace(Monitor.UserName) &&
181 !String.IsNullOrWhiteSpace(Monitor.ApiKey))
185 private void StartMonitor()
187 Task.Factory.StartNew(() =>
195 var message = String.Format("An exception occured. Can't start monitoring\nWill retry in 10 seconds\n{0}",exc);
196 _events.Publish(new Notification{Title = "Error", Message = message, Level = TraceLevel.Error});
197 Task.Factory.StartNewDelayed(10000, StartMonitor);
203 public void NotifyChange(string status, TraceLevel level=TraceLevel.Info)
205 this.StatusMessage = status;
207 _events.Publish(new Notification { Title = "Pithos", Message = status, Level = level });
210 public void NotifyChangedFile(string filePath)
212 var entry = new FileEntry {FullPath=filePath};
213 IProducerConsumerCollection<FileEntry> files=this.RecentFiles;
215 while (files.Count > 5)
216 files.TryTake(out popped);