summaryrefslogtreecommitdiffstats
path: root/arts/kde/kplayobjectfactory.cc
blob: c02b896a5f0ee8090fe1cf06526f9358085938c5 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
	/*

	Copyright (C) 2001 Nikolas Zimmermann <wildfox@kde.org>

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Library General Public
	License as published by the Free Software Foundation; either
	version 2 of the License, or (at your option) any later version.

	This library 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
	Library General Public License for more details.

	You should have received a copy of the GNU Library General Public License
	along with this library; see the file COPYING.LIB.  If not, write to
	the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
	Boston, MA 02110-1301, USA.

	*/

#include <kio/kmimetype.h>
#include "kplayobject.h"
#include "artskde.h"
#include "kplayobjectfactory.h"
#include "kplayobjectfactory_p.h"
#include "kplayobjectcreator.h"
#include "kioinputstream_impl.h"
#include "kartsdispatcher.h"
#include "kartsserver.h"

#include <tqfile.h>
#include <kdebug.h>
#include "kaudiomanagerplay.h"
#include <flowsystem.h>
#include <kio/netaccess.h>

using namespace std;

KPlayObjectFactory::KPlayObjectFactory(Arts::SoundServerV2 server)
{
	m_server = server;
	m_allowStreaming = true;
	m_stream = false;
}

KPlayObjectFactory::KPlayObjectFactory(KArtsServer* server)
{
	m_server = server->server();
	m_allowStreaming = true;
	m_stream = false;
}

KPlayObjectFactory::~KPlayObjectFactory()
{
}

KPlayObject *KPlayObjectFactory::createPlayObject(const KURL& url, bool createBUS)
{
	KMimeType::Ptr mimetype = KMimeType::findByURL(url);
	return createPlayObject(url, mimetype->name(), createBUS);
}


KPlayObject *KPlayObjectFactory::createPlayObject(const KURL& _url, const TQString &mimetype, bool createBUS)
{
	// WHY DOES BROKEN KIO_MEDIA GIVE WRONG URLS?
	// I hate it
	// I hate it
	// It sucks
	// kio_media please die
	KURL url = KIO::NetAccess::mostLocalURL(_url, 0);

	if(!m_server.isNull())
	{
		if(mimetype == "application/octet-stream" && m_allowStreaming)
		{
			Arts::KIOInputStream instream;
			instream.openURL(url.url().latin1());

			m_stream = true;

			// TODO: what else than hardcoding audio/x-mp3 ?
			return new KPlayObject(m_server.createPlayObjectForStream(instream, string("audio/x-mp3"), createBUS), true);
		}
		else
			return new KPlayObject(m_server.createPlayObjectForURL(string(TQFile::encodeName(url.path())), string(mimetype.latin1()), createBUS), false);
	}
	else
		return new KPlayObject();
}



//

KDE::PlayObjectFactory::PlayObjectFactory(Arts::SoundServerV2 server)
{
	d = new PrivateData;
	d->server = server;
	d->amanPlay = 0;
	d->helper = 0;
	d->allowStreaming = true;
	d->isStream = false;
}

KDE::PlayObjectFactory::PlayObjectFactory(KArtsServer* server)
{
	d = new PrivateData;
	d->server = server->server();
	d->amanPlay = 0;
	d->helper = 0;
	d->allowStreaming = true;
	d->isStream = false;
}

KDE::PlayObjectFactory::~PlayObjectFactory()
{
	delete d->helper;
	delete d;
}

void KDE::PlayObjectFactory::setAudioManagerPlay( KAudioManagerPlay * amanPlay )
{
	d->amanPlay = amanPlay;
	if( ! d->helper )
		d->helper = new POFHelper;
}

KDE::PlayObject *KDE::PlayObjectFactory::createPlayObject(const KURL& url, bool createBUS)
{
	KMimeType::Ptr mimetype = KMimeType::findByURL(url);
	return createPlayObject(url, mimetype->name(), createBUS);
}

KDE::PlayObject *KDE::PlayObjectFactory::createPlayObject(const KURL& _url, const TQString &mimetype, bool createBUS)
{
	// WHY DOES BROKEN KIO_MEDIA GIVE WRONG URLS?
	// I hate it
	// I hate it
	// It sucks
	// kio_media please die
	KURL url = KIO::NetAccess::mostLocalURL(_url, 0);
	
	// return a NULL playobject if the server is NULL
	if ( d->server.isNull() || url.isEmpty() )
		return new KDE::PlayObject();

	// if the program wants to use it's own Synth_AMAN_PLAY we don't need a
	// bus
	if( d->amanPlay && createBUS )
	{
		kdWarning( 400 ) << "KDE::PlayObjectFactory was instructed to use a Synth_AMAN_PLAY for output but the program also asked for a Synth_BUS_UPLINK" << endl;
		createBUS = false;
	}

	// decide if it's a local file. mpeglib provides cdda reading and decoding, so we prefer that over kio_audiocd
	if ( url.isLocalFile() || !d->allowStreaming || (url.protocol() == "audiocd" && mimetype == "application/x-cda" && mimeTypes().tqcontains( "application/x-cda" ) ) )
	{
		// we rely on the delivered mimetype if it's a local file
		d->playObj = new KDE::PlayObject( d->server.createPlayObjectForURL( string( TQFile::encodeName( url.path() ) ), string( mimetype.latin1() ), createBUS ), false );
	}
	else
	{
		// if non-local, let the KPlayObject figure out the mimetype itself
		// this invokes asynchronous creation automatically
		d->playObj = new KDE::PlayObject( d->server, url, true, createBUS );
	}

	if( d->playObj->isNull() )
	{
		delete d->playObj;
		d->playObj = 0;
		return new KDE::PlayObject(); // return a NULL playobject
	}

	if( d->amanPlay )
	{
		d->helper->po = d->playObj;
		d->helper->ap = d->amanPlay;
		if( d->playObj->object().isNull() && d->amanPlay )
			TQObject::connect( d->playObj, TQT_SIGNAL( playObjectCreated() ), d->helper, TQT_SLOT( connectAmanPlay() ) );
		else
			d->helper->connectAmanPlay();
	}

	return d->playObj;
}

TQStringList KDE::PlayObjectFactory::mimeTypes(void)
{
	KArtsDispatcher dispatcher; // we need such a thing, otherwise we crash
	Arts::TraderQuery query;
	vector<Arts::TraderOffer> *offers = query.query();

	TQStringList results;
	for(vector<Arts::TraderOffer>::iterator offer = offers->begin();
	    offer != offers->end(); ++offer)
	{
		vector<string> *mimetypes = (*offer).getProperty("MimeType");

		for(vector<string>::iterator mimetype = mimetypes->begin();
		    mimetype != mimetypes->end(); ++mimetype)
		{
			TQString name = TQString::fromLocal8Bit((*mimetype).c_str()).stripWhiteSpace();
			if(KMimeType::mimeType(name))
				results.append(name);
		}

		delete mimetypes;
	}
	delete offers;

	// clean out duplicates
	results.sort();
	for(TQStringList::iterator result = results.begin(); result != results.end(); )
	{
		TQStringList::iterator previous = result;
		++result;
		if(result != results.end() && *result == *previous)
		{
			results.remove(result);
			result = previous;
		}
	}

	return results;
}

/* ### KDE4
void KDE::PlayObjectFactory::connectAmanPlay()
{
	kdDebug( 400 ) << k_funcinfo << endl;
	if( d->playObj->object().isNull() )
		return;

	d->amanPlay->start();
	d->playObj->object()._node()->start();
	Arts::connect( d->playObj->object(), "left" , d->amanPlay->amanPlay(), "left" );
	Arts::connect( d->playObj->object(), "right", d->amanPlay->amanPlay(), "right" );
}
*/

void KDE::POFHelper::connectAmanPlay()
{
	kdDebug( 400 ) << k_funcinfo << endl;
	if( po->object().isNull() )
		return;

	ap->start();
	po->object()._node()->start();
	Arts::connect( po->object(), "left" , ap->amanPlay(), "left" );
	Arts::connect( po->object(), "right", ap->amanPlay(), "right" );
}

#include "kplayobjectfactory_p.moc"

// vim: sw=4 ts=4 noet