From e7afe5ea93c2644584dd2a7fac6f792386eb60e1 Mon Sep 17 00:00:00 2001 From: David Sparer Date: Sat, 3 Mar 2018 17:34:45 -0600 Subject: [PATCH] made some references to the connection service non static --- .../Protocol/IntegratedProgramTests.cs | 6 ++-- .../UI/Forms/OptionsFormSetupAndTeardown.cs | 9 ++++-- .../App/Initialization/CredsAndConsSetup.cs | 21 +++++++++---- mRemoteV1/App/Runtime.cs | 3 +- mRemoteV1/App/Shutdown.cs | 7 +++-- mRemoteV1/App/Startup.cs | 10 +++--- mRemoteV1/App/Windows.cs | 7 +++-- .../UI/Forms/OptionsPages/SqlServerPage.cs | 20 +++++++----- mRemoteV1/UI/Forms/frmMain.cs | 31 ++++++++++--------- mRemoteV1/UI/Forms/frmOptions.cs | 10 +++--- 10 files changed, 76 insertions(+), 48 deletions(-) diff --git a/mRemoteNGTests/Connection/Protocol/IntegratedProgramTests.cs b/mRemoteNGTests/Connection/Protocol/IntegratedProgramTests.cs index 3ea086d7b..01812e510 100644 --- a/mRemoteNGTests/Connection/Protocol/IntegratedProgramTests.cs +++ b/mRemoteNGTests/Connection/Protocol/IntegratedProgramTests.cs @@ -1,6 +1,7 @@ using System; using System.Threading; using mRemoteNG.App; +using mRemoteNG.Config.Putty; using mRemoteNG.Config.Settings; using mRemoteNG.Connection; using mRemoteNG.Connection.Protocol; @@ -67,11 +68,12 @@ namespace mRemoteNGTests.Connection.Protocol connectionTreeWindow.ConnectionTreeContextMenu = connectionTreeContextMenu; var errorAndInfoWindow = new ErrorAndInfoWindow(new DockContent(), connectionTreeWindow); var screenshotManagerWindow = new ScreenshotManagerWindow(new DockContent()); - var shutdown = new Shutdown(new SettingsSaver(new ExternalToolsService())); + var shutdown = new Shutdown(new SettingsSaver(new ExternalToolsService()), new ConnectionsService(PuttySessionsManager.Instance)); Func updateWindowBuilder = () => new UpdateWindow(new DockContent(), shutdown); Func notificationAreaIconBuilder = () => new NotificationAreaIcon(FrmMain.Default, _connectionInitiator, shutdown); Func externalToolsWindowBuilder = () => new ExternalToolsWindow(_connectionInitiator, _externalToolsService); - var windows = new Windows(_connectionInitiator, connectionTreeWindow, configWindow, errorAndInfoWindow, screenshotManagerWindow, sshTransferWindow, updateWindowBuilder, notificationAreaIconBuilder, externalToolsWindowBuilder); + var windows = new Windows(_connectionInitiator, connectionTreeWindow, configWindow, errorAndInfoWindow, screenshotManagerWindow, + sshTransferWindow, updateWindowBuilder, notificationAreaIconBuilder, externalToolsWindowBuilder, Runtime.ConnectionsService); var connectionWindow = new ConnectionWindow(new DockContent(), _connectionInitiator, windows, _externalToolsService); var connectionInfo = new ConnectionInfo {ExtApp = extAppName}; return new InterfaceControl(connectionWindow, sut, connectionInfo); diff --git a/mRemoteNGTests/UI/Forms/OptionsFormSetupAndTeardown.cs b/mRemoteNGTests/UI/Forms/OptionsFormSetupAndTeardown.cs index e8063dc1f..95e96c6ba 100644 --- a/mRemoteNGTests/UI/Forms/OptionsFormSetupAndTeardown.cs +++ b/mRemoteNGTests/UI/Forms/OptionsFormSetupAndTeardown.cs @@ -1,4 +1,6 @@ -using mRemoteNG.App; +using System; +using mRemoteNG.App; +using mRemoteNG.Config.Putty; using mRemoteNG.Config.Settings; using mRemoteNG.Connection; using mRemoteNG.Tools; @@ -21,8 +23,9 @@ namespace mRemoteNGTests.UI.Forms public void Setup() { var connectionInitiator = Substitute.For(); - var shutdown = new Shutdown(new SettingsSaver(new ExternalToolsService())); - _optionsForm = new frmOptions(connectionInitiator, type => {}, () => new NotificationAreaIcon(FrmMain.Default, connectionInitiator, shutdown)); + var shutdown = new Shutdown(new SettingsSaver(new ExternalToolsService()), new ConnectionsService(PuttySessionsManager.Instance)); + Func notificationIconBuilder = () => new NotificationAreaIcon(FrmMain.Default, connectionInitiator, shutdown); + _optionsForm = new frmOptions(connectionInitiator, type => {}, notificationIconBuilder, Runtime.ConnectionsService); _optionsForm.Show(); } diff --git a/mRemoteV1/App/Initialization/CredsAndConsSetup.cs b/mRemoteV1/App/Initialization/CredsAndConsSetup.cs index 8e9dc623d..f4d57a84e 100644 --- a/mRemoteV1/App/Initialization/CredsAndConsSetup.cs +++ b/mRemoteV1/App/Initialization/CredsAndConsSetup.cs @@ -1,18 +1,27 @@ using System.IO; using mRemoteNG.Config.Connections; +using mRemoteNG.Connection; +using mRemoteNG.Tools; namespace mRemoteNG.App.Initialization { public class CredsAndConsSetup - { - public void LoadCredsAndCons() + { + private readonly ConnectionsService _connectionsService; + + public CredsAndConsSetup(ConnectionsService connectionsService) + { + _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); + } + + public void LoadCredsAndCons() { - new SaveConnectionsOnEdit(Runtime.ConnectionsService); + new SaveConnectionsOnEdit(_connectionsService); - if (Settings.Default.FirstStart && !Settings.Default.LoadConsFromCustomLocation && !File.Exists(Runtime.ConnectionsService.GetStartupConnectionFileName())) - Runtime.ConnectionsService.NewConnectionsFile(Runtime.ConnectionsService.GetStartupConnectionFileName()); + if (Settings.Default.FirstStart && !Settings.Default.LoadConsFromCustomLocation && !File.Exists(_connectionsService.GetStartupConnectionFileName())) + _connectionsService.NewConnectionsFile(_connectionsService.GetStartupConnectionFileName()); - Runtime.ConnectionsService.LoadConnections(); + _connectionsService.LoadConnections(); } } } \ No newline at end of file diff --git a/mRemoteV1/App/Runtime.cs b/mRemoteV1/App/Runtime.cs index 2ae97c8aa..d127192ab 100644 --- a/mRemoteV1/App/Runtime.cs +++ b/mRemoteV1/App/Runtime.cs @@ -1,4 +1,3 @@ -using mRemoteNG.Config.Putty; using mRemoteNG.Connection; using mRemoteNG.Messages; using mRemoteNG.Tools; @@ -21,6 +20,6 @@ namespace mRemoteNG.App public static MessageCollector MessageCollector { get; } = new MessageCollector(); public static NotificationAreaIcon NotificationAreaIcon { get; set; } - public static ConnectionsService ConnectionsService { get; } = new ConnectionsService(PuttySessionsManager.Instance); + public static ConnectionsService ConnectionsService { get; set; } } } \ No newline at end of file diff --git a/mRemoteV1/App/Shutdown.cs b/mRemoteV1/App/Shutdown.cs index 07b2375f1..7e0c1883e 100644 --- a/mRemoteV1/App/Shutdown.cs +++ b/mRemoteV1/App/Shutdown.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Windows.Forms; using mRemoteNG.Config.Putty; using mRemoteNG.Config.Settings; +using mRemoteNG.Connection; using mRemoteNG.UI.Controls; using mRemoteNG.UI.Forms; // ReSharper disable ArrangeAccessorOwnerBody @@ -13,11 +14,13 @@ namespace mRemoteNG.App public class Shutdown { private readonly SettingsSaver _settingsSaver; + private readonly ConnectionsService _connectionsService; private static string _updateFilePath; - public Shutdown(SettingsSaver settingsSaver) + public Shutdown(SettingsSaver settingsSaver, ConnectionsService connectionsService) { _settingsSaver = settingsSaver.ThrowIfNull(nameof(settingsSaver)); + _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); } private static bool UpdatePending @@ -62,7 +65,7 @@ namespace mRemoteNG.App private void SaveConnections() { if (Settings.Default.SaveConsOnExit) - Runtime.ConnectionsService.SaveConnections(); + _connectionsService.SaveConnections(); } private void SaveSettings(Control quickConnectToolStrip, ExternalToolsToolStrip externalToolsToolStrip, MultiSshToolStrip multiSshToolStrip, FrmMain frmMain) diff --git a/mRemoteV1/App/Startup.cs b/mRemoteV1/App/Startup.cs index 0cefa1fbf..c19104996 100644 --- a/mRemoteV1/App/Startup.cs +++ b/mRemoteV1/App/Startup.cs @@ -23,11 +23,13 @@ namespace mRemoteNG.App private readonly ConnectionIconLoader _connectionIconLoader; private readonly FrmMain _frmMain; private readonly Windows _windows; + private readonly ConnectionsService _connectionsService; - public Startup(FrmMain frmMain, Windows windows) + public Startup(FrmMain frmMain, Windows windows, ConnectionsService connectionsService) { - _frmMain = frmMain.ThrowIfNull(nameof(frmMain)); + _frmMain = frmMain.ThrowIfNull(nameof(frmMain)); _windows = windows.ThrowIfNull(nameof(windows)); + _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); _appUpdate = new AppUpdater(); _connectionIconLoader = new ConnectionIconLoader(GeneralAppInfo.HomePath + "\\Icons\\"); } @@ -56,8 +58,8 @@ namespace mRemoteNG.App messageCollector.AddMessage(MessageClass.DebugMsg, "Determining if we need a database syncronizer"); if (!Settings.Default.UseSQLServer) return; messageCollector.AddMessage(MessageClass.DebugMsg, "Creating database syncronizer"); - Runtime.ConnectionsService.RemoteConnectionsSyncronizer = new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker()); - Runtime.ConnectionsService.RemoteConnectionsSyncronizer.Enable(); + _connectionsService.RemoteConnectionsSyncronizer = new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker()); + _connectionsService.RemoteConnectionsSyncronizer.Enable(); } public void CheckForUpdate() diff --git a/mRemoteV1/App/Windows.cs b/mRemoteV1/App/Windows.cs index 71981e17d..91264b325 100644 --- a/mRemoteV1/App/Windows.cs +++ b/mRemoteV1/App/Windows.cs @@ -23,6 +23,7 @@ namespace mRemoteNG.App private readonly Func _updateWindowBuilder; private readonly Func _notificationAreaIconBuilder; private readonly Func _externalToolsWindowBuilder; + private readonly ConnectionsService _connectionsService; internal ConnectionTreeWindow TreeForm { get; } internal ConfigWindow ConfigForm { get; } @@ -39,7 +40,8 @@ namespace mRemoteNG.App SSHTransferWindow sshtransferForm, Func updateWindowBuilder, Func notificationAreaIconBuilder, - Func externalToolsWindowBuilder) + Func externalToolsWindowBuilder, + ConnectionsService connectionsService) { _connectionInitiator = connectionInitiator.ThrowIfNull(nameof(connectionInitiator)); TreeForm = treeForm.ThrowIfNull(nameof(treeForm)); @@ -50,6 +52,7 @@ namespace mRemoteNG.App _updateWindowBuilder = updateWindowBuilder; _notificationAreaIconBuilder = notificationAreaIconBuilder; _externalToolsWindowBuilder = externalToolsWindowBuilder; + _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); } public void Show(WindowType windowType) @@ -71,7 +74,7 @@ namespace mRemoteNG.App _adimportForm.Show(dockPanel); break; case WindowType.Options: - using (var optionsForm = new frmOptions(_connectionInitiator, Show, _notificationAreaIconBuilder)) + using (var optionsForm = new frmOptions(_connectionInitiator, Show, _notificationAreaIconBuilder, _connectionsService)) { optionsForm.ShowDialog(dockPanel); } diff --git a/mRemoteV1/UI/Forms/OptionsPages/SqlServerPage.cs b/mRemoteV1/UI/Forms/OptionsPages/SqlServerPage.cs index a1c87ea22..175d610fc 100644 --- a/mRemoteV1/UI/Forms/OptionsPages/SqlServerPage.cs +++ b/mRemoteV1/UI/Forms/OptionsPages/SqlServerPage.cs @@ -3,16 +3,20 @@ using mRemoteNG.App; using mRemoteNG.Config.Connections; using mRemoteNG.Config.Connections.Multiuser; using mRemoteNG.Config.DatabaseConnectors; +using mRemoteNG.Connection; using mRemoteNG.Security.SymmetricEncryption; +using mRemoteNG.Tools; namespace mRemoteNG.UI.Forms.OptionsPages { public partial class SqlServerPage { private readonly SqlDatabaseConnectionTester _databaseConnectionTester; + private readonly ConnectionsService _connectionsService; - public SqlServerPage() + public SqlServerPage(ConnectionsService connectionsService) { + _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); InitializeComponent(); base.ApplyTheme(); _databaseConnectionTester = new SqlDatabaseConnectionTester(); @@ -75,18 +79,18 @@ namespace mRemoteNG.UI.Forms.OptionsPages Settings.Default.Save(); } - private static void ReinitializeSqlUpdater() + private void ReinitializeSqlUpdater() { - Runtime.ConnectionsService.RemoteConnectionsSyncronizer?.Dispose(); - Runtime.ConnectionsService.RemoteConnectionsSyncronizer = new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker()); - Runtime.ConnectionsService.RemoteConnectionsSyncronizer.Enable(); + _connectionsService.RemoteConnectionsSyncronizer?.Dispose(); + _connectionsService.RemoteConnectionsSyncronizer = new RemoteConnectionsSyncronizer(new SqlConnectionsUpdateChecker()); + _connectionsService.RemoteConnectionsSyncronizer.Enable(); } private void DisableSql() { - Runtime.ConnectionsService.RemoteConnectionsSyncronizer?.Dispose(); - Runtime.ConnectionsService.RemoteConnectionsSyncronizer = null; - Runtime.ConnectionsService.LoadConnections(true); + _connectionsService.RemoteConnectionsSyncronizer?.Dispose(); + _connectionsService.RemoteConnectionsSyncronizer = null; + _connectionsService.LoadConnections(true); } private void chkUseSQLServer_CheckedChanged(object sender, EventArgs e) diff --git a/mRemoteV1/UI/Forms/frmMain.cs b/mRemoteV1/UI/Forms/frmMain.cs index 376b2c2fb..b00bcf9b9 100644 --- a/mRemoteV1/UI/Forms/frmMain.cs +++ b/mRemoteV1/UI/Forms/frmMain.cs @@ -50,7 +50,6 @@ namespace mRemoteNG.UI.Forms private ConnectionInfo _selectedConnection; private readonly IList _messageWriters = new List(); private readonly ThemeManager _themeManager; - private readonly Runtime _runtime; private readonly ConnectionInitiator _connectionInitiator; private readonly PanelAdder _panelAdder; private readonly WebHelper _webHelper; @@ -63,6 +62,7 @@ namespace mRemoteNG.UI.Forms private readonly Shutdown _shutdown; private readonly ICredentialRepositoryList _credentialRepositoryList; private readonly Func _notificationAreaIconBuilder; + private readonly ConnectionsService _connectionsService; internal FullscreenHandler Fullscreen { get; set; } @@ -71,11 +71,12 @@ namespace mRemoteNG.UI.Forms private FrmMain() { - _runtime = new Runtime(); _windowList = new WindowList(); _credentialRepositoryList = new CredentialRepositoryList(); var externalToolsService = new ExternalToolsService(); - ExternalToolsTypeConverter.ExternalToolsService = externalToolsService; + _connectionsService = new ConnectionsService(PuttySessionsManager.Instance); + + ExternalToolsTypeConverter.ExternalToolsService = externalToolsService; _export = new Export(_credentialRepositoryList); _connectionInitiator = new ConnectionInitiator(_windowList, externalToolsService); _webHelper = new WebHelper(_connectionInitiator); @@ -90,16 +91,16 @@ namespace mRemoteNG.UI.Forms var errorAndInfoWindow = new ErrorAndInfoWindow(new DockContent(), connectionTreeWindow); var screenshotManagerWindow = new ScreenshotManagerWindow(new DockContent()); _settingsSaver = new SettingsSaver(externalToolsService); - _shutdown = new Shutdown(_settingsSaver); + _shutdown = new Shutdown(_settingsSaver, _connectionsService); Func updateWindowBuilder = () => new UpdateWindow(new DockContent(), _shutdown); _notificationAreaIconBuilder = () => new NotificationAreaIcon(this, _connectionInitiator, _shutdown); Func externalToolsWindowBuilder = () => new ExternalToolsWindow(_connectionInitiator, externalToolsService); - _windows = new Windows(_connectionInitiator, connectionTreeWindow, configWindow, errorAndInfoWindow, screenshotManagerWindow, sshTransferWindow, updateWindowBuilder, _notificationAreaIconBuilder, externalToolsWindowBuilder); + _windows = new Windows(_connectionInitiator, connectionTreeWindow, configWindow, errorAndInfoWindow, screenshotManagerWindow, sshTransferWindow, updateWindowBuilder, _notificationAreaIconBuilder, externalToolsWindowBuilder, _connectionsService); Func connectionWindowBuilder = () => new ConnectionWindow(new DockContent(), _connectionInitiator, _windows, externalToolsService); _panelAdder = new PanelAdder(_windowList, connectionWindowBuilder); _showFullPathInTitle = Settings.Default.ShowCompleteConsPathInTitle; _connectionInitiator.Adder = _panelAdder; - _startup = new Startup(this, _windows); + _startup = new Startup(this, _windows, _connectionsService); connectionTreeContextMenu.ShowWindowAction = _windows.Show; InitializeComponent(); @@ -215,8 +216,8 @@ namespace mRemoteNG.UI.Forms if (Settings.Default.ResetPanels) SetDefaultLayout(); - Runtime.ConnectionsService.ConnectionsLoaded += ConnectionsServiceOnConnectionsLoaded; - var credsAndConsSetup = new CredsAndConsSetup(); + _connectionsService.ConnectionsLoaded += ConnectionsServiceOnConnectionsLoaded; + var credsAndConsSetup = new CredsAndConsSetup(_connectionsService); credsAndConsSetup.LoadCredsAndCons(); _windows.TreeForm.Focus(); @@ -334,7 +335,7 @@ namespace mRemoteNG.UI.Forms if (CTaskDialog.CommandButtonResult != 1) return; - using (var optionsForm = new frmOptions(_connectionInitiator, _windows.Show, _notificationAreaIconBuilder, Language.strTabUpdates)) + using (var optionsForm = new frmOptions(_connectionInitiator, _windows.Show, _notificationAreaIconBuilder, _connectionsService, Language.strTabUpdates)) { optionsForm.ShowDialog(this); } @@ -403,7 +404,7 @@ namespace mRemoteNG.UI.Forms private void tmrAutoSave_Tick(object sender, EventArgs e) { Runtime.MessageCollector.AddMessage(MessageClass.DebugMsg, "Doing AutoSave"); - Runtime.ConnectionsService.SaveConnectionsAsync(); + _connectionsService.SaveConnectionsAsync(); } #endregion @@ -556,21 +557,21 @@ namespace mRemoteNG.UI.Forms var titleBuilder = new StringBuilder(Application.ProductName); const string separator = " - "; - if (Runtime.ConnectionsService.IsConnectionsFileLoaded) + if (_connectionsService.IsConnectionsFileLoaded) { - if (Runtime.ConnectionsService.UsingDatabase) + if (_connectionsService.UsingDatabase) { titleBuilder.Append(separator); titleBuilder.Append(Language.strSQLServer.TrimEnd(':')); } else { - if (!string.IsNullOrEmpty(Runtime.ConnectionsService.ConnectionFileName)) + if (!string.IsNullOrEmpty(_connectionsService.ConnectionFileName)) { titleBuilder.Append(separator); titleBuilder.Append(Settings.Default.ShowCompleteConsPathInTitle - ? Runtime.ConnectionsService.ConnectionFileName - : Path.GetFileName(Runtime.ConnectionsService.ConnectionFileName)); + ? _connectionsService.ConnectionFileName + : Path.GetFileName(_connectionsService.ConnectionFileName)); } } } diff --git a/mRemoteV1/UI/Forms/frmOptions.cs b/mRemoteV1/UI/Forms/frmOptions.cs index 6276886a5..f435f343d 100644 --- a/mRemoteV1/UI/Forms/frmOptions.cs +++ b/mRemoteV1/UI/Forms/frmOptions.cs @@ -17,17 +17,19 @@ namespace mRemoteNG.UI.Forms private readonly IConnectionInitiator _connectionInitiator; private readonly Action _showWindowAction; private readonly Func _notificationAreaIconBuilder; + private readonly ConnectionsService _connectionsService; - public frmOptions(IConnectionInitiator connectionInitiator, Action showWindowAction, Func notificationAreaIconBuilder) - : this(connectionInitiator, showWindowAction, notificationAreaIconBuilder, Language.strStartupExit) + public frmOptions(IConnectionInitiator connectionInitiator, Action showWindowAction, Func notificationAreaIconBuilder, ConnectionsService connectionsService) + : this(connectionInitiator, showWindowAction, notificationAreaIconBuilder, connectionsService, Language.strStartupExit) { } - public frmOptions(IConnectionInitiator connectionInitiator, Action showWindowAction, Func notificationAreaIconBuilder, string pageName) + public frmOptions(IConnectionInitiator connectionInitiator, Action showWindowAction, Func notificationAreaIconBuilder, ConnectionsService connectionsService, string pageName) { _connectionInitiator = connectionInitiator.ThrowIfNull(nameof(connectionInitiator)); _showWindowAction = showWindowAction.ThrowIfNull(nameof(showWindowAction)); _notificationAreaIconBuilder = notificationAreaIconBuilder; + _connectionsService = connectionsService.ThrowIfNull(nameof(connectionsService)); _pageName = pageName.ThrowIfNull(nameof(pageName)); InitializeComponent(); } @@ -73,7 +75,7 @@ namespace mRemoteNG.UI.Forms {typeof(NotificationsPage).Name, new NotificationsPage()}, {typeof(ConnectionsPage).Name, new ConnectionsPage()}, {typeof(CredentialsPage).Name, new CredentialsPage()}, - {typeof(SqlServerPage).Name, new SqlServerPage()}, + {typeof(SqlServerPage).Name, new SqlServerPage(_connectionsService)}, {typeof(UpdatesPage).Name, new UpdatesPage(_showWindowAction)}, {typeof(ThemePage).Name, new ThemePage()}, {typeof(SecurityPage).Name, new SecurityPage()},