This repository has been archived on 2022-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager/LoadBookmarksForm.cs

364 lines
11 KiB
C#

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.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using AppMode = bzit.bomg.MainForm.AppMode;
using Form = System.Windows.Forms.Form;
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 async override void OnShown(EventArgs e)
{
base.OnShown(e);
await loadFile(sessionFilename);
this.Close();
}
protected bool IsBusy
{
get => isBusy;
set
{
isBusy = value;
parentForm.IsBusy = value;
ThreadControl.SetImage(pictureBox1, (value) ? Properties.Resources.aniZomq2x32 : null);
//ThreadControl.SetEnable(button2, !value);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
this.Close();
}
protected async Task loadFile(string filename)
{
parentForm.ClearSession();
parentForm.TreeView.Clear();
parentForm.TreeView.HasChanged = false;
if (string.IsNullOrWhiteSpace(filename))
{
return;
}
ThreadControl.SetValue(progressBar1, 0, 0);
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;
}
}
if (string.IsNullOrWhiteSpace(password))
{
return;
}
}
else
{
parentForm.SetSessionFile(filename, string.Empty);
}
this.IsBusy = true;
string ext = Path.GetExtension(filename).Trim('.').ToLower();
if (ext.Equals("jsnx"))
{
await loadJSNXBookmarkFile(filename, password);
}
else if (ext.Equals("ryz"))
{
await loadRYZBookmarkFile(filename, password);
}
else
{
// do nothing
}
this.IsBusy = false;
parentForm.AllowSave = false;
parentForm.TreeView.HasChanged = false;
if (parentForm.TreeView.Nodes.Count > 0) parentForm.TreeView.Nodes[0].Expand();
}
protected async Task loadJSNXBookmarkFile(string filename, string password)
{
await Task.Run(() =>
{
int size = 2048;
byte[] buffer = new byte[size];
int bufferSize = 0;
List<BookmarkItemViewModel> 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<List<BookmarkItemViewModel>>(sr.ReadToEnd());
break;
}
readStream.Flush();
readStream.Close();
readStream.Dispose();
readStream = null;
// load bookmark items
ThreadControl.SetValue(progressBar1, 0, rs.Count);
for (int i=0; i<rs.Count; i++)
{
ThreadControl.SetValue(progressBar1, (i + 1));
parentForm.TreeView.AddItem(rs[i]);
}
parentForm.ApplicationMode = AppMode.Open;
});
}
protected async Task loadRYZBookmarkFile(string filename, string password)
{
await Task.Run(() =>
{
int size = 2048;
byte[] buffer = new byte[size];
int bufferSize = 0;
List<BookmarkItemViewModel> rs = new List<BookmarkItemViewModel>();
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;
// load bookmark items
ThreadControl.SetValue(progressBar1, 0, rs.Count);
for (int i = 0; i < rs.Count; i++)
{
ThreadControl.SetValue(progressBar1, (i + 1));
parentForm.TreeView.AddItem(rs[i]);
}
parentForm.ClearSession();
parentForm.ApplicationMode = AppMode.Open;
});
}
}
}