File indexing completed on 2024-12-01 06:31:44

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015, 2022 by Jeremy Whiting <jpwhiting@kde.org>        *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program 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 General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 #include "texttospeechsystem.h"
0022 
0023 #include <QTextToSpeech>
0024 
0025 #include <KConfigGroup>
0026 #include <KSharedConfig>
0027 
0028 #include "speech.h"
0029 
0030 TextToSpeechSystem::TextToSpeechSystem(QObject *parent)
0031     : QObject(parent)
0032     , encoding(QStringConverter::System)
0033     , stdIn(true)
0034     , useQtSpeech(true)
0035     , ttsEngine(QLatin1String("speechd"))
0036     , m_speech(new QTextToSpeech(ttsEngine))
0037 {
0038 }
0039 
0040 TextToSpeechSystem::~TextToSpeechSystem()
0041 {
0042     delete m_speech;
0043 }
0044 
0045 void TextToSpeechSystem::speak(const QString &text, const QString &language)
0046 {
0047     if (!text.isEmpty()) {
0048         if (useQtSpeech) {
0049             m_speech->say(text);
0050             return;
0051         }
0052 
0053         (new Speech())->speak(ttsCommand, stdIn, text, language, QStringConverter::Encoding(encoding));
0054     }
0055 }
0056 
0057 void TextToSpeechSystem::readOptions(const QString &langGroup)
0058 {
0059     KConfigGroup cg(KSharedConfig::openConfig(), langGroup);
0060     ttsCommand = cg.readPathEntry("Command", QString());
0061     stdIn = cg.readEntry("StdIn", true);
0062     useQtSpeech = cg.readEntry("useQtSpeech", true);
0063     ttsEngine = cg.readEntry("ttsEngine", "speechd");
0064     // No default, depends on current locale, etc. so just naturally
0065     // select first voice if none set by user.
0066     ttsVoice = cg.readEntry("ttsVoice", "");
0067 
0068     // Get encoding from settings, but default to system encoding, which
0069     // is what "Local" used to be
0070     encoding = QStringConverter::Encoding(cg.readEntry("encoding", int(QStringConverter::System)));
0071 }
0072 
0073 void TextToSpeechSystem::saveOptions(const QString &langGroup)
0074 {
0075     KConfigGroup cg(KSharedConfig::openConfig(), langGroup);
0076     cg.writePathEntry("Command", ttsCommand);
0077     cg.writeEntry("StdIn", stdIn);
0078     cg.writeEntry("useQtSpeech", useQtSpeech);
0079     cg.writeEntry("ttsEngine", ttsEngine);
0080     cg.writeEntry("ttsVoice", ttsVoice);
0081     cg.writeEntry("encoding", encoding);
0082 
0083     delete m_speech;
0084     m_speech = new QTextToSpeech(ttsEngine);
0085     const QList<QVoice> voices = m_speech->availableVoices();
0086     for (const QVoice &voice : voices) {
0087         if (voice.name() == ttsVoice) {
0088             m_speech->setVoice(voice);
0089         }
0090     }
0091 }