This repository has been archived on 2024-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager-r4/Forms/AddMultiPageForm.cs
2026-05-22 01:16:08 +01:00

276 lines
9.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using BukkuBuddy.DTOs.SaveFile;
using BukkuBuddy.Services;
using RyzStudio.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
namespace BukkuBuddy.Forms
{
public class AddMultiPageForm : Form
{
private Label label1;
private RyzStudio.Windows.ThemedForms.Composite.DialogFooter dialogFooter1;
private ThToolbarMemoBox memoBox1;
private ThUserControl userControl1;
private TProgressBar progressBar1;
private readonly WebPageService _webPageService;
private readonly string _rootPath;
private List<App6Options.Item> result = null;
private bool isBusy = false;
public AddMultiPageForm(string rootPath, App6Options options)
{
InitializeComponent();
UISetup.Dialog(this);
_webPageService = new WebPageService(options.AllowUnsafeSSL, options.Timeout, options.AllowCookies, options.AllowRedirects);
_rootPath = rootPath;
}
private void InitializeComponent()
{
label1 = new Label();
dialogFooter1 = new RyzStudio.Windows.ThemedForms.Composite.DialogFooter();
memoBox1 = new ThToolbarMemoBox();
userControl1 = new ThUserControl();
progressBar1 = new TProgressBar();
userControl1.SuspendLayout();
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(60, 23);
label1.TabIndex = 153;
label1.Text = "Addresses";
label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// dialogFooter1
//
dialogFooter1.BackColor = System.Drawing.Color.FromArgb(240, 240, 240);
dialogFooter1.Dock = DockStyle.Bottom;
dialogFooter1.EnableMovable = false;
dialogFooter1.IsBusy = false;
dialogFooter1.Location = new System.Drawing.Point(0, 451);
dialogFooter1.Name = "dialogFooter1";
dialogFooter1.Size = new System.Drawing.Size(384, 70);
dialogFooter1.TabIndex = 212;
dialogFooter1.TabStop = false;
dialogFooter1.OnButton1Click += dialogFooter1_OnButton1Click;
//
// memoBox1
//
memoBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
memoBox1.BackColor = System.Drawing.Color.Transparent;
memoBox1.ClearedValue = "";
memoBox1.EnableMovable = false;
memoBox1.Location = new System.Drawing.Point(95, 20);
memoBox1.Name = "memoBox1";
memoBox1.ReadOnly = false;
memoBox1.ScrollBars = ScrollBars.Vertical;
memoBox1.Size = new System.Drawing.Size(280, 387);
memoBox1.TabIndex = 211;
memoBox1.TabStop = false;
memoBox1.WordWrap = false;
//
// userControl1
//
userControl1.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
userControl1.BackColor = System.Drawing.Color.Transparent;
userControl1.Controls.Add(progressBar1);
userControl1.EnableMovable = false;
userControl1.Location = new System.Drawing.Point(95, 417);
userControl1.Name = "userControl1";
userControl1.Size = new System.Drawing.Size(280, 24);
userControl1.TabIndex = 214;
//
// progressBar1
//
progressBar1.BarColour = System.Drawing.Color.FromArgb(190, 100, 242);
progressBar1.Dock = DockStyle.Fill;
progressBar1.EnableMovable = false;
progressBar1.Location = new System.Drawing.Point(3, 3);
progressBar1.Maximum = 100;
progressBar1.Minimum = 0;
progressBar1.Name = "progressBar1";
progressBar1.OnProgressChanged = null;
progressBar1.ShowText = true;
progressBar1.Size = new System.Drawing.Size(274, 18);
progressBar1.TabIndex = 156;
progressBar1.Value = 25;
//
// AddBatchPageForm
//
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = System.Drawing.Color.White;
ClientSize = new System.Drawing.Size(384, 521);
Controls.Add(userControl1);
Controls.Add(memoBox1);
Controls.Add(dialogFooter1);
Controls.Add(label1);
KeyPreview = true;
MinimumSize = new System.Drawing.Size(400, 560);
Name = "AddBatchPageForm";
Text = "Add Multiple Pages";
userControl1.ResumeLayout(false);
ResumeLayout(false);
PerformLayout();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
memoBox1.Text = string.Empty;
progressBar1.Minimum = 0;
progressBar1.Value = 0;
progressBar1.Maximum = 0;
memoBox1.Focus();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (this.IsBusy)
{
e.Cancel = true;
}
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public List<App6Options.Item> Result
{
get => result;
private set => result = value;
}
protected bool IsBusy
{
get => isBusy;
set
{
isBusy = value;
UIControl.SetEnable(memoBox1, !isBusy);
dialogFooter1.IsBusy = isBusy;
}
}
private async void dialogFooter1_OnButton1Click(object sender, MouseEventArgs e)
{
if (this.IsBusy)
{
return;
}
this.IsBusy = true;
this.Result = new List<App6Options.Item>();
await Task.Run(async () =>
{
progressBar1.Value = 0;
progressBar1.Maximum = memoBox1.Lines?.Length ?? 0;
foreach (var item in memoBox1.Lines ?? new string[0])
{
progressBar1.Value++;
if (string.IsNullOrWhiteSpace(item))
{
continue;
}
if (!_webPageService.IsValidUrl(item))
{
continue;
}
var newBookmark = await CreateApp6OptionsItem(item, _rootPath);
if (newBookmark == null)
{
continue;
}
this.Result.Add(newBookmark);
}
});
this.IsBusy = false;
this.DialogResult = DialogResult.OK;
this.Close();
}
private async Task<App6Options.Item> CreateApp6OptionsItem(string url, string path)
{
if (!_webPageService.IsValidUrl(url))
{
return null;
}
HtmlAgilityPack.HtmlDocument htmlDocument = null;
try
{
htmlDocument = await _webPageService.GetDocument(url);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Update Icon", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
if (htmlDocument == null)
{
return null;
}
var response = new App6Options.Item();
response.Address = url;
response.Path = path;
response.Title = _webPageService.ParseTitle(htmlDocument) ?? string.Empty;
response.Description = _webPageService.ParseDescription(htmlDocument) ?? string.Empty;
var faviconUrl = _webPageService.ParseFavicon(htmlDocument);
if (!string.IsNullOrWhiteSpace(faviconUrl))
{
var favicon = await _webPageService.GetImage(faviconUrl);
if (favicon != null)
{
if (favicon.Width > 16)
{
favicon = RyzStudio.Drawing.ImageEditor.Resize(favicon, 16, 16);
}
}
response.Icon = favicon;
}
return response;
}
}
}