Skip to content

Commit

Permalink
Merge pull request #15 from gisellevonbingen/Hook
Browse files Browse the repository at this point in the history
Add Event Hook
  • Loading branch information
gisellevonbingen committed Dec 26, 2022
2 parents 2cf82c1 + c037028 commit d39a413
Show file tree
Hide file tree
Showing 18 changed files with 972 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Forms/Configs/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Configuration : IJsonObject
public AgentSettings Agent { get; } = new AgentSettings();
public NetworkSettings Network { get; } = new NetworkSettings();
public ContentSettings Content { get; } = new ContentSettings();
public HookSettings Hook { get; } = new HookSettings();

public Configuration()
{
Expand Down Expand Up @@ -44,6 +45,7 @@ public void Read(JToken json)
this.Agent.Read(json.Value<JObject>("Agent") ?? new JObject());
this.Network.Read(json.Value<JObject>("Network") ?? new JObject());
this.Content.Read(json.Value<JObject>("Content") ?? new JObject());
this.Hook.Read(json.Value<JObject>("Hook") ?? new JObject());
}

}
Expand All @@ -56,6 +58,7 @@ public void Write(JToken json)
this.Agent.Write(json["Agent"] = new JObject());
this.Network.Write(json["Network"] = new JObject());
this.Content.Write(json["Content"] = new JObject());
this.Hook.Write(json["Hook"] = new JObject());
}

}
Expand Down
85 changes: 85 additions & 0 deletions Forms/Configs/HookSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Giselle.Commons.Collections;
using Giselle.DoujinshiDownloader.Hooks;
using Giselle.Json;
using Newtonsoft.Json.Linq;

namespace Giselle.DoujinshiDownloader.Configs
{
public class HookSettings : IJsonObject
{
public bool WaitForExit { get; set; } = false;
public HookCommandlineSettings Commandlines { get; } = new HookCommandlineSettings();

public HookSettings()
{

}

public void Read(JToken json)
{
this.WaitForExit = json.Value<bool>("WaitForExit");
this.Commandlines.Read(json.Value<JObject>("Commandlines") ?? new JObject());
}

public void Write(JToken json)
{
json["WaitForExit"] = this.WaitForExit;
json["Commandlines"] = this.Commandlines.Write();
}

public class HookCommandlineSettings : IJsonObject
{
private readonly Dictionary<string, string> Map = new Dictionary<string, string>();

public string this[Hook hook]
{
get
{
return this.Map.GetSafe(hook.Name, string.Empty);
}

set
{
if (string.IsNullOrEmpty(value) == true)
{
this.Map.Remove(hook.Name);
}
else
{
this.Map[hook.Name] = value;
}

}

}

public void Read(JToken json)
{
this.Map.Clear();

foreach (var hook in Hook.Registry.Values)
{
this[hook] = json.Value<string>(hook.Name);
}

}

public void Write(JToken json)
{
foreach (var hook in Hook.Registry.Values)
{
json[hook.Name] = this[hook];
}

}

}

}

}
3 changes: 3 additions & 0 deletions Forms/DoujinshiDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Giselle.Commons;
using Giselle.DoujinshiDownloader.Configs;
using Giselle.DoujinshiDownloader.Forms;
using Giselle.DoujinshiDownloader.Hooks;
using Giselle.DoujinshiDownloader.Schedulers;
using Giselle.DoujinshiDownloader.Utils;
using Giselle.Forms;
Expand Down Expand Up @@ -73,6 +74,7 @@ public static void Main(string[] args)
public ConfigurationManager Config { get; }
public NotifyIconManager NotifyIconManager { get; }
public DownloadScheduler DownloadScheduler { get; }
public HookManager HookManager { get; }

public MainForm MainForm { get; private set; }
public event EventHandler MainFormVisibleChanged;
Expand All @@ -93,6 +95,7 @@ public DoujinshiDownloader(CommandLineOptions options)
this.Config = new ConfigurationManager(PathUtils.GetPath("Configuration.json"));
this.NotifyIconManager = new NotifyIconManager();
this.DownloadScheduler = new DownloadScheduler();
this.HookManager = new HookManager();

this.MainForm = null;
}
Expand Down
12 changes: 12 additions & 0 deletions Forms/Forms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Configs\ConfigurationManager.cs" />
<Compile Include="Configs\AgentSettings.cs" />
<Compile Include="Configs\ContentSettings.cs" />
<Compile Include="Configs\HookSettings.cs" />
<Compile Include="Configs\ImageConvertType.cs" />
<Compile Include="Configs\NetworkSettings.cs" />
<Compile Include="Configs\NotifyMessageRules.cs" />
Expand Down Expand Up @@ -126,7 +127,13 @@
<Compile Include="Forms\DownloadDetailListItem.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Forms\HookSettingsControls.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Forms\ImageConvertTypeItem.cs" />
<Compile Include="Forms\MacroSelectForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\ProgramSettingsControl.cs">
<SubType>Component</SubType>
</Compile>
Expand All @@ -136,6 +143,11 @@
<Compile Include="Forms\SettingControl.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Hooks\Hook.cs" />
<Compile Include="Hooks\HookCategory.cs" />
<Compile Include="Hooks\HookExecuteException.cs" />
<Compile Include="Hooks\HookManager.cs" />
<Compile Include="Hooks\Macro.cs" />
<Compile Include="NativeMethods.cs" />
<Compile Include="NotifyIconManager.cs" />
<Compile Include="Properties\Resources.Designer.cs">
Expand Down
206 changes: 206 additions & 0 deletions Forms/Forms/HookSettingsControls.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Giselle.Commons.Users;
using Giselle.DoujinshiDownloader.Configs;
using Giselle.DoujinshiDownloader.Hooks;
using Giselle.Drawing.Drawing;
using Giselle.Forms;
using Newtonsoft.Json.Linq;
using static Giselle.DoujinshiDownloader.Configs.HookSettings;

namespace Giselle.DoujinshiDownloader.Forms
{
public class HookSettingsControls : SettingControl
{
private readonly HookCommandlineSettings EditCommandlines;

private readonly CheckBox WaitForExitCheckBox;
private readonly Label ListLabel;
private readonly ListBox ListBox;
private readonly Label CommandlineLabel;
private readonly TextBox CommandlineTextBox;
private readonly OptimizedButton MacroButton;

private bool CommandlineChanging;

public HookSettingsControls()
{
this.SuspendLayout();

this.Text = SR.Get("Settings.Hook.Title");
this.EditCommandlines = new HookCommandlineSettings();

var waitForExitCheckBox = this.WaitForExitCheckBox = new CheckBox();
waitForExitCheckBox.Text = SR.Get("Settings.Hook.WaitForExit");
this.Controls.Add(waitForExitCheckBox);

var listLabel = this.ListLabel = new Label();
listLabel.Text = SR.Get("Settings.Hook.List");
listLabel.TextAlign = ContentAlignment.BottomLeft;
this.Controls.Add(listLabel);

var listBox = this.ListBox = new ListBox();
listBox.SelectedIndexChanged += this.OnListBoxSelectedIndexChanged;
this.Controls.Add(listBox);

var commandlineLabel = this.CommandlineLabel = new Label();
commandlineLabel.Text = SR.Get("Settings.Hook.Commandline");
commandlineLabel.TextAlign = ContentAlignment.BottomLeft;
this.Controls.Add(commandlineLabel);

var commandlineTextBox = this.CommandlineTextBox = new TextBox();
commandlineTextBox.WordWrap = true;
commandlineTextBox.Multiline = true;
commandlineTextBox.TextChanged += this.OnCommandlineTextBoxTextChanged;
this.Controls.Add(commandlineTextBox);

var macroButton = this.MacroButton = new OptimizedButton();
macroButton.Text = SR.Get("Settings.Hook.Macro");
macroButton.Click += this.OnMacroButtonClick;
this.Controls.Add(macroButton);

this.UpdateListBoxItems();

this.ResumeLayout(false);
}

private void OnMacroButtonClick(object sender, EventArgs e)
{
if (this.TryGetSelectedHook(out var hook) == true)
{
using (var form = new MacroSelectForm(hook.Category))
{
if (form.ShowDialog(this) == DialogResult.OK)
{
var placeHolder = form.SelectedMacro.ToPlaceHolder();
this.CommandlineTextBox.SelectedText = placeHolder;
this.CommandlineTextBox.Focus();
}

}

}

}

private void OnCommandlineTextBoxTextChanged(object sender, EventArgs e)
{
if (this.CommandlineChanging == false && this.TryGetSelectedHook(out var hook) == true)
{
this.EditCommandlines[hook] = this.CommandlineTextBox.Text;
}

}

protected override Dictionary<Control, Rectangle> GetPreferredBounds(Rectangle layoutbounds)
{
var map = base.GetPreferredBounds(layoutbounds);
map[this.WaitForExitCheckBox] = layoutbounds.InTopBounds(29);
map[this.ListLabel] = map[this.WaitForExitCheckBox].OutBottomBounds(29).InLeftBounds(210);
map[this.ListBox] = map[this.ListLabel].OutBottomBounds(map[this.ResetButton].Bottom - map[this.ListLabel].Bottom);
map[this.CommandlineLabel] = layoutbounds.InRightBounds(layoutbounds.Right - map[this.ListLabel].Right - 10).DeriveTop(map[this.ListLabel].Top).DeriveHeight(map[this.ListLabel].Height);
map[this.CommandlineTextBox] = map[this.CommandlineLabel].OutBottomBounds(map[this.ResetButton].Top - 10 - map[this.CommandlineLabel].Bottom);
map[this.MacroButton] = map[this.CommandlineTextBox].InLeftBounds(map[this.ResetButton].Width).DeriveTop(map[this.ResetButton].Top).DeriveHeight(map[this.ResetButton].Height);

return map;
}

private void UpdateListBoxItems()
{
var listBox = this.ListBox;
listBox.SuspendLayout();

var items = listBox.Items;
items.Clear();

foreach (var hook in Hook.Registry.Values)
{
items.Add(new ComboBoxItemWrapper<Hook>(hook, SR.Get($"Hook.{hook.Name}.Name")));
}

listBox.ResumeLayout(false);
}

private bool TryGetSelectedHook(out Hook hook)
{
if (this.ListBox.SelectedItem is ComboBoxItemWrapper<Hook> item)
{
hook = item.Value;
return true;
}
else
{
hook = null;
return false;
}

}

private void OnListBoxSelectedIndexChanged(object sender, EventArgs e)
{
if (this.TryGetSelectedHook(out var hook) == true)
{
this.OnSelect(hook);
}

}

private void Select(Hook hook)
{
var item = this.ListBox.Items.OfType<ComboBoxItemWrapper<Hook>>().Where(i => i.Value == hook).FirstOrDefault();
this.ListBox.SelectedItem = item;
this.OnSelect(hook);
}

private void OnSelect(Hook hook)
{
var prev = this.CommandlineChanging;

try
{
this.CommandlineChanging = true;
this.CommandlineTextBox.Text = this.EditCommandlines[hook];
this.CommandlineTextBox.Focus();
this.CommandlineTextBox.SelectionStart = this.CommandlineTextBox.Text.Length;
}
finally
{
this.CommandlineChanging = prev;
}

}

public override void Apply(Configuration config)
{
var hookConfig = config.Hook;
hookConfig.WaitForExit = this.WaitForExitCheckBox.Checked;

var commandlines = new JObject();
this.EditCommandlines.Write(commandlines);
hookConfig.Commandlines.Read(commandlines);
}

public override void Bind(Configuration config)
{
var hookConfig = config.Hook;
this.WaitForExitCheckBox.Checked = hookConfig.WaitForExit;

var commandlines = new JObject();
hookConfig.Commandlines.Write(commandlines);
this.EditCommandlines.Read(commandlines);
this.Select(Hook.Registry.Values.FirstOrDefault());
}

public override (string name, Control control) Validate()
{
return (null, null);
}

}

}
Loading

0 comments on commit d39a413

Please sign in to comment.