diff --git a/Classes/IconDatabase.cs b/Classes/IconDatabase.cs index aa1fccc..5b2ca3d 100644 --- a/Classes/IconDatabase.cs +++ b/Classes/IconDatabase.cs @@ -3,6 +3,7 @@ using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; +using System.Windows.Forms; namespace BookmarkManager { @@ -30,14 +31,6 @@ namespace BookmarkManager if (isNew) Initialise(); - - - - //if (File.Exists(filename)) - //{ - // File.Delete("test.db3"); - //} - } public bool AddIcon(string id, Image image) @@ -52,14 +45,17 @@ namespace BookmarkManager command.Connection = dbConnection; command.CommandText = "INSERT INTO icons (id, content) VALUES (@id, @content)"; 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 { command.ExecuteNonQuery(); } - catch (Exception) + catch (Exception exc) { +//#if DEBUG +// MessageBox.Show(exc.Message); +//#endif return false; } @@ -89,8 +85,11 @@ namespace BookmarkManager { command.ExecuteNonQuery(); } - catch (Exception) + catch (Exception exc) { +//#if DEBUG +// MessageBox.Show(exc.Message); +//#endif return false; } @@ -103,30 +102,30 @@ namespace BookmarkManager if (dbConnection == null) return null; if (string.IsNullOrWhiteSpace(id)) return null; + if (!HasIcon(id)) return null; + byte[] iconBytes = null; SqliteCommand command = new SqliteCommand(); 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; try { - using (var reader = command.ExecuteReader()) + var rs = command.ExecuteScalar(); + if (rs != null) { - while (reader.Read()) - { - if (reader["content"] != null && !Convert.IsDBNull(reader["content"])) - { - iconBytes = (byte[])reader["content"]; - } - } + iconBytes = (byte[])rs; } command.Dispose(); } - catch (Exception) + catch (Exception exc) { +//#if DEBUG +// MessageBox.Show(exc.Message); +//#endif return null; } @@ -141,8 +140,11 @@ namespace BookmarkManager { img = Image.FromStream(new MemoryStream(iconBytes)); } - catch (Exception) + catch (Exception exc) { +//#if DEBUG +// MessageBox.Show(exc.Message); +//#endif return null; } @@ -154,33 +156,30 @@ namespace BookmarkManager if (dbConnection == null) return false; if (string.IsNullOrWhiteSpace(id)) return false; - int rv = 0; + string rs = string.Empty; SqliteCommand command = new SqliteCommand(); 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; try { - using (var reader = command.ExecuteReader()) - { - while (reader.Read()) - { - if (reader["count"] != null && !Convert.IsDBNull(reader["count"])) - { - rv = (int)reader["count"]; - } - } - } + rs = command.ExecuteScalar()?.ToString() ?? string.Empty; command.Dispose(); } - catch (Exception) + catch (Exception exc) { +//#if DEBUG +// MessageBox.Show(exc.Message); +//#endif return false; } + int rv; + if (!int.TryParse(rs, out rv)) rv = 0; + return (rv > 0); } @@ -196,14 +195,17 @@ namespace BookmarkManager command.Connection = dbConnection; command.CommandText = "UPDATE icons SET content = @content WHERE id = @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 { command.ExecuteNonQuery(); } - catch (Exception) + catch (Exception exc) { +//#if DEBUG +// MessageBox.Show(exc.Message); +//#endif return false; } @@ -221,8 +223,11 @@ namespace BookmarkManager command.ExecuteNonQuery(); command.Dispose(); } - catch (Exception) + catch (Exception exc) { +//#if DEBUG +// MessageBox.Show(exc.Message); +//#endif return false; } @@ -239,6 +244,5 @@ namespace BookmarkManager return stream.ToArray(); } - } } \ No newline at end of file diff --git a/MainForm.cs b/MainForm.cs index a53486c..08274f4 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -44,13 +44,13 @@ namespace FizzyLauncher jsonfigFilename = Path.ChangeExtension(Application.ExecutablePath, "jsonfig"); if (iconDatabase == null) iconDatabase = new IconDatabase(Path.ChangeExtension(Application.ExecutablePath, "db")); - iconDatabase.AddIcon(Guid.NewGuid().ToString(), UIResource.arrow_down); this.AutoScaleMode = AutoScaleMode.None; this.StartPosition = FormStartPosition.WindowsDefaultLocation; this.ClientSize = new System.Drawing.Size(300, 580); //this.Visible = false; + treeView1.IconDatabase = iconDatabase; treeView1.RootContextMenu = rootContextMenu; treeView1.FolderContextMenu = folderContextMenu; treeView1.PageContextMenu = pageContextMenu; @@ -76,7 +76,6 @@ namespace FizzyLauncher //ThreadControl.SetVisible(this, false); await LoadAppSession(jsonfigFilename); - //await InitialiseIconDB(iconDBFilename); InvalidateAppSession(); diff --git a/Windows/Forms/BookmarkTreeView.cs b/Windows/Forms/BookmarkTreeView.cs index 2412fdb..b4fcf69 100644 --- a/Windows/Forms/BookmarkTreeView.cs +++ b/Windows/Forms/BookmarkTreeView.cs @@ -207,10 +207,8 @@ namespace RyzStudio.Windows.Forms protected const string DEFAULT_NEW_FOLDER_NAME = "New Folder"; protected const string PATH_SEPARATOR = "\n"; - public event EventHandler OnNodeChanged = null; - //protected IconDatabase iconDatabase = null; protected TreeNode draggingNode = null; protected bool hasChanged = false; @@ -616,9 +614,9 @@ namespace RyzStudio.Windows.Forms // 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); tn.Tag = item; @@ -629,22 +627,23 @@ namespace RyzStudio.Windows.Forms ThreadControl.Add(tn2, tn); this.HasChanged = true; + + return tn; } public TreeNode AddItem(TreeNode treeNode, BookmarkItem item) { - if (treeNode == null) - { - return null; - } + if (treeNode == null) return null; + if (item == null) return null; - int iconIndex = addIcon(item); + int iconIndex = FindIcon(item); TreeNode tn = new TreeNode(item.SiteName, iconIndex, iconIndex); tn.Tag = item; tn.ToolTipText = item.ToString(); - treeNode.Nodes.Add(tn); + //treeNode.Nodes.Add(tn); + ThreadControl.Add(treeNode, tn); this.HasChanged = true; @@ -859,14 +858,12 @@ namespace RyzStudio.Windows.Forms 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) - { - return; - } + if (treeNode == null) return; + if (item == null) return; - int iconIndex = addIcon(item); + int iconIndex = FindIcon(item); treeNode.Text = item.SiteName; treeNode.ImageIndex = iconIndex; @@ -878,9 +875,30 @@ namespace RyzStudio.Windows.Forms } - protected int addIcon(BookmarkItem viewModel) + protected int FindIcon(BookmarkItem item) { - return (int)IconSet.Default; + 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; + } + + 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()); @@ -976,27 +994,6 @@ namespace RyzStudio.Windows.Forms 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 rs, TreeNode node) { foreach (TreeNode tn in node.Nodes)