Added: JSON file support

This commit is contained in:
Ray 2021-09-29 16:20:46 +01:00
parent 646db1de56
commit dc73bb187e
4 changed files with 149 additions and 2 deletions

View File

@ -1,4 +1,5 @@
using RyzStudio.Windows.Forms;
using System.Collections.Generic;
namespace BookmarkManager
{
@ -8,6 +9,7 @@ namespace BookmarkManager
bool IsEncryptionSupported { get; set; }
List<string> SupportedExtensions { get; }
bool IsSupported(string filename);

View File

@ -0,0 +1,89 @@
using bzit.bomg.Models;
using Newtonsoft.Json;
using RyzStudio.IO;
using RyzStudio.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace BookmarkManager
{
public class JSONSupportedFile : SupportedFileBase
{
public JSONSupportedFile()
{
supportedExtensions = new List<string>() { ".json" };
}
public override bool IsEncryptionSupported { get; set; } = false;
public override bool IsEncrypted(string filename) => false;
public override Result Load(BookmarkTreeView treeview, string filename, string password)
{
treeview.Clear();
string sourceCode = null;
try
{
sourceCode = System.IO.File.ReadAllText(filename);
}
catch (Exception exc)
{
return Result.Create(false, "Could not read file (" + exc.Message + ")");
}
if (string.IsNullOrWhiteSpace(sourceCode))
{
return Result.Create(false, "Could not read file, unexpected format");
}
List<BookmarkItem> rs = JsonConvert.DeserializeObject<List<BookmarkItem>>(sourceCode);
if (rs == null)
{
return Result.Create(false, "Could not read file, incorrect format");
}
for (int i = 0; i < rs.Count; i++)
{
if (treeview.InvokeRequired)
{
treeview.Invoke(new MethodInvoker(() => {
treeview.AddItem(rs[i]);
}));
}
else
{
treeview.AddItem(rs[i]);
}
}
return Result.Create(true);
}
public override Result Save(BookmarkTreeView treeview, string filename, string password)
{
List<BookmarkItem> rs = treeview.GetBookmarkList();
if (rs.Count <= 0)
{
return Result.Create(false, "No bookmarks to save");
}
try
{
System.IO.File.WriteAllText(filename, JsonConvert.SerializeObject(rs));
}
catch (Exception exc)
{
return Result.Create(false, "Could not write file (" + exc.Message + ")");
}
return Result.Create(true);
}
}
}

View File

@ -16,6 +16,8 @@ namespace BookmarkManager
public virtual bool IsEncryptionSupported { get; set; } = false;
public List<string> SupportedExtensions { get => supportedExtensions; }
public virtual bool IsSupported(string filename)
{

View File

@ -10,6 +10,7 @@ using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -66,6 +67,11 @@ namespace FizzyLauncher
//ThreadControl.SetVisible(this, false);
ThreadControl.SetSize(this, 300, 580);
List<string> supportedFileExtensions = GetSupportedFileTypes();
openFileDialog1.Filter = BuildOpenFileSupportedFiles(supportedFileExtensions);
saveFileDialog1.Filter = BuildSaveFileSupportedFiles(supportedFileExtensions);
ApplicationMode = AppMode.Clear;
}
@ -547,7 +553,7 @@ namespace FizzyLauncher
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addFolderToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.AddFolder("New Folder");
private void addFolderToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.AddFolder();
/// <summary>
/// Edit root node
@ -576,7 +582,7 @@ namespace FizzyLauncher
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addFolderToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.AddFolder("New Folder");
private void addFolderToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.AddFolder();
/// <summary>
/// Open all pages
@ -737,6 +743,35 @@ namespace FizzyLauncher
ThreadControl.SetEnable(saveToolStripMenuItem, (treeView1.HasChanged && ApplicationMode == AppMode.Open));
}
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();
}
protected async Task<Result> CloseFile()
{
@ -813,6 +848,25 @@ namespace FizzyLauncher
return rs;
}
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;
}
protected void InvalidateAppSession()
{
if (CurrentSession == null) CurrentSession = new AppSession();