private PlaylistCollection SearchForParentNode(IITUserPlaylist playlist) { // if the playlist doesn't have a parent return null to signal that. if (playlist.get_Parent() == null) { return(null); } // if the current object (this) is the parent node -> return if (Equals(playlist.get_Parent(), this)) { return(this); } // perform the search one level deeper foreach (var playlistCollectionNode in PlaylistNodes) { var parentNode = playlistCollectionNode.SearchForParentNode(playlist); // did we find the parent node? if (parentNode != null) { return(parentNode); } } // we've searched all child nodes and could still not find the parent node. // if the current node doesn't have a parent anymore, we've reached the end and won't continue our search. //if (_parent == null) return null; //return _parent.SearchForParentNode(playlist); return(null); }
private static bool Equals(IITUserPlaylist x, PlaylistCollection y) { if (x == null && y == null || x == null && y.Name == "root") { return(true); } if (x == null) { return(false); } if (y == null) { return(false); } return(x.Name.Equals(y.Name) && Equals(x.get_Parent(), y._parent)); }
public IITUserPlaylist Find(string name, ITUserPlaylistSpecialKind kind, IITUserPlaylist parent) { foreach (IITPlaylist playlist in itunes.LibrarySource.Playlists) { if (playlist.Name == name && playlist.Kind == ITPlaylistKind.ITPlaylistKindUser) { IITUserPlaylist userPlaylist = (IITUserPlaylist)playlist; if (userPlaylist.SpecialKind == kind) { var foundParent = userPlaylist.get_Parent(); if ((foundParent == null && parent == null) || (foundParent != null && foundParent.Name == parent.Name)) { return(userPlaylist); } } } } return(null); }
public static IITUserPlaylist ShufflePlayList(IITUserPlaylist playlist) { IITTrackCollection tracks = playlist.Tracks; Random rndm = new Random((int)DateTime.Now.Ticks); int nTracks = tracks.Count; // Generate shuffled track order int[] shuffle = new int[nTracks]; for (int n = 0; n < nTracks; n++) { shuffle[n] = n + 1; } for (int n = 0; n < nTracks; n++) { int i1 = rndm.Next(nTracks); int i2 = rndm.Next(nTracks); int t = shuffle[i1]; shuffle[i1] = shuffle[i2]; shuffle[i2] = t; } // Create temporary playlist string plName = playlist.Name; string tplName = string.Format("{0}_shfl", plName); IITUserPlaylist parent = playlist.get_Parent(); IITUserPlaylist pl1 = (parent == null) ? App.iTunes.CreatePlaylist(tplName) as IITUserPlaylist : parent.CreatePlaylist(tplName) as IITUserPlaylist; // populate playlist with shuffled track order for (int n = 0; n < nTracks; n++) { IITTrack trk = tracks[shuffle[n]]; pl1.AddTrack(trk); } // delete original playlist playlist.Delete(); // rename new playlist to original name pl1.Name = plName; // Retuen new playlist so tree can be updated. return(pl1); }
public static CPlaylist LoadPlaylists(iTunesApp itunes) { CPlaylist retPlaylist = new CPlaylist(null); IITSourceCollection sources = itunes.Sources; IITPlaylistCollection playlists = null; IITSource plSource = null; foreach (IITSource source in sources) { if (source.Kind == ITSourceKind.ITSourceKindLibrary) { plSource = source; playlists = source.Playlists; break; } } foreach (IITPlaylist pl in playlists) { if (pl.Kind != ITPlaylistKind.ITPlaylistKindUser) { continue; } try { IITUserPlaylist upl = (IITUserPlaylist)pl; if (upl.Smart) { continue; } if (upl.SpecialKind == ITUserPlaylistSpecialKind.ITUserPlaylistSpecialKindPodcasts) { continue; } string strDir = string.Empty; Stack <IITUserPlaylist> parentStack = new Stack <IITUserPlaylist>(); IITUserPlaylist uplp = upl.get_Parent(); if (uplp != null) { parentStack.Push(uplp); IITUserPlaylist uplc = uplp; do { uplp = uplc.get_Parent(); if (uplp != null) { parentStack.Push(uplp); uplc = uplp; } } while (uplp != null); } CPlaylist parentPL = retPlaylist; bool bFoundLeaf = false; do { uplp = (parentStack.Count > 0) ? parentStack.Pop() : null; if (uplp == null) { bFoundLeaf = true; } else { CPlaylist childPL = null; foreach (var item in parentPL.Children) { if (item.Name == uplp.Name) { childPL = item; break; } } if (childPL != null) { parentPL = childPL; } else { bFoundLeaf = true; } } } while (!bFoundLeaf); while (uplp != null) { CPlaylist plChild = new CPlaylist(uplp, true); parentPL.Children.Add(plChild); parentPL = plChild; uplp = (parentStack.Count > 0) ? parentStack.Pop() : null; } parentPL.Children.Add(new CPlaylist(upl)); } catch (Exception ex) { Debug.WriteLine(ex); } } return(retPlaylist); }