Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download DBL resources data provider #1282

Merged
merged 10 commits into from
Nov 13, 2024
3 changes: 3 additions & 0 deletions c-sharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Paranext.DataProvider.Checks;
using Paranext.DataProvider.NetworkObjects;
using Paranext.DataProvider.Projects;
using Paranext.DataProvider.Projects.DigitalBibleLibrary;
using Paranext.DataProvider.Services;
using Paranext.DataProvider.Users;
using Paratext.Data;
Expand Down Expand Up @@ -32,10 +33,12 @@ public static async Task Main()

var paratextFactory = new ParatextProjectDataProviderFactory(papi, paratextProjects);
var checkRunner = new CheckRunner(papi);
var dblResources = new DblResourcesDataProvider(papi);
var paratextRegistrationService = new ParatextRegistrationService(papi);
await Task.WhenAll(
paratextFactory.InitializeAsync(),
checkRunner.RegisterDataProviderAsync(),
dblResources.RegisterDataProviderAsync(),
paratextRegistrationService.InitializeAsync()
);

Expand Down
145 changes: 145 additions & 0 deletions c-sharp/Projects/DigitalBibleLibrary/DblDownloadableDataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System.Text.Json;
using Paratext.Data;
using Paratext.Data.Archiving;
using Paratext.Data.Users;

namespace Paranext.DataProvider.Projects.DigitalBibleLibrary;

internal class DblResourcesDataProvider(PapiClient papiClient)
: NetworkObjects.DataProvider("platformBibleDownloadResources.dblResourcesProvider", papiClient)
{
#region Internal classes

private class DblResourceData(
string DblEntryUid,
string DisplayName,
string FullName,
string BestLanguageName,
long Size,
bool Installed,
bool UpdateAvailable
)
{
public string DblEntryUid { get; set; } = DblEntryUid;
public string DisplayName { get; set; } = DisplayName;
public string FullName { get; set; } = FullName;
public string BestLanguageName { get; set; } = BestLanguageName;
public long Size { get; set; } = Size;
public bool Installed { get; set; } = Installed;
public bool UpdateAvailable { get; set; } = UpdateAvailable;
}

#endregion

#region Consts and member variables

private List<InstallableResource> _resources = [];

#endregion

#region DataProvider methods

protected override List<(string functionName, Delegate function)> GetFunctions()
{
return
[
("getDblResources", GetDblResources),
("installDblResource", InstallDblResource),
("uninstallDblResource", UninstallDblResource),
];
}

protected override Task StartDataProviderAsync()
{
return Task.CompletedTask;
}

#endregion

#region Private properties and methods

private void FetchAvailableDBLResources()
{
_resources = InstallableDBLResource.GetInstallableDBLResources(
RegistrationInfo.DefaultUser,
new DBLRESTClientFactory(),
new DblProjectDeleter(),
new DblMigrationOperations(),
new DblResourcePasswordProvider()
);
}

private List<DblResourceData> GetDblResources(JsonElement _ignore)
{
if (!RegistrationInfo.DefaultUser.IsValid)
{
throw new Exception(
$"User registration is not valid. Cannot retrieve resources from DBL"
);
}

FetchAvailableDBLResources();

return _resources
.Select(resource => new DblResourceData(
resource.DBLEntryUid.Id,
resource.DisplayName,
resource.FullName,
resource.BestLanguageName,
resource.Size,
resource.Installed,
resource.IsNewerThanCurrentlyInstalled()
))
.ToList();
}

private bool TryFindResource(string dblEntryUid, out InstallableResource? resource)
{
resource = _resources?.FirstOrDefault(r => r.DBLEntryUid.Id == dblEntryUid);
return resource != null;
}

private bool InstallDblResource(string DBLEntryUid)
{
if (!TryFindResource(DBLEntryUid, out var installableResource))
{
throw new Exception($"Resource not available from DBL");
}

if (installableResource == null)
return false;
if (installableResource.Installed && !installableResource.IsNewerThanCurrentlyInstalled())
return true;

// Note that we don't get any info telling if the installation succeeded or failed
installableResource.Install();

ScrTextCollection.RefreshScrTexts();
SendDataUpdateEvent("DblResources", "DBL resources data updated");
return true;
}

private bool UninstallDblResource(string DBLEntryUid)
{
if (!TryFindResource(DBLEntryUid, out var installableResource))
{
throw new Exception($"Resource not available from DBL");
}

if (installableResource == null)
return false;
if (!installableResource.Installed)
return false;

ScrTextCollection.DeleteProject(
// Note that we don't get any info telling if uninstalling succeeded or failed
installableResource.ExistingScrText ?? installableResource.ExistingDictionary
);

ScrTextCollection.RefreshScrTexts();
SendDataUpdateEvent("DblResources", "DBL resources data updated");
return true;
}

#endregion
}
36 changes: 36 additions & 0 deletions c-sharp/Projects/DigitalBibleLibrary/DblMigrationOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Paratext.Data;
using Paratext.Data.Archiving;
using Paratext.Data.Languages;

namespace Paranext.DataProvider.Projects.DigitalBibleLibrary;

public class DblMigrationOperations : IMigrationOperations
{
/// <summary>
/// See `Paratext.Migration.PTMigration.Migrate` for steps involved in migrating data
/// </summary>
public UnsupportedReason MigrateProjectIfNeeded(ScrText scrText)
{
return scrText.NeedsMigration
? UnsupportedReason.CannotUpgrade
: UnsupportedReason.Supported;
}

/// <summary>
/// Adapted from `Paratext.Migration.MigrateLanguage`
/// </summary>
public LanguageId DetermineBestLangIdToUseForResource(
string ldmlLanguageId,
string dblLanguageId
)
{
LanguageId ethnologueDblLanguageId = LanguageId.FromEthnologueCode(dblLanguageId);
if (string.IsNullOrEmpty(ldmlLanguageId))
return ethnologueDblLanguageId;

LanguageId ethnologueLdmlLanguageId = LanguageId.FromEthnologueCode(ldmlLanguageId);
if (ethnologueLdmlLanguageId.Code == ethnologueDblLanguageId.Code)
return ethnologueLdmlLanguageId;
return ethnologueDblLanguageId;
}
}
20 changes: 20 additions & 0 deletions c-sharp/Projects/DigitalBibleLibrary/DblProjectDeleter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Paratext.Data;
using Paratext.Data.Archiving;
using Paratext.Data.ProjectComments;
using Paratext.Data.Repository;

namespace Paranext.DataProvider.Projects.DigitalBibleLibrary;

/// <summary>
/// Adapted from `Paratext.ProjectMenu.DeleteProjectForm.DeleteProject`
/// </summary>
public class DblProjectDeleter : IProjectDeleter
{
public void DeleteProject(ScrText scrText)
{
CommentManager.RemoveCommentManager(scrText);
VersioningManager.RemoveVersionedText(scrText);
if (!scrText.Settings.IsMarbleResource)
ScrTextCollection.DeleteProject(scrText);
}
}
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"biblionexus",
"camelcase",
"consts",
"deleter",
"deuterocanon",
"dockbox",
"electronmon",
Expand Down
2 changes: 1 addition & 1 deletion src/extension-host/data/menu.data.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"localizeNotes": "Application main menu > Project > Download/Install Resources",
"group": "platform.projectResources",
"order": 1,
"command": "platform.downloadAndInstallResources"
"command": "paratextBibleDownloadResources.openDownloadResources"
},
{
"label": "%mainMenu_settings%",
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/platform-bible-menu.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const supportAndDevelopmentMenuLayout: LocalizedMainMenu = {
localizeNotes: 'Main application menu > Paratext column > Download/Install Resources',
group: 'paratext.sendReceive',
order: 1,
command: 'platform.downloadAndInstallResources',
command: 'paratextBibleDownloadResources.openDownloadResources',
},
{
label: 'Open Project...',
Expand Down
Loading