Esempio n. 1
0
        private async Task ImportFiles()
        {
            var tracks = new List <TrackInfo>();

            await Task.Run(() =>
            {
                lock (this.lockObject)
                {
                    // Sort the files alphabetically
                    this.files.Sort();

                    // Convert the files to tracks
                    foreach (string path in this.files)
                    {
                        if (FileFormats.IsSupportedAudioFile(path))
                        {
                            // The file is a supported audio format: add it directly.
                            tracks.Add(IndexerUtils.Path2TrackInfo(path, "file-" + this.instanceGuid));
                        }
                        else if (FileFormats.IsSupportedPlaylistFile(path))
                        {
                            // The file is a supported playlist format: process the contents of the playlist file.
                            List <string> audioFilePaths = this.ProcessPlaylistFile(path);

                            foreach (string audioFilePath in audioFilePaths)
                            {
                                tracks.Add(IndexerUtils.Path2TrackInfo(audioFilePath, "file-" + this.instanceGuid));
                            }
                        }
                        else if (Directory.Exists(path))
                        {
                            // The file is a directory: get the audio files in that directory.
                            List <string> audioFilePaths = this.ProcessDirectory(path);

                            foreach (string audioFilePath in audioFilePaths)
                            {
                                tracks.Add(IndexerUtils.Path2TrackInfo(audioFilePath, "file-" + this.instanceGuid));
                            }
                        }
                        else
                        {
                            // The file is unknown: do not process it.
                        }
                    }

                    // When done importing files, clear the list.
                    this.files.Clear();
                }
            });

            LogClient.Instance.Logger.Info("Number of tracks to play = {0}", tracks.Count);

            if (tracks.Count > 0)
            {
                LogClient.Instance.Logger.Info("Enqueuing {0} tracks.", tracks.Count);
                await this.playbackService.Enqueue(tracks);
            }
        }
Esempio n. 2
0
        private async Task ImportFilesAsync()
        {
            try
            {
                List <string> tempFiles = null;

                await Task.Run(() =>
                {
                    lock (this.lockObject)
                    {
                        tempFiles = this.files.Select(item => (string)item.Clone()).ToList();
                        this.files.Clear(); // Clear the list
                    }

                    tempFiles.Sort(); // Sort the files alphabetically
                });

                List <PlayableTrack> tracks = await this.ProcessFilesAsync(tempFiles);

                PlayableTrack selectedTrack = null;

                if (tempFiles.Count.Equals(1) && FileFormats.IsSupportedAudioFile(tempFiles.First()))
                {
                    // If there is only 1 file and it's a supported audio format, we do something special.
                    Tuple <List <PlayableTrack>, PlayableTrack> processedTracks = await this.ProcessFileAsync(tempFiles.First());

                    tracks        = processedTracks.Item1;
                    selectedTrack = processedTracks.Item2;
                }
                else
                {
                    tracks = await this.ProcessFilesAsync(tempFiles);

                    selectedTrack = tracks.First();
                }

                LogClient.Info("Number of tracks to play = {0}", tracks.Count.ToString());

                if (tracks.Count > 0)
                {
                    LogClient.Info("Enqueuing {0} tracks.", tracks.Count.ToString());
                    this.TracksImported(tracks, selectedTrack);
                }
            }
            catch (Exception ex)
            {
                LogClient.Error("Could not enqueue tracks. Exception: {0}", ex.Message);
            }
        }
Esempio n. 3
0
        public async Task <List <PlayableTrack> > ProcessFilesAsync(List <string> filenames)
        {
            var tracks = new List <PlayableTrack>();

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

                // Convert the files to tracks
                foreach (string path in filenames)
                {
                    if (FileFormats.IsSupportedAudioFile(path))
                    {
                        // The file is a supported audio format: add it directly.
                        tracks.Add(await this.CreateTrackAsync(path));
                    }
                    else if (FileFormats.IsSupportedPlaylistFile(path))
                    {
                        // The file is a supported playlist format: process the contents of the playlist file.
                        List <string> audioFilePaths = this.ProcessPlaylistFile(path);

                        foreach (string audioFilePath in audioFilePaths)
                        {
                            tracks.Add(await this.CreateTrackAsync(audioFilePath));
                        }
                    }
                    else if (Directory.Exists(path))
                    {
                        // The file is a directory: get the audio files in that directory.
                        List <string> audioFilePaths = this.ProcessDirectory(path);

                        foreach (string audioFilePath in audioFilePaths)
                        {
                            tracks.Add(await this.CreateTrackAsync(audioFilePath));
                        }
                    }
                    else
                    {
                        // The file is unknown: do not process it.
                    }
                }
            });

            return(tracks);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public async Task <IList <TrackViewModel> > ProcessFilesInDirectoryAsync(string directoryPath)
        {
            if (!Directory.Exists(directoryPath))
            {
                return(new List <TrackViewModel>());
            }

            string[] paths = Directory.GetFiles(directoryPath);

            var tracks = new List <TrackViewModel>();

            await Task.Run(async() =>
            {
                foreach (string path in paths)
                {
                    if (FileFormats.IsSupportedAudioFile(path))
                    {
                        tracks.Add(await this.CreateTrackAsync(path));
                    }
                }
            });

            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;
                    IList <string>         filenames       = dropInfo.GetDroppedFilenames();
                    IList <TrackViewModel> tracks          = await this.fileService.ProcessFilesAsync(filenames);

                    if (hoveredPlaylist != null && tracks != null)
                    {
                        await this.playlistService.AddTracksToPlaylistAsync(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)
            {
                string uniquePlaylistName = await this.playlistService.GetUniquePlaylistAsync(ResourceUtils.GetString("Language_New_Playlist"));

                IList <string> allFilenames      = dropInfo.GetDroppedFilenames();
                IList <string> audioFileNames    = allFilenames.Select(f => f).Where(f => FileFormats.IsSupportedAudioFile(f)).ToList();
                IList <string> playlistFileNames = allFilenames.Select(f => f).Where(f => FileFormats.IsSupportedPlaylistFile(f)).ToList();

                // 2. Drop audio files in empty part of list: add these files to a new unique playlist
                IList <TrackViewModel> audiofileTracks = await this.fileService.ProcessFilesAsync(audioFileNames);

                if (audiofileTracks != null && audiofileTracks.Count > 0)
                {
                    await this.playlistService.AddPlaylistAsync(uniquePlaylistName);

                    await this.playlistService.AddTracksToPlaylistAsync(audiofileTracks, uniquePlaylistName);
                }

                // 3. Drop playlist files in empty part of list: add the playlist with a unique name
                foreach (string playlistFileName in playlistFileNames)
                {
                    uniquePlaylistName = await this.playlistService.GetUniquePlaylistAsync(System.IO.Path.GetFileNameWithoutExtension(playlistFileName));

                    IList <TrackViewModel> playlistFileTracks = await this.fileService.ProcessFilesAsync(new string[] { playlistFileName }.ToList());

                    await this.playlistService.AddPlaylistAsync(uniquePlaylistName);

                    await this.playlistService.AddTracksToPlaylistAsync(playlistFileTracks, uniquePlaylistName);
                }
            }
        }
Esempio n. 7
0
        public async Task <List <PlayableTrack> > ProcessFilesAsync(List <string> filenames)
        {
            var tracks = new List <PlayableTrack>();

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

                // Convert the files to tracks
                foreach (string path in filenames)
                {
                    if (FileFormats.IsSupportedAudioFile(path))
                    {
                        // The file is a supported audio format: add it directly.
                        tracks.Add(await this.CreateTrackAsync(path));

                        if (SettingsClient.Get <bool>("Behaviour", "EnqueueOtherFilesInFolder"))
                        {
                            // Get all files in that directory
                            List <string> audioFilePaths = this.ProcessDirectory(Path.GetDirectoryName(path));

                            // Add all files from that directory
                            foreach (string audioFilePath in audioFilePaths)
                            {
                                if (!audioFilePath.Equals(path))
                                {
                                    tracks.Add(await this.CreateTrackAsync(audioFilePath));
                                }
                            }
                        }
                    }
                    else if (FileFormats.IsSupportedPlaylistFile(path))
                    {
                        // The file is a supported playlist format: process the contents of the playlist file.
                        List <string> audioFilePaths = this.ProcessPlaylistFile(path);

                        foreach (string audioFilePath in audioFilePaths)
                        {
                            tracks.Add(await this.CreateTrackAsync(audioFilePath));
                        }
                    }
                    else if (Directory.Exists(path))
                    {
                        // The file is a directory: get the audio files in that directory.
                        List <string> audioFilePaths = this.ProcessDirectory(path);

                        foreach (string audioFilePath in audioFilePaths)
                        {
                            tracks.Add(await this.CreateTrackAsync(audioFilePath));
                        }
                    }
                    else
                    {
                        // The file is unknown: do not process it.
                    }
                }
            });

            return(tracks);
        }
Esempio n. 8
0
        private async Task ImportFilesAsync()
        {
            var tracks = new List <PlayableTrack>();

            await Task.Run(async() =>
            {
                List <string> tempFiles = null;

                lock (this.lockObject)
                {
                    tempFiles = (List <string>) this.files.Clone();
                    this.files.Clear(); // Clear the list
                }

                tempFiles.Sort(); // Sort the files alphabetically

                if (tempFiles == null)
                {
                    return;
                }

                // Convert the files to tracks
                foreach (string path in tempFiles)
                {
                    if (FileFormats.IsSupportedAudioFile(path))
                    {
                        // The file is a supported audio format: add it directly.
                        tracks.Add(await this.CreateTrackAsync(path));
                    }
                    else if (FileFormats.IsSupportedPlaylistFile(path))
                    {
                        // The file is a supported playlist format: process the contents of the playlist file.
                        List <string> audioFilePaths = this.ProcessPlaylistFile(path);

                        foreach (string audioFilePath in audioFilePaths)
                        {
                            tracks.Add(await this.CreateTrackAsync(audioFilePath));
                        }
                    }
                    else if (Directory.Exists(path))
                    {
                        // The file is a directory: get the audio files in that directory.
                        List <string> audioFilePaths = this.ProcessDirectory(path);

                        foreach (string audioFilePath in audioFilePaths)
                        {
                            tracks.Add(await this.CreateTrackAsync(audioFilePath));
                        }
                    }
                    else
                    {
                        // The file is unknown: do not process it.
                    }
                }
            });

            LogClient.Info("Number of tracks to play = {0}", tracks.Count.ToString());

            if (tracks.Count > 0)
            {
                LogClient.Info("Enqueuing {0} tracks.", tracks.Count.ToString());
                this.TracksImported(tracks);
            }
        }
Esempio n. 9
0
        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);

                    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)
            {
                string uniquePlaylistName = await this.playlistService.GetUniquePlaylistNameAsync(ResourceUtils.GetString("Language_New_Playlist"));

                IList <string> allFilenames      = dropInfo.GetDroppedFilenames();
                IList <string> audioFileNames    = allFilenames.Select(f => f).Where(f => FileFormats.IsSupportedAudioFile(f)).ToList();
                IList <string> playlistFileNames = allFilenames.Select(f => f).Where(f => FileFormats.IsSupportedStaticPlaylistFile(f)).ToList();

                // 2. Drop audio files in empty part of list: add these files to a new unique playlist
                IList <TrackViewModel> audiofileTracks = await this.fileService.ProcessFilesAsync(audioFileNames);

                if (audiofileTracks != null && audiofileTracks.Count > 0)
                {
                    await this.playlistService.CreateNewPlaylistAsync(uniquePlaylistName, PlaylistType.Static);

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

                // 3. Drop playlist files in empty part of list: add the playlist with a unique name
                await this.ImportPlaylistsAsync(playlistFileNames);
            }
        }