summaryrefslogtreecommitdiffstats
path: root/blinken/src/artsplayer.cpp
blob: a83890f929027183adfdfaf0c75c10a6d051557d (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
/***************************************************************************
 *   Copyright (C) 2005 by Albert Astals Cid <tsdgeos@terra.es>            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include <tqtimer.h>

#include <config.h>

#ifdef WITHOUT_ARTS
#include <tdelocale.h>
#include <tdemessagebox.h>
#endif
#include <kstandarddirs.h>

#include "artsplayer.h"
#include "settings.h"

artsPlayer::artsPlayer() : m_playobj(0)
{
	m_endChecker = new TQTimer(this);
	connect(m_endChecker, TQT_SIGNAL(timeout()), this, TQT_SLOT(checkEnded()));
	
#ifndef WITHOUT_ARTS
	m_dispatcher = new KArtsDispatcher;
	m_server = new KArtsServer;
	m_factory = new KDE::PlayObjectFactory(m_server->server());
	
	m_allPath = locate("appdata","sounds/lose.wav");
	m_greenPath = locate("appdata","sounds/1.wav");
	m_redPath = locate("appdata","sounds/2.wav");
	m_bluePath = locate("appdata","sounds/3.wav");
	m_yellowPath = locate("appdata","sounds/4.wav");
#else
	KMessageBox::information(0, i18n("aRts was not found, therefore the sounds will be disabled."), i18n("Sounds Disabled"), "infoaboutartsnotfound");
#endif
}

artsPlayer::~artsPlayer()
{
#ifndef WITHOUT_ARTS
	delete m_playobj;
	delete m_factory;
	delete m_server;
	delete m_dispatcher;
#endif
}

void artsPlayer::play(blinkenGame::color c, bool stopCurrent)
{
	int check;
#ifndef WITHOUT_ARTS
	if (blinkenSettings::playSounds())
	{
		if (m_playobj && m_playobj -> state() == Arts::posPlaying)
		{
			if (stopCurrent)
			{
				m_nextSounds.clear();
				m_nextSounds.append(c);
				m_playobj -> halt();
				play();
			}
			else m_nextSounds.append(c);
		}
		else
		{
			m_nextSounds.append(c);
			play();
		}
		check = 50;
	}
	else check = 250;
#else
	//shut up gcc
	(void)c;
	(void)stopCurrent;
	check = 250;
#endif
	if (!m_endChecker -> isActive()) m_endChecker -> start(check);
}

void artsPlayer::play()
{
#ifndef WITHOUT_ARTS
	TQString path;
	blinkenGame::color c = m_nextSounds.first();
	m_nextSounds.pop_front();
	switch (c)
	{
		case blinkenGame::red:
			path = m_redPath;
		break;
		
		case blinkenGame::green:
			path = m_greenPath;
		break;
		
		case blinkenGame::blue:
			path = m_bluePath;
		break;
		
		case blinkenGame::yellow:
			path = m_yellowPath;
		break;
		
		case blinkenGame::all:
			path = m_allPath;
		break;
		
		case blinkenGame::none:
		break;
	}
	delete m_playobj;
	m_playobj = m_factory -> createPlayObject(path, true);
	m_playobj -> play();
#endif
}

void artsPlayer::checkEnded()
{
#ifndef WITHOUT_ARTS
	if (blinkenSettings::playSounds())
	{
		if (m_playobj -> state() != Arts::posPlaying)
		{
			m_endChecker -> stop();
			emit ended();
			if (m_nextSounds.size() > 0) play();
		}
	}
	else
	{
		m_endChecker -> stop();
		emit ended();
	}
#else
	m_endChecker -> stop();
	emit ended();
#endif
}

#include "artsplayer.moc"