bookmark-manager-r4/AddBatchPageForm.cs

312 lines
11 KiB
C#
Raw Normal View History

2024-09-15 14:50:53 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookmarkManager.Services;
using bzit.bomg.Models;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
namespace FizzyLauncher
{
public class AddBatchPageForm : Form
{
private System.Windows.Forms.Label label1;
private readonly WebProvider _webProvider;
private string _rootPath;
private bool _isBusy = false;
private THorizontalSeparator tHorizontalSeparator3;
private ThButton button3;
private ThProgressBar progressBar2;
private RyzStudio.Windows.ThemedForms.Composite.DialogFooter dialogFooter1;
private ThToolbarMemoBox memoBox1;
private List<BookmarkModel> _result = new List<BookmarkModel>();
private bool _requestCancel = false;
public AddBatchPageForm(string rootPath)
{
InitializeComponent();
UISetup.Dialog(this);
_result = new List<BookmarkModel>();
_webProvider = new WebProvider();
_webProvider.IgnoreSSL = true;
_rootPath = rootPath;
}
private void InitializeComponent()
{
label1 = new Label();
tHorizontalSeparator3 = new THorizontalSeparator();
button3 = new ThButton();
progressBar2 = new ThProgressBar();
dialogFooter1 = new RyzStudio.Windows.ThemedForms.Composite.DialogFooter();
memoBox1 = new ThToolbarMemoBox();
SuspendLayout();
//
// label1
//
label1.AutoSize = true;
label1.BackColor = System.Drawing.Color.Transparent;
label1.ForeColor = System.Drawing.SystemColors.ControlText;
label1.Location = new System.Drawing.Point(10, 20);
label1.Margin = new Padding(0);
label1.Name = "label1";
label1.Padding = new Padding(0, 8, 0, 0);
label1.Size = new System.Drawing.Size(185, 23);
label1.TabIndex = 153;
label1.Text = "Add pages (one URL on each line)";
label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// tHorizontalSeparator3
//
tHorizontalSeparator3.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
tHorizontalSeparator3.AutoScrollMargin = new System.Drawing.Size(0, 0);
tHorizontalSeparator3.AutoScrollMinSize = new System.Drawing.Size(0, 0);
tHorizontalSeparator3.BackColor = System.Drawing.Color.Transparent;
tHorizontalSeparator3.Location = new System.Drawing.Point(10, 321);
tHorizontalSeparator3.Margin = new Padding(0, 10, 0, 0);
tHorizontalSeparator3.MaximumSize = new System.Drawing.Size(4920, 2);
tHorizontalSeparator3.MinimumSize = new System.Drawing.Size(0, 22);
tHorizontalSeparator3.Name = "tHorizontalSeparator3";
tHorizontalSeparator3.Size = new System.Drawing.Size(424, 22);
tHorizontalSeparator3.TabIndex = 207;
tHorizontalSeparator3.TabStop = false;
//
// button3
//
button3.ActiveImage = null;
button3.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
button3.BackColor = System.Drawing.Color.Transparent;
button3.EnableMenuOnClick = false;
button3.EnableReactiveVisual = true;
button3.HoverImage = null;
button3.IdleImage = null;
button3.LabelText = "&Add Pages";
button3.Location = new System.Drawing.Point(306, 393);
button3.Name = "button3";
button3.Size = new System.Drawing.Size(128, 32);
button3.TabIndex = 209;
button3.TabStop = false;
button3.MouseClick += button3_MouseClick;
//
// progressBar2
//
progressBar2.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
progressBar2.BackColor = System.Drawing.Color.Transparent;
progressBar2.BarColour = System.Drawing.Color.FromArgb(79, 193, 203);
progressBar2.BarTextColour = System.Drawing.Color.Black;
progressBar2.EnableReactiveVisual = false;
progressBar2.Font = new System.Drawing.Font("Segoe UI", 9F);
progressBar2.Location = new System.Drawing.Point(10, 353);
progressBar2.Maximum = 100;
progressBar2.Minimum = 0;
progressBar2.Name = "progressBar2";
progressBar2.ProgressText = "50/100";
progressBar2.ShowProgressText = true;
progressBar2.Size = new System.Drawing.Size(424, 20);
progressBar2.TabIndex = 210;
progressBar2.TabStop = false;
progressBar2.Value = 50;
//
// dialogFooter1
//
dialogFooter1.BackColor = System.Drawing.Color.FromArgb(240, 240, 240);
dialogFooter1.Button1Text = "&Close";
dialogFooter1.Dialog = this;
dialogFooter1.Dock = DockStyle.Bottom;
dialogFooter1.IsBusy = false;
dialogFooter1.Location = new System.Drawing.Point(0, 437);
dialogFooter1.Name = "dialogFooter1";
dialogFooter1.Size = new System.Drawing.Size(444, 84);
//
// memoBox1
//
memoBox1.AllowDrop = true;
memoBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
memoBox1.BackColor = System.Drawing.Color.Transparent;
memoBox1.ClearedValue = "";
memoBox1.EnableReactiveVisual = true;
memoBox1.Font = new System.Drawing.Font("Segoe UI", 9F);
memoBox1.Location = new System.Drawing.Point(10, 53);
memoBox1.Name = "memoBox1";
memoBox1.ReadOnly = false;
memoBox1.ScrollBars = ScrollBars.Vertical;
memoBox1.Size = new System.Drawing.Size(424, 258);
memoBox1.TabIndex = 211;
memoBox1.TabStop = false;
memoBox1.WordWrap = false;
//
// AddBatchPageForm
//
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = System.Drawing.Color.White;
ClientSize = new System.Drawing.Size(444, 521);
Controls.Add(memoBox1);
Controls.Add(dialogFooter1);
Controls.Add(progressBar2);
Controls.Add(button3);
Controls.Add(tHorizontalSeparator3);
Controls.Add(label1);
KeyPreview = true;
MinimumSize = new System.Drawing.Size(460, 560);
Name = "AddBatchPageForm";
Text = "Add Pages (Batch)";
ResumeLayout(false);
PerformLayout();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
memoBox1.Text = string.Empty;
progressBar2.Minimum = 0;
progressBar2.Value = 0;
progressBar2.Maximum = memoBox1.Lines?.Length ?? 0;
memoBox1.Focus();
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (this.IsBusy)
{
e.Cancel = true;
}
}
public List<BookmarkModel> Result
{
get => _result;
private set => _result = value;
}
protected bool IsBusy
{
get => _isBusy;
set
{
_isBusy = value;
UIControl.SetEnable(memoBox1, !this.IsBusy);
UIControl.Invoke(button3, (x) =>
{
button3.LabelText = (this.IsBusy ? "&Stop" : "&Add Pages");
});
dialogFooter1.IsBusy = _isBusy;
}
}
private async void button3_MouseClick(object sender, MouseEventArgs e)
{
await Task.Run(async () =>
{
if (this.IsBusy)
{
_requestCancel = true;
return;
}
this.IsBusy = true;
this.Result = new List<BookmarkModel>();
progressBar2.Minimum = 0;
progressBar2.Value = 0;
progressBar2.Maximum = memoBox1.Lines?.Length ?? 0;
foreach (var item in memoBox1.Lines ?? new string[0])
{
progressBar2.Value++;
if (string.IsNullOrWhiteSpace(item))
{
continue;
}
var newModel = new BookmarkModel();
newModel.Address = item;
newModel.Path = _rootPath;
if (_requestCancel)
{
break;
}
newModel = await UpdateBookmark(newModel, true, true);
if (newModel == null)
{
continue;
}
if (_requestCancel)
{
break;
}
this.Result.Add(newModel);
}
//_requestCancel = false;
this.IsBusy = false;
if (!_requestCancel)
{
this.DialogResult = DialogResult.OK;
UIControl.Close(this);
}
_requestCancel = false;
});
}
private async Task<BookmarkModel> UpdateBookmark(BookmarkModel model, bool updateTitle, bool updateDescription)
{
if (string.IsNullOrWhiteSpace(model.Address))
{
return null;
}
if (!model.Address.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase) && !model.Address.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase))
{
model.Address = "http://" + model.Address;
}
var document = await _webProvider.RetrieveHtmlDocument(model.Address);
if (document == null)
{
return null;
}
if (updateTitle)
{
model.Title = _webProvider.ParseTitle(document);
}
if (updateDescription)
{
model.Description = _webProvider.ParseDescription(document);
}
return model;
}
}
}