private void CheckForSampleTextChanges(ApiRequest request)
{
if (_sampleTextsWatcher == null)
{
if (string.IsNullOrEmpty(request.CurrentCollectionSettings?.SettingsFilePath))
{
// We've had cases (BL-4744) where this is apparently called before CurrentCollectionSettings is
// established. I'm not sure how this can happen but if we haven't even established a current collection
// yet I think it's pretty safe to say its sample texts haven't changed since we last read them.
request.ReplyWithText("no");
return;
}
var path = Path.Combine(Path.GetDirectoryName(request.CurrentCollectionSettings.SettingsFilePath), "Sample Texts");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
_sampleTextsWatcher = new FileSystemWatcher {
Path = path
};
_sampleTextsWatcher.Created += SampleTextsOnChange;
_sampleTextsWatcher.Changed += SampleTextsOnChange;
_sampleTextsWatcher.Renamed += SampleTextsOnChange;
_sampleTextsWatcher.Deleted += SampleTextsOnChange;
_sampleTextsWatcher.EnableRaisingEvents = true;
}
lock (_sampleTextsWatcher)
{
var hasChanged = _sampleTextsChanged;
// Reset the changed flag.
// NOTE: we are only resetting the flag if it was "true" when we checked in case the FileSystemWatcher detects a change
// after we check the flag but we reset it to false before we check again.
if (hasChanged)
{
_sampleTextsChanged = false;
}
request.ReplyWithText(hasChanged ? "yes" : "no");
}
}