GUI and build rules fix
[qstardict] / qstardict / speaker.cpp
1 /*****************************************************************************
2  * speaker.cpp - QStarDict, a StarDict clone written using Qt                *
3  * Copyright (C) 2008 Alexander Rodin                                        *
4  *                                                                           *
5  * This program is free software; you can redistribute it and/or modify      *
6  * it under the terms of the GNU General Public License as published by      *
7  * the Free Software Foundation; either version 2 of the License, or         *
8  * (at your option) any later version.                                       *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU General Public License for more details.                              *
14  *                                                                           *
15  * You should have received a copy of the GNU General Public License along   *
16  * with this program; if not, write to the Free Software Foundation, Inc.,   *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
18  *****************************************************************************/
19
20 #include "speaker.h"
21
22 #include <QProcess>
23 #include <QSettings>
24
25 namespace QStarDict
26 {
27
28 Speaker::Speaker()
29 {
30     m_speechProcess = new QProcess;
31     QSettings settings;
32     m_speechCmd = settings.value("Speaker/speechCmd", "espeak").toString();
33 }
34
35 Speaker::~Speaker()
36 {
37     QSettings settings;
38     settings.setValue("Speaker/speechCmd", m_speechCmd);
39     delete m_speechProcess;
40 }
41
42 void Speaker::speak(const QString &word)
43 {
44     if (m_speechCmd.isEmpty())
45         return;
46
47     if (m_speechProcess->state() != QProcess::NotRunning)
48     m_speechProcess->kill();
49     
50     QString s = m_speechCmd;
51     s.replace("%s", word);
52     m_speechProcess->start(s, QIODevice::WriteOnly);
53     if (! m_speechProcess->waitForStarted())
54         return;
55     if (! m_speechCmd.contains("%s"))
56     {
57         m_speechProcess->write(word.toUtf8());
58         m_speechProcess->closeWriteChannel();
59     }
60 }
61
62 }
63
64 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
65