File indexing completed on 2024-04-21 04:05:06

0001 /*
0002     SPDX-FileCopyrightText: 1999-2006 Éric Bischoff <ebischoff@nerim.net>
0003     SPDX-FileCopyrightText: 2007 Albert Astals Cid <aacid@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 /* Sound factory */
0009 
0010 #include "soundfactory.h"
0011 
0012 #include <stdlib.h>
0013 
0014 #include <QAudioOutput>
0015 #include <QDir>
0016 #include <QDomDocument>
0017 #include <QFile>
0018 #include <QMediaPlayer>
0019 #include <QSet>
0020 #include <QUrl>
0021 
0022 #include "filefactory.h"
0023 
0024 // Constructor
0025 SoundFactory::SoundFactory(SoundFactoryCallbacks *callbacks)
0026  : m_callbacks(callbacks)
0027 {
0028   player = new QMediaPlayer();
0029   player->setAudioOutput(new QAudioOutput);
0030 }
0031 
0032 // Destructor
0033 SoundFactory::~SoundFactory()
0034 {
0035   delete player;
0036 }
0037 
0038 // Play some sound
0039 void SoundFactory::playSound(const QString &soundRef) const
0040 {
0041   if (!m_callbacks->isSoundEnabled()) return;
0042 
0043   int sound;
0044   for (sound = 0; sound < sounds; sound++)
0045       if (!namesList[sound].compare(soundRef)) break;
0046   if (sound == sounds) return;
0047 
0048   const QString soundFile = FileFactory::locate(QLatin1String( "sounds/" ) + filesList[sound]);
0049   if (soundFile.isEmpty()) return;
0050 
0051   if (soundFile.startsWith(QLatin1Char(':'))) {
0052     player->setSource(QUrl(QLatin1String("qrc") + soundFile));
0053   } else {
0054     player->setSource(QUrl::fromLocalFile(soundFile));
0055   }
0056   // we need to go to 0, otherwise sounds don't play again if the soundfile hasn't changed
0057   player->setPosition(0);
0058   player->play();
0059 }
0060 
0061 // Register the various languages
0062 void SoundFactory::registerLanguages()
0063 {
0064   QSet<QString> list;
0065   const QStringList dirs = FileFactory::locateAll(QStringLiteral("sounds"));
0066   for (const QString &dir : dirs)
0067   {
0068     const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.soundtheme"));
0069     for (const QString &file :  fileNames)
0070     {
0071         list <<dir + QLatin1Char('/') + file;
0072     }
0073   }
0074 
0075   for(const QString &soundTheme : std::as_const(list))
0076   {
0077     QFile file(soundTheme);
0078     if (file.open(QIODevice::ReadOnly))
0079     {
0080       QDomDocument document;
0081       if (document.setContent(&file))
0082       {
0083         const QString code = document.documentElement().attribute(QStringLiteral( "code" ));
0084         const bool enabled = FileFactory::folderExists(QLatin1String( "sounds/" ) + code + QLatin1Char( '/' ));
0085         m_callbacks->registerLanguage(code, soundTheme, enabled);
0086       }
0087     }
0088   }
0089 }
0090 
0091 // Load the sounds of one given language
0092 bool SoundFactory::loadLanguage(const QString &selectedLanguageFile)
0093 {
0094   QDomNodeList languagesList,
0095                soundNamesList;
0096   QDomElement languageElement,
0097               soundNameElement;
0098   QDomAttr nameAttribute, fileAttribute;
0099 
0100   QFile file(selectedLanguageFile);
0101   if (!file.open(QIODevice::ReadOnly)) return false;
0102 
0103   QDomDocument document;
0104   if (!document.setContent(&file)) return false;
0105 
0106   languageElement = document.documentElement();
0107 
0108   soundNamesList = languageElement.elementsByTagName(QStringLiteral( "sound" ));
0109   sounds = soundNamesList.count();
0110   if (sounds < 1)
0111     return false;
0112 
0113 
0114   namesList.clear();
0115   filesList.clear();
0116   for (int sound = 0; sound < sounds; sound++)
0117   {
0118     soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement();
0119 
0120     nameAttribute = soundNameElement.attributeNode(QStringLiteral( "name" ));
0121     namesList << nameAttribute.value();
0122     fileAttribute = soundNameElement.attributeNode(QStringLiteral( "file" ));
0123     filesList << fileAttribute.value();
0124   }
0125 
0126   currentSndFile = selectedLanguageFile;
0127 
0128   return true;
0129 }
0130 
0131 QString SoundFactory::currentSoundFile() const
0132 {
0133   return currentSndFile;
0134 }