1 /*************************************************************************** 2 * Copyright (C) 1999-2006 by Éric Bischoff <ebischoff@nerim.net> * 3 * Copyright (C) 2007 by Albert Astals Cid <aacid@kde.org> * 4 * * 5 * This program is free software; you can redistribute it and/or modify * 6 * it under the terms of the GNU General Public License as published by * 7 * the Free Software Foundation; either version 2 of the License, or * 8 * (at your option) any later version. * 9 ***************************************************************************/ 10 11 /* Sound factory */ 12 13 #include "soundfactory.h" 14 15 #include <stdlib.h> 16 17 #include <QDir> 18 #include <QDomDocument> 19 #include <QFile> 20 #include <QMediaPlayer> 21 #include <QSet> 22 #include <QUrl> 23 24 #include "filefactory.h" 25 26 // Constructor SoundFactory(SoundFactoryCallbacks * callbacks)27SoundFactory::SoundFactory(SoundFactoryCallbacks *callbacks) 28 : m_callbacks(callbacks) 29 { 30 player = new QMediaPlayer(); 31 } 32 33 // Destructor ~SoundFactory()34SoundFactory::~SoundFactory() 35 { 36 delete player; 37 } 38 39 // Play some sound playSound(const QString & soundRef) const40void SoundFactory::playSound(const QString &soundRef) const 41 { 42 if (!m_callbacks->isSoundEnabled()) return; 43 44 int sound; 45 for (sound = 0; sound < sounds; sound++) 46 if (!namesList[sound].compare(soundRef)) break; 47 if (sound == sounds) return; 48 49 const QString soundFile = FileFactory::locate(QLatin1String( "sounds/" ) + filesList[sound]); 50 if (soundFile.isEmpty()) return; 51 52 player->setMedia(QUrl::fromLocalFile(soundFile)); 53 player->play(); 54 } 55 56 // Register the various languages registerLanguages()57void SoundFactory::registerLanguages() 58 { 59 QSet<QString> list; 60 const QStringList dirs = FileFactory::locateAll(QStringLiteral("sounds")); 61 Q_FOREACH (const QString &dir, dirs) 62 { 63 const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.soundtheme")); 64 Q_FOREACH (const QString &file, fileNames) 65 { 66 list <<dir + '/' + file; 67 } 68 } 69 70 foreach(const QString &soundTheme, list) 71 { 72 QFile file(soundTheme); 73 if (file.open(QIODevice::ReadOnly)) 74 { 75 QDomDocument document; 76 if (document.setContent(&file)) 77 { 78 const QString code = document.documentElement().attribute(QStringLiteral( "code" )); 79 const bool enabled = FileFactory::folderExists(QLatin1String( "sounds/" ) + code + QLatin1Char( '/' )); 80 m_callbacks->registerLanguage(code, soundTheme, enabled); 81 } 82 } 83 } 84 } 85 86 // Load the sounds of one given language loadLanguage(const QString & selectedLanguageFile)87bool SoundFactory::loadLanguage(const QString &selectedLanguageFile) 88 { 89 QDomNodeList languagesList, 90 soundNamesList; 91 QDomElement languageElement, 92 soundNameElement; 93 QDomAttr nameAttribute, fileAttribute; 94 95 QFile file(selectedLanguageFile); 96 if (!file.open(QIODevice::ReadOnly)) return false; 97 98 QDomDocument document; 99 if (!document.setContent(&file)) return false; 100 101 languageElement = document.documentElement(); 102 103 soundNamesList = languageElement.elementsByTagName(QStringLiteral( "sound" )); 104 sounds = soundNamesList.count(); 105 if (sounds < 1) 106 return false; 107 108 109 namesList.clear(); 110 filesList.clear(); 111 for (int sound = 0; sound < sounds; sound++) 112 { 113 soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement(); 114 115 nameAttribute = soundNameElement.attributeNode(QStringLiteral( "name" )); 116 namesList << nameAttribute.value(); 117 fileAttribute = soundNameElement.attributeNode(QStringLiteral( "file" )); 118 filesList << fileAttribute.value(); 119 } 120 121 currentSndFile = selectedLanguageFile; 122 123 return true; 124 } 125 currentSoundFile() const126QString SoundFactory::currentSoundFile() const 127 { 128 return currentSndFile; 129 } 130