Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Search working

  • Loading branch information...
commit 04659ba578f038b00ab5c2df68e8527a207c93a5 1 parent 4311db2
@Alxandr authored
View
11 SpotiFire.LibSpotify/Enums.h
@@ -356,4 +356,15 @@ namespace SpotiFire {
/// <summary> Track not available on artist's request. </summary>
BannedByArtist = 3,
};
+
+ ///-------------------------------------------------------------------------------------------------
+ /// <summary> Values that represent type of search. </summary>
+ ///
+ /// <remarks> Aleksander, 03.11.2012. </remarks>
+ ///-------------------------------------------------------------------------------------------------
+ public enum class SearchType
+ {
+ Standard = 0,
+ Suggest = 1,
+ };
}
View
9 SpotiFire.LibSpotify/Interfaces.h
@@ -12,6 +12,15 @@ using namespace msclr::interop;
#define SP_DATA(type, data) (*(gcroot<type ^> *)data)
+template<typename T1>
+__forceinline T1 __SP_DATA_GET_AND_FREE(void *data) {
+ gcroot<T1> *root = (gcroot<T1> *)data;
+ T1 ret = *root;
+ delete root;
+ return ret;
+}
+#define SP_DATA_REM(type, data) __SP_DATA_GET_AND_FREE<type ^>(data)
+
namespace SpotiFire {
ref class Session;
View
201 SpotiFire.LibSpotify/Search.cpp
@@ -35,8 +35,209 @@ Session ^Search::Session::get() {
return _session;
}
+bool Search::IsLoaded::get() {
+ SPLock lock;
+ return sp_search_is_loaded(_ptr);
+}
+
+Error Search::Error::get() {
+ SPLock lock;
+ return ENUM(SpotiFire::Error, sp_search_error(_ptr));
+}
+
+String ^Search::Query::get() {
+ SPLock lock;
+ return UTF8(sp_search_query(_ptr));
+}
+
String ^Search::DidYouMean::get() {
SPLock lock;
return UTF8(sp_search_did_you_mean(_ptr));
}
+ref class $Search$Tracks sealed : ReadOnlyList<Track ^>
+{
+internal:
+ Search ^_search;
+ $Search$Tracks(Search ^search) { _search = search; }
+
+public:
+ virtual int DoCount() override sealed {
+ SPLock lock;
+ return sp_search_num_tracks(_search->_ptr);
+ }
+
+ virtual Track ^DoFetch(int index) override sealed {
+ SPLock lock;
+ return gcnew Track(_search->_session, sp_search_track(_search->_ptr, index));
+ }
+};
+
+IList<Track ^> ^Search::Tracks::get() {
+ if(_tracks == nullptr) {
+ Interlocked::CompareExchange<IList<Track ^> ^>(_tracks, gcnew $Search$Tracks(this), nullptr);
+ }
+ return _tracks;
+}
+
+ref class $Search$Albums sealed : ReadOnlyList<Album ^>
+{
+internal:
+ Search ^_search;
+ $Search$Albums(Search ^search) { _search = search; }
+
+public:
+ virtual int DoCount() override sealed {
+ SPLock lock;
+ return sp_search_num_albums(_search->_ptr);
+ }
+
+ virtual Album ^DoFetch(int index) override sealed {
+ SPLock lock;
+ return gcnew Album(_search->_session, sp_search_album(_search->_ptr, index));
+ }
+};
+
+IList<Album ^> ^Search::Albums::get() {
+ if(_albums == nullptr) {
+ Interlocked::CompareExchange<IList<Album ^> ^>(_albums, gcnew $Search$Albums(this), nullptr);
+ }
+ return _albums;
+}
+
+ref class $Search$Playlists sealed : ReadOnlyList<Playlist ^>
+{
+internal:
+ Search ^_search;
+ $Search$Playlists(Search ^search) { _search = search; }
+
+public:
+ virtual int DoCount() override sealed {
+ SPLock lock;
+ return sp_search_num_playlists(_search->_ptr);
+ }
+
+ virtual Playlist ^DoFetch(int index) override sealed {
+ SPLock lock;
+ return gcnew Playlist(_search->_session, sp_search_playlist(_search->_ptr, index));
+ }
+};
+
+IList<Playlist ^> ^Search::Playlists::get() {
+ if(_playlists == nullptr) {
+ Interlocked::CompareExchange<IList<Playlist ^> ^>(_playlists, gcnew $Search$Playlists(this), nullptr);
+ }
+ return _playlists;
+}
+
+ref class $Search$Artists sealed : ReadOnlyList<Artist ^>
+{
+internal:
+ Search ^_search;
+ $Search$Artists(Search ^search) { _search = search; }
+
+public:
+ virtual int DoCount() override sealed {
+ SPLock lock;
+ return sp_search_num_artists(_search->_ptr);
+ }
+
+ virtual Artist ^DoFetch(int index) override sealed {
+ SPLock lock;
+ return gcnew Artist(_search->_session, sp_search_artist(_search->_ptr, index));
+ }
+};
+
+IList<Artist ^> ^Search::Artists::get() {
+ if(_artists == nullptr) {
+ Interlocked::CompareExchange<IList<Artist ^> ^>(_artists, gcnew $Search$Artists(this), nullptr);
+ }
+ return _artists;
+}
+
+int Search::TotalTracks::get() {
+ SPLock lock;
+ return sp_search_total_tracks(_ptr);
+}
+
+int Search::TotalAlbums::get() {
+ SPLock lock;
+ return sp_search_total_albums(_ptr);
+}
+
+int Search::TotalPlaylists::get() {
+ SPLock lock;
+ return sp_search_total_playlists(_ptr);
+}
+
+int Search::TotalArtists::get() {
+ SPLock lock;
+ return sp_search_total_artists(_ptr);
+}
+
+void SP_CALLCONV $Search$complete(sp_search *search, void *userdata) {
+ TP0(SP_DATA_REM(Search, userdata), Search::complete);
+}
+
+Search ^Search::Create(SpotiFire::Session ^session,
+ String ^query,
+ int trackOffset, int trackCount,
+ int albumOffset, int albumCount,
+ int artistOffset, int artistCount,
+ int playlistOffset, int playlistCount,
+ SearchType type) {
+ SPLock lock;
+ marshal_context context;
+ gcroot<Search ^> *root = new gcroot<Search ^>();
+ Search ^ret = gcnew Search(session,
+ sp_search_create(
+ session->_ptr, context.marshal_as<const char *>(query),
+ trackOffset, trackCount,
+ albumOffset, albumCount,
+ artistOffset, artistCount,
+ playlistOffset, playlistCount,
+ (sp_search_type)type,
+ &$Search$complete, root
+ )
+ );
+ *root = ret;
+ return ret;
+}
+
+void Search::complete() {
+ array<Action ^> ^continuations = nullptr;
+ {
+ SPLock lock;
+ _complete = true;
+ if(_continuations != nullptr) {
+ continuations = gcnew array<Action ^>(_continuations->Count);
+ _continuations->CopyTo(continuations, 0);
+ _continuations->Clear();
+ _continuations = nullptr;
+ }
+ }
+ if(continuations != nullptr) {
+ for(int i = 0; i < continuations->Length; i++)
+ if(continuations[i])
+ continuations[i]();
+ }
+}
+
+//------------------------------------------
+// Await
+bool Search::IsComplete::get() {
+ SPLock lock;
+ return _complete;
+}
+
+bool Search::AddContinuation(Action ^continuationAction) {
+ SPLock lock;
+ if(IsLoaded)
+ return false;
+
+ if(_continuations == nullptr)
+ _continuations = gcnew List<Action ^>;
+
+ _continuations->Add(continuationAction);
+ return true;
+}
View
34 SpotiFire.LibSpotify/Search.h
@@ -8,8 +8,17 @@ using namespace System::Collections::Generic;
namespace SpotiFire {
- public ref class Search sealed : ISpotifyObject
+ public ref class Search sealed : ISpotifyObject, ISpotifyAwaitable
{
+ private:
+ IList<Track ^> ^_tracks;
+ IList<Album ^> ^_albums;
+ IList<Playlist ^> ^_playlists;
+ IList<Artist ^> ^_artists;
+
+ List<Action ^> ^_continuations;
+ bool _complete;
+
internal:
Session ^_session;
sp_search *_ptr;
@@ -20,6 +29,29 @@ namespace SpotiFire {
public:
virtual property Session ^Session { SpotiFire::Session ^get() sealed; }
+ virtual property bool IsLoaded { bool get() sealed; }
+ virtual property Error Error { SpotiFire::Error get() sealed; }
+ virtual property String ^Query { String ^get() sealed; }
virtual property String ^DidYouMean { String ^get() sealed; }
+
+ virtual property IList<Track ^> ^Tracks { IList<Track ^> ^get() sealed; }
+ virtual property IList<Album ^> ^Albums { IList<Album ^> ^get() sealed; }
+ virtual property IList<Playlist ^> ^Playlists { IList<Playlist ^> ^get() sealed; }
+ virtual property IList<Artist ^> ^Artists { IList<Artist ^> ^get() sealed; }
+
+ virtual property int TotalTracks { int get() sealed; }
+ virtual property int TotalAlbums { int get() sealed; }
+ virtual property int TotalPlaylists { int get() sealed; }
+ virtual property int TotalArtists { int get() sealed; }
+
+ private:
+ virtual property bool IsComplete { bool get() override sealed = ISpotifyAwaitable::IsComplete::get; }
+ virtual bool AddContinuation(Action ^continuationAction) override sealed = ISpotifyAwaitable::AddContinuation;
+
+ internal:
+ static Search ^Create(SpotiFire::Session ^session, String ^query, int trackOffset, int trackCount, int albumOffset, int albumCount, int artistOffset, int artistCount, int playlistOffset, int playlistCount, SearchType type);
+
+ // Spotify events
+ void complete();
};
}
View
23 SpotiFire.SpotifyLib/SpotifyLibExtensions.cs
@@ -42,25 +42,32 @@ public static AwaitHelper.AwaitableAwaiter<Search> GetAwaiter(this Search search
return AwaitHelper.GetAwaiter<Search>(search);
}
+ public static Search Search(this Session session,
+ string query,
+ int trackOffset, int trackCount,
+ int albumOffset, int albumCount,
+ int artistOffset, int artistCount,
+ int playlistOffset, int playlistCount,
+ SearchType type)
+ {
+ return SpotiFire.Search.Create(session, query, trackOffset, trackCount, albumOffset, albumCount, artistOffset, artistCount, playlistOffset, playlistCount, type);
+ }
+
public static Search SearchTracks(this Session session, string query, int trackOffset, int trackCount)
{
- throw new NotImplementedException();
- //return session.Search(query, trackOffset, trackCount, 0, 0, 0, 0, 0, 0, SearchType.Standard);
+ return Search(session, query, trackOffset, trackCount, 0, 0, 0, 0, 0, 0, SearchType.Standard);
}
public static Search SearchAlbums(this Session session, string query, int albumOffset, int albumCount)
{
- throw new NotImplementedException();
- //return session.Search(query, 0, 0, albumOffset, albumCount, 0, 0, 0, 0, SearchType.Standard);
+ return Search(session, query, 0, 0, albumOffset, albumCount, 0, 0, 0, 0, SearchType.Standard);
}
public static Search SearchArtists(this Session session, string query, int artistOffset, int artistCount)
{
- throw new NotImplementedException();
- //return session.Search(query, 0, 0, 0, 0, artistOffset, artistCount, 0, 0, SearchType.Standard);
+ return Search(session, query, 0, 0, 0, 0, artistOffset, artistCount, 0, 0, SearchType.Standard);
}
public static Search SearchPlaylist(this Session session, string query, int playlistOffset, int playlistCount)
{
- throw new NotImplementedException();
- //return session.Search(query, 0, 0, 0, 0, 0, 0, playlistOffset, playlistCount, SearchType.Standard);
+ return Search(session, query, 0, 0, 0, 0, 0, 0, playlistOffset, playlistCount, SearchType.Standard);
}
// ArtistBrowse methods made Synchronously
View
10 SpotiFire.WinFormsTest/Form1.cs
@@ -40,12 +40,12 @@ void session_MusicDeliverd(Session sender, MusicDeliveryEventArgs e)
private async void button1_Click(object sender, EventArgs e)
{
- //var search = await session.SearchTracks(textBox1.Text, 0, 100);
- //List<CachedTrack> tracks = new List<CachedTrack>(search.Tracks.Count);
- //foreach (var t in search.Tracks)
- // tracks.Add(await CachedTrack.Make(t));
+ var search = await session.SearchTracks(textBox1.Text, 0, 100);
+ List<CachedTrack> tracks = new List<CachedTrack>(search.Tracks.Count);
+ foreach (var t in search.Tracks)
+ tracks.Add(await CachedTrack.Make(t));
- //dataGridView1.DataSource = tracks;
+ dataGridView1.DataSource = tracks;
}
private async void Form1_Load(object sender, EventArgs e)
Please sign in to comment.
Something went wrong with that request. Please try again.