upcomingplaylist.cpp 8.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/***************************************************************************
    begin                : Thu Aug 19 2004 
    copyright            : (C) 2002 - 2004 by Michael Pyne
    email                : michael.pyne@kde.org
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

16 17 18 19 20 21 22 23 24 25 26 27 28
#include <kdebug.h>
#include <kapplication.h>
#include <kaction.h>

#include "upcomingplaylist.h"
#include "playlistitem.h"
#include "playlistcollection.h"
#include "tracksequencemanager.h"
#include "collectionlist.h"
#include "actioncollection.h"

using namespace ActionCollection;

29
UpcomingPlaylist::UpcomingPlaylist(PlaylistCollection *collection, int defaultSize) :
30 31 32 33
    Playlist(collection, true),
    m_oldIterator(0),
    m_defaultSize(defaultSize)
{
34
    setName(i18n("Play Queue"));
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    setAllowDuplicates(true);
    setSorting(-1);
}

UpcomingPlaylist::~UpcomingPlaylist()
{
    removeIteratorOverride();
    setUpcomingPlaylist(0);
}

void UpcomingPlaylist::initialize()
{
    m_oldIterator = manager()->takeIterator();
    manager()->installIterator(new UpcomingSequenceIterator(this));

    if(!m_oldIterator->current())
        m_oldIterator->prepareToPlay(CollectionList::instance());
    else
        manager()->iterator()->setCurrent(m_oldIterator->current());

    // If an item was already playing, then it will be the last item in the
    // list instead of the first, so we'll need to move it after the list is
    // initialized.  Also, we should fill the list to our set number of items.

59
    bool needsAdjusting = playingItem() != 0;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    if(needsAdjusting && action<KToggleAction>("loopPlaylist")->isChecked()) {
        m_oldIterator->advance();
        fillList();
        PlaylistItem *item = static_cast<PlaylistItem *>(lastItem());
        takeItem(item);
        insertItem(item);
        setPlaying(item, false);
    }

    setUpcomingPlaylist(this);
}

void UpcomingPlaylist::appendItems(const PlaylistItemList &itemList)
{
    if(itemList.isEmpty())
        return;

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    PlaylistItem *after = static_cast<PlaylistItem *>(lastItem());

    for(PlaylistItemList::ConstIterator it = itemList.begin(); it != itemList.end(); ++it) {
        after = createItem(*it, after);
        m_playlistIndex.insert(after, (*it)->playlist());
    }

    dataChanged();
    slotWeightDirty();
}

void UpcomingPlaylist::playNext()
{
    TrackSequenceManager::instance()->setCurrentPlaylist(this);
    setPlaying(TrackSequenceManager::instance()->nextItem());

    if(!playing())
        return;

    Playlist *source = m_playlistIndex[playingItem()];

    if(!source)
        return;

    PlaylistList l;
    l.append(this);

    source->synchronizePlayingItems(l, false);
}

void UpcomingPlaylist::clearItem(PlaylistItem *item, bool emitChanged)
{
    m_playlistIndex.remove(item);
    Playlist::clearItem(item, emitChanged);
112 113
}

114 115 116 117 118
QMap< PlaylistItem *, QGuardedPtr<Playlist> > &UpcomingPlaylist::playlistIndex()
{
    return m_playlistIndex;
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
void UpcomingPlaylist::removeIteratorOverride()
{
    if(!m_oldIterator)
        return;

    PlaylistItem *newItem = 0;
    if(playingItem())
        newItem = playingItem()->collectionItem();

    m_oldIterator->reset();
    m_oldIterator->setCurrent(newItem);

    manager()->setCurrent(newItem);
    manager()->installIterator(m_oldIterator);

    setPlaying(newItem, false);

    Watched::currentChanged();
}

void UpcomingPlaylist::fillList()
{
    PlaylistItemList list;
    int i = 0, limit = m_defaultSize - items().count();

    if(!m_oldIterator->current())
        m_oldIterator->prepareToPlay(CollectionList::instance());

    if(!m_oldIterator->current())
        return;

    while(i++ < limit && m_oldIterator->current()) {
        list.append(m_oldIterator->current());
        m_oldIterator->advance();
    }

    createItems(list);
}

void UpcomingPlaylist::addNewItem()
{
    PlaylistItem *last = static_cast<PlaylistItem *>(lastChild());
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    if(firstChild()) {
        Playlist *source = m_playlistIndex[firstChild()];

        if(!source)
            return;

        CollectionListItem *base = firstChild()->collectionItem();
        PlaylistItem *target = 0;
        for(QListViewItemIterator it(source); it.current(); ++it) {
            if(static_cast<PlaylistItem *>(it.current())->collectionItem() == base) {
                target = static_cast<PlaylistItem *>(it.current());
                break;
            }
        }

        if(!target)
            return;

        m_oldIterator->setCurrent(target);
        m_oldIterator->advance();
    }

    if(m_oldIterator->current()) {
        last = createItem(m_oldIterator->current(), last);
        m_playlistIndex.insert(last, m_oldIterator->current()->playlist());
        dataChanged();
        slotWeightDirty();
    }
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

inline TrackSequenceManager *UpcomingPlaylist::manager() const
{
    return TrackSequenceManager::instance();
}

UpcomingPlaylist::UpcomingSequenceIterator::UpcomingSequenceIterator(UpcomingPlaylist *playlist) :
    TrackSequenceIterator(), m_playlist(playlist)
{
}

UpcomingPlaylist::UpcomingSequenceIterator::UpcomingSequenceIterator(const UpcomingSequenceIterator &other) :
    TrackSequenceIterator(other), m_playlist(other.m_playlist)
{
}

UpcomingPlaylist::UpcomingSequenceIterator::~UpcomingSequenceIterator()
{
}

void UpcomingPlaylist::UpcomingSequenceIterator::advance()
{
    PlaylistItem *item = m_playlist->firstChild();
214

215
    if(item) {
216
        if(!item->nextSibling())
217 218
            m_playlist->addNewItem();

219
        m_playlist->setPlaying(0);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
        m_playlist->clearItem(item);
    }
    else
        m_playlist->fillList();

    setCurrent(m_playlist->firstChild());
}

void UpcomingPlaylist::UpcomingSequenceIterator::backup()
{
}

UpcomingPlaylist::UpcomingSequenceIterator *UpcomingPlaylist::UpcomingSequenceIterator::clone() const
{
    return new UpcomingSequenceIterator(*this);
}

void UpcomingPlaylist::UpcomingSequenceIterator::setCurrent(PlaylistItem *currentItem)
{
    if(!currentItem) {
        TrackSequenceIterator::setCurrent(currentItem);
        return;
    }

    // If the upcoming playlist is playing something, clear it out since
    // apparently the user didn't want to hear it.

    PlaylistItem *playingItem = m_playlist->playingItem();
    if(playingItem && playingItem->playlist() == m_playlist && currentItem != playingItem) {
        if(action<KToggleAction>("loopPlaylist")->isChecked())
            m_playlist->addNewItem();

        m_playlist->clearItem(playingItem);
    }

    // If a different playlist owns this item, add it to the upcoming playlist

    Playlist *p = currentItem->playlist();

    if(p != m_playlist) {
260 261 262 263
        PlaylistItem *i = m_playlist->createItem(currentItem, (PlaylistItem *) 0);
        m_playlist->playlistIndex().insert(i, p);
        m_playlist->dataChanged();
        m_playlist->slotWeightDirty();
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    }
    else {

        // Bump this item up to the top

        m_playlist->takeItem(currentItem);
        m_playlist->insertItem(currentItem);
    }

    TrackSequenceIterator::setCurrent(m_playlist->firstChild());
}

void UpcomingPlaylist::UpcomingSequenceIterator::reset()
{
    setCurrent(0);
}

void UpcomingPlaylist::UpcomingSequenceIterator::prepareToPlay(Playlist *)
{
    if(m_playlist->items().isEmpty())
        m_playlist->fillList();
    setCurrent(m_playlist->firstChild());
}

QDataStream &operator<<(QDataStream &s, const UpcomingPlaylist &p)
{
    PlaylistItemList l = const_cast<UpcomingPlaylist *>(&p)->items();

    s << Q_INT32(l.count());

    for(PlaylistItemList::ConstIterator it = l.begin(); it != l.end(); ++it)
        s << (*it)->file().absFilePath();

    return s;
}

QDataStream &operator>>(QDataStream &s, UpcomingPlaylist &p)
{
    QString fileName;
    PlaylistItem *newItem = 0;
    Q_INT32 count;

    s >> count;

    for(Q_INT32 i = 0; i < count; ++i) {
        s >> fileName;
        newItem = p.createItem(FileHandle(fileName), newItem, false);
    }

    return s;
}

// vim: set et ts=4 sw=4: