示例#1
0
        public static int RemoveArtistOrAlbumFromPlaylist(int playlistId, int id, bool isAlbum)
        {
            if (playlistId != 1 && playlistId != 2)
            {
                return(0);
            }

            int            count  = 0;
            PlaylistSource source = playlistId == 1 ? RemotePlaylist : PlayQueuePlaylist;

            for (int i = 0; i < source.TrackModel.Count; i++)
            {
                object t = source.TrackModel.GetItem(i);

                if (t is DatabaseTrackInfo && (isAlbum && ((DatabaseTrackInfo)t).AlbumId == id ||
                                               !isAlbum && ((DatabaseTrackInfo)t).ArtistId == id))
                {
                    source.RemoveTrack(i);
                    i--;
                    count++;
                }
            }

            return(count);
        }
示例#2
0
        /// <summary>
        /// Remove track from playlist.
        /// </summary>
        /// <param name="playlistId">
        /// ID of playlist (1 and 2 supported only).
        /// </param>
        /// <param name="trackId">
        /// Track ID of track to remove.
        /// </param>
        /// <returns>
        /// Amount of removed tracks.
        /// </returns>
        public static bool RemoveTrackFromPlaylist(int playlistId, int trackId)
        {
            if (playlistId != 1 && playlistId != 2)
            {
                return(false);
            }

            PlaylistSource source = playlistId == 1 ? RemotePlaylist : PlayQueuePlaylist;

            for (int i = 0; i < source.TrackModel.Count; i++)
            {
                object t = source.TrackModel.GetItem(i);

                if (t is DatabaseTrackInfo && ((DatabaseTrackInfo)t).TrackId == trackId)
                {
                    source.RemoveTrack(i);
                    return(true);
                }
            }

            return(false);
        }
        public void Check(TrackInfo track)
        {
            if (OrderAndLimit == null)
            {
                // If this SmartPlaylist doesn't have an OrderAndLimit clause, then it's quite simple
                // to check this track - if it matches the Condition we make sure it's in, and vice-versa
                //Console.WriteLine ("Limitless condition");

                object id = Globals.Library.Db.QuerySingle(String.Format(
                                                               "SELECT TrackId FROM Tracks WHERE TrackId = {0} {1}",
                                                               track.TrackId, PrependCondition("AND")
                                                               ));

                if (id == null || (int)id != track.TrackId)
                {
                    if (Source.ContainsTrack(track))
                    {
                        // If it didn't match and is in the playlist, remove it
                        Source.RemoveTrack(track);
                    }
                }
                else if (!Source.ContainsTrack(track))
                {
                    // If it matched and isn't already in the playlist
                    Source.AddTrack(track);
                }
            }
            else
            {
                // If this SmartPlaylist has an OrderAndLimit clause things are more complicated as there are a limited
                // number of tracks -- so if we remove a track, we probably need to add a different one and vice-versa.
                //Console.WriteLine ("Checking track {0} ({1}) against condition & order/limit {2} {3}", track.Uri.LocalPath, track.TrackId, Condition, OrderAndLimit);

                // See if there is a track that was in the SmartPlaylist that now shouldn't be because
                // this track we are checking displaced it.
                IDataReader reader = Globals.Library.Db.Query(String.Format(
                                                                  "SELECT TrackId FROM PlaylistEntries WHERE PlaylistID = {0} " +
                                                                  "AND TrackId NOT IN (SELECT TrackID FROM Tracks {1} {2})",
                                                                  Source.Id, PrependCondition("WHERE"), OrderAndLimit
                                                                  ));

                while (reader.Read())
                {
                    Source.RemoveTrack(Globals.Library.Tracks[Convert.ToInt32(reader[0])] as TrackInfo);
                }

                reader.Dispose();

                // Remove those tracks from the database
                Globals.Library.Db.Execute(String.Format(
                                               "DELETE FROM PlaylistEntries WHERE PlaylistID = {0} " +
                                               "AND TrackId NOT IN (SELECT TrackID FROM Tracks {1} {2})",
                                               Source.Id, PrependCondition("WHERE"), OrderAndLimit
                                               ));

                // If we are already a member of this smart playlist
                if (Source.ContainsTrack(track))
                {
                    return;
                }

                // We have removed tracks no longer in this smart playlist, now need to add
                // tracks that replace those that were removed (if any)
                IDataReader new_tracks = Globals.Library.Db.Query(String.Format(
                                                                      @"SELECT TrackId FROM Tracks 
                        WHERE TrackID NOT IN (SELECT TrackID FROM PlaylistEntries WHERE PlaylistID = {0})
                        AND TrackID IN (SELECT TrackID FROM Tracks {1} {2})",
                                                                      Source.Id, PrependCondition("WHERE"), OrderAndLimit
                                                                      ));

                bool have_new_tracks = false;
                while (new_tracks.Read())
                {
                    Source.AddTrack(Globals.Library.Tracks[Convert.ToInt32(new_tracks[0])] as TrackInfo);
                    have_new_tracks = true;
                }

                new_tracks.Dispose();

                if (have_new_tracks)
                {
                    Globals.Library.Db.Execute(String.Format(
                                                   @"INSERT INTO PlaylistEntries 
                            SELECT NULL as EntryId, {0} as PlaylistId, TrackId FROM Tracks 
                            WHERE TrackID NOT IN (SELECT TrackID FROM PlaylistEntries WHERE PlaylistID = {0})
                            AND TrackID IN (SELECT TrackID FROM Tracks {1} {2})",
                                                   Source.Id, PrependCondition("WHERE"), OrderAndLimit
                                                   ));
                }
            }
        }