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

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