Skip to content

Commit

Permalink
Modified settings, added concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhm committed Jun 23, 2018
1 parent 49b8b93 commit 567e9b5
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 204 deletions.
1 change: 1 addition & 0 deletions build/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ def sources(self, build):
"preferences/broadcastsettings_legacy.cpp",
"preferences/broadcastsettingsmodel.cpp",
"preferences/broadcastprofile.cpp",
"preferences/metadatafilesettings.cpp",
"preferences/upgrade.cpp",
"preferences/dlgpreferencepage.cpp",

Expand Down
79 changes: 45 additions & 34 deletions src/broadcast/filelistener.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@

#include <QtCore/QTextCodec>
#include <QTextCodec>
#include <QtConcurrentRun>
#include <functional>

#include "broadcast/filelistener.h"
#include "preferences/dialog/dlgprefmetadata.h"
#include "preferences/metadatafilesettings.h"


FileListener::FileListener(UserSettingsPointer pConfig)
: m_COsettingsChanged(kSettingsChanged),
: m_pFile(new QFile,[](QFile *file)->void{
file->resize(0);
delete file;
}),
m_COsettingsChanged(kSettingsChanged),
m_pConfig(pConfig),
m_latestSettings(DlgPrefMetadata::getPersistedSettings(pConfig)) {
m_latestSettings(MetadataFileSettings::getPersistedSettings(pConfig)) {

connect(&m_COsettingsChanged,SIGNAL(valueChanged(double)),
this,SLOT(slotFileSettingsChanged(double)));
updateStateFromSettings();
}

FileListener::~FileListener() {
m_file.resize(0);
}

void FileListener::slotBroadcastCurrentTrack(TrackPointer pTrack) {
if (!pTrack)
return;
QTextStream stream(&m_file);
//Clear file
m_file.resize(0);
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);
stream.setCodec(codec);
QString writtenString =
m_latestSettings.fileFormatString.replace("author",pTrack->getArtist()).
replace("title",pTrack->getTitle());
stream << writtenString << '\n';
QByteArray *fileContents = new QByteArray;
*fileContents = codec->fromUnicode(m_fileContents);
QtConcurrent::run(&FileListener::writeMetadataToFile,fileContents,m_pFile);
}

void FileListener::slotScrobbleTrack(TrackPointer pTrack) {
Q_UNUSED(pTrack);
}

void FileListener::slotAllTracksPaused() {
m_file.resize(0);
m_pFile->resize(0);
}

void FileListener::slotFileSettingsChanged(double value) {
if (value) {
m_latestSettings = DlgPrefMetadata::getLatestSettings();
FileSettings latestSettings = MetadataFileSettings::getLatestSettings();
filePathChanged = latestSettings.filePath != m_latestSettings.filePath;
m_latestSettings = latestSettings;
updateStateFromSettings();
}
}
Expand All @@ -53,33 +57,40 @@ void FileListener::updateStateFromSettings() {
updateFile();
}
else {
m_file.setFileName(m_latestSettings.filePath);
if (m_file.exists()) {
m_file.remove();
m_pFile->setFileName(m_latestSettings.filePath);
if (m_pFile->exists()) {
m_pFile->remove();
}
}
}

void FileListener::updateFile() {
if (m_file.isOpen()) {
m_file.seek(0);
QByteArray fileContents = m_file.readAll();
if (m_pFile->isOpen()) {
if (filePathChanged) {
m_pFile->remove();
m_pFile->setFileName(m_latestSettings.filePath);
m_pFile->open(QIODevice::ReadWrite |
QIODevice::Truncate |
QIODevice::Text |
QIODevice::Unbuffered);
}
QTextCodec *codec = QTextCodec::codecForName(m_latestSettings.fileEncoding);
DEBUG_ASSERT(codec);
QByteArray newFileContents = codec->fromUnicode(fileContents);
m_file.remove();
m_file.setFileName(m_latestSettings.filePath);
m_file.open(QIODevice::ReadWrite |
QIODevice::Truncate |
QIODevice::Text |
QIODevice::Unbuffered);
m_file.write(newFileContents);
QByteArray *fileContents = new QByteArray;
*fileContents = codec->fromUnicode(m_fileContents);
QtConcurrent::run(&FileListener::writeMetadataToFile,fileContents,m_pFile);
}
else {
m_file.setFileName(m_latestSettings.filePath);
m_file.open(QIODevice::ReadWrite |
m_pFile->setFileName(m_latestSettings.filePath);
m_pFile->open(QIODevice::ReadWrite |
QIODevice::Truncate |
QIODevice::Text |
QIODevice::Unbuffered);
}
}

void FileListener::writeMetadataToFile(const QByteArray *contents, std::shared_ptr<QFile> file) {
file->resize(0);
file->write(*contents);
delete contents;
}
10 changes: 5 additions & 5 deletions src/broadcast/filelistener.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
class FileListener: public ScrobblingService {
Q_OBJECT
public:
enum class FileListenerType {
SAMBroadcaster
};
explicit FileListener(UserSettingsPointer pSettings);
~FileListener() override;
~FileListener() override = default;
void slotBroadcastCurrentTrack(TrackPointer pTrack) override;
void slotScrobbleTrack(TrackPointer pTrack) override;
void slotAllTracksPaused() override;
Expand All @@ -23,9 +20,12 @@ class FileListener: public ScrobblingService {

void updateStateFromSettings();
void updateFile();
static void writeMetadataToFile(const QByteArray *contents,std::shared_ptr<QFile> file);

QFile m_file;
std::shared_ptr<QFile> m_pFile;
QString m_fileContents; //We need this to translate between codecs.
ControlPushButton m_COsettingsChanged;
UserSettingsPointer m_pConfig;
FileSettings m_latestSettings;
bool filePathChanged = false;
};
2 changes: 1 addition & 1 deletion src/mixer/basetrackplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,5 +478,5 @@ void BaseTrackPlayerImpl::setReplayGain(double value) {
}

bool BaseTrackPlayerImpl::isTrackPaused() const {
return m_pPlay->toBool();
return !m_pPlay->toBool();
}
146 changes: 19 additions & 127 deletions src/preferences/dialog/dlgprefmetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,153 +7,45 @@
#include "preferences/dialog/dlgprefmetadata.h"
#include "preferences/dialog/ui_dlgfilelistenerbox.h"

FileSettings DlgPrefMetadata::s_latestSettings;

DlgPrefMetadata::DlgPrefMetadata(QWidget *pParent,UserSettingsPointer pSettings)
: DlgPreferencePage(pParent),
m_pSettings(pSettings),
m_CPSettingsChanged(kSettingsChanged) {
m_pFileSettings(nullptr) {
setupUi(this);
s_latestSettings = getPersistedSettings(pSettings);
setupWidgets();
}

FileSettings DlgPrefMetadata::getPersistedSettings(const UserSettingsPointer &pSettings) {
FileSettings ret;
ret.enabled =
pSettings->getValue(kMetadataFileEnabled,defaultFileMetadataEnabled);
ret.fileEncoding =
pSettings->getValue(kFileEncoding,defaultEncoding.constData()).toUtf8();
ret.fileFormat =
pSettings->getValue(kFileFormat,defaultFileFormat);
ret.fileFormatString =
pSettings->getValue(kFileFormatString,defaultFileFormatString);
ret.filePath =
pSettings->getValue(kFilePath,defaultFilePath);
return ret;
}

void DlgPrefMetadata::setupWidgets() {
enableFileListener->setChecked(s_latestSettings.enabled);

fileEncodingComboBox->clear();
QList<QByteArray> codecs = QTextCodec::availableCodecs();
for (const QByteArray &codec : codecs) {
fileEncodingComboBox->addItem(codec);
}

formatComboBox->clear();
//To be extended when adding more file formats.
QVariant SAMBroadcasterData("author - title");
formatComboBox->addItem("SAMBroadcaster",SAMBroadcasterData);

formatLineEdit->setText(formatComboBox->itemData(formatComboBox->currentIndex()).toString());
connect(formatComboBox,SIGNAL(currentIndexChanged(int)),
this,SLOT(slotFormatChanged(int)));

customFormatEnabledBox->setChecked(s_latestSettings.fileFormat == "Custom");
if (s_latestSettings.fileFormat == "Custom")
customFormatLineEdit->setText(s_latestSettings.fileFormatString);

filePathLineEdit->setText(s_latestSettings.filePath);
filePathLineEdit->setStyleSheet("");
connect(filePathButton,SIGNAL(pressed()),
this,SLOT(slotFilepathButtonClicked()));

setFileSettings();
}

void DlgPrefMetadata::slotFormatChanged(int newIndex) {
formatLineEdit->setText(formatComboBox->itemData(newIndex).toString());
}
void DlgPrefMetadata::setFileSettings() {
FileWidgets widgets;
widgets.enableCheckbox = enableFileListener;
widgets.enableCustomFormatBox = customFormatEnabledBox;
widgets.encodingBox = fileEncodingComboBox;
widgets.formatBox = formatComboBox;
widgets.formatLineEdit = formatLineEdit;
widgets.customFormatLineEdit = customFormatLineEdit;
widgets.filePathLineEdit = filePathLineEdit;
widgets.changeFilePathButton = filePathButton;

void DlgPrefMetadata::slotFilepathButtonClicked() {
QString newFilePath = QFileDialog::getSaveFileName(
this,
"Choose new file path",
"./",
"Text files(*.txt)"
);
filePathLineEdit->setText(newFilePath);
m_pFileSettings = new MetadataFileSettings(m_pSettings,widgets,this);
}

void DlgPrefMetadata::slotApply() {
if (fileSettingsDifferent() && checkIfSettingsCorrect()) {
saveLatestSettingsAndNotify();
persistSettings();
}
}

bool DlgPrefMetadata::fileSettingsDifferent() {
return s_latestSettings.enabled != enableFileListener->isChecked() ||
s_latestSettings.fileEncoding != fileEncodingComboBox->currentText() ||
s_latestSettings.fileFormat != "Custom" &&
s_latestSettings.fileFormat != formatComboBox->currentText() ||
s_latestSettings.fileFormat != "Custom" && customFormatEnabledBox->isChecked() ||
s_latestSettings.fileFormat == "Custom" && !customFormatEnabledBox->isChecked() ||
s_latestSettings.fileFormat == "Custom" &&
s_latestSettings.fileFormatString != customFormatLineEdit->text() ||
s_latestSettings.filePath != filePathLineEdit->text();
}

bool DlgPrefMetadata::checkIfSettingsCorrect() {
QString supposedPath = filePathLineEdit->text();
int lastIndex = supposedPath.lastIndexOf('/');
if (lastIndex != -1) {
QString supposedDir = supposedPath.left(lastIndex);
QDir dir(supposedDir);
bool dirExists = dir.exists();
if (!dirExists) {
filePathLineEdit->setStyleSheet("border: 1px solid red");
}
else {
filePathLineEdit->setStyleSheet("");
}
return dirExists;
}
return true;
}

void DlgPrefMetadata::saveLatestSettingsAndNotify() {
s_latestSettings.enabled = enableFileListener->isChecked();
s_latestSettings.fileEncoding = fileEncodingComboBox->currentText().toUtf8();
s_latestSettings.fileFormat =
customFormatEnabledBox->isChecked() ? "Custom" :
formatComboBox->currentText();
s_latestSettings.fileFormatString =
customFormatEnabledBox->isChecked() ? customFormatLineEdit->text() :
formatLineEdit->text();
s_latestSettings.filePath = QDir(filePathLineEdit->text()).absolutePath();
m_CPSettingsChanged.set(true);
}

void DlgPrefMetadata::persistSettings() {
m_pSettings->setValue(kMetadataFileEnabled,s_latestSettings.enabled);
m_pSettings->setValue(kFileEncoding,QString(s_latestSettings.fileEncoding));
m_pSettings->setValue(kFileFormat,s_latestSettings.fileFormat);
m_pSettings->setValue(kFileFormatString,s_latestSettings.fileFormatString);
m_pSettings->setValue(kFilePath,s_latestSettings.filePath);
m_pFileSettings->applySettings();
}

void DlgPrefMetadata::slotCancel() {
setupWidgets();
m_pFileSettings->cancelSettings();
}

void DlgPrefMetadata::slotResetToDefaults() {
resetSettingsToDefault();
setupWidgets();
m_pFileSettings->setSettingsToDefault();
}

void DlgPrefMetadata::resetSettingsToDefault() {
s_latestSettings.enabled = defaultFileMetadataEnabled;
s_latestSettings.fileEncoding = defaultEncoding;
s_latestSettings.fileFormat = defaultFileFormat;
s_latestSettings.fileFormatString = defaultFileFormatString;
s_latestSettings.filePath = defaultFilePath;
DlgPrefMetadata::~DlgPrefMetadata() {
delete m_pFileSettings;
}

FileSettings DlgPrefMetadata::getLatestSettings() {
return DlgPrefMetadata::s_latestSettings;
}




Expand Down
Loading

0 comments on commit 567e9b5

Please sign in to comment.