strawberry-audio-player-win.../src/smartplaylists/playlistquerygenerator.cpp

106 lines
2.6 KiB
C++
Raw Normal View History

/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <QIODevice>
#include <QDataStream>
#include <QByteArray>
#include <QString>
#include "playlistquerygenerator.h"
#include "collection/collectionbackend.h"
2021-06-20 19:04:08 +02:00
PlaylistQueryGenerator::PlaylistQueryGenerator(QObject *parent) : PlaylistGenerator(parent), dynamic_(false), current_pos_(0) {}
2021-06-20 19:04:08 +02:00
PlaylistQueryGenerator::PlaylistQueryGenerator(const QString &name, const SmartPlaylistSearch &search, const bool dynamic, QObject *parent)
2021-07-11 07:40:57 +02:00
: PlaylistGenerator(parent),
search_(search),
dynamic_(dynamic),
current_pos_(0) {
set_name(name);
}
void PlaylistQueryGenerator::Load(const SmartPlaylistSearch &search) {
search_ = search;
dynamic_ = false;
current_pos_ = 0;
}
void PlaylistQueryGenerator::Load(const QByteArray &data) {
QDataStream s(data);
s >> search_;
s >> dynamic_;
}
QByteArray PlaylistQueryGenerator::Save() const {
QByteArray ret;
QDataStream s(&ret, QIODevice::WriteOnly);
s << search_;
s << dynamic_;
return ret;
}
2023-02-18 14:09:27 +01:00
PlaylistItemPtrList PlaylistQueryGenerator::Generate() {
previous_ids_.clear();
current_pos_ = 0;
return GenerateMore(0);
}
2023-02-18 14:09:27 +01:00
PlaylistItemPtrList PlaylistQueryGenerator::GenerateMore(const int count) {
SmartPlaylistSearch search_copy = search_;
search_copy.id_not_in_ = previous_ids_;
2021-06-22 13:54:58 +02:00
if (count > 0) {
search_copy.limit_ = count;
}
2023-02-18 14:09:27 +01:00
if (search_copy.sort_type_ != SmartPlaylistSearch::SortType::Random) {
search_copy.first_item_ = current_pos_;
current_pos_ += search_copy.limit_;
}
2024-04-23 17:15:42 +02:00
const SongList songs = collection_backend_->SmartPlaylistsFindSongs(search_copy);
2023-02-18 14:09:27 +01:00
PlaylistItemPtrList items;
2021-06-20 19:04:08 +02:00
items.reserve(songs.count());
for (const Song &song : songs) {
2022-06-13 00:23:42 +02:00
items << PlaylistItem::NewFromSong(song);
previous_ids_ << song.id();
2021-08-23 21:21:08 +02:00
if (previous_ids_.count() > GetDynamicFuture() + GetDynamicHistory()) {
previous_ids_.removeFirst();
2021-08-23 21:21:08 +02:00
}
}
return items;
}