New context with albums and lyrics +++ much more

* Added new lyrics provider with lyrics from AudD and API Seeds
* New improved context widget with albums and lyrics
* Fixed playing and context widget getting stuck in play mode when there was an error
* Changed icons for artists in collection, tidal and cover manager
* Removed "search" icon from "Search automatically" checkbox (right click) that looked ugly
* Removed some unused widgets from the src/widgets directory
* Fixed initial size of window and side panel
* Fixed saving window size correctly
This commit is contained in:
Jonas Kvinge
2018-08-29 21:42:24 +02:00
parent 3b30e66e87
commit ac6cac8da1
96 changed files with 4361 additions and 3135 deletions

View File

@@ -0,0 +1,183 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QtGlobal>
#include <QObject>
#include <QByteArray>
#include <QList>
#include <QPair>
#include <QMap>
#include <QSet>
#include <QVariant>
#include <QString>
#include <QStringBuilder>
#include <QStringList>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include "core/closure.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/utilities.h"
#include "lyricsprovider.h"
#include "lyricsfetcher.h"
#include "apiseedslyricsprovider.h"
const char *APISeedsLyricsProvider::kUrlSearch = "https://orion.apiseeds.com/api/music/lyric";
const char *APISeedsLyricsProvider::kAPIKeyB64 = "REdWenJhR245Qm03cnE5NlhoS1pTd0V5UVNCNjBtTWVEZlp0ZEttVXhKZTRRdnZSbTRYcmlaUVlaMlM3c0JQUw==";
APISeedsLyricsProvider::APISeedsLyricsProvider(QObject *parent) : LyricsProvider("APISeeds", parent), network_(new NetworkAccessManager(this)) {}
bool APISeedsLyricsProvider::StartSearch(const QString &artist, const QString &album, const QString &title, quint64 id) {
typedef QPair<QString, QString> Arg;
typedef QList<Arg> ArgList;
typedef QPair<QByteArray, QByteArray> EncodedArg;
ArgList args = ArgList();
args.append(Arg("apikey", QByteArray::fromBase64(kAPIKeyB64)));
QUrlQuery url_query;
for (const Arg &arg : args) {
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first), QUrl::toPercentEncoding(arg.second));
url_query.addQueryItem(encoded_arg.first, encoded_arg.second);
}
QUrl url(QString("%1/%2/%3").arg(kUrlSearch).arg(artist).arg(title));
url.setQuery(url_query);
QNetworkReply *reply = network_->get(QNetworkRequest(url));
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply*, quint64, QString, QString)), reply, id, artist, title);
return true;
}
void APISeedsLyricsProvider::CancelSearch(quint64 id) {
}
void APISeedsLyricsProvider::HandleSearchReply(QNetworkReply *reply, quint64 id, const QString artist, const QString title) {
reply->deleteLater();
QJsonObject json_obj = ExtractResult(reply, id);
if (json_obj.isEmpty()) return;
if (!json_obj.contains("artist") || !json_obj.contains("track")) {
Error(id, "APISeedsLyrics: Invalid Json reply, result is missing artist or track.", json_obj);
return;
}
QJsonObject json_artist(json_obj["artist"].toObject());
QJsonObject json_track(json_obj["track"].toObject());
if (!json_track.contains("text")) {
Error(id, "APISeedsLyrics: Invalid Json reply, track is missing text.", json_obj);
return;
}
LyricsSearchResults results;
LyricsSearchResult result;
result.artist = json_artist["name"].toString();
result.title = json_track["name"].toString();
result.lyrics = json_track["text"].toString();
result.score = 0.0;
if (result.artist.toLower() == artist.toLower()) result.score += 1.0;
if (result.title.toLower() == title.toLower()) result.score += 1.0;
results << result;
emit SearchFinished(id, results);
}
QJsonObject APISeedsLyricsProvider::ExtractJsonObj(QNetworkReply *reply, quint64 id) {
if (reply->error() != QNetworkReply::NoError) {
QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
Error(id, failure_reason);
return QJsonObject();
}
QByteArray data(reply->readAll());
QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
Error(id, "Reply from server missing Json data.");
return QJsonObject();
}
if (json_doc.isNull() || json_doc.isEmpty()) {
Error(id, "Received empty Json document.");
return QJsonObject();
}
if (!json_doc.isObject()) {
Error(id, "Json document is not an object.");
return QJsonObject();
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
Error(id, "Received empty Json object.");
return QJsonObject();
}
return json_obj;
}
QJsonObject APISeedsLyricsProvider::ExtractResult(QNetworkReply *reply, quint64 id) {
QJsonObject json_obj = ExtractJsonObj(reply, id);
if (json_obj.isEmpty()) return QJsonObject();
if (json_obj.contains("error")) {
Error(id, json_obj["error"].toString(), json_obj);
return QJsonObject();
}
if (!json_obj.contains("result")) {
Error(id, "Json reply is missing result.", json_obj);
return QJsonObject();
}
QJsonObject json_result = json_obj["result"].toObject();
if (json_result.isEmpty()) {
Error(id, "Json result object is empty.");
return QJsonObject();
}
return json_result;
}
void APISeedsLyricsProvider::Error(quint64 id, QString error, QVariant debug) {
LyricsSearchResults results;
qLog(Error) << "APISeedsLyrics:" << error;
if (debug.isValid()) qLog(Debug) << debug;
emit SearchFinished(id, results);
}

View File

@@ -0,0 +1,62 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef APISEEDSLYRICSPROVIDER_H
#define APISEEDSLYRICSPROVIDER_H
#include "config.h"
#include <stdbool.h>
#include <QtGlobal>
#include <QObject>
#include <QHash>
#include <QMetaType>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "lyricsprovider.h"
#include "lyricsfetcher.h"
class APISeedsLyricsProvider : public LyricsProvider {
Q_OBJECT
public:
explicit APISeedsLyricsProvider(QObject *parent = nullptr);
bool StartSearch(const QString &artist, const QString &album, const QString &title, quint64 id);
void CancelSearch(quint64 id);
private slots:
void HandleSearchReply(QNetworkReply *reply, quint64 id, const QString artist, const QString title);
private:
static const char *kUrlSearch;
static const char *kAPIKeyB64;
QNetworkAccessManager *network_;
void Error(quint64 id, QString error, QVariant debug = QVariant());
QJsonObject ExtractJsonObj(QNetworkReply *reply, quint64 id);
QJsonObject ExtractResult(QNetworkReply *reply, quint64 id);
};
#endif // APISEEDSLYRICSPROVIDER_H

View File

@@ -0,0 +1,215 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QObject>
#include <QByteArray>
#include <QList>
#include <QPair>
#include <QMap>
#include <QSet>
#include <QVariant>
#include <QString>
#include <QStringBuilder>
#include <QStringList>
#include <QUrl>
#include <QUrlQuery>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include "core/closure.h"
#include "core/logging.h"
#include "core/network.h"
#include "core/utilities.h"
#include "lyricsprovider.h"
#include "lyricsfetcher.h"
#include "auddlyricsprovider.h"
const char *AuddLyricsProvider::kUrlSearch = "https://api.audd.io/findLyrics/";
const char *AuddLyricsProvider::kAPITokenB64 = "ZjA0NjQ4YjgyNDM3ZTc1MjY3YjJlZDI5ZDBlMzQxZjk=";
AuddLyricsProvider::AuddLyricsProvider(QObject *parent) : LyricsProvider("AudD", parent), network_(new NetworkAccessManager(this)) {}
bool AuddLyricsProvider::StartSearch(const QString &artist, const QString &album, const QString &title, quint64 id) {
QString search(artist + " " + title);
typedef QPair<QString, QString> Arg;
typedef QList<Arg> ArgList;
typedef QPair<QByteArray, QByteArray> EncodedArg;
typedef QList<EncodedArg> EncodedArgList;
ArgList args = ArgList();
args.append(Arg("api_token", QByteArray::fromBase64(kAPITokenB64)));
args.append(Arg("q", search));
QUrlQuery url_query;
QUrl url(kUrlSearch);
for (const Arg &arg : args) {
EncodedArg encoded_arg(QUrl::toPercentEncoding(arg.first), QUrl::toPercentEncoding(arg.second));
url_query.addQueryItem(encoded_arg.first, encoded_arg.second);
}
url.setQuery(url_query);
QNetworkReply *reply = network_->get(QNetworkRequest(url));
NewClosure(reply, SIGNAL(finished()), this, SLOT(HandleSearchReply(QNetworkReply*, quint64, QString, QString)), reply, id, artist, title);
return true;
}
void AuddLyricsProvider::CancelSearch(quint64 id) {
}
void AuddLyricsProvider::HandleSearchReply(QNetworkReply *reply, quint64 id, const QString artist, const QString title) {
reply->deleteLater();
QJsonArray json_result = ExtractResult(reply, id);
if (json_result.isEmpty()) {
return;
}
LyricsSearchResults results;
for (const QJsonValue &value : json_result) {
if (!value.isObject()) {
qLog(Error) << "AuddLyrics: Invalid Json reply, result is not an object.";
qLog(Debug) << value;
continue;
}
QJsonObject json_obj = value.toObject();
if (
!json_obj.contains("song_id") ||
!json_obj.contains("artist_id") ||
!json_obj.contains("title") ||
!json_obj.contains("artist") ||
!json_obj.contains("lyrics")
) {
qLog(Error) << "AuddLyrics: Invalid Json reply, result is missing data.";
qLog(Debug) << value;
continue;
}
LyricsSearchResult result;
result.artist = json_obj["artist"].toString();
result.title = json_obj["title"].toString();
result.lyrics = json_obj["lyrics"].toString();
result.score = 0.0;
if (result.artist.toLower() == artist.toLower()) result.score += 1.0;
if (result.title.toLower() == title.toLower()) result.score += 1.0;
results << result;
}
emit SearchFinished(id, results);
}
QJsonObject AuddLyricsProvider::ExtractJsonObj(QNetworkReply *reply, quint64 id) {
if (reply->error() != QNetworkReply::NoError) {
QString failure_reason = QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
Error(id, failure_reason);
return QJsonObject();
}
QByteArray data(reply->readAll());
QJsonParseError error;
QJsonDocument json_doc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
Error(id, "Reply from server missing Json data.");
return QJsonObject();
}
if (json_doc.isNull() || json_doc.isEmpty()) {
Error(id, "Received empty Json document.");
return QJsonObject();
}
if (!json_doc.isObject()) {
Error(id, "Json document is not an object.");
return QJsonObject();
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
Error(id, "Received empty Json object.");
return QJsonObject();
}
return json_obj;
}
QJsonArray AuddLyricsProvider::ExtractResult(QNetworkReply *reply, quint64 id) {
QJsonObject json_obj = ExtractJsonObj(reply, id);
if (json_obj.isEmpty()) return QJsonArray();
if (!json_obj.contains("status")) {
Error(id, "Json reply is missing status.", json_obj);
return QJsonArray();
}
if (json_obj["status"].toString() == "error") {
if (!json_obj.contains("error")) {
Error(id, "Json reply is missing error status.", json_obj);
return QJsonArray();
}
QJsonObject json_error = json_obj["error"].toObject();
if (!json_error.contains("error_code") || !json_error.contains("error_message")) {
Error(id, "Json reply is missing error code or message.", json_error);
return QJsonArray();
}
QString error_code(json_error["error_code"].toString());
QString error_message(json_error["error_message"].toString());
Error(id, error_message);
return QJsonArray();
}
if (!json_obj.contains("result")) {
Error(id, "Json reply is missing result.", json_obj);
return QJsonArray();
}
QJsonArray json_result = json_obj["result"].toArray();
if (json_result.isEmpty()) {
Error(id, "No match.");
return QJsonArray();
}
return json_result;
}
void AuddLyricsProvider::Error(quint64 id, QString error, QVariant debug) {
qLog(Error) << "AuddLyrics:" << error;
if (debug.isValid()) qLog(Debug) << debug;
LyricsSearchResults results;
emit SearchFinished(id, results);
}

View File

@@ -0,0 +1,61 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef AUDDLYRICSPROVIDER_H
#define AUDDLYRICSPROVIDER_H
#include "config.h"
#include <stdbool.h>
#include <QObject>
#include <QHash>
#include <QMetaType>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "lyricsprovider.h"
#include "lyricsfetcher.h"
class AuddLyricsProvider : public LyricsProvider {
Q_OBJECT
public:
explicit AuddLyricsProvider(QObject *parent = nullptr);
bool StartSearch(const QString &artist, const QString &album, const QString &title, quint64 id);
void CancelSearch(quint64 id);
private slots:
void HandleSearchReply(QNetworkReply *reply, quint64 id, const QString artist, const QString title);
private:
static const char *kUrlSearch;
static const char *kAPITokenB64;
QNetworkAccessManager *network_;
void Error(quint64 id, QString error, QVariant debug = QVariant());
QJsonObject ExtractJsonObj(QNetworkReply *reply, quint64 id);
QJsonArray ExtractResult(QNetworkReply *reply, quint64 id);
};
#endif // AUDDLYRICSPROVIDER_H

View File

@@ -0,0 +1,124 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QtGlobal>
#include <QObject>
#include <QTimer>
#include <QString>
#include "core/logging.h"
#include "lyricsfetcher.h"
#include "lyricsfetchersearch.h"
const int LyricsFetcher::kMaxConcurrentRequests = 5;
const QRegExp LyricsFetcher::kRemoveNonAlpha("[^a-zA-Z0-9\\d\\s]");
const QRegExp LyricsFetcher::kRemoveFromTitle(" ?-? ((\\(|\\[)?)(Remastered|Live) ?((\\)|\\])?)$");
LyricsFetcher::LyricsFetcher(LyricsProviders *lyrics_providers, QObject *parent)
: QObject(parent),
lyrics_providers_(lyrics_providers),
next_id_(0),
request_starter_(new QTimer(this))
{
request_starter_->setInterval(500);
connect(request_starter_, SIGNAL(timeout()), SLOT(StartRequests()));
}
quint64 LyricsFetcher::Search(const QString &artist, const QString &album, const QString &title) {
LyricsSearchRequest request;
request.artist = artist;
request.album = album;
request.album.remove(kRemoveNonAlpha);
request.album.remove(kRemoveFromTitle);
request.title = title;
request.title.remove(kRemoveNonAlpha);
request.title.remove(kRemoveFromTitle);
request.id = next_id_++;
AddRequest(request);
return request.id;
}
void LyricsFetcher::AddRequest(const LyricsSearchRequest &req) {
queued_requests_.enqueue(req);
if (!request_starter_->isActive()) request_starter_->start();
if (active_requests_.size() < kMaxConcurrentRequests) StartRequests();
}
void LyricsFetcher::Clear() {
queued_requests_.clear();
for (LyricsFetcherSearch *search : active_requests_.values()) {
search->Cancel();
search->deleteLater();
}
active_requests_.clear();
}
void LyricsFetcher::StartRequests() {
if (queued_requests_.isEmpty()) {
request_starter_->stop();
return;
}
while (!queued_requests_.isEmpty() && active_requests_.size() < kMaxConcurrentRequests) {
LyricsSearchRequest request = queued_requests_.dequeue();
LyricsFetcherSearch *search = new LyricsFetcherSearch(request, this);
active_requests_.insert(request.id, search);
connect(search, SIGNAL(SearchFinished(quint64, LyricsSearchResults)), SLOT(SingleSearchFinished(quint64, LyricsSearchResults)));
connect(search, SIGNAL(LyricsFetched(quint64, const QString&)), SLOT(SingleLyricsFetched(quint64, const QString&)));
search->Start(lyrics_providers_);
}
}
void LyricsFetcher::SingleSearchFinished(quint64 request_id, LyricsSearchResults results) {
LyricsFetcherSearch *search = active_requests_.take(request_id);
if (!search) return;
search->deleteLater();
emit SearchFinished(request_id, results);
}
void LyricsFetcher::SingleLyricsFetched(quint64 request_id, const QString &lyrics) {
LyricsFetcherSearch *search = active_requests_.take(request_id);
if (!search) return;
search->deleteLater();
emit LyricsFetched(request_id, lyrics);
}

View File

@@ -0,0 +1,95 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef LYRICSFETCHER_H
#define LYRICSFETCHER_H
#include "config.h"
#include <stdbool.h>
#include <QtGlobal>
#include <QObject>
#include <QQueue>
#include <QTimer>
#include <QHash>
#include <QList>
#include <QString>
#include <QUrl>
class LyricsProviders;
class LyricsFetcherSearch;
struct LyricsSearchRequest {
quint64 id;
QString artist;
QString album;
QString title;
};
struct LyricsSearchResult {
QString provider;
QString artist;
QString album;
QString title;
QString lyrics;
float score;
};
Q_DECLARE_METATYPE(LyricsSearchResult);
typedef QList<LyricsSearchResult> LyricsSearchResults;
Q_DECLARE_METATYPE(QList<LyricsSearchResult>);
class LyricsFetcher : public QObject {
Q_OBJECT
public:
LyricsFetcher(LyricsProviders *lyrics_providers, QObject *parent = nullptr);
virtual ~LyricsFetcher() {}
static const int kMaxConcurrentRequests;
static const QRegExp kRemoveNonAlpha;
static const QRegExp kRemoveFromTitle;
quint64 Search(const QString &artist, const QString &album, const QString &title);
void Clear();
signals:
void LyricsFetched(quint64, const QString &lyrics);
void SearchFinished(quint64, const LyricsSearchResults &results);
private slots:
void SingleSearchFinished(quint64, LyricsSearchResults results);
void SingleLyricsFetched(quint64, const QString &lyrics);
void StartRequests();
private:
void AddRequest(const LyricsSearchRequest &req);
LyricsProviders *lyrics_providers_;
quint64 next_id_;
QQueue<LyricsSearchRequest> queued_requests_;
QHash<quint64, LyricsFetcherSearch*> active_requests_;
QTimer *request_starter_;
};
#endif // LYRICSFETCHER_H

View File

@@ -0,0 +1,117 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <algorithm>
#include <cmath>
#include <QObject>
#include <QtAlgorithms>
#include <QTimer>
#include <QList>
#include <QtDebug>
#include "core/closure.h"
#include "core/logging.h"
#include "lyricsfetcher.h"
#include "lyricsfetchersearch.h"
#include "lyricsprovider.h"
#include "lyricsproviders.h"
const int LyricsFetcherSearch::kSearchTimeoutMs = 6000;
LyricsFetcherSearch::LyricsFetcherSearch(
const LyricsSearchRequest &request, QObject *parent)
: QObject(parent),
request_(request),
cancel_requested_(false) {
QTimer::singleShot(kSearchTimeoutMs, this, SLOT(TerminateSearch()));
}
void LyricsFetcherSearch::TerminateSearch() {
for (int id : pending_requests_.keys()) {
pending_requests_.take(id)->CancelSearch(id);
}
AllProvidersFinished();
}
void LyricsFetcherSearch::Start(LyricsProviders *lyrics_providers) {
for (LyricsProvider *provider : lyrics_providers->List()) {
connect(provider, SIGNAL(SearchFinished(quint64, QList<LyricsSearchResult>)), SLOT(ProviderSearchFinished(quint64, QList<LyricsSearchResult>)));
const int id = lyrics_providers->NextId();
const bool success = provider->StartSearch(request_.artist, request_.album, request_.title, id);
if (success) pending_requests_[id] = provider;
}
if (pending_requests_.isEmpty()) TerminateSearch();
}
void LyricsFetcherSearch::ProviderSearchFinished(quint64 id, const QList<LyricsSearchResult> &results) {
if (!pending_requests_.contains(id)) return;
LyricsProvider *provider = pending_requests_.take(id);
LyricsSearchResults results_copy(results);
for (int i = 0; i < results_copy.count(); ++i) {
results_copy[i].provider = provider->name();
}
results_.append(results_copy);
if (!pending_requests_.isEmpty()) {
return;
}
AllProvidersFinished();
}
void LyricsFetcherSearch::AllProvidersFinished() {
if (cancel_requested_) return;
if (!results_.isEmpty()) {
LyricsSearchResult result_use;
result_use.score = 0.0;
for (LyricsSearchResult result : results_) {
if (result_use.lyrics.isEmpty() || result.score > result_use.score) result_use = result;
}
emit LyricsFetched(request_.id, result_use.lyrics);
}
emit SearchFinished(request_.id, results_);
}
void LyricsFetcherSearch::Cancel() {
cancel_requested_ = true;
if (!pending_requests_.isEmpty()) {
TerminateSearch();
}
}

View File

@@ -0,0 +1,69 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef LYRICSFETCHERSEARCH_H
#define LYRICSFETCHERSEARCH_H
#include "config.h"
#include <stdbool.h>
#include <QtGlobal>
#include <QObject>
#include <QList>
#include <QMap>
#include "lyricsfetcher.h"
class LyricsProvider;
class LyricsProviders;
class LyricsFetcherSearch : public QObject {
Q_OBJECT
public:
LyricsFetcherSearch(const LyricsSearchRequest &request, QObject *parent);
void Start(LyricsProviders *cover_providers);
void Cancel();
signals:
void SearchFinished(quint64, const LyricsSearchResults &results);
void LyricsFetched(quint64, const QString &lyrics);
private slots:
void ProviderSearchFinished(quint64 id, const QList<LyricsSearchResult> &results);
void TerminateSearch();
private:
void AllProvidersFinished();
void SendBestImage();
private:
static const int kSearchTimeoutMs;
LyricsSearchRequest request_;
LyricsSearchResults results_;
QMap<int, LyricsProvider*> pending_requests_;
bool cancel_requested_;
};
#endif // LYRICSFETCHERSEARCH_H

View File

@@ -0,0 +1,28 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QObject>
#include <QString>
#include "lyricsprovider.h"
LyricsProvider::LyricsProvider(const QString &name, QObject *parent)
: QObject(parent), name_(name) {}

View File

@@ -0,0 +1,52 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef LYRICSPROVIDER_H
#define LYRICSPROVIDER_H
#include "config.h"
#include <stdbool.h>
#include <QObject>
#include <QList>
#include <QString>
struct LyricsSearchResult;
class LyricsProvider : public QObject {
Q_OBJECT
public:
explicit LyricsProvider(const QString &name, QObject *parent);
QString name() const { return name_; }
virtual bool StartSearch(const QString &artist, const QString &album, const QString &title, quint64 id) = 0;
virtual void CancelSearch(quint64 id) {}
signals:
void SearchFinished(quint64 id, const QList<LyricsSearchResult>& results);
private:
QString name_;
};
#endif // LYRICSPROVIDER_H

View File

@@ -0,0 +1,76 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <QObject>
#include <QMutex>
#include <QString>
#include <QtDebug>
#include "core/application.h"
#include "core/logging.h"
#include "lyricsprovider.h"
#include "lyricsproviders.h"
#include "lyricsfetcher.h"
LyricsProviders::LyricsProviders(QObject *parent) : QObject(parent) {}
void LyricsProviders::AddProvider(LyricsProvider *provider) {
{
QMutexLocker locker(&mutex_);
lyrics_providers_.insert(provider, provider->name());
connect(provider, SIGNAL(destroyed()), SLOT(ProviderDestroyed()));
}
qLog(Debug) << "Registered lyrics provider" << provider->name();
}
void LyricsProviders::RemoveProvider(LyricsProvider *provider) {
if (!provider) return;
// It's not safe to dereference provider at this point because it might have already been destroyed.
QString name;
{
QMutexLocker locker(&mutex_);
name = lyrics_providers_.take(provider);
}
if (name.isNull()) {
qLog(Debug) << "Tried to remove a lyrics provider that was not registered";
}
else {
qLog(Debug) << "Unregistered lyrics provider" << name;
}
}
void LyricsProviders::ProviderDestroyed() {
LyricsProvider *provider = static_cast<LyricsProvider*>(sender());
RemoveProvider(provider);
}
int LyricsProviders::NextId() { return next_id_.fetchAndAddRelaxed(1); }

View File

@@ -0,0 +1,60 @@
/*
* Strawberry Music Player
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* 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/>.
*
*/
#ifndef LYRICSPROVIDERS_H
#define LYRICSPROVIDERS_H
#include "config.h"
#include <stdbool.h>
#include <QtGlobal>
#include <QObject>
#include <QMutex>
#include <QList>
#include <QMap>
#include <QString>
#include <QAtomicInt>
class LyricsProvider;
class LyricsProviders : public QObject {
Q_OBJECT
public:
explicit LyricsProviders(QObject *parent = nullptr);
void AddProvider(LyricsProvider *provider);
void RemoveProvider(LyricsProvider *provider);
QList<LyricsProvider*> List() const { return lyrics_providers_.keys(); }
bool HasAnyProviders() const { return !lyrics_providers_.isEmpty(); }
int NextId();
private slots:
void ProviderDestroyed();
private:
Q_DISABLE_COPY(LyricsProviders);
QMap<LyricsProvider *, QString> lyrics_providers_;
QMutex mutex_;
QAtomicInt next_id_;
};
#endif // LYRICSPROVIDERS_H