Exemplo n.º 1
0
        public async Task<ImportPlaylistResult> ImportPlaylistsAsync(IList<string> fileNames)
        {
            ImportPlaylistResult finalResult = ImportPlaylistResult.Success;

            this.watcher.Suspend(); // Stop watching the playlist folder

            IList<ImportPlaylistResult> results = new List<ImportPlaylistResult>();

            foreach (string fileName in fileNames)
            {
                if (FileFormats.IsSupportedStaticPlaylistFile(fileName))
                {
                    results.Add(await this.ImportStaticPlaylistAsync(fileName));
                }
                else if (FileFormats.IsSupportedSmartPlaylistFile(fileName))
                {
                    results.Add(await this.ImportSmartPlaylistAsync(fileName));
                }
            }

            this.watcher.Resume(); // Start watching the playlist folder


            if (results.Any(x => x.Equals(ImportPlaylistResult.Success)))
            {
                this.PlaylistFolderChanged(this, new EventArgs());
            }

            return finalResult;
        }
Exemplo n.º 2
0
        public async Task <IList <TrackViewModel> > ProcessFilesAsync(IList <string> paths, bool processPlaylistFiles)
        {
            var tracks = new List <TrackViewModel>();

            await Task.Run(async() =>
            {
                if (paths == null)
                {
                    return;
                }

                // Convert the files to tracks
                foreach (string path in paths)
                {
                    if (FileFormats.IsSupportedAudioFile(path))
                    {
                        // The file is a supported audio format: add it directly.
                        tracks.Add(await this.CreateTrackAsync(path));
                    }
                    else if (processPlaylistFiles && FileFormats.IsSupportedStaticPlaylistFile(path))
                    {
                        // The file is a supported playlist format: process the contents of the playlist file.
                        foreach (string audioFilePath in this.ProcessPlaylistFile(path))
                        {
                            tracks.Add(await this.CreateTrackAsync(audioFilePath));
                        }
                    }
                    else if (Directory.Exists(path))
                    {
                        // The file is a directory: get the audio files in that directory and all its sub directories.
                        foreach (string audioFilePath in this.ProcessDirectory(path))
                        {
                            tracks.Add(await this.CreateTrackAsync(audioFilePath));
                        }
                    }
                    else
                    {
                        // The file is unknown: do not process it.
                    }
                }
            });

            return(tracks);
        }
        private async Task AddDroppedFilesToPlaylists(IDropInfo dropInfo)
        {
            // 3 possibilities
            if (dropInfo.TargetItem is PlaylistViewModel)
            {
                // 1. Drop audio and playlist files on a playlist name: add all files
                // (including those in the dropped playlist files) to that playlist.
                try
                {
                    PlaylistViewModel hoveredPlaylist = (PlaylistViewModel)dropInfo.TargetItem;

                    // Don't add anything to smart playlists
                    if (hoveredPlaylist.Type.Equals(PlaylistType.Smart))
                    {
                        return;
                    }

                    IList <string>         filenames = dropInfo.GetDroppedFilenames();
                    IList <TrackViewModel> tracks    = await this.fileService.ProcessFilesAsync(filenames, true);

                    if (hoveredPlaylist != null && tracks != null)
                    {
                        await this.playlistService.AddTracksToStaticPlaylistAsync(tracks, hoveredPlaylist.Name);
                    }
                }
                catch (Exception ex)
                {
                    LogClient.Error("Could not add dropped files to hovered playlist. Exception: {0}", ex.Message);
                }
            }
            else if (dropInfo.TargetItem == null)
            {
                // 2. Drop audio files in empty part of list: add these files to a new unique playlist
                IList <string>         filenames = dropInfo.GetDroppedFilenames();
                IList <TrackViewModel> tracks    = await this.fileService.ProcessFilesAsync(filenames, false);

                if (tracks != null && tracks.Count > 0)
                {
                    string uniquePlaylistName = await this.playlistService.GetUniquePlaylistNameAsync(ResourceUtils.GetString("Language_New_Playlist"));

                    await this.playlistService.CreateNewPlaylistAsync(new EditablePlaylistViewModel(uniquePlaylistName, PlaylistType.Static));

                    await this.playlistService.AddTracksToStaticPlaylistAsync(tracks, uniquePlaylistName);
                }

                // 3. Drop playlist files in empty part of list: add the playlist with a unique name
                IList <string> playlistFileNames = filenames.Select(f => f).Where(f => FileFormats.IsSupportedStaticPlaylistFile(f)).ToList();
                await this.ImportPlaylistsAsync(playlistFileNames);
            }
        }