Make it possible to obtain list of streams from MPD server via http
This commit is contained in:
@@ -32,6 +32,7 @@
|
||||
#include <qglobal.h>
|
||||
#include <QXmlStreamReader>
|
||||
#include <QXmlStreamWriter>
|
||||
#include <QNetworkReply>
|
||||
#include "config.h"
|
||||
#include "settings.h"
|
||||
#if defined Q_OS_WIN
|
||||
@@ -46,6 +47,7 @@
|
||||
#include "icons.h"
|
||||
#include "utils.h"
|
||||
#include "qtiocompressor/qtiocompressor.h"
|
||||
#include "networkaccessmanager.h"
|
||||
|
||||
#ifdef ENABLE_KDE_SUPPORT
|
||||
K_GLOBAL_STATIC(StreamsModel, instance)
|
||||
@@ -111,6 +113,9 @@ static const QLatin1String constStreamsOldFileName("streams.xml");
|
||||
|
||||
static void convertOldFile(const QString &compressedName)
|
||||
{
|
||||
if (compressedName.startsWith("http:/")) {
|
||||
return;
|
||||
}
|
||||
QString prev=compressedName;
|
||||
prev.replace(constStreamsCompressedFileName, constStreamsOldFileName);
|
||||
|
||||
@@ -149,6 +154,7 @@ StreamsModel::StreamsModel()
|
||||
: QAbstractItemModel(0)
|
||||
, modified(false)
|
||||
, timer(0)
|
||||
, job(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -299,7 +305,17 @@ void StreamsModel::save(bool force)
|
||||
bool StreamsModel::load(const QString &filename, bool isInternal)
|
||||
{
|
||||
if (isInternal) {
|
||||
convertOldFile(filename);
|
||||
if (filename.startsWith("http:/")) {
|
||||
if (job) {
|
||||
return false;
|
||||
}
|
||||
emit downloading(true);
|
||||
job=NetworkAccessManager::self()->get(QUrl(filename));
|
||||
connect(job, SIGNAL(finished()), SLOT(downloadFinished()));
|
||||
return true;
|
||||
} else {
|
||||
convertOldFile(filename);
|
||||
}
|
||||
}
|
||||
|
||||
QFile file(filename);
|
||||
@@ -319,7 +335,38 @@ bool StreamsModel::load(const QString &filename, bool isInternal)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
QXmlStreamReader doc(isCompressed ? (QIODevice *)&compressor : (QIODevice *)&file);
|
||||
return load(isCompressed ? (QIODevice *)&compressor : (QIODevice *)&file, isInternal);
|
||||
}
|
||||
|
||||
void StreamsModel::downloadFinished()
|
||||
{
|
||||
QNetworkReply *reply=qobject_cast<QNetworkReply *>(sender());
|
||||
|
||||
if (reply==job) {
|
||||
job=0;
|
||||
if(QNetworkReply::NoError==reply->error()) {
|
||||
QtIOCompressor comp(reply);
|
||||
comp.setStreamFormat(QtIOCompressor::GzipFormat);
|
||||
if (comp.open(QIODevice::ReadOnly)) {
|
||||
beginResetModel();
|
||||
if (!load(&comp, true)) {
|
||||
emit error(i18n("Failed to parse downloaded stream list."));
|
||||
}
|
||||
endResetModel();
|
||||
} else {
|
||||
emit error(i18n("Failed to read downloaded stream list."));
|
||||
}
|
||||
} else {
|
||||
emit error(i18n("Failed to download stream list."));
|
||||
}
|
||||
emit downloading(false);
|
||||
}
|
||||
reply->deleteLater();
|
||||
}
|
||||
|
||||
bool StreamsModel::load(QIODevice *dev, bool isInternal)
|
||||
{
|
||||
QXmlStreamReader doc(dev);
|
||||
bool haveInserted=false;
|
||||
int level=0;
|
||||
CategoryItem *cat=0;
|
||||
|
||||
@@ -30,6 +30,8 @@
|
||||
#include <QSet>
|
||||
|
||||
class QTimer;
|
||||
class QIODevice;
|
||||
class QNetworkReply;
|
||||
|
||||
class StreamsModel : public QAbstractItemModel
|
||||
{
|
||||
@@ -110,12 +112,17 @@ public:
|
||||
void setWritable(bool w) { writable=w; }
|
||||
|
||||
Q_SIGNALS:
|
||||
void downloading(bool);
|
||||
void updateGenres(const QSet<QString> &genres);
|
||||
void error(const QString &e);
|
||||
|
||||
private Q_SLOTS:
|
||||
void downloadFinished();
|
||||
|
||||
private:
|
||||
void clearCategories();
|
||||
bool load(const QString &filename, bool isInternal);
|
||||
bool load(QIODevice *dev, bool isInternal);
|
||||
CategoryItem * getCategory(const QString &name, bool create=false, bool signal=false);
|
||||
QString name(CategoryItem *cat, const QString &url);
|
||||
bool entryExists(CategoryItem *cat, const QString &name, const QUrl &url=QUrl()) { return 0!=getStream(cat, name, url); }
|
||||
@@ -129,6 +136,7 @@ private:
|
||||
bool writable;
|
||||
bool modified;
|
||||
QTimer *timer;
|
||||
QNetworkReply *job;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -277,6 +277,7 @@ StreamsPage::StreamsPage(MainWindow *p)
|
||||
connect(genreCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(searchItems()));
|
||||
connect(StreamsModel::self(), SIGNAL(updateGenres(const QSet<QString> &)), genreCombo, SLOT(update(const QSet<QString> &)));
|
||||
connect(StreamsModel::self(), SIGNAL(error(const QString &)), mw, SLOT(showError(QString)));
|
||||
connect(StreamsModel::self(), SIGNAL(downloading(bool)), this, SLOT(downloading(bool)));
|
||||
connect(MPDConnection::self(), SIGNAL(dirChanged()), SLOT(mpdDirChanged()));
|
||||
Icon::init(menuButton);
|
||||
menuButton->setPopupMode(QToolButton::InstantPopup);
|
||||
@@ -337,7 +338,7 @@ void StreamsPage::mpdDirChanged()
|
||||
|
||||
void StreamsPage::checkWriteable()
|
||||
{
|
||||
bool dirWritable=QFileInfo(StreamsModel::dir()).isWritable();
|
||||
bool dirWritable=!StreamsModel::dir().startsWith("http:/") && QFileInfo(StreamsModel::dir()).isWritable();
|
||||
infoMsg->setVisible(!dirWritable);
|
||||
if (dirWritable!=StreamsModel::self()->isWritable()) {
|
||||
StreamsModel::self()->setWritable(dirWritable);
|
||||
@@ -396,6 +397,15 @@ void StreamsPage::itemDoubleClicked(const QModelIndex &index)
|
||||
}
|
||||
}
|
||||
|
||||
void StreamsPage::downloading(bool dl)
|
||||
{
|
||||
if (dl) {
|
||||
view->showSpinner();
|
||||
} else {
|
||||
view->hideSpinner();
|
||||
}
|
||||
}
|
||||
|
||||
void StreamsPage::downloadFinished()
|
||||
{
|
||||
QNetworkReply *reply=qobject_cast<QNetworkReply *>(sender());
|
||||
|
||||
@@ -78,6 +78,7 @@ private Q_SLOTS:
|
||||
void edit();
|
||||
void searchItems();
|
||||
void itemDoubleClicked(const QModelIndex &index);
|
||||
void downloading(bool dl);
|
||||
|
||||
private:
|
||||
void addItemsToPlayQueue(const QModelIndexList &indexes, bool replace, quint8 priorty=0);
|
||||
|
||||
Reference in New Issue
Block a user