Skip to content

Commit 5c5a5b0

Browse files
author
Martin Rotter
committed
enhance notification sounds with volume control
1 parent 8b7b57a commit 5c5a5b0

File tree

6 files changed

+68
-16
lines changed

6 files changed

+68
-16
lines changed

src/librssguard/gui/notifications/notificationseditor.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void NotificationsEditor::loadNotifications(const QList<Notification>& notificat
3939
connect(notif_editor, &SingleNotificationEditor::notificationChanged, this, &NotificationsEditor::someNotificationChanged);
4040

4141
m_layout->addWidget(notif_editor);
42-
4342
}
4443
}
4544

src/librssguard/gui/notifications/singlenotificationeditor.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ SingleNotificationEditor::SingleNotificationEditor(const Notification& notificat
2626
connect(m_ui.m_btnBrowseSound, &QPushButton::clicked, this, &SingleNotificationEditor::selectSoundFile);
2727
connect(m_ui.m_txtSound, &QLineEdit::textChanged, this, &SingleNotificationEditor::notificationChanged);
2828
connect(m_ui.m_cbBalloon, &QCheckBox::toggled, this, &SingleNotificationEditor::notificationChanged);
29+
connect(m_ui.m_slidVolume, &QSlider::valueChanged, this, &SingleNotificationEditor::notificationChanged);
2930

3031
QCompleter* completer = new QCompleter(qApp->builtinSounds(), this);
3132
m_ui.m_txtSound->setCompleter(completer);
@@ -34,27 +35,28 @@ SingleNotificationEditor::SingleNotificationEditor(const Notification& notificat
3435
}
3536

3637
Notification SingleNotificationEditor::notification() const {
37-
return Notification(m_notificationEvent, m_ui.m_cbBalloon->isChecked(), m_ui.m_txtSound->text());
38+
return Notification(m_notificationEvent, m_ui.m_cbBalloon->isChecked(), m_ui.m_txtSound->text(), m_ui.m_slidVolume->value());
3839
}
3940

4041
void SingleNotificationEditor::selectSoundFile() {
4142
auto fil = QFileDialog::getOpenFileName(window(), tr("Select sound file"),
4243
qApp->homeFolder(),
43-
tr("WAV files (*.wav)"));
44+
tr("WAV files (*.wav);;MP3 files (*.mp3)"));
4445

4546
if (!fil.isEmpty()) {
4647
m_ui.m_txtSound->setText(fil);
4748
}
4849
}
4950

5051
void SingleNotificationEditor::playSound() {
51-
Notification({}, {}, m_ui.m_txtSound->text()).playSound(qApp);
52+
notification().playSound(qApp);
5253
}
5354

5455
void SingleNotificationEditor::loadNotification(const Notification& notification) {
5556
m_ui.m_txtSound->setText(notification.soundPath());
57+
m_ui.m_slidVolume->setValue(notification.volume());
5658
m_ui.m_cbBalloon->setChecked(notification.balloonEnabled());
57-
setTitle(Notification::nameForEvent(notification.event()));
58-
5959
m_notificationEvent = notification.event();
60+
61+
setTitle(Notification::nameForEvent(notification.event()));
6062
}

src/librssguard/gui/notifications/singlenotificationeditor.ui

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@
8080
</item>
8181
</layout>
8282
</item>
83+
<item row="1" column="1">
84+
<widget class="QSlider" name="m_slidVolume">
85+
<property name="orientation">
86+
<enum>Qt::Horizontal</enum>
87+
</property>
88+
</widget>
89+
</item>
90+
<item row="1" column="0">
91+
<widget class="QLabel" name="label_2">
92+
<property name="text">
93+
<string>Volume</string>
94+
</property>
95+
<property name="buddy">
96+
<cstring>m_slidVolume</cstring>
97+
</property>
98+
</widget>
99+
</item>
83100
</layout>
84101
</widget>
85102
</item>

src/librssguard/miscellaneous/notification.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
#include <QDir>
88

99
#if !defined(Q_OS_OS2)
10-
#include <QSound>
10+
#include <QMediaPlayer>
1111
#endif
1212

13-
Notification::Notification(Notification::Event event, bool balloon, const QString& sound_path)
14-
: m_event(event), m_balloonEnabled(balloon), m_soundPath(sound_path) {}
13+
Notification::Notification(Notification::Event event, bool balloon, const QString& sound_path, int volume)
14+
: m_event(event), m_balloonEnabled(balloon), m_soundPath(sound_path), m_volume(volume) {}
1515

1616
Notification::Event Notification::event() const {
1717
return m_event;
1818
}
1919

20-
void Notification::setEvent(const Event& event) {
20+
void Notification::setEvent(Event event) {
2121
m_event = event;
2222
}
2323

@@ -32,7 +32,26 @@ void Notification::setSoundPath(const QString& sound_path) {
3232
void Notification::playSound(Application* app) const {
3333
if (!m_soundPath.isEmpty()) {
3434
#if !defined(Q_OS_OS2)
35-
QSound::play(QDir::toNativeSeparators(app->replaceDataUserDataFolderPlaceholder(m_soundPath)));
35+
QMediaPlayer* play = new QMediaPlayer(app);
36+
37+
QObject::connect(play, &QMediaPlayer::stateChanged, play, [play](QMediaPlayer::State state) {
38+
if (state == QMediaPlayer::State::StoppedState) {
39+
play->deleteLater();
40+
}
41+
});
42+
43+
if (m_soundPath.startsWith(QSL(":"))) {
44+
play->setMedia(QMediaContent(QUrl(QSL("qrc") + m_soundPath)));
45+
46+
}
47+
else {
48+
play->setMedia(QMediaContent(
49+
QUrl::fromLocalFile(
50+
QDir::toNativeSeparators(app->replaceDataUserDataFolderPlaceholder(m_soundPath)))));
51+
}
52+
53+
play->setVolume(m_volume);
54+
play->play();
3655
#endif
3756
}
3857
}
@@ -63,7 +82,7 @@ QString Notification::nameForEvent(Notification::Event event) {
6382
return QObject::tr("Login failed");
6483

6584
case Notification::Event::NewAppVersionAvailable:
66-
return QObject::tr("New %1 version is available").arg(APP_NAME);
85+
return QObject::tr("New %1 version is available").arg(QSL(APP_NAME));
6786

6887
case Notification::Event::GeneralEvent:
6988
return QObject::tr("Miscellaneous events");
@@ -73,6 +92,14 @@ QString Notification::nameForEvent(Notification::Event event) {
7392
}
7493
}
7594

95+
int Notification::volume() const {
96+
return m_volume;
97+
}
98+
99+
void Notification::setVolume(int volume) {
100+
m_volume = volume;
101+
}
102+
76103
bool Notification::balloonEnabled() const {
77104
return m_balloonEnabled;
78105
}

src/librssguard/miscellaneous/notification.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ class Notification {
3333
LoginFailure = 6
3434
};
3535

36-
explicit Notification(Event event = Event::NoEvent, bool balloon = {}, const QString& sound_path = {});
36+
explicit Notification(Event event = Event::NoEvent, bool balloon = {}, const QString& sound_path = {},
37+
int volume = {});
3738

3839
bool balloonEnabled() const;
3940

4041
Event event() const;
41-
void setEvent(const Event& event);
42+
void setEvent(Event event);
43+
44+
int volume() const;
45+
void setVolume(int volume);
4246

4347
// Returns full path to audio file which should be played when notification
4448
// is launched.
@@ -55,6 +59,7 @@ class Notification {
5559
Event m_event;
5660
bool m_balloonEnabled;
5761
QString m_soundPath;
62+
int m_volume;
5863
};
5964

6065
#endif // NOTIFICATION_H

src/librssguard/miscellaneous/notificationfactory.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ void NotificationFactory::load(Settings* settings) {
4848
auto data = settings->value(GROUP(Notifications), key).toStringList();
4949
auto enabled = data.at(0).toInt() != 0;
5050
auto sound = data.at(1);
51+
auto volume = data.size() > 2 ? data.at(2).toInt() : 100;
5152

52-
m_notifications.append(Notification(event, enabled, sound));
53+
m_notifications.append(Notification(event, enabled, sound, volume));
5354
}
5455
}
5556

@@ -60,7 +61,8 @@ void NotificationFactory::save(const QList<Notification>& new_notifications, Set
6061
for (const auto& n : qAsConst(m_notifications)) {
6162
settings->setValue(GROUP(Notifications), QString::number(int(n.event())), QStringList {
6263
n.balloonEnabled() ? QSL("1") : QSL("0"),
63-
n.soundPath()
64+
n.soundPath(),
65+
QString::number(n.volume())
6466
});
6567
}
6668
}

0 commit comments

Comments
 (0)