File indexing completed on 2023-05-30 10:39:36

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 <QTextCodec>
0025 #include <QTextStream>
0026 
0027 #define macroExpander
0028 #include <KMacroExpander>
0029 
0030 Speech::Speech()
0031 {
0032 }
0033 
0034 Speech::~Speech()
0035 {
0036 }
0037 
0038 QString Speech::prepareCommand(const QString &command, const QString &text,
0039                                const QString &filename, const QString &language)
0040 {
0041     QHash<QChar, QString> map;
0042     map[QLatin1Char('t')] = text;
0043     map[QLatin1Char('f')] = filename;
0044     map[QLatin1Char('l')] = language;
0045     return KMacroExpander::expandMacrosShellQuote(command, map);
0046 }
0047 
0048 void Speech::speak(QString command, bool stdIn, const QString &text, const QString &language, int encoding, QTextCodec *codec)
0049 {
0050     if (!text.isEmpty()) {
0051         // 1. prepare the text:
0052         // 1.a) encode the text
0053         QTextStream ts(&encText, QIODevice::WriteOnly);
0054         if (encoding == Local)
0055             ts.setCodec(QTextCodec::codecForLocale());
0056         else if (encoding == Latin1)
0057             ts.setCodec("ISO-8859-1");
0058         else if (encoding == Unicode)
0059             ts.setCodec("UTF-16");
0060         else
0061             ts.setCodec(codec);
0062         ts << text;
0063         ts.flush();
0064 
0065         // 1.b) create a temporary file for the text
0066         tempFile.open();
0067         QTextStream fs(&tempFile);
0068         if (encoding == Local)
0069             fs.setCodec(QTextCodec::codecForLocale());
0070         else if (encoding == Latin1)
0071             fs.setCodec("ISO-8859-1");
0072         else if (encoding == Unicode)
0073             fs.setCodec("UTF-16");
0074         else
0075             fs.setCodec(codec);
0076         fs << text;
0077         fs << "\n";
0078         QString filename = tempFile.fileName();
0079         tempFile.flush();
0080 
0081         // 2. prepare the command:
0082         command = prepareCommand(command, QLatin1String(encText), filename, language);
0083 
0084         // 3. create a new process
0085         connect(&m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &Speech::processExited);
0086         //connect(&process, SIGNAL(wroteStdin(K3Process*)), this, SLOT(wroteStdin(K3Process*)));
0087         //connect(&process, SIGNAL(receivedStdout(K3Process*, char*, int)), this, SLOT(receivedStdout(K3Process*, char*, int)));
0088         //connect(&process, SIGNAL(receivedStderr(K3Process*, char*, int)), this, SLOT(receivedStderr(K3Process*, char*, int)));
0089 
0090         // 4. start the process
0091         if (stdIn) {
0092             m_process.start(command);
0093             if (!encText.isEmpty())
0094                 m_process.write(encText.constData(), encText.size());
0095             else
0096                 m_process.close();
0097         } else
0098             m_process.start(command);
0099     }
0100 }
0101 
0102 //void Speech::receivedStdout(K3Process *, char *buffer, int buflen)
0103 //{
0104 //    kDebug() << QString::fromLatin1(buffer, buflen) + QLatin1Char('\n');
0105 //}
0106 //void Speech::receivedStderr(K3Process *, char *buffer, int buflen)
0107 //{
0108 //    kDebug() << QString::fromLatin1(buffer, buflen) + QLatin1Char('\n');
0109 //}
0110 
0111 //void Speech::wroteStdin(K3Process *)
0112 //{
0113 //    process.closeStdin();
0114 //}
0115 
0116 void Speech::processExited(int exitCode, QProcess::ExitStatus exitStatus)
0117 {
0118     Q_UNUSED(exitCode);
0119     Q_UNUSED(exitStatus);
0120     delete this;
0121 }
0122