From 2c21183692ac4e541c6c48dc8e4e52eaada66e2c Mon Sep 17 00:00:00 2001 From: David Hernandez Date: Thu, 28 Jun 2018 13:50:37 +0200 Subject: [PATCH] Added connections to lambda expressions in player manager, file listener now opens and closes the file --- src/broadcast/filelistener.cpp | 39 ++++++++------------ src/broadcast/filelistener.h | 16 +++++--- src/broadcast/listenbrainzjsonfactory.cpp | 38 +++++++++++++++++++ src/broadcast/listenbrainzjsonfactory.h | 10 +++++ src/broadcast/listenbrainzservice.cpp | 14 +++++++ src/broadcast/listenbrainzservice.h | 12 ++++++ src/broadcast/metadatafileworker.cpp | 13 ++----- src/broadcast/metadatafileworker.h | 1 - src/broadcast/scrobblingmanager.cpp | 15 +++----- src/broadcast/scrobblingmanager.h | 6 +-- src/mixer/playermanager.cpp | 21 ++++++----- src/preferences/dialog/dlgprefmetadatadlg.ui | 20 +++++++++- src/preferences/metadatafilesettings.cpp | 2 +- 13 files changed, 144 insertions(+), 63 deletions(-) create mode 100644 src/broadcast/listenbrainzjsonfactory.cpp create mode 100644 src/broadcast/listenbrainzjsonfactory.h create mode 100644 src/broadcast/listenbrainzservice.cpp create mode 100644 src/broadcast/listenbrainzservice.h diff --git a/src/broadcast/filelistener.cpp b/src/broadcast/filelistener.cpp index 8ce4d33767c..69af1b8fa7b 100644 --- a/src/broadcast/filelistener.cpp +++ b/src/broadcast/filelistener.cpp @@ -21,9 +21,6 @@ FileListener::FileListener(UserSettingsPointer pConfig) connect(this,SIGNAL(deleteFile()), newWorker,SLOT(slotDeleteFile())); - connect(this,SIGNAL(openFile()), - newWorker,SLOT(slotOpenFile())); - connect(this,SIGNAL(moveFile(QString)), newWorker,SLOT(slotMoveFile(QString))); @@ -36,8 +33,6 @@ FileListener::FileListener(UserSettingsPointer pConfig) connect(&m_COsettingsChanged,SIGNAL(valueChanged(double)), this,SLOT(slotFileSettingsChanged(double))); - updateStateFromSettings(); - m_workerThread.start(); } @@ -50,14 +45,15 @@ FileListener::~FileListener() { void FileListener::slotBroadcastCurrentTrack(TrackPointer pTrack) { if (!pTrack) return; + m_fileContents.title = pTrack->getTitle(); + m_fileContents.artist = pTrack->getArtist(); QString writtenString(m_latestSettings.fileFormatString); writtenString.replace("$author",pTrack->getArtist()). replace("$title",pTrack->getTitle()) += '\n'; - m_fileContents = writtenString; QTextCodec *codec = QTextCodec::codecForName(m_latestSettings.fileEncoding); DEBUG_ASSERT(codec); - QByteArray fileContents = codec->fromUnicode(m_fileContents); - tracksPaused = false; + QByteArray fileContents = codec->fromUnicode(writtenString); + m_tracksPaused = false; emit writeMetadataToFile(fileContents); } @@ -66,14 +62,14 @@ void FileListener::slotScrobbleTrack(TrackPointer pTrack) { } void FileListener::slotAllTracksPaused() { - tracksPaused = true; + m_tracksPaused = true; emit clearFile(); } void FileListener::slotFileSettingsChanged(double value) { if (value) { FileSettings latestSettings = MetadataFileSettings::getLatestSettings(); - filePathChanged = latestSettings.filePath != m_latestSettings.filePath; + m_filePathChanged = latestSettings.filePath != m_latestSettings.filePath; m_latestSettings = latestSettings; updateStateFromSettings(); } @@ -89,20 +85,17 @@ void FileListener::updateStateFromSettings() { } void FileListener::updateFile() { - if (fileOpen) { - if (filePathChanged) { - emit moveFile(m_latestSettings.filePath); - } - else if (!tracksPaused) { - QTextCodec *codec = QTextCodec::codecForName(m_latestSettings.fileEncoding); - DEBUG_ASSERT(codec); - QByteArray fileContents = codec->fromUnicode(m_fileContents); - emit writeMetadataToFile(fileContents); - } + if (m_filePathChanged) { + emit moveFile(m_latestSettings.filePath); } - else { - emit openFile(); - fileOpen = true; + if (!m_tracksPaused && !m_fileContents.isEmpty()) { + QTextCodec *codec = QTextCodec::codecForName(m_latestSettings.fileEncoding); + DEBUG_ASSERT(codec); + QString newContents(m_latestSettings.fileFormatString); + newContents.replace("$author",m_fileContents.artist) + .replace("$title",m_fileContents.title) += '\n'; + QByteArray contentsBinary = codec->fromUnicode(newContents); + emit writeMetadataToFile(contentsBinary); } } diff --git a/src/broadcast/filelistener.h b/src/broadcast/filelistener.h index a82930f9008..5e361716337 100644 --- a/src/broadcast/filelistener.h +++ b/src/broadcast/filelistener.h @@ -16,7 +16,6 @@ class FileListener: public ScrobblingService { void slotAllTracksPaused() override; signals: void deleteFile(); - void openFile(); void moveFile(QString destination); void writeMetadataToFile(QByteArray contents); void clearFile(); @@ -24,16 +23,21 @@ class FileListener: public ScrobblingService { void slotFileSettingsChanged(double value); private: + struct WrittenMetadata { + QString title,artist; + bool isEmpty() { + return title.isEmpty() && artist.isEmpty(); + } + }; + void updateStateFromSettings(); void updateFile(); - static void writeMetadataToFile(const QByteArray *contents,std::shared_ptr file); - QString m_fileContents; //We need this to translate between codecs. ControlPushButton m_COsettingsChanged; UserSettingsPointer m_pConfig; FileSettings m_latestSettings; QThread m_workerThread; - bool filePathChanged = false; - bool fileOpen = false; - bool tracksPaused = false; + WrittenMetadata m_fileContents; + bool m_filePathChanged = false; + bool m_tracksPaused = false; }; \ No newline at end of file diff --git a/src/broadcast/listenbrainzjsonfactory.cpp b/src/broadcast/listenbrainzjsonfactory.cpp new file mode 100644 index 00000000000..201760c9b86 --- /dev/null +++ b/src/broadcast/listenbrainzjsonfactory.cpp @@ -0,0 +1,38 @@ + +#include +#include +#include +#include + +#include "listenbrainzjsonfactory.h" + + +QByteArray ListenBrainzJSONFactory::getJSONFromTrack(TrackPointer pTrack, JsonType type) { + QJsonObject jsonObject; + QString stringType; + if (type == NowListening) { + stringType = "playing_now"; + } + else { + stringType = "single"; + } + + QJsonArray payloadArray; + QJsonObject payloadObject; + QJsonObject metadataObject; + QString title = pTrack->getTitle(); + QString artist = pTrack->getArtist(); + metadataObject.insert("artist_name",artist); + metadataObject.insert("track_name",title); + payloadObject.insert("track_metadata",metadataObject); + qint64 timeStamp = QDateTime::currentSecsSinceEpoch(); + + if (type == Single) { + payloadObject.insert("listened_at",timeStamp); + } + payloadArray.append(payloadObject); + jsonObject.insert("listen_type",stringType); + jsonObject.insert("payload",payloadArray); + QJsonDocument doc(jsonObject); + return doc.toJson(QJsonDocument::Compact); +} diff --git a/src/broadcast/listenbrainzjsonfactory.h b/src/broadcast/listenbrainzjsonfactory.h new file mode 100644 index 00000000000..8a011cae8e7 --- /dev/null +++ b/src/broadcast/listenbrainzjsonfactory.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +#include "track/track.h" + +namespace ListenBrainzJSONFactory { + enum JsonType {NowListening, Single}; + QByteArray getJSONFromTrack(TrackPointer pTrack, JsonType type); +}; \ No newline at end of file diff --git a/src/broadcast/listenbrainzservice.cpp b/src/broadcast/listenbrainzservice.cpp new file mode 100644 index 00000000000..02e1c5d6c8c --- /dev/null +++ b/src/broadcast/listenbrainzservice.cpp @@ -0,0 +1,14 @@ + +#include "broadcast/listenbrainzservice.h" + +void ListenBrainzService::slotBroadcastCurrentTrack(TrackPointer pTrack) { + +} + +void ListenBrainzService::slotScrobbleTrack(TrackPointer pTrack) { + +} + +void ListenBrainzService::slotAllTracksPaused() { + +} diff --git a/src/broadcast/listenbrainzservice.h b/src/broadcast/listenbrainzservice.h new file mode 100644 index 00000000000..18851754381 --- /dev/null +++ b/src/broadcast/listenbrainzservice.h @@ -0,0 +1,12 @@ +#pragma once + +#include "broadcast/scrobblingservice.h" + +class ListenBrainzService : public ScrobblingService { + Q_OBJECT +public: + void slotBroadcastCurrentTrack(TrackPointer pTrack) override; + void slotScrobbleTrack(TrackPointer pTrack) override; + void slotAllTracksPaused() override; +}; + diff --git a/src/broadcast/metadatafileworker.cpp b/src/broadcast/metadatafileworker.cpp index f98568c2d6e..19c4c9dde8e 100644 --- a/src/broadcast/metadatafileworker.cpp +++ b/src/broadcast/metadatafileworker.cpp @@ -10,22 +10,17 @@ void MetadataFileWorker::slotDeleteFile() { m_file.remove(); } -void MetadataFileWorker::slotOpenFile() { - m_file.open(QIODevice::ReadWrite | - QIODevice::Truncate | - QIODevice::Text | - QIODevice::Unbuffered); -} - void MetadataFileWorker::slotMoveFile(QString destination) { m_file.remove(); m_file.setFileName(destination); - slotOpenFile(); } void MetadataFileWorker::slotWriteMetadataToFile(QByteArray fileContents) { - slotClearFile(); + m_file.open(QIODevice::WriteOnly | + QIODevice::Text | + QIODevice::Unbuffered); m_file.write(fileContents); + m_file.close(); } void MetadataFileWorker::slotClearFile() { diff --git a/src/broadcast/metadatafileworker.h b/src/broadcast/metadatafileworker.h index a84405d7db1..4a9e19b1ab3 100644 --- a/src/broadcast/metadatafileworker.h +++ b/src/broadcast/metadatafileworker.h @@ -9,7 +9,6 @@ class MetadataFileWorker : public QObject { explicit MetadataFileWorker(const QString &filePath); public slots: void slotDeleteFile(); - void slotOpenFile(); void slotMoveFile(QString destination); void slotWriteMetadataToFile(QByteArray fileContents); void slotClearFile(); diff --git a/src/broadcast/scrobblingmanager.cpp b/src/broadcast/scrobblingmanager.cpp index c77196e6120..7981be0ad8c 100644 --- a/src/broadcast/scrobblingmanager.cpp +++ b/src/broadcast/scrobblingmanager.cpp @@ -129,8 +129,8 @@ void ScrobblingManager::slotTrackPaused(TrackPointer pPausedTrack) { } } -void ScrobblingManager::slotTrackResumed(TrackPointer pResumedTrack) { - BaseTrackPlayer *player = qobject_cast(sender()); +void ScrobblingManager::slotTrackResumed(TrackPointer pResumedTrack, const QString &playerGroup) { + BaseTrackPlayer *player = m_pManager->getPlayer(playerGroup); DEBUG_ASSERT(player); if (m_pAudibleStrategy->isTrackAudible(pResumedTrack,player)) { for (auto &trackInfoPtr : m_trackList) { @@ -144,22 +144,19 @@ void ScrobblingManager::slotTrackResumed(TrackPointer pResumedTrack) { } } -void ScrobblingManager::slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack) { +void ScrobblingManager::slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack, const QString &playerGroup) { Q_UNUSED(pNewTrack); - BaseTrackPlayer *sourcePlayer = - qobject_cast(sender()); - DEBUG_ASSERT(sourcePlayer); if (pOldTrack) { m_tracksToBeReset.append(TrackToBeReset(pOldTrack, - sourcePlayer->getGroup())); + playerGroup)); } } -void ScrobblingManager::slotNewTrackLoaded(TrackPointer pNewTrack) { +void ScrobblingManager::slotNewTrackLoaded(TrackPointer pNewTrack, const QString &playerGroup) { //Empty player gives a null pointer. if (!pNewTrack) return; - BaseTrackPlayer *player = qobject_cast(sender()); + BaseTrackPlayer *player = m_pManager->getPlayer(playerGroup); DEBUG_ASSERT(player); bool trackAlreadyAdded = false; for (auto &trackInfoPtr : m_trackList) { diff --git a/src/broadcast/scrobblingmanager.h b/src/broadcast/scrobblingmanager.h index 6c1294afafb..4adaa5b74f3 100644 --- a/src/broadcast/scrobblingmanager.h +++ b/src/broadcast/scrobblingmanager.h @@ -54,9 +54,9 @@ class ScrobblingManager : public QObject { public slots: void slotTrackPaused(TrackPointer pPausedTrack); - void slotTrackResumed(TrackPointer pResumedTrack); - void slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack); - void slotNewTrackLoaded(TrackPointer pNewTrack); + void slotTrackResumed(TrackPointer pResumedTrack, const QString &playerGroup); + void slotLoadingTrack(TrackPointer pNewTrack, TrackPointer pOldTrack, const QString &playerGroup); + void slotNewTrackLoaded(TrackPointer pNewTrack, const QString &playerGroup); void slotPlayerEmpty(); void slotGuiTick(double timeSinceLastTick); diff --git a/src/mixer/playermanager.cpp b/src/mixer/playermanager.cpp index 7822614c3f4..c84a4b04825 100644 --- a/src/mixer/playermanager.cpp +++ b/src/mixer/playermanager.cpp @@ -1,4 +1,4 @@ -// playermanager.cpp + // playermanager.cpp // Created 6/1/2010 by RJ Ryan (rryan@mit.edu) #include "mixer/playermanager.h" @@ -373,14 +373,17 @@ void PlayerManager::addDeckInner() { connect(pDeck, SIGNAL(noVinylControlInputConfigured()), this, SIGNAL(noVinylControlInputConfigured())); - connect(pDeck,SIGNAL(trackPaused(TrackPointer)), - &m_scrobblingManager, SLOT(slotTrackPaused(TrackPointer))); - connect(pDeck,SIGNAL(trackResumed(TrackPointer)), - &m_scrobblingManager, SLOT(slotTrackResumed(TrackPointer))); - connect(pDeck,SIGNAL(newTrackLoaded(TrackPointer)), - &m_scrobblingManager, SLOT(slotNewTrackLoaded(TrackPointer))); - connect(pDeck,SIGNAL(loadingTrack(TrackPointer,TrackPointer)), - &m_scrobblingManager, SLOT(slotLoadingTrack(TrackPointer,TrackPointer))); + connect(pDeck,&Deck::trackPaused, + &m_scrobblingManager, &ScrobblingManager::slotTrackPaused); + connect(pDeck,&Deck::trackResumed, + [this,group] (TrackPointer pTrack) -> void + {m_scrobblingManager.slotTrackResumed(pTrack,group);}); + connect(pDeck,&Deck::newTrackLoaded, + [this,group] (TrackPointer pTrack) -> void + {m_scrobblingManager.slotNewTrackLoaded(pTrack,group);}); + connect(pDeck,&Deck::loadingTrack, + [this,group] (TrackPointer pOldTrack,TrackPointer pNewTrack) -> void + {m_scrobblingManager.slotLoadingTrack(pOldTrack,pNewTrack,group);}); connect(pDeck,SIGNAL(playerEmpty()), &m_scrobblingManager, SLOT(slotPlayerEmpty())); diff --git a/src/preferences/dialog/dlgprefmetadatadlg.ui b/src/preferences/dialog/dlgprefmetadatadlg.ui index e9ea04fc958..dec988ab1d5 100644 --- a/src/preferences/dialog/dlgprefmetadatadlg.ui +++ b/src/preferences/dialog/dlgprefmetadatadlg.ui @@ -6,8 +6,8 @@ 0 0 - 647 - 541 + 668 + 514 @@ -145,6 +145,22 @@ + + + + User token. If you don't have one please register <a href="https://listenbrainz.org/login/">here</a> + + + Qt::RichText + + + true + + + + + + diff --git a/src/preferences/metadatafilesettings.cpp b/src/preferences/metadatafilesettings.cpp index 8d5d6332630..caa3643ccce 100644 --- a/src/preferences/metadatafilesettings.cpp +++ b/src/preferences/metadatafilesettings.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include "metadatafilesettings.h"