1 /*
2 SPDX-FileCopyrightText: 2014-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "texttospeech.h"
8
9 #include <KConfig>
10 #include <KConfigGroup>
11 #include <QLocale>
12 #include <QTextToSpeech>
13 #include <QVector>
14
15 using namespace KPIMTextEdit;
TextToSpeech(QObject * parent)16 TextToSpeech::TextToSpeech(QObject *parent)
17 : QObject(parent)
18 {
19 reloadSettings();
20 }
21
~TextToSpeech()22 TextToSpeech::~TextToSpeech()
23 {
24 }
25
reloadSettings()26 void TextToSpeech::reloadSettings()
27 {
28 KConfig config(QStringLiteral("texttospeechrc"));
29 KConfigGroup grp = config.group("Settings");
30 const QString engineName = grp.readEntry("engine");
31 if (!mTextToSpeech) {
32 mTextToSpeech = new QTextToSpeech(engineName, this);
33 connect(mTextToSpeech, &QTextToSpeech::stateChanged, this, &TextToSpeech::slotStateChanged);
34 } else if (mDefaultEngine != engineName) {
35 disconnect(mTextToSpeech, &QTextToSpeech::stateChanged, this, &TextToSpeech::slotStateChanged);
36 delete mTextToSpeech;
37 mTextToSpeech = new QTextToSpeech(engineName, this);
38 connect(mTextToSpeech, &QTextToSpeech::stateChanged, this, &TextToSpeech::slotStateChanged);
39 }
40 mDefaultEngine = engineName;
41 mTextToSpeech->setRate(grp.readEntry("rate", 0.0));
42 mTextToSpeech->setPitch(grp.readEntry("pitch", 0.0));
43 mTextToSpeech->setVolume(grp.readEntry("volume", 0));
44 mTextToSpeech->setLocale(QLocale(grp.readEntry("localeName")));
45 // It doesn't have api for it mTextToSpeech->setVoice(grp.readEntry("voice"));
46 }
47
self()48 TextToSpeech *TextToSpeech::self()
49 {
50 static TextToSpeech s_self;
51 return &s_self;
52 }
53
slotStateChanged()54 void TextToSpeech::slotStateChanged()
55 {
56 TextToSpeech::State state;
57 switch (mTextToSpeech->state()) {
58 case QTextToSpeech::Ready:
59 state = TextToSpeech::Ready;
60 break;
61 case QTextToSpeech::Speaking:
62 state = TextToSpeech::Speaking;
63 break;
64 case QTextToSpeech::Paused:
65 state = TextToSpeech::Paused;
66 break;
67 case QTextToSpeech::BackendError:
68 state = TextToSpeech::BackendError;
69 break;
70 }
71 Q_EMIT stateChanged(state);
72 }
73
isReady() const74 bool TextToSpeech::isReady() const
75 {
76 return mTextToSpeech->state() != QTextToSpeech::BackendError;
77 }
78
say(const QString & text)79 void TextToSpeech::say(const QString &text)
80 {
81 mTextToSpeech->say(text);
82 }
83
stop()84 void TextToSpeech::stop()
85 {
86 mTextToSpeech->stop();
87 }
88
pause()89 void TextToSpeech::pause()
90 {
91 mTextToSpeech->pause();
92 }
93
resume()94 void TextToSpeech::resume()
95 {
96 mTextToSpeech->resume();
97 }
98
setRate(double rate)99 void TextToSpeech::setRate(double rate)
100 {
101 mTextToSpeech->setRate(rate);
102 }
103
setPitch(double pitch)104 void TextToSpeech::setPitch(double pitch)
105 {
106 mTextToSpeech->setPitch(pitch);
107 }
108
setVolume(double volume)109 void TextToSpeech::setVolume(double volume)
110 {
111 mTextToSpeech->setVolume(volume);
112 }
113
volume() const114 double TextToSpeech::volume() const
115 {
116 return mTextToSpeech->volume();
117 }
118
availableLocales() const119 QVector<QLocale> TextToSpeech::availableLocales() const
120 {
121 return mTextToSpeech->availableLocales();
122 }
123
availableVoices() const124 QStringList TextToSpeech::availableVoices() const
125 {
126 QStringList lst;
127 const QVector<QVoice> voices = mTextToSpeech->availableVoices();
128 lst.reserve(voices.count());
129 for (const QVoice &voice : voices) {
130 lst << voice.name();
131 }
132 return lst;
133 }
134
availableEngines() const135 QStringList TextToSpeech::availableEngines() const
136 {
137 return QTextToSpeech::availableEngines();
138 }
139
setLocale(const QLocale & locale) const140 void TextToSpeech::setLocale(const QLocale &locale) const
141 {
142 mTextToSpeech->setLocale(locale);
143 }
144
locale() const145 QLocale TextToSpeech::locale() const
146 {
147 return mTextToSpeech->locale();
148 }
149