/* * QMPDClient - An MPD client written in Qt 4. * Copyright (C) 2005-2007 Håvard Tautra Knutsen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "config.h" #include "dynamicplaylist.h" #include "mpd.h" #include "mpdcache.h" DynamicPlaylist::DynamicPlaylist(QObject *parent) : QObject(parent), m_config(Config::instance()), m_mpd(MPD::instance()), m_cache(MPDCache::instance()) { setObjectName("dynamicplaylist"); connect(m_mpd, SIGNAL(playingSongUpdated(const MPDSong &)), this, SLOT(autoAdd(const MPDSong &))); connect(m_mpd, SIGNAL(playingSongUpdated(const MPDSong &)), this, SLOT(autoRemove(const MPDSong &))); connect(m_mpd, SIGNAL(playlistUpdated(const MPDSongList &)), this, SLOT(playlistUpdated(const MPDSongList &))); } void DynamicPlaylist::playlistUpdated(const MPDSongList &fs) { m_playlist = fs; } void DynamicPlaylist::autoAdd(const MPDSong &song) { if (song.isNull() || !m_config->autoAddSongs()) return; if (m_playlist.indexOf(song) == m_playlist.size() - (1 + m_config->autoAddPos())) { MPDSongList add = m_cache->randomSongs(m_config->autoAddCount()); Q_ASSERT(add.size() == m_config->autoAddCount()); m_mpd->addSongs(add); } } void DynamicPlaylist::autoRemove(const MPDSong &song) { if (song.isNull() || song == m_lastPlaying || !m_config->autoRemoveSongs()) { m_lastPlaying = song; return; } // Find song's new ID. foreach(MPDSong s, m_playlist) { if (!m_lastPlaying.isNull() && s.url() == m_lastPlaying.url()) { m_mpd->removeSongs(MPDSongList() << s); break; } } m_lastPlaying = song; }