Skip to content

Commit

Permalink
Added connections to lambda expressions in player manager, file liste…
Browse files Browse the repository at this point in the history
…ner now opens and closes the file
  • Loading branch information
davidhm committed Jun 28, 2018
1 parent 9c43d09 commit 2c21183
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 63 deletions.
39 changes: 16 additions & 23 deletions src/broadcast/filelistener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand All @@ -36,8 +33,6 @@ FileListener::FileListener(UserSettingsPointer pConfig)
connect(&m_COsettingsChanged,SIGNAL(valueChanged(double)),
this,SLOT(slotFileSettingsChanged(double)));

updateStateFromSettings();

m_workerThread.start();
}

Expand All @@ -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);
}

Expand All @@ -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();
}
Expand All @@ -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);
}
}

16 changes: 10 additions & 6 deletions src/broadcast/filelistener.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@ class FileListener: public ScrobblingService {
void slotAllTracksPaused() override;
signals:
void deleteFile();
void openFile();
void moveFile(QString destination);
void writeMetadataToFile(QByteArray contents);
void clearFile();
private slots:
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<QFile> 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;
};
38 changes: 38 additions & 0 deletions src/broadcast/listenbrainzjsonfactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#include <QDateTime>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

#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);
}
10 changes: 10 additions & 0 deletions src/broadcast/listenbrainzjsonfactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <QByteArray>

#include "track/track.h"

namespace ListenBrainzJSONFactory {
enum JsonType {NowListening, Single};
QByteArray getJSONFromTrack(TrackPointer pTrack, JsonType type);
};
14 changes: 14 additions & 0 deletions src/broadcast/listenbrainzservice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#include "broadcast/listenbrainzservice.h"

void ListenBrainzService::slotBroadcastCurrentTrack(TrackPointer pTrack) {

}

void ListenBrainzService::slotScrobbleTrack(TrackPointer pTrack) {

}

void ListenBrainzService::slotAllTracksPaused() {

}
12 changes: 12 additions & 0 deletions src/broadcast/listenbrainzservice.h
Original file line number Diff line number Diff line change
@@ -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;
};

13 changes: 4 additions & 9 deletions src/broadcast/metadatafileworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion src/broadcast/metadatafileworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 6 additions & 9 deletions src/broadcast/scrobblingmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ void ScrobblingManager::slotTrackPaused(TrackPointer pPausedTrack) {
}
}

void ScrobblingManager::slotTrackResumed(TrackPointer pResumedTrack) {
BaseTrackPlayer *player = qobject_cast<BaseTrackPlayer*>(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) {
Expand All @@ -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<BaseTrackPlayer*>(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<BaseTrackPlayer*>(sender());
BaseTrackPlayer *player = m_pManager->getPlayer(playerGroup);
DEBUG_ASSERT(player);
bool trackAlreadyAdded = false;
for (auto &trackInfoPtr : m_trackList) {
Expand Down
6 changes: 3 additions & 3 deletions src/broadcast/scrobblingmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
21 changes: 12 additions & 9 deletions src/mixer/playermanager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// playermanager.cpp
// playermanager.cpp
// Created 6/1/2010 by RJ Ryan (rryan@mit.edu)
#include "mixer/playermanager.h"

Expand Down Expand Up @@ -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()));

Expand Down
20 changes: 18 additions & 2 deletions src/preferences/dialog/dlgprefmetadatadlg.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>647</width>
<height>541</height>
<width>668</width>
<height>514</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -145,6 +145,22 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>User token. If you don't have one please register &lt;a href=&quot;https://listenbrainz.org/login/&quot;&gt;here&lt;/a&gt;</string>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
</layout>
</widget>
</item>
Expand Down
2 changes: 1 addition & 1 deletion src/preferences/metadatafilesettings.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <QTextCodec>
#include <QObject>
#include <QLineEdit>
#include <QtGui/QFileDialog>
#include <QFileDialog>

#include "metadatafilesettings.h"

Expand Down

0 comments on commit 2c21183

Please sign in to comment.