2016-03-03 22:21:15 +01:00
|
|
|
#ifndef DBQUERY_H
|
|
|
|
#define DBQUERY_H
|
|
|
|
|
|
|
|
#include <c++utilities/application/global.h>
|
2016-11-23 21:46:33 +01:00
|
|
|
#include <c++utilities/conversion/types.h>
|
2016-03-03 22:21:15 +01:00
|
|
|
|
|
|
|
#include <QAbstractTableModel>
|
2016-11-10 23:45:08 +01:00
|
|
|
#include <QNetworkReply>
|
2016-03-03 22:21:15 +01:00
|
|
|
|
2016-10-09 22:41:34 +02:00
|
|
|
#ifdef DEBUG_BUILD
|
|
|
|
# include <iostream>
|
|
|
|
#endif
|
|
|
|
|
2016-03-06 17:52:33 +01:00
|
|
|
QT_FORWARD_DECLARE_CLASS(QNetworkReply)
|
|
|
|
|
2016-03-03 22:21:15 +01:00
|
|
|
namespace Media {
|
|
|
|
class TagValue;
|
2016-07-11 21:34:05 +02:00
|
|
|
DECLARE_ENUM_CLASS(KnownField, unsigned int);
|
2016-03-03 22:21:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace QtGui {
|
|
|
|
|
|
|
|
struct SongDescription
|
|
|
|
{
|
|
|
|
SongDescription();
|
|
|
|
|
|
|
|
QString title;
|
|
|
|
QString album;
|
2016-03-06 17:52:33 +01:00
|
|
|
QString albumId;
|
2016-03-03 22:21:15 +01:00
|
|
|
QString artist;
|
|
|
|
QString year;
|
|
|
|
QString genre;
|
2016-11-23 21:46:33 +01:00
|
|
|
int32 track;
|
|
|
|
int32 totalTracks;
|
|
|
|
int32 disk;
|
2016-03-06 17:52:33 +01:00
|
|
|
QByteArray cover;
|
2016-10-09 22:41:34 +02:00
|
|
|
QString lyrics;
|
2016-03-03 22:21:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class QueryResultsModel : public QAbstractTableModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum Column {
|
|
|
|
TitleCol,
|
|
|
|
AlbumCol,
|
|
|
|
ArtistCol,
|
|
|
|
GenreCol,
|
|
|
|
YearCol,
|
|
|
|
TrackCol,
|
|
|
|
TotalTracksCol
|
|
|
|
};
|
|
|
|
|
|
|
|
const QList<SongDescription> &results() const;
|
|
|
|
const QStringList &errorList() const;
|
|
|
|
bool areResultsAvailable() const;
|
2016-03-06 17:52:33 +01:00
|
|
|
bool isFetchingCover() const;
|
2016-03-03 22:21:15 +01:00
|
|
|
Media::TagValue fieldValue(int row, Media::KnownField knownField) const;
|
|
|
|
|
2016-10-24 20:15:10 +02:00
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
2016-03-03 22:21:15 +01:00
|
|
|
Qt::ItemFlags flags(const QModelIndex &index) const;
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
2016-10-24 20:15:10 +02:00
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
int columnCount(const QModelIndex &parent = QModelIndex()) const;
|
2016-03-06 17:52:33 +01:00
|
|
|
const QByteArray *cover(const QModelIndex &index) const;
|
|
|
|
virtual bool fetchCover(const QModelIndex &index);
|
2016-10-09 22:41:34 +02:00
|
|
|
const QString *lyrics(const QModelIndex &index) const;
|
|
|
|
virtual bool fetchLyrics(const QModelIndex &index);
|
2016-03-06 17:52:33 +01:00
|
|
|
virtual void abort();
|
2016-03-03 22:21:15 +01:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void resultsAvailable();
|
2016-03-06 17:52:33 +01:00
|
|
|
void coverAvailable(const QModelIndex &index);
|
2016-10-09 22:41:34 +02:00
|
|
|
void lyricsAvailable(const QModelIndex &index);
|
2016-03-03 22:21:15 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
QueryResultsModel(QObject *parent = nullptr);
|
|
|
|
void setResultsAvailable(bool resultsAvailable);
|
2016-03-06 17:52:33 +01:00
|
|
|
void setFetchingCover(bool fetchingCover);
|
2016-03-03 22:21:15 +01:00
|
|
|
|
|
|
|
QList<SongDescription> m_results;
|
|
|
|
QStringList m_errorList;
|
|
|
|
bool m_resultsAvailable;
|
2016-03-06 17:52:33 +01:00
|
|
|
bool m_fetchingCover;
|
2016-03-03 22:21:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline const QList<SongDescription> &QueryResultsModel::results() const
|
|
|
|
{
|
|
|
|
return m_results;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const QStringList &QueryResultsModel::errorList() const
|
|
|
|
{
|
|
|
|
return m_errorList;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool QueryResultsModel::areResultsAvailable() const
|
|
|
|
{
|
|
|
|
return m_resultsAvailable;
|
|
|
|
}
|
|
|
|
|
2016-03-06 17:52:33 +01:00
|
|
|
inline bool QueryResultsModel::isFetchingCover() const
|
|
|
|
{
|
|
|
|
return m_fetchingCover;
|
|
|
|
}
|
|
|
|
|
2016-10-09 19:44:06 +02:00
|
|
|
class HttpResultsModel : public QueryResultsModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
~HttpResultsModel();
|
|
|
|
void abort();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
HttpResultsModel(SongDescription &&initialSongDescription, QNetworkReply *reply);
|
2016-10-09 22:41:34 +02:00
|
|
|
template<class Object, class Function> void addReply(QNetworkReply *reply, Object object, Function handler);
|
|
|
|
template<class Function> void addReply(QNetworkReply *reply, Function handler);
|
|
|
|
virtual void parseInitialResults(const QByteArray &data) = 0;
|
|
|
|
QNetworkReply *evaluateReplyResults(QNetworkReply *reply, QByteArray &data, bool alwaysFollowRedirection = false);
|
2016-10-09 19:44:06 +02:00
|
|
|
|
|
|
|
private slots:
|
2016-10-09 22:41:34 +02:00
|
|
|
void handleInitialReplyFinished();
|
2016-10-09 19:44:06 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
QList<QNetworkReply *> m_replies;
|
|
|
|
const SongDescription m_initialDescription;
|
|
|
|
};
|
|
|
|
|
2016-10-09 22:41:34 +02:00
|
|
|
template<class Object, class Function>
|
|
|
|
inline void HttpResultsModel::addReply(QNetworkReply *reply, Object object, Function handler)
|
|
|
|
{
|
|
|
|
(m_replies << reply), connect(reply, &QNetworkReply::finished, object, handler);
|
|
|
|
#ifdef DEBUG_BUILD
|
2016-12-19 23:53:12 +01:00
|
|
|
std::cerr << "HTTP query: " << reply->url().toString().toUtf8().data() << std::endl;
|
2016-10-09 22:41:34 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Adds a reply.
|
|
|
|
* \remarks Called within c'tor and handleReplyFinished() in case of redirection. Might be called when subclassing to do further requests.
|
|
|
|
*/
|
|
|
|
template<class Function>
|
|
|
|
inline void HttpResultsModel::addReply(QNetworkReply *reply, Function handler)
|
|
|
|
{
|
|
|
|
(m_replies << reply), connect(reply, &QNetworkReply::finished, handler);
|
|
|
|
#ifdef DEBUG_BUILD
|
2016-12-19 23:53:12 +01:00
|
|
|
std::cerr << "HTTP query: " << reply->url().toString().toUtf8().data() << std::endl;
|
2016-10-09 22:41:34 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-09 19:44:06 +02:00
|
|
|
QueryResultsModel *queryMusicBrainz(SongDescription &&songDescription);
|
|
|
|
QueryResultsModel *queryLyricsWikia(SongDescription &&songDescription);
|
2016-03-06 17:52:33 +01:00
|
|
|
QNetworkReply *queryCoverArtArchive(const QString &albumId);
|
2016-03-03 22:21:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DBQUERY_H
|