Added: icon database support

This commit is contained in:
Ray 2021-09-21 14:53:18 +01:00
parent 4dd399f537
commit 253365d25e
3 changed files with 80 additions and 80 deletions

View File

@ -3,6 +3,7 @@ using System;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Windows.Forms;
namespace BookmarkManager namespace BookmarkManager
{ {
@ -30,14 +31,6 @@ namespace BookmarkManager
if (isNew) Initialise(); if (isNew) Initialise();
//if (File.Exists(filename))
//{
// File.Delete("test.db3");
//}
} }
public bool AddIcon(string id, Image image) public bool AddIcon(string id, Image image)
@ -52,14 +45,17 @@ namespace BookmarkManager
command.Connection = dbConnection; command.Connection = dbConnection;
command.CommandText = "INSERT INTO icons (id, content) VALUES (@id, @content)"; command.CommandText = "INSERT INTO icons (id, content) VALUES (@id, @content)";
command.Parameters.Add("@id", SqliteType.Text).Value = id; command.Parameters.Add("@id", SqliteType.Text).Value = id;
command.Parameters.Add("@content", SqliteType.Blob, 20).Value = ImageToBytes(image); command.Parameters.Add("@content", SqliteType.Blob).Value = ImageToBytes(image);
try try
{ {
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
catch (Exception) catch (Exception exc)
{ {
//#if DEBUG
// MessageBox.Show(exc.Message);
//#endif
return false; return false;
} }
@ -89,8 +85,11 @@ namespace BookmarkManager
{ {
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
catch (Exception) catch (Exception exc)
{ {
//#if DEBUG
// MessageBox.Show(exc.Message);
//#endif
return false; return false;
} }
@ -103,30 +102,30 @@ namespace BookmarkManager
if (dbConnection == null) return null; if (dbConnection == null) return null;
if (string.IsNullOrWhiteSpace(id)) return null; if (string.IsNullOrWhiteSpace(id)) return null;
if (!HasIcon(id)) return null;
byte[] iconBytes = null; byte[] iconBytes = null;
SqliteCommand command = new SqliteCommand(); SqliteCommand command = new SqliteCommand();
command.Connection = dbConnection; command.Connection = dbConnection;
command.CommandText = "SELECT content FROM icons WHERE @id"; command.CommandText = "SELECT content FROM icons WHERE id = @id";
command.Parameters.Add("@id", SqliteType.Text).Value = id; command.Parameters.Add("@id", SqliteType.Text).Value = id;
try try
{ {
using (var reader = command.ExecuteReader()) var rs = command.ExecuteScalar();
if (rs != null)
{ {
while (reader.Read()) iconBytes = (byte[])rs;
{
if (reader["content"] != null && !Convert.IsDBNull(reader["content"]))
{
iconBytes = (byte[])reader["content"];
}
}
} }
command.Dispose(); command.Dispose();
} }
catch (Exception) catch (Exception exc)
{ {
//#if DEBUG
// MessageBox.Show(exc.Message);
//#endif
return null; return null;
} }
@ -141,8 +140,11 @@ namespace BookmarkManager
{ {
img = Image.FromStream(new MemoryStream(iconBytes)); img = Image.FromStream(new MemoryStream(iconBytes));
} }
catch (Exception) catch (Exception exc)
{ {
//#if DEBUG
// MessageBox.Show(exc.Message);
//#endif
return null; return null;
} }
@ -154,33 +156,30 @@ namespace BookmarkManager
if (dbConnection == null) return false; if (dbConnection == null) return false;
if (string.IsNullOrWhiteSpace(id)) return false; if (string.IsNullOrWhiteSpace(id)) return false;
int rv = 0; string rs = string.Empty;
SqliteCommand command = new SqliteCommand(); SqliteCommand command = new SqliteCommand();
command.Connection = dbConnection; command.Connection = dbConnection;
command.CommandText = "SELECT COUNT(1) AS count FROM icons WHERE @id"; command.CommandText = "SELECT COUNT(1) AS count FROM icons WHERE id = @id";
command.Parameters.Add("@id", SqliteType.Text).Value = id; command.Parameters.Add("@id", SqliteType.Text).Value = id;
try try
{ {
using (var reader = command.ExecuteReader()) rs = command.ExecuteScalar()?.ToString() ?? string.Empty;
{
while (reader.Read())
{
if (reader["count"] != null && !Convert.IsDBNull(reader["count"]))
{
rv = (int)reader["count"];
}
}
}
command.Dispose(); command.Dispose();
} }
catch (Exception) catch (Exception exc)
{ {
//#if DEBUG
// MessageBox.Show(exc.Message);
//#endif
return false; return false;
} }
int rv;
if (!int.TryParse(rs, out rv)) rv = 0;
return (rv > 0); return (rv > 0);
} }
@ -196,14 +195,17 @@ namespace BookmarkManager
command.Connection = dbConnection; command.Connection = dbConnection;
command.CommandText = "UPDATE icons SET content = @content WHERE id = @id"; command.CommandText = "UPDATE icons SET content = @content WHERE id = @id";
command.Parameters.Add("@id", SqliteType.Text).Value = id; command.Parameters.Add("@id", SqliteType.Text).Value = id;
command.Parameters.Add("@content", SqliteType.Blob, 20).Value = ImageToBytes(image); command.Parameters.Add("@content", SqliteType.Blob).Value = ImageToBytes(image);
try try
{ {
command.ExecuteNonQuery(); command.ExecuteNonQuery();
} }
catch (Exception) catch (Exception exc)
{ {
//#if DEBUG
// MessageBox.Show(exc.Message);
//#endif
return false; return false;
} }
@ -221,8 +223,11 @@ namespace BookmarkManager
command.ExecuteNonQuery(); command.ExecuteNonQuery();
command.Dispose(); command.Dispose();
} }
catch (Exception) catch (Exception exc)
{ {
//#if DEBUG
// MessageBox.Show(exc.Message);
//#endif
return false; return false;
} }
@ -239,6 +244,5 @@ namespace BookmarkManager
return stream.ToArray(); return stream.ToArray();
} }
} }
} }

View File

@ -44,13 +44,13 @@ namespace FizzyLauncher
jsonfigFilename = Path.ChangeExtension(Application.ExecutablePath, "jsonfig"); jsonfigFilename = Path.ChangeExtension(Application.ExecutablePath, "jsonfig");
if (iconDatabase == null) iconDatabase = new IconDatabase(Path.ChangeExtension(Application.ExecutablePath, "db")); if (iconDatabase == null) iconDatabase = new IconDatabase(Path.ChangeExtension(Application.ExecutablePath, "db"));
iconDatabase.AddIcon(Guid.NewGuid().ToString(), UIResource.arrow_down);
this.AutoScaleMode = AutoScaleMode.None; this.AutoScaleMode = AutoScaleMode.None;
this.StartPosition = FormStartPosition.WindowsDefaultLocation; this.StartPosition = FormStartPosition.WindowsDefaultLocation;
this.ClientSize = new System.Drawing.Size(300, 580); this.ClientSize = new System.Drawing.Size(300, 580);
//this.Visible = false; //this.Visible = false;
treeView1.IconDatabase = iconDatabase;
treeView1.RootContextMenu = rootContextMenu; treeView1.RootContextMenu = rootContextMenu;
treeView1.FolderContextMenu = folderContextMenu; treeView1.FolderContextMenu = folderContextMenu;
treeView1.PageContextMenu = pageContextMenu; treeView1.PageContextMenu = pageContextMenu;
@ -76,7 +76,6 @@ namespace FizzyLauncher
//ThreadControl.SetVisible(this, false); //ThreadControl.SetVisible(this, false);
await LoadAppSession(jsonfigFilename); await LoadAppSession(jsonfigFilename);
//await InitialiseIconDB(iconDBFilename);
InvalidateAppSession(); InvalidateAppSession();

View File

@ -207,10 +207,8 @@ namespace RyzStudio.Windows.Forms
protected const string DEFAULT_NEW_FOLDER_NAME = "New Folder"; protected const string DEFAULT_NEW_FOLDER_NAME = "New Folder";
protected const string PATH_SEPARATOR = "\n"; protected const string PATH_SEPARATOR = "\n";
public event EventHandler OnNodeChanged = null; public event EventHandler OnNodeChanged = null;
//protected IconDatabase iconDatabase = null;
protected TreeNode draggingNode = null; protected TreeNode draggingNode = null;
protected bool hasChanged = false; protected bool hasChanged = false;
@ -616,9 +614,9 @@ namespace RyzStudio.Windows.Forms
// this.HasChanged = false; // this.HasChanged = false;
//} //}
public void AddItem(BookmarkItem item) public TreeNode AddItem(BookmarkItem item)
{ {
int iconIndex = addIcon(item); int iconIndex = FindIcon(item);
TreeNode tn = new TreeNode(item.SiteName, iconIndex, iconIndex); TreeNode tn = new TreeNode(item.SiteName, iconIndex, iconIndex);
tn.Tag = item; tn.Tag = item;
@ -629,22 +627,23 @@ namespace RyzStudio.Windows.Forms
ThreadControl.Add(tn2, tn); ThreadControl.Add(tn2, tn);
this.HasChanged = true; this.HasChanged = true;
return tn;
} }
public TreeNode AddItem(TreeNode treeNode, BookmarkItem item) public TreeNode AddItem(TreeNode treeNode, BookmarkItem item)
{ {
if (treeNode == null) if (treeNode == null) return null;
{ if (item == null) return null;
return null;
}
int iconIndex = addIcon(item); int iconIndex = FindIcon(item);
TreeNode tn = new TreeNode(item.SiteName, iconIndex, iconIndex); TreeNode tn = new TreeNode(item.SiteName, iconIndex, iconIndex);
tn.Tag = item; tn.Tag = item;
tn.ToolTipText = item.ToString(); tn.ToolTipText = item.ToString();
treeNode.Nodes.Add(tn); //treeNode.Nodes.Add(tn);
ThreadControl.Add(treeNode, tn);
this.HasChanged = true; this.HasChanged = true;
@ -859,14 +858,12 @@ namespace RyzStudio.Windows.Forms
public new void Sort() => Sort(this.SelectedNode); public new void Sort() => Sort(this.SelectedNode);
public void UpdateItem(TreeNode treeNode, BookmarkItem item, Image image) public void UpdateItem(TreeNode treeNode, BookmarkItem item)
{ {
if (treeNode == null) if (treeNode == null) return;
{ if (item == null) return;
return;
}
int iconIndex = addIcon(item); int iconIndex = FindIcon(item);
treeNode.Text = item.SiteName; treeNode.Text = item.SiteName;
treeNode.ImageIndex = iconIndex; treeNode.ImageIndex = iconIndex;
@ -878,11 +875,32 @@ namespace RyzStudio.Windows.Forms
} }
protected int addIcon(BookmarkItem viewModel) protected int FindIcon(BookmarkItem item)
{
if (this.IconDatabase == null) return (int)IconSet.Default;
if (item == null) return (int)IconSet.Default;
if (string.IsNullOrWhiteSpace(item.SiteName)) return (int)IconSet.Default;
string iconID = Crypto.GetSHA256Hash(item?.SiteName);
Image image = this.IconDatabase.FindIcon(iconID);
if (image == null)
{ {
return (int)IconSet.Default; return (int)IconSet.Default;
} }
if (this.ImageList.Images.ContainsKey(iconID))
{
this.ImageList.Images.RemoveByKey(iconID);
}
ThreadControl.Add(this, this.ImageList, iconID, image);
return this.ImageList.Images.IndexOfKey(iconID);
//return (int)IconSet.Default;
}
//protected int addIcon(BookmarkItemViewModel viewModel) => addIcon(viewModel.ToModel()); //protected int addIcon(BookmarkItemViewModel viewModel) => addIcon(viewModel.ToModel());
//protected int addIcon(BookmarkItemModel model) //protected int addIcon(BookmarkItemModel model)
@ -976,27 +994,6 @@ namespace RyzStudio.Windows.Forms
return tn; return tn;
} }
//protected bool IsNodeChild(TreeNode dragNode, TreeNode dropNode)
//{
// TreeNode tn = dropNode;
// while (true)
// {
// if (tn.Parent == null)
// {
// break;
// }
// if (tn.Equals(dragNode))
// {
// return true;
// }
// tn = tn.Parent;
// }
// return false;
//}
protected void TraverseBookmarkList(List<BookmarkItem> rs, TreeNode node) protected void TraverseBookmarkList(List<BookmarkItem> rs, TreeNode node)
{ {
foreach (TreeNode tn in node.Nodes) foreach (TreeNode tn in node.Nodes)