using bzit.bomg.Models; using ICSharpCode.SharpZipLib.Zip; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Text; using System.Windows.Forms; using System.Xml; using static RyzStudio.Windows.Forms.BookmarkTreeView; using TextBoxForm = RyzStudio.Windows.ThemedForms.TextBoxForm; using Resources = bzit.bomg.Properties.Resources; namespace bzit.bomg { public partial class MainForm : Form { public enum AppMode { Clear = 0, Open, New } protected AppMode appMode = AppMode.Clear; protected string sessionFilename = null; public MainForm() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); bool rv = false; string iconDBFilename = Path.ChangeExtension(Application.ExecutablePath, ".db"); string errorMessage; rv = treeView1.InitialiseIconDatabase(out errorMessage, iconDBFilename); if (!rv) { MessageBox.Show("Can not find icon database. [" + errorMessage + "]"); this.Close(); } this.ApplicationMode = AppMode.Clear; sessionFilename = null; //treeView1.OnNodeCountUpdate = delegate (ulong v) { // statusBarPanel2.Text = v.ToString(); //}; //treeView1.OnChanged = delegate (object sender, EventArgs ev) { // if (this.ApplicationMode == AppMode.New) // { // saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = false; // } // else // { // saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = treeView1.HasChanged; // } //}; } protected override void OnShown(EventArgs e) { base.OnShown(e); this.Height = Screen.PrimaryScreen.WorkingArea.Height; this.Location = Screen.PrimaryScreen.WorkingArea.Location; // command line //string[] szr = Environment.GetCommandLineArgs(); //int i = 0; //while (true) //{ // if (i > (szr.Length - 1)) // { // break; // } // switch (szr[i].Trim().ToLower()) // { // case "-o": // case "-open": // if ((i + 1) > (szr.Length - 1)) // { // break; // } // sessionFilename = szr[(i + 1)]; // if (string.IsNullOrEmpty(sessionFilename)) // { // sessionFilename = null; // break; // } // if (!File.Exists(sessionFilename)) // { // sessionFilename = null; // break; // } // OpenBookmarkFile(sessionFilename); // i++; // break; // } // i++; //} } protected override void OnFormClosing(FormClosingEventArgs e) { if (this.ApplicationMode == AppMode.Clear) { // do later } else { if (treeView1.HasChanged) { if (this.ApplicationMode == AppMode.New) { DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { bool hasSaved = saveAsBookmarkFile(); if (!hasSaved) { e.Cancel = true; return; } } else if (response == DialogResult.No) { // do later } else if (response == DialogResult.Cancel) { e.Cancel = true; return; } } else if (this.ApplicationMode == AppMode.Open) { DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { saveBookmarkFile_ForJSNX(sessionFilename); } else if (response == DialogResult.No) { // do later } else if (response == DialogResult.Cancel) { e.Cancel = true; return; } } } else { // do later } } treeView1.CloseIconDatabase(); treeView1.Clear(); this.ApplicationMode = AppMode.Clear; sessionFilename = null; Application.Exit(); } public AppMode ApplicationMode { get => appMode; set { appMode = value; switch (value) { case AppMode.Clear: this.Text = Properties.Resources.app_name; closeFileMenuItem.Enabled = false; saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false; saveAsFileMenuItem.Enabled = false; break; case AppMode.Open: closeFileMenuItem.Enabled = true; saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = true; saveAsFileMenuItem.Enabled = true; break; case AppMode.New: this.Text = Properties.Resources.app_name; closeFileMenuItem.Enabled = true; saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = true; saveAsFileMenuItem.Enabled = true; break; } } } #region Toolbar #1 private void fileNewMenuItem_Click(object sender, EventArgs e) { if (this.ApplicationMode == AppMode.Clear) { // do later } else { if (treeView1.HasChanged) { if (this.ApplicationMode == AppMode.New) { DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { bool hasSaved = saveAsBookmarkFile(); if (!hasSaved) { return; } } else if (response == DialogResult.No) { // do later } else if (response == DialogResult.Cancel) { return; } } else if (this.ApplicationMode == AppMode.Open) { DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { saveBookmarkFile_ForJSNX(sessionFilename); } else if (response == DialogResult.No) { // do later } else if (response == DialogResult.Cancel) { return; } } } else { // do later } } TextBoxForm newSessionForm = new TextBoxForm("New Session", "Name"); string rv = newSessionForm.ShowDialog(); if (string.IsNullOrWhiteSpace(rv)) { return; } treeView1.Clear(rv); this.ApplicationMode = AppMode.New; sessionFilename = null; } private void fileOpenMenuItem_Click(object sender, EventArgs e) { if (this.ApplicationMode == AppMode.Clear) { // do later } else { if (treeView1.HasChanged) { if (this.ApplicationMode == AppMode.New) { DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { bool hasSaved = saveAsBookmarkFile(); if (!hasSaved) { return; } } else if (response == DialogResult.No) { // do later } else if (response == DialogResult.Cancel) { return; } } else if (this.ApplicationMode == AppMode.Open) { DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { saveBookmarkFile_ForJSNX(sessionFilename); } else if (response == DialogResult.No) { // do later } else if (response == DialogResult.Cancel) { return; } } } else { // do later } } if (openFileDialog.ShowDialog() == DialogResult.OK) { loadBookmarkFile(openFileDialog.FileName, openFileDialog.FilterIndex); } } private void fileCloseMenuItem_Click(object sender, EventArgs e) { if (this.ApplicationMode == AppMode.Clear) { // do later } else { if (treeView1.HasChanged) { if (this.ApplicationMode == AppMode.New) { DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { bool hasSaved = saveAsBookmarkFile(); if (!hasSaved) { return; } } else if (response == DialogResult.No) { // do later } else if (response == DialogResult.Cancel) { return; } } else if (this.ApplicationMode == AppMode.Open) { DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { saveBookmarkFile_ForJSNX(sessionFilename); } else if (response == DialogResult.No) { // do later } else if (response == DialogResult.Cancel) { return; } } } else { // do later } } treeView1.Clear(); this.ApplicationMode = AppMode.Clear; sessionFilename = null; } private void fileSaveMenuItem_Click(object sender, EventArgs e) { if (this.ApplicationMode == AppMode.New) { DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (response == DialogResult.Yes) { bool hasSaved = saveAsBookmarkFile(); if (!hasSaved) { return; } } else if (response == DialogResult.No) { return; } else if (response == DialogResult.Cancel) { return; } } else if (this.ApplicationMode == AppMode.Open) { saveBookmarkFile_ForJSNX(sessionFilename); } } private void fileSaveAsMenuItem_Click(object sender, EventArgs e) { if (this.ApplicationMode == AppMode.Clear) { return; } bool hasSaved = saveAsBookmarkFile(); if (!hasSaved) { return; } } private void fileExitMenuItem_Click(object sender, EventArgs e) { this.Close(); } /** * Find */ private void toolStripButton5_Click(object sender, EventArgs e) { FindForm frm = new FindForm(this); frm.Show(); } private void viewExpandAllMenuItem_Click(object sender, EventArgs e) { if (treeView1.SelectedNode == null) { treeView1.ExpandAll(); } else { treeView1.SelectedNode.ExpandAll(); } } private void viewCollapseAllMenuItem_Click(object sender, EventArgs e) { if (treeView1.SelectedNode == null) { treeView1.CollapseAll(); } else { treeView1.SelectedNode.Collapse(false); } } private void viewAlwaysOnTopMenuItem_Click(object sender, EventArgs e) { this.TopMost = !this.TopMost; alwaysOnTopToolStripMenuItem1.Checked = this.TopMost; } /** * Tools -> Refresh Icons */ //private void toolStripMenuItem1_Click(object sender, EventArgs e) //{ // if (string.IsNullOrEmpty(sessionFilename)) // { // return; // } // if (this.ApplicationMode == AppMode.Clear) // { // return; // } // UpdateIconForm frm = new UpdateIconForm(this); // frm.ShowDialog(); //} private void toolsOptionsMenuItem_Click(object sender, EventArgs e) { //OptionsForm frm = new OptionsForm(this); //frm.ShowDialog(); } private void helpViewHelpMenuItem_Click(object sender, EventArgs e) { try { System.Diagnostics.Process.Start(Resources.url_help); } catch { // do nothing } } private void helpAboutMenuItem_Click(object sender, EventArgs e) { try { System.Diagnostics.Process.Start(Resources.url_about); } catch { // do nothing } } #endregion protected void loadBookmarkFile(string filename, int filterIndex) { switch (filterIndex) { case 1: if (RyzStudio.IO.SharpZipLib.IsZipEncrypted(filename)) { TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true); string password = string.Empty; while (true) { password = passwordForm.ShowDialog(); if (string.IsNullOrWhiteSpace(password)) { break; } if (RyzStudio.IO.SharpZipLib.TestZipEncrypted(filename, password)) { sessionFilename = filename; //this.ApplicationMode = AppMode.Open; loadBookmarkFile_ForJSNX(filename, password); break; } } } else { sessionFilename = filename; //this.ApplicationMode = AppMode.Open; loadBookmarkFile_ForJSNX(filename); } break; case 2: if (RyzStudio.IO.SharpZipLib.IsZipEncrypted(filename)) { TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true); string password = string.Empty; while (true) { password = passwordForm.ShowDialog(); if (string.IsNullOrWhiteSpace(password)) { break; } if (RyzStudio.IO.SharpZipLib.TestZipEncrypted(filename, password)) { loadBookmarkFile_ForRYZ(filename, password); sessionFilename = null; this.ApplicationMode = AppMode.New; break; } } } else { loadBookmarkFile_ForRYZ(filename); sessionFilename = null; this.ApplicationMode = AppMode.New; } break; default: break; } } protected void loadBookmarkFile_ForJSNX(string filename, string password = "") { int size = 2048; byte[] buffer = new byte[size]; int bufferSize = 0; List rs = null; ZipEntry readEntry = null; ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename)); readStream.Password = password; while (true) { readEntry = readStream.GetNextEntry(); if (readEntry == null) { break; } if (string.IsNullOrWhiteSpace(readEntry.Name)) { continue; } if (!readEntry.IsFile) { continue; } if (!readEntry.Name.Equals("bookmarks.json")) { continue; } MemoryStream ms = new MemoryStream(); buffer = new byte[size]; bufferSize = 0; do { bufferSize = readStream.Read(buffer, 0, buffer.Length); ms.Write(buffer, 0, bufferSize); } while (bufferSize > 0); ms.Position = 0; StreamReader sr = new StreamReader(ms); rs = JsonConvert.DeserializeObject>(sr.ReadToEnd()); break; } readStream.Flush(); readStream.Close(); readStream.Dispose(); readStream = null; treeView1.AddItem(rs); treeView1.HasChanged = false; this.ApplicationMode = AppMode.Open; setAppTitle(Path.GetFileNameWithoutExtension(filename)); } protected void loadBookmarkFile_ForRYZ(string filename, string password = "") { int size = 2048; byte[] buffer = new byte[size]; int bufferSize = 0; List rs = new List(); ZipEntry readEntry = null; ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename)); readStream.Password = password; while (true) { readEntry = readStream.GetNextEntry(); if (readEntry == null) { break; } if (string.IsNullOrWhiteSpace(readEntry.Name)) { continue; } if (!readEntry.IsFile) { continue; } if (!readEntry.Name.Equals("bookmarks.xml")) { continue; } MemoryStream ms = new MemoryStream(); buffer = new byte[size]; bufferSize = 0; do { bufferSize = readStream.Read(buffer, 0, buffer.Length); ms.Write(buffer, 0, bufferSize); } while (bufferSize > 0); ms.Position = 0; // read stream StreamReader sr = new StreamReader(ms); // load xml XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(sr.ReadToEnd()); // parse XmlNode xnl = xmlDocument.SelectSingleNode("bomg/b/g"); if (xnl == null) { break; } string rootName = (xnl.Attributes["name"] == null) ? "Bookmarks" : (string.IsNullOrWhiteSpace(xnl.Attributes["name"].InnerText) ? "Bookmarks" : xnl.Attributes["name"].InnerText.Trim()); foreach (XmlNode xn in xnl.ChildNodes) { BookmarkItemViewModel viewModel = new BookmarkItemViewModel(); foreach (XmlNode xn2 in xn.ChildNodes) { switch (xn2.LocalName) { case "name": viewModel.SiteName = xn2.InnerText?.Trim(); break; case "address": viewModel.SiteAddress = xn2.InnerText?.Trim(); break; case "description": viewModel.SiteDescription = xn2.InnerText?.Trim(); break; //case "created": // bi.Created = xn2.InnerText?.Trim(); // break; default: break; } } // fix fullpath and name if (viewModel.SiteName.Contains("|")) { StringBuilder sb = new StringBuilder(); sb.Append("\\"); sb.Append(System.Web.HttpUtility.UrlEncode(rootName)); sb.Append("\\"); string[] pathParts = viewModel.SiteName.Split('|'); for (int i = 0; i < (pathParts.Length - 1); i++) { sb.Append(pathParts[i]); //sb.Append(System.Web.HttpUtility.UrlDecode(pathParts[i])); sb.Append("\\"); } viewModel.SiteName = System.Web.HttpUtility.UrlDecode(pathParts[(pathParts.Length - 1)] ?? string.Empty); viewModel.TreeviewPath = sb.ToString(); } else { viewModel.SiteName = System.Web.HttpUtility.UrlDecode(viewModel.SiteName); viewModel.TreeviewPath = string.Format("\\{0}\\", System.Web.HttpUtility.UrlEncode(rootName)); } rs.Add(viewModel); } break; } readStream.Flush(); readStream.Close(); readStream.Dispose(); readStream = null; treeView1.AddItem(rs); treeView1.HasChanged = false; //this.ApplicationMode = AppMode.New; setAppTitle(Path.GetFileNameWithoutExtension(filename)); } protected bool saveAsBookmarkFile() { if (saveFileDialog.ShowDialog() == DialogResult.OK) { switch (saveFileDialog.FilterIndex) { case 1: saveBookmarkFile_ForJSNX(saveFileDialog.FileName); setAppTitle(Path.GetFileNameWithoutExtension(saveFileDialog.FileName)); sessionFilename = saveFileDialog.FileName; this.ApplicationMode = AppMode.Open; treeView1.HasChanged = false; break; case 2: TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true); string password = passwordForm.ShowDialog(); saveBookmarkFile_ForJSNX(saveFileDialog.FileName, password ?? string.Empty); setAppTitle(Path.GetFileNameWithoutExtension(saveFileDialog.FileName)); sessionFilename = saveFileDialog.FileName; this.ApplicationMode = AppMode.Open; treeView1.HasChanged = false; break; default: break; } return true; } else { return false; } } protected void saveBookmarkFile_ForJSNX(string filename, string password = "") { List rs = treeView1.GetBookmarkList(); int size = 2048; byte[] buffer = new byte[size]; int bufferSize = 0; ZipOutputStream zipStream = new ZipOutputStream(File.Create(filename)); zipStream.SetLevel(9); zipStream.Password = password; // stream MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(rs))); ms.Position = 0; // write file entry zipStream.PutNextEntry(new ZipEntry("bookmarks.json")); buffer = new byte[size]; bufferSize = 0; do { bufferSize = ms.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, bufferSize); } while (bufferSize > 0); ms.Flush(); ms.Close(); ms.Dispose(); ms = null; zipStream.Finish(); zipStream.Flush(); zipStream.Close(); zipStream.Dispose(); zipStream = null; treeView1.HasChanged = false; } protected void setAppTitle(string title) { this.Text = title + " - " + Properties.Resources.app_name; } protected void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Button != MouseButtons.Right) { return; } switch (treeView1.SNode.GetNodeType()) { case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Root: rootTreeNodeMenu.Show(Cursor.Position); break; case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Folder: folderTreeNodeMenu.Show(Cursor.Position); break; case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Page: pageTreeNodeMenu.Show(Cursor.Position); break; default: break; } } protected void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) => openBookmark(e.Node); protected void treeView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { TreeNode tn = treeView1.SelectedNode; if (tn == null) { return; } NodeType nodeType = treeView1.SNode.GetNodeType(); switch (e.KeyCode) { case Keys.Apps: treeView1_NodeMouseClick(sender, new TreeNodeMouseClickEventArgs(tn, MouseButtons.Right, 1, tn.Bounds.X, tn.Bounds.Y)); return; case Keys.Enter: openBookmark(tn); break; case Keys.Insert: if (e.Modifiers != Keys.Shift) { if ((nodeType == NodeType.Root) || (nodeType == NodeType.Folder)) { (new BookmarkEditForm(treeView1)).ShowDialog(); } else if (nodeType == NodeType.Page) { treeView1.SelectedNode = tn.Parent; (new BookmarkEditForm(treeView1)).ShowDialog(); } } break; default: break; } } protected void openBookmark(TreeNode node) { if (treeView1.GetNodeType(node) != RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Page) { return; } BookmarkItemViewModel viewModel = (BookmarkItemViewModel)node.Tag; if (viewModel == null) { return; } if (string.IsNullOrWhiteSpace(viewModel.SiteAddress)) { return; } try { System.Diagnostics.Process.Start(viewModel.SiteAddress); } catch { // do nothing } //if (node == null) //{ // return; //} //if (node.Tag == null) //{ // return; //} //if (!(node.Tag is BookmarkItem)) //{ // return; //} //BookmarkItem item = (BookmarkItem)node.Tag; //if (item == null) //{ // return; //} //if (string.IsNullOrEmpty(item.SiteAddress)) //{ // return; //} //int bookmarkAction; //if (!int.TryParse(this.IconDatabase.GetConfig("core.bookmark.action", string.Empty), out bookmarkAction)) //{ // bookmarkAction = 0; //} //string bookmarkCustom1 = this.IconDatabase.GetConfig("core.bookmark.customcommand1", string.Empty).Trim(); //string bookmarkCustom2 = this.IconDatabase.GetConfig("core.bookmark.customcommand2", string.Empty).Trim(); //switch (bookmarkAction) //{ // case 1: // if (string.IsNullOrEmpty(bookmarkCustom1)) // { // return; // } // bookmarkCustom1 = bookmarkCustom1.Replace("%1", item.SiteAddress); // bookmarkCustom2 = bookmarkCustom2.Replace("%1", item.SiteAddress); // try // { // System.Diagnostics.Process.Start(bookmarkCustom1, bookmarkCustom2); // } // catch // { // // do nothing // } // break; // default: // try // { // System.Diagnostics.Process.Start(item.SiteAddress); // } // catch // { // // do nothing // } // break; //} } #region root context menu private void addPageContextMenu_Click(object sender, EventArgs e) => (new BookmarkEditForm(treeView1)).ShowDialog(); private void addFolderContextMenu_Click(object sender, EventArgs e) => treeView1.SelectedNode = treeView1.SNode.AddFolder(); private void editContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.Edit(); private void sortContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.Sort(); #endregion #region folder context menu private void addPageContextMenu2_Click(object sender, EventArgs e) => (new BookmarkEditForm(treeView1)).ShowDialog(); private void addFolderContextMenu2_Click(object sender, EventArgs e) => treeView1.SelectedNode = treeView1.SNode.AddFolder(); private void openAllContextMenu_Click(object sender, EventArgs e) { if (treeView1.SelectedNode == null) { return; } if (treeView1.SelectedNode.Nodes.Count <= 0) { return; } foreach (TreeNode item in treeView1.SelectedNode.Nodes) { openBookmark(item); } } private void editContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.Edit(); private void deleteContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.Delete(); private void sortContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.Sort(); private void moveUpContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.MoveUp(); private void moveDownContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.MoveDown(); #endregion #region page context menu private void openContextMenu_Click(object sender, EventArgs e) => openBookmark(treeView1.SelectedNode); private void editContextMenu3_Click(object sender, EventArgs e) => (new BookmarkEditForm(treeView1)).ShowDialog(); private void deleteContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.Delete(); private void moveUpContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.MoveUp(); private void moveDownContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.MoveDown(); #endregion } }