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.Windows.Forms; using static RyzStudio.Windows.Forms.BookmarkTreeView; namespace bzit.bomg { public partial class MainForm : Form { public enum AppMode { Clear = 0, Open, New } protected AppMode appMode = AppMode.Clear; protected string sessionFilename = null; //protected SessionFileFormat sessionFileFormat = 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(); } // toolbar viewHelpHelpMenuItem.Enabled = File.Exists(Path.ChangeExtension(Application.ExecutablePath, ".chm")); //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(); //} //sessionFileFormat = new SessionFileFormat(this); 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) { this.ApplicationMode = AppMode.Clear; Application.Exit(); return; } if (!treeView1.HasChanged) { this.ApplicationMode = AppMode.Clear; Application.Exit(); return; } //if (this.ApplicationMode == AppMode.Open) //{ // if (string.IsNullOrEmpty(sessionFilename)) // { // this.ApplicationMode = AppMode.Clear; // Application.Exit(); // return; // } // if (MessageBox.Show("Save changes to open bookmarks.", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) // { // this.ApplicationMode = AppMode.Clear; // Application.Exit(); // return; // } // while (true) // { // bool rv = sessionFileFormat.saveToRyz(sessionFilename); // if (!rv) // { // switch (MessageBox.Show("There was a problem saving bookmarks. Retry?", "Retry?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)) // { // case DialogResult.Yes: // break; // case DialogResult.No: // this.ApplicationMode = AppMode.Clear; // Application.Exit(); // break; // case DialogResult.Cancel: // e.Cancel = true; // break; // default: break; // } // } // else // { // this.ApplicationMode = AppMode.Clear; // Application.Exit(); // break; // } // } //} //else if (this.ApplicationMode == AppMode.New) //{ // if (MessageBox.Show("Save changes to open bookmarks.", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) // { // bool rt = SaveBookmarkFile(); // if (rt) // { // this.ApplicationMode = AppMode.Clear; // Application.Exit(); // return; // } // } // else // { // this.ApplicationMode = AppMode.Clear; // Application.Exit(); // return; // } //} } //#region public properties public AppMode ApplicationMode { get { return appMode; } set { appMode = value; switch (value) { case AppMode.Clear: this.Text = Properties.Resources.app_name; statusBarPanel2.Text = string.Empty; treeView1.Clear(); sessionFilename = null; closeFileMenuItem.Enabled = false; saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false; saveAsFileMenuItem.Enabled = false; updateIconsToolMenuItem.Enabled = false; break; case AppMode.Open: closeFileMenuItem.Enabled = true; saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false; saveAsFileMenuItem.Enabled = true; updateIconsToolMenuItem.Enabled = true; break; case AppMode.New: this.Text = Properties.Resources.app_name; statusBarPanel2.Text = string.Empty; sessionFilename = null; closeFileMenuItem.Enabled = true; saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false; saveAsFileMenuItem.Enabled = true; updateIconsToolMenuItem.Enabled = true; break; } } } //[DefaultValue(null)] //public IconDatabase IconDatabase { get; set; } //#endregion #region Toolbar #1 /// /// File New /// /// /// private void newMenuItem_Click(object sender, EventArgs e) { NewSessionForm newSessionForm = new NewSessionForm(this); newSessionForm.ShowDialog(); } /// /// File Open /// /// /// private void openMenuItem_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { sessionFilename = openFileDialog.FileName; OpenBookmarkFile(sessionFilename); } } /** * File -> Close */ private void closeMenuItem_Click(object sender, EventArgs e) { //if (ApplicationMode == AppMode.Clear) //{ // return; //} //if (!treeView1.HasChanged) //{ // ApplicationMode = AppMode.Clear; // return; //} //if (ApplicationMode == AppMode.Open) //{ // if (string.IsNullOrEmpty(sessionFilename)) // { // ApplicationMode = AppMode.Clear; // return; // } // if (MessageBox.Show("Save changes to open bookmarks.", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) // { // ApplicationMode = AppMode.Clear; // return; // } // bool rv = sessionFileFormat.saveToRyz(sessionFilename); // if (!rv) // { // if (MessageBox.Show("There was a problem saving bookmarks. Bookmarks are not saved", "Not Saved", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) return; // } // ApplicationMode = AppMode.Clear; //} //else if (ApplicationMode == AppMode.New) //{ // if (MessageBox.Show("Save changes to open bookmarks.", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) // { // ApplicationMode = AppMode.Clear; // return; // } // bool rt = SaveBookmarkFile(); // if (rt) // { // ApplicationMode = AppMode.Clear; // } //} } /** * File -> Save */ private void importSnapshotToolStripMenuItem_Click(object sender, EventArgs e) { //if (string.IsNullOrEmpty(sessionFilename)) //{ // return; //} //bool rv = sessionFileFormat.saveToRyz(sessionFilename); //if (rv) //{ // treeView1.HasChanged = false; //} } /** * File -> Save As */ private void exportSnapshotToolStripMenuItem_Click(object sender, EventArgs e) { //if (SaveBookmarkFile()) //{ // ApplicationMode = AppMode.Open; // treeView1.HasChanged = false; //} } /** * File -> Export */ private void toolStripMenuItem7_Click(object sender, EventArgs e) { } /** * File -> Exit */ private void exitMenuItem_Click(object sender, EventArgs e) => this.Close(); /** * Find */ private void toolStripButton5_Click(object sender, EventArgs e) { FindForm frm = new FindForm(this); frm.Show(); } /** * View -> Expand All */ private void expandAllToolStripMenuItem_Click(object sender, EventArgs e) { if (treeView1.SelectedNode == null) { treeView1.ExpandAll(); } else { treeView1.SelectedNode.ExpandAll(); } } /** * View -> Collapse All */ private void collapseAllToolStripMenuItem_Click(object sender, EventArgs e) { if (treeView1.SelectedNode == null) { treeView1.CollapseAll(); } else { treeView1.SelectedNode.Collapse(false); } } /** * 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(); } /** * Tools -> Options */ private void optionsToolStripMenuItem1_Click(object sender, EventArgs e) { //OptionsForm frm = new OptionsForm(this); //frm.ShowDialog(); } /** * Help -> Documentation */ public void documentationToolStripMenuItem_Click(object sender, EventArgs e) { string chm = Path.ChangeExtension(Application.ExecutablePath, ".chm"); if (!File.Exists(chm)) { return; } try { System.Diagnostics.Process.Start(chm); } catch { // do nothing } } /** * Help -> About */ private void aboutToolStripMenuItem1_Click(object sender, EventArgs e) { MessageBox.Show("Name: " + Properties.Resources.app_name + Environment.NewLine + "Version: " + Properties.Resources.app_version + Environment.NewLine + "Author: " + Properties.Resources.app_author, "About", MessageBoxButtons.OK, MessageBoxIcon.Information); } /** * ? -> Always-On-Top */ private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e) => this.TopMost = !this.TopMost; #endregion protected void OpenBookmarkFile(string filename) { //treeView1.HasChanged = false; //if (RyzStudio.IO.SharpZipLib.IsZipEncrypted(filename)) //{ // PasswordForm oPassword = new PasswordForm(this); // sessionFileFormat.passkey = oPassword.ShowDialog(); // if (sessionFileFormat.passkey.Equals("")) // { // return; // } //} //else //{ // sessionFileFormat.passkey = ""; //} //bool rv = sessionFileFormat.loadFromRyz(filename); //if (rv) //{ // this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Properties.Resources.app_name; // this.ApplicationMode = AppMode.Open; // treeView1.HasChanged = false; //} } protected bool SaveBookmarkFile() { bool rv = false; //if (saveFileDialog.ShowDialog() == DialogResult.OK) //{ // switch (saveFileDialog.FilterIndex) // { // case 1: // sessionFileFormat.passkey = ""; // rv = sessionFileFormat.saveToRyz(saveFileDialog.FileName); // break; // case 2: // PasswordForm oPassword = new PasswordForm(this); // sessionFileFormat.passkey = oPassword.ShowDialog(); // rv = sessionFileFormat.saveToRyz(saveFileDialog.FileName); // if (rv) // { // treeView1.HasChanged = false; // } // break; // default: break; // } //} //if (rv) //{ // ApplicationMode = AppMode.Open; // sessionFilename = saveFileDialog.FileName; // this.Text = Path.GetFileNameWithoutExtension(saveFileDialog.FileName) + " - " + Properties.Resources.app_name; //} return rv; } protected void saveBookmarkFile(string filename) { 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); // make readable MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(rs))); ms.Position = 0; // write new document xml 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; } 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 private void toolStripButton1_Click(object sender, EventArgs e) { List rs = treeView1.GetBookmarkList(); string filename = @"N:\test.jsnx"; saveBookmarkFile(filename); } } }