SIL.FieldWorks.XWorks.DTMenuHandler.InsertMediaFile C# (CSharp) Method

InsertMediaFile() private method

private InsertMediaFile ( object cmd, string filter, string keyCaption, string defaultCaption ) : bool
cmd object
filter string
keyCaption string
defaultCaption string
return bool
		private bool InsertMediaFile(object cmd, string filter, string keyCaption, string defaultCaption)
		{
			ICmObject obj;
			int flid;
			bool fOnPronunciationSlice = CanInsertPictureOrMediaFile(cmd, out flid);
			if (fOnPronunciationSlice)
			{
				obj = m_dataEntryForm.CurrentSlice.Object;
			}
			else
			{
				// Find the first pronunciation object on the current entry, creating it if
				// necessary.
				var le = m_dataEntryForm.Root as ILexEntry;
				if (le == null)
					return false;
				if (le.PronunciationsOS.Count == 0)
				{
					// Ensure that the pronunciation writing systems have been initialized.
					// Otherwise, the crash reported in FWR-2086 can happen!
					int wsPron = Cache.DefaultPronunciationWs;
					UndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW("Undo Create Pronuciation",
						"Redo Create Pronunciation", Cache.ActionHandlerAccessor, () =>
						{
							le.PronunciationsOS.Add(Cache.ServiceLocator.GetInstance<ILexPronunciationFactory>().Create());
						});
				}
				obj = le.PronunciationsOS[0];
				flid = LexPronunciationTags.kflidMediaFiles;
			}
			using (var dlg = new OpenFileDialogAdapter())
			{
				string defaultDirectory = Cache.LangProject.LinkedFilesRootDir;
				dlg.InitialDirectory = m_mediator != null ? m_mediator.PropertyTable.GetStringProperty("InsertMediaFile-LastDirectory", defaultDirectory)
					: defaultDirectory;
				dlg.Filter = filter;
				dlg.FilterIndex = 1;
				if (m_mediator != null && m_mediator.HasStringTable)
					dlg.Title = m_mediator.StringTbl.GetString(keyCaption);
				if (string.IsNullOrEmpty(dlg.Title) || dlg.Title == "*" + keyCaption + "*")
					dlg.Title = defaultCaption;
				dlg.RestoreDirectory = true;
				dlg.CheckFileExists = true;
				dlg.CheckPathExists = true;
				dlg.Multiselect = true;

				DialogResult dialogResult = DialogResult.None;
				while (dialogResult != DialogResult.OK && dialogResult != DialogResult.Cancel)
				{
					dialogResult = dlg.ShowDialog();
					if (dialogResult == DialogResult.OK)
					{
						string[] fileNames = MoveOrCopyFilesController.MoveCopyOrLeaveMediaFiles(dlg.FileNames,
							Cache.LangProject.LinkedFilesRootDir, m_mediator.HelpTopicProvider, Cache.ProjectId.IsLocal);
						string sFolderName = null;
						if (m_mediator != null && m_mediator.HasStringTable)
							sFolderName = m_mediator.StringTbl.GetString("kstidMediaFolder");
						if (string.IsNullOrEmpty(sFolderName) || sFolderName == "*kstidMediaFolder*")
							sFolderName = CmFolderTags.LocalMedia;
						if (!obj.IsValidObject)
							return true; // Probably some other client deleted it while we were choosing the file.

						int chvo = obj.Cache.DomainDataByFlid.get_VecSize(obj.Hvo, flid);
						UndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW(
							xWorksStrings.ksUndoInsertMedia, xWorksStrings.ksRedoInsertMedia,
							Cache.ActionHandlerAccessor, () =>
							{
								foreach (string fileName in fileNames.Where(f => !string.IsNullOrEmpty(f)))
								{
									int hvo = obj.Cache.DomainDataByFlid.MakeNewObject(CmMediaTags.kClassId, obj.Hvo, flid, chvo);
									ICmMedia media = Cache.ServiceLocator.GetInstance<ICmMediaRepository>().GetObject(hvo);
									ICmFolder folder = DomainObjectServices.FindOrCreateFolder(obj.Cache, LangProjectTags.kflidMedia, sFolderName);
									media.MediaFileRA = DomainObjectServices.FindOrCreateFile(folder, fileName);
								}
							});

						if (m_mediator != null)
						{
							string fileName = dlg.FileNames.FirstOrDefault(f => !string.IsNullOrEmpty(f));
							if (fileName != null)
							{
								string directory = Path.GetDirectoryName(fileName);
								m_mediator.PropertyTable.SetProperty("InsertMediaFile-LastDirectory", directory);
								m_mediator.PropertyTable.SetPropertyPersistence("InsertMediaFile-LastDirectory", false);
							}
						}
					}
				}
			}
			return true;
		}