private void GetArtworkExec(object sender, ExecutedRoutedEventArgs e) { AutoDownloader autoDownloader = null; if (SelectedItems.Count > 1 && Properties.Settings.Default.FileBrowseAutoDownload) { autoDownloader = new AutoDownloader(); } else { //Warn if there are a lot of selected items if (SelectedItems.Count > Properties.Settings.Default.EnqueueWarning) { EnqueueWarning enqueueWarning = new EnqueueWarning(); enqueueWarning.Owner = Window.GetWindow(this); enqueueWarning.NumberToEnqueue = SelectedItems.Count; if (!enqueueWarning.ShowDialog().GetValueOrDefault()) { //Cancelled return; } //Trim the selection back to the number to enqueue while (SelectedItems.Count > enqueueWarning.NumberToEnqueue) { SelectedItems.RemoveAt(SelectedItems.Count - 1); } } } //The art file search pattern is used as the default path to save the found image to, but first: // *In case of alternates, use the first alternate only. string artFileSearchPattern = ImagePathPattern.Split(new[] { '|' }, 2)[0]; // *Don't substitute placeholders, but do substitute recursive path matching with the simplest solution to it, just putting saving to the immediate subfolder artFileSearchPattern = artFileSearchPattern.Replace("**\\", "").Replace("**/", ""); // *Also replace a wildcarded extension if (artFileSearchPattern.EndsWith(".*")) { artFileSearchPattern = artFileSearchPattern.Substring(0, artFileSearchPattern.Length - 2) + ".%extension%"; } // *If the pattern ends in just a wildcard, replace with %name%.%extension% else if (artFileSearchPattern.EndsWith("*")) { artFileSearchPattern = artFileSearchPattern.Substring(0, artFileSearchPattern.Length - 1) + "%name%.%extension%"; } //Replace other wildcards with the %name%, so that for local files search they become wildcards again artFileSearchPattern = artFileSearchPattern.Replace("*", "%name%"); var cascade = new Common.WindowCascade(); foreach (Album album in SelectedItems) { //If the image path is relative, get an absolute path for it. string rootedArtFileSearchPattern; if (Path.IsPathRooted(artFileSearchPattern)) { rootedArtFileSearchPattern = artFileSearchPattern; } else { rootedArtFileSearchPattern = Path.Combine(album.BasePath, artFileSearchPattern); } if (autoDownloader != null) { album.ArtFile = rootedArtFileSearchPattern; //The destination filename to download to autoDownloader.Add(album); } else { ArtSearchWindow searchWindow = Common.NewSearchWindow(Window.GetWindow(this) as IAppWindow); cascade.Arrange(searchWindow); searchWindow.SetDefaultSaveFolderPattern(rootedArtFileSearchPattern, true); //Default save to the location where the image was searched for. searchWindow.Search(album.Artist, album.Name); //Kick off the search. //Watch for the window being closed to update the status of the artwork mSearchWindowAlbumLookup.Add(searchWindow, album); searchWindow.Closed += OnSearchWindowClosed; } } if (autoDownloader != null) { autoDownloader.Show(); } }
private void ArtFileSearchWorker() { try { do { mArtFileSearchTrigger.WaitOne(); //Wait until there is work to do State = BrowserState.FindingArt; do //Loop through all the queued art. { Album album; lock (mArtFileSearchQueue) { if (mArtFileSearchQueue.Count == 0) { break; //Nothing to search for, so go back and wait until there is. } else { album = mArtFileSearchQueue.Dequeue(); } } System.Diagnostics.Debug.Assert(album.ArtFileStatus == ArtFileStatus.Queued, "Expecting the album to be queued for searching"); album.ArtFileStatus = ArtFileStatus.Searching; try { ProgressText = String.Format("Finding art... {0} / {1}", album.Artist, album.Name); foreach (string imagePathPatternAlternate in ImagePathPattern.Split('|')) { string artFileSearchPattern = Common.SubstitutePlaceholders(imagePathPatternAlternate, album.Artist, album.Name); Regex artFileCheckPattern = MakeCheckPattern(imagePathPatternAlternate, album.Artist, album.Name); if (!Path.IsPathRooted(artFileSearchPattern)) { artFileSearchPattern = Path.Combine(album.BasePath, artFileSearchPattern); } foreach (string artFile in Common.ResolvePathPattern(artFileSearchPattern)) { if (artFileCheckPattern.IsMatch(artFile)) { album.SetArtFile(artFile); break; //Only use the first art file that matches, if there are multiple matches. } } //If a matching art file is found, don't search further alternates if (album.ArtFileStatus == ArtFileStatus.Present) { break; } } } catch (Exception) { album.ArtFileStatus = ArtFileStatus.Unknown; //It might not be missing, we just haven't found it before hitting an exception } if (album.ArtFileStatus != ArtFileStatus.Present) //If it wasn't found, then it's missing. { album.ArtFileStatus = ArtFileStatus.Missing; } } while (true); State = BrowserState.Done; } while (true); } catch (ThreadAbortException) { State = BrowserState.Stopped; return; } }