You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tdegames/ktuberling/soundfactory.cpp

149 lines
4.1 KiB

/* -------------------------------------------------------------
KDE Tuberling
Play ground widget
mailto:e.bischoff@noos.fr
------------------------------------------------------------- */
#include <stdlib.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <kstandarddirs.h>
#include <kaudioplayer.h>
#include <tqdom.h>
#include "soundfactory.h"
#include "soundfactory.moc"
#include "toplevel.h"
// Constructor
SoundFactory::SoundFactory(TopLevel *parent, const char *name, uint selectedLanguage)
: TQObject(parent, name)
{
topLevel = parent;
namesList = filesList = 0;
TQDomDocument layoutsDocument;
bool ok = topLevel->loadLayout(layoutsDocument);
if (ok) ok = registerLanguages(layoutsDocument);
if (ok) ok = loadLanguage(layoutsDocument, selectedLanguage);
if (!ok) loadFailure();
}
// Destructor
SoundFactory::~SoundFactory()
{
if (namesList) delete [] namesList;
if (filesList) delete [] filesList;
}
// Change the language
void SoundFactory::change(uint selectedLanguage)
{
TQDomDocument layoutsDocument;
bool ok = topLevel->loadLayout(layoutsDocument);
if (ok) ok = loadLanguage(layoutsDocument, selectedLanguage);
if (!ok) loadFailure();
}
// Play some sound
void SoundFactory::playSound(const TQString &soundRef) const
{
int sound;
TQString soundFile;
if (!topLevel->isSoundEnabled()) return;
for (sound = 0; sound < sounds; sound++)
if (!namesList[sound].compare(soundRef)) break;
if (sound == sounds) return;
soundFile = locate("data", "ktuberling/sounds/" + filesList[sound]);
if (soundFile == 0) return;
//printf("%s\n", (const char *) soundFile);
KAudioPlayer::play(soundFile);
}
// Report a load failure
void SoundFactory::loadFailure()
{
KMessageBox::error(topLevel, i18n("Error while loading the sound names."));
}
// Register the various languages
bool SoundFactory::registerLanguages(TQDomDocument &layoutDocument)
{
TQDomNodeList languagesList, menuItemsList, labelsList;
TQDomElement languageElement, menuItemElement, labelElement;
TQDomAttr codeAttribute, actionAttribute;
bool enabled;
languagesList = layoutDocument.elementsByTagName("language");
if (languagesList.count() < 1)
return false;
for (uint i = 0; i < languagesList.count(); i++)
{
languageElement = (const TQDomElement &) languagesList.item(i).toElement();
codeAttribute = languageElement.attributeNode("code");
enabled = locate("data", "ktuberling/sounds/" + codeAttribute.value() + "/") != 0;
menuItemsList = languageElement.elementsByTagName("menuitem");
if (menuItemsList.count() != 1)
return false;
menuItemElement = (const TQDomElement &) menuItemsList.item(0).toElement();
labelsList = menuItemElement.elementsByTagName("label");
if (labelsList.count() != 1)
return false;
labelElement = (const TQDomElement &) labelsList.item(0).toElement();
actionAttribute = menuItemElement.attributeNode("action");
topLevel->registerLanguage(labelElement.text(), actionAttribute.value().latin1(), enabled);
}
return true;
}
// Load the sounds of one given language
bool SoundFactory::loadLanguage(TQDomDocument &layoutDocument, uint toLoad)
{
TQDomNodeList languagesList,
soundNamesList;
TQDomElement languageElement,
soundNameElement;
TQDomAttr nameAttribute, fileAttribute;
languagesList = layoutDocument.elementsByTagName("language");
if (toLoad >= languagesList.count())
return false;
languageElement = (const TQDomElement &) languagesList.item(toLoad).toElement();
soundNamesList = languageElement.elementsByTagName("sound");
sounds = soundNamesList.count();
if (sounds < 1)
return false;
if (!(namesList = new TQString[sounds]))
return false;
if (!(filesList = new TQString[sounds]))
return false;
for (uint sound = 0; sound < sounds; sound++)
{
soundNameElement = (const TQDomElement &) soundNamesList.item(sound).toElement();
nameAttribute = soundNameElement.attributeNode("name");
namesList[sound] = nameAttribute.value();
fileAttribute = soundNameElement.attributeNode("file");
filesList[sound] = fileAttribute.value();
}
return true;
}