Пример #1
0
        private void findInDBButton_Click(object sender, EventArgs e)
        {
            System.Threading.CancellationTokenSource tokenSource = new System.Threading.CancellationTokenSource();
            TaskProgressMonitor monitor = new TaskProgressMonitor(this, StringResources.FindingFilesInDB, tokenSource);

            monitor.IncreaseProgress(0.1, StringResources.ExtractingMusicIds);
            Ares.TagsImport.SequentialProgressMonitor musicIdMonitor = new TagsImport.SequentialProgressMonitor(monitor, 0.1, 89.9);
            var task  = Ares.TagsImport.MusicIdentification.UpdateMusicIdentificationAsync(musicIdMonitor, m_Files, Ares.Settings.Settings.Instance.MusicDirectory, tokenSource.Token);
            var task2 = task.ContinueWith((t) =>
            {
                var dbRead  = Ares.Tags.TagsModule.GetTagsDB().ReadInterface;
                var fileIds = dbRead.GetIdentificationForFiles(m_Files);
                return(Ares.TagsImport.TagsFromIds.FindTagsByIdsAsync(monitor, fileIds, m_Files, tokenSource.Token));
            }, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.NotOnFaulted, System.Threading.Tasks.TaskScheduler.Default).Unwrap();

            task2.ContinueWith((t) =>
            {
                monitor.Close();
                Ares.Editor.Actions.TagChanges.Instance.FireTagsDBChanged(this);
                UpdateAll();
                try
                {
                    int result = task2.Result;
                    MessageBox.Show(this, String.Format(StringResources.TagsDownloadStats, result, m_Files.Count), StringResources.Ares, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (AggregateException)
                {
                }
            }, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.NotOnFaulted, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
            task2.ContinueWith((t) =>
            {
                monitor.Close();
                if (t.Exception != null)
                {
                    TaskHelpers.HandleTaskException(this, t.Exception, StringResources.TagsDbError);
                }
            }, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
            task.ContinueWith((t) =>
            {
                monitor.Close();
                if (t.Exception != null)
                {
                    TaskHelpers.HandleTaskException(this, t.Exception, StringResources.MusicIdExtractionError);
                }
            }, System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted, System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
        }
Пример #2
0
        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);
                }
            }
        }