File indexing completed on 2024-04-28 11:43:48

0001 /*
0002     SPDX-FileCopyrightText: 2007 Olivier Goffart <ogoffart at kde.org>
0003     SPDX-FileCopyrightText: 2009 Laurent Montel <montel@kde.org>
0004     SPDX-FileCopyrightText: 2015 Jeremy Whiting <jpwhiting@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "notifybytts.h"
0010 #include "debug_p.h"
0011 #include "knotification.h"
0012 #include "knotifyconfig.h"
0013 #include <KMacroExpander>
0014 
0015 #include <QTextToSpeech>
0016 
0017 NotifyByTTS::NotifyByTTS(QObject *parent)
0018     : KNotificationPlugin(parent)
0019     , m_speech(new QTextToSpeech(this))
0020 {
0021 }
0022 
0023 NotifyByTTS::~NotifyByTTS()
0024 {
0025     delete m_speech;
0026 }
0027 
0028 void NotifyByTTS::notify(KNotification *notification, KNotifyConfig *config)
0029 {
0030 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0031     if (m_speech->state() != QTextToSpeech::BackendError) {
0032 #else
0033     if (m_speech->state() != QTextToSpeech::Error) {
0034 #endif
0035         QString say = config->readEntry(QStringLiteral("TTS"));
0036 
0037         if (!say.isEmpty()) {
0038             // Create a hash of characters to strings to expand text into the notification text.
0039             QHash<QChar, QString> subst;
0040             subst.insert(QLatin1Char('e'), notification->eventId());
0041             subst.insert(QLatin1Char('a'), notification->appName());
0042             subst.insert(QLatin1Char('s'), notification->text());
0043             // subst.insert('w', QString::number((quintptr)config->winId));
0044             // subst.insert('i', QString::number(id));
0045             subst.insert(QLatin1Char('m'), notification->text());
0046             say = KMacroExpander::expandMacrosShellQuote(say, subst);
0047         }
0048 
0049         if (say.isEmpty()) {
0050             say = notification->text(); // fallback on the plain text
0051         }
0052 
0053         m_speech->say(say);
0054 
0055         Q_EMIT finished(notification);
0056     } else {
0057         qCDebug(LOG_KNOTIFICATIONS) << "Speech backend has an error, not speaking";
0058     }
0059 }
0060 
0061 #include "moc_notifybytts.cpp"