Main Page   Compound List   File List   Compound Members   File Members  

soundfactory.cpp

Go to the documentation of this file.
00001 /* -------------------------------------------------------------
00002    From KDE Tuberling
00003    mailto:e.bischoff@noos.fr
00004  ------------------------------------------------------------- */
00005 /*
00006  * Copyright (C) 2001-2003 Eric Bischoff
00007   Anne-Marie Mahfouf <annma@kde.org>
00008 
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of version 2 of the GNU General Public
00011     License as published by the Free Software Foundation.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  */
00022 #include <stdlib.h>
00023 
00024 #include <kmessagebox.h>
00025 #include <klocale.h>
00026 #include <kaudioplayer.h>
00027 #include <kdebug.h>
00028 
00029 
00030 #include "soundfactory.h"
00031 #include "soundfactory.moc"
00032 #include "klettres.h"
00033 
00035 SoundFactory::SoundFactory(KLettres *parent, const char *name, uint selectedLanguage)
00036         : QObject(parent, name)
00037 {
00038   klettres = parent;
00039 
00040   namesList = filesList = 0;
00041 
00042   QDomDocument layoutsDocument;
00043   bool ok = klettres->loadLayout(layoutsDocument);
00044    if (ok)
00045         ok = registerLanguages(layoutsDocument);
00046   if (ok)
00047         ok = loadLanguage(layoutsDocument, selectedLanguage);
00048   if (!ok) loadFailure();
00049 }
00050 
00052 SoundFactory::~SoundFactory()
00053 {
00054   if (namesList) delete [] namesList;
00055   if (filesList) delete [] filesList;
00056 }
00057 
00058 //When the language changes in KLettres menu
00059 void SoundFactory::change(uint selectedLanguage)
00060 {
00061   QDomDocument layoutsDocument;
00062   bool ok = klettres->loadLayout(layoutsDocument);
00063   //go load the sounds for the current language
00064   if (ok) ok = loadLanguage(layoutsDocument, selectedLanguage);
00065   //tell the user if there are no sounds
00066   if (!ok) loadFailure();
00067 }
00068 
00069 //Play the sound associated to int soundRef
00070 void SoundFactory::playSound(int mySound)
00071 {
00072   QString soundFile;
00073 
00074    if (mySound >= sounds) return;
00075 
00076   soundFile = locate("data", "klettres/" + filesList[mySound]);
00077   kdDebug() << "File to play:  " << soundFile << endl;
00078 
00079   if (soundFile == 0) return;
00080 
00081   KAudioPlayer::play(soundFile);
00082 }
00083 
00084 //Report a load failure
00085 void SoundFactory::loadFailure()
00086 {
00087         KMessageBox::error(klettres, i18n("Error while loading the sound names."));
00088 }
00089 
00091 bool SoundFactory::registerLanguages(QDomDocument &layoutDocument)
00092 {
00093   QDomNodeList languagesList, menuItemsList, labelsList;
00094   QDomElement languageElement, menuItemElement, labelElement;
00095   QDomAttr codeAttribute, actionAttribute;
00096   bool enabled;
00097   languagesList = layoutDocument.elementsByTagName("language");
00098   if (languagesList.count() < 1)
00099     return false;
00100 
00101   for (uint i = 0; i < languagesList.count(); i++)
00102   {
00103     languageElement = (const QDomElement &) languagesList.item(i).toElement();
00104     codeAttribute = languageElement.attributeNode("code");
00105     //here it looks in $KDEDIR/share/apps/klettres and in $KDEHOME/share/apps/klettres
00106     enabled = locate("data", "klettres/" + codeAttribute.value() + "/") != 0;
00107     menuItemsList = languageElement.elementsByTagName("menuitem");
00108     if (menuItemsList.count() != 1)
00109       return false;
00110 
00111     menuItemElement = (const QDomElement &) menuItemsList.item(0).toElement();
00112 
00113     labelsList = menuItemElement.elementsByTagName("label");
00114     if (labelsList.count() != 1)
00115       return false;
00116 
00117     labelElement = (const QDomElement &) labelsList.item(0).toElement();
00118     actionAttribute = menuItemElement.attributeNode("action");
00119 
00120     klettres->registerLanguage(labelElement.text(), actionAttribute.value().latin1(), enabled);
00121   }
00122 
00123   return true;
00124 }
00125 
00126 /*
00127  * Load the sounds of one given language
00128  * Call that when you read the language from Config and when the language changes
00129  * or when the level changes
00130  */
00131 bool SoundFactory::loadLanguage(QDomDocument &layoutDocument, uint toLoad)
00132 {
00133   QDomNodeList languagesList,
00134                alphabetList,
00135                syllablesList,
00136                soundNamesList;
00137   QDomElement languageElement,
00138               alphabetElement,
00139               syllableElement,
00140               soundNameElement;
00141   QDomAttr nameAttribute, fileAttribute;
00142 
00143   languagesList = layoutDocument.elementsByTagName("language");
00144   if (toLoad >= languagesList.count())
00145     return false;
00146 
00147   languageElement = (const QDomElement &) languagesList.item(toLoad).toElement();
00148 
00149   //load the sounds for level 1 and 2 (alphabet)
00150   if ((klettres->m_view->niveau == 1) || (klettres->m_view->niveau == 2))
00151   {
00152         alphabetList = languageElement.elementsByTagName("alphabet");
00153         if (alphabetList.count() != 1)
00154                 return false;
00155 
00156         alphabetElement = (const QDomElement &) alphabetList.item(0).toElement();
00157 
00158         soundNamesList = alphabetElement.elementsByTagName("sound");
00159   }
00160 
00161   //load the sounds for level 3 and 4 (syllables)
00162   if ((klettres->m_view->niveau == 3) || (klettres->m_view->niveau == 4))
00163   {
00164         syllablesList = languageElement.elementsByTagName("syllables");
00165         if (syllablesList.count() != 1)
00166                 return false;
00167 
00168         syllableElement = (const QDomElement &) syllablesList.item(0).toElement();
00169 
00170         soundNamesList = syllableElement.elementsByTagName("sound");
00171   }
00173   sounds = soundNamesList.count();
00174   if (sounds < 1)
00175     return false;
00176 
00177   if (!(namesList = new QString[sounds]))
00178     return false;
00179   if (!(filesList = new QString[sounds]))
00180     return false;
00181 
00182   for (uint sound = 0; sound < sounds; sound++)
00183   {
00184     soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement();
00185     nameAttribute = soundNameElement.attributeNode("name");
00186     //namesList helds the names of the letter or syllable to display
00187     namesList[sound] = nameAttribute.value();
00188     fileAttribute = soundNameElement.attributeNode("file");
00189     //filesList helds the names of the sound files (i.e the location of the sounds like fr/alpha/a-0.mp3
00190     filesList[sound] = fileAttribute.value();
00191   }
00192   return true;
00193 }
00194 

Generated on Sat Oct 18 21:37:50 2003 by doxygen1.2.18