2014-12-17 19:02:21 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011-2013, Alan Briolat <alan.briolat@gmail.com>
|
|
|
|
Copyright 2013, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2013, Ross Wolfson <ross.wolfson@gmail.com>
|
|
|
|
Copyright 2013-2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014, Chocobozzz <florian.bigard@gmail.com>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-12-06 00:10:25 +01:00
|
|
|
#include "subsonicservice.h"
|
2013-01-24 17:48:58 +01:00
|
|
|
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkCookieJar>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QSslConfiguration>
|
|
|
|
#include <QXmlStreamReader>
|
2015-04-11 22:52:31 +02:00
|
|
|
#include <QUrlQuery>
|
2013-01-24 17:48:58 +01:00
|
|
|
|
2012-07-28 15:10:42 +02:00
|
|
|
#include "core/application.h"
|
2013-01-24 17:48:58 +01:00
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/database.h"
|
2011-12-07 19:06:11 +01:00
|
|
|
#include "core/logging.h"
|
2013-01-10 23:08:52 +01:00
|
|
|
#include "core/mergedproxymodel.h"
|
2013-01-24 17:48:58 +01:00
|
|
|
#include "core/player.h"
|
2013-01-17 23:13:57 +01:00
|
|
|
#include "core/taskmanager.h"
|
2013-01-24 17:48:58 +01:00
|
|
|
#include "core/timeconstants.h"
|
|
|
|
#include "core/utilities.h"
|
2013-01-21 23:13:48 +01:00
|
|
|
#include "globalsearch/globalsearch.h"
|
|
|
|
#include "globalsearch/librarysearchprovider.h"
|
2014-12-18 23:35:21 +01:00
|
|
|
#include "internet/core/internetmodel.h"
|
|
|
|
#include "internet/subsonic/subsonicurlhandler.h"
|
2015-12-01 07:13:37 +01:00
|
|
|
#include "internet/subsonic/subsonicdynamicplaylist.h"
|
2013-01-24 17:48:58 +01:00
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "library/libraryfilterwidget.h"
|
2015-12-01 07:13:37 +01:00
|
|
|
#include "smartplaylists/generator.h"
|
|
|
|
#include "smartplaylists/querygenerator.h"
|
2013-01-24 17:48:58 +01:00
|
|
|
#include "ui/iconloader.h"
|
2011-12-06 00:10:25 +01:00
|
|
|
|
|
|
|
const char* SubsonicService::kServiceName = "Subsonic";
|
|
|
|
const char* SubsonicService::kSettingsGroup = "Subsonic";
|
2013-01-21 23:13:48 +01:00
|
|
|
const char* SubsonicService::kApiVersion = "1.8.0";
|
2011-12-06 00:10:25 +01:00
|
|
|
const char* SubsonicService::kApiClientName = "Clementine";
|
|
|
|
|
2013-01-10 23:08:52 +01:00
|
|
|
const char* SubsonicService::kSongsTable = "subsonic_songs";
|
|
|
|
const char* SubsonicService::kFtsTable = "subsonic_songs_fts";
|
|
|
|
|
2013-07-03 03:16:41 +02:00
|
|
|
const int SubsonicService::kMaxRedirects = 10;
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
SubsonicService::SubsonicService(Application* app, InternetModel* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: InternetService(kServiceName, app, parent, parent),
|
|
|
|
network_(new QNetworkAccessManager(this)),
|
|
|
|
url_handler_(new SubsonicUrlHandler(this, this)),
|
|
|
|
scanner_(new SubsonicLibraryScanner(this, this)),
|
|
|
|
load_database_task_id_(0),
|
|
|
|
context_menu_(nullptr),
|
|
|
|
root_(nullptr),
|
|
|
|
library_backend_(nullptr),
|
|
|
|
library_model_(nullptr),
|
|
|
|
library_filter_(nullptr),
|
|
|
|
library_sort_model_(new QSortFilterProxyModel(this)),
|
|
|
|
total_song_count_(0),
|
|
|
|
login_state_(LoginState_OtherError),
|
2015-12-08 23:15:56 +01:00
|
|
|
redirect_count_(0),
|
|
|
|
is_ampache_(false) {
|
2013-01-09 23:20:55 +01:00
|
|
|
app_->player()->RegisterUrlHandler(url_handler_);
|
2013-01-10 23:08:52 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(scanner_, SIGNAL(ScanFinished()), SLOT(ReloadDatabaseFinished()));
|
2013-01-15 00:36:23 +01:00
|
|
|
|
2013-01-10 23:08:52 +01:00
|
|
|
library_backend_ = new LibraryBackend;
|
|
|
|
library_backend_->moveToThread(app_->database()->thread());
|
2019-11-09 23:45:28 +01:00
|
|
|
library_backend_->Init(app_->database(), kSongsTable, QString(),
|
|
|
|
QString(), kFtsTable);
|
2013-01-21 00:24:14 +01:00
|
|
|
connect(library_backend_, SIGNAL(TotalSongCountUpdated(int)),
|
|
|
|
SLOT(UpdateTotalSongCount(int)));
|
2013-01-10 23:08:52 +01:00
|
|
|
|
2015-12-01 07:13:37 +01:00
|
|
|
using smart_playlists::Generator;
|
|
|
|
using smart_playlists::GeneratorPtr;
|
|
|
|
|
2013-01-10 23:08:52 +01:00
|
|
|
library_model_ = new LibraryModel(library_backend_, app_, this);
|
|
|
|
library_model_->set_show_various_artists(false);
|
2015-12-01 07:13:37 +01:00
|
|
|
library_model_->set_show_smart_playlists(true);
|
|
|
|
library_model_->set_default_smart_playlists(
|
|
|
|
LibraryModel::DefaultGenerators()
|
|
|
|
<< (LibraryModel::GeneratorList()
|
|
|
|
<< GeneratorPtr(new SubsonicDynamicPlaylist(
|
|
|
|
tr("Newest"),
|
|
|
|
SubsonicDynamicPlaylist::QueryStat_Newest))
|
|
|
|
<< GeneratorPtr(new SubsonicDynamicPlaylist(
|
|
|
|
tr("Random"),
|
|
|
|
SubsonicDynamicPlaylist::QueryStat_Random))
|
|
|
|
<< GeneratorPtr(new SubsonicDynamicPlaylist(
|
|
|
|
tr("Frequently Played"),
|
|
|
|
SubsonicDynamicPlaylist::QueryStat_Frequent))
|
|
|
|
<< GeneratorPtr(new SubsonicDynamicPlaylist(
|
|
|
|
tr("Top Rated"),
|
|
|
|
SubsonicDynamicPlaylist::QueryStat_Highest))
|
|
|
|
<< GeneratorPtr(new SubsonicDynamicPlaylist(
|
|
|
|
tr("Recently Played"),
|
|
|
|
SubsonicDynamicPlaylist::QueryStat_Recent))
|
|
|
|
<< GeneratorPtr(new SubsonicDynamicPlaylist(
|
|
|
|
tr("Starred"),
|
|
|
|
SubsonicDynamicPlaylist::QueryStat_Starred))
|
|
|
|
));
|
2013-01-10 23:08:52 +01:00
|
|
|
|
2013-01-17 22:01:54 +01:00
|
|
|
library_filter_ = new LibraryFilterWidget(0);
|
|
|
|
library_filter_->SetSettingsGroup(kSettingsGroup);
|
|
|
|
library_filter_->SetLibraryModel(library_model_);
|
|
|
|
library_filter_->SetFilterHint(tr("Search Subsonic"));
|
|
|
|
library_filter_->SetAgeFilterEnabled(false);
|
|
|
|
|
2013-01-10 23:08:52 +01:00
|
|
|
library_sort_model_->setSourceModel(library_model_);
|
|
|
|
library_sort_model_->setSortRole(LibraryModel::Role_SortText);
|
|
|
|
library_sort_model_->setDynamicSortFilter(true);
|
2013-02-05 12:17:09 +01:00
|
|
|
library_sort_model_->setSortLocaleAware(true);
|
2013-01-10 23:08:52 +01:00
|
|
|
library_sort_model_->sort(0);
|
|
|
|
|
2012-01-20 00:22:50 +01:00
|
|
|
connect(this, SIGNAL(LoginStateChanged(SubsonicService::LoginState)),
|
2013-01-24 17:48:58 +01:00
|
|
|
SLOT(OnLoginStateChanged(SubsonicService::LoginState)));
|
2013-01-17 22:01:54 +01:00
|
|
|
|
|
|
|
context_menu_ = new QMenu;
|
|
|
|
context_menu_->addActions(GetPlaylistActions());
|
|
|
|
context_menu_->addSeparator();
|
2015-10-14 03:01:08 +02:00
|
|
|
context_menu_->addAction(IconLoader::Load("view-refresh", IconLoader::Base),
|
2014-02-07 16:34:20 +01:00
|
|
|
tr("Refresh catalogue"), this,
|
|
|
|
SLOT(ReloadDatabase()));
|
2013-01-24 17:48:58 +01:00
|
|
|
QAction* config_action = context_menu_->addAction(
|
2016-03-29 21:23:53 +02:00
|
|
|
IconLoader::Load("configure", IconLoader::Base), tr("Configure Subsonic..."),
|
2015-10-14 03:01:08 +02:00
|
|
|
this, SLOT(ShowConfig()));
|
2013-01-17 23:13:57 +01:00
|
|
|
context_menu_->addSeparator();
|
2013-01-17 22:01:54 +01:00
|
|
|
context_menu_->addMenu(library_filter_->menu());
|
2013-01-21 23:13:48 +01:00
|
|
|
|
|
|
|
library_filter_->AddMenuAction(config_action);
|
|
|
|
|
|
|
|
app_->global_search()->AddProvider(new LibrarySearchProvider(
|
2014-02-07 16:34:20 +01:00
|
|
|
library_backend_, tr("Subsonic"), "subsonic",
|
2015-10-14 03:01:08 +02:00
|
|
|
IconLoader::Load("subsonic", IconLoader::Provider), true, app_, this));
|
2011-12-06 00:10:25 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SubsonicService::~SubsonicService() {}
|
2011-12-06 00:10:25 +01:00
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
QStandardItem* SubsonicService::CreateRootItem() {
|
2016-03-29 21:23:53 +02:00
|
|
|
root_ = new QStandardItem(IconLoader::Load("subsonic", IconLoader::Provider),
|
2015-10-14 03:01:08 +02:00
|
|
|
kServiceName);
|
2013-01-17 22:01:54 +01:00
|
|
|
root_->setData(true, InternetModel::Role_CanLazyLoad);
|
|
|
|
return root_;
|
2011-12-06 00:10:25 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::LazyPopulate(QStandardItem* item) {
|
|
|
|
switch (item->data(InternetModel::Role_Type).toInt()) {
|
|
|
|
case InternetModel::Type_Service:
|
|
|
|
library_model_->Init();
|
|
|
|
if (login_state() != LoginState_Loggedin) {
|
|
|
|
ShowConfig();
|
|
|
|
} else if (total_song_count_ == 0 && !load_database_task_id_) {
|
|
|
|
ReloadDatabase();
|
|
|
|
}
|
|
|
|
model()->merged_model()->AddSubModel(item->index(), library_sort_model_);
|
|
|
|
break;
|
2011-12-06 00:10:25 +01:00
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
default:
|
|
|
|
break;
|
2011-12-09 01:13:17 +01:00
|
|
|
}
|
2011-12-06 00:10:25 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::ShowContextMenu(const QPoint& global_pos) {
|
2013-01-17 22:01:54 +01:00
|
|
|
const bool is_valid = model()->current_index().model() == library_sort_model_;
|
|
|
|
|
|
|
|
GetAppendToPlaylistAction()->setEnabled(is_valid);
|
|
|
|
GetReplacePlaylistAction()->setEnabled(is_valid);
|
|
|
|
GetOpenInNewPlaylistAction()->setEnabled(is_valid);
|
|
|
|
context_menu_->popup(global_pos);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QWidget* SubsonicService::HeaderWidget() const { return library_filter_; }
|
2013-01-17 22:01:54 +01:00
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::ReloadSettings() {
|
2011-12-07 19:06:11 +01:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
2013-07-03 03:16:41 +02:00
|
|
|
UpdateServer(s.value("server").toString());
|
2011-12-07 19:06:11 +01:00
|
|
|
username_ = s.value("username").toString();
|
|
|
|
password_ = s.value("password").toString();
|
2013-05-05 03:33:03 +02:00
|
|
|
usesslv3_ = s.value("usesslv3").toBool();
|
2018-05-23 15:23:53 +02:00
|
|
|
verifycert_ = s.value("verifycert", true).toBool();
|
2011-12-07 19:06:11 +01:00
|
|
|
|
|
|
|
Login();
|
|
|
|
}
|
|
|
|
|
2013-02-17 05:14:55 +01:00
|
|
|
bool SubsonicService::IsConfigured() const {
|
2014-02-07 16:34:20 +01:00
|
|
|
return !configured_server_.isEmpty() && !username_.isEmpty() &&
|
2013-02-17 05:14:55 +01:00
|
|
|
!password_.isEmpty();
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:15:56 +01:00
|
|
|
bool SubsonicService::IsAmpache() const { return is_ampache_; }
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::Login() {
|
2014-02-07 16:34:20 +01:00
|
|
|
// Recreate fresh network state, otherwise old HTTPS settings seem to get
|
|
|
|
// reused
|
2013-05-30 03:57:45 +02:00
|
|
|
network_->deleteLater();
|
2013-05-05 03:33:03 +02:00
|
|
|
network_ = new QNetworkAccessManager(this);
|
2011-12-07 19:06:11 +01:00
|
|
|
network_->setCookieJar(new QNetworkCookieJar(network_));
|
2011-12-08 21:00:50 +01:00
|
|
|
// Forget login state whilst waiting
|
|
|
|
login_state_ = LoginState_Unknown;
|
2013-02-17 05:14:55 +01:00
|
|
|
|
|
|
|
if (IsConfigured()) {
|
|
|
|
// Ping is enough to check credentials
|
|
|
|
Ping();
|
2013-06-11 03:57:57 +02:00
|
|
|
} else {
|
|
|
|
login_state_ = LoginState_IncompleteCredentials;
|
|
|
|
emit LoginStateChanged(login_state_);
|
2013-02-17 05:14:55 +01:00
|
|
|
}
|
2011-12-07 19:06:11 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SubsonicService::Login(const QString& server, const QString& username,
|
2018-05-23 15:23:53 +02:00
|
|
|
const QString& password, const bool& usesslv3,
|
|
|
|
const bool& verifycert) {
|
2013-07-03 03:16:41 +02:00
|
|
|
UpdateServer(server);
|
2013-01-24 17:48:58 +01:00
|
|
|
username_ = username;
|
|
|
|
password_ = password;
|
2013-05-05 03:33:03 +02:00
|
|
|
usesslv3_ = usesslv3;
|
2018-05-23 15:23:53 +02:00
|
|
|
verifycert_ = verifycert;
|
2011-12-07 19:06:11 +01:00
|
|
|
Login();
|
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::Ping() {
|
2013-01-15 00:36:23 +01:00
|
|
|
QNetworkReply* reply = Send(BuildRequestUrl("ping"));
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(OnPingFinished(QNetworkReply*)), reply);
|
2011-12-07 19:06:11 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
QUrl SubsonicService::BuildRequestUrl(const QString& view) const {
|
2013-07-03 03:16:41 +02:00
|
|
|
QUrl url(working_server_ + "/rest/" + view + ".view");
|
2015-04-11 22:52:31 +02:00
|
|
|
QUrlQuery url_query;
|
|
|
|
url_query.addQueryItem("v", kApiVersion);
|
|
|
|
url_query.addQueryItem("c", kApiClientName);
|
|
|
|
url_query.addQueryItem("u", username_);
|
|
|
|
url_query.addQueryItem("p", QString("enc:" + password_.toUtf8().toHex()));
|
|
|
|
url.setQuery(url_query);
|
2011-12-06 00:10:25 +01:00
|
|
|
return url;
|
|
|
|
}
|
2011-12-07 19:06:11 +01:00
|
|
|
|
2013-07-03 03:16:41 +02:00
|
|
|
QUrl SubsonicService::ScrubUrl(const QUrl& url) {
|
|
|
|
QUrl return_url(url);
|
|
|
|
QString path = url.path();
|
|
|
|
int rest_location = path.lastIndexOf("/rest", -1, Qt::CaseInsensitive);
|
|
|
|
if (rest_location >= 0) {
|
|
|
|
return_url.setPath(path.left(rest_location));
|
|
|
|
}
|
|
|
|
return return_url;
|
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
QNetworkReply* SubsonicService::Send(const QUrl& url) {
|
2012-01-17 00:00:39 +01:00
|
|
|
QNetworkRequest request(url);
|
|
|
|
// Don't try and check the authenticity of the SSL certificate - it'll almost
|
|
|
|
// certainly be self-signed.
|
|
|
|
QSslConfiguration sslconfig = QSslConfiguration::defaultConfiguration();
|
2018-05-23 15:23:53 +02:00
|
|
|
sslconfig.setPeerVerifyMode(verifycert_ ? QSslSocket::VerifyPeer
|
|
|
|
: QSslSocket::VerifyNone);
|
2013-05-05 03:33:03 +02:00
|
|
|
if (usesslv3_) {
|
|
|
|
sslconfig.setProtocol(QSsl::SslV3);
|
|
|
|
}
|
2012-01-17 00:00:39 +01:00
|
|
|
request.setSslConfiguration(sslconfig);
|
2014-02-07 16:34:20 +01:00
|
|
|
QNetworkReply* reply = network_->get(request);
|
2013-01-15 00:36:23 +01:00
|
|
|
return reply;
|
2011-12-10 20:04:04 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::UpdateTotalSongCount(int count) {
|
2013-01-21 00:25:54 +01:00
|
|
|
total_song_count_ = count;
|
2013-01-17 23:13:57 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::ReloadDatabase() {
|
|
|
|
if (!load_database_task_id_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
load_database_task_id_ =
|
|
|
|
app_->task_manager()->StartTask(tr("Fetching Subsonic library"));
|
2013-01-24 17:48:58 +01:00
|
|
|
}
|
2013-01-17 23:13:57 +01:00
|
|
|
scanner_->Scan();
|
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::ReloadDatabaseFinished() {
|
2013-01-17 23:13:57 +01:00
|
|
|
app_->task_manager()->SetTaskFinished(load_database_task_id_);
|
|
|
|
load_database_task_id_ = 0;
|
|
|
|
|
|
|
|
library_backend_->DeleteAll();
|
|
|
|
library_backend_->AddOrUpdateSongs(scanner_->GetSongs());
|
2013-01-18 11:35:47 +01:00
|
|
|
library_model_->Reset();
|
2013-01-17 23:13:57 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SubsonicService::OnLoginStateChanged(
|
|
|
|
SubsonicService::LoginState newstate) {
|
2014-12-17 19:02:21 +01:00
|
|
|
// TODO(Alan Briolat): library refresh logic?
|
2015-05-20 13:50:17 +02:00
|
|
|
if (newstate != LoginState_Loggedin) library_backend_->DeleteAll();
|
2012-01-20 00:22:50 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::OnPingFinished(QNetworkReply* reply) {
|
2011-12-07 19:06:11 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2014-02-07 16:34:20 +01:00
|
|
|
switch (reply->error()) {
|
2013-05-08 04:33:45 +02:00
|
|
|
case QNetworkReply::ConnectionRefusedError:
|
|
|
|
login_state_ = LoginState_ConnectionRefused;
|
|
|
|
break;
|
|
|
|
case QNetworkReply::HostNotFoundError:
|
|
|
|
login_state_ = LoginState_HostNotFound;
|
|
|
|
break;
|
|
|
|
case QNetworkReply::TimeoutError:
|
|
|
|
login_state_ = LoginState_Timeout;
|
|
|
|
break;
|
|
|
|
case QNetworkReply::SslHandshakeFailedError:
|
|
|
|
login_state_ = LoginState_SslError;
|
|
|
|
break;
|
2014-02-07 16:34:20 +01:00
|
|
|
default: // Treat uncaught error types here as generic
|
2013-05-08 04:33:45 +02:00
|
|
|
login_state_ = LoginState_BadServer;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-16 15:22:30 +01:00
|
|
|
qLog(Error) << "Failed to connect ("
|
2014-02-07 16:34:20 +01:00
|
|
|
<< Utilities::EnumToString(QNetworkReply::staticMetaObject,
|
|
|
|
"NetworkError", reply->error())
|
2012-01-16 15:22:30 +01:00
|
|
|
<< "):" << reply->errorString();
|
2013-01-24 17:48:58 +01:00
|
|
|
} else {
|
2011-12-07 19:06:11 +01:00
|
|
|
QXmlStreamReader reader(reply);
|
|
|
|
reader.readNextStartElement();
|
2015-12-08 23:15:56 +01:00
|
|
|
is_ampache_ = (reader.attributes().value("type") == "ampache");
|
2011-12-07 19:06:11 +01:00
|
|
|
QStringRef status = reader.attributes().value("status");
|
2014-02-07 16:34:20 +01:00
|
|
|
int http_status_code =
|
|
|
|
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2013-01-24 17:48:58 +01:00
|
|
|
if (status == "ok") {
|
2011-12-07 19:06:11 +01:00
|
|
|
login_state_ = LoginState_Loggedin;
|
2013-07-03 03:16:41 +02:00
|
|
|
} else if (http_status_code >= 300 && http_status_code <= 399) {
|
|
|
|
// Received a redirect status code, follow up on it.
|
|
|
|
QUrl redirect_url =
|
|
|
|
reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
|
|
|
|
if (redirect_url.isEmpty()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Debug) << "Received HTTP code " << http_status_code
|
|
|
|
<< ", but no URL";
|
2013-07-03 03:16:41 +02:00
|
|
|
login_state_ = LoginState_RedirectNoUrl;
|
|
|
|
} else {
|
|
|
|
redirect_count_++;
|
2019-08-22 05:43:16 +02:00
|
|
|
qLog(Debug) << "Redirect received to "
|
2013-07-03 03:16:41 +02:00
|
|
|
<< redirect_url.toString(QUrl::RemoveQuery)
|
2014-02-07 16:34:20 +01:00
|
|
|
<< ", current redirect count is " << redirect_count_;
|
2013-07-03 03:16:41 +02:00
|
|
|
if (redirect_count_ <= kMaxRedirects) {
|
|
|
|
working_server_ = ScrubUrl(redirect_url).toString(QUrl::RemoveQuery);
|
|
|
|
Ping();
|
2014-02-07 16:34:20 +01:00
|
|
|
// To avoid the LoginStateChanged, as it will come from the recursive
|
|
|
|
// request.
|
2013-07-03 03:16:41 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Redirect limit exceeded
|
|
|
|
login_state_ = LoginState_RedirectLimitExceeded;
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 17:48:58 +01:00
|
|
|
} else {
|
2011-12-07 19:06:11 +01:00
|
|
|
reader.readNextStartElement();
|
|
|
|
int error = reader.attributes().value("code").toString().toInt();
|
2012-01-16 15:31:45 +01:00
|
|
|
qLog(Error) << "Subsonic error ("
|
2014-02-07 16:34:20 +01:00
|
|
|
<< Utilities::EnumToString(SubsonicService::staticMetaObject,
|
|
|
|
"ApiError", error)
|
2012-01-16 15:31:45 +01:00
|
|
|
<< "):" << reader.attributes().value("message").toString();
|
2013-01-24 17:48:58 +01:00
|
|
|
switch (error) {
|
|
|
|
// "Parameter missing" for "ping" is always blank username or password
|
|
|
|
case ApiError_ParameterMissing:
|
|
|
|
case ApiError_BadCredentials:
|
|
|
|
login_state_ = LoginState_BadCredentials;
|
|
|
|
break;
|
|
|
|
case ApiError_OutdatedClient:
|
|
|
|
login_state_ = LoginState_OutdatedClient;
|
|
|
|
break;
|
|
|
|
case ApiError_OutdatedServer:
|
|
|
|
login_state_ = LoginState_OutdatedServer;
|
|
|
|
break;
|
|
|
|
case ApiError_Unlicensed:
|
|
|
|
login_state_ = LoginState_Unlicensed;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
login_state_ = LoginState_OtherError;
|
|
|
|
break;
|
2011-12-07 19:06:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 15:22:30 +01:00
|
|
|
qLog(Debug) << "Login state changed:"
|
2014-02-07 16:34:20 +01:00
|
|
|
<< Utilities::EnumToString(SubsonicService::staticMetaObject,
|
|
|
|
"LoginState", login_state_);
|
2011-12-08 21:00:50 +01:00
|
|
|
emit LoginStateChanged(login_state_);
|
2011-12-07 19:06:11 +01:00
|
|
|
}
|
2011-12-09 01:13:17 +01:00
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicService::ShowConfig() {
|
2013-01-21 23:13:48 +01:00
|
|
|
app_->OpenSettingsDialogAtPage(SettingsDialog::Page_Subsonic);
|
|
|
|
}
|
|
|
|
|
2013-07-03 03:16:41 +02:00
|
|
|
void SubsonicService::UpdateServer(const QString& server) {
|
|
|
|
configured_server_ = server;
|
|
|
|
working_server_ = server;
|
|
|
|
redirect_count_ = 0;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
const int SubsonicLibraryScanner::kAlbumChunkSize = 500;
|
|
|
|
const int SubsonicLibraryScanner::kConcurrentRequests = 8;
|
2016-03-29 21:23:53 +02:00
|
|
|
const int SubsonicLibraryScanner::kCoverArtSize = 1024;
|
2013-01-15 00:36:23 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SubsonicLibraryScanner::SubsonicLibraryScanner(SubsonicService* service,
|
|
|
|
QObject* parent)
|
|
|
|
: QObject(parent), service_(service), scanning_(false) {}
|
2013-01-15 00:36:23 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SubsonicLibraryScanner::~SubsonicLibraryScanner() {}
|
2013-01-15 00:36:23 +01:00
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicLibraryScanner::Scan() {
|
|
|
|
if (scanning_) {
|
2013-01-17 23:13:57 +01:00
|
|
|
return;
|
2013-01-24 17:48:58 +01:00
|
|
|
}
|
2013-01-17 23:13:57 +01:00
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
album_queue_.clear();
|
2013-01-17 23:13:57 +01:00
|
|
|
pending_requests_.clear();
|
|
|
|
songs_.clear();
|
|
|
|
scanning_ = true;
|
2013-01-15 00:36:23 +01:00
|
|
|
GetAlbumList(0);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SubsonicLibraryScanner::OnGetAlbumListFinished(QNetworkReply* reply,
|
|
|
|
int offset) {
|
2011-12-10 20:04:04 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2015-05-15 16:59:43 +02:00
|
|
|
bool skip_read_albums = false;
|
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
QXmlStreamReader reader(reply);
|
2011-12-10 20:04:04 +01:00
|
|
|
reader.readNextStartElement();
|
2015-05-20 13:50:17 +02:00
|
|
|
|
|
|
|
if (reader.name() != "subsonic-response") {
|
|
|
|
ParsingError("Not a subsonic-response. Aborting scan.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
if (reader.attributes().value("status") != "ok") {
|
2014-07-16 18:49:58 +02:00
|
|
|
reader.readNextStartElement();
|
|
|
|
int error = reader.attributes().value("code").toString().toInt();
|
|
|
|
|
|
|
|
// Compatibility with Ampache :
|
|
|
|
// When there is no data, Ampache returns NotFound
|
|
|
|
// whereas Subsonic returns empty albumList2 tag
|
|
|
|
switch (error) {
|
|
|
|
case SubsonicService::ApiError_NotFound:
|
2015-05-15 16:59:43 +02:00
|
|
|
skip_read_albums = true;
|
2014-07-16 18:49:58 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-05-20 13:50:17 +02:00
|
|
|
ParsingError("Response status not ok. Aborting scan.");
|
2014-07-16 18:49:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-12-10 20:04:04 +01:00
|
|
|
}
|
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
int albums_added = 0;
|
2015-05-20 13:50:17 +02:00
|
|
|
if (!skip_read_albums) {
|
2015-05-15 16:59:43 +02:00
|
|
|
reader.readNextStartElement();
|
2015-05-20 13:50:17 +02:00
|
|
|
if (reader.name() != "albumList2") {
|
|
|
|
ParsingError("albumList2 tag expected. Aborting scan.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:59:43 +02:00
|
|
|
while (reader.readNextStartElement()) {
|
2015-05-20 13:50:17 +02:00
|
|
|
if (reader.name() != "album") {
|
|
|
|
ParsingError("album tag expected. Aborting scan.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-15 16:59:43 +02:00
|
|
|
album_queue_ << reader.attributes().value("id").toString();
|
|
|
|
albums_added++;
|
|
|
|
reader.skipCurrentElement();
|
|
|
|
}
|
2011-12-10 20:04:04 +01:00
|
|
|
}
|
2013-01-10 23:08:52 +01:00
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
if (albums_added > 0) {
|
2013-01-17 23:13:57 +01:00
|
|
|
// Non-empty reply means potentially more albums to fetch
|
2013-01-15 00:36:23 +01:00
|
|
|
GetAlbumList(offset + kAlbumChunkSize);
|
2019-02-22 18:44:39 +01:00
|
|
|
} else if (album_queue_.empty()) {
|
2013-01-17 23:13:57 +01:00
|
|
|
// Empty reply and no albums means an empty Subsonic server
|
|
|
|
scanning_ = false;
|
2015-02-06 11:23:57 +01:00
|
|
|
emit ScanFinished();
|
2013-01-15 00:36:23 +01:00
|
|
|
} else {
|
2013-01-17 23:13:57 +01:00
|
|
|
// Empty reply but we have some albums, time to start fetching songs
|
2014-02-07 16:34:20 +01:00
|
|
|
// Start up the maximum number of concurrent requests, finished requests get
|
|
|
|
// replaced with new ones
|
2013-01-15 00:36:23 +01:00
|
|
|
for (int i = 0; i < kConcurrentRequests && !album_queue_.empty(); ++i) {
|
|
|
|
GetAlbum(album_queue_.dequeue());
|
|
|
|
}
|
2013-01-12 00:14:15 +01:00
|
|
|
}
|
2011-12-10 20:04:04 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicLibraryScanner::OnGetAlbumFinished(QNetworkReply* reply) {
|
2011-12-09 01:13:17 +01:00
|
|
|
reply->deleteLater();
|
2013-01-17 23:13:57 +01:00
|
|
|
pending_requests_.remove(reply);
|
2013-01-12 00:14:15 +01:00
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
QXmlStreamReader reader(reply);
|
2011-12-09 01:13:17 +01:00
|
|
|
reader.readNextStartElement();
|
2015-05-20 13:50:17 +02:00
|
|
|
|
|
|
|
if (reader.name() != "subsonic-response") {
|
|
|
|
ParsingError("Not a subsonic-response. Aborting scan.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
if (reader.attributes().value("status") != "ok") {
|
2014-12-17 19:02:21 +01:00
|
|
|
// TODO(Alan Briolat): error handling
|
2011-12-09 01:13:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 21:18:26 +01:00
|
|
|
// Read album information
|
2011-12-09 01:13:17 +01:00
|
|
|
reader.readNextStartElement();
|
2015-05-20 13:50:17 +02:00
|
|
|
if (reader.name() != "album") {
|
|
|
|
ParsingError("album tag expected. Aborting scan.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 21:18:26 +01:00
|
|
|
QString album_artist = reader.attributes().value("artist").toString();
|
|
|
|
|
|
|
|
// Read song information
|
2013-01-15 00:36:23 +01:00
|
|
|
while (reader.readNextStartElement()) {
|
2015-05-20 13:50:17 +02:00
|
|
|
if (reader.name() != "song") {
|
|
|
|
ParsingError("song tag expected. Aborting scan.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:36:23 +01:00
|
|
|
Song song;
|
|
|
|
QString id = reader.attributes().value("id").toString();
|
|
|
|
song.set_title(reader.attributes().value("title").toString());
|
|
|
|
song.set_album(reader.attributes().value("album").toString());
|
|
|
|
song.set_track(reader.attributes().value("track").toString().toInt());
|
2013-06-17 02:24:51 +02:00
|
|
|
song.set_disc(reader.attributes().value("discNumber").toString().toInt());
|
2013-01-15 00:36:23 +01:00
|
|
|
song.set_artist(reader.attributes().value("artist").toString());
|
2013-01-17 21:18:26 +01:00
|
|
|
song.set_albumartist(album_artist);
|
2013-01-15 00:36:23 +01:00
|
|
|
song.set_bitrate(reader.attributes().value("bitRate").toString().toInt());
|
|
|
|
song.set_year(reader.attributes().value("year").toString().toInt());
|
|
|
|
song.set_genre(reader.attributes().value("genre").toString());
|
|
|
|
qint64 length = reader.attributes().value("duration").toString().toInt();
|
2013-01-24 17:48:58 +01:00
|
|
|
length *= kNsecPerSec;
|
2013-01-15 00:36:23 +01:00
|
|
|
song.set_length_nanosec(length);
|
2018-05-23 15:23:21 +02:00
|
|
|
QUrl url = QUrl(QString("subsonic://"));
|
|
|
|
QUrlQuery song_query(url.query());
|
|
|
|
song_query.addQueryItem("id", id);
|
|
|
|
url.setQuery(song_query);
|
2017-10-06 15:08:00 +02:00
|
|
|
QUrl cover_url = service_->BuildRequestUrl("getCoverArt");
|
2018-02-01 09:50:42 +01:00
|
|
|
QUrlQuery cover_url_query(url.query());
|
|
|
|
cover_url_query.addQueryItem("id", id);
|
|
|
|
cover_url.setQuery(cover_url_query);
|
2017-10-06 15:08:00 +02:00
|
|
|
song.set_art_automatic(cover_url.toEncoded());
|
2013-01-15 00:36:23 +01:00
|
|
|
song.set_url(url);
|
|
|
|
song.set_filesize(reader.attributes().value("size").toString().toInt());
|
|
|
|
// We need to set these to satisfy the database constraints
|
|
|
|
song.set_directory_id(0);
|
|
|
|
song.set_mtime(0);
|
|
|
|
song.set_ctime(0);
|
2015-12-08 23:15:56 +01:00
|
|
|
|
|
|
|
if (reader.attributes().hasAttribute("playCount")) {
|
|
|
|
song.set_playcount(
|
|
|
|
reader.attributes().value("playCount").toString().toInt());
|
|
|
|
}
|
|
|
|
|
2013-01-17 23:13:57 +01:00
|
|
|
songs_ << song;
|
2013-01-15 00:36:23 +01:00
|
|
|
reader.skipCurrentElement();
|
2011-12-09 01:13:17 +01:00
|
|
|
}
|
2013-01-12 00:14:15 +01:00
|
|
|
|
2013-01-17 23:13:57 +01:00
|
|
|
// Start the next request if albums remain
|
2013-01-24 17:48:58 +01:00
|
|
|
if (!album_queue_.empty()) {
|
2013-01-15 00:36:23 +01:00
|
|
|
GetAlbum(album_queue_.dequeue());
|
2013-01-24 17:48:58 +01:00
|
|
|
}
|
2013-01-15 00:36:23 +01:00
|
|
|
|
2013-01-17 23:13:57 +01:00
|
|
|
// If this was the last response, we're done!
|
|
|
|
if (album_queue_.empty() && pending_requests_.empty()) {
|
|
|
|
scanning_ = false;
|
|
|
|
emit ScanFinished();
|
2013-01-15 00:36:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicLibraryScanner::GetAlbumList(int offset) {
|
2013-01-15 00:36:23 +01:00
|
|
|
QUrl url = service_->BuildRequestUrl("getAlbumList2");
|
2016-04-19 22:50:21 +02:00
|
|
|
QUrlQuery url_query(url.query());
|
2015-04-11 22:52:31 +02:00
|
|
|
url_query.addQueryItem("type", "alphabeticalByName");
|
|
|
|
url_query.addQueryItem("size", QString::number(kAlbumChunkSize));
|
|
|
|
url_query.addQueryItem("offset", QString::number(offset));
|
|
|
|
url.setQuery(url_query);
|
2013-01-15 00:36:23 +01:00
|
|
|
QNetworkReply* reply = service_->Send(url);
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(OnGetAlbumListFinished(QNetworkReply*, int)), reply, offset);
|
2013-01-15 00:36:23 +01:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:48:58 +01:00
|
|
|
void SubsonicLibraryScanner::GetAlbum(const QString& id) {
|
2013-01-15 00:36:23 +01:00
|
|
|
QUrl url = service_->BuildRequestUrl("getAlbum");
|
2016-04-19 22:50:21 +02:00
|
|
|
QUrlQuery url_query(url.query());
|
2015-04-11 22:52:31 +02:00
|
|
|
url_query.addQueryItem("id", id);
|
2015-12-08 23:15:56 +01:00
|
|
|
if (service_->IsAmpache()) {
|
2015-12-13 20:05:12 +01:00
|
|
|
url_query.addQueryItem("ampache", "1");
|
2015-12-08 23:15:56 +01:00
|
|
|
}
|
2015-04-11 22:52:31 +02:00
|
|
|
url.setQuery(url_query);
|
2013-01-15 00:36:23 +01:00
|
|
|
QNetworkReply* reply = service_->Send(url);
|
2014-02-07 16:34:20 +01:00
|
|
|
NewClosure(reply, SIGNAL(finished()), this,
|
|
|
|
SLOT(OnGetAlbumFinished(QNetworkReply*)), reply);
|
2013-01-17 23:13:57 +01:00
|
|
|
pending_requests_.insert(reply);
|
2011-12-09 01:13:17 +01:00
|
|
|
}
|
2015-05-20 13:50:17 +02:00
|
|
|
|
|
|
|
void SubsonicLibraryScanner::ParsingError(const QString& message) {
|
|
|
|
qLog(Warning) << "Subsonic parsing error: " << message;
|
|
|
|
scanning_ = false;
|
|
|
|
emit ScanFinished();
|
|
|
|
}
|