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/MainForm.cs

1269 lines
41 KiB
C#
Raw Normal View History

using bzit.bomg.Models;
2019-04-20 17:25:14 +00:00
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
2017-07-30 11:59:34 +00:00
using System;
2019-04-20 16:05:55 +00:00
using System.Collections.Generic;
2017-07-30 11:59:34 +00:00
using System.ComponentModel;
using System.IO;
2019-04-21 17:53:56 +00:00
using System.Text;
2017-07-30 11:59:34 +00:00
using System.Windows.Forms;
2019-04-21 17:53:56 +00:00
using System.Xml;
2019-04-20 16:05:55 +00:00
using static RyzStudio.Windows.Forms.BookmarkTreeView;
2019-04-21 17:53:56 +00:00
using TextBoxForm = RyzStudio.Windows.ThemedForms.TextBoxForm;
2019-04-22 11:40:29 +00:00
using Resources = bzit.bomg.Properties.Resources;
2017-07-30 11:59:34 +00:00
namespace bzit.bomg
{
public partial class MainForm : Form
{
public enum AppMode
{
Clear = 0,
Open,
New
}
2019-04-23 19:15:17 +00:00
public enum LoadFileType
{
None = 0,
Jsnx,
Ryz
}
protected BackgroundWorker loadFileThread = null;
protected LoadFileType loadFileType = LoadFileType.None;
2017-07-30 11:59:34 +00:00
protected AppMode appMode = AppMode.Clear;
protected string sessionFilename = null;
2019-04-23 19:15:17 +00:00
protected string sessionPassword = null;
protected bool usePassword = false;
2017-07-30 11:59:34 +00:00
public MainForm()
{
InitializeComponent();
2019-04-23 19:15:17 +00:00
if (loadFileThread == null)
{
loadFileThread = new BackgroundWorker();
loadFileThread.WorkerReportsProgress = loadFileThread.WorkerSupportsCancellation = true;
}
loadFileThread.DoWork += loadFileThread_DoWork;
loadFileThread.RunWorkerCompleted += loadFileThread_RunWorkerCompleted;
treeView1.OnNodeChanged += treeView1_OnNodeChanged;
2017-07-30 11:59:34 +00:00
}
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();
}
2017-07-30 11:59:34 +00:00
this.ApplicationMode = AppMode.Clear;
2020-08-23 10:40:44 +00:00
2019-04-23 19:15:17 +00:00
sessionFilename = sessionPassword = null;
usePassword = false;
2017-07-30 11:59:34 +00:00
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
2017-07-30 11:59:34 +00:00
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
// command line
2019-04-20 17:25:14 +00:00
//string[] szr = Environment.GetCommandLineArgs();
//int i = 0;
//while (true)
//{
// if (i > (szr.Length - 1))
// {
// break;
// }
2017-07-30 11:59:34 +00:00
2019-04-20 17:25:14 +00:00
// 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;
// }
2017-07-30 11:59:34 +00:00
2019-04-20 17:25:14 +00:00
// i++;
//}
2017-07-30 11:59:34 +00:00
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
e.Cancel = true;
return;
}
2017-07-30 11:59:34 +00:00
if (this.ApplicationMode == AppMode.Clear)
{
2019-04-22 11:40:29 +00:00
// do later
2017-07-30 11:59:34 +00:00
}
2019-04-22 11:40:29 +00:00
else
2017-07-30 11:59:34 +00:00
{
2019-04-22 11:40:29 +00:00
if (treeView1.HasChanged)
{
if (this.ApplicationMode == AppMode.New)
{
DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
bool hasSaved = saveAsBookmarkFile();
if (!hasSaved)
{
e.Cancel = true;
return;
}
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
e.Cancel = true;
return;
}
}
else if (this.ApplicationMode == AppMode.Open)
{
DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
2019-04-23 19:15:17 +00:00
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
2019-04-22 11:40:29 +00:00
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
e.Cancel = true;
return;
}
}
}
else
{
// do later
}
2017-07-30 11:59:34 +00:00
}
2019-04-22 11:40:29 +00:00
treeView1.CloseIconDatabase();
2017-07-30 11:59:34 +00:00
2019-04-22 11:40:29 +00:00
treeView1.Clear();
this.ApplicationMode = AppMode.Clear;
2019-04-23 19:15:17 +00:00
sessionFilename = sessionPassword = null;
2017-07-30 11:59:34 +00:00
2019-04-22 11:40:29 +00:00
Application.Exit();
2017-07-30 11:59:34 +00:00
}
2019-04-23 19:15:17 +00:00
protected AppMode ApplicationMode
2017-07-30 11:59:34 +00:00
{
2019-04-22 11:40:29 +00:00
get => appMode;
2017-07-30 11:59:34 +00:00
set
{
appMode = value;
2019-04-21 17:53:56 +00:00
2017-07-30 11:59:34 +00:00
switch (value)
{
case AppMode.Clear:
this.Text = Properties.Resources.app_name;
closeFileMenuItem.Enabled = false;
saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false;
saveAsFileMenuItem.Enabled = false;
2019-04-22 14:51:03 +00:00
findEditMenuItem.Enabled = false;
2019-04-22 14:54:55 +00:00
expandAllViewMenuItem.Enabled = collapseAllViewMenuItem.Enabled = false;
2017-07-30 11:59:34 +00:00
break;
case AppMode.Open:
closeFileMenuItem.Enabled = true;
2019-04-22 11:40:29 +00:00
saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = true;
2017-07-30 11:59:34 +00:00
saveAsFileMenuItem.Enabled = true;
2019-04-22 14:51:03 +00:00
findEditMenuItem.Enabled = true;
2019-04-22 14:54:55 +00:00
expandAllViewMenuItem.Enabled = collapseAllViewMenuItem.Enabled = true;
2017-07-30 11:59:34 +00:00
break;
case AppMode.New:
this.Text = Properties.Resources.app_name;
closeFileMenuItem.Enabled = true;
2019-04-22 11:40:29 +00:00
saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = true;
2017-07-30 11:59:34 +00:00
saveAsFileMenuItem.Enabled = true;
2019-04-22 14:51:03 +00:00
findEditMenuItem.Enabled = true;
2019-04-22 14:54:55 +00:00
expandAllViewMenuItem.Enabled = collapseAllViewMenuItem.Enabled = true;
2017-07-30 11:59:34 +00:00
break;
}
}
}
2019-04-23 19:15:17 +00:00
protected bool IsBusy
{
get
{
return loadFileThread.IsBusy;
}
set
{
treeView1.Enabled = !value;
}
}
#region toolbar
2017-07-30 11:59:34 +00:00
2019-04-22 11:40:29 +00:00
private void fileNewMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-22 11:40:29 +00:00
if (this.ApplicationMode == AppMode.Clear)
{
// do later
}
else
{
if (treeView1.HasChanged)
{
if (this.ApplicationMode == AppMode.New)
{
DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
bool hasSaved = saveAsBookmarkFile();
if (!hasSaved)
{
return;
}
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
else if (this.ApplicationMode == AppMode.Open)
{
DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
2019-04-23 19:15:17 +00:00
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
2019-04-22 11:40:29 +00:00
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
}
else
{
// do later
}
}
2019-04-21 17:53:56 +00:00
TextBoxForm newSessionForm = new TextBoxForm("New Session", "Name");
string rv = newSessionForm.ShowDialog();
if (string.IsNullOrWhiteSpace(rv))
{
return;
}
2019-04-22 11:40:29 +00:00
treeView1.Clear(rv);
this.ApplicationMode = AppMode.New;
2019-04-23 19:15:17 +00:00
sessionFilename = sessionPassword = null;
2019-04-22 11:40:29 +00:00
}
private void fileOpenMenuItem_Click(object sender, EventArgs e)
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-21 17:53:56 +00:00
if (this.ApplicationMode == AppMode.Clear)
{
2019-04-22 11:40:29 +00:00
// do later
2019-04-21 17:53:56 +00:00
}
else
{
2019-04-22 11:40:29 +00:00
if (treeView1.HasChanged)
{
if (this.ApplicationMode == AppMode.New)
{
DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
bool hasSaved = saveAsBookmarkFile();
if (!hasSaved)
{
return;
}
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
else if (this.ApplicationMode == AppMode.Open)
{
DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
2019-04-23 19:15:17 +00:00
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
2019-04-22 11:40:29 +00:00
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
}
else
{
// do later
}
2019-04-21 17:53:56 +00:00
}
2017-07-30 11:59:34 +00:00
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
2019-04-21 17:53:56 +00:00
loadBookmarkFile(openFileDialog.FileName, openFileDialog.FilterIndex);
2017-07-30 11:59:34 +00:00
}
}
2019-04-22 11:40:29 +00:00
private void fileCloseMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-22 11:40:29 +00:00
if (this.ApplicationMode == AppMode.Clear)
{
// do later
}
else
{
if (treeView1.HasChanged)
{
if (this.ApplicationMode == AppMode.New)
{
DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
bool hasSaved = saveAsBookmarkFile();
if (!hasSaved)
{
return;
}
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
else if (this.ApplicationMode == AppMode.Open)
{
DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
2019-04-23 19:15:17 +00:00
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
2019-04-22 11:40:29 +00:00
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
}
else
{
// do later
}
}
2019-04-22 11:40:29 +00:00
treeView1.Clear();
this.ApplicationMode = AppMode.Clear;
2019-04-23 19:15:17 +00:00
sessionFilename = sessionPassword = null;
2019-04-22 11:40:29 +00:00
}
2017-07-30 11:59:34 +00:00
2019-04-22 11:40:29 +00:00
private void fileSaveMenuItem_Click(object sender, EventArgs e)
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-22 11:40:29 +00:00
if (this.ApplicationMode == AppMode.New)
{
DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
bool hasSaved = saveAsBookmarkFile();
if (!hasSaved)
{
return;
}
}
else if (response == DialogResult.No)
{
return;
}
else if (response == DialogResult.Cancel)
{
return;
}
}
else if (this.ApplicationMode == AppMode.Open)
{
2019-04-23 19:15:17 +00:00
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
2019-04-22 11:40:29 +00:00
}
2017-07-30 11:59:34 +00:00
}
2019-04-22 11:40:29 +00:00
private void fileSaveAsMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-22 11:40:29 +00:00
if (this.ApplicationMode == AppMode.Clear)
{
return;
}
2017-07-30 11:59:34 +00:00
2019-04-22 11:40:29 +00:00
bool hasSaved = saveAsBookmarkFile();
if (!hasSaved)
{
return;
}
2017-07-30 11:59:34 +00:00
}
2019-04-22 11:40:29 +00:00
private void fileExitMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-22 11:40:29 +00:00
this.Close();
2017-07-30 11:59:34 +00:00
}
2019-04-22 14:51:03 +00:00
private void editFindMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-22 14:51:03 +00:00
FindForm findForm = new FindForm(this);
findForm.Show();
2017-07-30 11:59:34 +00:00
}
2019-04-22 11:40:29 +00:00
private void viewExpandAllMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-22 14:51:03 +00:00
if (this.ApplicationMode == AppMode.Clear)
{
return;
}
2017-07-30 11:59:34 +00:00
if (treeView1.SelectedNode == null)
{
treeView1.ExpandAll();
}
else
{
treeView1.SelectedNode.ExpandAll();
}
}
2019-04-22 11:40:29 +00:00
private void viewCollapseAllMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
2019-04-22 14:51:03 +00:00
if (this.ApplicationMode == AppMode.Clear)
{
return;
}
2017-07-30 11:59:34 +00:00
if (treeView1.SelectedNode == null)
{
treeView1.CollapseAll();
}
else
{
treeView1.SelectedNode.Collapse(false);
}
}
2019-04-22 11:40:29 +00:00
private void viewAlwaysOnTopMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-22 11:40:29 +00:00
this.TopMost = !this.TopMost;
2017-07-30 11:59:34 +00:00
2019-04-22 11:40:29 +00:00
alwaysOnTopToolStripMenuItem1.Checked = this.TopMost;
2017-07-30 11:59:34 +00:00
}
2019-04-22 11:40:29 +00:00
private void toolsOptionsMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-23 19:15:17 +00:00
if (this.IsBusy)
{
return;
}
OptionForm optionForm = new OptionForm(this);
optionForm.ShowDialog();
2017-07-30 11:59:34 +00:00
}
2019-04-22 11:40:29 +00:00
private void helpViewHelpMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
try
{
2019-04-22 11:40:29 +00:00
System.Diagnostics.Process.Start(Resources.url_help);
2017-07-30 11:59:34 +00:00
}
catch
{
// do nothing
}
}
2019-04-22 11:40:29 +00:00
private void helpAboutMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
2019-04-22 11:40:29 +00:00
try
{
System.Diagnostics.Process.Start(Resources.url_about);
}
catch
{
// do nothing
}
2017-07-30 11:59:34 +00:00
}
#endregion
2019-04-21 17:53:56 +00:00
protected void loadBookmarkFile(string filename, int filterIndex)
{
switch (filterIndex)
{
case 1:
if (RyzStudio.IO.SharpZipLib.IsZipEncrypted(filename))
{
2019-04-22 11:40:29 +00:00
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
2019-04-21 17:53:56 +00:00
string password = string.Empty;
while (true)
{
password = passwordForm.ShowDialog();
if (string.IsNullOrWhiteSpace(password))
{
break;
}
if (RyzStudio.IO.SharpZipLib.TestZipEncrypted(filename, password))
{
2019-04-22 11:40:29 +00:00
sessionFilename = filename;
2019-04-23 19:15:17 +00:00
sessionPassword = password;
usePassword = true;
loadFileType = LoadFileType.Jsnx;
2019-04-21 17:53:56 +00:00
break;
}
}
}
else
{
2019-04-22 11:40:29 +00:00
sessionFilename = filename;
2019-04-23 19:15:17 +00:00
sessionPassword = null;
usePassword = false;
loadFileType = LoadFileType.Jsnx;
2019-04-21 17:53:56 +00:00
}
break;
case 2:
if (RyzStudio.IO.SharpZipLib.IsZipEncrypted(filename))
{
2019-04-22 11:40:29 +00:00
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
2019-04-21 17:53:56 +00:00
string password = string.Empty;
while (true)
{
password = passwordForm.ShowDialog();
if (string.IsNullOrWhiteSpace(password))
{
break;
}
if (RyzStudio.IO.SharpZipLib.TestZipEncrypted(filename, password))
{
2019-04-23 19:15:17 +00:00
sessionFilename = filename;
sessionPassword = password;
usePassword = true;
loadFileType = LoadFileType.Ryz;
2019-04-22 11:40:29 +00:00
2019-04-21 17:53:56 +00:00
break;
}
}
}
else
{
2019-04-23 19:15:17 +00:00
sessionFilename = filename;
sessionPassword = null;
usePassword = false;
loadFileType = LoadFileType.Ryz;
2019-04-21 17:53:56 +00:00
}
break;
default:
2019-04-24 21:33:42 +00:00
sessionFilename = sessionPassword = null;
usePassword = false;
2019-04-21 17:53:56 +00:00
break;
}
2019-04-24 21:33:42 +00:00
if (string.IsNullOrWhiteSpace(sessionFilename))
{
return;
}
this.IsBusy = true;
loadFileThread.RunWorkerAsync();
2019-04-21 17:53:56 +00:00
}
protected void loadBookmarkFile_ForJSNX(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;
}
2019-04-22 11:40:29 +00:00
readStream.Flush();
readStream.Close();
readStream.Dispose();
readStream = null;
2019-04-27 02:17:24 +00:00
treeView1.AddItem(rs);
if (this.InvokeRequired)
2019-04-23 19:15:17 +00:00
{
2019-04-27 02:17:24 +00:00
this.Invoke(new MethodInvoker(() =>
{
this.ApplicationMode = AppMode.Open;
2019-04-23 19:15:17 +00:00
}));
}
else
{
2019-04-27 02:17:24 +00:00
this.ApplicationMode = AppMode.Open;
2019-04-23 19:15:17 +00:00
}
if (this.InvokeRequired)
{
2019-04-27 02:17:24 +00:00
this.Invoke(new MethodInvoker(() =>
{
this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name;
2019-04-23 19:15:17 +00:00
}));
}
else
{
2019-04-27 02:17:24 +00:00
this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name;
2019-04-23 19:15:17 +00:00
}
2019-04-21 17:53:56 +00:00
}
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;
}
2019-04-22 11:40:29 +00:00
readStream.Flush();
readStream.Close();
readStream.Dispose();
readStream = null;
2019-04-27 02:17:24 +00:00
treeView1.AddItem(rs);
2019-04-23 19:15:17 +00:00
sessionFilename = sessionPassword = null;
usePassword = !string.IsNullOrWhiteSpace(password);
if (this.InvokeRequired)
{
2019-04-27 02:17:24 +00:00
this.Invoke(new MethodInvoker(() =>
{
2019-04-23 19:15:17 +00:00
this.ApplicationMode = AppMode.New;
}));
}
else
{
this.ApplicationMode = AppMode.New;
}
2019-04-22 11:40:29 +00:00
2019-04-27 02:17:24 +00:00
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() =>
{
this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name;
}));
}
else
{
this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Resources.app_name;
}
2019-04-21 17:53:56 +00:00
}
2019-04-23 19:15:17 +00:00
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
}
}
2019-04-22 11:40:29 +00:00
protected bool saveAsBookmarkFile()
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
switch (saveFileDialog.FilterIndex)
{
case 1:
saveBookmarkFile_ForJSNX(saveFileDialog.FileName);
2019-04-27 02:17:24 +00:00
this.Text = Path.GetFileNameWithoutExtension(saveFileDialog.FileName) + " - " + Properties.Resources.app_name;
2019-04-22 11:40:29 +00:00
sessionFilename = saveFileDialog.FileName;
2019-04-23 19:15:17 +00:00
usePassword = false;
2019-04-22 11:40:29 +00:00
this.ApplicationMode = AppMode.Open;
treeView1.HasChanged = false;
break;
case 2:
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
string password = passwordForm.ShowDialog();
saveBookmarkFile_ForJSNX(saveFileDialog.FileName, password ?? string.Empty);
2019-04-27 02:17:24 +00:00
this.Text = Path.GetFileNameWithoutExtension(saveFileDialog.FileName) + " - " + Properties.Resources.app_name;
2019-04-22 11:40:29 +00:00
sessionFilename = saveFileDialog.FileName;
2019-04-23 19:15:17 +00:00
sessionPassword = password;
usePassword = true;
2019-04-22 11:40:29 +00:00
this.ApplicationMode = AppMode.Open;
treeView1.HasChanged = false;
break;
default: break;
}
return true;
}
else
{
return false;
}
}
protected void saveBookmarkFile_ForJSNX(string filename, string password = "")
2019-04-20 17:25:14 +00:00
{
List<BookmarkItemViewModel> rs = treeView1.GetBookmarkList();
int size = 2048;
byte[] buffer = new byte[size];
int bufferSize = 0;
ZipOutputStream zipStream = new ZipOutputStream(File.Create(filename));
zipStream.SetLevel(9);
2019-04-21 17:53:56 +00:00
zipStream.Password = password;
2019-04-20 17:25:14 +00:00
2019-04-21 17:53:56 +00:00
// stream
2019-04-20 17:25:14 +00:00
MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(rs)));
ms.Position = 0;
2019-04-21 17:53:56 +00:00
// write file entry
2019-04-20 17:25:14 +00:00
zipStream.PutNextEntry(new ZipEntry("bookmarks.json"));
buffer = new byte[size];
bufferSize = 0;
do
2017-07-30 11:59:34 +00:00
{
2019-04-20 17:25:14 +00:00
bufferSize = ms.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, bufferSize);
2017-07-30 11:59:34 +00:00
}
2019-04-20 17:25:14 +00:00
while (bufferSize > 0);
2017-07-30 11:59:34 +00:00
2019-04-20 17:25:14 +00:00
ms.Flush();
ms.Close();
ms.Dispose();
ms = null;
2017-07-30 11:59:34 +00:00
2019-04-20 17:25:14 +00:00
zipStream.Finish();
zipStream.Flush();
zipStream.Close();
zipStream.Dispose();
zipStream = null;
2019-04-20 16:05:55 +00:00
2019-04-22 11:40:29 +00:00
treeView1.HasChanged = false;
}
2019-04-20 16:05:55 +00:00
2019-04-20 17:25:14 +00:00
protected void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
2017-07-30 11:59:34 +00:00
{
if (e.Button != MouseButtons.Right)
{
return;
}
switch (treeView1.SNode.GetNodeType())
{
case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Root:
2020-08-23 10:40:44 +00:00
rootTreeNodeMenu.Show(e.Node.TreeView, e.X, e.Y);
break;
case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Folder:
2020-08-23 10:40:44 +00:00
folderTreeNodeMenu.Show(e.Node.TreeView, e.X, e.Y);
break;
case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Page:
2020-08-23 10:40:44 +00:00
pageTreeNodeMenu.Show(e.Node.TreeView, e.X, e.Y);
break;
default:
break;
}
2017-07-30 11:59:34 +00:00
}
2019-04-20 17:25:14 +00:00
protected void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) => openBookmark(e.Node);
2017-07-30 11:59:34 +00:00
2019-04-20 17:25:14 +00:00
protected void treeView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
2017-07-30 11:59:34 +00:00
{
TreeNode tn = treeView1.SelectedNode;
if (tn == null)
{
return;
}
2019-04-20 16:05:55 +00:00
NodeType nodeType = treeView1.SNode.GetNodeType();
2017-07-30 11:59:34 +00:00
switch (e.KeyCode)
{
case Keys.Apps:
treeView1_NodeMouseClick(sender, new TreeNodeMouseClickEventArgs(tn, MouseButtons.Right, 1, tn.Bounds.X, tn.Bounds.Y));
return;
2019-04-20 16:05:55 +00:00
case Keys.Enter:
openBookmark(tn);
2019-04-23 20:57:35 +00:00
break;
case Keys.F2:
if (nodeType == NodeType.Page)
{
(new BookmarkEditForm(treeView1)).ShowDialog();
}
2019-04-20 16:05:55 +00:00
break;
case Keys.Insert:
if (e.Modifiers != Keys.Shift)
2017-07-30 11:59:34 +00:00
{
2019-04-20 16:05:55 +00:00
if ((nodeType == NodeType.Root) || (nodeType == NodeType.Folder))
{
(new BookmarkEditForm(treeView1)).ShowDialog();
}
else if (nodeType == NodeType.Page)
{
treeView1.SelectedNode = tn.Parent;
2017-07-30 11:59:34 +00:00
2019-04-20 16:05:55 +00:00
(new BookmarkEditForm(treeView1)).ShowDialog();
}
2017-07-30 11:59:34 +00:00
}
break;
default: break;
}
}
#region root context menu
private void addPageContextMenu_Click(object sender, EventArgs e) => (new BookmarkEditForm(treeView1)).ShowDialog();
2019-04-23 19:15:17 +00:00
private void addFolderContextMenu_Click(object sender, EventArgs e) => treeView1.SelectedNode = treeView1.SNode.AddFolder();
2019-04-23 19:15:17 +00:00
private void editContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.Edit();
2019-04-23 19:15:17 +00:00
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();
2019-04-23 19:15:17 +00:00
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();
2019-04-23 19:15:17 +00:00
private void deleteContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.Delete();
2019-04-23 19:15:17 +00:00
private void sortContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.Sort();
2019-04-23 19:15:17 +00:00
private void moveUpContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.MoveUp();
2019-04-23 19:15:17 +00:00
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);
2019-04-23 19:15:17 +00:00
private void editContextMenu3_Click(object sender, EventArgs e) => (new BookmarkEditForm(treeView1)).ShowDialog();
2019-04-23 19:15:17 +00:00
private void deleteContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.Delete();
2019-04-23 19:15:17 +00:00
private void moveUpContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.MoveUp();
2019-04-23 19:15:17 +00:00
private void moveDownContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.MoveDown();
#endregion
protected void treeView1_OnNodeChanged(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = treeView1.HasChanged;
}));
}
else
{
saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = treeView1.HasChanged;
}
}
2019-04-23 19:15:17 +00:00
private void loadFileThread_DoWork(object sender, DoWorkEventArgs e)
{
if (loadFileType == LoadFileType.Jsnx)
{
2019-04-27 02:17:24 +00:00
loadBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
2019-04-23 19:15:17 +00:00
}
else if (loadFileType == LoadFileType.Ryz)
{
2019-04-27 02:17:24 +00:00
loadBookmarkFile_ForRYZ(sessionFilename, sessionPassword ?? string.Empty);
2019-04-23 19:15:17 +00:00
}
}
private void loadFileThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.IsBusy = false;
saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = false;
2019-04-23 19:15:17 +00:00
}
2017-07-30 11:59:34 +00:00
}
2019-04-20 17:25:14 +00:00
}