bookmark-manager-r4/Classes/SupportedFile/RYZSupportedFile.cs

126 lines
4.2 KiB
C#
Raw Normal View History

2021-09-28 21:39:45 +00:00
using bzit.bomg.Models;
using RyzStudio.IO;
2021-09-07 11:32:24 +00:00
using RyzStudio.Windows.Forms;
using System.Collections.Generic;
2021-09-28 21:39:45 +00:00
using System.Text;
using System.Windows.Forms;
using System.Xml;
2021-09-07 11:32:24 +00:00
namespace BookmarkManager
{
public class RYZSupportedFile : SupportedFileBase
{
public RYZSupportedFile()
{
supportedExtensions = new List<string>() { ".ryz" };
}
public override bool IsEncryptionSupported { get; set; } = true;
public override bool IsEncrypted(string filename) => SharpZipLib.IsZipEncrypted(filename);
public override Result Load(BookmarkTreeView treeview, string filename, string password)
{
2021-09-28 21:39:45 +00:00
treeview.Clear();
if (!SharpZipLib.TestArchive(filename, password))
{
return Result.Create(false, "Could not read file, is password correct?");
}
string sourceCode = SharpZipLib.ReadSingle(filename, password, "bookmarks.xml");
if (string.IsNullOrWhiteSpace(sourceCode))
{
return Result.Create(false, "Could not read file, unexpected format");
}
// load xml
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(sourceCode);
// parse
XmlNode xnl = xmlDocument.SelectSingleNode("bomg/b/g");
if (xnl == null)
{
return Result.Create(false, "Could not load file, unexpected format");
}
string rootName = (xnl.Attributes["name"] == null) ? "Bookmarks" : (string.IsNullOrWhiteSpace(xnl.Attributes["name"].InnerText) ? "Bookmarks" : xnl.Attributes["name"].InnerText.Trim());
foreach (XmlNode xn in xnl.ChildNodes)
{
BookmarkItem item = new BookmarkItem();
foreach (XmlNode xn2 in xn.ChildNodes)
{
switch (xn2.LocalName)
{
case "name":
item.SiteName = xn2.InnerText?.Trim();
break;
case "address":
item.SiteAddress = xn2.InnerText?.Trim();
break;
case "description":
item.SiteDescription = xn2.InnerText?.Trim();
break;
//case "created":
// bi.Created = xn2.InnerText?.Trim();
// break;
default:
break;
}
}
// fix fullpath and name
if (item.SiteName.Contains("|"))
{
StringBuilder sb = new StringBuilder();
sb.Append("\\");
sb.Append(System.Web.HttpUtility.UrlEncode(rootName));
sb.Append("\\");
string[] pathParts = item.SiteName.Split('|');
for (int i = 0; i < (pathParts.Length - 1); i++)
{
sb.Append(pathParts[i]);
//sb.Append(System.Web.HttpUtility.UrlDecode(pathParts[i]));
sb.Append("\\");
}
item.SiteName = System.Web.HttpUtility.UrlDecode(pathParts[(pathParts.Length - 1)] ?? string.Empty);
item.TreeviewPath = sb.ToString();
}
else
{
item.SiteName = System.Web.HttpUtility.UrlDecode(item.SiteName);
item.TreeviewPath = string.Format("\\{0}\\", System.Web.HttpUtility.UrlEncode(rootName));
}
if (treeview.InvokeRequired)
{
treeview.Invoke(new MethodInvoker(() =>
{
treeview.AddItem(item);
}));
}
else
{
treeview.AddItem(item);
}
}
return Result.Create(true);
2021-09-07 11:32:24 +00:00
}
public override Result Save(BookmarkTreeView treeview, string filename, string password)
{
return Result.Create(false);
}
}
}