From c9eccd8b7006b3bcfa0a4d70cd26e43323c3c2b5 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 1 Dec 2010 13:45:34 +0000 Subject: [PATCH] Add signal_track_changed to Jukebox Make sure the current_track iterator is never uninitialized or invalid Add get_current_track method --- source/jukebox.cpp | 16 ++++++++++++++++ source/jukebox.h | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/source/jukebox.cpp b/source/jukebox.cpp index 3cd5e08..391edd1 100644 --- a/source/jukebox.cpp +++ b/source/jukebox.cpp @@ -7,6 +7,7 @@ Distributed under the LGPL #include #include +#include #include "jukebox.h" #include "sound.h" @@ -18,6 +19,7 @@ namespace AL { Jukebox::Jukebox(): streamer(source), sound(0), + current_track(tracks.end()), shuffle(false) { } @@ -32,7 +34,10 @@ void Jukebox::add_track(const string &trk) bool was_empty=tracks.empty(); tracks.push_back(trk); if(was_empty) + { current_track=tracks.begin(); + signal_track_changed.emit(*current_track); + } } void Jukebox::remove_track(const string &trk) @@ -43,6 +48,8 @@ void Jukebox::remove_track(const string &trk) if(i==current_track) next(); tracks.erase(i); + if(tracks.empty()) + current_track=tracks.end(); } } @@ -50,6 +57,14 @@ void Jukebox::clear_tracks() { stop(); tracks.clear(); + current_track=tracks.end(); +} + +const string &Jukebox::get_current_track() const +{ + if(tracks.empty()) + throw InvalidState("No current track"); + return *current_track; } void Jukebox::set_shuffle(bool s) @@ -91,6 +106,7 @@ void Jukebox::next() if(current_track==tracks.end()) current_track=tracks.begin(); } + signal_track_changed.emit(*current_track); } play(); } diff --git a/source/jukebox.h b/source/jukebox.h index 2b4e0cd..85a2b03 100644 --- a/source/jukebox.h +++ b/source/jukebox.h @@ -10,6 +10,7 @@ Distributed under the LGPL #include #include +#include #include "source.h" #include "streamer.h" @@ -20,6 +21,9 @@ class Sound; class Jukebox { +public: + sigc::signal signal_track_changed; + private: Source source; Streamer streamer; @@ -36,6 +40,7 @@ public: void add_track(const std::string &); void remove_track(const std::string &); void clear_tracks(); + const std::string &get_current_track() const; void set_shuffle(bool); void play(); -- 2.45.2