| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "miscellaneous/notification.h" |
| 4 | |
| 5 | #include "miscellaneous/application.h" |
| 6 | |
| 7 | #include <QDir> |
| 8 | |
| 9 | #if !defined(Q_OS_OS2) |
| 10 | #include <QMediaPlayer> |
| 11 | #include <QSoundEffect> |
| 12 | |
| 13 | #if QT_VERSION_MAJOR == 6 |
| 14 | #include <QAudioOutput> |
| 15 | #endif |
| 16 | #endif |
| 17 | |
| 18 | Notification::Notification(Notification::Event event, |
| 19 | bool balloon, |
| 20 | bool dialog, |
| 21 | bool play_sound, |
| 22 | const QString& sound_path, |
| 23 | int volume) |
| 24 | : m_event(event), m_balloonEnabled(balloon), m_dialogEnabled(dialog), m_soundEnabled(play_sound), |
| 25 | m_soundPath(sound_path), m_volume(volume) {} |
| 26 | |
| 27 | Notification::Event Notification::event() const { |
| 28 | return m_event; |
| 29 | } |
| 30 | |
| 31 | void Notification::setEvent(Event event) { |
| 32 | m_event = event; |
| 33 | } |
| 34 | |
| 35 | QString Notification::soundPath() const { |
| 36 | return m_soundPath; |
| 37 | } |
| 38 | |
| 39 | void Notification::setSoundPath(const QString& sound_path) { |
| 40 | m_soundPath = sound_path; |
| 41 | } |
| 42 | |
| 43 | void Notification::playSound(Application* app) const { |
| 44 | if (!m_soundPath.isEmpty()) { |
| 45 | #if !defined(Q_OS_OS2) |
| 46 | if (m_soundPath.endsWith(QSL(".wav"), Qt::CaseSensitivity::CaseInsensitive)) { |
| 47 | qDebugNN << LOGSEC_CORE << "Using QSoundEffect to play notification sound."; |
| 48 | |
| 49 | QSoundEffect* play = new QSoundEffect(app); |
| 50 | |
| 51 | QObject::connect(play, &QSoundEffect::playingChanged, play, [play]() { |
| 52 | if (!play->isPlaying()) { |
| 53 | play->deleteLater(); |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | if (m_soundPath.startsWith(QSL(":"))) { |
| 58 | play->setSource(QUrl(QSL("qrc") + m_soundPath)); |
| 59 | } |
| 60 | else { |
| 61 | play |
| 62 | ->setSource(QUrl::fromLocalFile(QDir::toNativeSeparators(app |
| 63 | ->replaceUserDataFolderPlaceholder(m_soundPath)))); |
| 64 | } |
| 65 | |
| 66 | play->setVolume(fractionalVolume()); |
| 67 | play->play(); |
| 68 | } |
| 69 | else { |
| 70 | qDebugNN << LOGSEC_CORE << "Using QMediaPlayer to play notification sound."; |
| 71 | |
| 72 | QMediaPlayer* play = new QMediaPlayer(app); |
| 73 | |
| 74 | #if QT_VERSION_MAJOR == 6 |
| 75 | QAudioOutput* out = new QAudioOutput(app); |
| 76 | |
| 77 | play->setAudioOutput(out); |
| 78 | |
| 79 | QObject::connect(play, &QMediaPlayer::playbackStateChanged, play, [play, out](QMediaPlayer::PlaybackState state) { |
| 80 | if (state == QMediaPlayer::PlaybackState::StoppedState) { |
| 81 | out->deleteLater(); |
| 82 | play->deleteLater(); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | if (m_soundPath.startsWith(QSL(":"))) { |
| 87 | play->setSource(QUrl(QSL("qrc") + m_soundPath)); |
| 88 | } |
| 89 | else { |
| 90 | play |
| 91 | ->setSource(QUrl::fromLocalFile(QDir::toNativeSeparators(app |
| 92 | ->replaceUserDataFolderPlaceholder(m_soundPath)))); |
| 93 | } |
| 94 | |
| 95 | play->audioOutput()->setVolume(fractionalVolume()); |
| 96 | play->play(); |
| 97 | #else |
| 98 | QObject::connect(play, &QMediaPlayer::stateChanged, play, [play](QMediaPlayer::State state) { |
| 99 | if (state == QMediaPlayer::State::StoppedState) { |
| 100 | play->deleteLater(); |
| 101 | } |
| 102 | }); |
| 103 | |
| 104 | if (m_soundPath.startsWith(QSL(":"))) { |
| 105 | play->setMedia(QMediaContent(QUrl(QSL("qrc") + m_soundPath))); |
| 106 | } |
| 107 | else { |
| 108 | play->setMedia(QMediaContent( |
| 109 | QUrl::fromLocalFile(QDir::toNativeSeparators(app->replaceUserDataFolderPlaceholder(m_soundPath))))); |
| 110 | } |
| 111 | |
| 112 | play->setVolume(m_volume); |
| 113 | play->play(); |
| 114 | #endif |
| 115 | } |
| 116 | #endif |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | QList<Notification::Event> Notification::allEvents() { |
| 121 | return {Event::GeneralEvent, |
| 122 | Event::NewUnreadArticlesFetched, |
| 123 | Event::ArticlesFetchingStarted, |
| 124 | Event::ArticlesFetchingError, |
| 125 | Event::LoginDataRefreshed, |
| 126 | Event::LoginFailure, |
| 127 | Event::NewAppVersionAvailable, |
| 128 | Event::NodePackageUpdated, |
| 129 | Event::NodePackageFailedToUpdate}; |
| 130 | } |
| 131 | |
| 132 | QString Notification::nameForEvent(Notification::Event event) { |
| 133 | switch (event) { |
| 134 | case Notification::Event::NewUnreadArticlesFetched: |
| 135 | return QObject::tr("New (unread) articles fetched"); |
| 136 | |
| 137 | case Notification::Event::ArticlesFetchingStarted: |
| 138 | return QObject::tr("Fetching articles right now"); |
| 139 | |
| 140 | case Notification::Event::LoginDataRefreshed: |
| 141 | return QObject::tr("Login data refreshed"); |
| 142 | |
| 143 | case Notification::Event::LoginFailure: |
| 144 | return QObject::tr("Login failed"); |
| 145 | |
| 146 | case Notification::Event::NewAppVersionAvailable: |
| 147 | return QObject::tr("New %1 version is available").arg(QSL(APP_NAME)); |
| 148 | |
| 149 | case Notification::Event::GeneralEvent: |
| 150 | return QObject::tr("Miscellaneous events"); |
| 151 | |
| 152 | case Notification::Event::NodePackageUpdated: |
| 153 | return QObject::tr("Node.js - package(s) updated"); |
| 154 | |
| 155 | case Notification::Event::NodePackageFailedToUpdate: |
| 156 | return QObject::tr("Node.js - package(s) failed to update"); |
| 157 | |
| 158 | case Notification::Event::ArticlesFetchingError: |
| 159 | return QObject::tr("Error when fetching articles"); |
| 160 | |
| 161 | default: |
| 162 | return QObject::tr("Unknown event"); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | bool Notification::dialogEnabled() const { |
| 167 | return m_dialogEnabled; |
| 168 | } |
| 169 | |
| 170 | void Notification::setSoundEnabled(bool play_sound) { |
| 171 | m_soundEnabled = play_sound; |
| 172 | } |
| 173 | |
| 174 | int Notification::volume() const { |
| 175 | return m_volume; |
| 176 | } |
| 177 | |
| 178 | qreal Notification::fractionalVolume() const { |
| 179 | return (m_volume * 1.0f) / 100.0f; |
| 180 | } |
| 181 | |
| 182 | void Notification::setVolume(int volume) { |
| 183 | m_volume = volume; |
| 184 | } |
| 185 | |
| 186 | bool Notification::soundEnabled() const { |
| 187 | return m_soundEnabled; |
| 188 | } |
| 189 | |
| 190 | bool Notification::balloonEnabled() const { |
| 191 | return m_balloonEnabled; |
| 192 | } |