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

360 lines
11 KiB
C#
Raw Normal View History

2020-08-29 13:56:40 +00:00
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;
2020-08-29 22:46:36 +00:00
using System.Text;
2020-08-29 13:56:40 +00:00
using System.Threading.Tasks;
using System.Windows.Forms;
2020-08-29 22:46:36 +00:00
using System.Xml;
2020-08-29 13:56:40 +00:00
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;
}
}
2020-08-29 22:46:36 +00:00
protected async override void OnShown(EventArgs e)
2020-08-29 13:56:40 +00:00
{
base.OnShown(e);
2020-08-29 22:46:36 +00:00
await loadFile(sessionFilename);
this.Close();
2020-08-29 13:56:40 +00:00
}
protected bool IsBusy
{
get => isBusy;
set
{
isBusy = value;
parentForm.IsBusy = value;
2020-08-29 23:25:48 +00:00
ThreadControl.SetImage(pictureBox1, (value) ? Properties.Resources.aniZomq2x32 : null);
2020-08-29 13:56:40 +00:00
//ThreadControl.SetEnable(button2, !value);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
this.Close();
}
2020-08-29 22:46:36 +00:00
protected async Task loadFile(string filename)
2020-08-29 13:56:40 +00:00
{
parentForm.ClearSession();
2020-08-29 22:46:36 +00:00
parentForm.TreeView.Clear();
parentForm.TreeView.HasChanged = false;
2020-08-29 13:56:40 +00:00
if (string.IsNullOrWhiteSpace(filename))
{
return;
}
2020-08-29 22:46:36 +00:00
ThreadControl.SetValue(progressBar1, 0, 0);
2020-08-29 13:56:40 +00:00
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;
}
}
2020-08-29 22:46:36 +00:00
if (string.IsNullOrWhiteSpace(password))
{
return;
}
2020-08-29 13:56:40 +00:00
}
this.IsBusy = true;
string ext = Path.GetExtension(filename).Trim('.').ToLower();
if (ext.Equals("jsnx"))
{
2020-08-29 22:46:36 +00:00
await loadJSNXBookmarkFile(filename, password);
2020-08-29 13:56:40 +00:00
}
else if (ext.Equals("ryz"))
{
2020-08-29 22:46:36 +00:00
await loadRYZBookmarkFile(filename, password);
2020-08-29 13:56:40 +00:00
}
else
{
// do nothing
}
this.IsBusy = false;
parentForm.AllowSave = false;
2020-08-29 22:46:36 +00:00
parentForm.TreeView.HasChanged = false;
if (parentForm.TreeView.Nodes.Count > 0) parentForm.TreeView.Nodes[0].Expand();
2020-08-29 13:56:40 +00:00
}
2020-08-29 22:46:36 +00:00
protected async Task loadJSNXBookmarkFile(string filename, string password)
2020-08-29 13:56:40 +00:00
{
2020-08-29 22:46:36 +00:00
await Task.Run(() =>
{
int size = 2048;
byte[] buffer = new byte[size];
int bufferSize = 0;
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
List<BookmarkItemViewModel> rs = null;
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
ZipEntry readEntry = null;
ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename));
readStream.Password = password;
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
while (true)
2020-08-29 13:56:40 +00:00
{
2020-08-29 22:46:36 +00:00
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());
2020-08-29 13:56:40 +00:00
break;
}
2020-08-29 22:46:36 +00:00
readStream.Flush();
readStream.Close();
readStream.Dispose();
readStream = null;
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
// load bookmark items
ThreadControl.SetValue(progressBar1, 0, rs.Count);
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
for (int i=0; i<rs.Count; i++)
2020-08-29 13:56:40 +00:00
{
2020-08-29 22:46:36 +00:00
ThreadControl.SetValue(progressBar1, (i + 1));
parentForm.TreeView.AddItem(rs[i]);
2020-08-29 13:56:40 +00:00
}
2020-08-29 22:46:36 +00:00
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;
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
List<BookmarkItemViewModel> rs = new List<BookmarkItemViewModel>();
ZipEntry readEntry = null;
ZipInputStream readStream = new ZipInputStream(File.OpenRead(filename));
readStream.Password = password;
while (true)
2020-08-29 13:56:40 +00:00
{
2020-08-29 22:46:36 +00:00
readEntry = readStream.GetNextEntry();
if (readEntry == null)
{
break;
}
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
if (string.IsNullOrWhiteSpace(readEntry.Name))
{
continue;
}
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
if (!readEntry.IsFile)
{
continue;
}
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
if (!readEntry.Name.Equals("bookmarks.xml"))
{
continue;
}
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
MemoryStream ms = new MemoryStream();
buffer = new byte[size];
bufferSize = 0;
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
do
{
bufferSize = readStream.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, bufferSize);
}
while (bufferSize > 0);
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
ms.Position = 0;
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
// read stream
StreamReader sr = new StreamReader(ms);
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
// load xml
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(sr.ReadToEnd());
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
// parse
XmlNode xnl = xmlDocument.SelectSingleNode("bomg/b/g");
if (xnl == null)
{
break;
}
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
string rootName = (xnl.Attributes["name"] == null) ? "Bookmarks" : (string.IsNullOrWhiteSpace(xnl.Attributes["name"].InnerText) ? "Bookmarks" : xnl.Attributes["name"].InnerText.Trim());
2020-08-29 13:56:40 +00:00
2020-08-29 22:46:36 +00:00
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;
});
}
2020-08-29 13:56:40 +00:00
}
}