29 #include <QtCore/QVector>
30 #include <QtXml/QDomDocument>
36 #include <kservicetypetrader.h>
38 class TalkerCodePrivate
66 :d(new TalkerCodePrivate(this))
69 parseTalkerCode(code);
78 :d(new TalkerCodePrivate(this))
80 d->name = other.
name();
83 d->volume = other.
volume();
84 d->rate = other.
rate();
85 d->pitch = other.
pitch();
100 d->name = other.
name();
103 d->volume = other.
volume();
104 d->rate = other.
rate();
105 d->pitch = other.
pitch();
151 return d->outputModule;
191 d->outputModule = moduleName;
199 parseTalkerCode(code);
204 QString xml(QLatin1String(
"<voice name=\"%1\" lang=\"%2\" outputModule=\"%3\""
205 " voiceName=\"%4\" voiceType=\"%5\">"
206 "<prosody volume=\"%6\" rate=\"%7\" pitch=\"%8\" /></voice>"));
207 QString code = xml.arg(d->name)
209 .arg(d->outputModule)
224 if (!d->name.isEmpty())
232 QString fullLangCode = d->language;
237 if (!d->outputModule.isEmpty()) code += QLatin1Char(
' ' ) +
stripPrefer(d->outputModule, prefer);
239 code += QString(QLatin1String(
" volume: %1 rate: %2")).arg(d->volume).arg(d->rate);
240 code = code.trimmed();
243 code = i18nc(
"Default language code",
"default");
251 case 1:
return i18nc(
"The name of the first Male voice",
"Male 1");
break;
252 case 2:
return i18n(
"Male 2");
break;
253 case 3:
return i18n(
"Male 3");
break;
254 case 4:
return i18nc(
"The name of the first Female voice",
"Female 1");
break;
255 case 5:
return i18n(
"Female 2");
break;
256 case 6:
return i18n(
"Female 3");
break;
257 case 7:
return i18nc(
"The name of the male child voice",
"Boy");
break;
258 case 8:
return i18nc(
"The name of the female child voice",
"Girl");
break;
260 return i18nc(
"Somehow user has gotten a voice type that is not valid, i.e. not Male1, Male2, etc.",
"Invalid voice type");
266 if (language.left(1) == QLatin1String(
"*" ))
267 language = language.mid(1);
270 KGlobal::locale()->splitLocale(language, languageCode, countryCode, modifier, charSet);
287 if (languageCode == QLatin1String(
"other" ))
288 language = i18nc(
"Other language",
"Other");
292 language = KGlobal::locale()->languageCodeToName(langAlpha);
294 if (!countryCode.isEmpty())
296 QString countryName = KGlobal::locale()->countryCodeToName(countryCode);
298 if (countryName == i18nc(
"full country name",
"United States of America"))
299 countryName = i18nc(
"abbreviated country name",
"USA");
300 if (countryName == i18nc(
"full country name",
"United Kingdom"))
301 countryName = i18nc(
"abbreviated country name",
"UK");
302 language += QLatin1String(
" (" ) + countryName + QLatin1Char(
')' );
311 void TalkerCode::parseTalkerCode(
const QString &talkerCode)
314 doc.setContent(talkerCode);
316 QDomElement voice = doc.firstChildElement(QLatin1String(
"voice" ));
319 d->name = voice.attribute(QLatin1String(
"name" ));
320 d->language = voice.attribute(QLatin1String(
"lang" ));
321 d->outputModule = voice.attribute(QLatin1String(
"outputModule" ));
322 d->voiceName = voice.attribute(QLatin1String(
"voiceName" ));
324 d->voiceType = voice.attribute(QLatin1String(
"voiceType" )).toInt(&result);
328 QDomElement prosody = voice.firstChildElement(QLatin1String(
"prosody" ));
329 if (!prosody.isNull())
332 d->volume = prosody.attribute(QLatin1String(
"volume" )).toInt(&result);
335 d->rate = prosody.attribute(QLatin1String(
"rate" )).toInt(&result);
338 d->pitch = prosody.attribute(QLatin1String(
"pitch" )).toInt(&result);
344 kDebug() <<
"got a voice with no prosody tag";
349 kDebug() <<
"got a voice with no voice tag";
364 const QString& talker,
365 bool assumeDefaultLang)
369 if (talker.isEmpty())
return 0;
373 if (assumeDefaultLang)
376 talkers[0].language());
379 int talkersCount = int(talkers.count());
380 QVector<int> priorityMatch(talkersCount);
381 for (
int ndx = 0; ndx < talkersCount; ++ndx)
383 priorityMatch[ndx] = 0;
387 int maxPriority = -1;
388 for (
int ndx = 0; ndx < talkersCount; ++ndx)
390 if (priorityMatch[ndx] > maxPriority) maxPriority = priorityMatch[ndx];
395 for (
int ndx = 0; ndx < talkersCount; ++ndx)
397 if (priorityMatch[ndx] == maxPriority)
411 QVector<int> preferredMatch(talkersCount);
412 for (
int ndx = 0; ndx < talkersCount; ++ndx)
414 preferredMatch[ndx] = 0;
415 if (priorityMatch[ndx] == maxPriority)
420 int maxPreferred = -1;
421 for (
int ndx = 0; ndx < talkersCount; ++ndx)
423 if (preferredMatch[ndx] > maxPreferred) maxPreferred = preferredMatch[ndx];
429 for (
int ndx = talkersCount-1; ndx >= 0; --ndx)
431 if (priorityMatch[ndx] == maxPriority)
433 if (preferredMatch[ndx] == maxPreferred)
445 if (winner < 0) winner = 0;
452 if ( code.left(1) == QLatin1String(
"*" ) )
464 return d->language == other.
language() &&
466 d->rate == other.
rate() &&
467 d->volume == other.
volume() &&
468 d->pitch == other.
pitch() &&
475 return d->language != other.
language() ||
477 d->rate != other.
rate() ||
478 d->volume != other.
volume() ||
479 d->pitch != other.
pitch() ||
void setLanguage(const QString &language)
static QString stripPrefer(const QString &code, bool &preferred)
Strips leading * from a code.
QString getTranslatedDescription() const
The Talker Code translated for display.
TalkerCode & operator=(const TalkerCode &other)
void setOutputModule(const QString &moduleName)
QString voiceName() const
QList< TalkerCode > TalkerCodeList
bool operator!=(TalkerCode &other) const
static QString translatedVoiceType(int voiceType)
These functions return translated Talker Code attributes.
void setVolume(int volume)
bool operator==(TalkerCode &other) const
static void splitFullLanguageCode(const QString &lang, QString &languageCode, QString &countryCode)
Given a language code that might contain a country code, splits the code into the two letter language...
void setName(const QString &name)
QString name() const
Properties.
QString getTalkerCode() const
TalkerCode(const QString &code=QString(), bool normal=false)
Constructor.
static QString languageCodeToLanguage(const QString &languageCode)
Converts a language code plus optional country code to language description.
QString outputModule() const
void setVoiceName(const QString &voiceName)
static int findClosestMatchingTalker(const TalkerCodeList &talkers, const QString &talker, bool assumeDefaultLang=true)
Given a list of parsed talker codes and a desired talker code, finds the closest matching talker in t...
static QString defaultTalkerCode(const QString &fullLanguageCode, const QString &moduleName)
Given a language code and plugin name, returns a normalized default talker code.
void setTalkerCode(const QString &code)
The Talker Code returned in XML format.
void setVoiceType(int voiceType)