upcomingplaylist.cpp 7.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (C) 2002-2004, 2008 Michael Pyne <mpyne@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.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */
16

17
#include "upcomingplaylist.h"
18
#include "juk-exception.h"
19

Michael Pyne's avatar
Michael Pyne committed
20
#include <QAction>
21 22 23 24 25 26

#include "playlistitem.h"
#include "playlistcollection.h"
#include "tracksequencemanager.h"
#include "collectionlist.h"
#include "actioncollection.h"
27
#include "juk_debug.h"
28 29 30

using namespace ActionCollection;

31
UpcomingPlaylist::UpcomingPlaylist(PlaylistCollection *collection) :
32
    Playlist(collection, true),
33
    m_active(false),
34
    m_oldIterator(0)
35
{
36
    setName(i18n("Play Queue"));
37
    setAllowDuplicates(true);
Kacper Kasper's avatar
Kacper Kasper committed
38
    setSortingEnabled(false);
39 40 41 42 43 44 45 46 47
}

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

void UpcomingPlaylist::initialize()
{
48 49
    // Prevent duplicate initialization.

50 51 52 53 54
    if(m_active)
        return;

    m_active = true;

55 56 57 58 59 60 61 62 63 64 65
    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());
}

void UpcomingPlaylist::appendItems(const PlaylistItemList &itemList)
{
66 67
    initialize();

68 69 70
    if(itemList.isEmpty())
        return;

71
    PlaylistItem *after = static_cast<PlaylistItem *>(topLevelItem(topLevelItemCount() - 1));
72

73 74 75
    foreach(PlaylistItem *playlistItem, itemList) {
        after = createItem(playlistItem, after);
        m_playlistIndex.insert(after, playlistItem->playlist());
76 77
    }

78
    playlistItemsChanged();
79 80 81 82 83
    slotWeightDirty();
}

void UpcomingPlaylist::playNext()
{
84
    initialize();
85

86
    PlaylistItem *next = TrackSequenceManager::instance()->nextItem();
87

88 89 90 91 92 93 94 95 96
    if(next) {
        setPlaying(next);
        Playlist *source = m_playlistIndex[next];
        if(source) {
            PlaylistList l;
            l.append(this);
            source->synchronizePlayingItems(l, false);
        }
    }
97
    else {
98
        removeIteratorOverride();
99 100 101 102 103 104 105 106 107 108

        // Normally we continue to play the currently playing item that way
        // a user can continue to hear their song when deselecting Play Queue.
        // However we're technically still "playing" when the queue empties and
        // we reinstall the old iterator so in this situation manually advance
        // to the next track. (Otherwise we hear the same song twice in a row
        // during the transition)

        setPlaying(manager()->nextItem());
    }
109 110
}

111
void UpcomingPlaylist::clearItem(PlaylistItem *item)
112 113
{
    m_playlistIndex.remove(item);
114
    Playlist::clearItem(item);
115 116
}

117
void UpcomingPlaylist::addFiles(const QStringList &files, PlaylistItem *after)
118
{
119
    CollectionList::instance()->addFiles(files, after);
120

121
    PlaylistItemList l;
122 123
    foreach(const QString &file, files) {
        FileHandle f(file);
124 125 126 127
        PlaylistItem *i = CollectionList::instance()->lookup(f.absFilePath());
        if(i)
            l.append(i);
    }
128

129
    appendItems(l);
130 131
}

Laurent Montel's avatar
Laurent Montel committed
132
QMap< PlaylistItem::Pointer, QPointer<Playlist> > &UpcomingPlaylist::playlistIndex()
133
{
134
    return m_playlistIndex;
135 136
}

137
void UpcomingPlaylist::removeIteratorOverride()
138
{
139 140
    if(!m_active)
        return;
141

142
    m_active = false; // Allow re-initialization.
143

144
    if(!m_oldIterator)
145
        return;
146

147 148
    // Install the old track iterator.

149
    manager()->installIterator(m_oldIterator);
150 151 152 153 154 155 156 157 158 159 160 161

    // If we have an item that is currently playing, allow it to keep playing.
    // Otherwise, just reset to the default iterator (probably not playing
    // anything.)
    // XXX: Reset to the last playing playlist?

    m_oldIterator->reset();
    if(playingItem())
        m_oldIterator->setCurrent(playingItem()->collectionItem());

    setPlaying(manager()->currentItem(), true);

162
    Watched::currentPlayingItemChanged();
163 164
}

165
TrackSequenceManager *UpcomingPlaylist::manager() const
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
    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();
187

188
    if(item) {
189 190
        QTreeWidgetItemIterator it(item);
        PlaylistItem *next = static_cast<PlaylistItem *>(*(++it));
191
        m_playlist->clearItem(item);
192
        setCurrent(next);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    }
}

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();
216
    if(playingItem && playingItem->playlist() == m_playlist && currentItem != playingItem)
217 218 219 220 221 222 223
        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) {
224 225
        PlaylistItem *i = m_playlist->createItem(currentItem,
                static_cast<PlaylistItem*>(nullptr));
226
        m_playlist->playlistIndex().insert(i, p);
227
        m_playlist->playlistItemsChanged();
228
        m_playlist->slotWeightDirty();
229 230
    }
    else {
231
        // if(p == m_playlist) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

        // 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 *)
{
248 249
    if(!m_playlist->items().isEmpty())
        setCurrent(m_playlist->firstChild());
250 251 252 253 254 255
}

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

Laurent Montel's avatar
Laurent Montel committed
256
    s << qint32(l.count());
257

258 259
    foreach(const PlaylistItem *playlistItem, l)
        s << playlistItem->file().absFilePath();
260 261 262 263 264 265 266 267

    return s;
}

QDataStream &operator>>(QDataStream &s, UpcomingPlaylist &p)
{
    QString fileName;
    PlaylistItem *newItem = 0;
Laurent Montel's avatar
Laurent Montel committed
268
    qint32 count;
269 270 271

    s >> count;

Laurent Montel's avatar
Laurent Montel committed
272
    for(qint32 i = 0; i < count; ++i) {
273
        s >> fileName;
274 275 276
        if(fileName.isEmpty())
            throw BICStreamException();

277
        newItem = p.createItem(FileHandle(fileName), newItem);
278 279 280 281 282
    }

    return s;
}

283
// vim: set et sw=4 tw=0 sta: