From 0429efd44bee0b3e397f063721138e6e825995f4 Mon Sep 17 00:00:00 2001 From: Roger Ye Date: Fri, 6 Aug 2021 10:27:27 +0800 Subject: [PATCH] Moved PxItem, PxEntry and PxGroup to PassXYZ.Vault --- KPCLib.nuspec | 2 +- KPCLib.xunit/GfxUtilTests.cs | 58 ------ KPCLib.xunit/KPCLib.xunit.csproj | 2 + KPCLib.xunit/PasswordDBTests.cs | 96 --------- KPCLib.xunit/PxDatabaseTests.cs | 15 +- KPCLib.xunit/PxEntryTests.cs | 127 ------------ KPCLib/KPCLib.csproj | 6 +- PassXYZLib.nuspec | 2 +- PassXYZLib/ItemExtensions.cs | 329 ------------------------------- PassXYZLib/PassXYZLib.csproj | 13 +- PassXYZLib/PxEntry.cs | 329 ------------------------------- PassXYZLib/PxGroup.cs | 36 ---- 12 files changed, 11 insertions(+), 1004 deletions(-) delete mode 100644 KPCLib.xunit/PasswordDBTests.cs delete mode 100644 KPCLib.xunit/PxEntryTests.cs delete mode 100644 PassXYZLib/ItemExtensions.cs delete mode 100644 PassXYZLib/PxEntry.cs delete mode 100644 PassXYZLib/PxGroup.cs diff --git a/KPCLib.nuspec b/KPCLib.nuspec index 25e0eaa..d0cf027 100644 --- a/KPCLib.nuspec +++ b/KPCLib.nuspec @@ -2,7 +2,7 @@ KPCLib - 1.3.1.1 + 1.3.2.0 Roger Ye Roger Ye false diff --git a/KPCLib.xunit/GfxUtilTests.cs b/KPCLib.xunit/GfxUtilTests.cs index 71f41f8..a00e1fa 100644 --- a/KPCLib.xunit/GfxUtilTests.cs +++ b/KPCLib.xunit/GfxUtilTests.cs @@ -128,64 +128,6 @@ public void ScaleMultipleImages(int value) SaveScaledImage(resizedFile, value, value); } - [Theory] - [InlineData("http://github.com")] - [InlineData("http://www.baidu.com")] - [InlineData("https://www.bing.com/")] - [InlineData("http://www.youdao.com")] - [InlineData("https://www.dell.com")] - [InlineData("http://www.cmbchina.com")] - public void GetIconTest(string url) - { - var faviconUrl = ItemExtensions.RetrieveFavicon(url); - if(faviconUrl != null) - { - var imageFolder = "images"; - try - { - DirectoryInfo di = new DirectoryInfo(imageFolder); - try - { - // Determine whether the directory exists. - if (!di.Exists) - { - di.Create(); - } - } - catch (Exception e) - { - Debug.WriteLine("The process failed: {0}", e.ToString()); - } - - var uri = new Uri(faviconUrl); - WebClient myWebClient = new WebClient(); - byte[] pb = myWebClient.DownloadData(faviconUrl); - - if (faviconUrl.EndsWith(".ico") || faviconUrl.EndsWith(".png")) - { - GfxUtil.SaveImage(GfxUtil.ScaleImage(GfxUtil.LoadImage(pb), 128, 128), $"{imageFolder}/{uri.Host}.png"); - } - else if (faviconUrl.EndsWith(".svg")) - { - GfxUtil.SaveImage(GfxUtil.LoadSvgImage(pb), $"{imageFolder}/{uri.Host}.png"); - } - Debug.WriteLine($"{imageFolder}/{uri.Host}.png"); - } - catch (System.Net.WebException ex) - { - Debug.WriteLine($"{ex}"); - } - } - Assert.NotNull(faviconUrl); - } - - [Theory] - [InlineData("https://favicon.io/tutorials/what-is-a-favicon/")] - public void NoFaviconTest(string url) - { - Assert.Null(ItemExtensions.RetrieveFavicon(url)); - } - [Fact] public void PrintImageFormat() { diff --git a/KPCLib.xunit/KPCLib.xunit.csproj b/KPCLib.xunit/KPCLib.xunit.csproj index 0b336b4..276e5c5 100644 --- a/KPCLib.xunit/KPCLib.xunit.csproj +++ b/KPCLib.xunit/KPCLib.xunit.csproj @@ -4,6 +4,8 @@ net5.0 false + + 1.3.2 diff --git a/KPCLib.xunit/PasswordDBTests.cs b/KPCLib.xunit/PasswordDBTests.cs deleted file mode 100644 index 37c9749..0000000 --- a/KPCLib.xunit/PasswordDBTests.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Xunit; -using KeePassLib; -using KeePassLib.Keys; -using KeePassLib.Serialization; -using KeePassLib.Utility; -using PassXYZLib; - -namespace KPCLib.xunit -{ - public class PasswordDBFixture : IDisposable - { - const string TEST_DB = "pass_d_E8f4pEk.xyz"; - const string TEST_DB_KEY = "12345"; - - public PasswordDBFixture() - { - PxDb = PasswordDb.Instance; - PxDb.Open(TEST_DB, TEST_DB_KEY); - } - - public void Dispose() - { - PxDb.Close(); - } - - public PasswordDb PxDb { get; private set; } - public KPCLibLogger Logger { get; private set; } - } - - [CollectionDefinition("PasswordDB collection")] - public class PasswordDBCollection : ICollectionFixture - { - // This class has no code, and is never created. Its purpose is simply - // to be the place to apply [CollectionDefinition] and all the - // ICollectionFixture<> interfaces. - } - - [Collection("PasswordDB collection")] - public class PasswordDBTests - { - PasswordDBFixture passxyz; - - public PasswordDBTests(PasswordDBFixture passXYZFixture) - { - this.passxyz = passXYZFixture; - } - - [Fact] - public void IsOpenDbTest() - { - Debug.WriteLine($"{passxyz.PxDb}"); - Assert.True((passxyz.PxDb.IsOpen)); - } - - [Fact] - public void ListItemsTests() - { - PwGroup pg = passxyz.PxDb.RootGroup; - - List itemList = pg.GetItems(); - foreach (var item in itemList) - { - Debug.WriteLine($"\t{item.Name} : {item.ImgSource}"); - } - Assert.NotNull(itemList); - } - - [Theory] - [InlineData("http://github.com")] - [InlineData("http://www.baidu.com")] - [InlineData("http://www.youdao.com")] - [InlineData("http://www.qq.com")] - [InlineData("http://www.hp.com")] - [InlineData("http://www.163.com")] - [InlineData("http://www.bing.com")] - public void CustomIconTests(string url) - { - var entry = new PwEntry(); - entry.AddNewIcon(url); - PwCustomIcon icon = passxyz.PxDb.GetPwCustomIcon(entry.CustomIconUuid); - entry.Name = icon.Name; - Debug.WriteLine($"{icon.Name} is stored at {passxyz.PxDb.CurrentPath}"); - passxyz.PxDb.CurrentGroup.AddEntry(entry, true); - passxyz.PxDb.Save(null); - Assert.False(entry.CustomIconUuid.Equals(PwUuid.Zero)); - } - - } -} diff --git a/KPCLib.xunit/PxDatabaseTests.cs b/KPCLib.xunit/PxDatabaseTests.cs index 8c9ed48..1b0792b 100644 --- a/KPCLib.xunit/PxDatabaseTests.cs +++ b/KPCLib.xunit/PxDatabaseTests.cs @@ -84,19 +84,6 @@ public void ListEntriesTests() } } - [Fact] - public void ListItemsTests() - { - PwGroup pg = passxyz.PxDb.RootGroup; - - List itemList = pg.GetItems(); - foreach (var item in itemList) - { - Debug.WriteLine($"\t{item.Name} : {item.ImgSource}"); - } - Assert.NotNull(itemList); - } - [Fact] public void DeleteEmptyEntryTest() { @@ -392,7 +379,7 @@ public class PxLibInfoTests public void PxLibVersion() { Debug.WriteLine($"{PxLibInfo.Version}"); - Assert.Equal(PxLibInfo.Version, new System.Version("1.3.1.0")); + Assert.Equal(PxLibInfo.Version, new System.Version("1.3.2.0")); } [Fact] diff --git a/KPCLib.xunit/PxEntryTests.cs b/KPCLib.xunit/PxEntryTests.cs deleted file mode 100644 index fe11b3d..0000000 --- a/KPCLib.xunit/PxEntryTests.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Xunit; - -using KeePassLib; -using PassXYZLib; - -namespace KPCLib.xunit -{ - public class PxEntryFixture : IDisposable - { - const string TEST_DB = "pass_d_E8f4pEk.xyz"; - const string TEST_DB_KEY = "12345"; - - public PxEntryFixture() - { - PxDb = new PxDatabase(); - PxDb.Open(TEST_DB, TEST_DB_KEY); - } - - public void Dispose() - { - PxDb.Close(); - } - - public PxDatabase PxDb { get; private set; } - public KPCLibLogger Logger { get; private set; } - - public string Username - { - get { return PxDefs.GetUserNameFromDataFile(TEST_DB); } - } - } - - [CollectionDefinition("PxEntry collection")] - public class PxEntryCollection : ICollectionFixture - { - // This class has no code, and is never created. Its purpose is simply - // to be the place to apply [CollectionDefinition] and all the - // ICollectionFixture<> interfaces. - } - - [Collection("PxEntry collection")] - public class PxEntryTests - { - PxEntryFixture passxyz; - - public PxEntryTests(PxEntryFixture fixture) - { - this.passxyz = fixture; - } - - [Fact] - public void PxEntryInitTests() - { - var entry = new PxEntry(); - Assert.NotNull(entry); - } - - [Fact] - public void PrintPxEntryTest() - { - Debug.WriteLine("*** PrintPxEntryTest ***"); - foreach (PwEntry entry in passxyz.PxDb.GetAllEntries()) - { - if (entry.IsPxEntry()) - { - Debug.WriteLine($"Title: {entry.Name}"); - Debug.WriteLine($"{entry.EncodeKey("TestKey")}"); - var fields = entry.GetFields(); - foreach (var field in fields) { Debug.WriteLine($"{field.EncodedKey}={field.Value}"); } - Debug.WriteLine($"Notes: {entry.GetNotes()}"); - Assert.True(true); - return; - } - } - Debug.WriteLine("Cannot find PxEntry."); - Assert.True(false); - } - - [Fact] - public void PrintPwEntryTest() - { - Debug.WriteLine("*** PrintPwEntryTest ***"); - foreach (PwEntry entry in passxyz.PxDb.GetAllEntries()) - { - if(!entry.IsNotes()) - { - if (!entry.IsPxEntry()) - { - Debug.WriteLine($"Title: {entry.Name}"); - var fields = entry.GetFields(); - foreach (var field in fields) { Debug.WriteLine($"{field.Key}={field.Value}"); } - Debug.WriteLine($"Notes: {entry.GetNotes()}"); - Assert.True(true); - return; - } - } - } - Debug.WriteLine("Cannot find PwEntry."); - Assert.True(false); - } - - [Fact] - public void PrintNotesTest() - { - Debug.WriteLine("*** PrintNotesTest ***"); - foreach (PwEntry entry in passxyz.PxDb.GetAllEntries()) - { - if (entry.IsNotes()) - { - Debug.WriteLine($"Title: {entry.Name}"); - Debug.WriteLine($"Notes: {entry.GetNotes()}"); - Assert.True(true); - return; - } - } - Debug.WriteLine("Cannot find Notes."); - Assert.True(false); - } - } -} diff --git a/KPCLib/KPCLib.csproj b/KPCLib/KPCLib.csproj index 97deccc..7fe8770 100644 --- a/KPCLib/KPCLib.csproj +++ b/KPCLib/KPCLib.csproj @@ -6,7 +6,7 @@ Library true - 1.3.1.0 + 1.3.2 https://github.com/passxyz/KPCLib https://github.com/passxyz/KPCLib This is the build of KeePassLib in Xamarin Portable Class Library. Three platforms, UWP, Android and iOS, are supported and tested. @@ -17,8 +17,8 @@ - Updated to KeePass 2.48.1 - Fixed an issue in CryptoRandom.cs en-US - 1.3.1.0 - 1.3.1.0 + 1.3.2.0 + 1.3.2.0 diff --git a/PassXYZLib.nuspec b/PassXYZLib.nuspec index c7e9a60..596310e 100644 --- a/PassXYZLib.nuspec +++ b/PassXYZLib.nuspec @@ -2,7 +2,7 @@ PassXYZLib - 1.3.1.2 + 1.3.2.0 Roger Ye Roger Ye false diff --git a/PassXYZLib/ItemExtensions.cs b/PassXYZLib/ItemExtensions.cs deleted file mode 100644 index c8303b7..0000000 --- a/PassXYZLib/ItemExtensions.cs +++ /dev/null @@ -1,329 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Net; - -using System.Text; - -using HtmlAgilityPack; -using Xamarin.Forms; -using Xamarin.Essentials; -using SkiaSharp; - -using FontAwesome.Regular; -using KeePassLib; -using KeePassLib.Utility; - -namespace PassXYZLib -{ - /// - /// ItemExtensions is a static class which defines a set of extension methods for Item. - /// - public static class ItemExtensions - { - private static bool UrlExists(string url) - { - try - { - if (!string.IsNullOrWhiteSpace(url)) - { - // Check URL format - Uri uri = new Uri(url); - if (uri.Scheme.Contains("http") || uri.Scheme.Contains("https")) - { - var webRequest = WebRequest.Create(url); - webRequest.Method = "HEAD"; - var webResponse = (HttpWebResponse)webRequest.GetResponse(); - return webResponse.StatusCode == HttpStatusCode.OK; - } - } - } - catch (Exception ex) - { - Debug.WriteLine($"{ex}"); - } - return false; - } - - private static string FormatUrl(string url, string baseUrl) - { - if (url.StartsWith("//")) { return ("http:" + url); } - else if (url.StartsWith("/")) { return (baseUrl + url); } - - return url; - } - - public static string RetrieveFavicon(string url) - { - string returnFavicon = null; - - // declare htmlweb and load html document - HtmlWeb web = new HtmlWeb(); - var htmlDoc = web.Load(url); - - //1. 处理 apple-touch-icon 的情况 - var elementsAppleTouchIcon = htmlDoc.DocumentNode.SelectNodes("//link[contains(@rel, 'apple-touch-icon')]"); - if (elementsAppleTouchIcon != null && elementsAppleTouchIcon.Any()) - { - var favicon = elementsAppleTouchIcon.First(); - var faviconUrl = FormatUrl(favicon.GetAttributeValue("href", null), url); - if (UrlExists(faviconUrl)) - { - return faviconUrl; - } - } - - // 2. Try to get svg version - var el = htmlDoc.DocumentNode.SelectSingleNode("/html/head/link[@rel='icon' and @href]"); - if (el != null) - { - try - { - var faviconUrl = FormatUrl(el.Attributes["href"].Value, url); - - if (UrlExists(faviconUrl)) - { - return faviconUrl; - } - } - catch (WebException ex) - { - Debug.WriteLine($"{ex}"); - } - } - - // 3. 从页面的 HTML 中抓取 - var elements = htmlDoc.DocumentNode.SelectNodes("//link[contains(@rel, 'icon')]"); - if (elements != null && elements.Any()) - { - var favicon = elements.First(); - var faviconUrl = FormatUrl(favicon.GetAttributeValue("href", null), url); - if (UrlExists(faviconUrl)) - { - return faviconUrl; - } - } - - // 4. 直接获取站点的根目录图标 - try - { - var uri = new Uri(url); - if (uri.HostNameType == UriHostNameType.Dns) - { - var faviconUrl = string.Format("{0}://{1}/favicon.ico", uri.Scheme == "https" ? "https" : "http", uri.Host); - if (UrlExists(faviconUrl)) - { - return faviconUrl; - } - } - } - catch (UriFormatException ex) - { - Debug.WriteLine($"{ex}"); - return returnFavicon; - } - - return returnFavicon; - } - - /// - /// Create a SKBitmap instance from a byte arrary - /// - /// byte arraty - /// This is the url using to retrieve icon. - private static SKBitmap LoadImage(byte[] pb, string faviconUrl = null) - { - int w = 96, h = 96; - if (DeviceInfo.Platform.Equals(DevicePlatform.Android)) - { - w = 96; h = 96; - } - else if (DeviceInfo.Platform.Equals(DevicePlatform.iOS)) - { - w = 64; h = 64; - } - else if (DeviceInfo.Platform.Equals(DevicePlatform.UWP)) - { - w = 32; h = 32; - } - - if (faviconUrl != null) - { - if (faviconUrl.EndsWith(".ico") || faviconUrl.EndsWith(".png")) - { - return GfxUtil.ScaleImage(GfxUtil.LoadImage(pb), w, h); - } - else if (faviconUrl.EndsWith(".svg")) - { - return GfxUtil.LoadSvgImage(pb, w, h); - } - else { return null; } - } - else - { - return GfxUtil.ScaleImage(GfxUtil.LoadImage(pb), w, h); - } - - } - - private static ImageSource GetImageSource(SKBitmap bitmap) - { - if (bitmap != null) - { - //SKImage image = SKImage.FromPixels(bitmap.PeekPixels()); - //SKData encoded = image.Encode(); - //Stream stream = encoded.AsStream(); - // There is a bug so we cannot use stream here. Please refer to the below link about the issue. - // https://github.cohttps://github.com/xamarin/Xamarin.Forms/issues/11495m/xamarin/Xamarin.Forms/issues/11495 - return ImageSource.FromStream(() => SKImage.FromPixels(bitmap.PeekPixels()).Encode().AsStream()); - } - else { return null; } - } - - public static SKBitmap GetBitmapByUrl(string url) - { - var faviconUrl = RetrieveFavicon(url); - - try - { - var uri = new Uri(faviconUrl); - WebClient myWebClient = new WebClient(); - byte[] pb = myWebClient.DownloadData(faviconUrl); - - return LoadImage(pb, faviconUrl); - } - catch (WebException ex) - { - Debug.WriteLine($"{ex}"); - } - return null; - } - - public static ImageSource GetImageByUrl(string url) - { - SKBitmap bitmap = GetBitmapByUrl(url); - - try - { - if (bitmap != null) { return GetImageSource(bitmap); } - } - catch (WebException ex) - { - Debug.WriteLine($"{ex}"); - } - return null; - } - - /// - /// Extension method of KeePassLib.Item - /// This method can be used to retrieve icon from a url. - /// - /// Instance of Item - /// This is the url using to retrieve icon. - public static void UpdateIcon(this Item item, string url) - { - var faviconUrl = RetrieveFavicon(url); - - try - { - var uri = new Uri(faviconUrl); - WebClient myWebClient = new WebClient(); - byte[] pb = myWebClient.DownloadData(faviconUrl); - - SKBitmap bitmap = LoadImage(pb, faviconUrl); - item.ImgSource = GetImageSource(bitmap); - } - catch (WebException ex) - { - Debug.WriteLine($"{ex}"); - } - } - - public static void SetDefaultIcon(this Item item) - { - var icon = new IconSource(); - - if (item.IsGroup) - { - icon.Icon = Icon.Folder; - } - else - { - icon.Icon = Icon.File; - } - item.ImgSource = icon; - } - - public static void SetIcon(this Item item) - { - if (item.CustomIconUuid != PwUuid.Zero) - { - PasswordDb db = PasswordDb.Instance; - if(db != null) - { - if(db.IsOpen) - { - PwCustomIcon customIcon = db.GetPwCustomIcon(item.CustomIconUuid); - if(customIcon != null) - { - var pb = customIcon.ImageDataPng; - SKBitmap bitmap = LoadImage(pb); - item.ImgSource = GetImageSource(bitmap); - return; - } - } - else - { - Debug.WriteLine("SetIcon: PasswordDb is closed"); - } - } - else - { - Debug.WriteLine("SetIcon: No PasswordDb instance"); - } - } - - SetDefaultIcon(item); - } - - /// - /// Add a new custom icon to the database and set the new icon as the icon for this item. - /// - /// an instance of Item. Must not be null. - /// Url used to retrieve the new icon. - public static void AddNewIcon(this Item item, string url) - { - PasswordDb db = PasswordDb.Instance; - if (db != null) - { - if (db.IsOpen) - { - Uri uri = new Uri(url); - var old = db.GetCustomIcon(uri.Host); - if (old == null) - { - var bitmap = GetBitmapByUrl(url); - var uuid = db.SaveCustomIcon(bitmap, uri.Host); - item.CustomIconUuid = uuid; - Debug.WriteLine($"AddNewIcon: hostname={uri.Host}"); - } - else - { - item.CustomIconUuid = old.Uuid; - Debug.WriteLine($"AddNewIcon: Found an existing icon as {uri.Host}"); - } - } - else - { - Debug.WriteLine("AddNewIcon: PasswordDb is closed"); - } - } - else - { - Debug.WriteLine("AddNewIcon: No PasswordDb instance"); - } - } - } -} diff --git a/PassXYZLib/PassXYZLib.csproj b/PassXYZLib/PassXYZLib.csproj index 2ce11b3..db4de33 100644 --- a/PassXYZLib/PassXYZLib.csproj +++ b/PassXYZLib/PassXYZLib.csproj @@ -2,9 +2,9 @@ netstandard2.0 - 1.3.1.0 - 1.3.1.0 - 1.3.1.0 + 1.3.2 + 1.3.2.0 + 1.3.2.0 PassXYZ Inc. Roger Ye Roger Ye @@ -18,14 +18,7 @@ - - - - - - - diff --git a/PassXYZLib/PxEntry.cs b/PassXYZLib/PxEntry.cs deleted file mode 100644 index 3ab80e7..0000000 --- a/PassXYZLib/PxEntry.cs +++ /dev/null @@ -1,329 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Diagnostics; -using System.Text; -using Xamarin.Forms; - -using PureOtp; - -using KeePassLib; -using KeePassLib.Interfaces; -using KeePassLib.Security; - -namespace PassXYZLib -{ - /// - /// A class representing a field in PwEntry. A field is stored in ProtectedStringDictionary. - /// We convert key value pair into a field so that it can be used by the user interface. - /// - public class Field : INotifyPropertyChanged - { - private string _key; - /// - /// This is the key used by Field. This Key should be decoded for PxEntry. - /// - public string Key - { - get => _key; - set - { - _key = value; - OnPropertyChanged("Key"); - } - } - - /// - /// The EncodeKey is used by PxEntry. For PwEntry, this is an empty string. - /// - public string EncodedKey = string.Empty; - - public bool IsEncoded { get => !string.IsNullOrEmpty(EncodedKey); } - - private string _value; - private string _shadowValue = string.Empty; - public string Value - { - get => _value; - set - { - if(IsProtected) - { - _shadowValue = value; - _value = new string('*', _shadowValue.Length); - } - else - { - _value = value; - } - OnPropertyChanged("Value"); - } - } - - private bool _isProtected = false; - public bool IsProtected - { - get => _isProtected; - set - { - _isProtected = value; - OnPropertyChanged("IsProtected"); - } - } - - private bool _isHide = true; - public bool IsHide - { - get => _isHide; - } - - public ImageSource ImgSource { get; set; } - - public Field(string key, string value, bool isProtected, string encodedKey = "") - { - Key = key; - EncodedKey = encodedKey; - IsProtected = isProtected; - Value = value; - - var lastWord = key.Split(' ').Last(); - ImgSource = FieldIcons.GetImage(lastWord.ToLower()); - } - - public void ShowPassword() - { - if (IsProtected && !string.IsNullOrEmpty(_shadowValue)) - { - _value = _shadowValue; - _isHide = false; - OnPropertyChanged("Value"); - } - } - - public void HidePassword() - { - if (IsProtected && !string.IsNullOrEmpty(_shadowValue)) - { - _value = new string('*', _shadowValue.Length); - _isHide = true; - OnPropertyChanged("Value"); - } - } - - #region INotifyPropertyChanged - public event PropertyChangedEventHandler PropertyChanged; - protected void OnPropertyChanged([CallerMemberName] string propertyName = "") - { - var changed = PropertyChanged; - if (changed == null) - return; - - changed.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - #endregion - } - - public class PxEntry : PwEntry - { - public PxEntry(bool bCreateNewUuid, bool bSetTimes) : base(bCreateNewUuid, bSetTimes) { } - public PxEntry() : base() { } - } - - public static class PwEntryEx - { - public static bool IsNotes(this PwEntry entry) - { - string subType = entry.CustomData.Get(PxDefs.PxCustomDataItemSubType); - if(string.IsNullOrEmpty(subType)) - { - return false; - } - else - { - if (subType.Equals(ItemSubType.Notes.ToString())) - { - return true; - } - else - { - return false; - } - } - } - - /// - /// This is an extension method of PwEntry. - /// This method is used to set the sub-type of a PwEntry. - /// - /// an instance of PwEntry - /// sub-type of PwEntry - /// A list of fields - public static void SetType(this PwEntry entry, ItemSubType itemSubType) - { - if (itemSubType != ItemSubType.None && itemSubType != ItemSubType.Group) - { - entry.CustomData.Set(PxDefs.PxCustomDataItemSubType, itemSubType.ToString()); - } - } - - public static bool IsPxEntry(this PwEntry entry) - { - return PxDefs.IsPxEntry(entry); - } - - public static string EncodeKey(this PwEntry entry, string key) - { - if(PxDefs.IsPxEntry(entry)) - { - string lastKey = string.Empty; - foreach (var pstr in entry.Strings) - { - if (!pstr.Key.Equals(PwDefs.TitleField) && !pstr.Key.Equals(PwDefs.NotesField)) - { - lastKey = pstr.Key; - } - } - - if(string.IsNullOrEmpty(lastKey)) - { - return "000" + key; - } - else - { - uint index = uint.Parse(lastKey.Substring(0, PxDefs.PxEntryKeyDigits)); - return PxDefs.EncodeKey(key, index + 1); - } - } - else - { - return key; - } - } - - public static string GetNotes(this PwEntry entry) - { - return entry.Strings.ReadSafe(PwDefs.NotesField); - } - - /// - /// This is an extension method of PwEntry. - /// Convert ProtectedStringDictionary into a list of fields. TitleField and NotesField - /// are not included in the list. - /// TitleField will be used to display the title in UI and NotesField will be displayed at the - /// bottom of a page with Markdown support. - /// - /// an instance of PwEntry - /// A list of fields - public static List GetFields(this PwEntry entry) - { - List fields = new List(); - bool isPxEntry = PxDefs.IsPxEntry(entry); - - if (isPxEntry) - { - // If this is an instance of PxEntry, we handle it here. We need to convert ProtectedString to Field. - foreach (var pstr in entry.Strings) - { - if (!pstr.Key.Equals(PwDefs.TitleField) && !pstr.Key.Equals(PwDefs.NotesField)) - { - fields.Add(new Field(PxDefs.DecodeKey(pstr.Key), entry.Strings.ReadSafe(pstr.Key), entry.Strings.GetSafe(pstr.Key).IsProtected, pstr.Key)); - } - } - } - else - { - // If this is an instance of PwEntry, we handle it here. - if(entry.Strings.Exists(PwDefs.UserNameField)) - { - fields.Add(new Field(PwDefs.UserNameField, entry.Strings.ReadSafe(PwDefs.UserNameField), entry.Strings.GetSafe(PwDefs.UserNameField).IsProtected)); - } - - if (entry.Strings.Exists(PwDefs.PasswordField)) - { - fields.Add(new Field(PwDefs.PasswordField, entry.Strings.ReadSafe(PwDefs.PasswordField), entry.Strings.GetSafe(PwDefs.PasswordField).IsProtected)); - } - - if (entry.Strings.Exists(PwDefs.UrlField)) - { - fields.Add(new Field(PwDefs.UrlField, entry.Strings.ReadSafe(PwDefs.UrlField), entry.Strings.GetSafe(PwDefs.UrlField).IsProtected)); - } - - foreach (var field in entry.Strings) - { - if (!PwDefs.IsStandardField(field.Key)) - { - fields.Add(new Field(field.Key, entry.Strings.ReadSafe(field.Key), entry.Strings.GetSafe(field.Key).IsProtected)); - } - } - } - - return fields; - } - } - - public static class FieldIcons - { - public static Dictionary RegularIcons = new Dictionary() - { - { "calendar", FontAwesome.Regular.Icon.CalendarAlt } - }; - - public static Dictionary SolidIcons = new Dictionary() - { - { "address", FontAwesome.Solid.Icon.MapMarkerAlt }, - { "card", FontAwesome.Solid.Icon.IdCard }, - { "date", FontAwesome.Solid.Icon.CalendarAlt }, - { "email", FontAwesome.Solid.Icon.Envelope }, - { "mobile", FontAwesome.Solid.Icon.Phone }, - { "name", FontAwesome.Solid.Icon.User }, - { "password", FontAwesome.Solid.Icon.Key }, - { "phone", FontAwesome.Solid.Icon.Phone }, - { "pin", FontAwesome.Solid.Icon.Key }, - { "url", FontAwesome.Solid.Icon.Link }, - { "username", FontAwesome.Solid.Icon.User } - }; - - public static Dictionary BrandIcons = new Dictionary() - { - { "alipay", FontAwesome.Brand.Icon.Alipay }, - { "qq", FontAwesome.Brand.Icon.Qq }, - { "wechat", FontAwesome.Brand.Icon.Weixin } - }; - - public static ImageSource GetImage(string key) - { - if (BrandIcons.ContainsKey(key)) - { - var brandIconSource = new FontAwesome.Brand.IconSource - { - Icon = BrandIcons[key] - }; - return brandIconSource; - } - else if (RegularIcons.ContainsKey(key)) - { - var regularIconSource = new FontAwesome.Regular.IconSource - { - Icon = RegularIcons[key] - }; - return regularIconSource; - } - else - { - var solidIconSource = new FontAwesome.Solid.IconSource - { - Icon = FontAwesome.Solid.Icon.File - }; - - if (SolidIcons.ContainsKey(key)) - { - solidIconSource.Icon = SolidIcons[key]; - } - - return solidIconSource; - } - } - } -} diff --git a/PassXYZLib/PxGroup.cs b/PassXYZLib/PxGroup.cs deleted file mode 100644 index ca73f05..0000000 --- a/PassXYZLib/PxGroup.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -using KeePassLib; -using KeePassLib.Interfaces; - -namespace PassXYZLib -{ - public class PxGroup: PwGroup - { - public PxGroup(bool bCreateNewUuid, bool bSetTimes) : base(bCreateNewUuid, bSetTimes) { } - public PxGroup() : base() { } - } - - public static class PwGroupEx - { - public static List GetItems(this PwGroup group) - { - List itemList = new List(); - - foreach (PwEntry entry in group.Entries) - { - entry.SetIcon(); - itemList.Add((Item)entry); - } - - foreach (PwGroup gp in group.Groups) - { - gp.SetIcon(); - itemList.Add((Item)gp); - } - return itemList; - } - } -}