]> git.tdb.fi Git - libs/al.git/commitdiff
Add signal_track_changed to Jukebox
authorMikko Rasa <tdb@tdb.fi>
Wed, 1 Dec 2010 13:45:34 +0000 (13:45 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 1 Dec 2010 13:45:34 +0000 (13:45 +0000)
Make sure the current_track iterator is never uninitialized or invalid
Add get_current_track method

source/jukebox.cpp
source/jukebox.h

index 3cd5e080848ff0aeb517136774e2883aab9498bf..391edd14a305a6485f9a0741cd672bde0fef3c2e 100644 (file)
@@ -7,6 +7,7 @@ Distributed under the LGPL
 
 #include <algorithm>
 #include <cstdlib>
+#include <msp/core/except.h>
 #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();
 }
index 2b4e0cdf7c6d1667db823b1973a521535d082321..85a2b03201280e172e24828bf29b0771ad32c7ec 100644 (file)
@@ -10,6 +10,7 @@ Distributed under the LGPL
 
 #include <list>
 #include <string>
+#include <sigc++/signal.h>
 #include "source.h"
 #include "streamer.h"
 
@@ -20,6 +21,9 @@ class Sound;
 
 class Jukebox
 {
+public:
+       sigc::signal<void, const std::string &> 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();