began implementing plumbing for saving connections on edit

This commit is contained in:
David Sparer
2017-11-12 15:06:18 -06:00
parent 42e59ee564
commit 1d80b166b1
8 changed files with 86 additions and 23 deletions

View File

@@ -1,4 +1,5 @@
using System.IO;
using mRemoteNG.Config.Connections;
namespace mRemoteNG.App.Initialization
{
@@ -6,6 +7,8 @@ namespace mRemoteNG.App.Initialization
{
public void LoadCredsAndCons()
{
new SaveConnectionsOnEdit(Runtime.ConnectionsService);
if (Settings.Default.FirstStart && !Settings.Default.LoadConsFromCustomLocation && !File.Exists(Runtime.ConnectionsService.GetStartupConnectionFileName()))
Runtime.ConnectionsService.NewConnectionsFile(Runtime.ConnectionsService.GetStartupConnectionFileName());

View File

@@ -87,8 +87,7 @@ namespace mRemoteNG.App
backupPruner.PruneBackupFiles(connectionFileName);
}
ConnectionsService.ConnectionTreeModel = ConnectionsService.LoadConnections(Settings.Default.UseSQLServer, false, connectionFileName);
Windows.TreeForm.ConnectionTree.ConnectionTreeModel = ConnectionsService.ConnectionTreeModel;
ConnectionsService.LoadConnections(Settings.Default.UseSQLServer, false, connectionFileName);
if (Settings.Default.UseSQLServer)
{
@@ -179,7 +178,7 @@ namespace mRemoteNG.App
return;
}
MessageCollector.AddExceptionMessage(string.Format(Language.strConnectionsFileCouldNotBeLoaded, connectionFileName), ex);
MessageCollector.AddExceptionStackTrace(string.Format(Language.strConnectionsFileCouldNotBeLoaded, connectionFileName), ex);
if (connectionFileName != ConnectionsService.GetStartupConnectionFileName())
{
LoadConnections(withDialog);

View File

@@ -1,4 +1,5 @@
using System;
using mRemoteNG.Tools;
using mRemoteNG.Tree;
namespace mRemoteNG.Config.Connections
@@ -9,7 +10,7 @@ namespace mRemoteNG.Config.Connections
/// The previous <see cref="ConnectionTreeModel"/> that is being
/// unloaded.
/// </summary>
public ConnectionTreeModel PreviousConnectionTreeModel { get; }
public Maybe<ConnectionTreeModel> PreviousConnectionTreeModel { get; }
/// <summary>
/// True if the previous <see cref="ConnectionTreeModel"/> was loaded from
@@ -36,7 +37,7 @@ namespace mRemoteNG.Config.Connections
public string NewSourcePath { get; }
public ConnectionsLoadedEventArgs(
ConnectionTreeModel previousTreeModelModel, ConnectionTreeModel newTreeModelModel,
Maybe<ConnectionTreeModel> previousTreeModelModel, ConnectionTreeModel newTreeModelModel,
bool previousSourceWasDatabase, bool newSourceIsDatabase,
string newSourcePath)
{

View File

@@ -33,7 +33,7 @@ namespace mRemoteNG.Config.Connections.Multiuser
private void Load(object sender, ConnectionsUpdateAvailableEventArgs args)
{
Runtime.ConnectionsService.ConnectionTreeModel = Runtime.ConnectionsService.LoadConnections(true, false, "");
Runtime.ConnectionsService.LoadConnections(true, false, "");
args.Handled = true;
}

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using mRemoteNG.Connection;
namespace mRemoteNG.Config.Connections
{
public class SaveConnectionsOnEdit
{
private readonly ConnectionsService _connectionsService;
public SaveConnectionsOnEdit(ConnectionsService connectionsService)
{
if (connectionsService == null)
throw new ArgumentNullException(nameof(connectionsService));
_connectionsService = connectionsService;
connectionsService.ConnectionsLoaded += ConnectionsServiceOnConnectionsLoaded;
}
private void ConnectionsServiceOnConnectionsLoaded(object sender, ConnectionsLoadedEventArgs connectionsLoadedEventArgs)
{
connectionsLoadedEventArgs.NewConnectionTreeModel.CollectionChanged += ConnectionTreeModelOnCollectionChanged;
connectionsLoadedEventArgs.NewConnectionTreeModel.PropertyChanged += ConnectionTreeModelOnPropertyChanged;
foreach (var oldTree in connectionsLoadedEventArgs.PreviousConnectionTreeModel)
{
oldTree.CollectionChanged -= ConnectionTreeModelOnCollectionChanged;
oldTree.PropertyChanged -= ConnectionTreeModelOnPropertyChanged;
}
}
private void ConnectionTreeModelOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
SaveConnectionOnEdit();
}
private void ConnectionTreeModelOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
SaveConnectionOnEdit();
}
private void SaveConnectionOnEdit()
{
if (!mRemoteNG.Settings.Default.SaveConnectionsAfterEveryEdit)
return;
_connectionsService.SaveConnections();
}
}
}

View File

@@ -8,6 +8,7 @@ using mRemoteNG.Config.Connections.Multiuser;
using mRemoteNG.Config.Putty;
using mRemoteNG.Connection.Protocol;
using mRemoteNG.Security;
using mRemoteNG.Tools;
using mRemoteNG.Tree;
using mRemoteNG.Tree.Root;
@@ -23,11 +24,12 @@ namespace mRemoteNG.Connection
public RemoteConnectionsSyncronizer RemoteConnectionsSyncronizer { get; set; }
public DateTime LastSqlUpdate { get; set; }
public ConnectionTreeModel ConnectionTreeModel
{
get { return Windows.TreeForm.ConnectionTree.ConnectionTreeModel; }
set { Windows.TreeForm.ConnectionTree.ConnectionTreeModel = value; }
}
public ConnectionTreeModel ConnectionTreeModel { get; private set; }
//public ConnectionTreeModel ConnectionTreeModel
//{
// get { return Windows.TreeForm.ConnectionTree.ConnectionTreeModel; }
// set { Windows.TreeForm.ConnectionTree.ConnectionTreeModel = value; }
//}
public ConnectionsService(PuttySessionsManager puttySessionsManager)
{
@@ -209,7 +211,7 @@ namespace mRemoteNG.Connection
public event EventHandler<ConnectionsLoadedEventArgs> ConnectionsLoaded;
public event EventHandler<ConnectionsSavedEventArgs> ConnectionsSaved;
private void RaiseConnectionsLoadedEvent(ConnectionTreeModel previousTreeModel, ConnectionTreeModel newTreeModel,
private void RaiseConnectionsLoadedEvent(Maybe<ConnectionTreeModel> previousTreeModel, ConnectionTreeModel newTreeModel,
bool previousSourceWasDatabase, bool newSourceIsDatabase,
string newSourcePath)
{

View File

@@ -35,7 +35,12 @@ namespace mRemoteNG.Tools
return _maybe.Any() ? _maybe.First().ToString() : "";
}
public static Maybe<TOut> FromNullable<TOut>(TOut? value) where TOut : struct
public static implicit operator Maybe<T>(T value)
{
return new Maybe<T>(value);
}
public static Maybe<TOut> FromNullable<TOut>(TOut? value) where TOut : struct
{
return value.HasValue
? new Maybe<TOut>(value.Value)

View File

@@ -6,6 +6,7 @@ using System.Linq;
using System.Windows.Forms;
using BrightIdeasSoftware;
using mRemoteNG.App;
using mRemoteNG.Config.Connections;
using mRemoteNG.Config.Putty;
using mRemoteNG.Connection;
using mRemoteNG.Container;
@@ -17,7 +18,6 @@ namespace mRemoteNG.UI.Controls
{
public partial class ConnectionTree : TreeListView, IConnectionTree
{
private ConnectionTreeModel _connectionTreeModel;
private readonly ConnectionTreeDragAndDropHandler _dragAndDropHandler = new ConnectionTreeDragAndDropHandler();
private readonly PuttySessionsManager _puttySessionsManager = PuttySessionsManager.Instance;
private readonly StatusImageList _statusImageList = new StatusImageList();
@@ -37,15 +37,7 @@ namespace mRemoteNG.UI.Controls
public ITreeNodeClickHandler<ConnectionInfo> SingleClickHandler { get; set; } = new TreeNodeCompositeClickHandler();
public ConnectionTreeModel ConnectionTreeModel
{
get { return _connectionTreeModel; }
set
{
_connectionTreeModel = value;
PopulateTreeView();
}
}
public ConnectionTreeModel ConnectionTreeModel { get; set; }
public ConnectionTree()
{
@@ -54,12 +46,15 @@ namespace mRemoteNG.UI.Controls
UseOverlays = false;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
components?.Dispose();
_statusImageList?.Dispose();
Runtime.ConnectionsService.ConnectionsLoaded -= ConnectionsServiceOnConnectionsLoaded;
}
base.Dispose(disposing);
}
@@ -124,6 +119,7 @@ namespace mRemoteNG.UI.Controls
ModelDropped += _dragAndDropHandler.HandleEvent_ModelDropped;
BeforeLabelEdit += OnBeforeLabelEdit;
AfterLabelEdit += OnAfterLabelEdit;
Runtime.ConnectionsService.ConnectionsLoaded += ConnectionsServiceOnConnectionsLoaded;
}
/// <summary>
@@ -211,6 +207,12 @@ namespace mRemoteNG.UI.Controls
action.Execute(this);
}
}
private void ConnectionsServiceOnConnectionsLoaded(object o, ConnectionsLoadedEventArgs connectionsLoadedEventArgs)
{
ConnectionTreeModel = connectionsLoadedEventArgs.NewConnectionTreeModel;
PopulateTreeView();
}
#endregion
#region ConnectionTree Behavior