Compile
[kdeaccessibility.git] / kttsd / plugins / freetts / freettsconf.cpp
blobe94788ad099d32720ab35a4b62941d3e175ab54b
1 /****************************************************************************
2 Configuration widget and functions for FreeTTS (interactive) plug in
3 -------------------
4 Copyright : (C) 2004 Paul Giannaros
5 -------------------
6 Original author: Paul Giannaros <ceruleanblaze@gmail.com>
7 Current Maintainer: Paul Giannaros <ceruleanblaze@gmail.com>
8 ******************************************************************************/
10 /***************************************************************************
11 * *
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. *
15 * *
16 ***************************************************************************/
18 // Qt includes.
19 #include <qlayout.h>
20 #include <qlabel.h>
21 #include <qstring.h>
22 #include <qstringlist.h>
23 #include <qfile.h>
24 #include <qapplication.h>
25 //Added by qt3to4:
26 #include <QVBoxLayout>
28 // KDE includes.
29 #include <kdialog.h>
30 #include <ktempfile.h>
31 #include <kstandarddirs.h>
32 #include <kmessagebox.h>
33 #include <klocale.h>
34 #include <kprogress.h>
36 // KTTS includes.
37 #include <pluginconf.h>
38 #include <testplayer.h>
40 // FreeTTS includes.
41 #include "freettsconf.h"
42 #include "freettsconfigwidget.h"
44 /** Constructor */
45 FreeTTSConf::FreeTTSConf( QWidget* parent, const char* name, const QStringList&/*args*/) :
46 PlugInConf( parent, name ) {
48 // kdDebug() << "FreeTTSConf::FreeTTSConf: Running" << endl;
49 m_freettsProc = 0;
50 m_progressDlg = 0;
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);
58 defaults();
60 connect(m_widget->freettsPath, SIGNAL(textChanged(const QString&)),
61 this, SLOT(configChanged()));
62 connect(m_widget->freettsTest, SIGNAL(clicked()), this, SLOT(slotFreeTTSTest_clicked()));
65 /** Destructor */
66 FreeTTSConf::~FreeTTSConf() {
67 // kdDebug() << "Running: FreeTTSConf::~FreeTTSConf()" << endl;
68 if (!m_waveFile.isNull()) QFile::remove(m_waveFile);
69 delete m_freettsProc;
70 delete m_progressDlg;
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())
120 return QString(
121 "<voice lang=\"%1\" name=\"%2\" gender=\"%3\" />"
122 "<prosody volume=\"%4\" rate=\"%5\" />"
123 "<kttsd synthesizer=\"%6\" />")
124 .arg(m_languageCode)
125 .arg("fixed")
126 .arg("neutral")
127 .arg("medium")
128 .arg("medium")
129 .arg("FreeTTS");
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;
142 // fullName += "/";
143 // fullName += name;
144 // /// The user either has the directory of the file in the path...
145 // if(QFile::exists(fullName)) {
146 // return fullName;
147 // kdDebug() << fullName << endl;
148 // }
149 // /// ....Or the file itself
150 // else if(QFileInfo(*it).baseName().append(QString(".").append(QFileInfo(*it).extension())) == name) {
151 // return fullName;
152 // kdDebug() << fullName << endl;
153 // }
154 // }
155 // return "";
156 // }
159 void FreeTTSConf::slotFreeTTSTest_clicked()
161 // kdDebug() << "FreeTTSConf::slotFreeTTSTest_clicked(): Running" << endl;
162 // If currently synthesizing, stop it.
163 if (m_freettsProc)
164 m_freettsProc->stopText();
165 else
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();
173 tempFile.close();
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",
180 i18n("Testing"),
181 i18n("Testing."),
182 true);
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(
190 testMsg,
191 tmpWaveFile,
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;
200 m_progressDlg = 0;
203 void FreeTTSConf::slotSynthFinished()
205 // If user canceled, progress dialog is gone, so exit.
206 if (!m_progressDlg)
208 m_freettsProc->ackFinished();
209 return;
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"