During #475 found issue with deleter

* changed / improved deleter
* formatting in related class soundgenerator
This commit is contained in:
Klaus Basan
2015-09-30 05:01:34 +02:00
committed by Mathew Sutcliffe
parent 84b06c51d9
commit 68d0b8cc98
4 changed files with 87 additions and 40 deletions

View File

@@ -7,28 +7,57 @@
* contained in the LICENSE file.
*/
#include <QFile>
#include "filedeleter.h"
#include <QFile>
namespace BlackMisc
{
void CFileDeleter::addFile(const QString &file)
void CFileDeleter::addFileForDeletion(const QString &file)
{
if (file.isEmpty()) return;
if (file.isEmpty()) { return; }
if (!this->m_fileNames.contains(file)) this->m_fileNames.append(file);
}
void CFileDeleter::addFileForDeletion(const QString &file)
void CFileDeleter::addFilesForDeletion(const QStringList &files)
{
CFileDeleter::fileDeleter().addFile(file);
if (files.isEmpty()) { return; }
this->m_fileNames.append(files);
}
CFileDeleter::~CFileDeleter()
{
foreach(const QString fn, this->m_fileNames)
this->deleteFiles();
}
void CFileDeleter::deleteFiles()
{
const QStringList files(m_fileNames);
m_fileNames.clear();
for (const QString &fn : files)
{
if (fn.isEmpty()) { continue; }
QFile f(fn);
f.remove();
}
}
}
CTimedFileDeleter::CTimedFileDeleter(const QString &file, int deleteAfterMs, QObject *parent) :
QObject(parent)
{
Q_ASSERT_X(!file.isEmpty(), Q_FUNC_INFO, "No file name");
if (deleteAfterMs < 100) { deleteAfterMs = 100; } // makes sure timer is started properly
this->m_fileDeleter.addFileForDeletion(file);
m_timerId = startTimer(deleteAfterMs);
}
void CTimedFileDeleter::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
if (m_timerId > 0) { this->killTimer(m_timerId); }
m_timerId = -1;
m_fileDeleter.deleteFiles();
this->deleteLater();
}
} // ns

View File

@@ -18,33 +18,49 @@
namespace BlackMisc
{
/*!
* Utility class, deleting files when it is destroyed
*/
class BLACKMISC_EXPORT CFileDeleter
{
private:
QStringList m_fileNames;
public:
//! File deleter
CFileDeleter() {}
//! add given file for deletion
void addFile(const QString &file);
public:
//! file deleter
static CFileDeleter &fileDeleter() { static CFileDeleter f; return f;}
//! add a file (name)
static void addFileForDeletion(const QString &file);
void addFileForDeletion(const QString &file);
//! Add files (names)
void addFilesForDeletion(const QStringList &files);
//! Destructor
~CFileDeleter();
//! Delete files
void deleteFiles();
private:
QStringList m_fileNames;
};
}
/*!
* Utility class, deleting files after time
*/
class BLACKMISC_EXPORT CTimedFileDeleter : public QObject
{
public:
//! Constructor
CTimedFileDeleter(const QString &file, int deleteAfterMs, QObject *parent = nullptr);
protected:
//! \copydoc QObject::timerEvent
virtual void timerEvent(QTimerEvent *event) override;
private:
CFileDeleter m_fileDeleter;
int m_timerId = -1;
};
} // ns
#endif // guard

View File

@@ -22,6 +22,7 @@
#include <QFile>
#include <QDir>
using namespace BlackMisc;
using namespace BlackMisc::Aviation;
using namespace BlackMisc::PhysicalQuantities;
using namespace BlackMisc::Audio;
@@ -35,7 +36,7 @@ namespace BlackSound
m_tones(tones), m_position(0), m_playMode(mode), m_endReached(false), m_oneCycleDurationMs(calculateDurationMs(tones)),
m_device(device), m_audioFormat(format), m_audioOutput(new QAudioOutput(format))
{
Q_ASSERT(tones.size() > 0);
Q_ASSERT_X(tones.size() > 0, Q_FUNC_INFO, "No tones");
}
CSoundGenerator::CSoundGenerator(const QList<Tone> &tones, CNotificationSounds::PlayMode mode, QObject *parent)
@@ -44,7 +45,7 @@ namespace BlackSound
m_device(QAudioDeviceInfo::defaultOutputDevice()), m_audioFormat(CSoundGenerator::defaultAudioFormat()),
m_audioOutput(new QAudioOutput(CSoundGenerator::defaultAudioFormat()))
{
Q_ASSERT(tones.size() > 0);
Q_ASSERT_X(tones.size() > 0, Q_FUNC_INFO, "No tones");
}
CSoundGenerator::~CSoundGenerator()
@@ -92,7 +93,9 @@ namespace BlackSound
// in auto delete mode force deleteLater when thread is finished
if (this->m_playMode == CNotificationSounds::SingleWithAutomaticDeletion)
{
connect(this->m_ownThread, &QThread::finished, this, &CSoundGenerator::deleteLater);
}
// start thread and begin processing by calling start via signal startThread
this->m_ownThread->start();
@@ -517,7 +520,7 @@ namespace BlackSound
mediaPlayer->setVolume(volume); // 0-100
mediaPlayer->play();
// I cannot delete the file here, only after it has been played
if (removeFileAfterPlaying) BlackMisc::CFileDeleter::addFileForDeletion(file);
if (removeFileAfterPlaying) { new CTimedFileDeleter(file, 1000 * 60, QCoreApplication::instance()); }
}
void CSoundGenerator::printAllQtSoundDevices(QTextStream &out)

View File

@@ -1,4 +1,4 @@
/* Copyright (C) 2015
/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
@@ -179,6 +179,9 @@ namespace BlackSound
//! \remarks With singleShot the signal indicates that sound sequence has finished
void stopped();
//! Generator is stopping
void stopping();
public slots:
//! Play sound, open device
//! \param volume 0..100
@@ -190,10 +193,6 @@ namespace BlackSound
//! \param volume 0..100
void startInOwnThread(int volume);
signals:
//! Generator is stopping
void stopping();
private slots:
//! Push mode, timer expired
void pushTimerExpired();
@@ -203,18 +202,18 @@ namespace BlackSound
void generateData();
private:
QList<Tone> m_tones; //!< tones to be played
qint64 m_position; //!< position in buffer
QList<Tone> m_tones; //!< tones to be played
qint64 m_position; //!< position in buffer
CNotificationSounds::PlayMode m_playMode; //!< end data provisioning after playing all tones, play endless loop
bool m_endReached; //!< indicates end in combination with single play
qint64 m_oneCycleDurationMs; //!< how long is one cycle of tones
QByteArray m_buffer; //!< generated buffer for data
QAudioDeviceInfo m_device; //!< audio device
QAudioFormat m_audioFormat; //!< used format
bool m_endReached; //!< indicates end in combination with single play
qint64 m_oneCycleDurationMs; //!< how long is one cycle of tones
QByteArray m_buffer; //!< generated buffer for data
QAudioDeviceInfo m_device; //!< audio device
QAudioFormat m_audioFormat; //!< used format
QScopedPointer<QAudioOutput> m_audioOutput;
QTimer *m_pushTimer = nullptr; //!< Push mode timer
QIODevice *m_pushModeIODevice = nullptr; //!< IO device when used in push mode
QThread *m_ownThread = nullptr;
QTimer *m_pushTimer = nullptr; //!< Push mode timer
QIODevice *m_pushModeIODevice = nullptr; //!< IO device when used in push mode
QThread *m_ownThread = nullptr;
static QDateTime s_selcalStarted;
//! Header for saving .wav files