Use QStringLiteral

This commit is contained in:
Jonas Kvinge
2024-04-09 23:20:26 +02:00
parent 3cfffa5fbb
commit 58944993b8
233 changed files with 3885 additions and 3885 deletions

View File

@@ -61,7 +61,7 @@ const char *GeniusLyricsProvider::kUrlSearch = "https://api.genius.com/search/";
const char *GeniusLyricsProvider::kClientIDB64 = "RUNTNXU4U1VyMU1KUU5hdTZySEZteUxXY2hkanFiY3lfc2JjdXBpNG5WMU9SNUg4dTBZelEtZTZCdFg2dl91SQ==";
const char *GeniusLyricsProvider::kClientSecretB64 = "VE9pMU9vUjNtTXZ3eFR3YVN0QVRyUjVoUlhVWDI1Ylp5X240eEt1M0ZkYlNwRG5JUnd0LXFFbHdGZkZkRWY2VzJ1S011UnQzM3c2Y3hqY0tVZ3NGN2c=";
GeniusLyricsProvider::GeniusLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : JsonLyricsProvider("Genius", true, true, network, parent), server_(nullptr) {
GeniusLyricsProvider::GeniusLyricsProvider(SharedPtr<NetworkAccessManager> network, QObject *parent) : JsonLyricsProvider(QStringLiteral("Genius"), true, true, network, parent), server_(nullptr) {
QSettings s;
s.beginGroup(kSettingsGroup);
@@ -106,18 +106,18 @@ void GeniusLyricsProvider::Authenticate() {
}
QUrlQuery url_query;
url_query.addQueryItem("client_id", QUrl::toPercentEncoding(QByteArray::fromBase64(kClientIDB64)));
url_query.addQueryItem("redirect_uri", QUrl::toPercentEncoding(redirect_url.toString()));
url_query.addQueryItem("scope", "me");
url_query.addQueryItem("state", QUrl::toPercentEncoding(code_challenge_));
url_query.addQueryItem("response_type", "code");
url_query.addQueryItem(QStringLiteral("client_id"), QUrl::toPercentEncoding(QByteArray::fromBase64(kClientIDB64)));
url_query.addQueryItem(QStringLiteral("redirect_uri"), QUrl::toPercentEncoding(redirect_url.toString()));
url_query.addQueryItem(QStringLiteral("scope"), QStringLiteral("me"));
url_query.addQueryItem(QStringLiteral("state"), QUrl::toPercentEncoding(code_challenge_));
url_query.addQueryItem(QStringLiteral("response_type"), QStringLiteral("code"));
QUrl url(kOAuthAuthorizeUrl);
url.setQuery(url_query);
const bool result = QDesktopServices::openUrl(url);
if (!result) {
QMessageBox messagebox(QMessageBox::Information, tr("Genius Authentication"), tr("Please open this URL in your browser") + QString(":<br /><a href=\"%1\">%1</a>").arg(url.toString()), QMessageBox::Ok);
QMessageBox messagebox(QMessageBox::Information, tr("Genius Authentication"), tr("Please open this URL in your browser") + QStringLiteral(":<br /><a href=\"%1\">%1</a>").arg(url.toString()), QMessageBox::Ok);
messagebox.setTextFormat(Qt::RichText);
messagebox.exec();
}
@@ -132,10 +132,10 @@ void GeniusLyricsProvider::RedirectArrived() {
QUrl url = server_->request_url();
if (url.isValid()) {
QUrlQuery url_query(url);
if (url_query.hasQueryItem("error")) {
AuthError(QUrlQuery(url).queryItemValue("error"));
if (url_query.hasQueryItem(QStringLiteral("error"))) {
AuthError(QUrlQuery(url).queryItemValue(QStringLiteral("error")));
}
else if (url_query.hasQueryItem("code")) {
else if (url_query.hasQueryItem(QStringLiteral("code"))) {
QUrl redirect_url(kOAuthRedirectUrl);
redirect_url.setPort(server_->url().port());
RequestAccessToken(url, redirect_url);
@@ -164,17 +164,17 @@ void GeniusLyricsProvider::RequestAccessToken(const QUrl &url, const QUrl &redir
QUrlQuery url_query(url);
if (url.hasQuery() && url_query.hasQueryItem("code") && url_query.hasQueryItem("state")) {
if (url.hasQuery() && url_query.hasQueryItem(QStringLiteral("code")) && url_query.hasQueryItem(QStringLiteral("state"))) {
const QString code = url_query.queryItemValue("code");
const QString code = url_query.queryItemValue(QStringLiteral("code"));
QUrlQuery new_url_query;
new_url_query.addQueryItem("code", QUrl::toPercentEncoding(code));
new_url_query.addQueryItem("client_id", QUrl::toPercentEncoding(QByteArray::fromBase64(kClientIDB64)));
new_url_query.addQueryItem("client_secret", QUrl::toPercentEncoding(QByteArray::fromBase64(kClientSecretB64)));
new_url_query.addQueryItem("redirect_uri", QUrl::toPercentEncoding(redirect_url.toString()));
new_url_query.addQueryItem("grant_type", "authorization_code");
new_url_query.addQueryItem("response_type", "code");
new_url_query.addQueryItem(QStringLiteral("code"), QUrl::toPercentEncoding(code));
new_url_query.addQueryItem(QStringLiteral("client_id"), QUrl::toPercentEncoding(QByteArray::fromBase64(kClientIDB64)));
new_url_query.addQueryItem(QStringLiteral("client_secret"), QUrl::toPercentEncoding(QByteArray::fromBase64(kClientSecretB64)));
new_url_query.addQueryItem(QStringLiteral("redirect_uri"), QUrl::toPercentEncoding(redirect_url.toString()));
new_url_query.addQueryItem(QStringLiteral("grant_type"), QStringLiteral("authorization_code"));
new_url_query.addQueryItem(QStringLiteral("response_type"), QStringLiteral("code"));
QUrl new_url(kOAuthAccessTokenUrl);
QNetworkRequest req(new_url);
@@ -213,7 +213,7 @@ void GeniusLyricsProvider::AccessTokenRequestFinished(QNetworkReply *reply) {
if (reply->error() != QNetworkReply::NoError || reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
if (reply->error() != QNetworkReply::NoError && reply->error() < 200) {
// This is a network error, there is nothing more to do.
AuthError(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
AuthError(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
return;
}
else {
@@ -223,18 +223,18 @@ void GeniusLyricsProvider::AccessTokenRequestFinished(QNetworkReply *reply) {
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
if (json_error.error == QJsonParseError::NoError && !json_doc.isEmpty() && json_doc.isObject()) {
QJsonObject json_obj = json_doc.object();
if (!json_obj.isEmpty() && json_obj.contains("error") && json_obj.contains("error_description")) {
QString error = json_obj["error"].toString();
QString error_description = json_obj["error_description"].toString();
login_errors_ << QString("Authentication failure: %1 (%2)").arg(error, error_description);
if (!json_obj.isEmpty() && json_obj.contains(QStringLiteral("error")) && json_obj.contains(QStringLiteral("error_description"))) {
QString error = json_obj[QStringLiteral("error")].toString();
QString error_description = json_obj[QStringLiteral("error_description")].toString();
login_errors_ << QStringLiteral("Authentication failure: %1 (%2)").arg(error, error_description);
}
}
if (login_errors_.isEmpty()) {
if (reply->error() != QNetworkReply::NoError) {
login_errors_ << QString("%1 (%2)").arg(reply->errorString()).arg(reply->error());
login_errors_ << QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error());
}
else {
login_errors_ << QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
login_errors_ << QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
}
}
AuthError();
@@ -248,32 +248,32 @@ void GeniusLyricsProvider::AccessTokenRequestFinished(QNetworkReply *reply) {
QJsonDocument json_doc = QJsonDocument::fromJson(data, &json_error);
if (json_error.error != QJsonParseError::NoError) {
Error(QString("Failed to parse Json data in authentication reply: %1").arg(json_error.errorString()));
Error(QStringLiteral("Failed to parse Json data in authentication reply: %1").arg(json_error.errorString()));
return;
}
if (json_doc.isEmpty()) {
AuthError("Authentication reply from server has empty Json document.");
AuthError(QStringLiteral("Authentication reply from server has empty Json document."));
return;
}
if (!json_doc.isObject()) {
AuthError("Authentication reply from server has Json document that is not an object.", json_doc);
AuthError(QStringLiteral("Authentication reply from server has Json document that is not an object."), json_doc);
return;
}
QJsonObject json_obj = json_doc.object();
if (json_obj.isEmpty()) {
AuthError("Authentication reply from server has empty Json object.", json_doc);
AuthError(QStringLiteral("Authentication reply from server has empty Json object."), json_doc);
return;
}
if (!json_obj.contains("access_token")) {
AuthError("Authentication reply from server is missing access token.", json_obj);
if (!json_obj.contains(QStringLiteral("access_token"))) {
AuthError(QStringLiteral("Authentication reply from server is missing access token."), json_obj);
return;
}
access_token_ = json_obj["access_token"].toString();
access_token_ = json_obj[QStringLiteral("access_token")].toString();
QSettings s;
s.beginGroup(kSettingsGroup);
@@ -297,7 +297,7 @@ bool GeniusLyricsProvider::StartSearch(const int id, const LyricsSearchRequest &
requests_search_.insert(id, search);
QUrlQuery url_query;
url_query.addQueryItem("q", QUrl::toPercentEncoding(QString("%1 %2").arg(request.artist, request.title)));
url_query.addQueryItem(QStringLiteral("q"), QUrl::toPercentEncoding(QStringLiteral("%1 %2").arg(request.artist, request.title)));
QUrl url(kUrlSearch);
url.setQuery(url_query);
@@ -330,78 +330,78 @@ void GeniusLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id)
return;
}
if (!json_obj.contains("meta")) {
Error("Json reply is missing meta object.", json_obj);
if (!json_obj.contains(QStringLiteral("meta"))) {
Error(QStringLiteral("Json reply is missing meta object."), json_obj);
EndSearch(search);
return;
}
if (!json_obj["meta"].isObject()) {
Error("Json reply meta is not an object.", json_obj);
if (!json_obj[QStringLiteral("meta")].isObject()) {
Error(QStringLiteral("Json reply meta is not an object."), json_obj);
EndSearch(search);
return;
}
QJsonObject obj_meta = json_obj["meta"].toObject();
if (!obj_meta.contains("status")) {
Error("Json reply meta object is missing status.", obj_meta);
QJsonObject obj_meta = json_obj[QStringLiteral("meta")].toObject();
if (!obj_meta.contains(QStringLiteral("status"))) {
Error(QStringLiteral("Json reply meta object is missing status."), obj_meta);
EndSearch(search);
return;
}
int status = obj_meta["status"].toInt();
int status = obj_meta[QStringLiteral("status")].toInt();
if (status != 200) {
if (obj_meta.contains("message")) {
Error(QString("Received error %1: %2.").arg(status).arg(obj_meta["message"].toString()));
if (obj_meta.contains(QStringLiteral("message"))) {
Error(QStringLiteral("Received error %1: %2.").arg(status).arg(obj_meta[QStringLiteral("message")].toString()));
}
else {
Error(QString("Received error %1.").arg(status));
Error(QStringLiteral("Received error %1.").arg(status));
}
EndSearch(search);
return;
}
if (!json_obj.contains("response")) {
Error("Json reply is missing response.", json_obj);
if (!json_obj.contains(QStringLiteral("response"))) {
Error(QStringLiteral("Json reply is missing response."), json_obj);
EndSearch(search);
return;
}
if (!json_obj["response"].isObject()) {
Error("Json response is not an object.", json_obj);
if (!json_obj[QStringLiteral("response")].isObject()) {
Error(QStringLiteral("Json response is not an object."), json_obj);
EndSearch(search);
return;
}
QJsonObject obj_response = json_obj["response"].toObject();
if (!obj_response.contains("hits")) {
Error("Json response is missing hits.", obj_response);
QJsonObject obj_response = json_obj[QStringLiteral("response")].toObject();
if (!obj_response.contains(QStringLiteral("hits"))) {
Error(QStringLiteral("Json response is missing hits."), obj_response);
EndSearch(search);
return;
}
if (!obj_response["hits"].isArray()) {
Error("Json hits is not an array.", obj_response);
if (!obj_response[QStringLiteral("hits")].isArray()) {
Error(QStringLiteral("Json hits is not an array."), obj_response);
EndSearch(search);
return;
}
QJsonArray array_hits = obj_response["hits"].toArray();
QJsonArray array_hits = obj_response[QStringLiteral("hits")].toArray();
for (const QJsonValueRef value_hit : array_hits) {
if (!value_hit.isObject()) {
continue;
}
QJsonObject obj_hit = value_hit.toObject();
if (!obj_hit.contains("result")) {
if (!obj_hit.contains(QStringLiteral("result"))) {
continue;
}
if (!obj_hit["result"].isObject()) {
if (!obj_hit[QStringLiteral("result")].isObject()) {
continue;
}
QJsonObject obj_result = obj_hit["result"].toObject();
if (!obj_result.contains("title") || !obj_result.contains("primary_artist") || !obj_result.contains("url") || !obj_result["primary_artist"].isObject()) {
Error("Missing one or more values in result object", obj_result);
QJsonObject obj_result = obj_hit[QStringLiteral("result")].toObject();
if (!obj_result.contains(QStringLiteral("title")) || !obj_result.contains(QStringLiteral("primary_artist")) || !obj_result.contains(QStringLiteral("url")) || !obj_result[QStringLiteral("primary_artist")].isObject()) {
Error(QStringLiteral("Missing one or more values in result object"), obj_result);
continue;
}
QJsonObject primary_artist = obj_result["primary_artist"].toObject();
if (!primary_artist.contains("name")) continue;
QJsonObject primary_artist = obj_result[QStringLiteral("primary_artist")].toObject();
if (!primary_artist.contains(QStringLiteral("name"))) continue;
QString artist = primary_artist["name"].toString();
QString title = obj_result["title"].toString();
QString artist = primary_artist[QStringLiteral("name")].toString();
QString title = obj_result[QStringLiteral("title")].toString();
// Ignore results where both the artist and title don't match.
if (!artist.startsWith(search->request.albumartist, Qt::CaseInsensitive) &&
@@ -410,7 +410,7 @@ void GeniusLyricsProvider::HandleSearchReply(QNetworkReply *reply, const int id)
continue;
}
QUrl url(obj_result["url"].toString());
QUrl url(obj_result[QStringLiteral("url")].toString());
if (!url.isValid()) continue;
if (search->requests_lyric_.contains(url)) continue;
@@ -450,27 +450,27 @@ void GeniusLyricsProvider::HandleLyricReply(QNetworkReply *reply, const int sear
const GeniusLyricsLyricContext lyric = search->requests_lyric_.value(url);
if (reply->error() != QNetworkReply::NoError) {
Error(QString("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
Error(QStringLiteral("%1 (%2)").arg(reply->errorString()).arg(reply->error()));
EndSearch(search, lyric);
return;
}
else if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
Error(QString("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()));
Error(QStringLiteral("Received HTTP code %1").arg(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()));
EndSearch(search, lyric);
return;
}
QByteArray data = reply->readAll();
if (data.isEmpty()) {
Error("Empty reply received from server.");
Error(QStringLiteral("Empty reply received from server."));
EndSearch(search, lyric);
return;
}
QString content = QString::fromUtf8(data);
QString lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div data-lyrics-container=[^>]+>"), true);
QString lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression(QStringLiteral("<div[^>]*>")), QRegularExpression(QStringLiteral("<\\/div>")), QRegularExpression(QStringLiteral("<div data-lyrics-container=[^>]+>")), true);
if (lyrics.isEmpty()) {
lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression("<div[^>]*>"), QRegularExpression("<\\/div>"), QRegularExpression("<div class=\"lyrics\">"), true);
lyrics = HtmlLyricsProvider::ParseLyricsFromHTML(content, QRegularExpression(QStringLiteral("<div[^>]*>")), QRegularExpression(QStringLiteral("<\\/div>")), QRegularExpression(QStringLiteral("<div class=\"lyrics\">")), true);
}
if (!lyrics.isEmpty()) {