diff --git a/LoadBookmarksForm.Designer.cs b/LoadBookmarksForm.Designer.cs new file mode 100644 index 0000000..abf3896 --- /dev/null +++ b/LoadBookmarksForm.Designer.cs @@ -0,0 +1,156 @@ +namespace bzit.bomg +{ + partial class LoadBookmarksForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.label1 = new System.Windows.Forms.Label(); + this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.label5 = new System.Windows.Forms.Label(); + this.progressBar1 = new RyzStudio.Windows.ThemedForms.ProgressBar(); + this.horizontalSeparator1 = new RyzStudio.Windows.Forms.HorizontalSeparator(); + this.button2 = new RyzStudio.Windows.ThemedForms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.SuspendLayout(); + // + // label1 + // + this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 25); + this.label1.Margin = new System.Windows.Forms.Padding(3); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(63, 13); + this.label1.TabIndex = 0; + this.label1.Text = "Bookmarks"; + // + // pictureBox1 + // + this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.pictureBox1.BackColor = System.Drawing.Color.Transparent; + this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; + this.pictureBox1.ErrorImage = null; + this.pictureBox1.InitialImage = null; + this.pictureBox1.Location = new System.Drawing.Point(226, 77); + this.pictureBox1.Name = "pictureBox1"; + this.pictureBox1.Size = new System.Drawing.Size(32, 32); + this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; + this.pictureBox1.TabIndex = 60; + this.pictureBox1.TabStop = false; + // + // label5 + // + this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.label5.ForeColor = System.Drawing.Color.OrangeRed; + this.label5.Location = new System.Drawing.Point(9, 77); + this.label5.Margin = new System.Windows.Forms.Padding(3); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(211, 32); + this.label5.TabIndex = 64; + this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + // + // progressBar1 + // + this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.progressBar1.BackColor = System.Drawing.Color.Transparent; + this.progressBar1.BarColour = System.Drawing.Color.FromArgb(((int)(((byte)(158)))), ((int)(((byte)(225)))), ((int)(((byte)(249))))); + this.progressBar1.BarTextColour = System.Drawing.SystemColors.ControlText; + this.progressBar1.Location = new System.Drawing.Point(96, 15); + this.progressBar1.Margin = new System.Windows.Forms.Padding(3, 10, 3, 10); + this.progressBar1.Maximum = 0; + this.progressBar1.Minimum = 0; + this.progressBar1.Name = "progressBar1"; + this.progressBar1.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3); + this.progressBar1.Size = new System.Drawing.Size(276, 32); + this.progressBar1.TabIndex = 66; + this.progressBar1.Value = 0; + // + // horizontalSeparator1 + // + this.horizontalSeparator1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.horizontalSeparator1.Location = new System.Drawing.Point(12, 62); + this.horizontalSeparator1.Margin = new System.Windows.Forms.Padding(3, 10, 3, 10); + this.horizontalSeparator1.MaximumSize = new System.Drawing.Size(4920, 2); + this.horizontalSeparator1.Name = "horizontalSeparator1"; + this.horizontalSeparator1.Size = new System.Drawing.Size(360, 2); + this.horizontalSeparator1.TabIndex = 65; + // + // button2 + // + this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.button2.BackColor = System.Drawing.Color.Transparent; + this.button2.DefaultImage = null; + this.button2.DownImage = null; + this.button2.LabelText = "&Close"; + this.button2.Location = new System.Drawing.Point(264, 77); + this.button2.Name = "button2"; + this.button2.OverImage = null; + this.button2.Padding = new System.Windows.Forms.Padding(4, 4, 3, 3); + this.button2.Size = new System.Drawing.Size(108, 32); + this.button2.TabIndex = 4; + this.button2.Click += new System.EventHandler(this.button2_Click); + // + // LoadBookmarksForm + // + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; + this.BackColor = System.Drawing.Color.White; + this.ClientSize = new System.Drawing.Size(384, 121); + this.Controls.Add(this.progressBar1); + this.Controls.Add(this.horizontalSeparator1); + this.Controls.Add(this.label5); + this.Controls.Add(this.button2); + this.Controls.Add(this.pictureBox1); + this.Controls.Add(this.label1); + this.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "LoadBookmarksForm"; + this.ShowIcon = false; + this.ShowInTaskbar = false; + this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; + this.Text = "Load Bookmarks"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label label1; + private System.Windows.Forms.PictureBox pictureBox1; + private RyzStudio.Windows.ThemedForms.Button button2; + private System.Windows.Forms.Label label5; + private RyzStudio.Windows.Forms.HorizontalSeparator horizontalSeparator1; + private RyzStudio.Windows.ThemedForms.ProgressBar progressBar1; + } +} \ No newline at end of file diff --git a/LoadBookmarksForm.cs b/LoadBookmarksForm.cs new file mode 100644 index 0000000..8f75bdc --- /dev/null +++ b/LoadBookmarksForm.cs @@ -0,0 +1,413 @@ +using bzit.bomg.Models; +using ICSharpCode.SharpZipLib.Zip; +using Newtonsoft.Json; +using RyzStudio.Windows.Forms; +using RyzStudio.Windows.ThemedForms; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Threading.Tasks; +using System.Windows.Forms; +using AppMode = bzit.bomg.MainForm.AppMode; +using Form = System.Windows.Forms.Form; +using Resources = bzit.bomg.Properties.Resources; + +namespace bzit.bomg +{ + public partial class LoadBookmarksForm : Form + { + protected bool isBusy = false; + protected bool requestCancel = false; + + protected MainForm parentForm = null; + protected string sessionFilename = null; + + public LoadBookmarksForm(MainForm mainForm, string filename) : base() + { + InitializeComponent(); + + parentForm = mainForm; + sessionFilename = filename; + + this.StartPosition = FormStartPosition.WindowsDefaultLocation; + } + + protected override void OnClosing(CancelEventArgs e) + { + base.OnClosing(e); + + if (this.IsBusy) + { + e.Cancel = true; + } + } + + protected override void OnShown(EventArgs e) + { + base.OnShown(e); + + loadFile(sessionFilename); + } + + protected bool IsBusy + { + get => isBusy; + set + { + isBusy = value; + parentForm.IsBusy = value; + + //ThreadControl.SetImage(pictureBox1, (value) ? Resources.aniZomq2x32 : null); + //ThreadControl.SetEnable(button2, !value); + } + } + + private async void button1_Click(object sender, EventArgs e) + { + if (this.IsBusy) + { + //requestCancel = true; + //ThreadControl.SetText(button1, "&Cancelling..."); + return; + } + + this.IsBusy = true; + + await Task.Run(() => + { + //ThreadControl.SetText(button1, "&Updating..."); + + //if (requestCancel) + //{ + // this.IsBusy = false; + + // ThreadControl.SetText(button1, "&Update"); + + // requestCancel = false; + // return; + //} + + //List nodeList = parentForm.treeView1.GetBookmarkNodeList(); + + //ThreadControl.SetValue(progressBar1, 0, nodeList.Count); + + //for (int i = 0; i < nodeList.Count; i++) + //{ + // if (requestCancel) + // { + // this.IsBusy = false; + + // ThreadControl.SetText(button1, "&Update"); + + // requestCancel = false; + // return; + // } + + // ThreadControl.SetValue(progressBar1, (i + 1)); + + // BookmarkItemViewModel viewModel = (BookmarkItemViewModel)nodeList[i].Tag; + // BookmarkItemModel model = viewModel.ToModel(); + // bool rv = model.UpdateFavicon(); + // if (rv) + // { + // if (parentForm.treeView1.InvokeRequired) + // { + // parentForm.treeView1.Invoke(new MethodInvoker(() => + // { + // parentForm.treeView1.UpdateItem(nodeList[i], model.ToViewModel()); + // })); + // } + // else + // { + // parentForm.treeView1.UpdateItem(nodeList[i], model.ToViewModel()); + // } + // } + //} + + this.IsBusy = false; + + //ThreadControl.SetText(button1, "&Update"); + + //requestCancel = false; + }); + } + + private void button2_Click(object sender, EventArgs e) + { + if (this.IsBusy) + { + return; + } + + this.Close(); + } + + + protected void loadFile(string filename) + { + parentForm.ClearSession(); + + if (string.IsNullOrWhiteSpace(filename)) + { + return; + } + + string password = string.Empty; + + if (RyzStudio.IO.SharpZipLib.IsZipEncrypted(filename)) + { + TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true); + + while (true) + { + password = passwordForm.ShowDialog(); + if (string.IsNullOrWhiteSpace(password)) + { + break; + } + + if (RyzStudio.IO.SharpZipLib.TestZipEncrypted(filename, password)) + { + parentForm.SetSessionFile(filename, password); + break; + } + } + } + + this.IsBusy = true; + + string ext = Path.GetExtension(filename).Trim('.').ToLower(); + if (ext.Equals("jsnx")) + { + loadJSNXBookmarkFile(filename, password); + } + else if (ext.Equals("ryz")) + { + //##loadFileType = LoadFileType.Ryz; + } + else + { + // do nothing + } + + this.IsBusy = false; + parentForm.AllowSave = false; + } + + protected void loadJSNXBookmarkFile(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; + + parentForm.SetBookmarkItem(rs); + parentForm.ApplicationMode = AppMode.Open; + + //// RyzStudio.Windows.Forms.ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name); + } + + //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); + + // sessionFilename = sessionPassword = null; + // usePassword = !string.IsNullOrWhiteSpace(password); + + // if (this.InvokeRequired) + // { + // this.Invoke(new MethodInvoker(() => + // { + // this.ApplicationMode = AppMode.New; + // })); + // } + // else + // { + // this.ApplicationMode = AppMode.New; + // } + + // RyzStudio.Windows.Forms.ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name); + //} + + + + + + } +} \ No newline at end of file diff --git a/LoadBookmarksForm.resx b/LoadBookmarksForm.resx new file mode 100644 index 0000000..d58980a --- /dev/null +++ b/LoadBookmarksForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MainForm.cs b/MainForm.cs index d01f885..673726f 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -9,8 +9,9 @@ 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; +using TextBoxForm = RyzStudio.Windows.ThemedForms.TextBoxForm; +using ThreadControl = RyzStudio.Windows.Forms.ThreadControl; namespace bzit.bomg { @@ -30,7 +31,8 @@ namespace bzit.bomg Ryz } - protected BackgroundWorker loadFileThread = null; + //protected BackgroundWorker loadFileThread = null; + protected bool isBusy = false; protected LoadFileType loadFileType = LoadFileType.None; protected AppMode appMode = AppMode.Clear; @@ -42,14 +44,14 @@ namespace bzit.bomg { InitializeComponent(); - if (loadFileThread == null) - { - loadFileThread = new BackgroundWorker(); - loadFileThread.WorkerReportsProgress = loadFileThread.WorkerSupportsCancellation = true; - } + //if (loadFileThread == null) + //{ + // loadFileThread = new BackgroundWorker(); + // loadFileThread.WorkerReportsProgress = loadFileThread.WorkerSupportsCancellation = true; + //} - loadFileThread.DoWork += loadFileThread_DoWork; - loadFileThread.RunWorkerCompleted += loadFileThread_RunWorkerCompleted; + //loadFileThread.DoWork += loadFileThread_DoWork; + //loadFileThread.RunWorkerCompleted += loadFileThread_RunWorkerCompleted; treeView1.OnNodeChanged += treeView1_OnNodeChanged; } @@ -70,9 +72,7 @@ namespace bzit.bomg } this.ApplicationMode = AppMode.Clear; - - sessionFilename = sessionPassword = null; - usePassword = false; + this.ClearSession(); } protected override void OnShown(EventArgs e) @@ -105,15 +105,8 @@ namespace bzit.bomg if (string.IsNullOrWhiteSpace(openFilename)) break; if (!File.Exists(openFilename)) break; - string openExtension = Path.GetExtension(openFilename).Trim('.'); - if (openExtension.Equals("jsnx", StringComparison.CurrentCultureIgnoreCase)) - { - loadBookmarkFile(openFilename, 1); - } - else if (openExtension.Equals("ryz", StringComparison.CurrentCultureIgnoreCase)) - { - loadBookmarkFile(openFilename, 2); - } + LoadBookmarksForm loadFileForm = new LoadBookmarksForm(this, openFilename); + loadFileForm.ShowDialog(); i++; break; @@ -198,13 +191,14 @@ namespace bzit.bomg treeView1.CloseIconDatabase(); treeView1.Clear(); + this.ApplicationMode = AppMode.Clear; - sessionFilename = sessionPassword = null; + this.ClearSession(); Application.Exit(); } - protected AppMode ApplicationMode + public AppMode ApplicationMode { get => appMode; set @@ -214,46 +208,56 @@ namespace bzit.bomg switch (value) { case AppMode.Clear: - this.Text = Properties.Resources.app_name; + ThreadControl.SetText(this, Resources.app_name); - closeFileMenuItem.Enabled = false; - saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false; - saveAsFileMenuItem.Enabled = false; - findEditMenuItem.Enabled = false; - expandAllViewMenuItem.Enabled = collapseAllViewMenuItem.Enabled = false; - optionsToolMenuItem.Enabled = false; + ThreadControl.SetEnable(toolStrip1, closeFileMenuItem, false); + this.AllowSave = false; + ThreadControl.SetEnable(toolStrip1, saveAsFileMenuItem, false); + ThreadControl.SetEnable(toolStrip1, findEditMenuItem, false); + ThreadControl.SetEnable(toolStrip1, expandAllViewMenuItem, false); + ThreadControl.SetEnable(toolStrip1, collapseAllViewMenuItem, false); + ThreadControl.SetEnable(toolStrip1, optionsToolMenuItem, false); break; case AppMode.Open: - closeFileMenuItem.Enabled = true; - saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = true; - saveAsFileMenuItem.Enabled = true; - findEditMenuItem.Enabled = true; - expandAllViewMenuItem.Enabled = collapseAllViewMenuItem.Enabled = true; - optionsToolMenuItem.Enabled = true; + ThreadControl.SetEnable(toolStrip1, closeFileMenuItem, true); + this.AllowSave = true; + ThreadControl.SetEnable(toolStrip1, saveAsFileMenuItem, true); + ThreadControl.SetEnable(toolStrip1, findEditMenuItem, true); + ThreadControl.SetEnable(toolStrip1, expandAllViewMenuItem, true); + ThreadControl.SetEnable(toolStrip1, collapseAllViewMenuItem, true); + ThreadControl.SetEnable(toolStrip1, optionsToolMenuItem, true); break; case AppMode.New: - this.Text = Properties.Resources.app_name; + ThreadControl.SetText(this, Resources.app_name); - closeFileMenuItem.Enabled = true; - saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = true; - saveAsFileMenuItem.Enabled = true; - findEditMenuItem.Enabled = true; - expandAllViewMenuItem.Enabled = collapseAllViewMenuItem.Enabled = true; - optionsToolMenuItem.Enabled = true; + ThreadControl.SetEnable(toolStrip1, closeFileMenuItem, true); + this.AllowSave = true; + ThreadControl.SetEnable(toolStrip1, saveAsFileMenuItem, true); + ThreadControl.SetEnable(toolStrip1, findEditMenuItem, true); + ThreadControl.SetEnable(toolStrip1, expandAllViewMenuItem, true); + ThreadControl.SetEnable(toolStrip1, collapseAllViewMenuItem, true); + ThreadControl.SetEnable(toolStrip1, optionsToolMenuItem, true); break; } } } - protected bool IsBusy + public bool AllowSave { - get + get => (saveMenuBarItem.Enabled || saveFileMenuItem.Enabled); + set { - return loadFileThread.IsBusy; + ThreadControl.SetEnable(toolStrip1, saveMenuBarItem, value); + ThreadControl.SetEnable(toolStrip1, saveFileMenuItem, value); } + } + + public bool IsBusy + { + get => isBusy; set { treeView1.Enabled = !value; @@ -339,7 +343,7 @@ namespace bzit.bomg treeView1.Clear(rv); this.ApplicationMode = AppMode.New; - sessionFilename = sessionPassword = null; + this.ClearSession(); } private void fileOpenMenuItem_Click(object sender, EventArgs e) @@ -411,7 +415,8 @@ namespace bzit.bomg if (openFileDialog.ShowDialog() == DialogResult.OK) { - loadBookmarkFile(openFileDialog.FileName, openFileDialog.FilterIndex); + LoadBookmarksForm loadFileForm = new LoadBookmarksForm(this, openFileDialog.FileName); + loadFileForm.ShowDialog(); } } @@ -483,8 +488,9 @@ namespace bzit.bomg } treeView1.Clear(); + this.ApplicationMode = AppMode.Clear; - sessionFilename = sessionPassword = null; + this.ClearSession(); } private void fileSaveMenuItem_Click(object sender, EventArgs e) @@ -657,284 +663,284 @@ namespace bzit.bomg #endregion - protected void loadBookmarkFile(string filename, int filterIndex) - { - sessionFilename = sessionPassword = null; - usePassword = false; + //protected void loadBookmarkFile(string filename, int filterIndex) + //{ + // sessionFilename = sessionPassword = null; + // usePassword = false; - //sessionFilename = filename; - //sessionPassword = null; - //usePassword = false; + // //sessionFilename = filename; + // //sessionPassword = null; + // //usePassword = false; - if (RyzStudio.IO.SharpZipLib.IsZipEncrypted(filename)) - { - TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true); - string password = string.Empty; + // 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; - } + // while (true) + // { + // password = passwordForm.ShowDialog(); + // if (string.IsNullOrWhiteSpace(password)) + // { + // break; + // } - if (RyzStudio.IO.SharpZipLib.TestZipEncrypted(filename, password)) - { - sessionFilename = filename; - sessionPassword = password; - usePassword = true; + // if (RyzStudio.IO.SharpZipLib.TestZipEncrypted(filename, password)) + // { + // sessionFilename = filename; + // sessionPassword = password; + // usePassword = true; - loadFileType = LoadFileType.Jsnx; + // loadFileType = LoadFileType.Jsnx; - break; - } - } - } + // break; + // } + // } + // } - switch (filterIndex) - { - case 1: - loadFileType = LoadFileType.Jsnx; - break; - case 2: - loadFileType = LoadFileType.Ryz; - break; - default: - loadFileType = LoadFileType.None; - break; - } + // switch (filterIndex) + // { + // case 1: + // loadFileType = LoadFileType.Jsnx; + // break; + // case 2: + // loadFileType = LoadFileType.Ryz; + // break; + // default: + // loadFileType = LoadFileType.None; + // break; + // } - if (string.IsNullOrWhiteSpace(sessionFilename)) - { - return; - } + // if (string.IsNullOrWhiteSpace(sessionFilename)) + // { + // return; + // } - this.IsBusy = true; + // this.IsBusy = true; - loadFileThread.RunWorkerAsync(); - } + // loadFileThread.RunWorkerAsync(); + //} - protected void loadBookmarkFile_ForJSNX(string filename, string password = "") - { - int size = 2048; - byte[] buffer = new byte[size]; - int bufferSize = 0; + //protected void loadBookmarkFile_ForJSNX(string filename, string password = "") + //{ + // int size = 2048; + // byte[] buffer = new byte[size]; + // int bufferSize = 0; - List rs = null; + // List rs = null; - ZipEntry readEntry = null; - ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename)); - readStream.Password = password; + // ZipEntry readEntry = null; + // ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename)); + // readStream.Password = password; - while (true) - { - readEntry = readStream.GetNextEntry(); - if (readEntry == null) - { - break; - } + // while (true) + // { + // readEntry = readStream.GetNextEntry(); + // if (readEntry == null) + // { + // break; + // } - if (string.IsNullOrWhiteSpace(readEntry.Name)) - { - continue; - } + // if (string.IsNullOrWhiteSpace(readEntry.Name)) + // { + // continue; + // } - if (!readEntry.IsFile) - { - continue; - } + // if (!readEntry.IsFile) + // { + // continue; + // } - if (!readEntry.Name.Equals("bookmarks.json")) - { - continue; - } + // if (!readEntry.Name.Equals("bookmarks.json")) + // { + // continue; + // } - MemoryStream ms = new MemoryStream(); - buffer = new byte[size]; - bufferSize = 0; + // 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); + // do + // { + // bufferSize = readStream.Read(buffer, 0, buffer.Length); + // ms.Write(buffer, 0, bufferSize); + // } + // while (bufferSize > 0); - ms.Position = 0; + // ms.Position = 0; - StreamReader sr = new StreamReader(ms); - rs = JsonConvert.DeserializeObject>(sr.ReadToEnd()); + // StreamReader sr = new StreamReader(ms); + // rs = JsonConvert.DeserializeObject>(sr.ReadToEnd()); - break; - } + // break; + // } - readStream.Flush(); - readStream.Close(); - readStream.Dispose(); - readStream = null; + // readStream.Flush(); + // readStream.Close(); + // readStream.Dispose(); + // readStream = null; - treeView1.AddItem(rs); + // treeView1.AddItem(rs); - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - this.ApplicationMode = AppMode.Open; - })); - } - else - { - this.ApplicationMode = AppMode.Open; - } + // if (this.InvokeRequired) + // { + // this.Invoke(new MethodInvoker(() => + // { + // this.ApplicationMode = AppMode.Open; + // })); + // } + // else + // { + // this.ApplicationMode = AppMode.Open; + // } - RyzStudio.Windows.Forms.ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name); - } + // RyzStudio.Windows.Forms.ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name); + //} - protected void loadBookmarkFile_ForRYZ(string filename, string password = "") - { - int size = 2048; - byte[] buffer = new byte[size]; - int bufferSize = 0; + //protected void loadBookmarkFile_ForRYZ(string filename, string password = "") + //{ + // int size = 2048; + // byte[] buffer = new byte[size]; + // int bufferSize = 0; - List rs = new List(); + // List rs = new List(); - ZipEntry readEntry = null; - ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename)); - readStream.Password = password; + // ZipEntry readEntry = null; + // ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename)); + // readStream.Password = password; - while (true) - { - readEntry = readStream.GetNextEntry(); - if (readEntry == null) - { - break; - } + // while (true) + // { + // readEntry = readStream.GetNextEntry(); + // if (readEntry == null) + // { + // break; + // } - if (string.IsNullOrWhiteSpace(readEntry.Name)) - { - continue; - } + // if (string.IsNullOrWhiteSpace(readEntry.Name)) + // { + // continue; + // } - if (!readEntry.IsFile) - { - continue; - } + // if (!readEntry.IsFile) + // { + // continue; + // } - if (!readEntry.Name.Equals("bookmarks.xml")) - { - continue; - } + // if (!readEntry.Name.Equals("bookmarks.xml")) + // { + // continue; + // } - MemoryStream ms = new MemoryStream(); - buffer = new byte[size]; - bufferSize = 0; + // 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); + // do + // { + // bufferSize = readStream.Read(buffer, 0, buffer.Length); + // ms.Write(buffer, 0, bufferSize); + // } + // while (bufferSize > 0); - ms.Position = 0; + // ms.Position = 0; - // read stream - StreamReader sr = new StreamReader(ms); + // // read stream + // StreamReader sr = new StreamReader(ms); - // load xml - XmlDocument xmlDocument = new XmlDocument(); - xmlDocument.LoadXml(sr.ReadToEnd()); + // // load xml + // XmlDocument xmlDocument = new XmlDocument(); + // xmlDocument.LoadXml(sr.ReadToEnd()); - // parse - XmlNode xnl = xmlDocument.SelectSingleNode("bomg/b/g"); - if (xnl == null) - { - break; - } + // // 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()); + // 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 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; - } - } + // 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("\\"); + // // 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("\\"); - } + // 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)); - } + // 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); - } + // rs.Add(viewModel); + // } - break; - } + // break; + // } - readStream.Flush(); - readStream.Close(); - readStream.Dispose(); - readStream = null; + // readStream.Flush(); + // readStream.Close(); + // readStream.Dispose(); + // readStream = null; - treeView1.AddItem(rs); + // treeView1.AddItem(rs); - sessionFilename = sessionPassword = null; - usePassword = !string.IsNullOrWhiteSpace(password); + // sessionFilename = sessionPassword = null; + // usePassword = !string.IsNullOrWhiteSpace(password); - if (this.InvokeRequired) - { - this.Invoke(new MethodInvoker(() => - { - this.ApplicationMode = AppMode.New; - })); - } - else - { - this.ApplicationMode = AppMode.New; - } + // if (this.InvokeRequired) + // { + // this.Invoke(new MethodInvoker(() => + // { + // this.ApplicationMode = AppMode.New; + // })); + // } + // else + // { + // this.ApplicationMode = AppMode.New; + // } - RyzStudio.Windows.Forms.ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name); - } + // RyzStudio.Windows.Forms.ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name); + //} protected void openBookmark(TreeNode node) { @@ -1185,34 +1191,69 @@ namespace bzit.bomg if (this.InvokeRequired) { this.Invoke(new MethodInvoker(() => { - saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = treeView1.HasChanged; + this.AllowSave = treeView1.HasChanged; })); } else { - saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = treeView1.HasChanged; + this.AllowSave = treeView1.HasChanged; } } - - private void loadFileThread_DoWork(object sender, DoWorkEventArgs e) + public void ClearSession() { - if (loadFileType == LoadFileType.Jsnx) - { - loadBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty); - } - else if (loadFileType == LoadFileType.Ryz) - { - loadBookmarkFile_ForRYZ(sessionFilename, sessionPassword ?? string.Empty); - } + sessionFilename = sessionPassword = null; + usePassword = false; } - private void loadFileThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + public void SetSessionFile(string filename, string password) { - this.IsBusy = false; - saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = false; + sessionFilename = filename; + sessionPassword = password; + usePassword = !string.IsNullOrWhiteSpace(password); + + string ext = Path.GetExtension(filename).Trim('.').ToLower(); + if (ext.Equals("jsnx")) + { + loadFileType = LoadFileType.Jsnx; + } + else if (ext.Equals("ryz")) + { + loadFileType = LoadFileType.Ryz; + } + else + { + loadFileType = LoadFileType.None; + } + + ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(sessionFilename) + " - " + Resources.app_name); } + public void SetBookmarkItem(List itmes) + { + treeView1.AddItem(itmes); + } + + + + //private void loadFileThread_DoWork(object sender, DoWorkEventArgs e) + //{ + // if (loadFileType == LoadFileType.Jsnx) + // { + // loadBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty); + // } + // else if (loadFileType == LoadFileType.Ryz) + // { + // loadBookmarkFile_ForRYZ(sessionFilename, sessionPassword ?? string.Empty); + // } + //} + + //private void loadFileThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) + //{ + // this.IsBusy = false; + // saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = false; + //} + } } diff --git a/RyzStudio/Windows/Forms/ThreadControl.cs b/RyzStudio/Windows/Forms/ThreadControl.cs index 13581da..c7ef544 100644 --- a/RyzStudio/Windows/Forms/ThreadControl.cs +++ b/RyzStudio/Windows/Forms/ThreadControl.cs @@ -355,6 +355,36 @@ namespace RyzStudio.Windows.Forms } } + public static void SetEnable(Control container, ToolStripMenuItem control, bool value) + { + if (container.InvokeRequired) + { + container.Invoke(new MethodInvoker(() => + { + control.Enabled = value; + })); + } + else + { + control.Enabled = value; + } + } + + public static void SetEnable(Control container, ToolStripItem control, bool value) + { + if (container.InvokeRequired) + { + container.Invoke(new MethodInvoker(() => + { + control.Enabled = value; + })); + } + else + { + control.Enabled = value; + } + } + public static int GetValue(NumericUpDown sender) { int rv = 0; diff --git a/bomg.csproj b/bomg.csproj index e4f9989..0acd95a 100644 --- a/bomg.csproj +++ b/bomg.csproj @@ -102,11 +102,11 @@ - + Form - - LoadFileForm.cs + + LoadBookmarksForm.cs Form @@ -205,8 +205,8 @@ Component - - LoadFileForm.cs + + LoadBookmarksForm.cs UpdateIconsForm.cs