batch saves when importing connection files

This reduces saves/backup overwrites when importing multiple files
This commit is contained in:
David Sparer
2019-03-04 08:41:27 -06:00
parent 08201b0f00
commit 07a20ed5ad
4 changed files with 72 additions and 22 deletions

View File

@@ -9,7 +9,7 @@ using mRemoteNG.Tools;
namespace mRemoteNG.App
{
public static class Import
public static class Import
{
public static void ImportFromFile(ContainerInfo importDestinationContainer)
{
@@ -35,22 +35,23 @@ namespace mRemoteNG.App
if (openFileDialog.ShowDialog() != DialogResult.OK)
return;
foreach (var fileName in openFileDialog.FileNames)
using (Runtime.ConnectionsService.BatchedSavingContext())
{
try
{
var importer = BuildConnectionImporterFromFileExtension(fileName);
importer.Import(fileName, importDestinationContainer);
}
catch (Exception ex)
{
MessageBox.Show(string.Format(Language.strImportFileFailedContent, fileName), Language.strImportFileFailedMainInstruction,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
Runtime.MessageCollector.AddExceptionMessage("Unable to import file.", ex);
}
foreach (var fileName in openFileDialog.FileNames)
{
try
{
var importer = BuildConnectionImporterFromFileExtension(fileName);
importer.Import(fileName, importDestinationContainer);
}
catch (Exception ex)
{
MessageBox.Show(string.Format(Language.strImportFileFailedContent, fileName), Language.strImportFileFailedMainInstruction,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
Runtime.MessageCollector.AddExceptionMessage("Unable to import file.", ex);
}
}
}
Runtime.ConnectionsService.SaveConnectionsAsync();
}
}
catch (Exception ex)

View File

@@ -1,5 +1,10 @@
using mRemoteNG.App;
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using mRemoteNG.App;
using mRemoteNG.App.Info;
using mRemoteNG.Config;
using mRemoteNG.Config.Connections;
using mRemoteNG.Config.Connections.Multiuser;
using mRemoteNG.Config.DataProviders;
@@ -12,15 +17,10 @@ using mRemoteNG.Tools;
using mRemoteNG.Tree;
using mRemoteNG.Tree.Root;
using mRemoteNG.UI;
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using mRemoteNG.Config;
namespace mRemoteNG.Connection
{
public class ConnectionsService
public class ConnectionsService
{
private static readonly object SaveLock = new object();
private readonly PuttySessionsManager _puttySessionsManager;
@@ -171,6 +171,19 @@ namespace mRemoteNG.Connection
SaveConnections();
}
/// <summary>
/// All calls to <see cref="SaveConnections()"/> or <see cref="SaveConnectionsAsync"/>
/// will be deferred until the returned <see cref="DisposableAction"/> is disposed.
/// Once disposed, this will immediately executes a single <see cref="SaveConnections()"/>
/// or <see cref="SaveConnectionsAsync"/> if one has been requested.
/// Place this call in a 'using' block to represent a batched saving context.
/// </summary>
/// <returns></returns>
public DisposableAction BatchedSavingContext()
{
return new DisposableAction(BeginBatchingSaves, EndBatchingSaves);
}
/// <summary>
/// Saves the currently loaded <see cref="ConnectionTreeModel"/> with
/// no <see cref="SaveFilter"/>.

View File

@@ -0,0 +1,35 @@
using System;
namespace mRemoteNG.Tools
{
/// <summary>
/// Represents an action that will be executed when the <see cref="Dispose"/>
/// method is called. Useful for creating Using blocks around logical start/end
/// actions.
/// </summary>
public class DisposableAction : IDisposable
{
private readonly Action _disposeAction;
/// <summary>
///
/// </summary>
/// <param name="initializeAction">
/// An <see cref="Action"/> that should be performed immediately
/// when this object is initialized. It should return quickly.
/// </param>
/// <param name="disposeAction">
/// An <see cref="Action"/> to be executed when this object is disposed.
/// </param>
public DisposableAction(Action initializeAction, Action disposeAction)
{
initializeAction();
_disposeAction = disposeAction;
}
public void Dispose()
{
_disposeAction();
}
}
}

View File

@@ -318,6 +318,7 @@
<Compile Include="Tools\Cmdline\StartupArgumentsInterpreter.cs" />
<Compile Include="Tools\CustomCollections\CollectionUpdatedEventArgs.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Tools\DisposableAction.cs" />
<Compile Include="Tools\Extensions.cs" />
<Compile Include="Tools\ExternalToolArgumentParser.cs" />
<Compile Include="Tools\Cmdline\CmdArgumentsInterpreter.cs" />