File indexing completed on 2023-05-30 11:30:53

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 "playlistitem.h"
0021 #include "playlistcollection.h"
0022 #include "collectionlist.h"
0023 #include "actioncollection.h"
0024 #include "juk_debug.h"
0025 
0026 using namespace ActionCollection;
0027 
0028 UpcomingPlaylist::UpcomingPlaylist(PlaylistCollection *collection)
0029   : Playlist(collection, true)
0030 {
0031     setName(i18n("Play Queue"));
0032     setAllowDuplicates(true);
0033     setSortingEnabled(false);
0034 }
0035 
0036 void UpcomingPlaylist::appendItems(const PlaylistItemList &itemList)
0037 {
0038     if(itemList.isEmpty())
0039         return;
0040 
0041     PlaylistItem *after = static_cast<PlaylistItem *>(topLevelItem(topLevelItemCount() - 1));
0042 
0043     for(auto *playlistItem : itemList) {
0044         after = createItem(playlistItem, after);
0045         m_playlistIndex.insert(after, playlistItem->playlist());
0046     }
0047 
0048     playlistItemsChanged();
0049     slotWeightDirty();
0050 }
0051 
0052 void UpcomingPlaylist::playNext()
0053 {
0054     auto currentPlaying = firstChild();
0055     const bool wasOurItem = currentPlaying
0056         && currentPlaying == Playlist::playingItem()
0057         && currentPlaying->playlist() == this;
0058 
0059     if(!currentPlaying) {
0060         qCWarning(JUK_LOG) << "Unexpectedly ended up looking to play from empty Play Queue??";
0061         CollectionList::instance()->slotBeginPlayback();
0062         return;
0063     }
0064 
0065     PlaylistItem *newToPlay = wasOurItem
0066         ? static_cast<PlaylistItem *>(currentPlaying->itemBelow())
0067         : currentPlaying;
0068 
0069     if(!newToPlay) {
0070         // We emptied the playlist, switch back to the playlist for the last item
0071         Playlist *source = m_playlistIndex[currentPlaying];
0072         if(source) {
0073             source->playNext();
0074         }
0075         else {
0076             CollectionList::instance()->slotBeginPlayback();
0077         }
0078     }
0079     else {
0080         beginPlayingItem(newToPlay);
0081 
0082         Playlist *source = m_playlistIndex[newToPlay];
0083         if(source) {
0084             source->synchronizePlayingItems(this, false);
0085         }
0086     }
0087 
0088     if(wasOurItem) {
0089         clearItem(currentPlaying);
0090     }
0091 }
0092 
0093 void UpcomingPlaylist::clearItem(PlaylistItem *item)
0094 {
0095     m_playlistIndex.remove(item);
0096     Playlist::clearItem(item);
0097 }
0098 
0099 void UpcomingPlaylist::addFiles(const QStringList &files, PlaylistItem *after)
0100 {
0101     CollectionList::instance()->addFiles(files, after);
0102 
0103     PlaylistItemList l;
0104     for(const auto &file : files) {
0105         const FileHandle f(file);
0106         PlaylistItem *i = CollectionList::instance()->lookup(f.absFilePath());
0107         if(i)
0108             l.append(i);
0109     }
0110 
0111     appendItems(l);
0112 }
0113 
0114 QMap< PlaylistItem::Pointer, QPointer<Playlist> > &UpcomingPlaylist::playlistIndex()
0115 {
0116     return m_playlistIndex;
0117 }
0118 
0119 QDataStream &operator<<(QDataStream &s, const UpcomingPlaylist &p)
0120 {
0121     const PlaylistItemList l = const_cast<UpcomingPlaylist *>(&p)->items();
0122 
0123     s << qint32(l.count());
0124 
0125     for(const auto &playlistItem : l) {
0126         s << playlistItem->file().absFilePath();
0127     }
0128 
0129     return s;
0130 }
0131 
0132 QDataStream &operator>>(QDataStream &s, UpcomingPlaylist &p)
0133 {
0134     QStringList upcomingFiles;
0135     QString fileName;
0136     qint32 count;
0137 
0138     s >> count;
0139 
0140     for(qint32 i = 0; i < count; ++i) {
0141         s >> fileName;
0142         if(fileName.isEmpty())
0143             throw BICStreamException();
0144 
0145         upcomingFiles << fileName;
0146     }
0147 
0148     p.addFiles(upcomingFiles);
0149 
0150     return s;
0151 }
0152 
0153 // vim: set et sw=4 tw=0 sta: