Skip to content

Commit

Permalink
Fixed a problem with playlist-folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxandr committed Feb 9, 2011
1 parent 88fd0a6 commit e67cd0e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
9 changes: 3 additions & 6 deletions SpotiFire.SpotifyLib/DisposeableSpotifyObject.cs
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SpotiFire.SpotifyLib
{
Expand Down Expand Up @@ -60,16 +57,16 @@ public override int GetHashCode()
return IntPtrHashCode();
}

static public bool operator == (DisposeableSpotifyObject obj, object obj2)
static public bool operator ==(DisposeableSpotifyObject obj, object obj2)
{
if(!Object.ReferenceEquals(obj, obj2))
if (!Object.ReferenceEquals(obj, obj2))
return obj.Equals(obj2);
return true;
}

static public bool operator !=(DisposeableSpotifyObject obj, object obj2)
{
if(!Object.ReferenceEquals(obj, obj2))
if (!Object.ReferenceEquals(obj, obj2))
return !obj.Equals(obj2);
return false;
}
Expand Down
56 changes: 43 additions & 13 deletions SpotiFire.SpotifyLib/SpotifyTypes/Playlist.cs
Expand Up @@ -13,12 +13,11 @@ namespace SpotiFire.SpotifyLib
internal class Playlist : CountedDisposeableSpotifyObject, IPlaylist
{
#region KeyGen
private class PlaylistMaintainer : Tuple<IntPtr, sp_playlist_type>
private class PlaylistMaintainer : Tuple<IntPtr, sp_playlist_type, IntPtr>
{
public PlaylistMaintainer(IntPtr ptr, sp_playlist_type type)
: base(ptr, type)
public PlaylistMaintainer(IntPtr ptr, sp_playlist_type type, IntPtr id)
: base(ptr, type, id)
{

}
}
#endregion
Expand All @@ -33,7 +32,7 @@ public PlaylistWrapper(Playlist playlist)

protected override void OnDispose()
{
Playlist.Delete(playlist.playlistPtr, playlist.type);
Playlist.Delete(playlist.playlistPtr, playlist.type, playlist.folderId);
playlist = null;
}

Expand All @@ -57,6 +56,19 @@ public Session Session
{
get { IsAlive(true); return playlist.Session; }
}

public override bool Equals(object obj)
{
PlaylistWrapper pw = obj as PlaylistWrapper;
if (pw == null)
{
Playlist p = obj as Playlist;
if (p == null)
return false;
return p == this.playlist;
}
return pw.playlist == this.playlist;
}
}
internal static IntPtr GetPointer(IPlaylist playlist)
{
Expand All @@ -69,25 +81,25 @@ internal static IntPtr GetPointer(IPlaylist playlist)
private static Dictionary<PlaylistMaintainer, Playlist> playlists = new Dictionary<PlaylistMaintainer, Playlist>();
private static readonly object playlistsLock = new object();

internal static IPlaylist Get(Session session, PlaylistContainer container, IntPtr playlistPtr, sp_playlist_type type)
internal static IPlaylist Get(Session session, PlaylistContainer container, IntPtr playlistPtr, sp_playlist_type type, IntPtr id)
{
Playlist playlist;
PlaylistMaintainer pm = new PlaylistMaintainer(playlistPtr, type);
PlaylistMaintainer pm = new PlaylistMaintainer(playlistPtr, type, id);
lock (playlistsLock)
{
if (!playlists.ContainsKey(pm))
{
playlists.Add(pm, new Playlist(session, container, playlistPtr, type));
playlists.Add(pm, new Playlist(session, container, playlistPtr, type, id));
}
playlist = playlists[pm];
playlist.AddRef();
}
return new PlaylistWrapper(playlist);
}

internal static void Delete(IntPtr playlistPtr, sp_playlist_type type)
internal static void Delete(IntPtr playlistPtr, sp_playlist_type type, IntPtr id)
{
PlaylistMaintainer pm = new PlaylistMaintainer(playlistPtr, type);
PlaylistMaintainer pm = new PlaylistMaintainer(playlistPtr, type, id);
lock (playlistsLock)
{
Playlist playlist = playlists[pm];
Expand Down Expand Up @@ -332,10 +344,11 @@ protected virtual void OnImageChanged(ImageEventArgs args)
private sp_playlist_callbacks callbacks;
private IntPtr callbacksPtr = IntPtr.Zero;
private sp_playlist_type type;
private IntPtr folderId = IntPtr.Zero;
#endregion

#region Constructor
private Playlist(Session session, PlaylistContainer container, IntPtr playlistPtr, sp_playlist_type type)
private Playlist(Session session, PlaylistContainer container, IntPtr playlistPtr, sp_playlist_type type, IntPtr folderId)
{
if (playlistPtr == IntPtr.Zero)
throw new ArgumentException("playlistPtr can't be zero.");
Expand All @@ -349,6 +362,7 @@ private Playlist(Session session, PlaylistContainer container, IntPtr playlistPt
this.container = container;
this.playlistPtr = playlistPtr;
this.type = type;
this.folderId = folderId;
lock (libspotify.Mutex)
{
libspotify.sp_playlist_add_ref(playlistPtr);
Expand Down Expand Up @@ -377,8 +391,12 @@ public string Name
get
{
IsAlive(true);
lock (libspotify.Mutex)
return libspotify.GetString(libspotify.sp_playlist_name(playlistPtr), String.Empty);
if (type == sp_playlist_type.SP_PLAYLIST_TYPE_PLAYLIST)
lock (libspotify.Mutex)
return libspotify.GetString(libspotify.sp_playlist_name(playlistPtr), String.Empty);
else if (type != sp_playlist_type.SP_PLAYLIST_TYPE_PLACEHOLDER)
return container.GetFolderName(this);
throw new InvalidOperationException();
}
set
{
Expand Down Expand Up @@ -424,5 +442,17 @@ protected override int IntPtrHashCode()
{
return playlistPtr.GetHashCode();
}

public override bool Equals(object obj)
{
if (Object.ReferenceEquals(obj, null))
return false;
if (obj.GetType() == typeof(Playlist))
{
Playlist p = (Playlist)obj;
return p.playlistPtr == this.playlistPtr && p.type == this.type && p.folderId == this.folderId;
}
return false;
}
}
}
6 changes: 3 additions & 3 deletions SpotiFire.SpotifyLib/SpotifyTypes/PlaylistContainer.cs
Expand Up @@ -195,7 +195,7 @@ private PlaylistContainer(Session session, IntPtr pcPtr)
{
IsAlive(true);
lock (libspotify.Mutex)
return Playlist.Get(session, this, libspotify.sp_playlistcontainer_playlist(pcPtr, index), libspotify.sp_playlistcontainer_playlist_type(pcPtr, index));
return Playlist.Get(session, this, libspotify.sp_playlistcontainer_playlist(pcPtr, index), libspotify.sp_playlistcontainer_playlist_type(pcPtr, index), libspotify.sp_playlistcontainer_playlist_folder_id(pcPtr, index));
},
(playlist) =>
{
Expand Down Expand Up @@ -285,11 +285,11 @@ protected virtual void OnLoaded(EventArgs args)
#endregion

#region Internal Playlist methods
internal sp_playlist_type GetPlaylistType(Playlist playlist)
internal string GetFolderName(Playlist playlist)
{
int index = playlists.IndexOf(playlist);
lock (libspotify.Mutex)
return libspotify.sp_playlistcontainer_playlist_type(pcPtr, index);
return libspotify.GetString(libspotify.sp_playlistcontainer_playlist_folder_name(pcPtr, index), String.Empty);
}
#endregion

Expand Down

0 comments on commit e67cd0e

Please sign in to comment.