Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/SIL.LCModel.Utils/SIL.LCModel.Utils.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
Expand Down
78 changes: 78 additions & 0 deletions src/SIL.LCModel/DomainImpl/CmPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -312,6 +313,83 @@ public ITsString PathNameTSS
}
}

/// ------------------------------------------------------------------------------------
/// <summary>
/// Method called to implement virtual property. Returns license metadata of associated
/// file.
/// </summary>----------------------------------------------------------------------------------
[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)))));
}
}

/// ------------------------------------------------------------------------------------
/// <summary>
/// Method called to implement virtual property. Returns creator metadata of associated
/// file. (LT-7104 requested internal path instead of original path.)
/// </summary>----------------------------------------------------------------------------------
[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));
}
}

/// <summary>
/// Get the sense number of the owning LexSense.
/// ENHANCE DamienD: register this property as modified when its dependencies change
Expand Down
1 change: 1 addition & 0 deletions src/SIL.LCModel/SIL.LCModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="protobuf-net" Version="2.4.6" />
<PackageReference Include="SharpZipLib" Version="1.4.0" />
<PackageReference Include="SIL.Core.Desktop" Version="17.0.0-*" />
<PackageReference Include="SIL.Lexicon" Version="17.0.0-*" />
<PackageReference Include="SIL.ReleaseTasks" Version="2.5.0" PrivateAssets="All" />
<PackageReference Include="SIL.WritingSystems" Version="17.0.0-*" />
Expand Down
28 changes: 27 additions & 1 deletion tests/SIL.LCModel.Tests/DomainImpl/CmPictureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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");
}

/// -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -343,6 +350,25 @@ public void CmPicture_GetTextRepOfPicture()
Assert.AreEqual("MyRef", figParams[6], "Picture reference should be exported.");
}

/// -------------------------------------------------------------------------------------
/// <summary>
/// Test ability to get creator and license of a picture.
/// </summary>
/// -------------------------------------------------------------------------------------
[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());
}
/// -------------------------------------------------------------------------------------
/// <summary>
/// Test ability to update the properties of a picture, given a file, folder, etc.
Expand Down
Loading