From dc73bb187ee21f4c7cd1ec05c62ee3a3cbdd75dc Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 29 Sep 2021 16:20:46 +0100 Subject: [PATCH] Added: JSON file support --- Classes/SupportedFile/ISupportedFileBase.cs | 2 + Classes/SupportedFile/JSONSupportedFile.cs | 89 +++++++++++++++++++++ Classes/SupportedFile/SupportedFileBase.cs | 2 + MainForm.cs | 58 +++++++++++++- 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 Classes/SupportedFile/JSONSupportedFile.cs diff --git a/Classes/SupportedFile/ISupportedFileBase.cs b/Classes/SupportedFile/ISupportedFileBase.cs index 0432064..424bde9 100644 --- a/Classes/SupportedFile/ISupportedFileBase.cs +++ b/Classes/SupportedFile/ISupportedFileBase.cs @@ -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 SupportedExtensions { get; } bool IsSupported(string filename); diff --git a/Classes/SupportedFile/JSONSupportedFile.cs b/Classes/SupportedFile/JSONSupportedFile.cs new file mode 100644 index 0000000..487300a --- /dev/null +++ b/Classes/SupportedFile/JSONSupportedFile.cs @@ -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() { ".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 rs = JsonConvert.DeserializeObject>(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 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); + } + + } +} diff --git a/Classes/SupportedFile/SupportedFileBase.cs b/Classes/SupportedFile/SupportedFileBase.cs index 667a206..10f6a6f 100644 --- a/Classes/SupportedFile/SupportedFileBase.cs +++ b/Classes/SupportedFile/SupportedFileBase.cs @@ -16,6 +16,8 @@ namespace BookmarkManager public virtual bool IsEncryptionSupported { get; set; } = false; + public List SupportedExtensions { get => supportedExtensions; } + public virtual bool IsSupported(string filename) { diff --git a/MainForm.cs b/MainForm.cs index 071ec09..c47f0a9 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -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 supportedFileExtensions = GetSupportedFileTypes(); + + openFileDialog1.Filter = BuildOpenFileSupportedFiles(supportedFileExtensions); + saveFileDialog1.Filter = BuildSaveFileSupportedFiles(supportedFileExtensions); + ApplicationMode = AppMode.Clear; } @@ -547,7 +553,7 @@ namespace FizzyLauncher /// /// /// - private void addFolderToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.AddFolder("New Folder"); + private void addFolderToolStripMenuItem_Click(object sender, EventArgs e) => treeView1.AddFolder(); /// /// Edit root node @@ -576,7 +582,7 @@ namespace FizzyLauncher /// /// /// - private void addFolderToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.AddFolder("New Folder"); + private void addFolderToolStripMenuItem1_Click(object sender, EventArgs e) => treeView1.AddFolder(); /// /// Open all pages @@ -737,6 +743,35 @@ namespace FizzyLauncher ThreadControl.SetEnable(saveToolStripMenuItem, (treeView1.HasChanged && ApplicationMode == AppMode.Open)); } + protected string BuildOpenFileSupportedFiles(List 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 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 CloseFile() { @@ -813,6 +848,25 @@ namespace FizzyLauncher return rs; } + protected List GetSupportedFileTypes() + { + List rs = new List(); + + List 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()); + } + + return rs; + } + protected void InvalidateAppSession() { if (CurrentSession == null) CurrentSession = new AppSession();