tracksequencemanager.cpp 4.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (C) 2002-2004 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

Yuri Chornoivan's avatar
Yuri Chornoivan committed
17
#include "tracksequencemanager.h"
Michael Pyne's avatar
Michael Pyne committed
18
#include <KLocalizedString>
19
#include <kconfig.h>
Michael Pyne's avatar
Michael Pyne committed
20

Michael Pyne's avatar
Michael Pyne committed
21
#include <QAction>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#include "actioncollection.h"
#include "playlist.h"
#include "playlistitem.h"
#include "tracksequenceiterator.h"
#include "tag.h"
#include "filehandle.h"
#include "collectionlist.h"

/////////////////////////////////////////////////////////////////////////////
// public functions
/////////////////////////////////////////////////////////////////////////////

TrackSequenceManager::~TrackSequenceManager()
{
Michael Pyne's avatar
Michael Pyne committed
37
    // m_playlist doesn't belong to us, don't try to delete
38 39 40 41 42 43 44 45 46 47
    if(m_iterator == m_defaultIterator)
        m_iterator = 0;

    delete m_iterator;
    delete m_defaultIterator;
}

bool TrackSequenceManager::installIterator(TrackSequenceIterator *iterator)
{
    PlaylistItem *oldItem = m_iterator ? m_iterator->current() : 0;
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    if(m_iterator != m_defaultIterator)
        delete m_iterator;

    m_iterator = m_defaultIterator;
    if(iterator)
        m_iterator = iterator;

    m_iterator->setCurrent(oldItem);

    return true;
}

PlaylistItem *TrackSequenceManager::currentItem() const
{
    return m_iterator->current();
}

TrackSequenceIterator *TrackSequenceManager::takeIterator()
{
    TrackSequenceIterator *temp = m_iterator;

70
    m_iterator = nullptr;
71 72 73 74 75
    return temp;
}

TrackSequenceManager *TrackSequenceManager::instance()
{
76
    static TrackSequenceManager manager;
77

78 79
    if(!manager.m_initialized)
        manager.initialize();
80

81
    return &manager;
82 83 84 85 86
}

PlaylistItem *TrackSequenceManager::nextItem()
{
    if(m_playNextItem) {
87

88 89
        // Force the iterator to reset state (such as random item lists)

90
        m_iterator->reset();
91
        m_iterator->prepareToPlay(m_playNextItem->playlist());
92
        m_iterator->setCurrent(m_playNextItem);
93
        m_playNextItem = nullptr;
94 95 96
    }
    else if(m_iterator->current())
        m_iterator->advance();
97 98
    else if(currentPlaylist())
        m_iterator->prepareToPlay(currentPlaylist());
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    else
        m_iterator->prepareToPlay(CollectionList::instance());

    return m_iterator->current();
}

PlaylistItem *TrackSequenceManager::previousItem()
{
    m_iterator->backup();
    return m_iterator->current();
}

/////////////////////////////////////////////////////////////////////////////
// public slots
/////////////////////////////////////////////////////////////////////////////

void TrackSequenceManager::setNextItem(PlaylistItem *item)
{
    m_playNextItem = item;
}

void TrackSequenceManager::setCurrentPlaylist(Playlist *list)
{
Michael Pyne's avatar
Michael Pyne committed
122 123
    if(m_playlist)
        m_playlist->disconnect(this);
124
    m_playlist = list;
Michael Pyne's avatar
Michael Pyne committed
125

Laurent Montel's avatar
Laurent Montel committed
126 127
    connect(m_playlist, SIGNAL(signalAboutToRemove(PlaylistItem*)),
            this,       SLOT(slotItemAboutToDie(PlaylistItem*)));
128 129 130 131 132 133
}

void TrackSequenceManager::setCurrent(PlaylistItem *item)
{
    if(item != m_iterator->current()) {
        m_iterator->setCurrent(item);
134
        if(item)
Michael Pyne's avatar
Michael Pyne committed
135 136
            setCurrentPlaylist(item->playlist());
        else
137
            m_iterator->reset();
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    }
}

/////////////////////////////////////////////////////////////////////////////
// private functions
/////////////////////////////////////////////////////////////////////////////

void TrackSequenceManager::initialize()
{
    CollectionList *collection = CollectionList::instance();

    if(!collection)
        return;

    // Make sure we don't use m_playNextItem if it's invalid.
Laurent Montel's avatar
Laurent Montel committed
153 154
    connect(collection, SIGNAL(signalAboutToRemove(PlaylistItem*)),
            this, SLOT(slotItemAboutToDie(PlaylistItem*)));
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    m_initialized = true;
}

TrackSequenceManager::TrackSequenceManager() :
    QObject(),
    m_playlist(0),
    m_playNextItem(0),
    m_iterator(0),
    m_initialized(false)
{
    m_defaultIterator = new DefaultSequenceIterator();
    m_iterator = m_defaultIterator;
}

/////////////////////////////////////////////////////////////////////////////
// protected slots
/////////////////////////////////////////////////////////////////////////////

void TrackSequenceManager::slotItemAboutToDie(PlaylistItem *item)
{
    if(item == m_playNextItem)
        m_playNextItem = 0;
Michael Pyne's avatar
Michael Pyne committed
178 179

    m_iterator->itemAboutToDie(item);
180 181
}

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