Added a convenience property and a way to remove sessions that triggers eventing

This commit is contained in:
David Sparer
2016-09-20 12:07:05 -06:00
parent 941e3d5904
commit 688dbd1ce9

View File

@@ -10,6 +10,7 @@ namespace mRemoteNG.Config.Putty
public abstract class AbstractPuttySessionsProvider
{
public virtual RootPuttySessionsNodeInfo RootInfo { get; } = new RootPuttySessionsNodeInfo();
protected virtual List<PuttySessionInfo> Sessions => RootInfo.Children.OfType<PuttySessionInfo>().ToList();
#region Public Methods
public abstract string[] GetSessionNames(bool raw = false);
@@ -22,16 +23,23 @@ namespace mRemoteNG.Config.Putty
var sessionInfo = GetSession(sessionName);
AddSession(sessionInfo);
}
return RootInfo.Children.OfType<PuttySessionInfo>();
return Sessions;
}
protected virtual void AddSession(PuttySessionInfo sessionInfo)
{
if (string.IsNullOrEmpty(sessionInfo?.Name) || RootInfo.Children.Any(child => child.Name == sessionInfo.Name))
if (string.IsNullOrEmpty(sessionInfo?.Name) || Sessions.Any(child => child.Name == sessionInfo.Name))
return;
RootInfo.AddChild(sessionInfo);
RaisePuttySessionCollectionChangedEvent(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, sessionInfo));
}
protected virtual void RemoveSession(PuttySessionInfo sessionInfo)
{
if (!Sessions.Contains(sessionInfo)) return;
RootInfo.RemoveChild(sessionInfo);
RaisePuttySessionCollectionChangedEvent(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, sessionInfo));
}
public virtual void StartWatcher() { }
@@ -40,14 +48,12 @@ namespace mRemoteNG.Config.Putty
public delegate void PuttySessionChangedEventHandler(object sender, PuttySessionChangedEventArgs e);
public event PuttySessionChangedEventHandler PuttySessionChanged;
protected virtual void RaiseSessionChangedEvent(PuttySessionChangedEventArgs args)
{
PuttySessionChanged?.Invoke(this, args);
}
public event NotifyCollectionChangedEventHandler PuttySessionsCollectionChanged;
protected void RaisePuttySessionCollectionChangedEvent(NotifyCollectionChangedEventArgs args)
{
PuttySessionsCollectionChanged?.Invoke(this, args);