SIL.FieldWorks.FdoUi.CmObjectUi.ConsiderDeletingRelatedFile C# (CSharp) Method

ConsiderDeletingRelatedFile() public static method

public static ConsiderDeletingRelatedFile ( ICmFile file, XCore.Mediator mediator ) : void
file ICmFile
mediator XCore.Mediator
return void
		public static void ConsiderDeletingRelatedFile(ICmFile file, Mediator mediator)
		{
			if (file == null)
				return;
			var refs = file.ReferringObjects;
			if (refs.Count > 1)
				return; // exactly one if only this CmPicture uses it.
			var path = file.InternalPath;
			if (Path.IsPathRooted(path))
				return; // don't delete external file
			string msg = String.Format(FdoUiStrings.ksDeleteFileAlso, path);
			if (MessageBox.Show(Form.ActiveForm, msg, FdoUiStrings.ksDeleteFileCaption, MessageBoxButtons.YesNo,
				MessageBoxIcon.Question)
				!= DialogResult.Yes)
			{
				return;
			}
			if (mediator != null)
			{
				var app = mediator.PropertyTable.GetValue("App") as FwApp;
				if (app != null)
					app.PictureHolder.ReleasePicture(file.AbsoluteInternalPath);
			string fileToDelete = file.AbsoluteInternalPath;
			// I'm not sure why, but if we try to delete it right away, we typically get a failure,
			// with an exception indicating that something is using the file, despite the code above that
			// tries to make our picture cache let go of it.
			// However, waiting until idle seems to solve the problem.
			mediator.IdleQueue.Add(IdleQueuePriority.Low, obj =>
				{
					try
					{
						File.Delete(fileToDelete);
					}
					catch (IOException)
					{
						// If we can't actually delete the file for some reason, don't bother the user complaining.
					}
					return true; // task is complete, don't try again.
				});
			file.Delete();
		}
		}