FrmProxyCmd: Changes for multiple configuration selection.

This commit is contained in:
2018-02-25 10:15:44 +01:00
parent 77e1c35e52
commit e6c66a642b
8 changed files with 179 additions and 90 deletions

View File

@@ -17,7 +17,7 @@ namespace VAR.Toolbox.Code
string path = Path.GetDirectoryName(location);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(location);
string fileOut = String.Format("{0}/{1}.{2}.txt", path, filenameWithoutExtension,
string fileOut = string.Format("{0}/{1}.{2}.txt", path, filenameWithoutExtension,
DateTime.UtcNow.ToString("yyyy-MM"));
return File.AppendText(fileOut);
}

View File

@@ -1,14 +1,21 @@
namespace VAR.Toolbox.Code
using System;
namespace VAR.Toolbox.Code
{
public class ProxyCmdExecutorFactory
{
public static IProxyCmdExecutor CreateFromConfig(string config)
{
if (string.IsNullOrEmpty(config))
if (string.IsNullOrEmpty(config) || config.StartsWith("Dummy:"))
{
return new ProxyCmdExecutorDummy();
}
return new ProxyCmdExecutorThroughSQLServer(config);
if (config.StartsWith("SqlServer:"))
{
string configSqlServer = config.Substring("SqlServer:".Length);
return new ProxyCmdExecutorThroughSQLServer(configSqlServer);
}
throw new NotImplementedException(string.Format("Cant create IProxyCmdExecutor with this config: {0}", config));
}
}
}

View File

@@ -1,38 +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 VAR.Toolbox.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.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;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=SRV7;Initial Catalog=master;Integrated Security=True")]
public string ProxyCmdConfig {
get {
return ((string)(this["ProxyCmdConfig"]));
}
set {
this["ProxyCmdConfig"] = value;
}
}
}
}

View File

@@ -1,9 +0,0 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="VAR.Toolbox.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="ProxyCmdConfig" Type="System.String" Scope="User">
<Value Profile="(Default)">Data Source=SRV7;Initial Catalog=master;Integrated Security=True</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@@ -31,6 +31,8 @@
this.splitMain = new System.Windows.Forms.SplitContainer();
this.txtOutput = new System.Windows.Forms.TextBox();
this.txtInput = new System.Windows.Forms.TextBox();
this.ddlCurrentConfig = new System.Windows.Forms.ComboBox();
this.btnConfig = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.splitMain)).BeginInit();
this.splitMain.Panel1.SuspendLayout();
this.splitMain.Panel2.SuspendLayout();
@@ -39,9 +41,11 @@
//
// splitMain
//
this.splitMain.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.splitMain.BackColor = System.Drawing.SystemColors.ControlDark;
this.splitMain.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitMain.Location = new System.Drawing.Point(0, 0);
this.splitMain.Location = new System.Drawing.Point(0, 28);
this.splitMain.Name = "splitMain";
this.splitMain.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
@@ -52,8 +56,8 @@
// splitMain.Panel2
//
this.splitMain.Panel2.Controls.Add(this.txtInput);
this.splitMain.Size = new System.Drawing.Size(392, 369);
this.splitMain.SplitterDistance = 313;
this.splitMain.Size = new System.Drawing.Size(413, 418);
this.splitMain.SplitterDistance = 353;
this.splitMain.SplitterWidth = 10;
this.splitMain.TabIndex = 3;
this.splitMain.TabStop = false;
@@ -69,7 +73,7 @@
this.txtOutput.Name = "txtOutput";
this.txtOutput.ReadOnly = true;
this.txtOutput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtOutput.Size = new System.Drawing.Size(392, 313);
this.txtOutput.Size = new System.Drawing.Size(413, 353);
this.txtOutput.TabIndex = 1;
this.txtOutput.TabStop = false;
//
@@ -83,15 +87,40 @@
this.txtInput.Multiline = true;
this.txtInput.Name = "txtInput";
this.txtInput.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtInput.Size = new System.Drawing.Size(392, 46);
this.txtInput.Size = new System.Drawing.Size(413, 55);
this.txtInput.TabIndex = 0;
this.txtInput.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyDown);
//
// ddlCurrentConfig
//
this.ddlCurrentConfig.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ddlCurrentConfig.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ddlCurrentConfig.FormattingEnabled = true;
this.ddlCurrentConfig.Location = new System.Drawing.Point(0, 1);
this.ddlCurrentConfig.Name = "ddlCurrentConfig";
this.ddlCurrentConfig.Size = new System.Drawing.Size(342, 21);
this.ddlCurrentConfig.TabIndex = 4;
this.ddlCurrentConfig.SelectedIndexChanged += new System.EventHandler(this.ddlCurrentConfig_SelectedIndexChanged);
//
// btnConfig
//
this.btnConfig.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnConfig.Location = new System.Drawing.Point(348, 1);
this.btnConfig.Name = "btnConfig";
this.btnConfig.Size = new System.Drawing.Size(65, 23);
this.btnConfig.TabIndex = 5;
this.btnConfig.Text = "Config";
this.btnConfig.UseVisualStyleBackColor = true;
this.btnConfig.Click += new System.EventHandler(this.btnConfig_Click);
//
// FrmProxyCmd
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(392, 369);
this.ClientSize = new System.Drawing.Size(413, 446);
this.Controls.Add(this.btnConfig);
this.Controls.Add(this.ddlCurrentConfig);
this.Controls.Add(this.splitMain);
this.Name = "FrmProxyCmd";
this.Text = "ProxyCmd";
@@ -110,5 +139,7 @@
private System.Windows.Forms.SplitContainer splitMain;
private System.Windows.Forms.TextBox txtOutput;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.ComboBox ddlCurrentConfig;
private System.Windows.Forms.Button btnConfig;
}
}

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using VAR.Toolbox.Code;
@@ -7,16 +9,27 @@ namespace VAR.Toolbox.UI
{
public partial class FrmProxyCmd : Form, IOutputHandler
{
public FrmProxyCmd()
{
InitializeComponent();
}
#region Declarations
private object _executionLock = new object();
private List<string> _cmdHistory = new List<string>();
private int _currentHistoryIndex = -1;
#endregion Declarations
#region Life cycle
public FrmProxyCmd()
{
InitializeComponent();
LoadConfig();
}
#endregion Life cycle
#region UI events
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if (Monitor.IsEntered(_executionLock))
@@ -35,6 +48,7 @@ namespace VAR.Toolbox.UI
Application.DoEvents();
txtInput.Text = string.Empty;
OutputLine(cmd);
PrepareProxyCmdExecutor();
new Thread(() => ExecuteCmd(cmd)).Start();
}
return;
@@ -88,14 +102,45 @@ namespace VAR.Toolbox.UI
}
}
private void ddlCurrentConfig_SelectedIndexChanged(object sender, EventArgs e)
{
CleanProxyCmdExecutor();
}
private void btnConfig_Click(object sender, EventArgs e)
{
throw new NotImplementedException("Implement btnConfig_Click");
}
#endregion UI events
#region ProxyCmdExecutor
private IProxyCmdExecutor _proxyCmdExecutor = null;
private void ExecuteCmd(string cmdString)
private void PrepareProxyCmdExecutor()
{
if (_proxyCmdExecutor == null)
{
_proxyCmdExecutor = ProxyCmdExecutorFactory.CreateFromConfig(Properties.Settings.Default.ProxyCmdConfig);
_proxyCmdExecutor = ProxyCmdExecutorFactory.CreateFromConfig(GetCurrentConfig());
}
}
private void CleanProxyCmdExecutor()
{
IDisposable disposableProxyCmdExecutor = _proxyCmdExecutor as IDisposable;
if (disposableProxyCmdExecutor !=null)
{
disposableProxyCmdExecutor.Dispose();
}
_proxyCmdExecutor = null;
}
#endregion ProxyCmdExecutor
#region Private methods
private void ExecuteCmd(string cmdString)
{
Monitor.Enter(_executionLock);
try
{
@@ -120,5 +165,83 @@ namespace VAR.Toolbox.UI
Application.DoEvents();
}));
}
#endregion Private methods
#region Config
public void LoadConfig()
{
CleanProxyCmdExecutor();
string configFile = GetConfigFileName();
string[] config = null;
if (File.Exists(configFile) == false)
{
config = new string[] { "Dummy|Dummy:" };
}
else
{
config = File.ReadAllLines(configFile);
}
SetLoadedConfig(config);
}
private void SetLoadedConfig(string[] configLines)
{
string previousSelectedName = null;
ProxyCmdConfigItem selectedConfig = ddlCurrentConfig.SelectedItem as ProxyCmdConfigItem;
if (selectedConfig != null) { previousSelectedName = selectedConfig.Name; }
ddlCurrentConfig.Items.Clear();
foreach (string configLine in configLines)
{
int idxSplit = configLine.IndexOf('|');
if(idxSplit < 0) { continue; }
string configName = configLine.Substring(0, idxSplit);
string configData = configLine.Substring(idxSplit + 1);
ddlCurrentConfig.Items.Add(new ProxyCmdConfigItem { Name = configName, Config = configData, });
}
if (string.IsNullOrEmpty(previousSelectedName))
{
ddlCurrentConfig.SelectedIndex = 0;
return;
}
foreach(ProxyCmdConfigItem configItem in ddlCurrentConfig.Items)
{
if(configItem.Name == previousSelectedName)
{
ddlCurrentConfig.SelectedItem = configItem;
break;
}
}
}
private string GetConfigFileName()
{
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string path = Path.GetDirectoryName(location);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(location);
string configFile = string.Format("{0}/{1}.ProxyCmd.cfg", path, filenameWithoutExtension);
return configFile;
}
private string GetCurrentConfig()
{
ProxyCmdConfigItem selectedConfig = ddlCurrentConfig.SelectedItem as ProxyCmdConfigItem;
if (selectedConfig == null) { return null; }
return selectedConfig.Config;
}
#endregion Config
}
public class ProxyCmdConfigItem
{
public string Name { get; set; }
public string Config { get; set; }
public override string ToString() { return Name; }
}
}

View File

@@ -157,11 +157,6 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="UI\FrmWebcam.cs">
<SubType>Form</SubType>
</Compile>
@@ -183,12 +178,7 @@
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="Images\Toolbox.png" />

View File

@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="VAR.Toolbox.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<VAR.Toolbox.Properties.Settings>
<setting name="ProxyCmdConfig" serializeAs="String">
<value>Data Source=SRV7;Initial Catalog=master;Integrated Security=True</value>
</setting>
</VAR.Toolbox.Properties.Settings>
</userSettings>
</configuration>