/// <summary> /// Start a drag&drop operation with the given IFileSearchResults /// </summary> /// <param name="searchResults"></param> /// <param name="overallItemAudioType"></param> /// <returns></returns> private DragDropEffects StartDragFileSearchResults(IEnumerable <IFileSearchResult> searchResults, AudioSearchResultType overallItemAudioType, AresTargetDirectoryProvider targetDirectoryProvider) { List <DraggedItem> draggedFiles = new List <DraggedItem>(); foreach (IFileSearchResult fileSearchResult in searchResults) { // Create a new DraggedItem (dragged file/folder) DraggedItem draggedFile = new DraggedItem(); // Set item & node type for the file draggedFile.ItemType = overallItemAudioType == AudioSearchResultType.MusicFile ? FileType.Music : FileType.Sound; draggedFile.NodeType = DraggedItemType.File; // Determine the relative path where the downloaded file will be placed string relativeDownloadPath = targetDirectoryProvider.GetFolderWithinLibrary(fileSearchResult); draggedFile.RelativePath = System.IO.Path.Combine(relativeDownloadPath, fileSearchResult.Filename); draggedFile.Title = fileSearchResult.Title; draggedFiles.Add(draggedFile); } // Start a file/folder drag & drop operation for those files FileDragInfo info = new FileDragInfo(); info.Source = FileSource.Online; info.DraggedItems = draggedFiles; info.TagsFilter = new TagsFilter(); return(DoDragDrop(info, DragDropEffects.Copy)); }
private void Editor_DragDrop(object sender, DragEventArgs e) { int row = 0; if (ContainerControl.PerformDrop(e, out row)) { return; } FileDragInfo info = e.Data.GetData(typeof(FileDragInfo)) as FileDragInfo; if (info != null && info.DraggedItems != null) { if (info.DraggedItems.Count > 1 || info.DraggedItems[0].NodeType == DraggedItemType.Directory) { System.Threading.CancellationTokenSource tokenSource = new System.Threading.CancellationTokenSource(); TaskProgressMonitor monitor = new TaskProgressMonitor(this, StringResources.AddingFiles, tokenSource); monitor.IncreaseProgress(0.1, StringResources.GettingTitles); var task = DragAndDrop.GetElementsFromDroppedItemsAsync(info, tokenSource.Token, monitor); task.ContinueWith((t) => { monitor.Close(); try { var result = task.Result; if (result != null) { ContainerControl.AddElements(result, row); } } catch (AggregateException) { } }, tokenSource.Token, System.Threading.Tasks.TaskContinuationOptions.OnlyOnRanToCompletion, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); task.ContinueWith((t) => { monitor.Close(); }, tokenSource.Token, System.Threading.Tasks.TaskContinuationOptions.NotOnRanToCompletion, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); } else { List <Ares.Data.IElement> elements = new List <Ares.Data.IElement>(DragAndDrop.GetElementsFromDroppedItems(info)); ContainerControl.AddElements(elements, row); } } }
private static IEnumerable <IElement> DoGetElementsFromDroppedItems(FileDragInfo dragInfo, String musicDirectory, String soundDirectory, System.Threading.CancellationToken token, Ares.ModelInfo.IProgressMonitor progressMonitor) { Ares.TagsImport.SequentialProgressMonitor monitor1 = null, monitor2 = null; if (progressMonitor != null) { monitor1 = new TagsImport.SequentialProgressMonitor(progressMonitor, 0.1, 9.9); } HashSet <String> allowedItems = null; HashSet <String> unallowedItems = null; if (dragInfo.TagsFilter != null && dragInfo.TagsFilter.FilterMode != TagsFilterMode.NoFilter) { IList <String> files = null; try { var dbRead = Ares.Tags.TagsModule.GetTagsDB().ReadInterface; if (dragInfo.TagsFilter.FilterMode == TagsFilterMode.NormalFilter) { switch (dragInfo.TagsFilter.TagCategoryCombination) { case TagCategoryCombination.UseOneTagOfEachCategory: files = dbRead.GetAllFilesWithAnyTagInEachCategory(dragInfo.TagsFilter.TagsByCategories); break; case TagCategoryCombination.UseAnyTag: { HashSet <int> allTags = new HashSet <int>(); foreach (var entry in dragInfo.TagsFilter.TagsByCategories) { allTags.UnionWith(entry.Value); } files = dbRead.GetAllFilesWithAnyTag(allTags); } break; case TagCategoryCombination.UseAllTags: default: { HashSet <int> allTags = new HashSet <int>(); foreach (var entry in dragInfo.TagsFilter.TagsByCategories) { allTags.UnionWith(entry.Value); } files = dbRead.GetAllFilesWithAllTags(allTags); } break; } if (files != null) { allowedItems = new HashSet <string>(); allowedItems.UnionWith(files); } } else { files = dbRead.GetAllFilesWithAnyTag(); if (files != null) { unallowedItems = new HashSet <string>(); unallowedItems.UnionWith(files); } } } catch (Ares.Tags.TagsDbException) { files = null; } } Dictionary <string, DraggedItem> uniqueItems = new Dictionary <string, DraggedItem>(); foreach (DraggedItem item in dragInfo.DraggedItems) { AddItemsToSet(uniqueItems, item, allowedItems, unallowedItems, musicDirectory, soundDirectory, token); token.ThrowIfCancellationRequested(); if (monitor1 != null) { monitor1.IncreaseProgress(100.0 / dragInfo.DraggedItems.Count); } } if (progressMonitor != null) { monitor2 = new TagsImport.SequentialProgressMonitor(progressMonitor, 10.0, 90.0); } foreach (DraggedItem item in uniqueItems.Values) { yield return(CreateFileElement(item, musicDirectory, soundDirectory)); token.ThrowIfCancellationRequested(); if (monitor2 != null) { monitor2.IncreaseProgress(100.0 / uniqueItems.Count); } } }
public static System.Threading.Tasks.Task <IList <IElement> > GetElementsFromDroppedItemsAsync(FileDragInfo dragInfo, System.Threading.CancellationToken token, Ares.ModelInfo.IProgressMonitor progressMonitor) { String musicDirectory = Settings.Settings.Instance.MusicDirectory; String soundDirectory = Settings.Settings.Instance.SoundDirectory; return(System.Threading.Tasks.Task.Factory.StartNew(() => { try { var result = new List <IElement>(DoGetElementsFromDroppedItems(dragInfo, musicDirectory, soundDirectory, token, progressMonitor)); return (IList <IElement>)result; } catch (OperationCanceledException) { return null; } catch (AggregateException) { return null; } })); }
public static IEnumerable <IElement> GetElementsFromDroppedItems(FileDragInfo dragInfo) { return(DoGetElementsFromDroppedItems(dragInfo, Settings.Settings.Instance.MusicDirectory, Settings.Settings.Instance.SoundDirectory, System.Threading.CancellationToken.None, null)); }