// -----------------------------------------------------------------------
//
// TODO: Update copyright text.
//
// -----------------------------------------------------------------------
using System.Collections.Concurrent;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using Caliburn.Micro;
using Pithos.Client.WPF.Properties;
using Pithos.Core;
using Pithos.Interfaces;
namespace Pithos.Client.WPF
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///
/// TODO: Update summary.
///
[Export]
public class TaskbarViewModel:ViewAware,IStatusNotification
{
private IStatusChecker _statusChecker;
private IEventAggregator _events;
public PithosMonitor Monitor { get; private set; }
public IPithosSettings Settings { get; private set; }
public IScreen Parent { get; set; }
[ImportingConstructor]
public TaskbarViewModel(IEventAggregator events, IStatusChecker statusChecker,PithosMonitor monitor,IPithosSettings settings)
{
OpenPithosFolderCommand = new PithosCommand(OpenPithosFolder);
_statusChecker = statusChecker;
_events = events;
Settings = settings;
Monitor = monitor;
Monitor.StatusNotification = this;
var account=settings.Accounts.FirstOrDefault(act => act.IsActive);
if (account != null)
{
Monitor.UserName = account.AccountName;
Monitor.ApiKey = account.ApiKey;
Monitor.UsePithos = account.UsePithos;
var appSettings = Properties.Settings.Default;
Monitor.AuthenticationUrl = account.UsePithos
? appSettings.PithosAuthenticationUrl
: appSettings.CloudfilesAuthenticationUrl;
Monitor.RootPath = Path.Combine(Settings.PithosPath, account.RootPath??"");
}
}
#region Status Properties
private string _statusMessage;
public string StatusMessage
{
get { return _statusMessage; }
set
{
_statusMessage = value;
NotifyOfPropertyChange(() => StatusMessage);
}
}
private string _usageMessage;
public string UsageMessage
{
get { return _usageMessage; }
set
{
_usageMessage = value;
NotifyOfPropertyChange(() => UsageMessage);
}
}
private string _pauseSyncCaption="Pause Syncing";
public string PauseSyncCaption
{
get { return _pauseSyncCaption; }
set
{
_pauseSyncCaption = value;
NotifyOfPropertyChange(() => PauseSyncCaption);
}
}
private readonly ObservableConcurrentCollection _recentFiles = new ObservableConcurrentCollection();
public ObservableConcurrentCollection RecentFiles
{
get { return _recentFiles; }
}
private string _statusIcon="Images/Tray.ico";
public string StatusIcon
{
get { return _statusIcon; }
set
{
_statusIcon = value;
NotifyOfPropertyChange(() => StatusIcon);
}
}
#endregion
#region Commands
public void ShowPreferences()
{
Settings.Reload();
}
public PithosCommand OpenPithosFolderCommand { get; private set; }
public void OpenPithosFolder()
{
Process.Start(Settings.PithosPath);
}
public void GoToSite()
{
Process.Start(Properties.Settings.Default.PithosSite);
}
public void ToggleSynching()
{
Monitor.Pause = !Monitor.Pause;
PauseSyncCaption = Monitor.Pause ? "Resume syncing" : "Pause syncing";
var iconKey = Monitor.Pause ? "TraySyncPaused" : "TrayInSynch";
StatusIcon = String.Format(@"Images/{0}.ico", iconKey);
}
public void ExitPithos()
{
Monitor.Stop();
Parent.TryClose();
}
#endregion
private Dictionary iconNames = new List
{
new StatusInfo(PithosStatus.InSynch, "All files up to date", "TrayInSynch"),
new StatusInfo(PithosStatus.Syncing, "Syncing Files", "TraySynching"),
new StatusInfo(PithosStatus.SyncPaused, "Sync Paused", "TraySyncPaused")
}.ToDictionary(s => s.Status);
public void UpdateStatus()
{
var pithosStatus = _statusChecker.GetPithosStatus();
if (iconNames.ContainsKey(pithosStatus))
{
var info = iconNames[pithosStatus];
StatusIcon = String.Format(@"Images/{0}.ico", info.IconName);
StatusMessage = String.Format("Pithos 1.0\r\n{0}", info.StatusText);
}
var tv=this.GetView();
_events.Publish(new Notification { Title = "Start", Message = "Start Monitoring", Level = TraceLevel.Info});
if (!String.IsNullOrWhiteSpace(Monitor.UserName) &&
!String.IsNullOrWhiteSpace(Monitor.ApiKey))
StartMonitor();
}
private void StartMonitor()
{
Task.Factory.StartNew(() =>
{
try
{
Monitor.Start();
}
catch(Exception exc)
{
var message = String.Format("An exception occured. Can't start monitoring\nWill retry in 10 seconds\n{0}",exc);
_events.Publish(new Notification{Title = "Error", Message = message, Level = TraceLevel.Error});
Task.Factory.StartNewDelayed(10000, StartMonitor);
}
});
}
public void NotifyChange(string status, TraceLevel level=TraceLevel.Info)
{
this.StatusMessage = status;
_events.Publish(new Notification { Title = "Pithos", Message = status, Level = level });
}
public void NotifyChangedFile(string filePath)
{
var entry = new FileEntry {FullPath=filePath};
IProducerConsumerCollection files=this.RecentFiles;
FileEntry popped;
while (files.Count > 5)
files.TryTake(out popped);
files.TryAdd(entry);
}
}
}