/// <summary> /// The dispatcher of the <see cref="PlaylistModified"/> event /// </summary> /// <param name="playlist">The playlist that was modified</param> /// <param name="type">The type of modification that occured</param> /// <param name="interactive">Whether the action was performed by the user directly</param> private static void DispatchPlaylistModified(PlaylistData playlist, ModifyType type, bool interactive = false) { if (PlaylistModified != null) PlaylistModified(playlist, new ModifiedEventArgs(type, null, interactive)); }
/// <summary> /// The dispatcher of the <see cref="PlaylistRenamed"/> event /// </summary> /// <param name="playlist">The playlist that was renamed</param> /// <param name="oldName">The name of the playlist before the change</param> /// <param name="newName">The name of the playlist after the change</param> private static void DispatchPlaylistRenamed(PlaylistData playlist, string oldName, string newName) { if (PlaylistRenamed != null) PlaylistRenamed(playlist, new RenamedEventArgs(WatcherChangeTypes.Renamed, "playlist", newName, oldName)); }
/// <summary> /// Parses a URL to a playlist. /// </summary> /// <param name="url">The URL to the playlist object</param> /// <returns>A list of tracks corresponding to the playlist</returns> public static PlaylistData ParseURL(string url) { if (YouTubeManager.IsPlaylist(url)) { return YouTubeManager.ParseURL(url); } else if (IsSupported(url)) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream str = resp.GetResponseStream(); Encoding enc = Encoding.GetEncoding("utf-8"); StreamReader read = new StreamReader(str, enc); PlaylistData pl = new PlaylistData(); pl.Tracks = ParsePlaylist(read, GetType(url)); resp.Close(); read.Close(); return pl; } else return new PlaylistData(); }
/// <summary> /// Renames a playlist. If a playlist with the new name already exist or the new name is either "Create new" or "" it will do nothing. /// </summary> /// <param name="playlist">The playlist to be renamed</param> /// <param name="newName">The new name of the playlist</param> public static void RenamePlaylist(PlaylistData playlist, String newName) { if (playlist != null && playlist.Name != newName) { string oldName = playlist.Name; if (FindPlaylist(newName) != null) { int pExt = 1; while (FindPlaylist(newName + pExt) != null) pExt++; newName = newName + pExt; } if (playlist != null && newName != "" && newName.ToLower() != U.T("NavigationCreateNew").ToLower()) DispatchPlaylistRenamed(playlist, oldName, newName); } }
/// <summary> /// Creates a new playlist /// </summary> /// <param name="name">The name of the new playlist (this will be appended with a number if neccessary)</param> /// <param name="id">The ID of the playlist in the cloud</param> /// <param name="owner">The ID of the user who owns the playlist</param> /// <param name="interactive">Whether the action was performed by the user directly</param> /// <returns>The newly created PlaylistData for the playlist</returns> public static PlaylistData CreatePlaylist(String name, uint id = 0, uint owner = 0, bool interactive = false) { name = U.CleanXMLString(name); if (FindPlaylist(name) != null) { int pExt = 1; while (FindPlaylist(name + pExt) != null) pExt++; name = name + pExt; } PlaylistData playlist = new PlaylistData(); playlist.Name = name; playlist.ID = id; playlist.Owner = owner; playlist.Time = 0; playlist.Tracks = new ObservableCollection<TrackData>(); playlist.Tracks.CollectionChanged += TracksChanged; SettingsManager.Playlists.Add(playlist); DispatchPlaylistModified(playlist, ModifyType.Created, interactive); return playlist; }
/// <summary> /// Checks whether a given playlist contains any of a given list of track. /// </summary> /// <param name="playlist">The playlist to search in</param> /// <param name="tracks">The tracks to search for</param> /// <returns>True of <paramref name="playlist"/> contains any of <paramref name="tracks"/>, otherwise false</returns> public static bool ContainsAny(PlaylistData playlist, List<TrackData> tracks) { foreach (TrackData t1 in playlist.Tracks) foreach (TrackData t2 in tracks) if (t1.Path == t2.Path) return true; return false; }
/// <summary> /// Checks whether a given playlist contains all of a given list of track. /// </summary> /// <param name="playlist">The playlist to search in</param> /// <param name="tracks">The tracks to search for</param> /// <returns>True of <paramref name="playlist"/> contains all of <paramref name="tracks"/>, otherwise false</returns> public static bool ContainsAll(PlaylistData playlist, List<TrackData> tracks) { foreach (TrackData t1 in playlist.Tracks) foreach (TrackData t2 in tracks) if (t1.Path != t2.Path) return false; return playlist.Tracks.Count != 0; }
/// <summary> /// Checks whether a given playlist contains a given track. /// </summary> /// <param name="playlist">The playlist to search in</param> /// <param name="track">The track to search for</param> /// <returns>True of <paramref name="playlist"/> contains <paramref name="track"/>, otherwise false</returns> public static bool Contains(PlaylistData playlist, TrackData track) { foreach (TrackData t in playlist.Tracks) if (t.Path == track.Path) return true; return false; }
/// <summary> /// Parses a URL to a playlist. /// </summary> /// <param name="url">The URL to the playlist object</param> /// <returns>A collection of the tracks corresponding to the playlist</returns> public static PlaylistData ParseURL(string url) { PlaylistData pl = new PlaylistData(); pl.Tracks = new ObservableCollection<TrackData>(); string id = GetPlaylistID(url); YouTubeRequest request = new YouTubeRequest(settings); Uri uri = new Uri("https://gdata.youtube.com/feeds/api/playlists/" + id + "?v=2"); Feed<Video> feed = request.Get<Video>(uri); if (feed != null) { pl.Name = feed.AtomFeed.Title.Text; try { foreach (Video v in feed.Entries) { try { pl.Tracks.Add(CreateTrack(v)); } catch (Exception e) { U.L(LogLevel.Warning, "YOUTUBE", "Problem adding track from playlist '" + pl.Name + "': " + e.Message); } } } catch (Exception exc) { U.L(LogLevel.Warning, "YOUTUBE", "Problem parsing playlist '" + pl.Name + "': " + exc.Message); } } else { U.L(LogLevel.Warning, "YOUTUBE", "Could not find playlist with ID '" + id + "'"); return null; } return pl; }