bookmark-manager-r4/UpdateIconsForm.cs
2024-07-20 18:09:44 +01:00

438 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookmarkManager.Services;
using bzit.bomg.Models;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
using RyzStudio.Windows.ThemedForms.PickerBox;
namespace FizzyLauncher
{
public class UpdateIconsForm : Form
{
private System.Windows.Forms.Label label1;
private RyzStudio.Windows.Forms.THorizontalSeparator tHorizontalSeparator2;
private Label label2;
private PictureBox pictureBox2;
private ThYesNoPickerBox pickerBox1;
private ThButton button2;
private RyzStudio.Windows.ThemedForms.ThProgressBar progressBar1;
private readonly WebProvider _webProvider;
private BookmarkTreeView _treeView;
private bool _isBusy = false;
private Label label4;
private THorizontalSeparator tHorizontalSeparator3;
private ThButton button3;
private ThProgressBar progressBar2;
private RyzStudio.Windows.ThemedForms.Composite.DialogFooter dialogFooter1;
private bool _requestCancel = false;
public UpdateIconsForm(BookmarkTreeView treeView)
{
InitializeComponent();
UISetup.Dialog(this);
_webProvider = new WebProvider();
_webProvider.IgnoreSSL = true;
_treeView = treeView;
}
private void InitializeComponent()
{
label1 = new Label();
tHorizontalSeparator2 = new THorizontalSeparator();
label2 = new Label();
pictureBox2 = new PictureBox();
pickerBox1 = new ThYesNoPickerBox();
button2 = new ThButton();
progressBar1 = new ThProgressBar();
label4 = new Label();
tHorizontalSeparator3 = new THorizontalSeparator();
button3 = new ThButton();
progressBar2 = new ThProgressBar();
dialogFooter1 = new RyzStudio.Windows.ThemedForms.Composite.DialogFooter();
((ISupportInitialize)pictureBox2).BeginInit();
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(156, 23);
label1.TabIndex = 153;
label1.Text = "Update Missing Icons (Only)";
label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// tHorizontalSeparator2
//
tHorizontalSeparator2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
tHorizontalSeparator2.AutoScrollMargin = new System.Drawing.Size(0, 0);
tHorizontalSeparator2.AutoScrollMinSize = new System.Drawing.Size(0, 0);
tHorizontalSeparator2.BackColor = System.Drawing.Color.Transparent;
tHorizontalSeparator2.Location = new System.Drawing.Point(10, 64);
tHorizontalSeparator2.Margin = new Padding(0, 10, 0, 0);
tHorizontalSeparator2.MaximumSize = new System.Drawing.Size(4920, 2);
tHorizontalSeparator2.MinimumSize = new System.Drawing.Size(0, 22);
tHorizontalSeparator2.Name = "tHorizontalSeparator2";
tHorizontalSeparator2.Size = new System.Drawing.Size(424, 22);
tHorizontalSeparator2.TabIndex = 190;
tHorizontalSeparator2.TabStop = false;
//
// label2
//
label2.AutoSize = true;
label2.BackColor = System.Drawing.Color.Transparent;
label2.ForeColor = System.Drawing.SystemColors.ControlText;
label2.Location = new System.Drawing.Point(10, 136);
label2.Margin = new Padding(0);
label2.Name = "label2";
label2.Padding = new Padding(0, 8, 0, 0);
label2.Size = new System.Drawing.Size(231, 23);
label2.TabIndex = 195;
label2.Text = "Retrieve supported icons from bookmarks.";
label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// pictureBox2
//
pictureBox2.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
pictureBox2.BackColor = System.Drawing.Color.Transparent;
pictureBox2.ErrorImage = null;
pictureBox2.InitialImage = null;
pictureBox2.Location = new System.Drawing.Point(402, 294);
pictureBox2.Name = "pictureBox2";
pictureBox2.Size = new System.Drawing.Size(32, 32);
pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox2.TabIndex = 202;
pictureBox2.TabStop = false;
//
// pickerBox1
//
pickerBox1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
pickerBox1.BackColor = System.Drawing.Color.Transparent;
pickerBox1.EnableReactiveVisual = true;
pickerBox1.Font = new System.Drawing.Font("Segoe UI", 9F);
pickerBox1.Location = new System.Drawing.Point(350, 20);
pickerBox1.Name = "pickerBox1";
pickerBox1.SelectedIndex = 1;
pickerBox1.Size = new System.Drawing.Size(84, 35);
pickerBox1.TabIndex = 0;
pickerBox1.TabStop = false;
pickerBox1.Value = true;
//
// button2
//
button2.ActiveImage = null;
button2.Anchor = AnchorStyles.Top | AnchorStyles.Right;
button2.BackColor = System.Drawing.Color.Transparent;
button2.EnableMenuOnClick = false;
button2.EnableReactiveVisual = true;
button2.HoverImage = null;
button2.IdleImage = null;
button2.LabelText = "&Run";
button2.Location = new System.Drawing.Point(306, 136);
button2.Name = "button2";
button2.Size = new System.Drawing.Size(128, 32);
button2.TabIndex = 1;
button2.TabStop = false;
button2.MouseClick += button2_MouseClick;
//
// progressBar1
//
progressBar1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
progressBar1.BackColor = System.Drawing.Color.Transparent;
progressBar1.BarColour = System.Drawing.Color.FromArgb(79, 202, 130);
progressBar1.BarTextColour = System.Drawing.Color.Black;
progressBar1.EnableReactiveVisual = false;
progressBar1.Font = new System.Drawing.Font("Segoe UI", 9F);
progressBar1.Location = new System.Drawing.Point(10, 96);
progressBar1.Maximum = 100;
progressBar1.Minimum = 0;
progressBar1.Name = "progressBar1";
progressBar1.ProgressText = "50/100";
progressBar1.ShowProgressText = true;
progressBar1.Size = new System.Drawing.Size(424, 20);
progressBar1.TabIndex = 206;
progressBar1.TabStop = false;
progressBar1.Value = 50;
//
// label4
//
label4.AutoSize = true;
label4.BackColor = System.Drawing.Color.Transparent;
label4.ForeColor = System.Drawing.SystemColors.ControlText;
label4.Location = new System.Drawing.Point(10, 250);
label4.Margin = new Padding(0);
label4.Name = "label4";
label4.Padding = new Padding(0, 8, 0, 0);
label4.Size = new System.Drawing.Size(83, 23);
label4.TabIndex = 208;
label4.Text = "Clear all icons.";
label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// tHorizontalSeparator3
//
tHorizontalSeparator3.Anchor = AnchorStyles.Top | 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, 178);
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.BackColor = System.Drawing.Color.Transparent;
button3.EnableMenuOnClick = false;
button3.EnableReactiveVisual = true;
button3.HoverImage = null;
button3.IdleImage = null;
button3.LabelText = "Clear &All";
button3.Location = new System.Drawing.Point(306, 250);
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.Top | 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, 210);
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 = null;
dialogFooter1.Dock = DockStyle.Bottom;
dialogFooter1.Location = new System.Drawing.Point(0, 437);
dialogFooter1.Name = "dialogFooter1";
dialogFooter1.Size = new System.Drawing.Size(444, 84);
//
// UpdateIconsForm
//
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = System.Drawing.Color.White;
ClientSize = new System.Drawing.Size(444, 521);
Controls.Add(dialogFooter1);
Controls.Add(progressBar2);
Controls.Add(button3);
Controls.Add(label4);
Controls.Add(tHorizontalSeparator3);
Controls.Add(progressBar1);
Controls.Add(button2);
Controls.Add(pickerBox1);
Controls.Add(pictureBox2);
Controls.Add(label2);
Controls.Add(tHorizontalSeparator2);
Controls.Add(label1);
KeyPreview = true;
MinimumSize = new System.Drawing.Size(460, 560);
Name = "UpdateIconsForm";
Text = "Update Icons";
((ISupportInitialize)pictureBox2).EndInit();
ResumeLayout(false);
PerformLayout();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var model = _treeView.ToNodeList<BookmarkModel>();
if (pickerBox1.Value)
{
model = model.Where(x => x.Value.Icon == null)?.ToList() ?? new List<KeyValuePair<TreeNode, BookmarkModel>>();
}
progressBar1.Minimum = 0;
progressBar1.Value = 0;
progressBar1.Maximum = model.Count;
progressBar2.Minimum = 0;
progressBar2.Value = 0;
progressBar2.Maximum = model.Count;
pickerBox1.Focus();
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (this.IsBusy)
{
e.Cancel = true;
}
}
protected bool IsBusy
{
get => _isBusy;
set
{
_isBusy = value;
UIControl.SetEnable(pickerBox1, !this.IsBusy);
UIControl.Invoke(button2, (x) =>
{
button2.LabelText = (this.IsBusy ? "&Stop" : "&Run");
});
UIControl.Invoke(button3, (x) =>
{
button3.LabelText = (this.IsBusy ? "&Stop" : "Clear &All");
});
UIControl.SetValue(pictureBox2, (this.IsBusy ? RyzStudio.Windows.ThemedForms.Resource2.loading_block : null));
}
}
private async void button2_MouseClick(object sender, MouseEventArgs e)
{
await Task.Run(async () =>
{
if (this.IsBusy)
{
_requestCancel = true;
return;
}
this.IsBusy = true;
var model = _treeView.ToNodeList<BookmarkModel>();
if (pickerBox1.Value)
{
model = model.Where(x => x.Value.Icon == null)?.ToList() ?? new List<KeyValuePair<TreeNode, BookmarkModel>>();
}
progressBar1.Minimum = 0;
progressBar1.Value = 0;
progressBar1.Maximum = model.Count;
foreach (var item in model)
{
progressBar1.Value++;
if (string.IsNullOrWhiteSpace(item.Value.Address))
{
continue;
}
if (_requestCancel)
{
break;
}
var document = await _webProvider.RetrieveHtmlDocument(item.Value.Address);
if (document == null)
{
continue;
}
if (_requestCancel)
{
break;
}
var image = await _webProvider.RetrieveImage(document);
if (image == null)
{
continue;
}
var newModel = item.Value;
newModel.Icon = image;
_treeView.UpdateNode(item.Key, newModel);
}
_requestCancel = false;
this.IsBusy = false;
});
}
private async void button3_MouseClick(object sender, MouseEventArgs e)
{
await Task.Run(() =>
{
if (this.IsBusy)
{
_requestCancel = true;
return;
}
this.IsBusy = true;
var model = _treeView.ToNodeList<BookmarkModel>();
progressBar2.Minimum = 0;
progressBar2.Value = 0;
progressBar2.Maximum = model.Count;
foreach (var item in model)
{
progressBar2.Value++;
if (item.Value.Icon == null)
{
continue;
}
if (_requestCancel)
{
break;
}
var newModel = item.Value;
newModel.Icon = null;
_treeView.UpdateNode(item.Key, newModel);
}
_requestCancel = false;
this.IsBusy = false;
});
}
}
}