strawberry-audio-player-win.../src/covermanager/coverproviders.cpp

142 lines
3.7 KiB
C++
Raw Permalink Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* 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/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
2024-04-23 17:15:42 +02:00
#include <utility>
#include <QObject>
#include <QMutex>
2020-05-09 01:48:08 +02:00
#include <QList>
#include <QMap>
#include <QString>
2020-05-09 01:48:08 +02:00
#include <QStringList>
#include <QSettings>
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "core/settings.h"
#include "coverprovider.h"
#include "coverproviders.h"
2018-02-27 18:06:05 +01:00
2024-10-22 18:12:33 +02:00
#include "constants/coverssettings.h"
2020-05-09 01:48:08 +02:00
int CoverProviders::NextOrderId = 0;
CoverProviders::CoverProviders(QObject *parent) : QObject(parent) {}
2018-02-27 18:06:05 +01:00
2019-07-22 20:53:05 +02:00
CoverProviders::~CoverProviders() {
while (!cover_providers_.isEmpty()) {
delete cover_providers_.firstKey();
}
}
2020-05-09 01:48:08 +02:00
void CoverProviders::ReloadSettings() {
QMap<int, QString> all_providers;
2021-03-21 04:47:11 +01:00
QList<CoverProvider*> old_providers = cover_providers_.keys();
2024-04-23 17:15:42 +02:00
for (CoverProvider *provider : std::as_const(old_providers)) {
2020-05-09 01:48:08 +02:00
if (!provider->is_enabled()) continue;
all_providers.insert(provider->order(), provider->name());
}
Settings s;
2024-10-22 18:12:33 +02:00
s.beginGroup(CoversSettings::kSettingsGroup);
const QStringList providers_enabled = s.value(CoversSettings::kProviders, QStringList() << all_providers.values()).toStringList();
2020-05-09 01:48:08 +02:00
s.endGroup();
int i = 0;
2021-03-21 04:47:11 +01:00
QList<CoverProvider*> new_providers;
2020-05-09 01:48:08 +02:00
for (const QString &name : providers_enabled) {
CoverProvider *provider = ProviderByName(name);
if (provider) {
provider->set_enabled(true);
provider->set_order(++i);
2021-06-20 19:04:08 +02:00
new_providers << provider; // clazy:exclude=reserve-candidates
2020-05-09 01:48:08 +02:00
}
}
2021-03-21 04:47:11 +01:00
old_providers = cover_providers_.keys();
2024-04-23 17:15:42 +02:00
for (CoverProvider *provider : std::as_const(old_providers)) {
2021-03-21 04:47:11 +01:00
if (!new_providers.contains(provider)) {
2020-05-09 01:48:08 +02:00
provider->set_enabled(false);
provider->set_order(++i);
}
}
}
CoverProvider *CoverProviders::ProviderByName(const QString &name) const {
2024-04-23 17:15:42 +02:00
const QList<CoverProvider*> cover_providers = cover_providers_.keys();
2021-03-21 04:47:11 +01:00
for (CoverProvider *provider : cover_providers) {
2020-05-09 01:48:08 +02:00
if (provider->name() == name) return provider;
}
return nullptr;
}
2018-02-27 18:06:05 +01:00
void CoverProviders::AddProvider(CoverProvider *provider) {
{
QMutexLocker locker(&mutex_);
cover_providers_.insert(provider, provider->name());
2021-01-26 16:48:04 +01:00
QObject::connect(provider, &CoverProvider::destroyed, this, &CoverProviders::ProviderDestroyed);
2018-02-27 18:06:05 +01:00
}
2020-05-09 01:48:08 +02:00
provider->set_order(++NextOrderId);
2018-02-27 18:06:05 +01:00
qLog(Debug) << "Registered cover provider" << provider->name();
}
void CoverProviders::RemoveProvider(CoverProvider *provider) {
if (!provider) return;
// It's not safe to dereference provider at this point because it might have already been destroyed.
2018-02-27 18:06:05 +01:00
QString name;
{
QMutexLocker locker(&mutex_);
name = cover_providers_.take(provider);
}
if (name.isNull()) {
qLog(Debug) << "Tried to remove a cover provider that was not registered";
}
else {
qLog(Debug) << "Unregistered cover provider" << name;
}
}
void CoverProviders::ProviderDestroyed() {
CoverProvider *provider = static_cast<CoverProvider*>(sender());
RemoveProvider(provider);
}
int CoverProviders::NextId() { return next_id_.fetchAndAddRelaxed(1); }