summaryrefslogtreecommitdiffstats
path: root/kttsd/libkttsd/pluginproc.cpp
blob: bcd79fdec009b5c4f1cb2b60783fea24da98d6e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/***************************************************** vim:set ts=4 sw=4 sts=4:
  This file is the template for the processing plug ins.
  -------------------
  Copyright : (C) 2002-2003 by José Pablo Ezequiel "Pupeno" Fernández
  -------------------
  Original author: José Pablo Ezequiel "Pupeno" Fernández <[email protected]>
  Current Maintainer: Gary Cramblitt <[email protected]>
 ******************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; version 2 of the License.               *
 *                                                                         *
 ***************************************************************************/

// TQt includes. 
#include <tqstring.h>
#include <tqtextcodec.h>

// KDE includes.
#include <kdebug.h>
#include <kstandarddirs.h>
#include <klocale.h>

// PlugInProc includes.
#include "pluginproc.h"
#include "pluginproc.moc"

/**
* Constructor
*/
PlugInProc::PlugInProc( TQObject *parent, const char *name) : TQObject(parent, name){
    // kdDebug() << "PlugInProc::PlugInProc: Running" << endl;
}

/**
* Destructor
*/
PlugInProc::~PlugInProc(){
    // kdDebug() << "PlugInProc::~PlugInProc: Running" << endl;
}

/**
* Initializate the speech plugin.
*/
bool PlugInProc::init(KConfig* /*config*/, const TQString& /*configGroup*/){
    // kdDebug() << "PlugInProc::init: Running" << endl;
    return false;
}

/** 
* Say a text.  Synthesize and audibilize it.
* @param text                    The text to be spoken.
*
* If the plugin supports asynchronous operation, it should return immediately.
*/
void PlugInProc::sayText(const TQString& /*text*/){
    // kdDebug() << "PlugInProc::sayText: Running" << endl;
}

/**
* Synthesize text into an audio file, but do not send to the audio device.
* @param text                    The text to be synthesized.
* @param suggestedFilename       Full pathname of file to create.  The plugin
*                                may ignore this parameter and choose its own
*                                filename.  KTTSD will query the generated
*                                filename using getFilename().
*
* If the plugin supports asynchronous operation, it should return immediately.
*/
void PlugInProc::synthText(const TQString& /*text*/, const TQString& /*suggestedFilename*/) { }

/**
* Get the generated audio filename from synthText.
* @return                        Name of the audio file the plugin generated.
*                                Null if no such file.
*
* The plugin must not re-use the filename.
*/
TQString PlugInProc::getFilename() { return TQString(); }

/**
* Stop current operation (saying or synthesizing text).
* This function only makes sense in asynchronus modes.
* The plugin should return to the psIdle state.
*/
void PlugInProc::stopText(){
    // kdDebug() << "PlugInProc::stopText: Running" << endl;
}

/**
* Return the current state of the plugin.
* This function only makes sense in asynchronous mode.
* @return                        The pluginState of the plugin.
*
* @ref pluginState
*/
pluginState PlugInProc::getState() { return psIdle; }

/**
* Acknowledges a finished state and resets the plugin state to psIdle.
*
* If the plugin is not in state psFinished, nothing happens.
* The plugin may use this call to do any post-processing cleanup,
* for example, blanking the stored filename (but do not delete the file).
* Calling program should call getFilename prior to ackFinished.
*/
void PlugInProc::ackFinished() { }

/**
* Returns True if the plugin supports asynchronous processing,
* i.e., returns immediately from sayText or synthText.
* @return                        True if this plugin supports asynchronous processing.
*/
bool PlugInProc::supportsAsync() { return false; }

/**
* Returns True if the plugin supports synthText method,
* i.e., is able to synthesize text to a sound file without
* audibilizing the text.
* @return                        True if this plugin supports synthText method.
*/
bool PlugInProc::supportsSynth() { return false; }

/**
* Returns the name of an XSLT stylesheet that will convert a valid SSML file
* into a format that can be processed by the synth.  For example,
* The Festival plugin returns a stylesheet that will convert SSML into
* SABLE.  Any tags the synth cannot handle should be stripped (leaving
* their text contents though).  The default stylesheet strips all
* tags and converts the file to plain text.
* @return            Name of the XSLT file.
*/
TQString PlugInProc::getSsmlXsltFilename()
{
    return KGlobal::dirs()->resourceDirs("data").last() + "kttsd/xslt/SSMLtoPlainText.xsl";
}

/**
* Given the name of a codec, returns the TQTextCodec for the name.
* Handles the following "special" codec names:
*   Local               The user's current Locale codec.
*   Latin1              Latin1 (ISO 8859-1)
*   Unicode             UTF-16
* @param codecName      Name of desired codec.
* @return               The codec object.  Calling program must not delete this object
*                       as it is a reference to an existing TQTextCodec object.
*
* Caution: Do not pass translated codec names to this routine.
*/
/*static*/ TQTextCodec* PlugInProc::codecNameToCodec(const TQString &codecName)
{
    TQTextCodec* codec = 0;
    if (codecName == "Local")
        codec = TQTextCodec::codecForLocale();
    else if (codecName == "Latin1")
        codec = TQTextCodec::codecForName("ISO8859-1");
    else if (codecName == "Unicode")
        codec = TQTextCodec::codecForName("utf16");
    else
        codec = TQTextCodec::codecForName(codecName.latin1());
    if (!codec)
    {
        kdDebug() << "PluginProc::codecNameToCodec: Invalid codec name " << codecName << endl;
        kdDebug() << "PluginProc::codecNameToCodec: Defaulting to ISO 8859-1" << endl;
        codec = TQTextCodec::codecForName("ISO8859-1");
    }
    return codec;
}

/**
* Builds a list of codec names, suitable for display in a TQComboBox.
* The list includes the 3 special codec names (translated) at the top:
*   Local               The user's current Locale codec.
*   Latin1              Latin1 (ISO 8859-1)
*   Unicode             UTF-16
*/
/*static*/ TQStringList PlugInProc::buildCodecList()
{
    // kdDebug() << "PlugInConf::buildCodecList: Running" << endl;
    TQStringList codecList;
    TQString local = i18n("Local")+" (";
    local += TQTextCodec::codecForLocale()->name();
    local += ")";
    codecList.append(local);
    codecList.append(i18n("Latin1"));
    codecList.append(i18n("Unicode"));
    for (int i = 0; (TQTextCodec::codecForIndex(i)); ++i )
        codecList.append(TQTextCodec::codecForIndex(i)->name());
    return codecList;
}

/**
* Given the name of a codec, returns index into the codec list.
* Handles the following "special" codec names:
*   Local               The user's current Locale codec.
*   Latin1              Latin1 (ISO 8859-1)
*   Unicode             UTF-16
* @param codecName      Name of the codec.
* @param codecList      List of codec names. The first 3 entries may be translated names.
* @return               TQTextCodec object.  Caller must not delete this object.
*
* Caution: Do not pass translated codec names to this routine in codecName parameter.
*/
/*static*/ int PlugInProc::codecNameToListIndex(const TQString &codecName, const TQStringList &codecList)
{
    int codec;
    if (codecName == "Local")
        codec = PlugInProc::Local;
    else if (codecName == "Latin1")
        codec = PlugInProc::Latin1;
    else if (codecName == "Unicode")
        codec = PlugInProc::Unicode;
    else {
        codec = PlugInProc::Local;
        const uint codecListCount = codecList.count();
        for (uint i = PlugInProc::UseCodec; i < codecListCount; ++i )
            if (codecName == codecList[i])
                codec = i;
    }
    return codec;
}

/**
* Given index into codec list, returns the codec object.
* @param codecNum       Index of the codec.
* @param codecList      List of codec names. The first 3 entries may be translated names.
* @return               TQTextCodec object.  Caller must not delete this object.
*/
/*static*/ TQTextCodec* PlugInProc::codecIndexToCodec(int codecNum, const TQStringList &codecList)
{
    TQTextCodec* codec = 0;
    switch (codecNum) {
        case PlugInProc::Local:
            codec = TQTextCodec::codecForLocale();
            break;
        case PlugInProc::Latin1:
            codec = TQTextCodec::codecForName("ISO8859-1");
            break;
        case PlugInProc::Unicode:
            codec = TQTextCodec::codecForName("utf16");
            break;
        default:
            codec = TQTextCodec::codecForName(codecList[codecNum].latin1());
            break;
    }
    if (!codec)
    {
        kdDebug() << "PlugInProc::codecIndexToCodec: Invalid codec index " << codecNum << endl;
        kdDebug() << "PlugInProc::codecIndexToCodec: Defaulting to ISO 8859-1" << endl;
        codec = TQTextCodec::codecForName("ISO8859-1");
    }
    return codec;
}

/**
* Given index into codec list, returns the codec Name.
* Handles the following "special" codec names:
*   Local               The user's current Locale codec.
*   Latin1              Latin1 (ISO 8859-1)
*   Unicode             UTF-16
* @param codecNum       Index of the codec.
* @param codecList      List of codec names. The first 3 entries may be translated names.
* @return               Untranslated name of the codec.
*/
/*static*/ TQString PlugInProc::codecIndexToCodecName(int codecNum, const TQStringList &codecList)
{
    TQString codecName;
    switch (codecNum) {
        case PlugInProc::Local:
            codecName = "Local";
            break;
        case PlugInProc::Latin1:
            codecName = "Latin1";
            break;
        case PlugInProc::Unicode:
            codecName = "Unicode";
            break;
        default:
            if ((uint)codecNum < codecList.count())
                codecName = codecList[codecNum];
            else
            {
                kdDebug() << "PlugInProc::codecIndexToCodec: Invalid codec index " << codecNum << endl;
                kdDebug() << "PlugInProc::codecIndexToCodec: Defaulting to ISO 8859-1" << endl;
                codecName = "ISO8859-1";
            }
    }
    return codecName;
}