diff --git a/src/api.vala b/src/api.vala index fb61c7b..7f084c6 100644 --- a/src/api.vala +++ b/src/api.vala @@ -22,6 +22,8 @@ errordomain SubsonicError { ERROR } +public delegate void Wavelet.SongCallback (Song song); + public class Wavelet.Artist : Object, Json.Serializable { public string index; public string id; @@ -131,7 +133,7 @@ public struct Wavelet.API.PlayQueue { this.changed_by = reader.get_string_value (); reader.end_member (); - print("%s %lli %s %s\n",this.current, this.position, this.changed, this.changed_by); + //print("%s %lli %s %s\n",this.current, this.position, this.changed, this.changed_by); this.songs = new ListStore (typeof (Song)); @@ -193,6 +195,7 @@ public class Wavelet.Subsonic : Object { public async void ping () throws Error { var msg = new Soup.Message ("GET", @"$(this.url)/rest/ping?$(this.parameters)"); var bytes = yield this.session.send_and_read_async (msg, Priority.DEFAULT, null); + assert (msg.get_status () == Soup.Status.OK); var parser = new Json.Parser (); parser.load_from_data ((string) bytes.get_data ()); @@ -323,4 +326,34 @@ public class Wavelet.Subsonic : Object { } } } + + public async void get_random_songs (string? parameters, SongCallback callback) throws Error { + string str_parameters; + if (parameters == null) { + str_parameters = ""; + } else { + str_parameters = @"$parameters&"; + } + + var msg = new Soup.Message("GET", @"$(this.url)/rest/getRandomSongs?$(str_parameters)size=500&$(this.parameters)"); + var bytes = yield this.session.send_and_read_async (msg, Priority.DEFAULT, null); + assert (msg.get_status () == Soup.Status.OK); + + var parser = new Json.Parser (); + parser.load_from_data ((string) bytes.get_data ()); + + var reader = new Json.Reader (parser.get_root ()); + this.unwrap_response (reader); + + reader.read_member ("randomSongs"); + reader.read_member ("song"); + + for (int i = 0; i < reader.count_elements (); i += 1) { + reader.read_element (i); + callback (new Song (reader)); + reader.end_element (); + } + + assert (reader.get_error () == null); + } } diff --git a/src/application.vala b/src/application.vala index 91b0124..ba9e4e3 100644 --- a/src/application.vala +++ b/src/application.vala @@ -71,6 +71,18 @@ public class Wavelet.Application : Adw.Application { win.song_list.set_ready (); }); //api.reload.begin (); + + win.shuffle_all_tracks.sensitive = true; + win.shuffle_all_tracks.activated.connect (() => { + win.shuffle_all_tracks.sensitive = false; + win.play_queue.clear (); + api.get_random_songs.begin (null, (song) => { + win.play_queue.queue (song); + }, (obj, res) => { + api.get_random_songs.end (res); + win.shuffle_all_tracks.sensitive = true; + }); + }); }); win.setup.load (config_db); } diff --git a/src/play_queue.blp b/src/play_queue.blp index 89c1f0f..4688b2e 100644 --- a/src/play_queue.blp +++ b/src/play_queue.blp @@ -10,15 +10,11 @@ template $WaveletPlayQueue: Adw.NavigationPage { ScrolledWindow { ListView list_view { - styles [ - "view", - ] - factory: BuilderListItemFactory { template ListItem { child: Label { halign: start; - label: bind template.item as <$WaveletSong>.name; + label: bind template.item as <$WaveletSong>.title; }; } }; diff --git a/src/window.blp b/src/window.blp index 5f6945e..fcdd797 100644 --- a/src/window.blp +++ b/src/window.blp @@ -53,9 +53,10 @@ template $WaveletWindow: Adw.ApplicationWindow { "navigation-sidebar", ] - Adw.ButtonRow { + Adw.ButtonRow shuffle_all_tracks { title: _("Shuffle all tracks"); start-icon-name: "media-playlist-shuffle"; + sensitive: false; } } }; diff --git a/src/window.vala b/src/window.vala index e7a2501..ca3873f 100644 --- a/src/window.vala +++ b/src/window.vala @@ -29,6 +29,7 @@ public class Wavelet.Window : Adw.ApplicationWindow { [GtkChild] public unowned Wavelet.ArtistList artist_list; [GtkChild] public unowned Wavelet.SongList song_list; [GtkChild] public unowned Wavelet.PlayQueue play_queue; + [GtkChild] public unowned Adw.ButtonRow shuffle_all_tracks; public Window (Gtk.Application app) { Object (application: app);