File indexing completed on 2024-06-16 04:29:31

0001 /*
0002     SnoreNotify is a Notification Framework based on Qt
0003     Copyright (C) 2015  Hannah von Reth <vonreth@kde.org>
0004 
0005     SnoreNotify is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU Lesser General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     SnoreNotify is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public License
0016     along with SnoreNotify.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 #include "sound.h"
0019 #include "soundconstants.h"
0020 
0021 #include <QtMultimedia/QMediaPlayer>
0022 #include <QTimer>
0023 
0024 namespace SnorePlugin {
0025 
0026 Sound::Sound():
0027     m_player(new QMediaPlayer(this))
0028 {
0029 //    connect(m_player,QMediaPlayer::positionChanged,[](qint64 pos){
0030 //        qCDebug(SNORE) << "Player: " << pos;
0031 //    });
0032     connect(m_player, &QMediaPlayer::stateChanged, this, [](QMediaPlayer::State state) {
0033         qCDebug(SNORE) << "Player: " << state;
0034     });
0035 }
0036 
0037 void Sound::setDefaultSettings()
0038 {
0039     setDefaultSettingsValue(SoundConstants::Volume, 50);
0040     SnoreSecondaryBackend::setDefaultSettings();
0041 }
0042 
0043 void Sound::slotNotificationDisplayed(Snore::Notification notification)
0044 {
0045     if (notification.hints().value("silent").toBool()) {
0046         return;
0047     }
0048     m_player->setVolume(settingsValue(SoundConstants::Volume).toInt());
0049 
0050     QString sound = notification.hints().value("sound").toString();
0051     if (sound.isEmpty()) {
0052         sound = settingsValue(SoundConstants::SoundKey).toString();
0053     }
0054     qCDebug(SNORE) << "SoundFile:" << sound;
0055     if (!sound.isEmpty()) {
0056         m_player->setMedia(QUrl::fromLocalFile(sound));
0057         qCDebug(SNORE) << "SoundFile:" << m_player->media().canonicalUrl();
0058         m_player->play();
0059         QTimer *timeout = new QTimer(this);
0060         timeout->setSingleShot(true);
0061         timeout->setInterval(notification.timeout() * 1000);
0062         connect(timeout, &QTimer::timeout, [this, timeout] {
0063             m_player->stop();
0064             timeout->deleteLater();
0065         });
0066     }
0067 }
0068 
0069 }