speaker.cpp 2.2 KB

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