Esempio n. 1
0
        public async void StartDownload(LoadifySession session, int startIndex = 0)
        {
            _CancellationToken = new CancellationTokenSource();

            var remainingTracksToDownload = RemainingTracks.Count > startIndex
                                            ? new ObservableCollection <TrackViewModel>(RemainingTracks.Skip(startIndex))
                                            : new ObservableCollection <TrackViewModel>();

            foreach (var track in remainingTracksToDownload)
            {
                CurrentTrack = track;

                try
                {
                    var trackDownloadService = new TrackDownloadService(CurrentTrack.Track,
                                                                        _SettingsManager.BehaviorSetting.AudioProcessor,
                                                                        _SettingsManager.BehaviorSetting.DownloadPathConfigurator)
                    {
                        Cleanup             = _SettingsManager.BehaviorSetting.CleanupAfterConversion,
                        OutputDirectory     = _SettingsManager.DirectorySetting.DownloadDirectory,
                        AudioConverter      = _SettingsManager.BehaviorSetting.AudioConverter,
                        AudioFileDescriptor = _SettingsManager.BehaviorSetting.AudioFileDescriptor,
                        Mp3MetaData         = new Mp3MetaData()
                        {
                            Title   = CurrentTrack.Name,
                            Artists = CurrentTrack.Artists.Select(artist => artist.Name),
                            Album   = CurrentTrack.Album.Name,
                            Year    = CurrentTrack.Album.ReleaseYear,
                            Cover   = CurrentTrack.Album.Cover
                        },
                        DownloadProgressUpdated = progress =>
                        {
                            TrackProgress = progress;
                        }
                    };

                    _Logger.Debug(String.Format("Configured Track download service: OutputDirectory {0}, Cleanup? {1}, Track: {2}",
                                                trackDownloadService.OutputDirectory,
                                                trackDownloadService.Cleanup ? "Yes" : "No",
                                                CurrentTrack.ToString()));
                    _Logger.Info(String.Format("Downloading {0}...", CurrentTrack.ToString()));
                    await session.DownloadTrack(trackDownloadService, _CancellationToken.Token);

                    _Logger.Debug(String.Format("Track downloaded with result: {0}", trackDownloadService.Cancellation.ToString()));

                    if (trackDownloadService.Cancellation == TrackDownloadService.CancellationReason.UserInteraction)
                    {
                        _Logger.Info("Download contract was cancelled");
                        _EventAggregator.PublishOnUIThread(new NotificationEvent("Download cancelled",
                                                                                 String.Format("The download contract was cancelled. \n" +
                                                                                               "Tracks downloaded: {0}\n" +
                                                                                               "Tracks remaining: {1}\n",
                                                                                               DownloadedTracks.Count, RemainingTracks.Count)));
                        break;
                    }

                    if (trackDownloadService.Cancellation == TrackDownloadService.CancellationReason.None)
                    {
                        DownloadedTracks.Add(CurrentTrack);
                        RemainingTracks.Remove(CurrentTrack);
                        _EventAggregator.PublishOnUIThread(new TrackDownloadComplete(CurrentTrack));
                        _Logger.Info(String.Format("{0} was successfully downloaded to directory {1}", CurrentTrack.ToString(), trackDownloadService.OutputDirectory));
                    }
                    else
                    {
                        _EventAggregator.PublishOnUIThread(new DownloadContractPausedEvent(
                                                               String.Format("{0} could not be downloaded because the account being used triggered an action in another client.",
                                                                             CurrentTrack.ToString()),
                                                               RemainingTracks.IndexOf(CurrentTrack)));
                        _Logger.Info("Download was paused because the account being used triggered an action in another client");
                        return;
                    }
                }
                catch (ConfigurationException exception)
                {
                    _Logger.Error("A configuration error occured", exception);
                    _EventAggregator.PublishOnUIThread(new DownloadContractPausedEvent(exception.ToString(), RemainingTracks.IndexOf(CurrentTrack)));
                    return;
                }
                catch (SpotifyException exception)
                {
                    _Logger.Error("A Spotify error occured", exception);
                    _EventAggregator.PublishOnUIThread(new DownloadContractPausedEvent(String.Format("{0} could not be download because a Spotify error occured.",
                                                                                                     CurrentTrack.ToString()),
                                                                                       RemainingTracks.IndexOf(CurrentTrack)));
                    return;
                }
            }

            _EventAggregator.PublishOnUIThread(new DownloadContractCompletedEvent());
            TrackProgress = 0;
        }