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 <kmessagebox.h> |
18 | #include <klocale.h> |
19 | #include <kstandarddirs.h> |
20 | #include <kdebug.h> |
21 | |
22 | #include <phonon/MediaObject> |
23 | |
24 | #include <QDomDocument> |
25 | #include <QFile> |
26 | |
27 | #include "toplevel.h" |
28 | |
29 | // Constructor |
30 | SoundFactory::SoundFactory(TopLevel *parent) |
31 | { |
32 | topLevel = parent; |
33 | player = Phonon::createPlayer(Phonon::GameCategory); |
34 | player->setParent(parent); |
35 | } |
36 | |
37 | // Destructor |
38 | SoundFactory::~SoundFactory() |
39 | { |
40 | } |
41 | |
42 | // Play some sound |
43 | void SoundFactory::playSound(const QString &soundRef) const |
44 | { |
45 | int sound; |
46 | QString soundFile; |
47 | |
48 | if (!topLevel->isSoundEnabled()) return; |
49 | |
50 | for (sound = 0; sound < sounds; sound++) |
51 | if (!namesList[sound].compare(soundRef)) break; |
52 | if (sound == sounds) return; |
53 | |
54 | soundFile = KStandardDirs::locate("appdata" , QLatin1String( "sounds/" ) + filesList[sound]); |
55 | if (soundFile.isEmpty()) return; |
56 | |
57 | //printf("%s\n", (const char *) soundFile); |
58 | player->setCurrentSource(soundFile); |
59 | player->play(); |
60 | } |
61 | |
62 | // Register the various languages |
63 | void SoundFactory::registerLanguages() |
64 | { |
65 | const QStringList list = KGlobal::dirs()->findAllResources("appdata" , QLatin1String( "sounds/*.soundtheme" )); |
66 | |
67 | foreach(const QString &soundTheme, list) |
68 | { |
69 | QFile file(soundTheme); |
70 | if (file.open(QIODevice::ReadOnly)) |
71 | { |
72 | QDomDocument document; |
73 | if (document.setContent(&file)) |
74 | { |
75 | QString code = document.documentElement().attribute(QLatin1String( "code" )); |
76 | bool enabled = !(KStandardDirs::locate("appdata" , QLatin1String( "sounds/" ) + code + QLatin1Char( '/' )).isEmpty()); |
77 | topLevel->registerLanguage(code, soundTheme, enabled); |
78 | } |
79 | } |
80 | } |
81 | } |
82 | |
83 | // Load the sounds of one given language |
84 | bool SoundFactory::loadLanguage(const QString &selectedLanguageFile) |
85 | { |
86 | QDomNodeList languagesList, |
87 | soundNamesList; |
88 | QDomElement languageElement, |
89 | soundNameElement; |
90 | QDomAttr nameAttribute, fileAttribute; |
91 | |
92 | QFile file(selectedLanguageFile); |
93 | if (!file.open(QIODevice::ReadOnly)) return false; |
94 | |
95 | QDomDocument document; |
96 | if (!document.setContent(&file)) return false; |
97 | |
98 | languageElement = document.documentElement(); |
99 | |
100 | soundNamesList = languageElement.elementsByTagName(QLatin1String( "sound" )); |
101 | sounds = soundNamesList.count(); |
102 | if (sounds < 1) |
103 | return false; |
104 | |
105 | |
106 | namesList.clear(); |
107 | filesList.clear(); |
108 | for (int sound = 0; sound < sounds; sound++) |
109 | { |
110 | soundNameElement = (const QDomElement &) soundNamesList.item(sound).toElement(); |
111 | |
112 | nameAttribute = soundNameElement.attributeNode(QLatin1String( "name" )); |
113 | namesList << nameAttribute.value(); |
114 | fileAttribute = soundNameElement.attributeNode(QLatin1String( "file" )); |
115 | filesList << fileAttribute.value(); |
116 | } |
117 | |
118 | currentSndFile = selectedLanguageFile; |
119 | |
120 | return true; |
121 | } |
122 | |
123 | QString SoundFactory::currentSoundFile() const |
124 | { |
125 | return currentSndFile; |
126 | } |
127 | |