SVN_SILENT made messages (.desktop file)
[kdegames.git] / kblocks / kblockssound.cpp
blobf8d6d3cd4b4138655a2f5164bf57d2fc13e04a75
1 /***************************************************************************
2 * KBlocks, a falling blocks game for KDE *
3 * Copyright (C) 20078 Mauricio Piacentini <piacentini@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 //Uses routines from Kapman sound manager (game.cpp)
12 #include "kblockssound.h"
13 #include <KDebug>
14 #include <KStandardDirs>
15 #include <QVariant>
16 #include <QFile>
17 #include <QFileInfo>
19 #include "settings.h"
21 KBlocksSound::KBlocksSound ( const QString& themeFile )
23 m_media1 = 0;
24 m_media2 = 0;
25 m_theme = new KGameTheme();
26 if (!m_theme->load(themeFile)) {
27 kDebug(11000) << "Error loading KBlocks .desktop theme" << themeFile << endl;
28 m_theme->loadDefault();
30 readThemeValues();
32 setSoundsEnabled(Settings::sounds());
35 KBlocksSound::~KBlocksSound()
37 delete m_theme;
38 delete m_media1;
39 delete m_media2;
42 bool KBlocksSound::loadTheme ( const QString& themeFile )
44 if (!m_theme->load(themeFile)) {
45 kDebug(11000) << "Error loading KBlocks .desktop theme" << themeFile << endl;
46 return false;
49 readThemeValues();
50 return true;
53 void KBlocksSound::readThemeValues()
55 //Extract sound directory info
56 //This functionality should be exposed by KGameTheme
57 QFile themefile(m_theme->path());
58 sndDirectory = QFileInfo(themefile).absolutePath() + '/';
59 themefile.close();
62 void KBlocksSound::setSoundsEnabled(bool p_enabled) {
63 sndActive = p_enabled;
64 if (p_enabled) {
65 if (!m_media1) {
66 m_media1 = Phonon::createPlayer(Phonon::GameCategory);
68 if (!m_media2) {
69 m_media2 = Phonon::createPlayer(Phonon::GameCategory);
71 } else {
72 delete m_media1;
73 delete m_media2;
74 m_media1 = 0;
75 m_media2 = 0;
79 void KBlocksSound::playSound(const QString& p_soundkey) {
80 Phonon::MediaObject* m_usedMedia;
81 QString p_sound = sndDirectory+m_theme->themeProperty(p_soundkey);
82 kDebug() << p_sound;
83 if (sndActive) {
84 // Choose the media object with the smallest remaining time
85 if (m_media1->remainingTime() <= m_media2->remainingTime()) {
86 m_usedMedia = m_media1;
87 } else {
88 m_usedMedia = m_media2;
90 m_usedMedia->setCurrentSource(p_sound);
91 m_usedMedia->play();