Пример #1
0
 protected virtual void OnTracksRemoved(TracksEventArgs args)
 {
     PlaylistTrack.Update(this, removals: args.TrackIndices);
     if (TracksRemoved != null)
     {
         TracksRemoved(this, args);
     }
 }
Пример #2
0
 protected virtual void OnTracksMoved(TracksMovedEventArgs args)
 {
     PlaylistTrack.Update(this, moves: args.TrackIndices, movedTo: args.NewPosition);
     if (TracksMoved != null)
     {
         TracksMoved(this, args);
     }
 }
Пример #3
0
 protected virtual void OnTracksAdded(TracksAddedEventArgs args)
 {
     PlaylistTrack.Update(this, aditions: args.TrackIndices);
     if (TracksAdded != null)
     {
         TracksAdded(this, args);
     }
 }
Пример #4
0
        internal static void Delete(Playlist playlist, int position)
        {
            KeyGen key = new KeyGen(playlist, position);

            lock (tracksLock)
            {
                PlaylistTrack track = tracks[key];
                int           count = track.RemRef();
                if (count == 0)
                {
                    tracks.Remove(key);
                }
            }
        }
Пример #5
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (obj.GetType() == typeof(Track))
            {
                return(base.Equals(obj));
            }
            if (obj.GetType() != typeof(PlaylistTrack))
            {
                return(false);
            }
            PlaylistTrack pt = (PlaylistTrack)obj;

            return(pt.playlist == this.playlist && pt.position == this.position);
        }
Пример #6
0
        protected override void OnDispose()
        {
            session.DisposeAll -= new SessionEventHandler(session_DisposeAll);
            PlaylistTrack.UnregisterPlaylist(this);
            if (!session.ProcExit)
            {
                lock (libspotify.Mutex)
                {
                    if (registerCallbacks)
                    {
                        try { libspotify.sp_playlist_remove_callbacks(playlistPtr, callbacksPtr, IntPtr.Zero); }
                        finally { Marshal.FreeHGlobal(callbacksPtr); }
                    }
                    libspotify.sp_playlist_release(playlistPtr);
                }
            }

            playlistPtr  = IntPtr.Zero;
            callbacksPtr = IntPtr.Zero;
        }
Пример #7
0
 internal bool GetTrackSeen(PlaylistTrack track)
 {
     lock (libspotify.Mutex)
         return(libspotify.sp_playlist_track_seen(playlistPtr, track.position));
 }
Пример #8
0
 internal DateTime GetTrackCreationTime(PlaylistTrack track)
 {
     lock (libspotify.Mutex)
         return(new DateTime(TimeSpan.FromSeconds(libspotify.sp_playlist_track_create_time(playlistPtr, track.position)).Ticks));
 }
Пример #9
0
        protected Playlist(Session session, IntPtr playlistPtr, bool registerCallbacks = true)
        {
            if (playlistPtr == IntPtr.Zero)
            {
                throw new ArgumentException("playlistPtr can't be zero.");
            }

            if (session == null)
            {
                throw new ArgumentNullException("Session can't be null.");
            }

            this.session           = session;
            this.playlistPtr       = playlistPtr;
            this.registerCallbacks = registerCallbacks;

            if (registerCallbacks)
            {
                tracks_added                = new tracks_added_cb(TracksAddedCallback);
                tracks_removed              = new tracks_removed_cb(TracksRemovedCallback);
                tracks_moved                = new tracks_moved_cd(TracksMovedCallback);
                playlist_renamed            = new playlist_renamed_cb(RenamedCallback);
                playlist_state_changed      = new playlist_state_changed_cb(StateChangedCallback);
                playlist_update_in_progress = new playlist_update_in_progress_cb(UpdateInProgressCallback);
                playlist_metadata_updated   = new playlist_metadata_updated_cb(MetadataUpdatedCallback);
                track_created_changed       = new track_created_changed_cb(TrackCreatedChangedCallback);
                track_seen_changed          = new track_seen_changed_cb(TrackSeenChangedCallback);
                description_changed         = new description_changed_cb(DescriptionChangedCallback);
                image_changed               = new image_changed_cb(ImageChangedCallback);
                sp_playlist_callbacks callbacks = new sp_playlist_callbacks
                {
                    tracks_added                = Marshal.GetFunctionPointerForDelegate(tracks_added),
                    tracks_removed              = Marshal.GetFunctionPointerForDelegate(tracks_removed),
                    tracks_moved                = Marshal.GetFunctionPointerForDelegate(tracks_moved),
                    playlist_renamed            = Marshal.GetFunctionPointerForDelegate(playlist_renamed),
                    playlist_state_changed      = Marshal.GetFunctionPointerForDelegate(playlist_state_changed),
                    playlist_update_in_progress = Marshal.GetFunctionPointerForDelegate(playlist_update_in_progress),
                    playlist_metadata_updated   = Marshal.GetFunctionPointerForDelegate(playlist_metadata_updated),
                    track_created_changed       = Marshal.GetFunctionPointerForDelegate(track_created_changed),
                    track_seen_changed          = Marshal.GetFunctionPointerForDelegate(track_seen_changed),
                    description_changed         = Marshal.GetFunctionPointerForDelegate(description_changed),
                    image_changed               = Marshal.GetFunctionPointerForDelegate(image_changed)
                };

                callbacksPtr = Marshal.AllocHGlobal(Marshal.SizeOf(callbacks));
                Marshal.StructureToPtr(callbacks, callbacksPtr, true);
            }

            lock (libspotify.Mutex)
            {
                libspotify.sp_playlist_add_ref(playlistPtr);
                if (registerCallbacks)
                {
                    libspotify.sp_playlist_add_callbacks(playlistPtr, callbacksPtr, IntPtr.Zero);
                }
            }

            session.DisposeAll += new SessionEventHandler(session_DisposeAll);

            tracks = new PlaylistTrackList(
                () =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(libspotify.sp_playlist_num_tracks(playlistPtr));
            },
                (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    return(PlaylistTrack.Get(session, this, libspotify.sp_playlist_track(playlistPtr, index), index));
            },
                (track, index) =>
            {
                IsAlive(true);
                IntPtr trackArrayPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
                IntPtr[] ptrArray    = new[] { Track.GetPointer(track) };

                try
                {
                    Marshal.Copy(ptrArray, 0, trackArrayPtr, 1);

                    lock (libspotify.Mutex)
                        libspotify.sp_playlist_add_tracks(playlistPtr, trackArrayPtr, 1, index, session.sessionPtr);
                }
                finally
                {
                    try { Marshal.FreeHGlobal(trackArrayPtr); }
                    catch
                    { }
                }
            },
                (index) =>
            {
                IsAlive(true);
                lock (libspotify.Mutex)
                    libspotify.sp_playlist_remove_tracks(playlistPtr, new int[] { index }, 1);
            },
                () => false
                );
            PlaylistTrack.RegisterPlaylist(this);
        }
Пример #10
0
 protected override void OnDispose()
 {
     PlaylistTrack.Delete(((PlaylistTrack)track).Playlist, ((PlaylistTrack)track).Position);
     track = null;
 }
Пример #11
0
 public PlaylistTrackWrapper(PlaylistTrack track)
     : base(track)
 {
 }
Пример #12
0
        internal static void Update(Playlist playlist, IEnumerable <int> moves = null, int movedTo = -1, IEnumerable <int> aditions = null, IEnumerable <int> removals = null)
        {
            Dictionary <int, int> moved     = new Dictionary <int, int>();
            List <int>            newTracks = new List <int>();
            List <int>            toDel     = new List <int>();
            int oldCount = playlistLengths[playlist];
            int count;
            int deleted;

            if (aditions != null)
            {
                count = aditions.Count();
                for (int i = aditions.First(); i < oldCount; i++)
                {
                    moved.Add(i, i + count);
                }
                foreach (int index in aditions)
                {
                    newTracks.Add(index);
                }
            }
            else if (removals != null)
            {
                deleted = 0;
                for (int i = 0; i < oldCount; i++)
                {
                    if (removals.Contains(i))
                    {
                        deleted++;
                        continue;
                    }
                    if (deleted > 0)
                    {
                        moved.Add(i, i - deleted);
                    }
                }
            }
            else if (moves != null && movedTo != -1)
            {
                deleted = 0;
                for (int i = 0; i < oldCount; i++)
                {
                    if (moves.Contains(i))
                    {
                        deleted++;
                        continue;
                    }
                    if (deleted > 0)
                    {
                        moved.Add(i, i - deleted);
                    }
                }
                count = moves.Count();
                for (int i = movedTo; i < oldCount; i++)
                {
                    if (moved.ContainsKey(i))
                    {
                        moved[i] += count;
                    }
                    else
                    {
                        moved.Add(i, i + count);
                    }
                }
                int i1 = 0;
                foreach (int index in moves)
                {
                    if (moved.ContainsKey(index))
                    {
                        moved[index] = movedTo + (i1++);
                    }
                    else
                    {
                        moved.Add(index, movedTo + (i1++));
                    }
                }
            }

            foreach (var itm in moved)
            {
                KeyGen kg = new KeyGen(playlist, itm.Key);
                if (tracks.ContainsKey(kg))
                {
                    PlaylistTrack track = tracks[kg];
                    tracks.Remove(kg);
                    track.position = itm.Value;
                    kg             = new KeyGen(playlist, itm.Value);
                    tracks.Add(kg, track);
                }
            }
            foreach (var itm in toDel)
            {
                KeyGen kg = new KeyGen(playlist, itm);
                if (tracks.ContainsKey(kg))
                {
                    PlaylistTrack track = tracks[kg];
                    track.Dispose();
                    tracks.Remove(kg);
                }
            }
        }
 public PlaylistTrackWrapper(PlaylistTrack track)
     : base(track)
 {
 }