Fixed empty label in library after start
[someplayer] / src / player / player.cpp
index 746c708..51071b1 100644 (file)
@@ -32,21 +32,19 @@ using namespace SomePlayer::Storage;
 
 int Randomizer::next() {
        int res = 0;
-       if (_current == _rand.count()) {
+       if (_rand.count() == 0) {
                _shuffle();
-               _current = 0;
                res = next();
        } else {
-               res = _rand.at(_current);
+               res = _rand.takeFirst();
        }
-       ++_current;
        return res;
 }
 
 void Randomizer::setPlaylist(QList<int> pl) {
        _playlist = pl;
-       _current = 0;
        _shuffle();
+       _last = -1;
 }
 
 void Randomizer::_shuffle() {
@@ -62,6 +60,17 @@ void Randomizer::_shuffle() {
                _rand[i] = _rand[j];
                _rand[j] = tmp;
        }
+       if (cnt > 1 && _last == _rand[0]) {
+               _rand.removeAt(0);
+               _rand.insert(qrand() % (cnt-1) + 1, _last);
+       }
+       if (!_rand.isEmpty())
+               _last = _rand.last();
+       else _last = -1;
+}
+
+void Randomizer::removeId(int id) {
+       _rand.removeOne(id);
 }
 
 Player::Player(QObject *parent) :
@@ -74,22 +83,19 @@ Player::Player(QObject *parent) :
        _equalizer_enabled = false;
        connect(_player, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(_stateChanged(Phonon::State,Phonon::State)));
        connect(_player, SIGNAL(tick(qint64)), this, SLOT(_tick(qint64)));
+       connect(_player, SIGNAL(finished()), this, SLOT(next()));
        _path = Phonon::createPath(_player, _output);
        QList<Phonon::EffectDescription> effects = Phonon::BackendCapabilities::availableAudioEffects();
        foreach (Phonon::EffectDescription desc, effects) {
                if (desc.name() == "equalizer-10bands") {
                        _equalizer = new Phonon::Effect(desc, this);
                        Config config;
-                       if (config.getValue("equalizer/equalizer").toString() == "enabled") {
+                       if (config.equalizerEnabled()) {
                                for (int i = 0; i < 10; i++) {
-                                       QVariant var = config.getValue(QString("equalizer/band%1").arg(i));
+                                       QVariant var = config.getEqualizerValue(QString("band%1").arg(i));
                                        setEqualizerValue(i, var.toDouble());
                                }
                                enableEqualizer();
-                       } else if (config.getValue("equalizer/equalizer") == "") {
-                               for (int i = 0; i < 10; i++) {
-                                       config.setValue(QString("equalizer/band%1").arg(i), 0);
-                               }
                        }
                }
        }
@@ -101,6 +107,9 @@ Player::Player(QObject *parent) :
 }
 
 void Player::setTrackId(int id) {
+       if (_random) {
+               _randomizer.removeId(id);
+       }
        _current = id;
        if (!_history.isEmpty() && _history.top() != _current || _history.isEmpty()) {
                _history.push(_current);
@@ -183,22 +192,10 @@ void Player::_stateChanged(Phonon::State newState, Phonon::State /*oldState*/) {
                _state = PLAYER_PLAYING;
                emit stateChanged(_state);
                break;
-       case Phonon::StoppedState:
-               break;
-       case Phonon::LoadingState:
-               break;
-       case Phonon::PausedState:
-               if (_state == PLAYER_PLAYING) {
-                       next();
-               } else if (_state == PLAYER_ERROR) {
-                       play();
-               }
-               break;
-       case Phonon::BufferingState:
-               break;
        case Phonon::ErrorState:
                play(); // force
-//             _state = PLAYER_ERROR;
+               break;
+       default:
                break;
        }
 }
@@ -228,6 +225,9 @@ void Player::setPlaylist(Playlist playlist) {
 
 void Player::seek(int s) {
        _player->seek(s*1000);
+       if (s >= _track.metadata().length()) {
+               next();
+       }
 }
 
 void Player::play() {
@@ -285,7 +285,7 @@ void Player::enableEqualizer() {
        _equalizer_enabled = true;
        _path.insertEffect(_equalizer);
        Config config;
-       config.setValue("equalizer/equalizer", "enabled");
+       config.setEqualizerEnabled(true);
 }
 
 void Player::disableEqualizer() {
@@ -294,7 +294,7 @@ void Player::disableEqualizer() {
        _equalizer_enabled = false;
        _path.removeEffect(_equalizer);
        Config config;
-       config.setValue("equalizer/equalizer", "disabled");
+       config.setEqualizerEnabled(false);
 }
 
 void Player::setEqualizerValue(int band, double value) {
@@ -306,7 +306,7 @@ void Player::setEqualizerValue(int band, double value) {
        QList<Phonon::EffectParameter> plist = _equalizer->parameters();
        _equalizer->setParameterValue(plist[band], QVariant::fromValue(value));
        Config config;
-       config.setValue(QString("equalizer/band%1").arg(band), value);
+       config.setEqualizerValue(QString("band%1").arg(band), value);
 }
 
 QString Player::artist() {
@@ -326,3 +326,25 @@ QString Player::title() {
                return "";
        return _playlist.tracks().at(_current).metadata().title();
 }
+
+Track Player::current() {
+       if (_current >= 0 && _current < _playlist.tracks().count()) {
+               return _playlist.tracks().at(_current);
+       } else {
+               return Track();
+       }
+}
+
+void Player::pause() {
+       if (_state == PLAYER_PLAYING) {
+               _player->pause();
+               _state = PLAYER_PAUSED;
+               emit stateChanged(_state);
+       }
+}
+
+void Player::playIfPaused() {
+       if (_state == PLAYER_PAUSED) {
+               play();
+       }
+}