File indexing completed on 2021-12-21 13:28:02

0001 /**
0002  * Copyright (C) 2002-2004, 2008 Michael Pyne <mpyne@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "upcomingplaylist.h"
0018 #include "juk-exception.h"
0019 
0020 #include <QAction>
0021 
0022 #include "playlistitem.h"
0023 #include "playlistcollection.h"
0024 #include "collectionlist.h"
0025 #include "actioncollection.h"
0026 #include "juk_debug.h"
0027 
0028 using namespace ActionCollection;
0029 
0030 UpcomingPlaylist::UpcomingPlaylist(PlaylistCollection *collection)
0031   : Playlist(collection, true)
0032 {
0033     setName(i18n("Play Queue"));
0034     setAllowDuplicates(true);
0035     setSortingEnabled(false);
0036 }
0037 
0038 void UpcomingPlaylist::appendItems(const PlaylistItemList &itemList)
0039 {
0040     if(itemList.isEmpty())
0041         return;
0042 
0043     PlaylistItem *after = static_cast<PlaylistItem *>(topLevelItem(topLevelItemCount() - 1));
0044 
0045     for(auto *playlistItem : itemList) {
0046         after = createItem(playlistItem, after);
0047         m_playlistIndex.insert(after, playlistItem->playlist());
0048     }
0049 
0050     playlistItemsChanged();
0051     slotWeightDirty();
0052 }
0053 
0054 void UpcomingPlaylist::playNext()
0055 {
0056     auto currentPlaying = firstChild();
0057     const bool wasOurItem = currentPlaying
0058         && currentPlaying == Playlist::playingItem()
0059         && currentPlaying->playlist() == this;
0060 
0061     if(!currentPlaying) {
0062         qCWarning(JUK_LOG) << "Unexpectedly ended up looking to play from empty Play Queue??";
0063         CollectionList::instance()->slotBeginPlayback();
0064         return;
0065     }
0066 
0067     PlaylistItem *newToPlay = wasOurItem
0068         ? static_cast<PlaylistItem *>(currentPlaying->itemBelow())
0069         : currentPlaying;
0070 
0071     if(!newToPlay) {
0072         // We emptied the playlist, switch back to the playlist for the last item
0073         Playlist *source = m_playlistIndex[currentPlaying];
0074         if(source) {
0075             source->playNext();
0076         }
0077         else {
0078             CollectionList::instance()->slotBeginPlayback();
0079         }
0080     }
0081     else {
0082         beginPlayingItem(newToPlay);
0083 
0084         Playlist *source = m_playlistIndex[newToPlay];
0085         if(source) {
0086             source->synchronizePlayingItems(this, false);
0087         }
0088     }
0089 
0090     if(wasOurItem) {
0091         clearItem(currentPlaying);
0092     }
0093 }
0094 
0095 void UpcomingPlaylist::clearItem(PlaylistItem *item)
0096 {
0097     m_playlistIndex.remove(item);
0098     Playlist::clearItem(item);
0099 }
0100 
0101 void UpcomingPlaylist::addFiles(const QStringList &files, PlaylistItem *after)
0102 {
0103     CollectionList::instance()->addFiles(files, after);
0104 
0105     PlaylistItemList l;
0106     for(const auto &file : files) {
0107         const FileHandle f(file);
0108         PlaylistItem *i = CollectionList::instance()->lookup(f.absFilePath());
0109         if(i)
0110             l.append(i);
0111     }
0112 
0113     appendItems(l);
0114 }
0115 
0116 QMap< PlaylistItem::Pointer, QPointer<Playlist> > &UpcomingPlaylist::playlistIndex()
0117 {
0118     return m_playlistIndex;
0119 }
0120 
0121 QDataStream &operator<<(QDataStream &s, const UpcomingPlaylist &p)
0122 {
0123     const PlaylistItemList l = const_cast<UpcomingPlaylist *>(&p)->items();
0124 
0125     s << qint32(l.count());
0126 
0127     for(const auto &playlistItem : l) {
0128         s << playlistItem->file().absFilePath();
0129     }
0130 
0131     return s;
0132 }
0133 
0134 QDataStream &operator>>(QDataStream &s, UpcomingPlaylist &p)
0135 {
0136     QString fileName;
0137     PlaylistItem *newItem = 0;
0138     qint32 count;
0139 
0140     s >> count;
0141 
0142     for(qint32 i = 0; i < count; ++i) {
0143         s >> fileName;
0144         if(fileName.isEmpty())
0145             throw BICStreamException();
0146 
0147         newItem = p.createItem(FileHandle(fileName), newItem);
0148     }
0149 
0150     return s;
0151 }
0152 
0153 // vim: set et sw=4 tw=0 sta: