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

0001 /**
0002  * Copyright (C) 2003-2004 Scott Wheeler <wheeler@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 "searchplaylist.h"
0018 #include "juk-exception.h"
0019 
0020 #include <QHash>
0021 
0022 #include "playlistitem.h"
0023 #include "collectionlist.h"
0024 #include "juk_debug.h"
0025 
0026 ////////////////////////////////////////////////////////////////////////////////
0027 // public methods
0028 ////////////////////////////////////////////////////////////////////////////////
0029 
0030 SearchPlaylist::SearchPlaylist(PlaylistCollection *collection,
0031                                PlaylistSearch& search,
0032                                const QString &name,
0033                                bool setupPlaylist,
0034                                bool synchronizePlaying) :
0035     DynamicPlaylist(search.playlists(), collection, name, "edit-find",
0036                     setupPlaylist, synchronizePlaying),
0037     m_search(&search)
0038 {
0039 
0040 }
0041 
0042 SearchPlaylist::~SearchPlaylist()
0043 {
0044     // DynamicPlaylist needs us to call this while the virtual call still works
0045     updateItems();
0046 }
0047 
0048 void SearchPlaylist::setPlaylistSearch(PlaylistSearch* s, bool update)
0049 {
0050     m_search = s;
0051     if(update)
0052         setPlaylists(s->playlists());
0053 }
0054 
0055 ////////////////////////////////////////////////////////////////////////////////
0056 // protected methods
0057 ////////////////////////////////////////////////////////////////////////////////
0058 
0059 void SearchPlaylist::updateItems()
0060 {
0061     // Here we don't simply use "clear" since that would involve a call to
0062     // items() which would in turn call this method...
0063 
0064     PlaylistItemList items;
0065     const auto matchingItems = m_search->matchedItems();
0066     for(const QModelIndex &index : matchingItems)
0067         items.push_back(static_cast<PlaylistItem*>(itemFromIndex(index)));
0068     synchronizeItemsTo(items);
0069 
0070     if(synchronizePlaying()) {
0071         qCDebug(JUK_LOG) << "synchronizing playing";
0072         synchronizePlayingItems(m_search->playlists(), true);
0073     }
0074 }
0075 
0076 
0077 ////////////////////////////////////////////////////////////////////////////////
0078 // helper functions
0079 ////////////////////////////////////////////////////////////////////////////////
0080 
0081 QDataStream &operator<<(QDataStream &s, const SearchPlaylist &p)
0082 {
0083     s << p.name()
0084       << p.playlistSearch();
0085 
0086     return s;
0087 }
0088 
0089 QDataStream &operator>>(QDataStream &s, SearchPlaylist &p)
0090 {
0091     QString name;
0092     auto search = new PlaylistSearch(&p);
0093 
0094     s >> name
0095       >> *search;
0096 
0097     if(name.isEmpty())
0098         throw BICStreamException();
0099 
0100     p.setName(name);
0101     p.setPlaylistSearch(search, false);
0102 
0103     return s;
0104 }
0105 
0106 // vim: set et sw=4 tw=0 sta: