diff --git a/mRemoteV1/App/Initialization/CredsAndConsSetup.cs b/mRemoteV1/App/Initialization/CredsAndConsSetup.cs index a9f08c9f..af4b3329 100644 --- a/mRemoteV1/App/Initialization/CredsAndConsSetup.cs +++ b/mRemoteV1/App/Initialization/CredsAndConsSetup.cs @@ -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()); diff --git a/mRemoteV1/App/Runtime.cs b/mRemoteV1/App/Runtime.cs index 3903e4d7..5bc586a8 100644 --- a/mRemoteV1/App/Runtime.cs +++ b/mRemoteV1/App/Runtime.cs @@ -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); diff --git a/mRemoteV1/Config/Connections/ConnectionsLoadedEventArgs.cs b/mRemoteV1/Config/Connections/ConnectionsLoadedEventArgs.cs index 4e3e082d..2a0fb6eb 100644 --- a/mRemoteV1/Config/Connections/ConnectionsLoadedEventArgs.cs +++ b/mRemoteV1/Config/Connections/ConnectionsLoadedEventArgs.cs @@ -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 that is being /// unloaded. /// - public ConnectionTreeModel PreviousConnectionTreeModel { get; } + public Maybe PreviousConnectionTreeModel { get; } /// /// True if the previous was loaded from @@ -36,7 +37,7 @@ namespace mRemoteNG.Config.Connections public string NewSourcePath { get; } public ConnectionsLoadedEventArgs( - ConnectionTreeModel previousTreeModelModel, ConnectionTreeModel newTreeModelModel, + Maybe previousTreeModelModel, ConnectionTreeModel newTreeModelModel, bool previousSourceWasDatabase, bool newSourceIsDatabase, string newSourcePath) { diff --git a/mRemoteV1/Config/Connections/Multiuser/RemoteConnectionsSyncronizer.cs b/mRemoteV1/Config/Connections/Multiuser/RemoteConnectionsSyncronizer.cs index 93bdcfa0..a52ffea3 100644 --- a/mRemoteV1/Config/Connections/Multiuser/RemoteConnectionsSyncronizer.cs +++ b/mRemoteV1/Config/Connections/Multiuser/RemoteConnectionsSyncronizer.cs @@ -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; } diff --git a/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs b/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs new file mode 100644 index 00000000..91c76b98 --- /dev/null +++ b/mRemoteV1/Config/Connections/SaveConnectionsOnEdit.cs @@ -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(); + } + } +} diff --git a/mRemoteV1/Connection/ConnectionsService.cs b/mRemoteV1/Connection/ConnectionsService.cs index a75d7ed5..1f4f27c2 100644 --- a/mRemoteV1/Connection/ConnectionsService.cs +++ b/mRemoteV1/Connection/ConnectionsService.cs @@ -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 ConnectionsLoaded; public event EventHandler ConnectionsSaved; - private void RaiseConnectionsLoadedEvent(ConnectionTreeModel previousTreeModel, ConnectionTreeModel newTreeModel, + private void RaiseConnectionsLoadedEvent(Maybe previousTreeModel, ConnectionTreeModel newTreeModel, bool previousSourceWasDatabase, bool newSourceIsDatabase, string newSourcePath) { diff --git a/mRemoteV1/Tools/Maybe.cs b/mRemoteV1/Tools/Maybe.cs index 7e17ad9f..311591e4 100644 --- a/mRemoteV1/Tools/Maybe.cs +++ b/mRemoteV1/Tools/Maybe.cs @@ -35,7 +35,12 @@ namespace mRemoteNG.Tools return _maybe.Any() ? _maybe.First().ToString() : ""; } - public static Maybe FromNullable(TOut? value) where TOut : struct + public static implicit operator Maybe(T value) + { + return new Maybe(value); + } + + public static Maybe FromNullable(TOut? value) where TOut : struct { return value.HasValue ? new Maybe(value.Value) diff --git a/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs b/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs index f1f1acd4..103165ed 100644 --- a/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs +++ b/mRemoteV1/UI/Controls/ConnectionTree/ConnectionTree.cs @@ -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 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; } /// @@ -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