From 77a898c9ec5224e1b9a2e49521ee0663f04db5d4 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 22 Mar 2019 03:07:40 +0100 Subject: [PATCH] Utility functions for notification sounds --- src/blackmisc/fileutils.cpp | 13 ++++++++++++ src/blackmisc/fileutils.h | 9 ++++++++- src/blacksound/notificationplayer.cpp | 29 ++++++++++++--------------- src/blacksound/notificationplayer.h | 2 -- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/blackmisc/fileutils.cpp b/src/blackmisc/fileutils.cpp index 835a4def5..7e81bcba8 100644 --- a/src/blackmisc/fileutils.cpp +++ b/src/blackmisc/fileutils.cpp @@ -10,6 +10,7 @@ #include "blackmisc/math/mathutils.h" #include "blackmisc/worker.h" #include "blackmisc/fileutils.h" +#include "blackmisc/directoryutils.h" #include "blackconfig/buildconfig.h" #include @@ -574,4 +575,16 @@ namespace BlackMisc if (fileName.isEmpty()) { return false; } return fileName.contains("swift", Qt::CaseInsensitive) && fileName.contains("installer"); } + + QString CFileUtils::soundFilePathAndFileName(const QString &name) + { + if (name.isEmpty()) { return {}; } + return CFileUtils::appendFilePaths(CDirectoryUtils::soundFilesDirectory(), name); + } + + QUrl CFileUtils::soundFileQUrl(const QString &name) + { + if (name.isEmpty()) { return {}; } + return QUrl::fromLocalFile(CFileUtils::soundFilePathAndFileName(name)); + } } // ns diff --git a/src/blackmisc/fileutils.h b/src/blackmisc/fileutils.h index d01445222..f506b625b 100644 --- a/src/blackmisc/fileutils.h +++ b/src/blackmisc/fileutils.h @@ -13,12 +13,13 @@ #include "blackmisc/blackmiscexport.h" +#include #include #include #include #include -#include #include +#include #include #include @@ -197,6 +198,12 @@ namespace BlackMisc //! swift installer static bool isSwiftInstaller(const QString &fileName); + + //! Returns the full path and file name for a sound file + static QString soundFilePathAndFileName(const QString &name); + + //! QUrl of soundFilePathAndFileName + static QUrl soundFileQUrl(const QString &name); }; } // ns diff --git a/src/blacksound/notificationplayer.cpp b/src/blacksound/notificationplayer.cpp index 01d6b6680..f40b94454 100644 --- a/src/blacksound/notificationplayer.cpp +++ b/src/blacksound/notificationplayer.cpp @@ -7,60 +7,58 @@ */ #include "notificationplayer.h" -#include "blackmisc/directoryutils.h" -#include +#include "blackmisc/fileutils.h" using namespace BlackMisc; using namespace BlackMisc::Audio; namespace BlackSound { - CNotificationPlayer::CNotificationPlayer(QObject *parent) : QObject(parent) { QSoundEffect *effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/error.wav")); + effect->setSource(CFileUtils::soundFileQUrl("error.wav")); m_effects[CNotificationSounds::NotificationError] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/login.wav")); + effect->setSource(CFileUtils::soundFileQUrl("login.wav")); m_effects[CNotificationSounds::NotificationLogin] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/logoff.wav")); + effect->setSource(CFileUtils::soundFileQUrl("logoff.wav")); m_effects[CNotificationSounds::NotificationLogoff] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/privatemessage.wav")); + effect->setSource(CFileUtils::soundFileQUrl("privatemessage.wav")); m_effects[CNotificationSounds::NotificationTextMessagePrivate] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/supervisormessage.wav")); + effect->setSource(CFileUtils::soundFileQUrl("supervisormessage.wav")); m_effects[CNotificationSounds::NotificationTextMessageSupervisor] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/callsignmentioned.wav")); + effect->setSource(CFileUtils::soundFileQUrl("callsignmentioned.wav")); m_effects[CNotificationSounds::NotificationTextCallsignMentioned] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/voiceroomjoined.wav")); + effect->setSource(CFileUtils::soundFileQUrl("voiceroomjoined.wav")); m_effects[CNotificationSounds::NotificationVoiceRoomJoined] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/voiceroomleft.wav")); + effect->setSource(CFileUtils::soundFileQUrl("voiceroomleft.wav")); m_effects[CNotificationSounds::NotificationVoiceRoomLeft] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/noaudiotransmission.wav")); + effect->setSource(CFileUtils::soundFileQUrl("noaudiotransmission.wav")); m_effects[CNotificationSounds::NotificationNoAudioTransmission] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/pttclick.wav")); + effect->setSource(CFileUtils::soundFileQUrl("pttclick.wav")); m_effects[CNotificationSounds::PTTClickKeyDown] = effect; effect = new QSoundEffect(this); - effect->setSource(QUrl::fromLocalFile(CDirectoryUtils::soundFilesDirectory() + "/pttclick.wav")); + effect->setSource(CFileUtils::soundFileQUrl("pttclick.wav")); m_effects[CNotificationSounds::PTTClickKeyUp] = effect; } @@ -73,5 +71,4 @@ namespace BlackSound effect->play(); } } - -} +} // ns diff --git a/src/blacksound/notificationplayer.h b/src/blacksound/notificationplayer.h index c418a781b..21700239b 100644 --- a/src/blacksound/notificationplayer.h +++ b/src/blacksound/notificationplayer.h @@ -18,8 +18,6 @@ #include #include -class QTimer; - namespace BlackSound { //! Player for notification sounds