summaryrefslogtreecommitdiffstats
path: root/juk/tracksequencemanager.cpp
blob: 828c61d555c094b1a0b26577b1c51d8111ec7d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
112
113
114
115
116
117
118
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/***************************************************************************
    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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kconfig.h>
#include <klocale.h>
#include <kaction.h>
#include <kpopupmenu.h>

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

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

TrackSequenceManager::~TrackSequenceManager()
{
    // m_playlist and m_popupMenu don't belong to us, don't try to delete them
    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;
    
    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;

    m_iterator = 0;
    return temp;
}

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

    if(!manager.m_initialized)
        manager.initialize();

    return &manager;
}

PlaylistItem *TrackSequenceManager::nextItem()
{
    if(m_playNextItem) {
        
        // Force the iterator to reset state (such as random item lists)

        m_iterator->reset();        
        m_iterator->setCurrent(m_playNextItem);
        m_playNextItem = 0;
    }
    else if(m_iterator->current())
        m_iterator->advance();
    else if(currentPlaylist())
        m_iterator->prepareToPlay(currentPlaylist());
    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)
{
    if(m_playlist)
        m_playlist->disconnect(this);
    m_playlist = list;

    connect(m_playlist, TQT_SIGNAL(signalAboutToRemove(PlaylistItem *)),
            this,       TQT_SLOT(slotItemAboutToDie(PlaylistItem *)));
}

void TrackSequenceManager::setCurrent(PlaylistItem *item)
{
    if(item != m_iterator->current()) {
        m_iterator->setCurrent(item);
        if(item)
        setCurrentPlaylist(item->playlist());
    else
            m_iterator->reset();        
    }
}

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

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

    if(!collection)
        return;

    // Make sure we don't use m_playNextItem if it's invalid.
    connect(collection, TQT_SIGNAL(signalAboutToRemove(PlaylistItem *)),
            this, TQT_SLOT(slotItemAboutToDie(PlaylistItem *)));

    m_initialized = true;
}

TrackSequenceManager::TrackSequenceManager() :
    TQObject(),
    m_playlist(0),
    m_playNextItem(0),
    m_popupMenu(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;

    m_iterator->itemAboutToDie(item);
}

#include "tracksequencemanager.moc"

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