bookmark-manager-r4/BookmarkForm.cs
2021-09-21 13:28:52 +01:00

420 lines
19 KiB
C#

using FizzyLauncher.Models;
using System.Windows.Forms;
using RyzStudio.Windows.ThemedForms;
using System;
using RyzStudio.Windows.Forms;
using bzit.bomg.Models;
using System.Drawing;
using BookmarkManager;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Net;
using System.IO;
namespace FizzyLauncher
{
public class BookmarkForm : TDialogForm
{
private System.Windows.Forms.Label label1;
private TButton button1;
private RyzStudio.Windows.Forms.THorizontalSeparator tHorizontalSeparator1;
private TTextBox textBox1;
private RyzStudio.Windows.Forms.THorizontalSeparator tHorizontalSeparator2;
private Label label2;
private Label label3;
private TMemoBox memoBox1;
private TMemoBox memoBox2;
private Label label4;
private TButtonTextBox textBox2;
private PictureBox pictureBox1;
private PictureBox pictureBox2;
private ToolTip toolTip1;
private System.ComponentModel.IContainer components;
protected bool isBusy = false;
protected WebParser webParser = null;
protected WebClient webClient = null;
public BookmarkForm(BookmarkItem model, Image icon) : base()
{
InitializeComponent();
textBox1.Text = model.SiteName?.Trim() ?? string.Empty;
textBox2.Text = model.SiteAddress?.Trim() ?? string.Empty;
memoBox1.Text = model.SiteDescription?.Trim() ?? string.Empty;
memoBox2.Text = model.Notes?.Trim() ?? string.Empty;
pictureBox1.Image = icon;
textBox2.SetTooltipText(toolTip1, "Retrieve web page information");
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new RyzStudio.Windows.ThemedForms.TButton();
this.tHorizontalSeparator1 = new RyzStudio.Windows.Forms.THorizontalSeparator();
this.tHorizontalSeparator2 = new RyzStudio.Windows.Forms.THorizontalSeparator();
this.textBox1 = new RyzStudio.Windows.ThemedForms.TTextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.memoBox1 = new RyzStudio.Windows.ThemedForms.TMemoBox();
this.memoBox2 = new RyzStudio.Windows.ThemedForms.TMemoBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox2 = new RyzStudio.Windows.ThemedForms.TButtonTextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.pictureBox2 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.ForeColor = System.Drawing.SystemColors.ControlText;
this.label1.Location = new System.Drawing.Point(10, 21);
this.label1.Margin = new System.Windows.Forms.Padding(0);
this.label1.Name = "label1";
this.label1.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
this.label1.Size = new System.Drawing.Size(29, 34);
this.label1.TabIndex = 153;
this.label1.Text = "Title";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.BackColor = System.Drawing.Color.Transparent;
this.button1.DefaultImage = null;
this.button1.DownImage = null;
this.button1.IsSelected = false;
this.button1.LabelText = "&Save";
this.button1.Location = new System.Drawing.Point(241, 469);
this.button1.Margin = new System.Windows.Forms.Padding(10);
this.button1.Name = "button1";
this.button1.OverImage = null;
this.button1.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3);
this.button1.Size = new System.Drawing.Size(128, 32);
this.button1.TabIndex = 173;
this.button1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.button1_MouseClick);
//
// tHorizontalSeparator1
//
this.tHorizontalSeparator1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tHorizontalSeparator1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.tHorizontalSeparator1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.tHorizontalSeparator1.BackColor = System.Drawing.Color.Transparent;
this.tHorizontalSeparator1.Location = new System.Drawing.Point(10, 437);
this.tHorizontalSeparator1.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0);
this.tHorizontalSeparator1.MaximumSize = new System.Drawing.Size(4920, 2);
this.tHorizontalSeparator1.MinimumSize = new System.Drawing.Size(0, 22);
this.tHorizontalSeparator1.Name = "tHorizontalSeparator1";
this.tHorizontalSeparator1.Padding = new System.Windows.Forms.Padding(0, 10, 0, 10);
this.tHorizontalSeparator1.Size = new System.Drawing.Size(364, 22);
this.tHorizontalSeparator1.TabIndex = 188;
//
// tHorizontalSeparator2
//
this.tHorizontalSeparator2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tHorizontalSeparator2.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.tHorizontalSeparator2.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.tHorizontalSeparator2.BackColor = System.Drawing.Color.Transparent;
this.tHorizontalSeparator2.Location = new System.Drawing.Point(10, 223);
this.tHorizontalSeparator2.Margin = new System.Windows.Forms.Padding(10, 0, 10, 0);
this.tHorizontalSeparator2.MaximumSize = new System.Drawing.Size(4920, 2);
this.tHorizontalSeparator2.MinimumSize = new System.Drawing.Size(0, 22);
this.tHorizontalSeparator2.Name = "tHorizontalSeparator2";
this.tHorizontalSeparator2.Padding = new System.Windows.Forms.Padding(0, 10, 0, 10);
this.tHorizontalSeparator2.Size = new System.Drawing.Size(364, 22);
this.tHorizontalSeparator2.TabIndex = 190;
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.BackColor = System.Drawing.Color.Transparent;
this.textBox1.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.textBox1.Location = new System.Drawing.Point(110, 20);
this.textBox1.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
this.textBox1.Name = "textBox1";
this.textBox1.Padding = new System.Windows.Forms.Padding(10, 10, 9, 9);
this.textBox1.Size = new System.Drawing.Size(222, 35);
this.textBox1.SubmitButton = null;
this.textBox1.TabIndex = 192;
this.textBox1.UseSystemPasswordChar = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.BackColor = System.Drawing.Color.Transparent;
this.label2.ForeColor = System.Drawing.SystemColors.ControlText;
this.label2.Location = new System.Drawing.Point(10, 67);
this.label2.Margin = new System.Windows.Forms.Padding(0);
this.label2.Name = "label2";
this.label2.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
this.label2.Size = new System.Drawing.Size(49, 34);
this.label2.TabIndex = 193;
this.label2.Text = "Address";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.AutoSize = true;
this.label3.BackColor = System.Drawing.Color.Transparent;
this.label3.ForeColor = System.Drawing.SystemColors.ControlText;
this.label3.Location = new System.Drawing.Point(10, 113);
this.label3.Margin = new System.Windows.Forms.Padding(0);
this.label3.Name = "label3";
this.label3.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
this.label3.Size = new System.Drawing.Size(67, 34);
this.label3.TabIndex = 195;
this.label3.Text = "Description";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// memoBox1
//
this.memoBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.memoBox1.BackColor = System.Drawing.Color.Transparent;
this.memoBox1.Location = new System.Drawing.Point(110, 112);
this.memoBox1.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
this.memoBox1.Name = "memoBox1";
this.memoBox1.Padding = new System.Windows.Forms.Padding(10, 10, 3, 9);
this.memoBox1.Size = new System.Drawing.Size(259, 105);
this.memoBox1.TabIndex = 197;
//
// memoBox2
//
this.memoBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.memoBox2.BackColor = System.Drawing.Color.Transparent;
this.memoBox2.Location = new System.Drawing.Point(110, 251);
this.memoBox2.Margin = new System.Windows.Forms.Padding(10, 6, 10, 6);
this.memoBox2.Name = "memoBox2";
this.memoBox2.Padding = new System.Windows.Forms.Padding(10, 10, 3, 9);
this.memoBox2.Size = new System.Drawing.Size(259, 180);
this.memoBox2.TabIndex = 199;
//
// label4
//
this.label4.AutoSize = true;
this.label4.BackColor = System.Drawing.Color.Transparent;
this.label4.ForeColor = System.Drawing.SystemColors.ControlText;
this.label4.Location = new System.Drawing.Point(10, 252);
this.label4.Margin = new System.Windows.Forms.Padding(0);
this.label4.Name = "label4";
this.label4.Padding = new System.Windows.Forms.Padding(0, 9, 0, 10);
this.label4.Size = new System.Drawing.Size(38, 34);
this.label4.TabIndex = 198;
this.label4.Text = "Notes";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// textBox2
//
this.textBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox2.BackColor = System.Drawing.Color.Transparent;
this.textBox2.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.textBox2.HighlightImage = global::BookmarkManager.UIResource.search2;
this.textBox2.Location = new System.Drawing.Point(110, 66);
this.textBox2.Margin = new System.Windows.Forms.Padding(10, 3, 3, 3);
this.textBox2.Name = "textBox2";
this.textBox2.NormalImage = global::BookmarkManager.UIResource.search;
this.textBox2.Padding = new System.Windows.Forms.Padding(10, 10, 9, 9);
this.textBox2.Size = new System.Drawing.Size(259, 35);
this.textBox2.SubmitButton = null;
this.textBox2.TabIndex = 200;
this.textBox2.UseSystemPasswordChar = false;
this.textBox2.OnButtonClick += new System.EventHandler(this.textBox2_OnButtonClick);
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
this.pictureBox1.ErrorImage = null;
this.pictureBox1.InitialImage = null;
this.pictureBox1.Location = new System.Drawing.Point(337, 20);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(32, 35);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBox1.TabIndex = 201;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox2.BackColor = System.Drawing.Color.Transparent;
this.pictureBox2.ErrorImage = null;
this.pictureBox2.InitialImage = null;
this.pictureBox2.Location = new System.Drawing.Point(196, 469);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(32, 32);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBox2.TabIndex = 202;
this.pictureBox2.TabStop = false;
//
// BookmarkForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(384, 521);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.memoBox2);
this.Controls.Add(this.label4);
this.Controls.Add(this.memoBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.tHorizontalSeparator2);
this.Controls.Add(this.tHorizontalSeparator1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.MinimumSize = new System.Drawing.Size(400, 560);
this.Name = "BookmarkForm";
this.Text = "Edit Bookmark";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.DialogResult = DialogResult.None;
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (IsBusy)
{
e.Cancel = true;
}
}
public BookmarkResult Model
{
get => new BookmarkResult()
{
Item = new BookmarkItem()
{
SiteName = textBox1.Text?.Trim() ?? string.Empty,
SiteAddress = textBox2.Text?.Trim() ?? string.Empty,
SiteDescription = memoBox1.Text?.Trim() ?? string.Empty,
Notes = memoBox2.Text?.Trim() ?? string.Empty
},
Icon = pictureBox1.Image
};
}
protected bool IsBusy
{
get => isBusy;
set
{
isBusy = value;
ThreadControl.SetValue(pictureBox2, (isBusy ? AppResource.loading_block : null));
ThreadControl.SetEnable(textBox1, !isBusy);
ThreadControl.SetEnable(textBox2, !isBusy);
ThreadControl.SetEnable(memoBox1 , !isBusy);
ThreadControl.SetEnable(memoBox2, !isBusy);
ThreadControl.SetEnable(button1, !isBusy);
}
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private async void textBox2_OnButtonClick(object sender, EventArgs e)
{
await Task.Run(() =>
{
if (IsBusy) return;
IsBusy = true;
if (webParser == null) webParser = new WebParser();
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
IsBusy = false;
return;
}
BookmarkResult rs = webParser.RetrieveDetails(textBox2.Text);
if (rs == null)
{
IsBusy = false;
return;
}
if (rs.Item == null) rs.Item = new BookmarkItem();
if (!string.IsNullOrWhiteSpace(rs.Item.SiteName)) ThreadControl.SetText(textBox1, rs.Item.SiteName);
if (!string.IsNullOrWhiteSpace(rs.Item.SiteDescription)) ThreadControl.SetText(memoBox1, rs.Item.SiteDescription);
if (string.IsNullOrWhiteSpace(rs.IconURL))
{
ThreadControl.Clear(pictureBox1);
}
else
{
ThreadControl.SetValue(pictureBox1, RetrieveImage(rs.IconURL));
}
IsBusy = false;
});
}
private Image RetrieveImage(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
return null;
}
if (webClient == null) webClient = new WebClient();
webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
try
{
byte[] byteData = webClient.DownloadData(url);
if (!RyzStudio.IO.FileType.IsImage(byteData))
{
throw new Exception("Not a supported image");
}
Image img = Image.FromStream(new MemoryStream(byteData));
return new Bitmap(img, 16, 16);
}
catch (Exception)
{
return null;
}
}
}
}