tracksequenceiterator.cpp 8.68 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

17 18
#include "tracksequenceiterator.h"

19
#include <kaction.h>
20
#include <kapplication.h>
21
#include <kdebug.h>
22 23
#include <krandom.h>
#include <ktoggleaction.h>
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

#include "playlist.h"
#include "actioncollection.h"
#include "tag.h"
#include "filehandle.h"

using namespace ActionCollection;

TrackSequenceIterator::TrackSequenceIterator() :
    m_current(0)
{
}

TrackSequenceIterator::TrackSequenceIterator(const TrackSequenceIterator &other) :
    m_current(other.m_current)
{
}

TrackSequenceIterator::~TrackSequenceIterator()
{
}

void TrackSequenceIterator::setCurrent(PlaylistItem *current)
{
    m_current = current;
}

Michael Pyne's avatar
Michael Pyne committed
51 52 53 54 55 56 57 58
void TrackSequenceIterator::playlistChanged()
{
}

void TrackSequenceIterator::itemAboutToDie(const PlaylistItem *)
{
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
DefaultSequenceIterator::DefaultSequenceIterator() :
    TrackSequenceIterator()
{
}

DefaultSequenceIterator::DefaultSequenceIterator(const DefaultSequenceIterator &other)
    : TrackSequenceIterator(other)
{
}

DefaultSequenceIterator::~DefaultSequenceIterator()
{
}

void DefaultSequenceIterator::advance()
{
    if(!current())
        return;

    bool isRandom = action("randomPlay") && action<KToggleAction>("randomPlay")->isChecked();
79
    bool loop = action<KAction>("loopPlaylist") && action<KAction>("loopPlaylist")->isChecked();
80 81 82
    bool albumRandom = action("albumRandomPlay") && action<KToggleAction>("albumRandomPlay")->isChecked();

    if(isRandom || albumRandom) {
83 84 85 86 87 88 89 90 91 92 93
        if(m_randomItems.isEmpty() && loop) {

            // Since refillRandomList will remove the currently playing item,
            // we should clear it out first since that's not good for e.g.
            // lists with 1-2 items.  We need to remember the Playlist though.

            Playlist *playlist = current()->playlist();
            setCurrent(0);

            refillRandomList(playlist);
        }
94 95 96 97 98 99 100 101 102

        if(m_randomItems.isEmpty()) {
            setCurrent(0);
            return;
        }

        PlaylistItem *item;

        if(albumRandom) {
103
            if(m_albumSearch.isNull() || m_albumSearch.matchedItems().isEmpty()) {
104
                item = m_randomItems[KRandom::random() % m_randomItems.count()];
105 106
                initAlbumSearch(item);
            }
107

108
            // This can be null if initAlbumSearch() left the m_albumSearch
109 110
            // empty because the album text was empty.  Since we initAlbumSearch()
            // with an item, the matchedItems() should never be empty.
111

112 113
            if(!m_albumSearch.isNull()) {
                PlaylistItemList albumMatches = m_albumSearch.matchedItems();
114
                if(albumMatches.isEmpty()) {
115 116
                    kError() << "Unable to initialize album random play.\n";
                    kError() << "List of potential results is empty.\n";
117 118 119

                    return; // item is still set to random song from a few lines earlier.
                }
120

121
                item = albumMatches[0];
122

123
                // Pick first song remaining in list.
124

125
                for(int i = 0; i < albumMatches.count(); ++i)
126 127 128
                    if(albumMatches[i]->file().tag()->track() < item->file().tag()->track())
                        item = albumMatches[i];
                m_albumSearch.clearItem(item);
129 130 131 132 133

                if(m_albumSearch.matchedItems().isEmpty()) {
                    m_albumSearch.clearComponents();
                    m_albumSearch.search();
                }
134
            }
135
            else
136
                kError() << "Unable to perform album random play on " << *item << endl;
137 138
        }
        else
139
            item = m_randomItems[KRandom::random() % m_randomItems.count()];
140 141

        setCurrent(item);
142
        m_randomItems.removeAll(item);
143 144
    }
    else {
145
        PlaylistItem *next = current()->itemBelow();
146 147
        if(!next && loop) {
            Playlist *p = current()->playlist();
148
            next = p->firstChild();
149 150
            while(next && !next->isVisible())
                next = static_cast<PlaylistItem *>(next->nextSibling());
151 152 153 154 155 156 157 158 159 160 161
        }

        setCurrent(next);
    }
}

void DefaultSequenceIterator::backup()
{
    if(!current())
        return;

162
    PlaylistItem *item = current()->itemAbove();
163 164 165 166 167 168 169 170 171

    if(item)
        setCurrent(item);
}

void DefaultSequenceIterator::prepareToPlay(Playlist *playlist)
{
    bool random = action("randomPlay") && action<KToggleAction>("randomPlay")->isChecked();
    bool albumRandom = action("albumRandomPlay") && action<KToggleAction>("albumRandomPlay")->isChecked();
172

173 174 175 176 177 178 179
    if(random || albumRandom) {
        PlaylistItemList items = playlist->selectedItems();
        if(items.isEmpty())
            items = playlist->visibleItems();

        PlaylistItem *newItem = 0;
        if(!items.isEmpty())
180
            newItem = items[KRandom::random() % items.count()];
181 182 183 184 185

        setCurrent(newItem);
        refillRandomList();
    }
    else {
Laurent Montel's avatar
Laurent Montel committed
186
        Q3ListViewItemIterator it(playlist, Q3ListViewItemIterator::Visible | Q3ListViewItemIterator::Selected);
187
        if(!it.current())
Laurent Montel's avatar
Laurent Montel committed
188
            it = Q3ListViewItemIterator(playlist, Q3ListViewItemIterator::Visible);
189 190 191 192 193 194 195 196 197 198 199 200 201

        setCurrent(static_cast<PlaylistItem *>(it.current()));
    }
}

void DefaultSequenceIterator::reset()
{
    m_randomItems.clear();
    m_albumSearch.clearComponents();
    m_albumSearch.search();
    setCurrent(0);
}

Michael Pyne's avatar
Michael Pyne committed
202 203 204 205 206 207 208 209
void DefaultSequenceIterator::playlistChanged()
{
    refillRandomList();
}

void DefaultSequenceIterator::itemAboutToDie(const PlaylistItem *item)
{
    PlaylistItem *stfu_gcc = const_cast<PlaylistItem *>(item);
210
    m_randomItems.removeAll(stfu_gcc);
Michael Pyne's avatar
Michael Pyne committed
211 212
}

213 214
void DefaultSequenceIterator::setCurrent(PlaylistItem *current)
{
215
    PlaylistItem *oldCurrent = DefaultSequenceIterator::current();
216 217 218 219 220

    TrackSequenceIterator::setCurrent(current);

    bool random = action("randomPlay") && action<KToggleAction>("randomPlay")->isChecked();
    bool albumRandom = action("albumRandomPlay") && action<KToggleAction>("albumRandomPlay")->isChecked();
221

222 223 224 225 226 227 228 229
    if((albumRandom || random) && current && m_randomItems.isEmpty()) {

        // We're setting a current item, refill the random list now, and remove
        // the current item.

        refillRandomList();
    }

230
    m_randomItems.removeAll(current);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

    if(albumRandom && current && !oldCurrent) {

        // Same idea as above

        initAlbumSearch(current);
        m_albumSearch.clearItem(current);
    }
}

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

246
void DefaultSequenceIterator::refillRandomList(Playlist *p)
247
{
248 249 250
    if(!p) {
        if (!current())
            return;
251

252
        p = current()->playlist();
253

254
        if(!p) {
255
            kError() << "Item has no playlist!\n";
256 257
            return;
        }
258 259 260
    }

    m_randomItems = p->visibleItems();
261
    m_randomItems.removeAll(current());
262 263 264 265 266 267 268 269 270 271 272 273 274
    m_albumSearch.clearComponents();
    m_albumSearch.search();
}

void DefaultSequenceIterator::initAlbumSearch(PlaylistItem *searchItem)
{
    if(!searchItem)
        return;

    m_albumSearch.clearPlaylists();
    m_albumSearch.addPlaylist(searchItem->playlist());

    ColumnList columns;
275

276 277 278 279 280 281 282
    m_albumSearch.setSearchMode(PlaylistSearch::MatchAll);
    m_albumSearch.clearComponents();

    // If the album name is empty, it will mess up the search,
    // so ignore empty album names.

    if(searchItem->file().tag()->album().isEmpty())
283
        return;
284 285 286 287

    columns.append(PlaylistItem::AlbumColumn);

    m_albumSearch.addComponent(PlaylistSearch::Component(
288 289 290 291
        searchItem->file().tag()->album(),
        true,
        columns,
        PlaylistSearch::Component::Exact)
292 293
    );

294 295 296 297 298
    // If there is an Artist tag with the track, match against it as well
    // to avoid things like multiple "Greatest Hits" albums matching the
    // search.

    if(!searchItem->file().tag()->artist().isEmpty()) {
299
        kDebug() << "Searching both artist and album.";
300 301 302 303 304 305 306 307 308 309
        columns[0] = PlaylistItem::ArtistColumn;

        m_albumSearch.addComponent(PlaylistSearch::Component(
            searchItem->file().tag()->artist(),
            true,
            columns,
            PlaylistSearch::Component::Exact)
        );
    }

310 311 312
    m_albumSearch.search();
}

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