klettres
soundfactory.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "soundfactory.h"
00025
00026 #include <KMessageBox>
00027 #include <KLocale>
00028 #include <KDebug>
00029 #include <KRandomSequence>
00030 #include <KStandardDirs>
00031 #include <Phonon/MediaObject>
00032
00033 #include "klettres.h"
00034 #include "prefs.h"
00035
00036 SoundFactory::SoundFactory(KLettres *parent, const char *)
00037 : QObject(parent), m_player(0)
00038 {
00039 klettres = parent;
00040
00041 namesList.clear();
00042 filesList.clear();
00043 sounds = 0;
00044
00045 bool ok = klettres->loadLayout(m_layoutsDocument);
00046 if (ok) {
00047 change(Prefs::language());
00048 }
00049 if (!ok) {
00050 loadFailure();
00051 } else {
00052 setSoundSequence();
00053 }
00054 }
00055
00056 SoundFactory::~SoundFactory()
00057 {
00058 }
00059
00060 void SoundFactory::change(const QString ¤tLanguage)
00061 {
00062
00063 bool ok = loadLanguage(m_layoutsDocument, currentLanguage);
00064
00065 if (!ok) {
00066 loadFailure();
00067 } else {
00068 setSoundSequence();
00069 }
00070 }
00071
00072 void SoundFactory::playSound(int mySound)
00073 {
00074 QString soundFile;
00075
00076 if ((uint) mySound >= sounds) {
00077 return;
00078 }
00079
00080 soundFile = KStandardDirs::locate("data", "klettres/" + filesList[mySound]);
00081 kDebug() << "soundFile " << soundFile;
00082
00083 if (soundFile.isEmpty()) {
00084 return;
00085 }
00086
00087 if (!m_player) {
00088 m_player = Phonon::createPlayer(Phonon::GameCategory, soundFile);
00089 m_player->setParent(this);
00090 } else {
00091 m_player->setCurrentSource(soundFile);
00092 }
00093 m_player->play();
00094 }
00095
00096 void SoundFactory::loadFailure()
00097 {
00098 KMessageBox::error(klettres, i18n("Error while loading the sound names."));
00099 }
00100
00101 bool SoundFactory::loadLanguage(QDomDocument &layoutDocument, const QString ¤tLanguage)
00102 {
00103 QDomNodeList languagesList,
00104 alphabetList,
00105 syllablesList,
00106 soundNamesList;
00107 QDomElement languageElement,
00108 alphabetElement,
00109 syllableElement,
00110 soundNameElement;
00111 QDomAttr nameAttribute, fileAttribute;
00112
00113 languagesList = layoutDocument.elementsByTagName("language");
00114 QDomAttr codeAttribute;
00115
00116 languageElement = (const QDomElement &) languagesList.item(0).toElement();
00117 codeAttribute = languageElement.attributeNode("code");
00118
00119 if (currentLanguage != codeAttribute.value()) {
00120 kDebug() << "Fail reading language !!! ";
00121 return false;
00122 } else {
00123 kDebug() << "current language " << currentLanguage;
00124 }
00125
00126 if ((Prefs::level() == 1) || (Prefs::level() == 2)) {
00127 alphabetList = languageElement.elementsByTagName("alphabet");
00128 if (alphabetList.count() != 1) {
00129 return false;
00130 }
00131 alphabetElement = (const QDomElement &) alphabetList.item(0).toElement();
00132 soundNamesList = alphabetElement.elementsByTagName("sound");
00133 }
00134
00135
00136 if ((Prefs::level() == 3) || (Prefs::level() == 4)) {
00137 syllablesList = languageElement.elementsByTagName("syllables");
00138 if (syllablesList.count() != 1)
00139 return false;
00140
00141 syllableElement = (const QDomElement &) syllablesList.item(0).toElement();
00142
00143 soundNamesList = syllableElement.elementsByTagName("sound");
00144 }
00145
00146 sounds = soundNamesList.count();
00147 kDebug() << "number of sounds" << sounds << endl;
00148 if (sounds < 1) {
00149 return false;
00150 }
00151 namesList.clear();
00152 filesList.clear();
00153
00154 for (uint sound = 0; sound < sounds; sound++) {
00155 soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement();
00156 nameAttribute = soundNameElement.attributeNode("name");
00157
00158 namesList.append(nameAttribute.value());
00159 fileAttribute = soundNameElement.attributeNode("file");
00160
00161 filesList.append(fileAttribute.value());
00162 }
00163 if (namesList.isEmpty()) {
00164 return false;
00165 }
00166 if (filesList.isEmpty()) {
00167 return false;
00168 }
00169 return true;
00170 }
00171
00172 void SoundFactory::setSoundSequence()
00173 {
00174
00175 KRandomSequence randomSequence;
00176 randomList.clear();
00177
00178 for (uint j = 0; j < sounds; j++)
00179 randomList.append(j);
00180
00181 randomSequence.randomize(randomList);
00182 }
00183
00184 #include "soundfactory.moc"