File indexing completed on 2023-09-24 04:19:19

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 <QDir>
0015 #include <QDomDocument>
0016 #include <QFile>
0017 #include <QMediaPlayer>
0018 #include <QSet>
0019 #include <QUrl>
0020 
0021 #include "filefactory.h"
0022 
0023 // Constructor
0024 SoundFactory::SoundFactory(SoundFactoryCallbacks *callbacks)
0025  : m_callbacks(callbacks)
0026 {
0027   player = new QMediaPlayer();
0028 }
0029 
0030 // Destructor
0031 SoundFactory::~SoundFactory()
0032 {
0033   delete player;
0034 }
0035 
0036 // Play some sound
0037 void SoundFactory::playSound(const QString &soundRef) const
0038 {
0039   if (!m_callbacks->isSoundEnabled()) return;
0040 
0041   int sound;
0042   for (sound = 0; sound < sounds; sound++)
0043       if (!namesList[sound].compare(soundRef)) break;
0044   if (sound == sounds) return;
0045 
0046   const QString soundFile = FileFactory::locate(QLatin1String( "sounds/" ) + filesList[sound]);
0047   if (soundFile.isEmpty()) return;
0048 
0049   if (soundFile.startsWith(':')) {
0050 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0051     player->setSource(QUrl("qrc" + soundFile));
0052 #else
0053     player->setMedia(QUrl("qrc" + soundFile));
0054 #endif
0055   } else {
0056 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0057     player->setSource(QUrl::fromLocalFile(soundFile));
0058 #else
0059     player->setMedia(QUrl::fromLocalFile(soundFile));
0060 #endif
0061   }
0062   player->play();
0063 }
0064 
0065 // Register the various languages
0066 void SoundFactory::registerLanguages()
0067 {
0068   QSet<QString> list;
0069   const QStringList dirs = FileFactory::locateAll(QStringLiteral("sounds"));
0070   for (const QString &dir : dirs)
0071   {
0072     const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.soundtheme"));
0073     for (const QString &file :  fileNames)
0074     {
0075         list <<dir + '/' + file;
0076     }
0077   }
0078 
0079   for(const QString &soundTheme : std::as_const(list))
0080   {
0081     QFile file(soundTheme);
0082     if (file.open(QIODevice::ReadOnly))
0083     {
0084       QDomDocument document;
0085       if (document.setContent(&file))
0086       {
0087         const QString code = document.documentElement().attribute(QStringLiteral( "code" ));
0088         const bool enabled = FileFactory::folderExists(QLatin1String( "sounds/" ) + code + QLatin1Char( '/' ));
0089         m_callbacks->registerLanguage(code, soundTheme, enabled);
0090       }
0091     }
0092   }
0093 }
0094 
0095 // Load the sounds of one given language
0096 bool SoundFactory::loadLanguage(const QString &selectedLanguageFile)
0097 {
0098   QDomNodeList languagesList,
0099                soundNamesList;
0100   QDomElement languageElement,
0101               soundNameElement;
0102   QDomAttr nameAttribute, fileAttribute;
0103 
0104   QFile file(selectedLanguageFile);
0105   if (!file.open(QIODevice::ReadOnly)) return false;
0106 
0107   QDomDocument document;
0108   if (!document.setContent(&file)) return false;
0109 
0110   languageElement = document.documentElement();
0111 
0112   soundNamesList = languageElement.elementsByTagName(QStringLiteral( "sound" ));
0113   sounds = soundNamesList.count();
0114   if (sounds < 1)
0115     return false;
0116 
0117 
0118   namesList.clear();
0119   filesList.clear();
0120   for (int sound = 0; sound < sounds; sound++)
0121   {
0122     soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement();
0123 
0124     nameAttribute = soundNameElement.attributeNode(QStringLiteral( "name" ));
0125     namesList << nameAttribute.value();
0126     fileAttribute = soundNameElement.attributeNode(QStringLiteral( "file" ));
0127     filesList << fileAttribute.value();
0128   }
0129 
0130   currentSndFile = selectedLanguageFile;
0131 
0132   return true;
0133 }
0134 
0135 QString SoundFactory::currentSoundFile() const
0136 {
0137   return currentSndFile;
0138 }