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

1260 lines
42 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-22 11:40:29 +00:00
using Resources = bzit.bomg.Properties.Resources;
2020-08-29 13:56:40 +00:00
using TextBoxForm = RyzStudio.Windows.ThemedForms.TextBoxForm;
using ThreadControl = RyzStudio.Windows.Forms.ThreadControl;
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
}
2020-08-29 13:56:40 +00:00
//protected BackgroundWorker loadFileThread = null;
protected bool isBusy = false;
2019-04-23 19:15:17 +00:00
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
2020-08-29 13:56:40 +00:00
//if (loadFileThread == null)
//{
// loadFileThread = new BackgroundWorker();
// loadFileThread.WorkerReportsProgress = loadFileThread.WorkerSupportsCancellation = true;
//}
2019-04-23 19:15:17 +00:00
2020-08-29 13:56:40 +00:00
//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-29 13:56:40 +00:00
this.ClearSession();
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
2020-08-23 13:03:07 +00:00
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
int i = 1;
while (true)
{
if (i > (args.Length - 1))
{
break;
}
switch (args[i].Trim().ToLower())
{
case "-o":
case "-open":
if ((i + 1) > (args.Length - 1)) break;
string openFilename = args[(i + 1)];
if (string.IsNullOrWhiteSpace(openFilename)) break;
if (!File.Exists(openFilename)) break;
2020-08-29 13:56:40 +00:00
LoadBookmarksForm loadFileForm = new LoadBookmarksForm(this, openFilename);
loadFileForm.ShowDialog();
2020-08-23 13:03:07 +00:00
i++;
break;
}
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();
2020-08-29 13:56:40 +00:00
2019-04-22 11:40:29 +00:00
this.ApplicationMode = AppMode.Clear;
2020-08-29 13:56:40 +00:00
this.ClearSession();
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
}
2020-08-29 13:56:40 +00:00
public 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:
2020-08-29 13:56:40 +00:00
ThreadControl.SetText(this, Resources.app_name);
2017-07-30 11:59:34 +00:00
2020-08-29 13:56:40 +00:00
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);
2017-07-30 11:59:34 +00:00
break;
case AppMode.Open:
2020-08-29 13:56:40 +00:00
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);
2017-07-30 11:59:34 +00:00
break;
case AppMode.New:
2020-08-29 13:56:40 +00:00
ThreadControl.SetText(this, Resources.app_name);
2017-07-30 11:59:34 +00:00
2020-08-29 13:56:40 +00:00
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);
2017-07-30 11:59:34 +00:00
break;
}
}
}
2020-08-29 13:56:40 +00:00
public bool AllowSave
2019-04-23 19:15:17 +00:00
{
2020-08-29 13:56:40 +00:00
get => (saveMenuBarItem.Enabled || saveFileMenuItem.Enabled);
set
2019-04-23 19:15:17 +00:00
{
2020-08-29 13:56:40 +00:00
ThreadControl.SetEnable(toolStrip1, saveMenuBarItem, value);
ThreadControl.SetEnable(toolStrip1, saveFileMenuItem, value);
2019-04-23 19:15:17 +00:00
}
2020-08-29 13:56:40 +00:00
}
public bool IsBusy
{
get => isBusy;
2019-04-23 19:15:17 +00:00
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;
2020-08-29 13:56:40 +00:00
this.ClearSession();
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)
{
2020-08-29 13:56:40 +00:00
LoadBookmarksForm loadFileForm = new LoadBookmarksForm(this, openFileDialog.FileName);
loadFileForm.ShowDialog();
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();
2020-08-29 13:56:40 +00:00
2019-04-22 11:40:29 +00:00
this.ApplicationMode = AppMode.Clear;
2020-08-29 13:56:40 +00:00
this.ClearSession();
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;
}
2020-08-28 22:05:59 +00:00
UpdateIconsForm optionForm = new UpdateIconsForm(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
2020-08-29 13:56:40 +00:00
//protected void loadBookmarkFile(string filename, int filterIndex)
//{
// sessionFilename = 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;
// while (true)
// {
// password = passwordForm.ShowDialog();
// if (string.IsNullOrWhiteSpace(password))
// {
// break;
// }
// if (RyzStudio.IO.SharpZipLib.TestZipEncrypted(filename, password))
// {
// sessionFilename = filename;
// sessionPassword = password;
// usePassword = true;
// loadFileType = LoadFileType.Jsnx;
// 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;
// }
// this.IsBusy = true;
// loadFileThread.RunWorkerAsync();
//}
//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;
// }
// readStream.Flush();
// readStream.Close();
// readStream.Dispose();
// readStream = null;
// treeView1.AddItem(rs);
// 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);
//}
//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);
//}
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(() => {
2020-08-29 13:56:40 +00:00
this.AllowSave = treeView1.HasChanged;
}));
}
else
{
2020-08-29 13:56:40 +00:00
this.AllowSave = treeView1.HasChanged;
}
}
2020-08-29 13:56:40 +00:00
public void ClearSession()
{
sessionFilename = sessionPassword = null;
usePassword = false;
}
2020-08-29 13:56:40 +00:00
public void SetSessionFile(string filename, string password)
2019-04-23 19:15:17 +00:00
{
2020-08-29 13:56:40 +00:00
sessionFilename = filename;
sessionPassword = password;
usePassword = !string.IsNullOrWhiteSpace(password);
string ext = Path.GetExtension(filename).Trim('.').ToLower();
if (ext.Equals("jsnx"))
2019-04-23 19:15:17 +00:00
{
2020-08-29 13:56:40 +00:00
loadFileType = LoadFileType.Jsnx;
2019-04-23 19:15:17 +00:00
}
2020-08-29 13:56:40 +00:00
else if (ext.Equals("ryz"))
2019-04-23 19:15:17 +00:00
{
2020-08-29 13:56:40 +00:00
loadFileType = LoadFileType.Ryz;
2019-04-23 19:15:17 +00:00
}
2020-08-29 13:56:40 +00:00
else
{
loadFileType = LoadFileType.None;
}
ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(sessionFilename) + " - " + Resources.app_name);
2019-04-23 19:15:17 +00:00
}
2020-08-29 13:56:40 +00:00
public void SetBookmarkItem(List<BookmarkItemViewModel> itmes)
2019-04-23 19:15:17 +00:00
{
2020-08-29 13:56:40 +00:00
treeView1.AddItem(itmes);
2019-04-23 19:15:17 +00:00
}
2020-08-29 13:56:40 +00:00
//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;
//}
2017-07-30 11:59:34 +00:00
}
2019-04-20 17:25:14 +00:00
}