Allow users to have multiple configuration files
This commit is contained in:
@@ -7,21 +7,23 @@ namespace EveOPreview.Configuration
|
||||
{
|
||||
private const string ConfigurationFileName = "EVE-O Preview.json";
|
||||
|
||||
private readonly IAppConfig _configuration;
|
||||
private readonly IThumbnailConfig _configuration;
|
||||
|
||||
public ConfigurationStorage(IAppConfig configuration)
|
||||
public ConfigurationStorage(IThumbnailConfig configuration)
|
||||
{
|
||||
this._configuration = configuration;
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
if (!File.Exists(ConfigurationStorage.ConfigurationFileName))
|
||||
string filename = this.GetConfigFileName();
|
||||
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string rawData = File.ReadAllText(ConfigurationStorage.ConfigurationFileName);
|
||||
string rawData = File.ReadAllText(filename);
|
||||
|
||||
JsonConvert.PopulateObject(rawData, this._configuration);
|
||||
|
||||
@@ -33,7 +35,12 @@ namespace EveOPreview.Configuration
|
||||
{
|
||||
string rawData = JsonConvert.SerializeObject(this._configuration, Formatting.Indented);
|
||||
|
||||
File.WriteAllText(ConfigurationStorage.ConfigurationFileName, rawData);
|
||||
File.WriteAllText(this.GetConfigFileName(), rawData);
|
||||
}
|
||||
|
||||
private string GetConfigFileName()
|
||||
{
|
||||
return string.IsNullOrEmpty(this._configuration.ConfigFileName) ? ConfigurationStorage.ConfigurationFileName : this._configuration.ConfigFileName;
|
||||
}
|
||||
}
|
||||
}
|
@@ -3,8 +3,10 @@ using System.Windows.Forms;
|
||||
|
||||
namespace EveOPreview.Configuration
|
||||
{
|
||||
public interface IAppConfig
|
||||
public interface IThumbnailConfig
|
||||
{
|
||||
string ConfigFileName { get; set; }
|
||||
|
||||
bool MinimizeToTray { get; set; }
|
||||
int ThumbnailRefreshPeriod { get; set; }
|
||||
|
@@ -5,11 +5,13 @@ using Newtonsoft.Json;
|
||||
|
||||
namespace EveOPreview.Configuration
|
||||
{
|
||||
public class AppConfig : IAppConfig
|
||||
public class ThumbnailConfig : IThumbnailConfig
|
||||
{
|
||||
public AppConfig()
|
||||
public ThumbnailConfig()
|
||||
{
|
||||
// Default values
|
||||
this.ConfigFileName = null;
|
||||
|
||||
this.MinimizeToTray = false;
|
||||
this.ThumbnailRefreshPeriod = 500;
|
||||
|
||||
@@ -41,6 +43,9 @@ namespace EveOPreview.Configuration
|
||||
this.ClientHotkey = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public string ConfigFileName { get; set; }
|
||||
|
||||
public bool MinimizeToTray { get; set; }
|
||||
public int ThumbnailRefreshPeriod { get; set; }
|
||||
|
||||
@@ -163,11 +168,11 @@ namespace EveOPreview.Configuration
|
||||
/// </summary>
|
||||
public void ApplyRestrictions()
|
||||
{
|
||||
this.ThumbnailRefreshPeriod = AppConfig.ApplyRestrictions(this.ThumbnailRefreshPeriod, 300, 1000);
|
||||
this.ThumbnailSize = new Size(AppConfig.ApplyRestrictions(this.ThumbnailSize.Width, this.ThumbnailMinimumSize.Width, this.ThumbnailMaximumSize.Width),
|
||||
AppConfig.ApplyRestrictions(this.ThumbnailSize.Height, this.ThumbnailMinimumSize.Height, this.ThumbnailMaximumSize.Height));
|
||||
this.ThumbnailOpacity = AppConfig.ApplyRestrictions((int)(this.ThumbnailOpacity * 100.00), 20, 100) / 100.00;
|
||||
this.ThumbnailZoomFactor = AppConfig.ApplyRestrictions(this.ThumbnailZoomFactor, 2, 10);
|
||||
this.ThumbnailRefreshPeriod = ThumbnailConfig.ApplyRestrictions(this.ThumbnailRefreshPeriod, 300, 1000);
|
||||
this.ThumbnailSize = new Size(ThumbnailConfig.ApplyRestrictions(this.ThumbnailSize.Width, this.ThumbnailMinimumSize.Width, this.ThumbnailMaximumSize.Width),
|
||||
ThumbnailConfig.ApplyRestrictions(this.ThumbnailSize.Height, this.ThumbnailMinimumSize.Height, this.ThumbnailMaximumSize.Height));
|
||||
this.ThumbnailOpacity = ThumbnailConfig.ApplyRestrictions((int)(this.ThumbnailOpacity * 100.00), 20, 100) / 100.00;
|
||||
this.ThumbnailZoomFactor = ThumbnailConfig.ApplyRestrictions(this.ThumbnailZoomFactor, 2, 10);
|
||||
}
|
||||
|
||||
private static int ApplyRestrictions(int value, int minimum, int maximum)
|
@@ -97,7 +97,7 @@
|
||||
<Compile Include="ApplicationBase\Presenter.cs" />
|
||||
<Compile Include="ApplicationBase\PresenterGeneric.cs" />
|
||||
<Compile Include="Configuration\ConfigurationStorage.cs" />
|
||||
<Compile Include="Configuration\IAppConfig.cs" />
|
||||
<Compile Include="Configuration\IThumbnailConfig.cs" />
|
||||
<Compile Include="Configuration\ZoomAnchor.cs" />
|
||||
<Compile Include="DwmAPI\DWM_BLURBEHIND.cs" />
|
||||
<Compile Include="DwmAPI\DWM_THUMBNAIL_PROPERTIES.cs" />
|
||||
@@ -118,7 +118,7 @@
|
||||
<Compile Include="UI\Interface\IThumbnailDescriptionView.cs" />
|
||||
<Compile Include="UI\Interface\IThumbnailDescriptionViewFactory.cs" />
|
||||
<Compile Include="UI\Interface\ViewZoomAnchor.cs" />
|
||||
<Compile Include="Configuration\AppConfig.cs" />
|
||||
<Compile Include="Configuration\ThumbnailConfig.cs" />
|
||||
<Compile Include="Hotkeys\HotkeyHandler.cs" />
|
||||
<Compile Include="Hotkeys\HotkeyHandlerNativeMethods.cs" />
|
||||
<Compile Include="UI\Implementation\MainForm.cs">
|
||||
|
@@ -13,7 +13,7 @@ namespace EveOPreview.UI
|
||||
#endregion
|
||||
|
||||
#region Private fields
|
||||
private readonly IAppConfig _configuration;
|
||||
private readonly IThumbnailConfig _configuration;
|
||||
private readonly IConfigurationStorage _configurationStorage;
|
||||
private readonly IThumbnailDescriptionViewFactory _thumbnailDescriptionViewFactory;
|
||||
private readonly IDictionary<IntPtr, IThumbnailDescriptionView> _thumbnailDescriptionViews;
|
||||
@@ -22,7 +22,7 @@ namespace EveOPreview.UI
|
||||
private bool _exitApplication;
|
||||
#endregion
|
||||
|
||||
public MainPresenter(IApplicationController controller, IMainView view, IAppConfig configuration, IConfigurationStorage configurationStorage,
|
||||
public MainPresenter(IApplicationController controller, IMainView view, IThumbnailConfig configuration, IConfigurationStorage configurationStorage,
|
||||
IThumbnailManager thumbnailManager, IThumbnailDescriptionViewFactory thumbnailDescriptionViewFactory)
|
||||
: base(controller, view)
|
||||
{
|
||||
|
@@ -15,7 +15,7 @@ namespace EveOPreview.UI
|
||||
#endregion
|
||||
|
||||
#region Private fields
|
||||
private readonly IAppConfig _configuration;
|
||||
private readonly IThumbnailConfig _configuration;
|
||||
private readonly DispatcherTimer _thumbnailUpdateTimer;
|
||||
private readonly IThumbnailViewFactory _thumbnailViewFactory;
|
||||
private readonly Dictionary<IntPtr, IThumbnailView> _thumbnailViews;
|
||||
@@ -27,7 +27,7 @@ namespace EveOPreview.UI
|
||||
private bool _isHoverEffectActive;
|
||||
#endregion
|
||||
|
||||
public ThumbnailManager(IAppConfig configuration, IThumbnailViewFactory factory)
|
||||
public ThumbnailManager(IThumbnailConfig configuration, IThumbnailViewFactory factory)
|
||||
{
|
||||
this._configuration = configuration;
|
||||
this._thumbnailViewFactory = factory;
|
||||
|
@@ -7,9 +7,11 @@ namespace EveOPreview
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
private static string ConfigParameterName = "--config:";
|
||||
|
||||
/// <summary>The main entry point for the application.</summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
@@ -29,9 +31,41 @@ namespace EveOPreview
|
||||
.RegisterService<IThumbnailViewFactory, ThumbnailViewFactory>()
|
||||
.RegisterService<IThumbnailDescriptionViewFactory, ThumbnailDescriptionViewFactory>()
|
||||
.RegisterService<IConfigurationStorage, ConfigurationStorage>()
|
||||
.RegisterInstance<IAppConfig>(new AppConfig());
|
||||
.RegisterInstance<IThumbnailConfig>(new ThumbnailConfig());
|
||||
|
||||
controller.Create<IThumbnailConfig>().ConfigFileName = Program.GetCustomConfigFile(args);
|
||||
|
||||
controller.Run<MainPresenter>();
|
||||
}
|
||||
|
||||
// Parse startup parameters
|
||||
// Simple approach is used because something like NParams would be an overkill here
|
||||
private static string GetCustomConfigFile(string[] args)
|
||||
{
|
||||
string configFile = null;
|
||||
foreach (string arg in args)
|
||||
{
|
||||
if ((arg.Length <= Program.ConfigParameterName.Length) || !arg.StartsWith(Program.ConfigParameterName, StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
configFile = arg.Substring(Program.ConfigParameterName.Length);
|
||||
break;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(configFile))
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
// One more check to drop trailing "
|
||||
if ((configFile.Length > 3) && (configFile[0] == '"') && (configFile[configFile.Length - 1] == '"'))
|
||||
{
|
||||
configFile = configFile.Substring(1, configFile.Length - 2);
|
||||
}
|
||||
|
||||
return configFile;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user