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

413 lines
13 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.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<TreeNode> 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<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;
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<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;
// 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);
//}
}
}