private static bool UserFindFile(Uri uri, out Uri newUri, SelectFileFilterOptions options) { newUri = uri; var dlg = new OpenFileDialog { CheckFileExists = true, CheckPathExists = true, Multiselect = false, Title = string.Format( "Find or Replace {0}", Path.GetFileName(uri.LocalPath)) }; if (options == SelectFileFilterOptions.ExactMatch) { string fileName = Path.GetFileName(uri.LocalPath); dlg.Filter = fileName + "|" + fileName; } else if (options == SelectFileFilterOptions.ExtensionMatch) { string extension = Path.GetExtension(uri.LocalPath); dlg.Filter = extension + "|" + extension; } if (dlg.ShowDialog(DialogUtils.GetActiveWindow()) == true) { newUri = new Uri(dlg.FileName); s_localPathMap.Add(uri.LocalPath, newUri); return(true); } return(false); }
private static bool UserFindFile(Uri uri, out Uri newUri, SelectFileFilterOptions options) { newUri = uri; var dlg = new OpenFileDialog { CheckFileExists = true, CheckPathExists = true, Multiselect = false, Title = string.Format( "Find or Replace {0}", Path.GetFileName(uri.LocalPath)) }; if (options == SelectFileFilterOptions.ExactMatch) { string fileName = Path.GetFileName(uri.LocalPath); dlg.Filter = fileName + "|" + fileName; } else if (options == SelectFileFilterOptions.ExtensionMatch) { string extension = Path.GetExtension(uri.LocalPath); dlg.Filter = extension + "|" + extension; } if (dlg.ShowDialog(DialogUtils.GetActiveWindow()) == true) { newUri = new Uri(dlg.FileName); s_localPathMap.Add(uri.LocalPath, newUri); return true; } return false; }
// Implements interactive portions of Find(), but is called from a separate thread to // avoid recursion due to the dialog boxes. private static bool? QueryUser(Uri uri, Uri suggestedUri, out Uri newUri, SelectFileFilterOptions options) { // If the user cancels a sub-dialog box, reopen the first dialog box. while (true) { // Ask the user what we should do. // There are a two fewer options and slightly reorganized dialog box if there // is no suggested replacement for the missing file. var vm = new FindFileDialogViewModel() { Action = s_lastAction, OriginalPath = uri.LocalPath, SuggestedPath = suggestedUri != null ? suggestedUri.LocalPath : null, }; bool? res = DialogUtils.ShowDialogWithViewModel<FindFileDialog>(vm); s_lastAction = res == false ? FindFileAction.Quit : vm.Action; switch (s_lastAction) { case FindFileAction.AcceptSuggestion: newUri = suggestedUri; return true; case FindFileAction.AcceptAllSuggestions: newUri = suggestedUri; return true; case FindFileAction.SearchDirectory: if (SearchForFile(uri, out newUri, true)) return true; continue; case FindFileAction.SearchDirectoryForAll: if (SearchForFile(uri, out newUri, false)) return true; continue; case FindFileAction.UserSpecify: if (UserFindFile(uri, out newUri, options)) return true; continue; case FindFileAction.Ignore: newUri = uri; return false; case FindFileAction.IgnoreAll: newUri = uri; return false; case FindFileAction.Quit: newUri = uri; return null; } throw new InvalidOperationException("unhandled FindFileAction enum"); } }
/// <summary> /// Attempts to find the file specified by the given uri. A new replacement uri may be /// created, based on a user's decisions.</summary> /// <param name="uri">The uri to find, which may or may not be valid. It must not be null.</param> /// <param name="newUri">Will either be 'uri' or a new valid uri. It will not be set to null.</param> /// <returns>true if 'uri' exists or if a new uri was found. Otherwise, false.</returns> public static bool? Find(Uri uri, out Uri newUri, SelectFileFilterOptions options) { bool? result = false; lock (s_lockObject) { // Default new uri. newUri = uri; // Check for an exact match that the user has already made. This is very fast, so might // as well do this first. Uri mappedUri; if (s_localPathMap.TryGetValue(uri.LocalPath, out mappedUri)) { newUri = mappedUri; return true; } // Check if 'uri' is valid as-is. if (File.Exists(uri.LocalPath)) return true; // Then check if we can automatically correct the uri based on directory mapping. Uri suggestedUri = FindSuggestion(uri); bool accept = s_lastAction == FindFileAction.AcceptAllSuggestions || s_lastAction == FindFileAction.SearchDirectoryForAll || s_lastAction == FindFileAction.IgnoreAll; if (suggestedUri != null && accept) { newUri = suggestedUri; return true; } // Check if we can automatically do a file search. if (s_lastAction == FindFileAction.SearchDirectoryForAll || s_lastAction == FindFileAction.IgnoreAll) { bool fileFound = false; Uri foundUri = null; Application.Current.Dispatcher.Invoke((Action)(() => { fileFound = SearchForFile(uri, out foundUri, false); })); if (fileFound) { newUri = foundUri; return true; } } if (s_lastAction != FindFileAction.IgnoreAll) { Uri tempUri = null; Application.Current.Dispatcher.Invoke((Action)(() => { result = QueryUser(uri, suggestedUri, out tempUri, options); })); newUri = tempUri; } } return result; }
// Implements interactive portions of Find(), but is called from a separate thread to // avoid recursion due to the dialog boxes. private static bool?QueryUser(Uri uri, Uri suggestedUri, out Uri newUri, SelectFileFilterOptions options) { // If the user cancels a sub-dialog box, reopen the first dialog box. while (true) { // Ask the user what we should do. // There are a two fewer options and slightly reorganized dialog box if there // is no suggested replacement for the missing file. var vm = new FindFileDialogViewModel() { Action = s_lastAction, OriginalPath = uri.LocalPath, SuggestedPath = suggestedUri != null ? suggestedUri.LocalPath : null, }; bool?res = DialogUtils.ShowDialogWithViewModel <FindFileDialog>(vm); s_lastAction = res == false ? FindFileAction.Quit : vm.Action; switch (s_lastAction) { case FindFileAction.AcceptSuggestion: newUri = suggestedUri; return(true); case FindFileAction.AcceptAllSuggestions: newUri = suggestedUri; return(true); case FindFileAction.SearchDirectory: if (SearchForFile(uri, out newUri, true)) { return(true); } continue; case FindFileAction.SearchDirectoryForAll: if (SearchForFile(uri, out newUri, false)) { return(true); } continue; case FindFileAction.UserSpecify: if (UserFindFile(uri, out newUri, options)) { return(true); } continue; case FindFileAction.Ignore: newUri = uri; return(false); case FindFileAction.IgnoreAll: newUri = uri; return(false); case FindFileAction.Quit: newUri = uri; return(null); } throw new InvalidOperationException("unhandled FindFileAction enum"); } }
/// <summary> /// Attempts to find the file specified by the given uri. A new replacement uri may be /// created, based on a user's decisions.</summary> /// <param name="uri">The uri to find, which may or may not be valid. It must not be null.</param> /// <param name="newUri">Will either be 'uri' or a new valid uri. It will not be set to null.</param> /// <returns>true if 'uri' exists or if a new uri was found. Otherwise, false.</returns> public static bool?Find(Uri uri, out Uri newUri, SelectFileFilterOptions options) { bool?result = false; lock (s_lockObject) { // Default new uri. newUri = uri; // Check for an exact match that the user has already made. This is very fast, so might // as well do this first. Uri mappedUri; if (s_localPathMap.TryGetValue(uri.LocalPath, out mappedUri)) { newUri = mappedUri; return(true); } // Check if 'uri' is valid as-is. if (File.Exists(uri.LocalPath)) { return(true); } // Then check if we can automatically correct the uri based on directory mapping. Uri suggestedUri = FindSuggestion(uri); bool accept = s_lastAction == FindFileAction.AcceptAllSuggestions || s_lastAction == FindFileAction.SearchDirectoryForAll || s_lastAction == FindFileAction.IgnoreAll; if (suggestedUri != null && accept) { newUri = suggestedUri; return(true); } // Check if we can automatically do a file search. if (s_lastAction == FindFileAction.SearchDirectoryForAll || s_lastAction == FindFileAction.IgnoreAll) { bool fileFound = false; Uri foundUri = null; Application.Current.Dispatcher.Invoke((Action)(() => { fileFound = SearchForFile(uri, out foundUri, false); })); if (fileFound) { newUri = foundUri; return(true); } } if (s_lastAction != FindFileAction.IgnoreAll) { Uri tempUri = null; Application.Current.Dispatcher.Invoke((Action)(() => { result = QueryUser(uri, suggestedUri, out tempUri, options); })); newUri = tempUri; } } return(result); }