summaryrefslogtreecommitdiffstats
path: root/klettres/klettres/soundfactory.cpp
blob: e23bc50106de65e845a83f2469c2c1ac5155ed12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -------------------------------------------------------------
   From KDE Tuberling
   mailto:e.bischoff@noos.fr
 ------------------------------------------------------------- */
/*
 * Copyright (C) 2001 Eric Bischoff
   2004-2006 Anne-Marie Mahfouf <annma@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include <stdlib.h>

#include <kmessagebox.h>
#include <klocale.h>
#include <kaudioplayer.h>
#include <kdebug.h>
#include <kstandarddirs.h>

#include "soundfactory.h"
#include "soundfactory.moc"
#include "klettres.h"
#include "prefs.h"


SoundFactory::SoundFactory(KLettres *parent, const char *name)
        : TQObject(parent, name)
{
    klettres = parent;

    namesList = filesList = 0;
    sounds = 0;

    bool ok = klettres->loadLayout(m_layoutsDocument);
    if (ok) change(Prefs::language());
    if (!ok) loadFailure();
}

SoundFactory::~SoundFactory()
{
}

void SoundFactory::change(TQString currentLanguage)
{
    //go load the sounds for the current language
    bool ok = loadLanguage(m_layoutsDocument, currentLanguage);
    kdDebug() << "ok " << ok << endl;
    //tell the user if there are no sounds
    if (!ok) loadFailure();
}

void SoundFactory::playSound(int mySound)
{
    TQString soundFile;

    if ((uint) mySound >= sounds) return;

    soundFile = locate("data", "klettres/" + filesList[mySound]);
    kdDebug() << "soundFile " << soundFile << endl;
    
    if (soundFile == 0) return;
    
    KAudioPlayer::play(soundFile);
}

void SoundFactory::loadFailure()
{
    KMessageBox::error(klettres, i18n("Error while loading the sound names."));
}

bool SoundFactory::loadLanguage(TQDomDocument &layoutDocument, TQString currentLanguage)
{
    TQDomNodeList languagesList,
    alphabetList,
    syllablesList,
    soundNamesList;
    TQDomElement languageElement,
    alphabetElement,
    syllableElement,
    soundNameElement;
    TQDomAttr nameAttribute, fileAttribute;

    languagesList = layoutDocument.elementsByTagName("language");
    TQDomAttr codeAttribute;
    //check if the sound files match current language
    languageElement = (const TQDomElement &) languagesList.item(0).toElement();
    codeAttribute = languageElement.attributeNode("code");
    if (currentLanguage != codeAttribute.value()) {
        kdDebug() << "Fail reading language !!! " << endl;
        return false;
    }
    else kdDebug() << "current language " << currentLanguage << endl;
    //load the sounds for level 1 and 2 (alphabet)
    if ((Prefs::level() == 1) || (Prefs::level() == 2))
    {
        alphabetList = languageElement.elementsByTagName("alphabet");
        if (alphabetList.count() != 1)
            return false;

        alphabetElement = (const TQDomElement &) alphabetList.item(0).toElement();

        soundNamesList = alphabetElement.elementsByTagName("sound");
    }

    //load the sounds for level 3 and 4 (syllables)
    if ((Prefs::level() == 3) || (Prefs::level() == 4))
    {
        syllablesList = languageElement.elementsByTagName("syllables");
        if (syllablesList.count() != 1)
            return false;

        syllableElement = (const TQDomElement &) syllablesList.item(0).toElement();

        soundNamesList = syllableElement.elementsByTagName("sound");
    }
    //Counts the number of sounds
    sounds = soundNamesList.count();
    if (sounds < 1)
        return false;

   namesList.clear();
   filesList.clear();

    for (uint sound = 0; sound < sounds; sound++)
    {
        soundNameElement = (const TQDomElement &) soundNamesList.item(sound).toElement();
        nameAttribute = soundNameElement.attributeNode("name");
        //namesList helds the names of the letter or syllable to display
        namesList.append(nameAttribute.value());
        fileAttribute = soundNameElement.attributeNode("file");
        //filesList helds the names of the sound files (i.e the location of the sounds like fr/alpha/a-0.mp3)
        filesList.append(fileAttribute.value());
    }
    if (namesList.isEmpty())   return false;
    if (filesList.isEmpty())   return false;
    return true;
}