From 5c5a5b004c62e61976617c8ee747f36b2b01a7f0 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Tue, 7 Sep 2021 09:30:24 +0200 Subject: [PATCH] enhance notification sounds with volume control --- .../gui/notifications/notificationseditor.cpp | 1 - .../singlenotificationeditor.cpp | 12 +++--- .../notifications/singlenotificationeditor.ui | 17 ++++++++ .../miscellaneous/notification.cpp | 39 ++++++++++++++++--- src/librssguard/miscellaneous/notification.h | 9 ++++- .../miscellaneous/notificationfactory.cpp | 6 ++- 6 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/librssguard/gui/notifications/notificationseditor.cpp b/src/librssguard/gui/notifications/notificationseditor.cpp index c4cd432e3..3bb9aa68b 100644 --- a/src/librssguard/gui/notifications/notificationseditor.cpp +++ b/src/librssguard/gui/notifications/notificationseditor.cpp @@ -39,7 +39,6 @@ void NotificationsEditor::loadNotifications(const QList& notificat connect(notif_editor, &SingleNotificationEditor::notificationChanged, this, &NotificationsEditor::someNotificationChanged); m_layout->addWidget(notif_editor); - } } diff --git a/src/librssguard/gui/notifications/singlenotificationeditor.cpp b/src/librssguard/gui/notifications/singlenotificationeditor.cpp index 5fd294285..0a66538eb 100644 --- a/src/librssguard/gui/notifications/singlenotificationeditor.cpp +++ b/src/librssguard/gui/notifications/singlenotificationeditor.cpp @@ -26,6 +26,7 @@ SingleNotificationEditor::SingleNotificationEditor(const Notification& notificat connect(m_ui.m_btnBrowseSound, &QPushButton::clicked, this, &SingleNotificationEditor::selectSoundFile); connect(m_ui.m_txtSound, &QLineEdit::textChanged, this, &SingleNotificationEditor::notificationChanged); connect(m_ui.m_cbBalloon, &QCheckBox::toggled, this, &SingleNotificationEditor::notificationChanged); + connect(m_ui.m_slidVolume, &QSlider::valueChanged, this, &SingleNotificationEditor::notificationChanged); QCompleter* completer = new QCompleter(qApp->builtinSounds(), this); m_ui.m_txtSound->setCompleter(completer); @@ -34,13 +35,13 @@ SingleNotificationEditor::SingleNotificationEditor(const Notification& notificat } Notification SingleNotificationEditor::notification() const { - return Notification(m_notificationEvent, m_ui.m_cbBalloon->isChecked(), m_ui.m_txtSound->text()); + return Notification(m_notificationEvent, m_ui.m_cbBalloon->isChecked(), m_ui.m_txtSound->text(), m_ui.m_slidVolume->value()); } void SingleNotificationEditor::selectSoundFile() { auto fil = QFileDialog::getOpenFileName(window(), tr("Select sound file"), qApp->homeFolder(), - tr("WAV files (*.wav)")); + tr("WAV files (*.wav);;MP3 files (*.mp3)")); if (!fil.isEmpty()) { m_ui.m_txtSound->setText(fil); @@ -48,13 +49,14 @@ void SingleNotificationEditor::selectSoundFile() { } void SingleNotificationEditor::playSound() { - Notification({}, {}, m_ui.m_txtSound->text()).playSound(qApp); + notification().playSound(qApp); } void SingleNotificationEditor::loadNotification(const Notification& notification) { m_ui.m_txtSound->setText(notification.soundPath()); + m_ui.m_slidVolume->setValue(notification.volume()); m_ui.m_cbBalloon->setChecked(notification.balloonEnabled()); - setTitle(Notification::nameForEvent(notification.event())); - m_notificationEvent = notification.event(); + + setTitle(Notification::nameForEvent(notification.event())); } diff --git a/src/librssguard/gui/notifications/singlenotificationeditor.ui b/src/librssguard/gui/notifications/singlenotificationeditor.ui index 78a5546c0..a4f72c35c 100644 --- a/src/librssguard/gui/notifications/singlenotificationeditor.ui +++ b/src/librssguard/gui/notifications/singlenotificationeditor.ui @@ -80,6 +80,23 @@ + + + + Qt::Horizontal + + + + + + + Volume + + + m_slidVolume + + + diff --git a/src/librssguard/miscellaneous/notification.cpp b/src/librssguard/miscellaneous/notification.cpp index 261601a4b..f9f110c6e 100644 --- a/src/librssguard/miscellaneous/notification.cpp +++ b/src/librssguard/miscellaneous/notification.cpp @@ -7,17 +7,17 @@ #include #if !defined(Q_OS_OS2) -#include +#include #endif -Notification::Notification(Notification::Event event, bool balloon, const QString& sound_path) - : m_event(event), m_balloonEnabled(balloon), m_soundPath(sound_path) {} +Notification::Notification(Notification::Event event, bool balloon, const QString& sound_path, int volume) + : m_event(event), m_balloonEnabled(balloon), m_soundPath(sound_path), m_volume(volume) {} Notification::Event Notification::event() const { return m_event; } -void Notification::setEvent(const Event& event) { +void Notification::setEvent(Event event) { m_event = event; } @@ -32,7 +32,26 @@ void Notification::setSoundPath(const QString& sound_path) { void Notification::playSound(Application* app) const { if (!m_soundPath.isEmpty()) { #if !defined(Q_OS_OS2) - QSound::play(QDir::toNativeSeparators(app->replaceDataUserDataFolderPlaceholder(m_soundPath))); + QMediaPlayer* play = new QMediaPlayer(app); + + QObject::connect(play, &QMediaPlayer::stateChanged, play, [play](QMediaPlayer::State state) { + if (state == QMediaPlayer::State::StoppedState) { + play->deleteLater(); + } + }); + + if (m_soundPath.startsWith(QSL(":"))) { + play->setMedia(QMediaContent(QUrl(QSL("qrc") + m_soundPath))); + + } + else { + play->setMedia(QMediaContent( + QUrl::fromLocalFile( + QDir::toNativeSeparators(app->replaceDataUserDataFolderPlaceholder(m_soundPath))))); + } + + play->setVolume(m_volume); + play->play(); #endif } } @@ -63,7 +82,7 @@ QString Notification::nameForEvent(Notification::Event event) { return QObject::tr("Login failed"); case Notification::Event::NewAppVersionAvailable: - return QObject::tr("New %1 version is available").arg(APP_NAME); + return QObject::tr("New %1 version is available").arg(QSL(APP_NAME)); case Notification::Event::GeneralEvent: return QObject::tr("Miscellaneous events"); @@ -73,6 +92,14 @@ QString Notification::nameForEvent(Notification::Event event) { } } +int Notification::volume() const { + return m_volume; +} + +void Notification::setVolume(int volume) { + m_volume = volume; +} + bool Notification::balloonEnabled() const { return m_balloonEnabled; } diff --git a/src/librssguard/miscellaneous/notification.h b/src/librssguard/miscellaneous/notification.h index da0b3a403..291c24f26 100644 --- a/src/librssguard/miscellaneous/notification.h +++ b/src/librssguard/miscellaneous/notification.h @@ -33,12 +33,16 @@ class Notification { LoginFailure = 6 }; - explicit Notification(Event event = Event::NoEvent, bool balloon = {}, const QString& sound_path = {}); + explicit Notification(Event event = Event::NoEvent, bool balloon = {}, const QString& sound_path = {}, + int volume = {}); bool balloonEnabled() const; Event event() const; - void setEvent(const Event& event); + void setEvent(Event event); + + int volume() const; + void setVolume(int volume); // Returns full path to audio file which should be played when notification // is launched. @@ -55,6 +59,7 @@ class Notification { Event m_event; bool m_balloonEnabled; QString m_soundPath; + int m_volume; }; #endif // NOTIFICATION_H diff --git a/src/librssguard/miscellaneous/notificationfactory.cpp b/src/librssguard/miscellaneous/notificationfactory.cpp index 25d526727..e1ed09e10 100644 --- a/src/librssguard/miscellaneous/notificationfactory.cpp +++ b/src/librssguard/miscellaneous/notificationfactory.cpp @@ -48,8 +48,9 @@ void NotificationFactory::load(Settings* settings) { auto data = settings->value(GROUP(Notifications), key).toStringList(); auto enabled = data.at(0).toInt() != 0; auto sound = data.at(1); + auto volume = data.size() > 2 ? data.at(2).toInt() : 100; - m_notifications.append(Notification(event, enabled, sound)); + m_notifications.append(Notification(event, enabled, sound, volume)); } } @@ -60,7 +61,8 @@ void NotificationFactory::save(const QList& new_notifications, Set for (const auto& n : qAsConst(m_notifications)) { settings->setValue(GROUP(Notifications), QString::number(int(n.event())), QStringList { n.balloonEnabled() ? QSL("1") : QSL("0"), - n.soundPath() + n.soundPath(), + QString::number(n.volume()) }); } }