/// <summary> /// Prevents a default instance of the <see cref="ITunesDirectoryInfo"/> class from being created. /// </summary> /// <param name="parent">The parent directory.</param> private ITunesDirectoryInfo(ITunesDirectoryInfo parent) { this.Parent = parent; this.files = Enumerable.Empty <IFileInfo>(); this.directories = Enumerable.Empty <ITunesDirectoryInfo>(); this.Exists = true; }
/// <summary> /// Prevents a default instance of the <see cref="ITunesDirectoryInfo"/> class from being created. /// </summary> /// <param name="parent">The parent directory.</param> private ITunesDirectoryInfo(ITunesDirectoryInfo parent) { this.Parent = parent; this.files = Enumerable.Empty<IFileInfo>(); this.directories = Enumerable.Empty<ITunesDirectoryInfo>(); this.Exists = true; }
/// <summary> /// Maps the specified iTunes playlist to a directory structure. /// </summary> /// <param name="playlistName">The playlist.</param> /// <returns>A directory structure which represents the specified iTunes playlist</returns> public static IEnumerable<ITunesDirectoryInfo> MapPlaylistToDirectoryStructure(string playlistName) { if (String.IsNullOrEmpty(playlistName)) throw new ArgumentException("The playlist name name cannot be null or empty", Reflector.GetMemberName(() => playlistName)); var root = new ITunesDirectoryInfo(playlistName); var files = new iTunesApp() .LibrarySource .Playlists .ItemByName[playlistName] .Tracks .Cast<IITFileOrCDTrack>(); var tracksByArtist = files .GroupBy(file => file.Artist) .OrderBy(group => group.Key); var artistDirectories = new List<ITunesDirectoryInfo>(); foreach (var artistGroup in tracksByArtist) { var albumGroups = artistGroup .GroupBy(track => track.Album); // The artist name has to be normalized, so that it doesn't contain any characters that are invalid for a path string artistName = NormalizeArtistOrAlbumName(artistGroup.Key); var albumDirectories = albumGroups .Select(album => new { Album = album, AlbumName = NormalizeArtistOrAlbumName(album.Key) }) .Select ( directory => new ITunesDirectoryInfo ( directory.AlbumName, directory.Album .Where(track => track.Location != null) .Select ( track => (IFileInfo)new LocalFileInfo(new FileInfo(track.Location)) ), null ) ) .ToList(); //Execute the query immediately, because a streaming causes weird bugs var artistDirectory = new ITunesDirectoryInfo(artistName, albumDirectories, root); foreach (ITunesDirectoryInfo albumDirectory in artistDirectory.GetDirectories()) { albumDirectory.Parent = artistDirectory; } artistDirectories.Add(artistDirectory); } return artistDirectories; }
/// <summary> /// Initializes a new instance of the <see cref="ITunesDirectoryInfo"/> class. /// The directory will be the last level of the directory structure and contains the files. /// </summary> /// <param name="albumName">The name of the album.</param> /// <param name="files">The files that are contained in this directory.</param> /// <param name="parent">The parent.</param> public ITunesDirectoryInfo(string albumName, IEnumerable <IFileInfo> files, ITunesDirectoryInfo parent) : this(parent) { albumName.ThrowIfNull(() => albumName); files.ThrowIfNull(() => files); this.name = albumName; this.files = files; }
/// <summary> /// Initializes a new instance of the <see cref="ITunesDirectoryInfo"/> class. /// The directory will be the middle level of the directory structure and contains the album directories. /// </summary> /// <param name="artistName">The name of the artist.</param> /// <param name="directories">The directories that are contained in this directory.</param> /// <param name="parent">The parent.</param> public ITunesDirectoryInfo(string artistName, IEnumerable<ITunesDirectoryInfo> directories, ITunesDirectoryInfo parent) : this(parent) { artistName.ThrowIfNull(() => artistName); directories.ThrowIfNull(() => directories); this.name = artistName; this.directories = directories; }
/// <summary> /// Initializes a new instance of the <see cref="ITunesDirectoryInfo"/> class. /// The directory will be the last level of the directory structure and contains the files. /// </summary> /// <param name="albumName">The name of the album.</param> /// <param name="files">The files that are contained in this directory.</param> /// <param name="parent">The parent.</param> public ITunesDirectoryInfo(string albumName, IEnumerable<IFileInfo> files, ITunesDirectoryInfo parent) : this(parent) { albumName.ThrowIfNull(() => albumName); files.ThrowIfNull(() => files); this.name = albumName; this.files = files; }
/// <summary> /// Creates an iTunes directory info which does not exist. /// </summary> /// <param name="name">The name of the directory.</param> /// <param name="parent">The parent directory.</param> /// <returns>A directory whichs <see cref="Exists"/> property is set to false.</returns> public static ITunesDirectoryInfo CreateNonExistantDirectoryInfo(string name, ITunesDirectoryInfo parent) { name.ThrowIfNull(() => name); parent.ThrowIfNull(() => parent); var directory = new ITunesDirectoryInfo(parent) { name = name, Exists = false }; return(directory); }
/// <summary> /// Gets the directory info at the specified path. /// </summary> /// <param name="path">The path of the directory.</param> /// <returns> /// An <see cref="IDirectoryInfo"/> of the directory from the specified path. /// </returns> public IDirectoryInfo GetDirectoryInfo(string path) { path.ThrowIfNull(() => path); if (!this.DirectoryExists(path)) { return(ITunesDirectoryInfo .CreateNonExistantDirectoryInfo(Path.GetFileName(path), (ITunesDirectoryInfo)this.GetDirectoryInfo(Path.GetDirectoryName(path)))); } string[] split = path.Split(Path.DirectorySeparatorChar); string playlist = split[0]; string album = null; if (split.Length > 1) { string artist = split[1]; if (split.Length > 2) { album = split[2]; } var root = this.GetPlaylistStructure(playlist); IDirectoryInfo directoryInfo = root .SingleOrDefault(dir => dir.Name == artist); if (directoryInfo == null) { directoryInfo = ITunesDirectoryInfo .CreateNonExistantDirectoryInfo(album ?? artist, (ITunesDirectoryInfo)this.GetDirectoryInfo(Path.GetDirectoryName(path))); } else if (album != null) { directoryInfo = directoryInfo .GetDirectories() .Single(albumDir => albumDir.Name == album); } return(directoryInfo); } return(new ITunesDirectoryInfo(playlist)); }
/// <summary> /// Initializes a new instance of the <see cref="ITunesDirectoryInfo"/> class. /// The directory will be the middle level of the directory structure and contains the album directories. /// </summary> /// <param name="artistName">The name of the artist.</param> /// <param name="directories">The directories that are contained in this directory.</param> /// <param name="parent">The parent.</param> public ITunesDirectoryInfo(string artistName, IEnumerable <ITunesDirectoryInfo> directories, ITunesDirectoryInfo parent) : this(parent) { artistName.ThrowIfNull(() => artistName); directories.ThrowIfNull(() => directories); this.name = artistName; this.directories = directories; }
/// <summary> /// Creates an iTunes directory info which does not exist. /// </summary> /// <param name="name">The name of the directory.</param> /// <param name="parent">The parent directory.</param> /// <returns>A directory whichs <see cref="Exists"/> property is set to false.</returns> public static ITunesDirectoryInfo CreateNonExistantDirectoryInfo(string name, ITunesDirectoryInfo parent) { name.ThrowIfNull(() => name); parent.ThrowIfNull(() => parent); var directory = new ITunesDirectoryInfo(parent) { name = name, Exists = false }; return directory; }
/// <summary> /// Maps the specified iTunes playlist to a directory structure. /// </summary> /// <param name="playlistName">The playlist.</param> /// <returns>A directory structure which represents the specified iTunes playlist</returns> public static IEnumerable <ITunesDirectoryInfo> MapPlaylistToDirectoryStructure(string playlistName) { if (String.IsNullOrEmpty(playlistName)) { throw new ArgumentException("The playlist name name cannot be null or empty", Reflector.GetMemberName(() => playlistName)); } var root = new ITunesDirectoryInfo(playlistName); var files = new iTunesApp() .LibrarySource .Playlists .ItemByName[playlistName] .Tracks .Cast <IITFileOrCDTrack>(); var tracksByArtist = files .GroupBy(file => file.Artist) .OrderBy(group => group.Key); var artistDirectories = new List <ITunesDirectoryInfo>(); foreach (var artistGroup in tracksByArtist) { var albumGroups = artistGroup .GroupBy(track => track.Album); // The artist name has to be normalized, so that it doesn't contain any characters that are invalid for a path string artistName = NormalizeArtistOrAlbumName(artistGroup.Key); var albumDirectories = albumGroups .Select(album => new { Album = album, AlbumName = NormalizeArtistOrAlbumName(album.Key) }) .Select ( directory => new ITunesDirectoryInfo ( directory.AlbumName, directory.Album .Where(track => track.Location != null) .Select ( track => (IFileInfo) new LocalFileInfo(new FileInfo(track.Location)) ), null ) ) .ToList(); //Execute the query immediately, because a streaming causes weird bugs var artistDirectory = new ITunesDirectoryInfo(artistName, albumDirectories, root); foreach (ITunesDirectoryInfo albumDirectory in artistDirectory.GetDirectories()) { albumDirectory.Parent = artistDirectory; } artistDirectories.Add(artistDirectory); } return(artistDirectories); }