File: upcomingplaylist.cpp

package info (click to toggle)
juk 4%3A25.08.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,236 kB
  • sloc: cpp: 14,818; xml: 763; makefile: 5; sh: 2
file content (153 lines) | stat: -rw-r--r-- 4,027 bytes parent folder | download | duplicates (2)
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
/**
 * 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/>.
 */

#include "upcomingplaylist.h"
#include "juk-exception.h"

#include "playlistitem.h"
#include "playlistcollection.h"
#include "collectionlist.h"
#include "actioncollection.h"
#include "juk_debug.h"

using namespace ActionCollection;

UpcomingPlaylist::UpcomingPlaylist(PlaylistCollection *collection)
  : Playlist(collection, true)
{
    setName(i18n("Play Queue"));
    setAllowDuplicates(true);
    setSortingEnabled(false);
}

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

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

    for(auto *playlistItem : itemList) {
        after = createItem(playlistItem, after);
        m_playlistIndex.insert(after, playlistItem->playlist());
    }

    playlistItemsChanged();
    slotWeightDirty();
}

void UpcomingPlaylist::playNext()
{
    auto currentPlaying = firstChild();
    const bool wasOurItem = currentPlaying
        && currentPlaying == Playlist::playingItem()
        && currentPlaying->playlist() == this;

    if(!currentPlaying) {
        qCWarning(JUK_LOG) << "Unexpectedly ended up looking to play from empty Play Queue??";
        CollectionList::instance()->slotBeginPlayback();
        return;
    }

    PlaylistItem *newToPlay = wasOurItem
        ? static_cast<PlaylistItem *>(currentPlaying->itemBelow())
        : currentPlaying;

    if(!newToPlay) {
        // We emptied the playlist, switch back to the playlist for the last item
        Playlist *source = m_playlistIndex[currentPlaying];
        if(source) {
            source->playNext();
        }
        else {
            CollectionList::instance()->slotBeginPlayback();
        }
    }
    else {
        beginPlayingItem(newToPlay);

        Playlist *source = m_playlistIndex[newToPlay];
        if(source) {
            source->synchronizePlayingItems(this, false);
        }
    }

    if(wasOurItem) {
        clearItem(currentPlaying);
    }
}

void UpcomingPlaylist::clearItem(PlaylistItem *item)
{
    m_playlistIndex.remove(item);
    Playlist::clearItem(item);
}

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

    PlaylistItemList l;
    for(const auto &file : files) {
        const FileHandle f(file);
        PlaylistItem *i = CollectionList::instance()->lookup(f.absFilePath());
        if(i)
            l.append(i);
    }

    appendItems(l);
}

QMap< PlaylistItem::Pointer, QPointer<Playlist> > &UpcomingPlaylist::playlistIndex()
{
    return m_playlistIndex;
}

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

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

    for(const auto &playlistItem : l) {
        s << playlistItem->file().absFilePath();
    }

    return s;
}

QDataStream &operator>>(QDataStream &s, UpcomingPlaylist &p)
{
    QStringList upcomingFiles;
    QString fileName;
    qint32 count;

    s >> count;

    for(qint32 i = 0; i < count; ++i) {
        s >> fileName;
        if(fileName.isEmpty())
            throw BICStreamException();

        upcomingFiles << fileName;
    }

    p.addFiles(upcomingFiles);

    return s;
}

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