Added saving of last connection settings
This commit is contained in:
parent
04359a4d74
commit
12d2161117
63
App.xaml.cs
63
App.xaml.cs
@ -1,10 +1,13 @@
|
|||||||
using System.Globalization;
|
using System;
|
||||||
using System.Linq;
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Markup;
|
using System.Windows.Markup;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using PettingZoo.Model;
|
using PettingZoo.Model;
|
||||||
using PettingZoo.View;
|
using PettingZoo.View;
|
||||||
|
using PettingZoo.ViewModel;
|
||||||
using SimpleInjector;
|
using SimpleInjector;
|
||||||
|
|
||||||
namespace PettingZoo
|
namespace PettingZoo
|
||||||
@ -26,16 +29,16 @@ namespace PettingZoo
|
|||||||
{
|
{
|
||||||
var container = new Container();
|
var container = new Container();
|
||||||
|
|
||||||
|
container.RegisterSingleton(() => new UserSettings(new AppDataSettingsSerializer("Settings.json")));
|
||||||
|
|
||||||
container.Register<IConnectionFactory, RabbitMQClientConnectionFactory>();
|
container.Register<IConnectionFactory, RabbitMQClientConnectionFactory>();
|
||||||
container.Register<IConnectionInfoBuilder, WindowConnectionInfoBuilder>();
|
container.Register<IConnectionInfoBuilder, WindowConnectionInfoBuilder>();
|
||||||
|
|
||||||
// Automatically register all Window and BaseViewModel descendants
|
container.Register<MainWindow>();
|
||||||
foreach (var type in Assembly.GetExecutingAssembly().GetExportedTypes()
|
container.Register<MainViewModel>();
|
||||||
.Where(t => t.IsSubclassOf(typeof(Window)) ||
|
|
||||||
t.IsSubclassOf(typeof(Infrastructure.BaseViewModel))))
|
// Note: don't run Verify! It'll create a MainWindow which will then become
|
||||||
{
|
// Application.Current.MainWindow and prevent the process from shutting down.
|
||||||
container.Register(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
@ -48,5 +51,47 @@ namespace PettingZoo
|
|||||||
|
|
||||||
mainWindow.Show();
|
mainWindow.Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private class AppDataSettingsSerializer : IUserSettingsSerializer
|
||||||
|
{
|
||||||
|
private readonly string path;
|
||||||
|
private readonly string fullPath;
|
||||||
|
|
||||||
|
|
||||||
|
public AppDataSettingsSerializer(string filename)
|
||||||
|
{
|
||||||
|
var companyName = GetProductInfo<AssemblyCompanyAttribute>().Company;
|
||||||
|
var productName = GetProductInfo<AssemblyProductAttribute>().Product;
|
||||||
|
|
||||||
|
path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||||
|
companyName, productName);
|
||||||
|
fullPath = Path.Combine(path, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Read(UserSettings settings)
|
||||||
|
{
|
||||||
|
if (File.Exists(fullPath))
|
||||||
|
JsonConvert.PopulateObject(File.ReadAllText(fullPath), settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Write(UserSettings settings)
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(path);
|
||||||
|
File.WriteAllText(fullPath, JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private T GetProductInfo<T>()
|
||||||
|
{
|
||||||
|
var attributes = GetType().Assembly.GetCustomAttributes(typeof(T), true);
|
||||||
|
if (attributes.Length == 0)
|
||||||
|
throw new Exception("Missing product information in assembly");
|
||||||
|
|
||||||
|
return (T)attributes[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,19 +10,5 @@
|
|||||||
|
|
||||||
public string Exchange { get; set; }
|
public string Exchange { get; set; }
|
||||||
public string RoutingKey { get; set; }
|
public string RoutingKey { get; set; }
|
||||||
|
|
||||||
|
|
||||||
public static ConnectionInfo Default()
|
|
||||||
{
|
|
||||||
return new ConnectionInfo
|
|
||||||
{
|
|
||||||
Host = "localhost",
|
|
||||||
Port = 5672,
|
|
||||||
VirtualHost = "/",
|
|
||||||
Username = "guest",
|
|
||||||
Password = "guest",
|
|
||||||
RoutingKey = "#"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ namespace PettingZoo.Model
|
|||||||
Password = connectionInfo.Password
|
Password = connectionInfo.Password
|
||||||
};
|
};
|
||||||
|
|
||||||
var statusContext = String.Format(@"{0}:{1}{2}", connectionInfo.Host, connectionInfo.Port, connectionInfo.VirtualHost);
|
var statusContext = String.Format("{0}:{1}{2}", connectionInfo.Host, connectionInfo.Port, connectionInfo.VirtualHost);
|
||||||
|
|
||||||
while (!cancellationToken.IsCancellationRequested)
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
|
58
Model/UserSettings.cs
Normal file
58
Model/UserSettings.cs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
namespace PettingZoo.Model
|
||||||
|
{
|
||||||
|
public interface IUserSettingsSerializer
|
||||||
|
{
|
||||||
|
void Read(UserSettings settings);
|
||||||
|
void Write(UserSettings settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class ConnectionWindowSettings
|
||||||
|
{
|
||||||
|
public string LastHost { get; set; }
|
||||||
|
public string LastVirtualHost { get; set; }
|
||||||
|
public int LastPort { get; set; }
|
||||||
|
public string LastUsername { get; set; }
|
||||||
|
public string LastPassword { get; set; }
|
||||||
|
|
||||||
|
public string LastExchange { get; set; }
|
||||||
|
public string LastRoutingKey { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public ConnectionWindowSettings()
|
||||||
|
{
|
||||||
|
LastHost = "localhost";
|
||||||
|
LastPort = 5672;
|
||||||
|
LastVirtualHost = "/";
|
||||||
|
LastUsername = "guest";
|
||||||
|
LastPassword = "guest";
|
||||||
|
|
||||||
|
LastExchange = "amqp";
|
||||||
|
LastRoutingKey = "#";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class UserSettings
|
||||||
|
{
|
||||||
|
public ConnectionWindowSettings ConnectionWindow { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
|
private readonly IUserSettingsSerializer serializer;
|
||||||
|
|
||||||
|
|
||||||
|
public UserSettings(IUserSettingsSerializer serializer)
|
||||||
|
{
|
||||||
|
ConnectionWindow = new ConnectionWindowSettings();
|
||||||
|
|
||||||
|
this.serializer = serializer;
|
||||||
|
serializer.Read(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void Save()
|
||||||
|
{
|
||||||
|
serializer.Write(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -74,6 +74,7 @@
|
|||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
@ -117,6 +118,7 @@
|
|||||||
<Compile Include="Infrastructure\GridLayout.cs" />
|
<Compile Include="Infrastructure\GridLayout.cs" />
|
||||||
<Compile Include="Infrastructure\ListBoxAutoScroll.cs" />
|
<Compile Include="Infrastructure\ListBoxAutoScroll.cs" />
|
||||||
<Compile Include="Infrastructure\PasswordBoxAssistant.cs" />
|
<Compile Include="Infrastructure\PasswordBoxAssistant.cs" />
|
||||||
|
<Compile Include="Model\UserSettings.cs" />
|
||||||
<Compile Include="Model\IConnection.cs" />
|
<Compile Include="Model\IConnection.cs" />
|
||||||
<Compile Include="Model\IConnectionFactory.cs" />
|
<Compile Include="Model\IConnectionFactory.cs" />
|
||||||
<Compile Include="Model\IConnectionInfoBuilder.cs" />
|
<Compile Include="Model\IConnectionInfoBuilder.cs" />
|
||||||
@ -145,20 +147,11 @@
|
|||||||
<DesignTime>True</DesignTime>
|
<DesignTime>True</DesignTime>
|
||||||
<DependentUpon>Resources.resx</DependentUpon>
|
<DependentUpon>Resources.resx</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Properties\Settings.Designer.cs">
|
|
||||||
<AutoGen>True</AutoGen>
|
|
||||||
<DependentUpon>Settings.settings</DependentUpon>
|
|
||||||
<DesignTimeSharedInput>True</DesignTimeSharedInput>
|
|
||||||
</Compile>
|
|
||||||
<EmbeddedResource Include="Properties\Resources.resx">
|
<EmbeddedResource Include="Properties\Resources.resx">
|
||||||
<Generator>PublicResXFileCodeGenerator</Generator>
|
<Generator>PublicResXFileCodeGenerator</Generator>
|
||||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
<None Include="Properties\Settings.settings">
|
|
||||||
<Generator>SettingsSingleFileGenerator</Generator>
|
|
||||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
|
||||||
</None>
|
|
||||||
<AppDesigner Include="Properties\" />
|
<AppDesigner Include="Properties\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
3
Properties/Resources.Designer.cs
generated
3
Properties/Resources.Designer.cs
generated
@ -9,9 +9,6 @@
|
|||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
namespace PettingZoo.Properties {
|
namespace PettingZoo.Properties {
|
||||||
using System;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A strongly-typed resource class, for looking up localized strings, etc.
|
/// A strongly-typed resource class, for looking up localized strings, etc.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
30
Properties/Settings.Designer.cs
generated
30
Properties/Settings.Designer.cs
generated
@ -1,30 +0,0 @@
|
|||||||
//------------------------------------------------------------------------------
|
|
||||||
// <auto-generated>
|
|
||||||
// This code was generated by a tool.
|
|
||||||
// Runtime Version:4.0.30319.42000
|
|
||||||
//
|
|
||||||
// Changes to this file may cause incorrect behavior and will be lost if
|
|
||||||
// the code is regenerated.
|
|
||||||
// </auto-generated>
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
namespace PettingZoo.Properties
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
|
||||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
|
|
||||||
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
|
|
||||||
{
|
|
||||||
|
|
||||||
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
|
|
||||||
|
|
||||||
public static Settings Default
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return defaultInstance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
<?xml version='1.0' encoding='utf-8'?>
|
|
||||||
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
|
|
||||||
<Profiles>
|
|
||||||
<Profile Name="(Default)" />
|
|
||||||
</Profiles>
|
|
||||||
<Settings />
|
|
||||||
</SettingsFile>
|
|
@ -1,5 +1,6 @@
|
|||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Input;
|
using System.Windows.Input;
|
||||||
|
using AutoMapper;
|
||||||
using PettingZoo.Model;
|
using PettingZoo.Model;
|
||||||
using PettingZoo.ViewModel;
|
using PettingZoo.ViewModel;
|
||||||
|
|
||||||
@ -7,9 +8,27 @@ namespace PettingZoo.View
|
|||||||
{
|
{
|
||||||
public class WindowConnectionInfoBuilder : IConnectionInfoBuilder
|
public class WindowConnectionInfoBuilder : IConnectionInfoBuilder
|
||||||
{
|
{
|
||||||
|
private readonly UserSettings userSettings;
|
||||||
|
|
||||||
|
private static readonly IMapper ConnectionInfoMapper = new MapperConfiguration(cfg =>
|
||||||
|
{
|
||||||
|
cfg.RecognizeDestinationPrefixes("Last");
|
||||||
|
cfg.RecognizePrefixes("Last");
|
||||||
|
|
||||||
|
cfg.CreateMap<ConnectionInfo, ConnectionWindowSettings>().ReverseMap();
|
||||||
|
}).CreateMapper();
|
||||||
|
|
||||||
|
|
||||||
|
public WindowConnectionInfoBuilder(UserSettings userSettings)
|
||||||
|
{
|
||||||
|
this.userSettings = userSettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public ConnectionInfo Build()
|
public ConnectionInfo Build()
|
||||||
{
|
{
|
||||||
var viewModel = new ConnectionViewModel(ConnectionInfo.Default());
|
var connectionInfo = ConnectionInfoMapper.Map<ConnectionInfo>(userSettings.ConnectionWindow);
|
||||||
|
var viewModel = new ConnectionViewModel(connectionInfo);
|
||||||
|
|
||||||
var dialog = new ConnectionWindow(viewModel)
|
var dialog = new ConnectionWindow(viewModel)
|
||||||
{
|
{
|
||||||
@ -21,7 +40,14 @@ namespace PettingZoo.View
|
|||||||
dialog.DialogResult = true;
|
dialog.DialogResult = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
return dialog.ShowDialog().GetValueOrDefault() ? viewModel.ToModel() : null;
|
connectionInfo = dialog.ShowDialog().GetValueOrDefault() ? viewModel.ToModel() : null;
|
||||||
|
if (connectionInfo != null)
|
||||||
|
{
|
||||||
|
ConnectionInfoMapper.Map(connectionInfo, userSettings.ConnectionWindow);
|
||||||
|
userSettings.Save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return connectionInfo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,13 +6,11 @@ using PettingZoo.Model;
|
|||||||
|
|
||||||
namespace PettingZoo.ViewModel
|
namespace PettingZoo.ViewModel
|
||||||
{
|
{
|
||||||
public class ConnectionViewModel
|
public class ConnectionViewModel : BaseViewModel
|
||||||
{
|
{
|
||||||
private static readonly IMapper ModelMapper = new MapperConfiguration(cfg =>
|
private static readonly IMapper ModelMapper = new MapperConfiguration(cfg =>
|
||||||
{
|
cfg.CreateMap<ConnectionInfo, ConnectionViewModel>().ReverseMap()
|
||||||
cfg.CreateMap<ConnectionInfo, ConnectionViewModel>();
|
).CreateMapper();
|
||||||
cfg.CreateMap<ConnectionViewModel, ConnectionInfo>();
|
|
||||||
}).CreateMapper();
|
|
||||||
|
|
||||||
|
|
||||||
private readonly DelegateCommand okCommand;
|
private readonly DelegateCommand okCommand;
|
||||||
|
Loading…
Reference in New Issue
Block a user