Skip to content

Commit

Permalink
changed the writelock to not be class member (so we don't leave it ha…
Browse files Browse the repository at this point in the history
…nging around)
  • Loading branch information
bobeaton committed Jun 30, 2024
1 parent 81dc857 commit ec87207
Showing 1 changed file with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public partial class BackTranslationHelperForm : Form, IBackTranslationHelperDat
private readonly ParatextBackTranslationHelperPlugin _plugin;
private Action<BackTranslationHelperModel> _updateControls;
private BackTranslationHelperModel _model;
private IWriteLock _writeLock = null;
private readonly Action<IVerseRef> _setSyncReferenceGroup;
private bool _isNotInFocus;

Expand Down Expand Up @@ -533,7 +532,6 @@ static bool IsInline(IUSFMMarkerToken token)

private void GetNewReference(IVerseRef newReference)
{
Unlock();
_verseReference = newReference;
BackTranslationHelperModel model = null; // means query the interface to get the data
var cursor = Cursor;
Expand Down Expand Up @@ -640,8 +638,21 @@ private void TranslateEntireChapterMenuItem_Click(object sender, EventArgs e)
var translatorIndex = backTranslationHelperCtrl.TheTranslators.IndexOf(firstWithOwnCreds);
string keyBookChapter = null, startingKeyBookChapter = GetBookChapterKey(_verseReference);
backTranslationHelperCtrl.IsPaused = disableActivateRefreshUntilNextVerse = true; // something in the below causes the window to lose focus, which causes us to reprocess all the converters

IWriteLock writeLock = null;
try
{
// first have to get the lock (bkz if something was changed via Ptx and not saved, then the user
// will need to either save it or throw it away, which would force us to need to do a requery)
if (!AcquireWriteLock(ref writeLock))
{
// it won't still be null if the user chose 'Yes' or 'No' to Paratext dialog that asks,
// "Do you want to save the changes?", but it might if the user doesn't have edit permissions.
// if we still don't have it, it means the user doesn't have the privilege to change the target project
MessageBox.Show($"You don't have edit privilege on this chapter: {_verseReference} of the {_projectTarget.ShortName} project");
return;
}

var verseReferenceNext = _verseReference;
SortedDictionary<string, List<IUSFMToken>> vrefTokensTarget = null;
do
Expand Down Expand Up @@ -943,7 +954,7 @@ private static string GetBookChapterVerseRangeKey(IVerseRef verseReference)

private void ReleaseRequested(IWriteLock obj)
{
Unlock();
Unlock(obj);
}

string IBackTranslationHelperDataSource.ProjectName
Expand Down Expand Up @@ -1028,18 +1039,23 @@ private void MoveToNext()

bool IBackTranslationHelperDataSource.WriteToTarget(string text)
{
IWriteLock writeLock = null;
try
{
System.Diagnostics.Debug.WriteLine("PtxBTH: In IBackTranslationHelperDataSource.WriteToTarget");

_buttonPressed = ButtonPressed.WriteToTarget;

var vrefTokensTarget = CalculateTargetTokens(_verseReference, _versesReference, text, UsfmTokensSource, UsfmTokensTarget);
if (vrefTokensTarget == null)
// first have to get the lock (bkz if something was changed via Ptx and not saved, then the user will
// need to either save it or throw it away, the former of which would force us to need to do a requery)
if (!AcquireWriteLock(ref writeLock))
{
// if we don't already have it... it's probably because something was changed in the project
// external to this form (we lose the data if we get activated).
System.Diagnostics.Debug.WriteLine($"PtxBTH: RequeryWarning: _verseReference: {_verseReference}, _versesReference: {_versesReference}");
// it won't still be null if the user chose 'Yes' or 'No' to Paratext dialog that asks,
// "Do you want to save the changes?", but it might if the user doesn't have edit permissions.
// if we still don't have it, it means the user doesn't have edit permissions on the target project
MessageBox.Show($"You don't have edit privilege on this chapter: {_verseReference} of the {_projectTarget.ShortName} project");
return false;
}

// Note: if the Target Translation textbox has modified data, the current implementation shouldn't
// overwrite it (see UpdateData in BackTranslationHelperCtrl.cs, which shows that if it's
Expand All @@ -1064,12 +1080,11 @@ bool IBackTranslationHelperDataSource.WriteToTarget(string text)
return false;
}

private bool WriteToTarget(string text, SortedDictionary<string, List<IUSFMToken>> vrefTokensTarget)
private bool AcquireWriteLock(ref IWriteLock writeLock)
{
try
{
if (vrefTokensTarget == null)
return false;
writeLock = _projectTarget.RequestWriteLock(_plugin, ReleaseRequested, _verseReference.BookNum, _verseReference.ChapterNum);
return writeLock != null;
}

AreWeChangingTheTarget = true;
if (_writeLock == null)
Expand Down Expand Up @@ -1297,12 +1312,12 @@ private void BackTranslationHelperForm_FormClosing(object sender, FormClosingEve
Dispose();
}

private void Unlock()
private void Unlock(IWriteLock writeLock)
{
if (_writeLock != null)
if (writeLock != null)
{
IWriteLock temp = _writeLock;
_writeLock = null; // to prevent it being called twice while freeing the lock
IWriteLock temp = writeLock;
writeLock = null; // to prevent it being called twice while freeing the lock
temp.Dispose();
}
}
Expand Down

0 comments on commit ec87207

Please sign in to comment.