summaryrefslogtreecommitdiffstats
path: root/ktuberling/soundfactory.cpp
blob: 857c26d5c89d8ddd013ff01193c9ab3a84e258f9 (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
/* -------------------------------------------------------------
   KDE Tuberling
   Play ground widget
   mailto:e.bischoff@noos.fr
 ------------------------------------------------------------- */

#include <stdlib.h>

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

#include <tqdom.h>

#include "soundfactory.h"
#include "soundfactory.moc"
#include "toplevel.h"

// Constructor
SoundFactory::SoundFactory(TopLevel *parent, const char *name, uint selectedLanguage)
	: TQObject(parent, name)
{
  topLevel = parent;

  namesList = filesList = 0;

  TQDomDocument layoutsDocument;
  bool ok = topLevel->loadLayout(layoutsDocument);
  if (ok) ok = registerLanguages(layoutsDocument);
  if (ok) ok = loadLanguage(layoutsDocument, selectedLanguage);
  if (!ok) loadFailure();
}

// Destructor
SoundFactory::~SoundFactory()
{
  if (namesList) delete [] namesList;
  if (filesList) delete [] filesList;
}

// Change the language
void SoundFactory::change(uint selectedLanguage)
{
  TQDomDocument layoutsDocument;
  bool ok = topLevel->loadLayout(layoutsDocument);
  if (ok) ok = loadLanguage(layoutsDocument, selectedLanguage);
  if (!ok) loadFailure();
}

// Play some sound
void SoundFactory::playSound(const TQString &soundRef) const
{
  int sound;
  TQString soundFile;

  if (!topLevel->isSoundEnabled()) return;

  for (sound = 0; sound < sounds; sound++)
	  if (!namesList[sound].compare(soundRef)) break;
  if (sound == sounds) return;

  soundFile = locate("data", "ktuberling/sounds/" + filesList[sound]);
  if (soundFile == 0) return;

//printf("%s\n", (const char *) soundFile);
  KAudioPlayer::play(soundFile);
}

// Report a load failure
void SoundFactory::loadFailure()
{
	KMessageBox::error(topLevel, i18n("Error while loading the sound names."));
}

// Register the various languages
bool SoundFactory::registerLanguages(TQDomDocument &layoutDocument)
{
  TQDomNodeList languagesList, menuItemsList, labelsList;
  TQDomElement languageElement, menuItemElement, labelElement;
  TQDomAttr codeAttribute, actionAttribute;
  bool enabled;

  languagesList = layoutDocument.elementsByTagName("language");
  if (languagesList.count() < 1)
    return false;

  for (uint i = 0; i < languagesList.count(); i++)
  {
    languageElement = (const TQDomElement &) languagesList.item(i).toElement();
    codeAttribute = languageElement.attributeNode("code");
    enabled = locate("data", "ktuberling/sounds/" + codeAttribute.value() + "/") != 0;

    menuItemsList = languageElement.elementsByTagName("menuitem");
    if (menuItemsList.count() != 1)
      return false;

    menuItemElement = (const TQDomElement &) menuItemsList.item(0).toElement();

    labelsList = menuItemElement.elementsByTagName("label");
    if (labelsList.count() != 1)
      return false;

    labelElement = (const TQDomElement &) labelsList.item(0).toElement();
    actionAttribute = menuItemElement.attributeNode("action");
    topLevel->registerLanguage(labelElement.text(), actionAttribute.value().latin1(), enabled); 
  }
 
  return true;
}

// Load the sounds of one given language
bool SoundFactory::loadLanguage(TQDomDocument &layoutDocument, uint toLoad)
{
  TQDomNodeList languagesList,
               soundNamesList;
  TQDomElement languageElement,
              soundNameElement;
  TQDomAttr nameAttribute, fileAttribute;

  languagesList = layoutDocument.elementsByTagName("language");
  if (toLoad >= languagesList.count())
    return false;

  languageElement = (const TQDomElement &) languagesList.item(toLoad).toElement();

  soundNamesList = languageElement.elementsByTagName("sound");
  sounds = soundNamesList.count();
  if (sounds < 1)
    return false;

  if (!(namesList = new TQString[sounds]))
    return false;
  if (!(filesList = new TQString[sounds]))
    return false;

  for (uint sound = 0; sound < sounds; sound++)
  {
    soundNameElement = (const TQDomElement &) soundNamesList.item(sound).toElement();

    nameAttribute = soundNameElement.attributeNode("name");
    namesList[sound] = nameAttribute.value();
    fileAttribute = soundNameElement.attributeNode("file");
    filesList[sound] = fileAttribute.value();
  }
  return true;
}