bookmark-manager-r4/MainForm.cs

1233 lines
37 KiB
C#
Raw Normal View History

2021-09-07 11:32:24 +00:00
using BookmarkManager;
2021-09-12 13:40:44 +00:00
using bzit.bomg.Models;
2021-09-07 11:32:24 +00:00
using FizzyLauncher.Models;
2021-09-09 21:39:23 +00:00
using Newtonsoft.Json;
2021-09-07 11:32:24 +00:00
using RyzStudio.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
2021-09-21 12:28:52 +00:00
using System.Drawing;
2021-09-07 11:32:24 +00:00
using System.IO;
using System.Linq;
using System.Reflection;
2021-09-29 15:20:46 +00:00
using System.Text;
2021-09-07 11:32:24 +00:00
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FizzyLauncher
{
public partial class MainForm : Form
{
public enum AppMode
{
Clear = 0,
Open,
New
}
2021-09-29 00:58:02 +00:00
2021-09-21 12:28:52 +00:00
protected IconDatabase iconDatabase = null;
2021-09-30 21:26:17 +00:00
protected UpdateIconsForm updateIconsForm = null;
2021-09-07 11:32:24 +00:00
protected OptionsForm optionsForm = null;
2021-09-12 14:25:37 +00:00
protected FindForm findForm = null;
2021-09-07 11:32:24 +00:00
protected AppMode appMode = AppMode.Clear;
protected string sessionFilename = null;
protected string sessionPassword = null;
protected bool isBusy = false;
2021-09-09 21:39:23 +00:00
protected readonly string jsonfigFilename;
2021-09-07 11:32:24 +00:00
public MainForm()
{
InitializeComponent();
2021-09-21 12:28:52 +00:00
jsonfigFilename = Path.ChangeExtension(Application.ExecutablePath, "jsonfig");
if (iconDatabase == null) iconDatabase = new IconDatabase(Path.ChangeExtension(Application.ExecutablePath, "db"));
2021-09-07 11:32:24 +00:00
this.AutoScaleMode = AutoScaleMode.None;
this.StartPosition = FormStartPosition.WindowsDefaultLocation;
this.ClientSize = new System.Drawing.Size(300, 580);
//this.Visible = false;
2021-09-21 13:53:18 +00:00
treeView1.IconDatabase = iconDatabase;
2021-09-12 13:40:44 +00:00
treeView1.RootContextMenu = rootContextMenu;
treeView1.FolderContextMenu = folderContextMenu;
treeView1.PageContextMenu = pageContextMenu;
treeView1.NodeMouseDoubleClick += treeView1_NodeMouseDoubleClick;
2021-09-09 21:39:23 +00:00
treeView1.OnNodeChanged += treeView1_OnNodeChanged;
2021-09-12 13:40:44 +00:00
treeView1.PreviewKeyDown += treeView1_PreviewKeyDown;
2021-09-07 11:32:24 +00:00
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//ThreadControl.SetVisible(this, false);
ThreadControl.SetSize(this, 300, 580);
2021-09-09 21:39:23 +00:00
2021-09-29 15:20:46 +00:00
List<string> supportedFileExtensions = GetSupportedFileTypes();
openFileDialog1.Filter = BuildOpenFileSupportedFiles(supportedFileExtensions);
saveFileDialog1.Filter = BuildSaveFileSupportedFiles(supportedFileExtensions);
2021-09-09 21:39:23 +00:00
ApplicationMode = AppMode.Clear;
2021-09-07 11:32:24 +00:00
}
2021-09-09 21:39:23 +00:00
protected async override void OnShown(EventArgs e)
{
base.OnShown(e);
2021-09-07 11:32:24 +00:00
//ThreadControl.SetVisible(this, false);
2021-09-09 21:39:23 +00:00
await LoadAppSession(jsonfigFilename);
2021-09-07 11:32:24 +00:00
2021-09-09 21:39:23 +00:00
InvalidateAppSession();
2021-09-29 15:49:09 +00:00
string[] commandLineArgs = Environment.GetCommandLineArgs();
bool hasCommands = await LoadCommandLine(commandLineArgs);
if (!hasCommands)
{
newToolStripMenuItem_Click(null, null);
}
2021-09-09 21:39:23 +00:00
}
2021-09-07 11:32:24 +00:00
2021-09-07 16:21:28 +00:00
protected async override void OnClosing(CancelEventArgs e)
2021-09-07 11:32:24 +00:00
{
2021-09-07 16:21:28 +00:00
e.Cancel = true;
if (this.IsBusy)
{
return;
}
this.IsBusy = true;
Result result = await CloseFile();
if (!result.IsSuccess)
{
2021-09-09 21:39:23 +00:00
if (!string.IsNullOrWhiteSpace(result.Message))
{
MessageBox.Show(result.Message, "Close", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2021-09-07 16:21:28 +00:00
this.IsBusy = false;
e.Cancel = true;
return;
}
sessionFilename = null;
sessionPassword = null;
treeView1.Clear();
2021-09-21 12:28:52 +00:00
iconDatabase?.Close();
2021-09-07 11:32:24 +00:00
2021-09-07 16:21:28 +00:00
ApplicationMode = AppMode.Clear;
this.IsBusy = false;
Application.Exit();
2021-09-07 11:32:24 +00:00
}
2021-09-09 21:39:23 +00:00
public AppSession CurrentSession { get; set; } = null;
2021-09-07 11:32:24 +00:00
public bool IsBusy
{
get => isBusy;
set
{
treeView1.Enabled = !value;
}
}
protected AppMode ApplicationMode
{
get => appMode;
set
{
appMode = value;
switch (value)
{
case AppMode.Clear:
ThreadControl.SetText(this, AppResource.app_name);
ThreadControl.SetEnable(closeToolStripMenuItem, false);
ThreadControl.SetEnable(saveToolStripMenuItem, false);
ThreadControl.SetEnable(saveAsToolStripMenuItem, false);
2021-09-07 16:21:28 +00:00
ThreadControl.SetEnable(findToolStripMenuItem, false);
2021-09-30 21:26:17 +00:00
ThreadControl.SetEnable(toolStripMenuItem9, false);
2021-09-07 11:32:24 +00:00
ThreadControl.SetEnable(expandAllToolStripMenuItem, false);
ThreadControl.SetEnable(collapseAllToolStripMenuItem, false);
break;
case AppMode.Open:
ThreadControl.SetEnable(closeToolStripMenuItem, true);
ThreadControl.SetEnable(saveToolStripMenuItem, true);
ThreadControl.SetEnable(saveAsToolStripMenuItem, true);
2021-09-07 16:21:28 +00:00
ThreadControl.SetEnable(findToolStripMenuItem, true);
2021-09-30 21:26:17 +00:00
ThreadControl.SetEnable(toolStripMenuItem9, true);
2021-09-07 11:32:24 +00:00
ThreadControl.SetEnable(expandAllToolStripMenuItem, true);
ThreadControl.SetEnable(collapseAllToolStripMenuItem, true);
break;
case AppMode.New:
ThreadControl.SetText(this, AppResource.app_name);
ThreadControl.SetEnable(closeToolStripMenuItem, true);
ThreadControl.SetEnable(saveToolStripMenuItem, false);
ThreadControl.SetEnable(saveAsToolStripMenuItem, true);
2021-09-07 16:21:28 +00:00
ThreadControl.SetEnable(findToolStripMenuItem, true);
2021-09-30 21:26:17 +00:00
ThreadControl.SetEnable(toolStripMenuItem9, true);
2021-09-07 11:32:24 +00:00
ThreadControl.SetEnable(expandAllToolStripMenuItem, true);
ThreadControl.SetEnable(collapseAllToolStripMenuItem, true);
break;
2021-09-07 16:21:28 +00:00
default: break;
2021-09-07 11:32:24 +00:00
}
}
}
#region main menu
/// <summary>
/// New
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2021-09-07 16:21:28 +00:00
private async void newToolStripMenuItem_Click(object sender, EventArgs e)
2021-09-07 11:32:24 +00:00
{
if (this.IsBusy)
{
return;
}
this.IsBusy = true;
2021-09-07 16:21:28 +00:00
Result result = await CloseFile();
if (!result.IsSuccess)
{
2021-09-09 21:39:23 +00:00
if (!string.IsNullOrWhiteSpace(result.Message))
{
MessageBox.Show(result.Message, "New Session", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2021-09-07 16:21:28 +00:00
this.IsBusy = false;
return;
}
2021-09-07 11:32:24 +00:00
sessionFilename = null;
sessionPassword = null;
treeView1.Clear("Untitled");
ApplicationMode = AppMode.New;
this.IsBusy = false;
}
/// <summary>
/// Open file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
this.IsBusy = true;
2021-09-07 16:21:28 +00:00
Result result = await CloseFile();
if (!result.IsSuccess)
{
2021-09-09 21:39:23 +00:00
if (!string.IsNullOrWhiteSpace(result.Message))
{
MessageBox.Show(result.Message, "Open File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2021-09-07 16:21:28 +00:00
this.IsBusy = false;
return;
}
2021-09-07 11:32:24 +00:00
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
2021-09-07 16:21:28 +00:00
result = await LoadFile(openFileDialog1.FileName);
if (result.IsSuccess)
{
ThreadControl.SetText(this, Path.GetFileNameWithoutExtension(openFileDialog1.FileName) + " - " + AppResource.app_name);
}
else
2021-09-07 11:32:24 +00:00
{
MessageBox.Show(result.Message, "Open File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
this.IsBusy = false;
}
/// <summary>
/// Close
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2021-09-07 16:21:28 +00:00
private async void closeToolStripMenuItem_Click(object sender, EventArgs e)
2021-09-07 11:32:24 +00:00
{
if (this.IsBusy)
{
return;
}
this.IsBusy = true;
2021-09-07 16:21:28 +00:00
Result result = await CloseFile();
if (!result.IsSuccess)
{
2021-09-09 21:39:23 +00:00
if (!string.IsNullOrWhiteSpace(result.Message))
{
MessageBox.Show(result.Message, "Close File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2021-09-07 16:21:28 +00:00
this.IsBusy = false;
return;
}
2021-09-07 11:32:24 +00:00
sessionFilename = null;
sessionPassword = null;
treeView1.Clear();
ApplicationMode = AppMode.Clear;
this.IsBusy = false;
}
/// <summary>
/// Save
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (ApplicationMode == AppMode.Clear)
{
return;
}
this.IsBusy = true;
Result result = await SaveFile(sessionFilename, sessionPassword);
if (result.IsSuccess)
{
MessageBox.Show("File saved!", "Save File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(result.Message, "Save File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.IsBusy = false;
}
/// <summary>
/// Save As
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (ApplicationMode == AppMode.Clear)
{
return;
}
this.IsBusy = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Result result = await SaveAsFile(saveFileDialog1.FileName);
if (result.IsSuccess)
{
MessageBox.Show("File saved!", "Save As File", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(result.Message, "Save As File", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
this.IsBusy = false;
}
/// <summary>
/// Exit
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
this.Close();
}
/// <summary>
/// Find
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
2021-09-12 14:25:37 +00:00
if (this.IsBusy)
{
return;
}
2021-09-14 19:36:04 +00:00
if (findForm == null) findForm = new FindForm(treeView1);
2021-09-12 14:25:37 +00:00
findForm.Show();
2021-09-07 11:32:24 +00:00
}
/// <summary>
/// Expand all
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void expandAllToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy) return;
if (ApplicationMode == AppMode.Clear) return;
if (treeView1.SelectedNode == null)
{
treeView1.ExpandAll();
}
else
{
treeView1.SelectedNode.ExpandAll();
}
}
/// <summary>
/// Collapse all
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void collapseAllToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.IsBusy) return;
if (ApplicationMode == AppMode.Clear) return;
if (treeView1.SelectedNode == null)
{
treeView1.CollapseAll();
}
else
{
treeView1.SelectedNode.Collapse(false);
}
}
/// <summary>
/// Always on top
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
{
this.TopMost = !this.TopMost;
alwaysOnTopToolStripMenuItem.Checked = this.TopMost;
}
2021-09-30 21:26:17 +00:00
/// <summary>
/// Update icons
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
if (updateIconsForm == null) updateIconsForm = new UpdateIconsForm(treeView1, iconDatabase);
updateIconsForm.ShowDialog();
}
2021-09-07 11:32:24 +00:00
/// <summary>
/// Options
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2021-09-09 21:39:23 +00:00
private async void optionsToolStripMenuItem_Click(object sender, EventArgs e)
2021-09-07 11:32:24 +00:00
{
if (this.IsBusy)
{
return;
}
if (optionsForm == null) optionsForm = new OptionsForm(this);
2021-09-09 21:39:23 +00:00
if (optionsForm.ShowDialog() == DialogResult.OK)
{
await SaveAppSession(jsonfigFilename);
}
2021-09-07 11:32:24 +00:00
2021-09-09 21:39:23 +00:00
InvalidateAppSession();
2021-09-07 11:32:24 +00:00
}
/// <summary>
/// View help
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void viewHelpToolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("https://www.hiimray.co.uk/software-bookmark-manager");
}
catch
{
// do nothing
}
}
/// <summary>
/// About
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show(Application.ProductName + " v" + Application.ProductVersion, "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
2021-09-07 16:21:28 +00:00
#endregion
2021-09-07 11:32:24 +00:00
2021-09-12 13:40:44 +00:00
#region context menu
2021-09-07 11:32:24 +00:00
2021-09-12 13:40:44 +00:00
/// <summary>
/// Add page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addPageToolStripMenuItem_Click(object sender, EventArgs e)
2021-09-09 21:39:23 +00:00
{
2021-09-12 13:40:44 +00:00
if (this.IsBusy)
2021-09-09 21:39:23 +00:00
{
return;
}
if (treeView1.GetNodeType() == BookmarkTreeView.NodeType.Page)
{
return;
}
if (treeView1.SelectedNode == null)
{
return;
}
// add placeholder
TreeNode node = treeView1.AddItem(treeView1.SelectedNode, new BookmarkItem()
{
SiteName = "New Bookmark"
});
node.EnsureVisible();
treeView1.SelectedNode = node;
UpdateBookmarkNode(true);
2021-09-09 21:39:23 +00:00
}
2021-09-12 13:40:44 +00:00
/// <summary>
/// Add folder
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2021-09-29 15:20:46 +00:00
private void addFolderToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.AddFolder();
2021-09-12 13:40:44 +00:00
/// <summary>
/// Edit root node
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void editToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.EditNode();
/// <summary>
/// Sort
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sortToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.Sort();
/// <summary>
/// Add page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addPageToolStripMenuItem1_Click(object sender, EventArgs e) => addPageToolStripMenuItem_Click(sender, e);
2021-09-09 21:39:23 +00:00
2021-09-12 13:40:44 +00:00
/// <summary>
/// Add folder
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
2021-09-29 15:20:46 +00:00
private void addFolderToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.AddFolder();
2021-09-12 13:40:44 +00:00
/// <summary>
/// Open all pages
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void openAllToolStripMenuItem_Click(object sender, EventArgs e)
2021-09-09 21:39:23 +00:00
{
2021-09-12 13:40:44 +00:00
if (this.IsBusy)
{
return;
}
2021-09-09 21:39:23 +00:00
2021-09-12 13:40:44 +00:00
if (treeView1.SelectedNode == null)
2021-09-09 21:39:23 +00:00
{
2021-09-12 13:40:44 +00:00
return;
2021-09-09 21:39:23 +00:00
}
2021-09-12 13:40:44 +00:00
if (treeView1.SelectedNode.Nodes.Count <= 0)
{
return;
}
foreach (TreeNode item in treeView1.SelectedNode.Nodes)
{
await OpenBookmark(item);
}
2021-09-09 21:39:23 +00:00
}
2021-09-12 13:40:44 +00:00
/// <summary>
/// Edit folder name
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void editToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.EditNode();
/// <summary>
/// Delete folder and contents
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.DeleteNode();
/// <summary>
/// Sort children
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void sortToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.Sort();
/// <summary>
/// Move up
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void moveUpToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.MoveUp();
/// <summary>
/// Move down
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void moveDownToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.MoveDown();
/// <summary>
/// Open page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void openToolStripMenuItem1_Click(object sender, EventArgs e) => await OpenBookmark(treeView1.SelectedNode);
/// <summary>
/// Edit page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void editToolStripMenuItem2_Click(object sender, EventArgs e)
2021-09-09 21:39:23 +00:00
{
if (this.IsBusy)
{
return;
}
2021-09-09 21:39:23 +00:00
BookmarkTreeView.NodeType nodeType = treeView1.GetNodeType();
if (nodeType == BookmarkTreeView.NodeType.Page)
{
UpdateBookmarkNode(false);
}
2021-09-12 13:40:44 +00:00
}
2021-09-09 21:39:23 +00:00
2021-09-12 13:40:44 +00:00
/// <summary>
/// Delete page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void deleteToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.DeleteNode();
2021-09-09 21:39:23 +00:00
2021-09-12 13:40:44 +00:00
/// <summary>
/// Move up
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void moveUpToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.MoveUp();
2021-09-09 21:39:23 +00:00
2021-09-12 13:40:44 +00:00
/// <summary>
/// Move down
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void moveDownToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.MoveDown();
2021-09-09 21:39:23 +00:00
2021-09-12 13:40:44 +00:00
#endregion
2021-09-29 00:58:02 +00:00
2021-09-12 13:40:44 +00:00
private async void treeView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
TreeNode tn = treeView1.SelectedNode;
if (tn == null)
{
return;
}
BookmarkTreeView.NodeType nodeType = treeView1.GetNodeType();
switch (e.KeyCode)
{
case Keys.Enter:
await OpenBookmark(tn);
break;
case Keys.F2:
editToolStripMenuItem2_Click(sender, null);
2021-09-12 13:40:44 +00:00
break;
case Keys.Insert:
if (e.Modifiers != Keys.Shift)
2021-09-09 21:39:23 +00:00
{
2021-09-12 13:40:44 +00:00
if ((nodeType == BookmarkTreeView.NodeType.Root) || (nodeType == BookmarkTreeView.NodeType.Folder))
{
2021-09-14 19:36:04 +00:00
// do nothing
2021-09-12 13:40:44 +00:00
}
else if (nodeType == BookmarkTreeView.NodeType.Page)
{
treeView1.SelectedNode = tn.Parent;
2021-09-14 19:36:04 +00:00
}
addPageToolStripMenuItem_Click(sender, null);
2021-09-09 21:39:23 +00:00
}
2021-09-12 13:40:44 +00:00
break;
default: break;
}
2021-09-09 21:39:23 +00:00
}
2021-09-12 13:40:44 +00:00
private async void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) => await OpenBookmark(e.Node);
2021-09-09 21:39:23 +00:00
2021-09-12 13:40:44 +00:00
private void treeView1_OnNodeChanged(object sender, EventArgs e)
{
ThreadControl.SetEnable(saveToolStripMenuItem, (treeView1.HasChanged && ApplicationMode == AppMode.Open));
2021-09-09 21:39:23 +00:00
}
2021-09-29 15:20:46 +00:00
protected string BuildOpenFileSupportedFiles(List<string> supportList)
{
StringBuilder rs = new StringBuilder();
rs.Append("All supported files (*" + string.Join("; *", supportList) + ")");
rs.Append("|*" + string.Join(";*", supportList));
for (int i = 0; i < supportList.Count; i++)
{
rs.Append("|" + supportList[i].TrimStart('.')?.ToUpper() + " Bookmark files");
rs.Append("|*" + supportList[i]);
}
return rs.ToString();
}
protected string BuildSaveFileSupportedFiles(List<string> supportList)
{
StringBuilder rs = new StringBuilder();
for (int i = 0; i < supportList.Count; i++)
{
if (i > 0) rs.Append("|");
rs.Append(supportList[i].TrimStart('.')?.ToUpper() + " Bookmark file");
rs.Append("|*" + supportList[i]);
}
return rs.ToString();
}
2021-09-12 13:40:44 +00:00
2021-09-07 16:21:28 +00:00
protected async Task<Result> CloseFile()
2021-09-07 11:32:24 +00:00
{
2021-09-07 16:21:28 +00:00
return await Task.Run<Result>(async () =>
2021-09-07 11:32:24 +00:00
{
2021-09-07 16:21:28 +00:00
if (this.ApplicationMode == AppMode.Clear)
2021-09-07 11:32:24 +00:00
{
2021-09-07 16:21:28 +00:00
return Result.Create(true);
2021-09-07 11:32:24 +00:00
}
2021-09-07 16:21:28 +00:00
if (!treeView1.HasChanged)
2021-09-07 11:32:24 +00:00
{
2021-09-07 16:21:28 +00:00
return Result.Create(true);
2021-09-07 11:32:24 +00:00
}
2021-09-07 16:21:28 +00:00
if (this.ApplicationMode == AppMode.New)
2021-09-07 11:32:24 +00:00
{
2021-09-07 16:21:28 +00:00
DialogResult response = MessageBox.Show("Save bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
2021-09-07 11:32:24 +00:00
{
2021-09-07 16:21:28 +00:00
Result result = await SaveAsFile(sessionFilename);
if (!result.IsSuccess)
{
return Result.Create(false, result.Message);
}
}
else if (response == DialogResult.Cancel)
{
2021-09-09 21:39:23 +00:00
return Result.Create(false, "");
2021-09-07 11:32:24 +00:00
}
}
2021-09-07 16:21:28 +00:00
else if (this.ApplicationMode == AppMode.Open)
2021-09-07 11:32:24 +00:00
{
2021-09-07 16:21:28 +00:00
DialogResult response = MessageBox.Show("Save changes to open bookmarks", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (response == DialogResult.Yes)
{
Result result = await SaveFile(sessionFilename, sessionPassword);
if (!result.IsSuccess)
{
return Result.Create(false, result.Message);
}
}
else if (response == DialogResult.Cancel)
{
2021-09-09 21:39:23 +00:00
return Result.Create(false, "");
2021-09-07 16:21:28 +00:00
}
2021-09-07 11:32:24 +00:00
}
2021-09-07 16:21:28 +00:00
return Result.Create(true);
});
2021-09-07 11:32:24 +00:00
}
protected SupportedFileBase GetSupportedFileHandler(string filename)
{
SupportedFileBase rs = null;
List<Type> typeList = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsSubclassOf(typeof(SupportedFileBase))).ToList();
foreach (Type t in typeList)
{
rs = (SupportedFileBase)Activator.CreateInstance(t);
if (rs == null)
{
continue;
}
if (rs.IsSupported(filename))
{
break;
}
rs = null;
}
return rs;
}
2021-09-29 15:20:46 +00:00
protected List<string> GetSupportedFileTypes()
{
List<string> rs = new List<string>();
List<Type> typeList = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsSubclassOf(typeof(SupportedFileBase))).ToList();
foreach (Type t in typeList)
{
SupportedFileBase fileHandler = (SupportedFileBase)Activator.CreateInstance(t);
if (fileHandler == null)
{
continue;
}
rs.AddRange(fileHandler.SupportedExtensions ?? new List<string>());
}
return rs;
}
2021-09-12 13:40:44 +00:00
protected void InvalidateAppSession()
{
if (CurrentSession == null) CurrentSession = new AppSession();
if (CurrentSession.EnableAutoPosition)
{
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}
this.TopMost = CurrentSession.AlwaysOnTop;
}
protected async Task<bool> LoadAppSession(string filename)
{
return await Task.Run(() =>
{
if (File.Exists(filename))
{
string sourceCode = null;
try
{
sourceCode = File.ReadAllText(filename);
}
catch (Exception)
{
// do nothing
}
if (string.IsNullOrWhiteSpace(sourceCode))
{
this.CurrentSession = new AppSession();
return false;
}
try
{
this.CurrentSession = JsonConvert.DeserializeObject<AppSession>(sourceCode);
}
catch (Exception)
{
this.CurrentSession = null;
}
if (this.CurrentSession == null)
{
this.CurrentSession = new AppSession();
return false;
}
}
else
{
this.CurrentSession = new AppSession();
try
{
File.WriteAllText(filename, JsonConvert.SerializeObject(this.CurrentSession));
}
catch (Exception)
{
// do nothing
}
}
return true;
});
}
2021-09-29 15:49:09 +00:00
protected async Task<bool> LoadCommandLine(string[] args)
{
bool rs = false;
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;
await LoadFile(openFilename);
//LoadBookmarksForm loadFileForm = new LoadBookmarksForm(this, openFilename);
//loadFileForm.ShowDialog();
rs = true;
i++;
break;
}
i++;
}
}
return rs;
}
2021-09-07 11:32:24 +00:00
protected async Task<Result> LoadFile(string filename)
{
return await Task.Run(() =>
{
sessionFilename = null;
sessionPassword = null;
ApplicationMode = AppMode.Clear;
treeView1.Clear();
if (string.IsNullOrWhiteSpace(filename))
{
return Result.Create(false, "Filename is empty");
}
if (!File.Exists(filename))
{
return Result.Create(false, "File not found");
}
sessionFilename = filename;
SupportedFileBase fileHandler = GetSupportedFileHandler(sessionFilename);
if (fileHandler == null)
{
return Result.Create(false, "Handler not found for this file type");
}
if (fileHandler.IsEncrypted(sessionFilename))
{
2021-09-14 19:36:04 +00:00
PasswordForm passwordForm = new PasswordForm();
2021-09-07 11:32:24 +00:00
if (passwordForm.ShowDialog() == DialogResult.OK)
{
sessionPassword = passwordForm.Password;
}
}
Result result = fileHandler.Load(treeView1, sessionFilename, sessionPassword);
if (!result.IsSuccess)
{
return result;
}
if (treeView1.Nodes.Count > 0)
{
ThreadControl.Expand(treeView1.Nodes[0]);
}
ApplicationMode = AppMode.Open;
2021-09-09 21:39:23 +00:00
if (result.IsSuccess)
{
treeView1.SetNoChanges();
}
ThreadControl.SetFocus(treeView1);
2021-09-07 11:32:24 +00:00
return result;
});
}
2021-09-12 13:40:44 +00:00
protected async Task OpenBookmark(TreeNode node)
{
await Task.Run(() =>
{
if (BookmarkTreeView.GetNodeType(node) != BookmarkTreeView.NodeType.Page)
{
return;
}
2021-09-17 15:37:01 +00:00
BookmarkItem viewModel = (BookmarkItem)node.Tag;
2021-09-12 13:40:44 +00:00
if (viewModel == null)
{
return;
}
if (string.IsNullOrWhiteSpace(viewModel.SiteAddress))
{
return;
}
string cmd = (string.IsNullOrWhiteSpace(CurrentSession.RunCommand) ? viewModel.SiteAddress : CurrentSession.RunCommand.Replace("{0}", viewModel.SiteAddress));
try
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo
{
FileName = cmd,
UseShellExecute = true
};
System.Diagnostics.Process.Start(psi);
}
catch (Exception exc)
{
#if DEBUG
MessageBox.Show(exc.Message);
#endif
}
});
}
2021-09-07 11:32:24 +00:00
protected async Task<Result> SaveFile(string filename, string password = null)
{
return await Task.Run(() =>
{
if (string.IsNullOrWhiteSpace(filename))
{
return Result.Create(false, "Filename is empty");
}
SupportedFileBase fileHandler = GetSupportedFileHandler(filename);
if (fileHandler == null)
{
return Result.Create(false, "Handler not found for this file type");
}
Result result = fileHandler.Save(treeView1, filename, password);
if (result.IsSuccess)
{
treeView1.SetNoChanges();
}
return result;
});
}
2021-09-12 13:40:44 +00:00
protected async Task<bool> SaveAppSession(string filename)
{
return await Task.Run(() =>
{
if (this.CurrentSession == null) this.CurrentSession = new AppSession();
try
{
File.WriteAllText(filename, JsonConvert.SerializeObject(this.CurrentSession));
}
catch (Exception)
{
return false;
}
return true;
});
}
2021-09-07 11:32:24 +00:00
protected async Task<Result> SaveAsFile(string filename)
{
return await Task.Run(() =>
{
if (string.IsNullOrWhiteSpace(filename))
{
return Result.Create(false, "Filename is empty");
}
SupportedFileBase fileHandler = GetSupportedFileHandler(filename);
if (fileHandler == null)
{
return Result.Create(false, "Handler not found for this file type");
}
string password = null;
if (fileHandler.IsEncryptionSupported)
{
2021-09-14 19:36:04 +00:00
PasswordForm passwordForm = new PasswordForm();
2021-09-07 11:32:24 +00:00
if (passwordForm.ShowDialog() == DialogResult.OK)
{
if (!string.IsNullOrWhiteSpace(passwordForm.Password))
{
password = passwordForm.Password;
}
}
}
Result result = fileHandler.Save(treeView1, filename, password);
if (result.IsSuccess)
{
treeView1.SetNoChanges();
}
return result;
});
}
2021-09-21 14:27:00 +00:00
protected void UpdateBookmarkNode(bool deleteOnCancel)
{
BookmarkItem model = treeView1.GetNodeModel();
Image icon = null;
// retrieve icon from DB
2021-09-30 22:09:55 +00:00
string iconID = model?.ToHash();
2021-09-21 14:27:00 +00:00
2021-09-30 22:09:55 +00:00
icon = iconDatabase.FindIcon(iconID);
//if (!string.IsNullOrWhiteSpace(model.SiteAddress))
//{
// string iconID = model?.ToHash();
// icon = iconDatabase.FindIcon(iconID);
//}
2021-09-21 14:27:00 +00:00
BookmarkForm bookmarkForm = new BookmarkForm(model, icon);
if (bookmarkForm.ShowDialog() == DialogResult.OK)
{
2021-09-30 22:09:55 +00:00
//string iconID = bookmarkForm.Model.Item?.ToHash();
iconID = bookmarkForm.Model.Item?.ToHash();
2021-09-21 14:27:00 +00:00
if (!string.IsNullOrWhiteSpace(iconID))
{
iconDatabase.AddIcon(iconID, bookmarkForm.Model.Icon);
2021-09-21 14:27:00 +00:00
}
treeView1.UpdateItem(treeView1.SelectedNode, bookmarkForm.Model.Item);
}
else
{
if (deleteOnCancel)
{
treeView1.SelectedNode.Remove();
}
}
}
2021-09-12 13:40:44 +00:00
2021-09-07 11:32:24 +00:00
}
}