File indexing completed on 2024-12-08 06:26:55

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015 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 "speech.h"
0022 
0023 #include <QHash>
0024 #include <QTextStream>
0025 
0026 #define macroExpander
0027 #include <KMacroExpander>
0028 
0029 Speech::Speech()
0030 {
0031 }
0032 
0033 Speech::~Speech()
0034 {
0035 }
0036 
0037 QString Speech::prepareCommand(const QString &command, const QString &text, const QString &filename, const QString &language)
0038 {
0039     QHash<QChar, QString> map;
0040     map[QLatin1Char('t')] = text;
0041     map[QLatin1Char('f')] = filename;
0042     map[QLatin1Char('l')] = language;
0043     return KMacroExpander::expandMacrosShellQuote(command, map);
0044 }
0045 
0046 void Speech::speak(QString command, bool stdIn, const QString &text, const QString &language, QStringConverter::Encoding encoding)
0047 {
0048     if (!text.isEmpty()) {
0049         // 1. prepare the text:
0050         // 1.a) encode the text
0051         QTextStream ts(&encText, QIODevice::WriteOnly);
0052         ts.setEncoding(encoding);
0053         ts << text;
0054         ts.flush();
0055 
0056         // 1.b) create a temporary file for the text
0057         tempFile.open();
0058         QTextStream fs(&tempFile);
0059         fs.setEncoding(encoding);
0060         fs << text;
0061         fs << "\n";
0062         QString filename = tempFile.fileName();
0063         tempFile.flush();
0064 
0065         // 2. prepare the command:
0066         command = prepareCommand(command, QLatin1String(encText), filename, language);
0067 
0068         // 3. create a new process
0069         connect(&m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &Speech::processExited);
0070         // connect(&process, SIGNAL(wroteStdin(K3Process*)), this, SLOT(wroteStdin(K3Process*)));
0071         // connect(&process, SIGNAL(receivedStdout(K3Process*, char*, int)), this, SLOT(receivedStdout(K3Process*, char*, int)));
0072         // connect(&process, SIGNAL(receivedStderr(K3Process*, char*, int)), this, SLOT(receivedStderr(K3Process*, char*, int)));
0073 
0074         // 4. start the process
0075         if (stdIn) {
0076             m_process.start(command);
0077             if (!encText.isEmpty())
0078                 m_process.write(encText.constData(), encText.size());
0079             else
0080                 m_process.close();
0081         } else
0082             m_process.start(command);
0083     }
0084 }
0085 
0086 // void Speech::receivedStdout(K3Process *, char *buffer, int buflen)
0087 //{
0088 //     kDebug() << QString::fromLatin1(buffer, buflen) + QLatin1Char('\n');
0089 // }
0090 // void Speech::receivedStderr(K3Process *, char *buffer, int buflen)
0091 //{
0092 //     kDebug() << QString::fromLatin1(buffer, buflen) + QLatin1Char('\n');
0093 // }
0094 
0095 // void Speech::wroteStdin(K3Process *)
0096 //{
0097 //     process.closeStdin();
0098 // }
0099 
0100 void Speech::processExited(int exitCode, QProcess::ExitStatus exitStatus)
0101 {
0102     Q_UNUSED(exitCode);
0103     Q_UNUSED(exitStatus);
0104     delete this;
0105 }