1
0
mirror of synced 2024-12-22 01:13:09 +01:00

Added saving of last connection settings

This commit is contained in:
PsychoMark 2016-06-21 17:05:14 +02:00
parent 04359a4d74
commit 12d2161117
10 changed files with 147 additions and 81 deletions

View File

@ -1,10 +1,13 @@
using System.Globalization;
using System.Linq;
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;
using Newtonsoft.Json;
using PettingZoo.Model;
using PettingZoo.View;
using PettingZoo.ViewModel;
using SimpleInjector;
namespace PettingZoo
@ -26,16 +29,16 @@ namespace PettingZoo
{
var container = new Container();
container.RegisterSingleton(() => new UserSettings(new AppDataSettingsSerializer("Settings.json")));
container.Register<IConnectionFactory, RabbitMQClientConnectionFactory>();
container.Register<IConnectionInfoBuilder, WindowConnectionInfoBuilder>();
// Automatically register all Window and BaseViewModel descendants
foreach (var type in Assembly.GetExecutingAssembly().GetExportedTypes()
.Where(t => t.IsSubclassOf(typeof(Window)) ||
t.IsSubclassOf(typeof(Infrastructure.BaseViewModel))))
{
container.Register(type);
}
container.Register<MainWindow>();
container.Register<MainViewModel>();
// Note: don't run Verify! It'll create a MainWindow which will then become
// Application.Current.MainWindow and prevent the process from shutting down.
return container;
}
@ -48,5 +51,47 @@ namespace PettingZoo
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];
}
}
}
}

View File

@ -10,19 +10,5 @@
public string Exchange { get; set; }
public string RoutingKey { get; set; }
public static ConnectionInfo Default()
{
return new ConnectionInfo
{
Host = "localhost",
Port = 5672,
VirtualHost = "/",
Username = "guest",
Password = "guest",
RoutingKey = "#"
};
}
}
}

View File

@ -64,7 +64,7 @@ namespace PettingZoo.Model
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)
{

58
Model/UserSettings.cs Normal file
View 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);
}
}
}

View File

@ -74,6 +74,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
@ -117,6 +118,7 @@
<Compile Include="Infrastructure\GridLayout.cs" />
<Compile Include="Infrastructure\ListBoxAutoScroll.cs" />
<Compile Include="Infrastructure\PasswordBoxAssistant.cs" />
<Compile Include="Model\UserSettings.cs" />
<Compile Include="Model\IConnection.cs" />
<Compile Include="Model\IConnectionFactory.cs" />
<Compile Include="Model\IConnectionInfoBuilder.cs" />
@ -145,20 +147,11 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>

View File

@ -9,9 +9,6 @@
//------------------------------------------------------------------------------
namespace PettingZoo.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>

View File

@ -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;
}
}
}
}

View File

@ -1,7 +0,0 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
</SettingsFile>

View File

@ -1,5 +1,6 @@
using System.Windows;
using System.Windows.Input;
using AutoMapper;
using PettingZoo.Model;
using PettingZoo.ViewModel;
@ -7,9 +8,27 @@ namespace PettingZoo.View
{
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()
{
var viewModel = new ConnectionViewModel(ConnectionInfo.Default());
var connectionInfo = ConnectionInfoMapper.Map<ConnectionInfo>(userSettings.ConnectionWindow);
var viewModel = new ConnectionViewModel(connectionInfo);
var dialog = new ConnectionWindow(viewModel)
{
@ -21,7 +40,14 @@ namespace PettingZoo.View
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;
}
}

View File

@ -6,13 +6,11 @@ using PettingZoo.Model;
namespace PettingZoo.ViewModel
{
public class ConnectionViewModel
public class ConnectionViewModel : BaseViewModel
{
private static readonly IMapper ModelMapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ConnectionInfo, ConnectionViewModel>();
cfg.CreateMap<ConnectionViewModel, ConnectionInfo>();
}).CreateMapper();
private static readonly IMapper ModelMapper = new MapperConfiguration(cfg =>
cfg.CreateMap<ConnectionInfo, ConnectionViewModel>().ReverseMap()
).CreateMapper();
private readonly DelegateCommand okCommand;