strawberry-audio-player-win.../src/tidal/tidalurlhandler.cpp

85 lines
2.6 KiB
C++
Raw Normal View History

2020-04-13 19:04:06 +02:00
/*
* Strawberry Music Player
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2020-04-13 19:04:06 +02: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/>.
*
*/
#include "config.h"
#include <QObject>
#include <QString>
#include <QUrl>
#include "core/application.h"
#include "core/taskmanager.h"
#include "core/song.h"
#include "tidal/tidalservice.h"
#include "tidalurlhandler.h"
2021-07-11 07:40:57 +02:00
TidalUrlHandler::TidalUrlHandler(Application *app, TidalService *service)
: UrlHandler(service),
app_(app),
service_(service) {
2020-04-13 19:04:06 +02:00
QObject::connect(service, &TidalService::StreamURLFailure, this, &TidalUrlHandler::GetStreamURLFailure);
QObject::connect(service, &TidalService::StreamURLSuccess, this, &TidalUrlHandler::GetStreamURLSuccess);
2020-04-13 19:04:06 +02:00
}
UrlHandler::LoadResult TidalUrlHandler::StartLoading(const QUrl &url) {
Request req;
req.task_id = app_->task_manager()->StartTask(QString("Loading %1 stream...").arg(url.scheme()));
QString error;
req.id = service_->GetStreamURL(url, error);
if (req.id == 0) {
CancelTask(req.task_id);
2023-02-18 14:09:27 +01:00
return LoadResult(url, LoadResult::Type::Error, error);
}
requests_.insert(req.id, req);
2020-04-13 19:04:06 +02:00
LoadResult ret(url);
2023-02-18 14:09:27 +01:00
ret.type_ = LoadResult::Type::WillLoadAsynchronously;
2020-04-13 19:04:06 +02:00
return ret;
}
void TidalUrlHandler::GetStreamURLFailure(const uint id, const QUrl &original_url, const QString &error) {
2020-04-13 19:04:06 +02:00
if (!requests_.contains(id)) return;
Request req = requests_.take(id);
CancelTask(req.task_id);
2023-02-18 14:09:27 +01:00
emit AsyncLoadComplete(LoadResult(original_url, LoadResult::Type::Error, error));
}
void TidalUrlHandler::GetStreamURLSuccess(const uint id, const QUrl &original_url, const QUrl &stream_url, const Song::FileType filetype, const int samplerate, const int bit_depth, const qint64 duration) {
if (!requests_.contains(id)) return;
Request req = requests_.take(id);
CancelTask(req.task_id);
2023-02-18 14:09:27 +01:00
emit AsyncLoadComplete(LoadResult(original_url, LoadResult::Type::TrackAvailable, stream_url, filetype, samplerate, bit_depth, duration));
2020-04-13 19:04:06 +02:00
}
void TidalUrlHandler::CancelTask(const int task_id) {
app_->task_manager()->SetTaskFinished(task_id);
2020-04-13 19:04:06 +02:00
}