Improve dbquery code

This commit is contained in:
Martchus 2019-04-21 17:08:24 +02:00
parent 4e4f7f5b44
commit 258ebe7255
5 changed files with 75 additions and 51 deletions

View File

@ -110,8 +110,8 @@ inline bool QueryResultsModel::isFetchingCover() const
class HttpResultsModel : public QueryResultsModel { class HttpResultsModel : public QueryResultsModel {
Q_OBJECT Q_OBJECT
public: public:
~HttpResultsModel(); ~HttpResultsModel() override;
void abort(); void abort() override;
protected: protected:
HttpResultsModel(SongDescription &&initialSongDescription, QNetworkReply *reply); HttpResultsModel(SongDescription &&initialSongDescription, QNetworkReply *reply);

View File

@ -33,55 +33,72 @@ LyricsWikiaResultsModel::LyricsWikiaResultsModel(SongDescription &&initialSongDe
bool LyricsWikiaResultsModel::fetchCover(const QModelIndex &index) bool LyricsWikiaResultsModel::fetchCover(const QModelIndex &index)
{ {
// FIXME: avoid code duplication with musicbrainz.cpp
// find song description
if (index.parent().isValid() || index.row() >= m_results.size()) { if (index.parent().isValid() || index.row() >= m_results.size()) {
return true; return true;
} }
SongDescription &desc = m_results[index.row()]; SongDescription &desc = m_results[index.row()];
// skip if cover is already available
if (!desc.cover.isEmpty()) { if (!desc.cover.isEmpty()) {
// cover is already available -> nothing to do
return true; return true;
} }
// fail if album ID is unknown
if (desc.albumId.isEmpty()) { if (desc.albumId.isEmpty()) {
m_errorList << tr("Unable to fetch cover: Album ID unknown"); m_errorList << tr("Unable to fetch cover: Album ID unknown");
emit resultsAvailable(); emit resultsAvailable();
return true; return true;
} }
try {
// the item belongs to an album which cover has already been fetched // skip if the item belongs to an album which cover has already been fetched
desc.cover = m_coverData.at(desc.albumId); const auto coverData = m_coverData.find(desc.albumId);
} catch (const out_of_range &) { if (coverData != m_coverData.end()) {
if (desc.coverUrl.isEmpty()) { desc.cover = coverData->second;
// request the cover URL return true;
auto *const reply = requestAlbumDetails(desc);
addReply(reply, bind(&LyricsWikiaResultsModel::handleAlbumDetailsReplyFinished, this, reply, index.row()));
setFetchingCover(true);
} else {
// request the cover art
auto *const reply = networkAccessManager().get(QNetworkRequest(QUrl(desc.coverUrl)));
addReply(reply, bind(&LyricsWikiaResultsModel::handleCoverReplyFinished, this, reply, desc.albumId, index.row()));
setFetchingCover(true);
}
return false;
} }
return true;
// start http request
if (desc.coverUrl.isEmpty()) {
// request the cover URL
auto *const reply = requestAlbumDetails(desc);
addReply(reply, bind(&LyricsWikiaResultsModel::handleAlbumDetailsReplyFinished, this, reply, index.row()));
setFetchingCover(true);
} else {
// request the cover art
auto *const reply = networkAccessManager().get(QNetworkRequest(QUrl(desc.coverUrl)));
addReply(reply, bind(&LyricsWikiaResultsModel::handleCoverReplyFinished, this, reply, desc.albumId, index.row()));
setFetchingCover(true);
}
return false;
} }
bool LyricsWikiaResultsModel::fetchLyrics(const QModelIndex &index) bool LyricsWikiaResultsModel::fetchLyrics(const QModelIndex &index)
{ {
if (!index.parent().isValid() && index.row() < m_results.size()) { // find song description
SongDescription &desc = m_results[index.row()]; if (index.parent().isValid() || index.row() >= m_results.size()) {
if (!desc.lyrics.isEmpty()) { return true;
// lyrics already available -> nothing to do
} else if (!desc.artist.isEmpty() && !desc.title.isEmpty()) {
auto *reply = requestSongDetails(desc);
addReply(reply, bind(&LyricsWikiaResultsModel::handleSongDetailsFinished, this, reply, index.row()));
return false;
} else {
m_errorList << tr("Unable to fetch lyrics: Artist or title is unknown.");
emit resultsAvailable();
}
} }
return true; SongDescription &desc = m_results[index.row()];
// skip if lyrics already present
if (!desc.lyrics.isEmpty()) {
return true;
}
// fail if artist or title unknown
if (desc.artist.isEmpty() || desc.title.isEmpty()) {
m_errorList << tr("Unable to fetch lyrics: Artist or title is unknown.");
emit resultsAvailable();
return true;
}
// request lyrics
auto *reply = requestSongDetails(desc);
addReply(reply, bind(&LyricsWikiaResultsModel::handleSongDetailsFinished, this, reply, index.row()));
return false;
} }
void LyricsWikiaResultsModel::parseInitialResults(const QByteArray &data) void LyricsWikiaResultsModel::parseInitialResults(const QByteArray &data)

View File

@ -12,12 +12,12 @@ class LyricsWikiaResultsModel : public HttpResultsModel {
public: public:
LyricsWikiaResultsModel(SongDescription &&initialSongDescription, QNetworkReply *reply); LyricsWikiaResultsModel(SongDescription &&initialSongDescription, QNetworkReply *reply);
bool fetchCover(const QModelIndex &index); bool fetchCover(const QModelIndex &index) override;
bool fetchLyrics(const QModelIndex &index); bool fetchLyrics(const QModelIndex &index) override;
QUrl webUrl(const QModelIndex &index); QUrl webUrl(const QModelIndex &index) override;
protected: protected:
void parseInitialResults(const QByteArray &data); void parseInitialResults(const QByteArray &data) override;
private: private:
QNetworkReply *requestSongDetails(const SongDescription &songDescription); QNetworkReply *requestSongDetails(const SongDescription &songDescription);

View File

@ -25,31 +25,38 @@ MusicBrainzResultsModel::MusicBrainzResultsModel(SongDescription &&initialSongDe
bool MusicBrainzResultsModel::fetchCover(const QModelIndex &index) bool MusicBrainzResultsModel::fetchCover(const QModelIndex &index)
{ {
// FIXME: avoid code duplication with lyricswikia.cpp
// find song description
if (index.parent().isValid() || index.row() >= m_results.size()) { if (index.parent().isValid() || index.row() >= m_results.size()) {
return true; return true;
} }
SongDescription &desc = m_results[index.row()]; SongDescription &desc = m_results[index.row()];
// skip if cover is already available
if (!desc.cover.isEmpty()) { if (!desc.cover.isEmpty()) {
// cover is already available -> nothing to do
return true; return true;
} }
// fail if album ID is unknown
if (desc.albumId.isEmpty()) { if (desc.albumId.isEmpty()) {
m_errorList << tr("Unable to fetch cover: Album ID unknown"); m_errorList << tr("Unable to fetch cover: Album ID unknown");
emit resultsAvailable(); emit resultsAvailable();
return true; return true;
} }
try {
// the item belongs to an album which cover has already been fetched // skip if the item belongs to an album which cover has already been fetched
desc.cover = m_coverData.at(desc.albumId); const auto coverData = m_coverData.find(desc.albumId);
} catch (const out_of_range &) { if (coverData != m_coverData.end()) {
// request the cover art desc.cover = coverData->second;
auto *const reply = queryCoverArtArchive(desc.albumId); return true;
addReply(reply, bind(&MusicBrainzResultsModel::handleCoverReplyFinished, this, reply, desc.albumId, index.row()));
setFetchingCover(true);
return false;
} }
return true;
// request the cover art
auto *const reply = queryCoverArtArchive(desc.albumId);
addReply(reply, bind(&MusicBrainzResultsModel::handleCoverReplyFinished, this, reply, desc.albumId, index.row()));
setFetchingCover(true);
return false;
} }
QUrl MusicBrainzResultsModel::webUrl(const QModelIndex &index) QUrl MusicBrainzResultsModel::webUrl(const QModelIndex &index)

View File

@ -16,11 +16,11 @@ private:
public: public:
MusicBrainzResultsModel(SongDescription &&initialSongDescription, QNetworkReply *reply); MusicBrainzResultsModel(SongDescription &&initialSongDescription, QNetworkReply *reply);
bool fetchCover(const QModelIndex &index); bool fetchCover(const QModelIndex &index) override;
QUrl webUrl(const QModelIndex &index); QUrl webUrl(const QModelIndex &index) override;
protected: protected:
void parseInitialResults(const QByteArray &data); void parseInitialResults(const QByteArray &data) override;
private: private:
What m_what; What m_what;