diff --git a/CHANGELOG.md b/CHANGELOG.md index a9445f32..c6aafa0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added +- [SIL.LCModel] Add new virtual property LicenseTSS in CmPicture, to access info about the picture's copyright and license. +- [SIL.LCModel] Add new virtual property CreatorTSS in CmPicture, to access info about the picture's creator. - [SIL.LCModel] Add SpecificItemAndFieldName() to ISenseOrEntry - [SIL.LCModel] Added a parameter to GetBestGuess() and TryGetBestGuess() to do lowercase matching regardless of the occurrence index - [SIL.LCModel] Add GetCaptionOrHeadword() to CmPicture diff --git a/src/SIL.LCModel.Utils/SIL.LCModel.Utils.csproj b/src/SIL.LCModel.Utils/SIL.LCModel.Utils.csproj index 3e9ae38e..68e3c49a 100644 --- a/src/SIL.LCModel.Utils/SIL.LCModel.Utils.csproj +++ b/src/SIL.LCModel.Utils/SIL.LCModel.Utils.csproj @@ -1,4 +1,4 @@ - + net462;netstandard2.0 diff --git a/src/SIL.LCModel/DomainImpl/CmPicture.cs b/src/SIL.LCModel/DomainImpl/CmPicture.cs index 9f5a5b2a..255ce8ff 100644 --- a/src/SIL.LCModel/DomainImpl/CmPicture.cs +++ b/src/SIL.LCModel/DomainImpl/CmPicture.cs @@ -17,6 +17,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Security; using SIL.LCModel.Core.Cellar; using SIL.LCModel.Core.KernelInterfaces; using SIL.LCModel.Core.Text; @@ -312,6 +313,83 @@ public ITsString PathNameTSS } } + /// ------------------------------------------------------------------------------------ + /// + /// Method called to implement virtual property. Returns license metadata of associated + /// file. + /// ---------------------------------------------------------------------------------- + [VirtualProperty(CellarPropertyType.String)] + public ITsString LicenseTSS + { + get + { + var path = PictureFileRA?.AbsoluteInternalPath; + SIL.Core.ClearShare.MetadataCore metadata; + try + { + metadata = SIL.Core.ClearShare.MetadataCore.CreateMetadataCoreFromFile(path); + } + catch + { + // Error getting metadata from path + metadata = null; + } + + if (metadata == null) + return null; + + var analWs = m_cache.WritingSystemFactory.GetStrFromWs(m_cache.DefaultAnalWs); + var vernWs = m_cache.WritingSystemFactory.GetStrFromWs(m_cache.DefaultVernWs); + + // Set Localizer.Default to use L10NSharpLocalizer to localize the license. + ILocalizer oldLocalizer = Localizer.Default; + Localizer.Default = new SIL.Core.Desktop.i18n.L10NSharpLocalizer(); + + // Get the license in first analysis writing system if available, otherwise first vernacular ws, otherwise English. + var license = metadata.License?.GetMinimalFormForCredits(new[] { analWs, vernWs, "en" }, out _); + if (string.IsNullOrEmpty(metadata.CopyrightNotice) && string.IsNullOrEmpty(license)) + return null; + + // Reset Localizer default. + Localizer.Default = oldLocalizer; + + // We want the short copyright notice, but it isn't safe to ask for if CopyrightNotice is null. + var copyright = string.IsNullOrEmpty(metadata.CopyrightNotice) + ? string.Empty + : metadata.ShortCopyrightNotice; + return m_cache.MakeUserTss(SecurityElement.Escape(string.Join(", ", new[] { copyright, license }.Where(txt => !string.IsNullOrEmpty(txt))))); + } + } + + /// ------------------------------------------------------------------------------------ + /// + /// Method called to implement virtual property. Returns creator metadata of associated + /// file. (LT-7104 requested internal path instead of original path.) + /// ---------------------------------------------------------------------------------- + [VirtualProperty(CellarPropertyType.String)] + public ITsString CreatorTSS + { + get + { + var path = PictureFileRA?.AbsoluteInternalPath; + SIL.Core.ClearShare.MetadataCore metadata; + try + { + metadata = SIL.Core.ClearShare.MetadataCore.CreateMetadataCoreFromFile(path); + } + catch + { + // Error getting metadata from path + metadata = null; + } + + if (metadata == null || metadata.Creator == null) + return null; + + return m_cache.MakeUserTss(SecurityElement.Escape(metadata.Creator)); + } + } + /// /// Get the sense number of the owning LexSense. /// ENHANCE DamienD: register this property as modified when its dependencies change diff --git a/src/SIL.LCModel/SIL.LCModel.csproj b/src/SIL.LCModel/SIL.LCModel.csproj index a1a1487f..23d12593 100644 --- a/src/SIL.LCModel/SIL.LCModel.csproj +++ b/src/SIL.LCModel/SIL.LCModel.csproj @@ -22,6 +22,7 @@ + diff --git a/tests/SIL.LCModel.Tests/DomainImpl/CmPictureTests.cs b/tests/SIL.LCModel.Tests/DomainImpl/CmPictureTests.cs index 0cd353f8..7d53881f 100644 --- a/tests/SIL.LCModel.Tests/DomainImpl/CmPictureTests.cs +++ b/tests/SIL.LCModel.Tests/DomainImpl/CmPictureTests.cs @@ -7,6 +7,7 @@ using System.IO; using System.Text; using NUnit.Framework; +using SIL.Core.ClearShare; using SIL.LCModel.Core.KernelInterfaces; using SIL.LCModel.Core.Text; using SIL.LCModel.Core.WritingSystems; @@ -28,7 +29,7 @@ public class CmPictureTests: MemoryOnlyBackendProviderRestoredForEachTestTestBas private MockFileOS m_fileOs; private ICmPictureFactory m_pictureFactory; private ICmPicture m_pict; - private string m_internalPath = Path.DirectorySeparatorChar + Path.GetRandomFileName(); + private string m_internalPath; private CoreWritingSystemDefinition m_wsGerman; private CoreWritingSystemDefinition m_wsSpanish; #endregion @@ -43,6 +44,10 @@ public override void FixtureSetup() base.FixtureSetup(); Cache.ServiceLocator.WritingSystemManager.GetOrSet("de", out m_wsGerman); Cache.ServiceLocator.WritingSystemManager.GetOrSet("es", out m_wsSpanish); + // Initialize internalPath inside a temp folder + // .jpg file extension is needed for the test CmPicture_GetCreatorAndLicenseForPicture + string tempFolder = Path.GetTempPath(); + m_internalPath = Path.Combine(tempFolder, Guid.NewGuid().ToString() + ".jpg"); } /// ------------------------------------------------------------------------------------- @@ -74,6 +79,8 @@ protected override void CreateTestData() public override void TestTearDown() { FileUtils.Manager.Reset(); + if (File.Exists(m_internalPath)) + File.Delete(m_internalPath); base.TestTearDown(); } @@ -343,6 +350,25 @@ public void CmPicture_GetTextRepOfPicture() Assert.AreEqual("MyRef", figParams[6], "Picture reference should be exported."); } + /// ------------------------------------------------------------------------------------- + /// + /// Test ability to get creator and license of a picture. + /// + /// ------------------------------------------------------------------------------------- + [Test] + public void CmPicture_GetCreatorAndLicenseForPicture() + { + // Copy the test image penguin.jpg from TestData to a temp file located at m_internalPath + string penguinPath = Path.Combine(TestDirectoryFinder.TestDataDirectory, "penguin.jpg"); + File.Copy(penguinPath, m_internalPath, overwrite: true); + + MetadataCore metadata = new MetadataCore(); + metadata.Creator = "test creator"; + metadata.CopyrightNotice = "test copyright"; + metadata.Write(m_pict.PictureFileRA.AbsoluteInternalPath, false); + Assert.AreEqual("test creator", ((CmPicture)m_pict).CreatorTSS.ToString()); + Assert.AreEqual("test copyright",((CmPicture)m_pict).LicenseTSS.ToString()); + } /// ------------------------------------------------------------------------------------- /// /// Test ability to update the properties of a picture, given a file, folder, etc.