Application: Use shared pointers

Fixes #1239
This commit is contained in:
Jonas Kvinge
2023-07-21 05:55:24 +02:00
parent d6b53f78ab
commit 2e61235403
316 changed files with 2170 additions and 1643 deletions

View File

@@ -28,16 +28,18 @@
#include "playlistgenerator.h"
#include "playlistquerygenerator.h"
using std::make_shared;
const int PlaylistGenerator::kDefaultLimit = 20;
const int PlaylistGenerator::kDefaultDynamicHistory = 5;
const int PlaylistGenerator::kDefaultDynamicFuture = 15;
PlaylistGenerator::PlaylistGenerator(QObject *parent) : QObject(parent), backend_(nullptr) {}
PlaylistGenerator::PlaylistGenerator(QObject *parent) : QObject(parent), collection_backend_(nullptr) {}
PlaylistGeneratorPtr PlaylistGenerator::Create(const Type type) {
Q_UNUSED(type)
return std::make_shared<PlaylistQueryGenerator>();
return make_shared<PlaylistQueryGenerator>();
}

View File

@@ -29,11 +29,14 @@
#include <QByteArray>
#include <QString>
#include "core/shared_ptr.h"
#include "playlist/playlistitem.h"
class CollectionBackend;
class PlaylistGenerator : public QObject, public std::enable_shared_from_this<PlaylistGenerator> {
using std::enable_shared_from_this;
class PlaylistGenerator : public QObject, public enable_shared_from_this<PlaylistGenerator> {
Q_OBJECT
public:
@@ -49,12 +52,12 @@ class PlaylistGenerator : public QObject, public std::enable_shared_from_this<Pl
};
// Creates a new PlaylistGenerator of the given type
static std::shared_ptr<PlaylistGenerator> Create(const Type type = Type::Query);
static SharedPtr<PlaylistGenerator> Create(const Type type = Type::Query);
// Should be called before Load on a new PlaylistGenerator
void set_collection(CollectionBackend *backend) { backend_ = backend; }
void set_collection_backend(SharedPtr<CollectionBackend> collection_backend) { collection_backend_ = collection_backend; }
void set_name(const QString &name) { name_ = name; }
CollectionBackend *collection() const { return backend_; }
SharedPtr<CollectionBackend> collection() const { return collection_backend_; }
QString name() const { return name_; }
// Name of the subclass
@@ -88,7 +91,7 @@ class PlaylistGenerator : public QObject, public std::enable_shared_from_this<Pl
void Error(const QString &message);
protected:
CollectionBackend *backend_;
SharedPtr<CollectionBackend> collection_backend_;
private:
QString name_;

View File

@@ -23,10 +23,10 @@
#include "config.h"
#include <memory>
#include "core/shared_ptr.h"
class PlaylistGenerator;
using PlaylistGeneratorPtr = std::shared_ptr<PlaylistGenerator>;
using PlaylistGeneratorPtr = SharedPtr<PlaylistGenerator>;
#endif // PLAYLISTGENERATOR_FWD_H

View File

@@ -26,6 +26,7 @@
#include <QFuture>
#include <QFutureWatcher>
#include "core/shared_ptr.h"
#include "core/taskmanager.h"
#include "playlist/playlist.h"
@@ -34,10 +35,10 @@
class CollectionBackend;
PlaylistGeneratorInserter::PlaylistGeneratorInserter(TaskManager *task_manager, CollectionBackend *collection, QObject *parent)
PlaylistGeneratorInserter::PlaylistGeneratorInserter(SharedPtr<TaskManager> task_manager, SharedPtr<CollectionBackend> collection_backend, QObject *parent)
: QObject(parent),
task_manager_(task_manager),
collection_(collection),
collection_backend_(collection_backend),
task_id_(-1),
destination_(nullptr),
row_(0),
@@ -68,7 +69,7 @@ void PlaylistGeneratorInserter::Load(Playlist *destination, const int row, const
enqueue_next_ = enqueue_next;
is_dynamic_ = generator->is_dynamic();
QObject::connect(generator.get(), &PlaylistGenerator::Error, this, &PlaylistGeneratorInserter::Error);
QObject::connect(&*generator, &PlaylistGenerator::Error, this, &PlaylistGeneratorInserter::Error);
QFuture<PlaylistItemPtrList> future = QtConcurrent::run(PlaylistGeneratorInserter::Generate, generator, dynamic_count);
QFutureWatcher<PlaylistItemPtrList> *watcher = new QFutureWatcher<PlaylistItemPtrList>();

View File

@@ -26,6 +26,8 @@
#include <QObject>
#include <QString>
#include "core/shared_ptr.h"
#include "playlist/playlist.h"
#include "playlist/playlistitem.h"
@@ -39,7 +41,7 @@ class PlaylistGeneratorInserter : public QObject {
Q_OBJECT
public:
explicit PlaylistGeneratorInserter(TaskManager *task_manager, CollectionBackend *collection, QObject *parent);
explicit PlaylistGeneratorInserter(SharedPtr<TaskManager> task_manager, SharedPtr<CollectionBackend> collection_backend, QObject *parent);
void Load(Playlist *destination, const int row, const bool play_now, const bool enqueue, const bool enqueue_next, PlaylistGeneratorPtr generator, const int dynamic_count = 0);
@@ -54,8 +56,8 @@ class PlaylistGeneratorInserter : public QObject {
void Finished();
private:
TaskManager *task_manager_;
CollectionBackend *collection_;
SharedPtr<TaskManager> task_manager_;
SharedPtr<CollectionBackend> collection_backend_;
int task_id_;
Playlist *destination_;

View File

@@ -88,7 +88,7 @@ PlaylistItemPtrList PlaylistQueryGenerator::GenerateMore(const int count) {
current_pos_ += search_copy.limit_;
}
SongList songs = backend_->SmartPlaylistsFindSongs(search_copy);
SongList songs = collection_backend_->SmartPlaylistsFindSongs(search_copy);
PlaylistItemPtrList items;
items.reserve(songs.count());
for (const Song &song : songs) {

View File

@@ -20,8 +20,8 @@
#include "config.h"
#include <memory>
#include <algorithm>
#include <memory>
#include <QWizardPage>
#include <QList>
@@ -29,12 +29,17 @@
#include <QVBoxLayout>
#include <QScrollBar>
#include "core/scoped_ptr.h"
#include "core/shared_ptr.h"
#include "playlistquerygenerator.h"
#include "smartplaylistquerywizardplugin.h"
#include "smartplaylistsearchtermwidget.h"
#include "ui_smartplaylistquerysearchpage.h"
#include "ui_smartplaylistquerysortpage.h"
using std::make_unique;
using std::make_shared;
class SmartPlaylistQueryWizardPlugin::SearchPage : public QWizardPage { // clazy:exclude=missing-qobject-macro
friend class SmartPlaylistQueryWizardPlugin;
@@ -64,7 +69,7 @@ class SmartPlaylistQueryWizardPlugin::SearchPage : public QWizardPage { // claz
SmartPlaylistSearchPreview *preview_;
std::unique_ptr<Ui_SmartPlaylistQuerySearchPage> ui_;
ScopedPtr<Ui_SmartPlaylistQuerySearchPage> ui_;
};
class SmartPlaylistQueryWizardPlugin::SortPage : public QWizardPage { // clazy:exclude=missing-qobject-macro
@@ -80,7 +85,7 @@ class SmartPlaylistQueryWizardPlugin::SortPage : public QWizardPage { // clazy:
SmartPlaylistQueryWizardPlugin *plugin_;
};
SmartPlaylistQueryWizardPlugin::SmartPlaylistQueryWizardPlugin(Application *app, CollectionBackend *collection_backend, QObject *parent)
SmartPlaylistQueryWizardPlugin::SmartPlaylistQueryWizardPlugin(Application *app, SharedPtr<CollectionBackend> collection_backend, QObject *parent)
: SmartPlaylistWizardPlugin(app, collection_backend, parent),
search_page_(nullptr),
previous_scrollarea_max_(0) {}
@@ -99,7 +104,7 @@ int SmartPlaylistQueryWizardPlugin::CreatePages(QWizard *wizard, int finish_page
search_page_ = new SearchPage(wizard);
QWizardPage *sort_page = new SortPage(this, wizard, finish_page_id);
sort_ui_ = std::make_unique<Ui_SmartPlaylistQuerySortPage>();
sort_ui_ = make_unique<Ui_SmartPlaylistQuerySortPage>();
sort_ui_->setupUi(sort_page);
sort_ui_->limit_value->setValue(PlaylistGenerator::kDefaultLimit);
@@ -167,7 +172,7 @@ int SmartPlaylistQueryWizardPlugin::CreatePages(QWizard *wizard, int finish_page
void SmartPlaylistQueryWizardPlugin::SetGenerator(PlaylistGeneratorPtr g) {
std::shared_ptr<PlaylistQueryGenerator> gen = std::dynamic_pointer_cast<PlaylistQueryGenerator>(g);
SharedPtr<PlaylistQueryGenerator> gen = std::dynamic_pointer_cast<PlaylistQueryGenerator>(g);
if (!gen) return;
SmartPlaylistSearch search = gen->search();
@@ -206,7 +211,7 @@ void SmartPlaylistQueryWizardPlugin::SetGenerator(PlaylistGeneratorPtr g) {
PlaylistGeneratorPtr SmartPlaylistQueryWizardPlugin::CreateGenerator() const {
std::shared_ptr<PlaylistQueryGenerator> gen = std::make_shared<PlaylistQueryGenerator>();
SharedPtr<PlaylistQueryGenerator> gen = make_shared<PlaylistQueryGenerator>();
gen->Load(MakeSearch());
return std::static_pointer_cast<PlaylistGenerator>(gen);

View File

@@ -23,11 +23,11 @@
#include "config.h"
#include <memory>
#include <QObject>
#include <QString>
#include "core/scoped_ptr.h"
#include "core/shared_ptr.h"
#include "smartplaylistwizardplugin.h"
#include "smartplaylistsearch.h"
@@ -41,7 +41,7 @@ class SmartPlaylistQueryWizardPlugin : public SmartPlaylistWizardPlugin {
Q_OBJECT
public:
explicit SmartPlaylistQueryWizardPlugin(Application *app, CollectionBackend *collection_backend, QObject *parent);
explicit SmartPlaylistQueryWizardPlugin(Application *app, SharedPtr<CollectionBackend> collection_backend, QObject *parent);
~SmartPlaylistQueryWizardPlugin() override;
PlaylistGenerator::Type type() const override { return PlaylistGenerator::Type::Query; }
@@ -71,7 +71,7 @@ class SmartPlaylistQueryWizardPlugin : public SmartPlaylistWizardPlugin {
SmartPlaylistSearch MakeSearch() const;
std::unique_ptr<Ui_SmartPlaylistQuerySortPage> sort_ui_;
ScopedPtr<Ui_SmartPlaylistQuerySortPage> sort_ui_;
SearchPage *search_page_;
int previous_scrollarea_max_;

View File

@@ -29,16 +29,20 @@
#include <QFuture>
#include <QFutureWatcher>
#include "core/shared_ptr.h"
#include "smartplaylistsearchpreview.h"
#include "ui_smartplaylistsearchpreview.h"
#include "playlist/playlist.h"
#include "playlistquerygenerator.h"
using std::make_shared;
SmartPlaylistSearchPreview::SmartPlaylistSearchPreview(QWidget *parent)
: QWidget(parent),
ui_(new Ui_SmartPlaylistSearchPreview),
backend_(nullptr),
collection_backend_(nullptr),
model_(nullptr) {
ui_->setupUi(this);
@@ -64,11 +68,11 @@ void SmartPlaylistSearchPreview::set_application(Application *app) {
}
void SmartPlaylistSearchPreview::set_collection(CollectionBackend *backend) {
void SmartPlaylistSearchPreview::set_collection(SharedPtr<CollectionBackend> collection_backend) {
backend_ = backend;
collection_backend_ = collection_backend;
model_ = new Playlist(nullptr, nullptr, backend_, -1, QString(), false, this);
model_ = new Playlist(nullptr, nullptr, collection_backend_, -1, QString(), false, this);
ui_->tree->setModel(model_);
ui_->tree->SetPlaylist(model_);
ui_->tree->SetItemDelegates();
@@ -110,8 +114,8 @@ PlaylistItemPtrList DoRunSearch(PlaylistGeneratorPtr gen) { return gen->Generate
void SmartPlaylistSearchPreview::RunSearch(const SmartPlaylistSearch &search) {
generator_ = std::make_shared<PlaylistQueryGenerator>();
generator_->set_collection(backend_);
generator_ = make_shared<PlaylistQueryGenerator>();
generator_->set_collection_backend(collection_backend_);
std::dynamic_pointer_cast<PlaylistQueryGenerator>(generator_)->Load(search);
ui_->busy_container->show();

View File

@@ -26,6 +26,8 @@
#include <QWidget>
#include <QList>
#include "core/shared_ptr.h"
#include "smartplaylistsearch.h"
#include "playlistgenerator_fwd.h"
@@ -44,7 +46,7 @@ class SmartPlaylistSearchPreview : public QWidget {
~SmartPlaylistSearchPreview() override;
void set_application(Application *app);
void set_collection(CollectionBackend *backend);
void set_collection(SharedPtr<CollectionBackend> backend);
void Update(const SmartPlaylistSearch &search);
@@ -61,7 +63,7 @@ class SmartPlaylistSearchPreview : public QWidget {
Ui_SmartPlaylistSearchPreview *ui_;
QList<SmartPlaylistSearchTerm::Field> fields_;
CollectionBackend *backend_;
SharedPtr<CollectionBackend> collection_backend_;
Playlist *model_;
SmartPlaylistSearch pending_search_;

View File

@@ -34,6 +34,7 @@
#include <QKeyEvent>
#include <QEnterEvent>
#include "core/shared_ptr.h"
#include "core/iconloader.h"
#include "utilities/colorutils.h"
#include "playlist/playlist.h"
@@ -73,7 +74,7 @@ class SmartPlaylistSearchTermWidget::Overlay : public QWidget { // clazy:exclud
const int SmartPlaylistSearchTermWidget::Overlay::kSpacing = 6;
const int SmartPlaylistSearchTermWidget::Overlay::kIconSize = 22;
SmartPlaylistSearchTermWidget::SmartPlaylistSearchTermWidget(CollectionBackend *collection_backend, QWidget *parent)
SmartPlaylistSearchTermWidget::SmartPlaylistSearchTermWidget(SharedPtr<CollectionBackend> collection_backend, QWidget *parent)
: QWidget(parent),
ui_(new Ui_SmartPlaylistSearchTermWidget),
collection_backend_(collection_backend),

View File

@@ -26,6 +26,7 @@
#include <QWidget>
#include <QPushButton>
#include "core/shared_ptr.h"
#include "smartplaylistsearchterm.h"
class QPropertyAnimation;
@@ -43,7 +44,7 @@ class SmartPlaylistSearchTermWidget : public QWidget {
Q_PROPERTY(float overlay_opacity READ overlay_opacity WRITE set_overlay_opacity)
public:
explicit SmartPlaylistSearchTermWidget(CollectionBackend *collection_backend, QWidget *parent);
explicit SmartPlaylistSearchTermWidget(SharedPtr<CollectionBackend> collection_backend, QWidget *parent);
~SmartPlaylistSearchTermWidget() override;
void SetActive(const bool active);
@@ -81,7 +82,7 @@ class SmartPlaylistSearchTermWidget : public QWidget {
friend class Overlay;
Ui_SmartPlaylistSearchTermWidget *ui_;
CollectionBackend *collection_backend_;
SharedPtr<CollectionBackend> collection_backend_;
Overlay *overlay_;
QPropertyAnimation *animation_;

View File

@@ -28,6 +28,7 @@
#include <QSettings>
#include "core/logging.h"
#include "core/shared_ptr.h"
#include "core/application.h"
#include "core/iconloader.h"
#include "core/simpletreemodel.h"
@@ -44,9 +45,9 @@ const char *SmartPlaylistsModel::kSettingsGroup = "SerializedSmartPlaylists";
const char *SmartPlaylistsModel::kSmartPlaylistsMimeType = "application/x-strawberry-smart-playlist-generator";
const int SmartPlaylistsModel::kSmartPlaylistsVersion = 1;
SmartPlaylistsModel::SmartPlaylistsModel(CollectionBackend *backend, QObject *parent)
SmartPlaylistsModel::SmartPlaylistsModel(SharedPtr<CollectionBackend> collection_backend, QObject *parent)
: SimpleTreeModel<SmartPlaylistsItem>(new SmartPlaylistsItem(this), parent),
backend_(backend),
collection_backend_(collection_backend),
icon_(IconLoader::Load("view-media-playlist")) {
root_->lazy_loaded = true;
@@ -117,7 +118,7 @@ void SmartPlaylistsModel::Init() {
QSettings s;
s.beginGroup(kSettingsGroup);
int version = s.value(backend_->songs_table() + "_version", 0).toInt();
int version = s.value(collection_backend_->songs_table() + "_version", 0).toInt();
// How many defaults do we have to write?
int unwritten_defaults = 0;
@@ -128,11 +129,11 @@ void SmartPlaylistsModel::Init() {
// Save the defaults if there are any unwritten ones
if (unwritten_defaults > 0) {
// How many items are stored already?
int playlist_index = s.beginReadArray(backend_->songs_table());
int playlist_index = s.beginReadArray(collection_backend_->songs_table());
s.endArray();
// Append the new ones
s.beginWriteArray(backend_->songs_table(), playlist_index + unwritten_defaults);
s.beginWriteArray(collection_backend_->songs_table(), playlist_index + unwritten_defaults);
for (; version < default_smart_playlists_.count(); ++version) {
for (PlaylistGeneratorPtr gen : default_smart_playlists_[version]) { // clazy:exclude=range-loop-reference
SaveGenerator(&s, playlist_index++, gen);
@@ -141,9 +142,9 @@ void SmartPlaylistsModel::Init() {
s.endArray();
}
s.setValue(backend_->songs_table() + "_version", version);
s.setValue(collection_backend_->songs_table() + "_version", version);
const int count = s.beginReadArray(backend_->songs_table());
const int count = s.beginReadArray(collection_backend_->songs_table());
for (int i = 0; i < count; ++i) {
s.setArrayIndex(i);
ItemFromSmartPlaylist(s, false);
@@ -172,11 +173,11 @@ void SmartPlaylistsModel::AddGenerator(PlaylistGeneratorPtr gen) {
s.beginGroup(kSettingsGroup);
// Count the existing items
const int count = s.beginReadArray(backend_->songs_table());
const int count = s.beginReadArray(collection_backend_->songs_table());
s.endArray();
// Add this one to the end
s.beginWriteArray(backend_->songs_table(), count + 1);
s.beginWriteArray(collection_backend_->songs_table(), count + 1);
SaveGenerator(&s, count, gen);
// Add it to the model
@@ -198,10 +199,10 @@ void SmartPlaylistsModel::UpdateGenerator(const QModelIndex &idx, PlaylistGenera
s.beginGroup(kSettingsGroup);
// Count the existing items
const int count = s.beginReadArray(backend_->songs_table());
const int count = s.beginReadArray(collection_backend_->songs_table());
s.endArray();
s.beginWriteArray(backend_->songs_table(), count);
s.beginWriteArray(collection_backend_->songs_table(), count);
SaveGenerator(&s, idx.row(), gen);
s.endArray();
@@ -227,7 +228,7 @@ void SmartPlaylistsModel::DeleteGenerator(const QModelIndex &idx) {
s.beginGroup(kSettingsGroup);
// Rewrite all the items to the settings
s.beginWriteArray(backend_->songs_table(), static_cast<int>(root_->children.count()));
s.beginWriteArray(collection_backend_->songs_table(), static_cast<int>(root_->children.count()));
int i = 0;
for (SmartPlaylistsItem *item : root_->children) {
s.setArrayIndex(i++);
@@ -260,7 +261,7 @@ PlaylistGeneratorPtr SmartPlaylistsModel::CreateGenerator(const QModelIndex &idx
if (!ret) return ret;
ret->set_name(item->display_text);
ret->set_collection(backend_);
ret->set_collection_backend(collection_backend_);
ret->Load(item->smart_playlist_data);
return ret;

View File

@@ -31,6 +31,7 @@
#include <QSettings>
#include <QIcon>
#include "core/shared_ptr.h"
#include "core/simpletreemodel.h"
#include "smartplaylistsitem.h"
#include "playlistgenerator_fwd.h"
@@ -45,7 +46,7 @@ class SmartPlaylistsModel : public SimpleTreeModel<SmartPlaylistsItem> {
Q_OBJECT
public:
explicit SmartPlaylistsModel(CollectionBackend *backend, QObject *parent = nullptr);
explicit SmartPlaylistsModel(SharedPtr<CollectionBackend> backend, QObject *parent = nullptr);
~SmartPlaylistsModel();
void Init();
@@ -80,11 +81,10 @@ class SmartPlaylistsModel : public SimpleTreeModel<SmartPlaylistsItem> {
void ItemFromSmartPlaylist(const QSettings &s, const bool notify);
private:
CollectionBackend *backend_;
SharedPtr<CollectionBackend> collection_backend_;
QIcon icon_;
DefaultGenerators default_smart_playlists_;
QList<SmartPlaylistsItem*> items_;
};
#endif // SMARTPLAYLISTSMODEL_H

View File

@@ -27,6 +27,7 @@
#include <QVBoxLayout>
#include "core/logging.h"
#include "core/shared_ptr.h"
#include "core/iconloader.h"
#include "smartplaylistquerywizardplugin.h"
@@ -58,7 +59,7 @@ class SmartPlaylistWizard::FinishPage : public QWizardPage { // clazy:exclude=m
};
SmartPlaylistWizard::SmartPlaylistWizard(Application *app, CollectionBackend *collection_backend, QWidget *parent)
SmartPlaylistWizard::SmartPlaylistWizard(Application *app, SharedPtr<CollectionBackend> collection_backend, QWidget *parent)
: QWizard(parent),
app_(app),
collection_backend_(collection_backend),

View File

@@ -25,6 +25,8 @@
#include <QWizard>
#include "core/shared_ptr.h"
#include "playlistgenerator_fwd.h"
class Application;
@@ -35,7 +37,7 @@ class SmartPlaylistWizard : public QWizard {
Q_OBJECT
public:
explicit SmartPlaylistWizard(Application *app, CollectionBackend *collection_backend, QWidget *parent);
explicit SmartPlaylistWizard(Application *app, SharedPtr<CollectionBackend> collection_backend, QWidget *parent);
~SmartPlaylistWizard() override;
void SetGenerator(PlaylistGeneratorPtr gen);
@@ -55,14 +57,13 @@ class SmartPlaylistWizard : public QWizard {
private:
Application *app_;
CollectionBackend *collection_backend_;
SharedPtr<CollectionBackend> collection_backend_;
TypePage *type_page_;
FinishPage *finish_page_;
int finish_id_;
int type_index_;
QList<SmartPlaylistWizardPlugin*> plugins_;
};
#endif // SMARTPLAYLISTWIZARD_H

View File

@@ -23,9 +23,10 @@
#include <QObject>
#include <QWizard>
#include "core/shared_ptr.h"
#include "smartplaylistwizardplugin.h"
SmartPlaylistWizardPlugin::SmartPlaylistWizardPlugin(Application *app, CollectionBackend *collection_backend, QObject *parent)
SmartPlaylistWizardPlugin::SmartPlaylistWizardPlugin(Application *app, SharedPtr<CollectionBackend> collection_backend, QObject *parent)
: QObject(parent),
app_(app),
collection_backend_(collection_backend),

View File

@@ -23,6 +23,7 @@
#include <QObject>
#include "core/shared_ptr.h"
#include "playlistgenerator.h"
class QWizard;
@@ -34,7 +35,7 @@ class SmartPlaylistWizardPlugin : public QObject {
Q_OBJECT
public:
explicit SmartPlaylistWizardPlugin(Application *app, CollectionBackend *collection_backend, QObject *parent);
explicit SmartPlaylistWizardPlugin(Application *app, SharedPtr<CollectionBackend> collection_backend, QObject *parent);
virtual PlaylistGenerator::Type type() const = 0;
virtual QString name() const = 0;
@@ -51,7 +52,7 @@ class SmartPlaylistWizardPlugin : public QObject {
virtual int CreatePages(QWizard *wizard, const int finish_page_id) = 0;
Application *app_;
CollectionBackend *collection_backend_;
SharedPtr<CollectionBackend> collection_backend_;
private:
int start_page_;