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
2020-08-29 23:46:36 +01:00

957 lines
32 KiB
C#

using bzit.bomg.Models;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using static RyzStudio.Windows.Forms.BookmarkTreeView;
using Resources = bzit.bomg.Properties.Resources;
using TextBoxForm = RyzStudio.Windows.ThemedForms.TextBoxForm;
using ThreadControl = RyzStudio.Windows.Forms.ThreadControl;
namespace bzit.bomg
{
public partial class MainForm : Form
{
public enum AppMode
{
Clear = 0,
Open,
New
}
public enum LoadFileType
{
None = 0,
Jsnx,
Ryz
}
//protected BackgroundWorker loadFileThread = null;
protected bool isBusy = false;
protected LoadFileType loadFileType = LoadFileType.None;
protected AppMode appMode = AppMode.Clear;
protected string sessionFilename = null;
protected string sessionPassword = null;
protected bool usePassword = false;
public MainForm()
{
InitializeComponent();
//if (loadFileThread == null)
//{
// loadFileThread = new BackgroundWorker();
// loadFileThread.WorkerReportsProgress = loadFileThread.WorkerSupportsCancellation = true;
//}
//loadFileThread.DoWork += loadFileThread_DoWork;
//loadFileThread.RunWorkerCompleted += loadFileThread_RunWorkerCompleted;
treeView1.OnNodeChanged += treeView1_OnNodeChanged;
}
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();
}
this.ApplicationMode = AppMode.Clear;
this.ClearSession();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
// command line
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;
LoadBookmarksForm loadFileForm = new LoadBookmarksForm(this, openFilename);
loadFileForm.ShowDialog();
i++;
break;
}
i++;
}
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (this.IsBusy)
{
e.Cancel = true;
return;
}
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)
{
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)
{
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
e.Cancel = true;
return;
}
}
}
else
{
// do later
}
}
treeView1.CloseIconDatabase();
treeView1.Clear();
this.ApplicationMode = AppMode.Clear;
this.ClearSession();
Application.Exit();
}
public bool AllowSave
{
get => (saveMenuBarItem.Enabled || saveFileMenuItem.Enabled);
set
{
ThreadControl.SetEnable(toolStrip1, saveMenuBarItem, value);
ThreadControl.SetEnable(toolStrip1, saveFileMenuItem, value);
}
}
public AppMode ApplicationMode
{
get => appMode;
set
{
appMode = value;
switch (value)
{
case AppMode.Clear:
ThreadControl.SetText(this, Resources.app_name);
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);
break;
case AppMode.Open:
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);
break;
case AppMode.New:
ThreadControl.SetText(this, Resources.app_name);
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);
break;
}
}
}
public bool IsBusy
{
get => isBusy;
set
{
treeView1.Enabled = !value;
}
}
public RyzStudio.Windows.Forms.BookmarkTreeView TreeView { get => treeView1; set => treeView1 = value; }
#region toolbar
private void fileNewMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
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)
{
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
}
else
{
// do later
}
}
TextBoxForm newSessionForm = new TextBoxForm("New Session", "Name");
string rv = newSessionForm.ShowDialog();
if (string.IsNullOrWhiteSpace(rv))
{
return;
}
treeView1.Clear(rv);
this.ApplicationMode = AppMode.New;
this.ClearSession();
}
private void fileOpenMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
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)
{
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
}
else
{
// do later
}
}
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
LoadBookmarksForm loadFileForm = new LoadBookmarksForm(this, openFileDialog.FileName);
loadFileForm.ShowDialog();
}
}
private void fileCloseMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
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)
{
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
}
else if (response == DialogResult.No)
{
// do later
}
else if (response == DialogResult.Cancel)
{
return;
}
}
}
else
{
// do later
}
}
treeView1.Clear();
this.ApplicationMode = AppMode.Clear;
this.ClearSession();
}
private void fileSaveMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
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)
{
if (usePassword)
{
if (string.IsNullOrWhiteSpace(sessionPassword))
{
TextBoxForm passwordForm = new TextBoxForm("Password", "Password", true);
sessionPassword = passwordForm.ShowDialog();
}
}
saveBookmarkFile_ForJSNX(sessionFilename, sessionPassword ?? string.Empty);
}
}
private void fileSaveAsMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (this.ApplicationMode == AppMode.Clear)
{
return;
}
bool hasSaved = saveAsBookmarkFile();
if (!hasSaved)
{
return;
}
}
private void fileExitMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
this.Close();
}
private void editFindMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
FindForm findForm = new FindForm(this);
findForm.Show();
}
private void viewExpandAllMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (this.ApplicationMode == AppMode.Clear)
{
return;
}
if (treeView1.SelectedNode == null)
{
treeView1.ExpandAll();
}
else
{
treeView1.SelectedNode.ExpandAll();
}
}
private void viewCollapseAllMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (this.ApplicationMode == AppMode.Clear)
{
return;
}
if (treeView1.SelectedNode == null)
{
treeView1.CollapseAll();
}
else
{
treeView1.SelectedNode.Collapse(false);
}
}
private void viewAlwaysOnTopMenuItem_Click(object sender, EventArgs e)
{
this.TopMost = !this.TopMost;
alwaysOnTopToolStripMenuItem1.Checked = this.TopMost;
}
private void toolsOptionsMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
UpdateIconsForm optionForm = new UpdateIconsForm(this);
optionForm.ShowDialog();
}
private void helpViewHelpMenuItem_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start(Resources.url_help);
}
catch
{
// do nothing
}
}
private void helpAboutMenuItem_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start(Resources.url_about);
}
catch
{
// do nothing
}
}
#endregion
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
}
}
protected bool saveAsBookmarkFile()
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
switch (saveFileDialog.FilterIndex)
{
case 1:
saveBookmarkFile_ForJSNX(saveFileDialog.FileName);
this.Text = Path.GetFileNameWithoutExtension(saveFileDialog.FileName) + " - " + Properties.Resources.app_name;
sessionFilename = saveFileDialog.FileName;
usePassword = false;
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);
this.Text = Path.GetFileNameWithoutExtension(saveFileDialog.FileName) + " - " + Properties.Resources.app_name;
sessionFilename = saveFileDialog.FileName;
sessionPassword = password;
usePassword = true;
this.ApplicationMode = AppMode.Open;
treeView1.HasChanged = false;
break;
default: break;
}
return true;
}
else
{
return false;
}
}
protected void saveBookmarkFile_ForJSNX(string filename, string password = "")
{
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);
zipStream.Password = password;
// stream
MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(rs)));
ms.Position = 0;
// write file entry
zipStream.PutNextEntry(new ZipEntry("bookmarks.json"));
buffer = new byte[size];
bufferSize = 0;
do
{
bufferSize = ms.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, bufferSize);
}
while (bufferSize > 0);
ms.Flush();
ms.Close();
ms.Dispose();
ms = null;
zipStream.Finish();
zipStream.Flush();
zipStream.Close();
zipStream.Dispose();
zipStream = null;
treeView1.HasChanged = false;
}
protected void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
return;
}
switch (treeView1.SNode.GetNodeType())
{
case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Root:
rootTreeNodeMenu.Show(e.Node.TreeView, e.X, e.Y);
break;
case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Folder:
folderTreeNodeMenu.Show(e.Node.TreeView, e.X, e.Y);
break;
case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Page:
pageTreeNodeMenu.Show(e.Node.TreeView, e.X, e.Y);
break;
default:
break;
}
}
protected void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) => openBookmark(e.Node);
protected void treeView1_OnNodeChanged(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.AllowSave = treeView1.HasChanged;
}));
}
else
{
this.AllowSave = treeView1.HasChanged;
}
}
protected void treeView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
TreeNode tn = treeView1.SelectedNode;
if (tn == null)
{
return;
}
NodeType nodeType = treeView1.SNode.GetNodeType();
switch (e.KeyCode)
{
case Keys.Apps:
treeView1_NodeMouseClick(sender, new TreeNodeMouseClickEventArgs(tn, MouseButtons.Right, 1, tn.Bounds.X, tn.Bounds.Y));
return;
case Keys.Enter:
openBookmark(tn);
break;
case Keys.F2:
if (nodeType == NodeType.Page)
{
(new BookmarkEditForm(treeView1)).ShowDialog();
}
break;
case Keys.Insert:
if (e.Modifiers != Keys.Shift)
{
if ((nodeType == NodeType.Root) || (nodeType == NodeType.Folder))
{
(new BookmarkEditForm(treeView1)).ShowDialog();
}
else if (nodeType == NodeType.Page)
{
treeView1.SelectedNode = tn.Parent;
(new BookmarkEditForm(treeView1)).ShowDialog();
}
}
break;
default: break;
}
}
#region root context menu
private void addPageContextMenu_Click(object sender, EventArgs e) => (new BookmarkEditForm(treeView1)).ShowDialog();
private void addFolderContextMenu_Click(object sender, EventArgs e) => treeView1.SelectedNode = treeView1.SNode.AddFolder();
private void editContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.Edit();
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();
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();
private void deleteContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.Delete();
private void sortContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.Sort();
private void moveUpContextMenu_Click(object sender, EventArgs e) => treeView1.SNode.MoveUp();
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);
private void editContextMenu3_Click(object sender, EventArgs e) => (new BookmarkEditForm(treeView1)).ShowDialog();
private void deleteContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.Delete();
private void moveUpContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.MoveUp();
private void moveDownContextMenu2_Click(object sender, EventArgs e) => treeView1.SNode.MoveDown();
#endregion
public void ClearSession()
{
sessionFilename = sessionPassword = null;
usePassword = false;
}
public void SetSessionFile(string filename, string password)
{
sessionFilename = filename;
sessionPassword = password;
usePassword = !string.IsNullOrWhiteSpace(password);
string ext = Path.GetExtension(filename).Trim('.').ToLower();
if (ext.Equals("jsnx"))
{
loadFileType = LoadFileType.Jsnx;
}
else if (ext.Equals("ryz"))
{
loadFileType = LoadFileType.Ryz;
}
else
{
loadFileType = LoadFileType.None;
}
ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(sessionFilename) + " - " + Resources.app_name);
}
}
}