1/***************************************************************************
2 * KBlocks, a falling blocks game for KDE *
3 * Copyright (C) 2009 Mauricio Piacentini <mauricio@tabuleiro.com> *
4 * Zhongjie Cai <squall.leonhart.cai@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 ***************************************************************************/
11 //Uses routines from Kapman sound manager (game.cpp)
12
13#include "KBlocksSound.h"
14#include <KDebug>
15#include <QFile>
16#include <QFileInfo>
17
18#define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
19#include <libkdegamesprivate/kgametheme.h>
20
21#include "settings.h"
22
23KBlocksSound::KBlocksSound ( const QString& themeFile )
24{
25 m_media1 = 0;
26 m_media2 = 0;
27 m_theme = new KGameTheme();
28 if (!m_theme->load(themeFile)) {
29 kDebug(11000) << "Error loading KBlocks .desktop theme" << themeFile << endl;
30 m_theme->loadDefault();
31 kDebug(11000) << "Load default sound theme." << endl;
32 }
33 readThemeValues();
34 setSoundsEnabled(Settings::sounds());
35}
36
37KBlocksSound::~KBlocksSound()
38{
39 delete m_theme;
40 delete m_media1;
41 delete m_media2;
42}
43
44bool KBlocksSound::loadTheme ( const QString& themeFile )
45{
46 if (!m_theme->load(themeFile)) {
47 kDebug(11000) << "Error loading KBlocks .desktop theme" << themeFile << endl;
48 return false;
49 }
50 readThemeValues();
51 return true;
52}
53
54void KBlocksSound::readThemeValues()
55{
56 //Extract sound directory info
57 //This functionality should be exposed by KGameTheme
58 QFile themefile(m_theme->path());
59 sndDirectory = QFileInfo(themefile).absolutePath() + '/';
60 themefile.close();
61}
62
63void KBlocksSound::setSoundsEnabled(bool p_enabled) {
64 sndActive = p_enabled;
65 if (p_enabled)
66 {
67 if (!m_media1)
68 {
69 m_media1 = Phonon::createPlayer(Phonon::GameCategory);
70 }
71 if (!m_media2)
72 {
73 m_media2 = Phonon::createPlayer(Phonon::GameCategory);
74 }
75 }
76 else
77 {
78 delete m_media1;
79 delete m_media2;
80 m_media1 = 0;
81 m_media2 = 0;
82 }
83}
84
85void KBlocksSound::playSound(const QString& p_soundkey) {
86 Phonon::MediaObject* m_usedMedia;
87 QString p_sound = sndDirectory+m_theme->themeProperty(p_soundkey);
88 //kDebug(11000) << "Playing sound : " << p_sound << endl;
89 if (sndActive)
90 {
91 // Choose the media object with the smallest remaining time
92 if (m_media1->remainingTime() <= m_media2->remainingTime())
93 {
94 m_usedMedia = m_media1;
95 }
96 else
97 {
98 m_usedMedia = m_media2;
99 }
100 m_usedMedia->setCurrentSource(p_sound);
101 m_usedMedia->play();
102 }
103}
104
105
106