Beispiel #1
0
        public async void LaunchSearch(string albumName)
        {
            if (State != CoverSearchState.Wait)
                throw new InvalidOperationException("New search can't be launch while another is processing or canceled.");

            _searchResult = new CoverSearchResult();

            if (string.IsNullOrEmpty(albumName))
                return;

            _cancellationTokenSource = new CancellationTokenSource();
            CancellationToken ct = _cancellationTokenSource.Token;

            IEnumerable<CoversGallery> galleries = _galleryManager.GetAllComponentsInChildren<CoversGallery>().ToList();

            var status = new CoverSearchStatus
            {
                Progress = 0,
                SearchResult = new CoverSearchResult(),
                GalleryName = galleries.ElementAt(0).Name,
                Cached = true
            };

            State = CoverSearchState.Search;
            Logger.Info("Search launched : {0}", albumName);

            if (SearchLaunch != null)
                SearchLaunch.Invoke(this, EventArgs.Empty);

            int localCount = 0;
            int onlineCount = 0;
            int cacheCount = 0;
            foreach (ICoversGallery coversGallery in galleries)
            {
                if (!coversGallery.Enabled)
                    continue;

                if (coversGallery is LocalGallery)
                    localCount++;
                else
                {
                    onlineCount++;
                    if (coversGallery is OnlineGallery && (coversGallery as OnlineGallery).UseCache)
                        cacheCount++;
                }
            }

            Logger.Info("Galleries enabled: {0} local, {1} online, {2} cache", localCount, onlineCount, cacheCount);

            int galleryCount = localCount + onlineCount + cacheCount;

            try
            {
                ct.ThrowIfCancellationRequested();

                ReportProgress(status);

                double countProgress = 0;
                int i = 0;
                foreach (ICoversGallery gallery in galleries)
                {
                    if (gallery.Enabled && gallery is OnlineGallery && (gallery as OnlineGallery).UseCache)
                    {
                        status.Progress = countProgress / galleryCount;
                        status.GalleryName = galleries.ElementAt(i).Name + " (cache)";
                        status.SearchResult = new CoverSearchResult();
                        ReportProgress(status);

                        Logger.Info("Next gallery : {0}", status.GalleryName);

                        CoverEntry entry = (gallery as OnlineGallery).SearchCached(albumName);

                        status.SearchResult = new CoverSearchResult();
                        if (entry != null)
                        {
                            status.SearchResult.Add(entry);
                            _searchResult.Add(entry);
                            Logger.Info("Cached entry found");
                        }
                        else
                            Logger.Info("No cached entry");

                        i++;
                        countProgress++;

                        status.Progress = countProgress / galleryCount;
                        ReportProgress(status);
                    }

                    ct.ThrowIfCancellationRequested();
                }

                i = 0;
                status.Cached = false;

                foreach (ICoversGallery gallery in galleries)
                {
                    if (gallery.Enabled)
                    {
                        status.Progress = countProgress / galleryCount;
                        status.GalleryName = galleries.ElementAt(i).Name;
                        status.SearchResult = new CoverSearchResult();
                        ReportProgress(status);

                        Logger.Info("Next gallery : {0}", status.GalleryName);

                        status.SearchResult = gallery is OnlineGallery
                            ? await (gallery as OnlineGallery).SearchOnlineAsync(albumName, _cancellationTokenSource.Token)
                            : await gallery.SearchAsync(albumName, _cancellationTokenSource.Token);

                        foreach (CoverEntry entry in status.SearchResult)
                            _searchResult.Add(entry);

                        Logger.Info("{0} entries found", status.SearchResult.Count);

                        i++;
                        countProgress++;

                        status.Progress = countProgress / galleryCount;
                        ReportProgress(status);
                    }

                    ct.ThrowIfCancellationRequested();
                }

                Logger.Info("Search succeeded");

                if (SearchSuccess != null)
                    SearchSuccess.Invoke(this, EventArgs.Empty);
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception e)
            {
                var args = new SearchErrorEventArgs
                {
                    ErrorMessage = e.Message
                };

                Logger.Error(e.Message);

                if (SearchError != null)
                    SearchError.Invoke(this, args);
            }
            finally
            {
                State = CoverSearchState.Wait;
                Logger.Info("Search ended");

                if (SearchEnd != null)
                    SearchEnd.Invoke(this, EventArgs.Empty);
            }
        }
        public void SearchProgress(CoverSearchStatus searchStatus)
        {
            _searchProgressBar.Value = (int)(searchStatus.Progress * 100);
            _statusLabel.Text = string.Format("Search in {0}...", searchStatus.GalleryName);

            bool firstCached = false;
            ListViewGroup group = null;

            if (!searchStatus.Cached)
                group = _listView.Groups.Add(searchStatus.GalleryName, searchStatus.GalleryName);

            foreach (CoverEntry entry in searchStatus.SearchResult)
            {
                if (searchStatus.Cached && _listView.Groups["Cached"].Items.Count == 0)
                    firstCached = true;

                ListViewItem item = searchStatus.Cached
                    ? new ListViewItem(entry.GalleryName, _listView.Groups["Cached"])
                    : new ListViewItem(entry.Name, group);

                _imageList.Images.Add(entry.Cover);
                item.ImageIndex = _imageList.Images.Count - 1;

                _listView.Items.Add(item);
            }

            if (firstCached)
                _listView.Items[2].Selected = true;

            _listView.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
        }
Beispiel #3
0
        private void ReportProgress(CoverSearchStatus coverSearchStatus)
        {
            var args = new SearchProgressEventArgs
            {
                Status = coverSearchStatus
            };

            if (SearchProgress != null)
                SearchProgress.Invoke(this, args);
        }