Skip to content

Commit a3b309c

Browse files
authored
Add License and Creator virtual properties to CmPicture (#355)
1 parent e29a84c commit a3b309c

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818

1919
### Added
2020

21+
- [SIL.LCModel] Add new virtual property LicenseTSS in CmPicture, to access info about the picture's copyright and license.
22+
- [SIL.LCModel] Add new virtual property CreatorTSS in CmPicture, to access info about the picture's creator.
2123
- [SIL.LCModel] Add SpecificItemAndFieldName() to ISenseOrEntry
2224
- [SIL.LCModel] Added a parameter to GetBestGuess() and TryGetBestGuess() to do lowercase matching regardless of the occurrence index
2325
- [SIL.LCModel] Add GetCaptionOrHeadword() to CmPicture

src/SIL.LCModel.Utils/SIL.LCModel.Utils.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>

src/SIL.LCModel/DomainImpl/CmPicture.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Diagnostics;
1818
using System.IO;
1919
using System.Linq;
20+
using System.Security;
2021
using SIL.LCModel.Core.Cellar;
2122
using SIL.LCModel.Core.KernelInterfaces;
2223
using SIL.LCModel.Core.Text;
@@ -312,6 +313,83 @@ public ITsString PathNameTSS
312313
}
313314
}
314315

316+
/// ------------------------------------------------------------------------------------
317+
/// <summary>
318+
/// Method called to implement virtual property. Returns license metadata of associated
319+
/// file.
320+
/// </summary>----------------------------------------------------------------------------------
321+
[VirtualProperty(CellarPropertyType.String)]
322+
public ITsString LicenseTSS
323+
{
324+
get
325+
{
326+
var path = PictureFileRA?.AbsoluteInternalPath;
327+
SIL.Core.ClearShare.MetadataCore metadata;
328+
try
329+
{
330+
metadata = SIL.Core.ClearShare.MetadataCore.CreateMetadataCoreFromFile(path);
331+
}
332+
catch
333+
{
334+
// Error getting metadata from path
335+
metadata = null;
336+
}
337+
338+
if (metadata == null)
339+
return null;
340+
341+
var analWs = m_cache.WritingSystemFactory.GetStrFromWs(m_cache.DefaultAnalWs);
342+
var vernWs = m_cache.WritingSystemFactory.GetStrFromWs(m_cache.DefaultVernWs);
343+
344+
// Set Localizer.Default to use L10NSharpLocalizer to localize the license.
345+
ILocalizer oldLocalizer = Localizer.Default;
346+
Localizer.Default = new SIL.Core.Desktop.i18n.L10NSharpLocalizer();
347+
348+
// Get the license in first analysis writing system if available, otherwise first vernacular ws, otherwise English.
349+
var license = metadata.License?.GetMinimalFormForCredits(new[] { analWs, vernWs, "en" }, out _);
350+
if (string.IsNullOrEmpty(metadata.CopyrightNotice) && string.IsNullOrEmpty(license))
351+
return null;
352+
353+
// Reset Localizer default.
354+
Localizer.Default = oldLocalizer;
355+
356+
// We want the short copyright notice, but it isn't safe to ask for if CopyrightNotice is null.
357+
var copyright = string.IsNullOrEmpty(metadata.CopyrightNotice)
358+
? string.Empty
359+
: metadata.ShortCopyrightNotice;
360+
return m_cache.MakeUserTss(SecurityElement.Escape(string.Join(", ", new[] { copyright, license }.Where(txt => !string.IsNullOrEmpty(txt)))));
361+
}
362+
}
363+
364+
/// ------------------------------------------------------------------------------------
365+
/// <summary>
366+
/// Method called to implement virtual property. Returns creator metadata of associated
367+
/// file. (LT-7104 requested internal path instead of original path.)
368+
/// </summary>----------------------------------------------------------------------------------
369+
[VirtualProperty(CellarPropertyType.String)]
370+
public ITsString CreatorTSS
371+
{
372+
get
373+
{
374+
var path = PictureFileRA?.AbsoluteInternalPath;
375+
SIL.Core.ClearShare.MetadataCore metadata;
376+
try
377+
{
378+
metadata = SIL.Core.ClearShare.MetadataCore.CreateMetadataCoreFromFile(path);
379+
}
380+
catch
381+
{
382+
// Error getting metadata from path
383+
metadata = null;
384+
}
385+
386+
if (metadata == null || metadata.Creator == null)
387+
return null;
388+
389+
return m_cache.MakeUserTss(SecurityElement.Escape(metadata.Creator));
390+
}
391+
}
392+
315393
/// <summary>
316394
/// Get the sense number of the owning LexSense.
317395
/// ENHANCE DamienD: register this property as modified when its dependencies change

src/SIL.LCModel/SIL.LCModel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
2323
<PackageReference Include="protobuf-net" Version="2.4.6" />
2424
<PackageReference Include="SharpZipLib" Version="1.4.0" />
25+
<PackageReference Include="SIL.Core.Desktop" Version="17.0.0-*" />
2526
<PackageReference Include="SIL.Lexicon" Version="17.0.0-*" />
2627
<PackageReference Include="SIL.ReleaseTasks" Version="2.5.0" PrivateAssets="All" />
2728
<PackageReference Include="SIL.WritingSystems" Version="17.0.0-*" />

tests/SIL.LCModel.Tests/DomainImpl/CmPictureTests.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.IO;
88
using System.Text;
99
using NUnit.Framework;
10+
using SIL.Core.ClearShare;
1011
using SIL.LCModel.Core.KernelInterfaces;
1112
using SIL.LCModel.Core.Text;
1213
using SIL.LCModel.Core.WritingSystems;
@@ -28,7 +29,7 @@ public class CmPictureTests: MemoryOnlyBackendProviderRestoredForEachTestTestBas
2829
private MockFileOS m_fileOs;
2930
private ICmPictureFactory m_pictureFactory;
3031
private ICmPicture m_pict;
31-
private string m_internalPath = Path.DirectorySeparatorChar + Path.GetRandomFileName();
32+
private string m_internalPath;
3233
private CoreWritingSystemDefinition m_wsGerman;
3334
private CoreWritingSystemDefinition m_wsSpanish;
3435
#endregion
@@ -43,6 +44,10 @@ public override void FixtureSetup()
4344
base.FixtureSetup();
4445
Cache.ServiceLocator.WritingSystemManager.GetOrSet("de", out m_wsGerman);
4546
Cache.ServiceLocator.WritingSystemManager.GetOrSet("es", out m_wsSpanish);
47+
// Initialize internalPath inside a temp folder
48+
// .jpg file extension is needed for the test CmPicture_GetCreatorAndLicenseForPicture
49+
string tempFolder = Path.GetTempPath();
50+
m_internalPath = Path.Combine(tempFolder, Guid.NewGuid().ToString() + ".jpg");
4651
}
4752

4853
/// -------------------------------------------------------------------------------------
@@ -74,6 +79,8 @@ protected override void CreateTestData()
7479
public override void TestTearDown()
7580
{
7681
FileUtils.Manager.Reset();
82+
if (File.Exists(m_internalPath))
83+
File.Delete(m_internalPath);
7784
base.TestTearDown();
7885
}
7986

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

353+
/// -------------------------------------------------------------------------------------
354+
/// <summary>
355+
/// Test ability to get creator and license of a picture.
356+
/// </summary>
357+
/// -------------------------------------------------------------------------------------
358+
[Test]
359+
public void CmPicture_GetCreatorAndLicenseForPicture()
360+
{
361+
// Copy the test image penguin.jpg from TestData to a temp file located at m_internalPath
362+
string penguinPath = Path.Combine(TestDirectoryFinder.TestDataDirectory, "penguin.jpg");
363+
File.Copy(penguinPath, m_internalPath, overwrite: true);
364+
365+
MetadataCore metadata = new MetadataCore();
366+
metadata.Creator = "test creator";
367+
metadata.CopyrightNotice = "test copyright";
368+
metadata.Write(m_pict.PictureFileRA.AbsoluteInternalPath, false);
369+
Assert.AreEqual("test creator", ((CmPicture)m_pict).CreatorTSS.ToString());
370+
Assert.AreEqual("test copyright",((CmPicture)m_pict).LicenseTSS.ToString());
371+
}
346372
/// -------------------------------------------------------------------------------------
347373
/// <summary>
348374
/// Test ability to update the properties of a picture, given a file, folder, etc.

0 commit comments

Comments
 (0)