1 /****************************************************************************
2 Configuration widget and functions for FreeTTS (interactive) plug in
4 Copyright : (C) 2004 Paul Giannaros
6 Original author: Paul Giannaros <ceruleanblaze@gmail.com>
7 Current Maintainer: Paul Giannaros <ceruleanblaze@gmail.com>
8 ******************************************************************************/
10 /***************************************************************************
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; version 2 of the License. *
16 ***************************************************************************/
22 #include <qstringlist.h>
24 #include <qapplication.h>
26 #include <QVBoxLayout>
30 #include <ktempfile.h>
31 #include <kstandarddirs.h>
32 #include <kmessagebox.h>
34 #include <kprogress.h>
37 #include <pluginconf.h>
38 #include <testplayer.h>
41 #include "freettsconf.h"
42 #include "freettsconfigwidget.h"
45 FreeTTSConf::FreeTTSConf( QWidget
* parent
, const char* name
, const QStringList
&/*args*/) :
46 PlugInConf( parent
, name
) {
48 // kdDebug() << "FreeTTSConf::FreeTTSConf: Running" << endl;
52 QVBoxLayout
*layout
= new QVBoxLayout(this, KDialog::marginHint(),
53 KDialog::spacingHint(), "FreeTTSConfigWidgetLayout");
54 layout
->setAlignment (Qt::AlignTop
);
55 m_widget
= new FreeTTSConfWidget(this, "FreeTTSConfigWidget");
56 layout
->addWidget(m_widget
);
60 connect(m_widget
->freettsPath
, SIGNAL(textChanged(const QString
&)),
61 this, SLOT(configChanged()));
62 connect(m_widget
->freettsTest
, SIGNAL(clicked()), this, SLOT(slotFreeTTSTest_clicked()));
66 FreeTTSConf::~FreeTTSConf() {
67 // kdDebug() << "Running: FreeTTSConf::~FreeTTSConf()" << endl;
68 if (!m_waveFile
.isNull()) QFile::remove(m_waveFile
);
73 void FreeTTSConf::load(KConfig
*config
, const QString
&configGroup
) {
74 // kdDebug() << "FreeTTSConf::load: Running" << endl;
76 config
->setGroup(configGroup
);
77 QString freeTTSJar
= config
->readEntry("FreeTTSJarPath", QString::null
);
78 if (freeTTSJar
.isEmpty())
80 config
->setGroup("FreeTTS");
81 freeTTSJar
= config
->readEntry("FreeTTSJarPath", QString::null
);
83 if (freeTTSJar
.isEmpty())
84 freeTTSJar
= getLocation("freetts.jar");
85 m_widget
->freettsPath
->setURL(freeTTSJar
);
86 /// If freettsPath is still empty, then we couldn't find the file in the path.
89 void FreeTTSConf::save(KConfig
*config
, const QString
&configGroup
){
90 // kdDebug() << "FreeTTSConf::save: Running" << endl;
92 config
->setGroup("FreeTTS");
93 config
->writeEntry("FreeTTSJarPath",
94 realFilePath(m_widget
->freettsPath
->url()));
96 config
->setGroup(configGroup
);
97 if(m_widget
->freettsPath
->url().isEmpty())
98 KMessageBox::sorry(0, i18n("Unable to locate freetts.jar in your path.\nPlease specify the path to freetts.jar in the Properties tab before using KDE Text-to-Speech"), i18n("KDE Text-to-Speech"));
99 config
->writeEntry("FreeTTSJarPath",
100 realFilePath(m_widget
->freettsPath
->url()));
103 void FreeTTSConf::defaults(){
104 // kdDebug() << "Running: FreeTTSConf::defaults()" << endl;
105 m_widget
->freettsPath
->setURL("");
108 void FreeTTSConf::setDesiredLanguage(const QString
&lang
)
110 m_languageCode
= lang
;
113 QString
FreeTTSConf::getTalkerCode()
115 QString freeTTSJar
= realFilePath(m_widget
->freettsPath
->url());
116 if (!freeTTSJar
.isEmpty())
118 if (!getLocation(freeTTSJar
).isEmpty())
121 "<voice lang=\"%1\" name=\"%2\" gender=\"%3\" />"
122 "<prosody volume=\"%4\" rate=\"%5\" />"
123 "<kttsd synthesizer=\"%6\" />")
132 return QString::null
;
135 // QString FreeTTSConf::getLocation(const QString &name) {
136 // /// Iterate over the path and see if 'name' exists in it. Return the
137 // /// full path to it if it does. Else return an empty QString.
138 // kdDebug() << "FreeTTSConf::getLocation: Searching for " << name << " in the path... " << endl;
139 // kdDebug() << m_path << endl;
140 // for(QStringList::iterator it = m_path.begin(); it != m_path.end(); ++it) {
141 // QString fullName = *it;
144 // /// The user either has the directory of the file in the path...
145 // if(QFile::exists(fullName)) {
147 // kdDebug() << fullName << endl;
149 // /// ....Or the file itself
150 // else if(QFileInfo(*it).baseName().append(QString(".").append(QFileInfo(*it).extension())) == name) {
152 // kdDebug() << fullName << endl;
159 void FreeTTSConf::slotFreeTTSTest_clicked()
161 // kdDebug() << "FreeTTSConf::slotFreeTTSTest_clicked(): Running" << endl;
162 // If currently synthesizing, stop it.
164 m_freettsProc
->stopText();
167 m_freettsProc
= new FreeTTSProc();
168 connect (m_freettsProc
, SIGNAL(stopped()), this, SLOT(slotSynthStopped()));
170 // Create a temp file name for the wave file.
171 KTempFile
tempFile (locateLocal("tmp", "freettsplugin-"), ".wav");
172 QString tmpWaveFile
= tempFile
.file()->name();
175 // Get test message in the language of the voice.
176 QString testMsg
= testMessage(m_languageCode
);
178 // Tell user to wait.
179 m_progressDlg
= new KProgressDialog(m_widget
, "kttsmgr_freetts_testdlg",
183 m_progressDlg
->progressBar()->hide();
184 m_progressDlg
->setAllowCancel(true);
186 // I think FreeTTS only officialy supports English, but if anyone knows of someone
187 // whos built up a different language lexicon and has it working with FreeTTS gimme an email at ceruleanblaze@gmail.com
188 connect (m_freettsProc
, SIGNAL(synthFinished()), this, SLOT(slotSynthFinished()));
189 m_freettsProc
->synth(
192 realFilePath(m_widget
->freettsPath
->url()));
194 // Display progress dialog modally. Processing continues when plugin signals synthFinished,
195 // or if user clicks Cancel button.
196 m_progressDlg
->exec();
197 disconnect (m_freettsProc
, SIGNAL(synthFinished()), this, SLOT(slotSynthFinished()));
198 if (m_progressDlg
->wasCancelled()) m_freettsProc
->stopText();
199 delete m_progressDlg
;
203 void FreeTTSConf::slotSynthFinished()
205 // If user canceled, progress dialog is gone, so exit.
208 m_freettsProc
->ackFinished();
211 // Hide the Cancel button so user can't cancel in the middle of playback.
212 m_progressDlg
->showCancelButton(false);
213 // Get new wavefile name.
214 m_waveFile
= m_freettsProc
->getFilename();
215 // Tell synth we're done.
216 m_freettsProc
->ackFinished();
217 // Play the wave file (possibly adjusting its Speed).
218 // Player object deletes the wave file when done.
219 if (m_player
) m_player
->play(m_waveFile
);
220 QFile::remove(m_waveFile
);
221 m_waveFile
= QString::null
;
222 if (m_progressDlg
) m_progressDlg
->close();
225 void FreeTTSConf::slotSynthStopped()
227 // Clean up after canceling test.
228 QString filename
= m_freettsProc
->getFilename();
229 if (!filename
.isNull()) QFile::remove(filename
);
232 #include "freettsconf.moc"