summaryrefslogtreecommitdiffstats
path: root/kdemm/factory.cpp
blob: 1994b8e5eabfa76fcabb25eeb03cc7bd413477fe (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*  This file is part of the KDE project
    Copyright (C) 2004 Matthias Kretz <kretz@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 version 2 as published by the Free Software Foundation.

    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., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

*/

#include "factory.h"
#include "backend.h"
#include "player.h"
#include "videoplayer.h"
#include "channel.h"

#include <ktrader.h>
#include <kservice.h>
#include <klibloader.h>
#include <kmessagebox.h>
#include <tqfile.h>
#include <klocale.h>
#include <kmimetype.h>
#include <kdebug.h>

namespace KDE
{
namespace Multimedia
{
class Factory::Private
{
	public:
		Private()
			: backend( 0 )
		{
			createBackend();
		}

		void createBackend()
		{
			KTrader::OfferList offers = KTrader::self()->query( "KDEMultimediaBackend", "Type == 'Service'" );
			KTrader::OfferListIterator it = offers.begin();
			KTrader::OfferListIterator end = offers.end();
			TQStringList errormsg;
			for( ; it != end; ++it )
			{
				KService::Ptr ptr = *it;
				KLibFactory * factory = KLibLoader::self()->factory( TQFile::encodeName( ptr->library() ) );
				if( factory )
				{
					backend = ( Backend* )factory->create( 0, "Multimedia Backend", "KDE::Multimedia::Backend" );
					if( 0 == backend )
					{
						TQString e = i18n( "create method returned 0" );
						errormsg.append( e );
						kdDebug( 600 ) << "Error getting backend from factory for " <<
							ptr->name() << ":\n" << e << endl;
					}
					else
					{
						service = ptr;
						kdDebug( 600 ) << "using backend: " << ptr->name() << endl;
						break;
					}
				}
				else
				{
					TQString e = KLibLoader::self()->lastErrorMessage();
					errormsg.append( e );
					kdDebug( 600 ) << "Error getting factory for " << ptr->name() <<
						":\n" << e << endl;
				}
			}
#if 0
			if( 0 == backend )
			{
				if( offers.size() == 0 )
					KMessageBox::error( 0, i18n( "Unable to find a Multimedia Backend" ) );
				else
				{
					TQString details = "<qt><table>";
					TQStringList::Iterator eit = errormsg.begin();
					TQStringList::Iterator eend = errormsg.end();
					KTrader::OfferListIterator oit = offers.begin();
					KTrader::OfferListIterator oend = offers.end();
					for( ; eit != eend || oit != oend; ++eit, ++oit )
						details += TQString( "<tr><td><b>%1</b></td><td>%2</td></tr>" )
							.arg( ( *oit )->name() ).arg( *eit );
					details += "</table></qt>";

					KMessageBox::detailedError( 0,
							i18n( "Unable to use any of the available Multimedia Backends" ), details );
				}
			}
#endif
		}

		Backend * backend;
		KService::Ptr service;

		TQValueList<void*> objects;
};

Factory * Factory::m_self = 0;

Factory * Factory::self()
{
	if( ! m_self )
		m_self = new Factory();
	return m_self;
}

Factory::Factory()
	: DCOPObject( "KDEMMFactory" )
	, d( new Private )
{
	connectDCOPSignal( 0, 0, "kdemmBackendChanged()", "kdemmBackendChanged()", false);
}

Factory::~Factory()
{
	delete d;
}

void Factory::kdemmBackendChanged()
{
	if( d->backend )
	{
		// wouw, if we want to switch on the fly we have to exchange the
		// (Video)Player and Channel classes without the API user noticing. That
		// would mean to implement a kind of smartwrapper:
		// Player: Interface that accesses Player_skel which is abstract and is
		// implemented by the Backend. The API user would only get access to the
		// Player class and if you want to switch the backend you can delete the
		// Player_skel object and put another implementation in its place.
		//
		// Now we tell the kdemm using app to help us. With the first signal we
		// tell 'em to delete all the (Video)Players and Channels and with the
		// second to recreate them all again.
		emit deleteYourObjects();
		if( d->objects.size() > 0 )
		{
			kdWarning( 600 ) << "we were asked to change the backend but the application did\n"
				"not free all references to objects created by the factory. Therefor we can not\n"
				"change the backend without crashing. Now we have to wait for a restart to make\n"
				"backendswitching possible." << endl;
			// in case there were objects deleted give 'em a chance to recreate
			// them now
			emit recreateObjects();
			return;
		}
		delete d->backend;
		d->backend = 0;
	}
	d->createBackend();
	emit recreateObjects();
}

void Factory::objectDestroyed( TQObject * obj )
{
	kdDebug( 600 ) << k_funcinfo << obj << endl;
	void * p = ( void* )obj;
	d->objects.remove( p );
}

Player * Factory::createPlayer()
{
	if( d->backend )
	{
		Player * p = d->backend->createPlayer();
		connect( p, TQT_SIGNAL( destroyed( TQObject* ) ), TQT_SLOT( objectDestroyed( TQObject* ) ) );
		d->objects.append( p );
		return p;
	}
	else
		return 0;
}

VideoPlayer * Factory::createVideoPlayer()
{
	if( d->backend )
	{
		VideoPlayer * vp = d->backend->createVideoPlayer();
		connect( vp, TQT_SIGNAL( destroyed( TQObject* ) ), TQT_SLOT( objectDestroyed( TQObject* ) ) );
		d->objects.append( vp );
		return vp;
	}
	else
		return 0;
}

bool Factory::playSoundEvent(const KURL & url)
{
	if( d->backend )
		return d->backend->playSoundEvent(url);
	else
		return false;
}

Channel * Factory::createChannel( const TQString & title, const TQString & channeltype,
		Channel::Direction direction )
{
	if( d->backend )
	{
		Channel * c = d->backend->createChannel( title, channeltype, direction );
		connect( c, TQT_SIGNAL( destroyed( TQObject* ) ), TQT_SLOT( objectDestroyed( TQObject* ) ) );
		d->objects.append( c );
		return c;
	}
	else
		return 0;
}

TQStringList Factory::availableChannels( Channel::Direction direction ) const
{
	if( d->backend )
		return d->backend->availableChannels( direction );
	else
		return TQStringList();
}

TQStringList Factory::playableMimeTypes() const
{
	if( d->backend )
		return d->backend->playableMimeTypes();
	else
		return TQStringList();
}

bool Factory::isMimeTypePlayable( const TQString & type ) const
{
	if( d->backend )
	{
		KMimeType::Ptr mimetype = KMimeType::mimeType( type );
		TQStringList mimetypes = playableMimeTypes();
		for( TQStringList::ConstIterator i=mimetypes.begin(); i!=mimetypes.end(); i++ )
			if( mimetype->is( *i ) )
				return true;
	}
	return false;
}

TQString Factory::backendName() const
{
	if( d->service )
		return d->service->name();
	else
		return TQString::null;
}

TQString Factory::backendComment() const
{
	if( d->service )
		return d->service->comment();
	else
		return TQString::null;
}

TQString Factory::backendVersion() const
{
	if( d->service )
		return d->service->property( "X-KDE-MMBackendInfo-Version" ).toString();
	else
		return TQString::null;
}

TQString Factory::backendIcon() const
{
	if( d->service )
		return d->service->icon();
	else
		return TQString::null;
}

TQString Factory::backendWebsite() const
{
	if( d->service )
		return d->service->property( "X-KDE-MMBackendInfo-Website" ).toString();
	else
		return TQString::null;
}

}} //namespaces

#include "factory.moc"

// vim: sw=4 ts=4 noet