227 lines
7.0 KiB
C#
227 lines
7.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using BukkuBuddy.DTOs.SaveFile;
|
|
using BukkuBuddy.Services;
|
|
using RyzStudio.Windows.Forms;
|
|
|
|
namespace BukkuBuddy.Forms
|
|
{
|
|
public class UpdateIconsForm : Form
|
|
{
|
|
private TProgressBar progressBar1;
|
|
private RyzStudio.Windows.ThemedForms.ThUserControl userControl1;
|
|
private Label label1;
|
|
private Label label2;
|
|
|
|
private readonly WebPageService _webPageService;
|
|
|
|
private readonly BookmarkTreeView _treeView = null;
|
|
private readonly bool _updateOnly;
|
|
|
|
private bool cancelRequested = false;
|
|
|
|
|
|
public UpdateIconsForm(BookmarkTreeView treeView, App6Options options, bool updateOnly)
|
|
{
|
|
InitializeComponent();
|
|
|
|
UISetup.Dialog(this, true);
|
|
|
|
this.Text = "Updating Icons";
|
|
|
|
_treeView = treeView;
|
|
_updateOnly = updateOnly;
|
|
|
|
_webPageService = new WebPageService(options.AllowUnsafeSSL, options.Timeout, options.AllowCookies, options.AllowRedirects);
|
|
}
|
|
|
|
|
|
private void InitializeComponent()
|
|
{
|
|
progressBar1 = new TProgressBar();
|
|
userControl1 = new RyzStudio.Windows.ThemedForms.ThUserControl();
|
|
label1 = new Label();
|
|
label2 = new Label();
|
|
userControl1.SuspendLayout();
|
|
SuspendLayout();
|
|
//
|
|
// progressBar1
|
|
//
|
|
progressBar1.BarColour = Color.FromArgb(25, 104, 218);
|
|
progressBar1.Dock = DockStyle.Fill;
|
|
progressBar1.EnableMovable = false;
|
|
progressBar1.Location = new Point(3, 3);
|
|
progressBar1.Maximum = 100;
|
|
progressBar1.Minimum = 0;
|
|
progressBar1.Name = "progressBar1";
|
|
progressBar1.OnProgressChanged = null;
|
|
progressBar1.ShowText = true;
|
|
progressBar1.Size = new Size(418, 18);
|
|
progressBar1.TabIndex = 156;
|
|
progressBar1.Value = 25;
|
|
//
|
|
// userControl1
|
|
//
|
|
userControl1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
|
userControl1.BackColor = Color.Transparent;
|
|
userControl1.Controls.Add(progressBar1);
|
|
userControl1.EnableMovable = false;
|
|
userControl1.Location = new Point(10, 64);
|
|
userControl1.Name = "userControl1";
|
|
userControl1.Size = new Size(424, 24);
|
|
userControl1.TabIndex = 157;
|
|
//
|
|
// label1
|
|
//
|
|
label1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
|
label1.AutoEllipsis = true;
|
|
label1.Location = new Point(10, 20);
|
|
label1.Name = "label1";
|
|
label1.Size = new Size(424, 15);
|
|
label1.TabIndex = 159;
|
|
//
|
|
// label2
|
|
//
|
|
label2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
|
label2.AutoEllipsis = true;
|
|
label2.Location = new Point(10, 40);
|
|
label2.Name = "label2";
|
|
label2.Size = new Size(424, 15);
|
|
label2.TabIndex = 160;
|
|
//
|
|
// LoadIconsForm
|
|
//
|
|
BackColor = Color.White;
|
|
ClientSize = new Size(444, 117);
|
|
Controls.Add(label2);
|
|
Controls.Add(label1);
|
|
Controls.Add(userControl1);
|
|
Name = "LoadIconsForm";
|
|
Text = "Update Icons";
|
|
userControl1.ResumeLayout(false);
|
|
ResumeLayout(false);
|
|
}
|
|
|
|
protected async override void OnShown(EventArgs e)
|
|
{
|
|
base.OnShown(e);
|
|
|
|
await Task.Run(async () =>
|
|
{
|
|
this.Clear();
|
|
|
|
var model = _treeView.ToNodeList<App6Options.Item>();
|
|
|
|
// Update only
|
|
if (_updateOnly)
|
|
{
|
|
model = model.Where(x => x.Value.Icon == null)?.ToList() ?? new List<KeyValuePair<TreeNode, App6Options.Item>>();
|
|
}
|
|
|
|
// Update progress bars
|
|
progressBar1.Maximum = model.Count;
|
|
progressBar1.Value = 0;
|
|
|
|
UIControl.SetText(label1, "Updating...");
|
|
UIControl.SetText(label2, "");
|
|
|
|
foreach (var item in model)
|
|
{
|
|
if (cancelRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
UIControl.SetText(label2, "... " + item.Value.Title);
|
|
|
|
progressBar1.Value++;
|
|
|
|
if (!_webPageService.IsValidUrl(item.Value.Address))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Update only
|
|
if (_updateOnly)
|
|
{
|
|
if (item.Value.Icon != null)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
|
|
var newModel = item.Value;
|
|
|
|
HtmlAgilityPack.HtmlDocument htmlDocument = null;
|
|
|
|
try
|
|
{
|
|
htmlDocument = await _webPageService.GetDocument(newModel.Address);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (htmlDocument == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
newModel.Icon = favicon;
|
|
}
|
|
|
|
_treeView.UpdateNode(item.Key, newModel);
|
|
}
|
|
|
|
UIControl.SetText(label1, "");
|
|
UIControl.SetText(label2, "Done");
|
|
});
|
|
|
|
this.DialogResult = DialogResult.OK;
|
|
}
|
|
|
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
|
{
|
|
base.OnFormClosing(e);
|
|
|
|
if (this.DialogResult != DialogResult.OK)
|
|
{
|
|
e.Cancel = true;
|
|
|
|
if (!cancelRequested)
|
|
{
|
|
UIControl.SetText(label1, "Cancelling...");
|
|
|
|
cancelRequested = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void Clear()
|
|
{
|
|
UIControl.SetText(label1, "Ready");
|
|
UIControl.SetText(label2, "");
|
|
|
|
progressBar1.Value = progressBar1.Maximum = 0;
|
|
}
|
|
|
|
}
|
|
} |