]> git.tdb.fi Git - libs/al.git/blobdiff - source/jukebox.cpp
Use standard exception classes
[libs/al.git] / source / jukebox.cpp
index 3cd5e080848ff0aeb517136774e2883aab9498bf..d2a997d4cfffcf35f61f7e020183665b837f1492 100644 (file)
@@ -1,12 +1,6 @@
-/* $Id$
-
-This file is part of libmspal
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #include <algorithm>
 #include <cstdlib>
+#include <stdexcept>
 #include "jukebox.h"
 #include "sound.h"
 
@@ -18,6 +12,7 @@ namespace AL {
 Jukebox::Jukebox():
        streamer(source),
        sound(0),
+       current_track(tracks.end()),
        shuffle(false)
 { }
 
@@ -29,20 +24,25 @@ Jukebox::~Jukebox()
 
 void Jukebox::add_track(const string &trk)
 {
-       bool was_empty=tracks.empty();
+       bool was_empty = tracks.empty();
        tracks.push_back(trk);
        if(was_empty)
-               current_track=tracks.begin();
+       {
+               current_track = tracks.begin();
+               signal_track_changed.emit(*current_track);
+       }
 }
 
 void Jukebox::remove_track(const string &trk)
 {
-       list<string>::iterator i=find(tracks.begin(), tracks.end(), trk);
+       list<string>::iterator i = find(tracks.begin(), tracks.end(), trk);
        if(i!=tracks.end())
        {
                if(i==current_track)
                        next();
                tracks.erase(i);
+               if(tracks.empty())
+                       current_track = tracks.end();
        }
 }
 
@@ -50,11 +50,19 @@ void Jukebox::clear_tracks()
 {
        stop();
        tracks.clear();
+       current_track = tracks.end();
+}
+
+const string &Jukebox::get_current_track() const
+{
+       if(tracks.empty())
+               throw logic_error("No current track");
+       return *current_track;
 }
 
 void Jukebox::set_shuffle(bool s)
 {
-       shuffle=s;
+       shuffle = s;
 }
 
 void Jukebox::play()
@@ -62,7 +70,7 @@ void Jukebox::play()
        if(tracks.empty() || sound)
                return;
 
-       sound=new AL::Sound;
+       sound = new AL::Sound;
        sound->open_file(*current_track);
        streamer.play(*sound);
 }
@@ -76,7 +84,7 @@ void Jukebox::next()
                {
                        while(1)
                        {
-                               list<string>::iterator i=tracks.begin();
+                               list<string>::iterator i = tracks.begin();
                                advance(i, rand()%tracks.size());
                                if(i!=current_track)
                                {
@@ -89,8 +97,25 @@ void Jukebox::next()
                {
                        ++current_track;
                        if(current_track==tracks.end())
-                               current_track=tracks.begin();
+                               current_track = tracks.begin();
                }
+               signal_track_changed.emit(*current_track);
+       }
+       play();
+}
+
+void Jukebox::previous()
+{
+       if(shuffle)
+               return next();
+
+       stop();
+       if(tracks.size()>1)
+       {
+               if(current_track==tracks.begin())
+                       current_track = tracks.end();
+               --current_track;
+               signal_track_changed.emit(*current_track);
        }
        play();
 }
@@ -99,7 +124,7 @@ void Jukebox::stop()
 {
        streamer.stop();
        delete sound;
-       sound=0;
+       sound = 0;
 }
 
 void Jukebox::tick()