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

789 lines
24 KiB
C#
Raw Normal View History

using bzit.bomg.Models;
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;
using System.Windows.Forms;
2019-04-20 16:05:55 +00:00
using static RyzStudio.Windows.Forms.BookmarkTreeView;
2017-07-30 11:59:34 +00:00
namespace bzit.bomg
{
public partial class MainForm : Form
{
public enum AppMode
{
Clear = 0,
Open,
New
}
protected AppMode appMode = AppMode.Clear;
protected SessionFileFormat sessionFileFormat = null;
protected string sessionFilename = null;
public MainForm()
{
InitializeComponent();
}
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.IconDatabase = new IconDatabase();
// toolbar
viewHelpHelpMenuItem.Enabled = File.Exists(Path.ChangeExtension(Application.ExecutablePath, ".chm"));
//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();
//}
sessionFileFormat = new SessionFileFormat(this);
2017-07-30 11:59:34 +00:00
this.ApplicationMode = AppMode.Clear;
2019-04-19 02:51:55 +00:00
//treeView1.OnNodeCountUpdate = delegate (ulong v) {
// statusBarPanel2.Text = v.ToString();
//};
//treeView1.OnChanged = delegate (object sender, EventArgs ev) {
// if (this.ApplicationMode == AppMode.New)
// {
// saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = false;
// }
// else
// {
// saveMenuBarItem.Enabled = saveFileMenuItem.Enabled = treeView1.HasChanged;
// }
//};
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
string[] szr = Environment.GetCommandLineArgs();
int i = 0;
while (true)
{
if (i > (szr.Length - 1))
{
break;
}
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;
}
i++;
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
//this.IconDatabase?.Close();
2017-07-30 11:59:34 +00:00
if (this.ApplicationMode == AppMode.Clear)
{
Application.Exit();
return;
}
if (!treeView1.HasChanged)
{
this.ApplicationMode = AppMode.Clear;
Application.Exit();
return;
}
if (this.ApplicationMode == AppMode.Open)
{
if (string.IsNullOrEmpty(sessionFilename))
{
this.ApplicationMode = AppMode.Clear;
Application.Exit();
return;
}
if (MessageBox.Show("Save changes to open bookmarks.", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
this.ApplicationMode = AppMode.Clear;
Application.Exit();
return;
}
while (true)
{
bool rv = sessionFileFormat.saveToRyz(sessionFilename);
if (!rv)
{
switch (MessageBox.Show("There was a problem saving bookmarks. Retry?", "Retry?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
{
case DialogResult.Yes:
break;
case DialogResult.No:
this.ApplicationMode = AppMode.Clear;
Application.Exit();
break;
case DialogResult.Cancel:
e.Cancel = true;
break;
default: break;
}
}
else
{
this.ApplicationMode = AppMode.Clear;
Application.Exit();
break;
}
}
}
else if (this.ApplicationMode == AppMode.New)
{
if (MessageBox.Show("Save changes to open bookmarks.", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
bool rt = SaveBookmarkFile();
if (rt)
{
this.ApplicationMode = AppMode.Clear;
Application.Exit();
return;
}
}
else
{
this.ApplicationMode = AppMode.Clear;
Application.Exit();
return;
}
}
}
//#region public properties
2017-07-30 11:59:34 +00:00
public AppMode ApplicationMode
{
get { return appMode; }
set
{
appMode = value;
switch (value)
{
case AppMode.Clear:
this.Text = Properties.Resources.app_name;
statusBarPanel2.Text = string.Empty;
treeView1.Clear();
sessionFilename = null;
closeFileMenuItem.Enabled = false;
saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false;
saveAsFileMenuItem.Enabled = false;
updateIconsToolMenuItem.Enabled = false;
2017-07-30 11:59:34 +00:00
break;
case AppMode.Open:
closeFileMenuItem.Enabled = true;
saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false;
saveAsFileMenuItem.Enabled = true;
updateIconsToolMenuItem.Enabled = true;
break;
case AppMode.New:
this.Text = Properties.Resources.app_name;
statusBarPanel2.Text = string.Empty;
sessionFilename = null;
closeFileMenuItem.Enabled = true;
saveFileMenuItem.Enabled = saveMenuBarItem.Enabled = false;
saveAsFileMenuItem.Enabled = true;
updateIconsToolMenuItem.Enabled = true;
break;
}
}
}
//[DefaultValue(null)]
//public IconDatabase IconDatabase { get; set; }
//#endregion
2017-07-30 11:59:34 +00:00
#region Toolbar #1
2019-04-19 02:51:55 +00:00
/// <summary>
/// File New
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void newMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
NewSessionForm newSessionForm = new NewSessionForm(this);
newSessionForm.ShowDialog();
2017-07-30 11:59:34 +00:00
}
2019-04-19 02:51:55 +00:00
/// <summary>
/// File Open
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void openMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
sessionFilename = openFileDialog.FileName;
OpenBookmarkFile(sessionFilename);
}
}
/**
* File -> Close
*/
private void closeMenuItem_Click(object sender, EventArgs e)
2017-07-30 11:59:34 +00:00
{
if (ApplicationMode == AppMode.Clear)
{
return;
}
if (!treeView1.HasChanged)
{
ApplicationMode = AppMode.Clear;
return;
}
2017-07-30 11:59:34 +00:00
if (ApplicationMode == AppMode.Open)
{
if (string.IsNullOrEmpty(sessionFilename))
{
ApplicationMode = AppMode.Clear;
return;
}
if (MessageBox.Show("Save changes to open bookmarks.", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
ApplicationMode = AppMode.Clear;
return;
}
bool rv = sessionFileFormat.saveToRyz(sessionFilename);
if (!rv)
{
if (MessageBox.Show("There was a problem saving bookmarks. Bookmarks are not saved", "Not Saved", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) return;
}
2017-07-30 11:59:34 +00:00
ApplicationMode = AppMode.Clear;
}
else if (ApplicationMode == AppMode.New)
{
if (MessageBox.Show("Save changes to open bookmarks.", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
ApplicationMode = AppMode.Clear;
return;
}
bool rt = SaveBookmarkFile();
if (rt)
{
ApplicationMode = AppMode.Clear;
}
}
}
/**
* File -> Save
*/
private void importSnapshotToolStripMenuItem_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(sessionFilename))
{
return;
}
bool rv = sessionFileFormat.saveToRyz(sessionFilename);
if (rv)
{
treeView1.HasChanged = false;
}
}
/**
* File -> Save As
*/
private void exportSnapshotToolStripMenuItem_Click(object sender, EventArgs e)
{
if (SaveBookmarkFile())
{
ApplicationMode = AppMode.Open;
treeView1.HasChanged = false;
}
}
/**
* File -> Export
*/
private void toolStripMenuItem7_Click(object sender, EventArgs e) { }
/**
* File -> Exit
*/
2019-04-20 16:05:55 +00:00
private void exitMenuItem_Click(object sender, EventArgs e) => this.Close();
2017-07-30 11:59:34 +00:00
/**
* Find
*/
private void toolStripButton5_Click(object sender, EventArgs e)
{
FindForm frm = new FindForm(this);
frm.Show();
}
/**
* View -> Expand All
*/
private void expandAllToolStripMenuItem_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode == null)
{
treeView1.ExpandAll();
}
else
{
treeView1.SelectedNode.ExpandAll();
}
}
/**
* View -> Collapse All
*/
private void collapseAllToolStripMenuItem_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode == null)
{
treeView1.CollapseAll();
}
else
{
treeView1.SelectedNode.Collapse(false);
}
}
/**
* Tools -> Refresh Icons
*/
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(sessionFilename))
{
return;
}
if (this.ApplicationMode == AppMode.Clear)
{
return;
}
UpdateIconForm frm = new UpdateIconForm(this);
frm.ShowDialog();
}
/**
* Tools -> Options
*/
private void optionsToolStripMenuItem1_Click(object sender, EventArgs e)
{
2019-04-07 19:10:23 +00:00
//OptionsForm frm = new OptionsForm(this);
//frm.ShowDialog();
2017-07-30 11:59:34 +00:00
}
/**
* Help -> Documentation
*/
public void documentationToolStripMenuItem_Click(object sender, EventArgs e)
{
string chm = Path.ChangeExtension(Application.ExecutablePath, ".chm");
if (!File.Exists(chm))
{
return;
}
try
{
System.Diagnostics.Process.Start(chm);
}
catch
{
// do nothing
}
}
/**
* Help -> About
*/
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("Name: " + Properties.Resources.app_name + Environment.NewLine + "Version: " + Properties.Resources.app_version + Environment.NewLine + "Author: " + Properties.Resources.app_author, "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/**
* ? -> Always-On-Top
*/
private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
{
this.TopMost = !this.TopMost;
}
2017-07-30 11:59:34 +00:00
#endregion
protected void OpenBookmarkFile(string filename)
{
treeView1.HasChanged = false;
if (RyzStudio.IO.SharpZipLib.IsZipEncrypted(filename))
{
PasswordForm oPassword = new PasswordForm(this);
sessionFileFormat.passkey = oPassword.ShowDialog();
if (sessionFileFormat.passkey.Equals(""))
{
return;
}
}
else
{
sessionFileFormat.passkey = "";
}
bool rv = sessionFileFormat.loadFromRyz(filename);
if (rv)
{
this.Text = Path.GetFileNameWithoutExtension(filename) + " - " + Properties.Resources.app_name;
this.ApplicationMode = AppMode.Open;
treeView1.HasChanged = false;
}
}
protected bool SaveBookmarkFile()
{
bool rv = false;
2017-07-30 11:59:34 +00:00
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
switch (saveFileDialog.FilterIndex)
{
case 1:
sessionFileFormat.passkey = "";
rv = sessionFileFormat.saveToRyz(saveFileDialog.FileName);
break;
case 2:
PasswordForm oPassword = new PasswordForm(this);
sessionFileFormat.passkey = oPassword.ShowDialog();
rv = sessionFileFormat.saveToRyz(saveFileDialog.FileName);
if (rv)
{
treeView1.HasChanged = false;
}
break;
default: break;
}
2017-07-30 11:59:34 +00:00
}
2017-07-30 11:59:34 +00:00
if (rv)
{
ApplicationMode = AppMode.Open;
sessionFilename = saveFileDialog.FileName;
this.Text = Path.GetFileNameWithoutExtension(saveFileDialog.FileName) + " - " + Properties.Resources.app_name;
}
return rv;
}
2019-04-20 16:05:55 +00:00
2017-07-30 11:59:34 +00:00
private 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(Cursor.Position);
break;
case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Folder:
folderTreeNodeMenu.Show(Cursor.Position);
break;
case RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Page:
pageTreeNodeMenu.Show(Cursor.Position);
break;
default:
break;
}
2017-07-30 11:59:34 +00:00
}
2019-04-19 02:51:55 +00:00
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) => openBookmark(e.Node);
2017-07-30 11:59:34 +00:00
private void treeView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
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);
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;
}
}
2017-07-30 11:59:34 +00:00
protected void openBookmark(TreeNode node)
{
if (treeView1.GetNodeType(node) != RyzStudio.Windows.Forms.BookmarkTreeView.NodeType.Page)
2017-07-30 11:59:34 +00:00
{
return;
}
BookmarkItemViewModel viewModel = (BookmarkItemViewModel)node.Tag;
if (viewModel == null)
2017-07-30 11:59:34 +00:00
{
return;
}
if (string.IsNullOrWhiteSpace(viewModel.SiteAddress))
2017-07-30 11:59:34 +00:00
{
return;
}
try
2017-07-30 11:59:34 +00:00
{
System.Diagnostics.Process.Start(viewModel.SiteAddress);
2017-07-30 11:59:34 +00:00
}
catch
2017-07-30 11:59:34 +00:00
{
// do nothing
2017-07-30 11:59:34 +00:00
}
//if (node == null)
//{
// return;
//}
2017-07-30 11:59:34 +00:00
//if (node.Tag == null)
//{
// return;
//}
//if (!(node.Tag is BookmarkItem))
//{
// return;
//}
//BookmarkItem item = (BookmarkItem)node.Tag;
//if (item == null)
//{
// return;
//}
//if (string.IsNullOrEmpty(item.SiteAddress))
//{
// return;
//}
//int bookmarkAction;
//if (!int.TryParse(this.IconDatabase.GetConfig("core.bookmark.action", string.Empty), out bookmarkAction))
//{
// bookmarkAction = 0;
//}
//string bookmarkCustom1 = this.IconDatabase.GetConfig("core.bookmark.customcommand1", string.Empty).Trim();
//string bookmarkCustom2 = this.IconDatabase.GetConfig("core.bookmark.customcommand2", string.Empty).Trim();
//switch (bookmarkAction)
//{
// case 1:
// if (string.IsNullOrEmpty(bookmarkCustom1))
// {
// return;
// }
// bookmarkCustom1 = bookmarkCustom1.Replace("%1", item.SiteAddress);
// bookmarkCustom2 = bookmarkCustom2.Replace("%1", item.SiteAddress);
// try
// {
// System.Diagnostics.Process.Start(bookmarkCustom1, bookmarkCustom2);
// }
// catch
// {
// // do nothing
// }
// break;
// default:
// try
// {
// System.Diagnostics.Process.Start(item.SiteAddress);
// }
// catch
// {
// // do nothing
// }
// 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();
2019-04-20 16:05:55 +00:00
#endregion
2019-04-20 16:05:55 +00:00
private void toolStripButton1_Click(object sender, EventArgs e)
{
List<BookmarkItemViewModel> rs = treeView1.GetBookmarkList();
2019-04-20 16:05:55 +00:00
//MessageBox.Show(treeView1.GetNodePath(treeView1.SelectedNode));
MessageBox.Show("!");
}
2017-07-30 11:59:34 +00:00
}
}