1 | /*************************************************************************** |
2 | texttospeechsystem.cpp - description |
3 | ------------------- |
4 | begin : Son Sep 8 2002 |
5 | copyright : (C) 2002 by Gunnar Schmi Dt |
6 | email : kmouth@schmi-dt.de |
7 | ***************************************************************************/ |
8 | |
9 | /*************************************************************************** |
10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
12 | * it under the terms of the GNU General Public License as published by * |
13 | * the Free Software Foundation; either version 2 of the License, or * |
14 | * (at your option) any later version. * |
15 | * * |
16 | ***************************************************************************/ |
17 | |
18 | #include "texttospeechsystem.h" |
19 | #include <stdlib.h> |
20 | |
21 | #include <QtCore/QTextCodec> |
22 | #include <QtDBus/QtDBus> |
23 | |
24 | #include <kconfig.h> |
25 | #include <kconfiggroup.h> |
26 | #include <kspeech.h> |
27 | #include <kdebug.h> |
28 | #include <kspeech_interface.h> |
29 | |
30 | #include "speech.h" |
31 | |
32 | TextToSpeechSystem::TextToSpeechSystem() { |
33 | stdIn = true; |
34 | useKttsd = true; |
35 | codec = Speech::Local; // local encoding; |
36 | buildCodecList(); |
37 | } |
38 | |
39 | TextToSpeechSystem::~TextToSpeechSystem() { |
40 | delete codecList; |
41 | } |
42 | |
43 | bool kttsdSay (const QString &text, const QString &language) { |
44 | // TODO: Would be better to save off this QDBusInterface pointer and |
45 | // set defaults only once. |
46 | org::kde::KSpeech kspeech(QLatin1String( "org.kde.KSpeech" ), QLatin1String( "/KSpeech" ), QDBusConnection::sessionBus()); |
47 | kspeech.setApplicationName(QLatin1String( "KMouth" )); |
48 | kspeech.setDefaultTalker(language); |
49 | |
50 | // FIXME: language is incorrect. |
51 | kDebug() << "kttsdSay: language = " << language; |
52 | kspeech.setDefaultPriority(KSpeech::jpWarning); |
53 | QDBusReply<int> val = kspeech.say(text, 0); |
54 | |
55 | return (val>0); |
56 | } |
57 | |
58 | void TextToSpeechSystem::speak (const QString &text, const QString &language) { |
59 | if (text.length() > 0) { |
60 | if (useKttsd) { |
61 | if (kttsdSay(text, language)) |
62 | return; |
63 | } |
64 | |
65 | if (codec < Speech::UseCodec) |
66 | (new Speech())->speak(ttsCommand, stdIn, text, language, codec, 0); |
67 | else |
68 | (new Speech())->speak(ttsCommand, stdIn, text, language, Speech::UseCodec, |
69 | codecList->at (codec - Speech::UseCodec)); |
70 | } |
71 | } |
72 | |
73 | void TextToSpeechSystem::readOptions (KConfig *config, const QString &langGroup) { |
74 | KConfigGroup cg(config, langGroup); |
75 | ttsCommand = cg.readPathEntry("Command" , QString()); |
76 | stdIn = cg.readEntry("StdIn" , true); |
77 | useKttsd = cg.readEntry("useKttsd" , true); |
78 | |
79 | QString codecString = cg.readEntry("Codec" , "Local" ); |
80 | if (codecString == QLatin1String( "Local" )) |
81 | codec = Speech::Local; |
82 | else if (codecString == QLatin1String( "Latin1" )) |
83 | codec = Speech::Latin1; |
84 | else if (codecString == QLatin1String( "Unicode" )) |
85 | codec = Speech::Unicode; |
86 | else { |
87 | codec = Speech::Local; |
88 | for (int i = 0; i < codecList->count(); i++ ) |
89 | if (codecString == QLatin1String( codecList->at(i)->name() )) |
90 | codec = Speech::UseCodec + i; |
91 | } |
92 | } |
93 | |
94 | void TextToSpeechSystem::saveOptions (KConfig *config, const QString &langGroup) { |
95 | KConfigGroup cg(config, langGroup); |
96 | cg.writePathEntry("Command" , ttsCommand); |
97 | cg.writeEntry("StdIn" , stdIn); |
98 | cg.writeEntry("useKttsd" , useKttsd); |
99 | if (codec == Speech::Local) |
100 | cg.writeEntry("Codec" , "Local" ); |
101 | else if (codec == Speech::Latin1) |
102 | cg.writeEntry("Codec" , "Latin1" ); |
103 | else if (codec == Speech::Unicode) |
104 | cg.writeEntry("Codec" , "Unicode" ); |
105 | else { |
106 | QString codeName = QLatin1String( codecList->at (codec-Speech::UseCodec)->name() ); |
107 | cg.writeEntry("Codec" , codeName); |
108 | } |
109 | } |
110 | |
111 | void TextToSpeechSystem::buildCodecList () { |
112 | codecList = new QList<QTextCodec*>; |
113 | QList<QByteArray> availableCodecs = QTextCodec::availableCodecs(); |
114 | for (int i = 0; i < availableCodecs.count(); ++i) { |
115 | QTextCodec *codec = QTextCodec::codecForName(availableCodecs[i]); |
116 | codecList->append (codec); |
117 | } |
118 | } |
119 | |
120 | #include "texttospeechsystem.moc" |
121 | |