From 68d0b8cc98d4a3d46e9cc3c20f9455cdf8fc58d4 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 30 Sep 2015 05:01:34 +0200 Subject: [PATCH] During #475 found issue with deleter * changed / improved deleter * formatting in related class soundgenerator --- src/blackmisc/filedeleter.cpp | 43 ++++++++++++++++++++++++----- src/blackmisc/filedeleter.h | 46 +++++++++++++++++++++---------- src/blacksound/soundgenerator.cpp | 9 ++++-- src/blacksound/soundgenerator.h | 29 ++++++++++--------- 4 files changed, 87 insertions(+), 40 deletions(-) diff --git a/src/blackmisc/filedeleter.cpp b/src/blackmisc/filedeleter.cpp index 650fc4ae7..0f1df62e6 100644 --- a/src/blackmisc/filedeleter.cpp +++ b/src/blackmisc/filedeleter.cpp @@ -7,28 +7,57 @@ * contained in the LICENSE file. */ -#include #include "filedeleter.h" +#include 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 diff --git a/src/blackmisc/filedeleter.h b/src/blackmisc/filedeleter.h index 808bfedac..46e16bf8f 100644 --- a/src/blackmisc/filedeleter.h +++ b/src/blackmisc/filedeleter.h @@ -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 diff --git a/src/blacksound/soundgenerator.cpp b/src/blacksound/soundgenerator.cpp index 148babf79..2b36df6d3 100644 --- a/src/blacksound/soundgenerator.cpp +++ b/src/blacksound/soundgenerator.cpp @@ -22,6 +22,7 @@ #include #include +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 &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) diff --git a/src/blacksound/soundgenerator.h b/src/blacksound/soundgenerator.h index 03a6e218f..945eb9ca9 100644 --- a/src/blacksound/soundgenerator.h +++ b/src/blacksound/soundgenerator.h @@ -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 m_tones; //!< tones to be played - qint64 m_position; //!< position in buffer + QList 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 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