kdemm Library API Documentation

factory.cpp

00001 /*  This file is part of the KDE project
00002     Copyright (C) 2004 Matthias Kretz <kretz@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016     Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #include "factory.h"
00021 #include "backend.h"
00022 #include "player.h"
00023 #include "videoplayer.h"
00024 #include "channel.h"
00025 
00026 #include <ktrader.h>
00027 #include <kservice.h>
00028 #include <klibloader.h>
00029 #include <kmessagebox.h>
00030 #include <qfile.h>
00031 #include <klocale.h>
00032 #include <kmimetype.h>
00033 #include <kdebug.h>
00034 
00035 namespace KDE
00036 {
00037 namespace Multimedia
00038 {
00039 class Factory::Private
00040 {
00041     public:
00042         Private()
00043             : backend( 0 )
00044         {
00045             createBackend();
00046         }
00047 
00048         void createBackend()
00049         {
00050             KTrader::OfferList offers = KTrader::self()->query( "KDEMultimediaBackend", "Type == 'Service'" );
00051             KTrader::OfferListIterator it = offers.begin();
00052             KTrader::OfferListIterator end = offers.end();
00053             QStringList errormsg;
00054             for( ; it != end; ++it )
00055             {
00056                 KService::Ptr ptr = *it;
00057                 KLibFactory * factory = KLibLoader::self()->factory( QFile::encodeName( ptr->library() ) );
00058                 if( factory )
00059                 {
00060                     backend = ( Backend* )factory->create( 0, "Multimedia Backend", "KDE::Multimedia::Backend" );
00061                     if( 0 == backend )
00062                     {
00063                         QString e = i18n( "create method returned 0" );
00064                         errormsg.append( e );
00065                         kdDebug( 600 ) << "Error getting backend from factory for " <<
00066                             ptr->name() << ":\n" << e << endl;
00067                     }
00068                     else
00069                     {
00070                         service = ptr;
00071                         kdDebug( 600 ) << "using backend: " << ptr->name() << endl;
00072                         break;
00073                     }
00074                 }
00075                 else
00076                 {
00077                     QString e = KLibLoader::self()->lastErrorMessage();
00078                     errormsg.append( e );
00079                     kdDebug( 600 ) << "Error getting factory for " << ptr->name() <<
00080                         ":\n" << e << endl;
00081                 }
00082             }
00083 #if 0
00084             if( 0 == backend )
00085             {
00086                 if( offers.size() == 0 )
00087                     KMessageBox::error( 0, i18n( "Unable to find a Multimedia Backend" ) );
00088                 else
00089                 {
00090                     QString details = "<qt><table>";
00091                     QStringList::Iterator eit = errormsg.begin();
00092                     QStringList::Iterator eend = errormsg.end();
00093                     KTrader::OfferListIterator oit = offers.begin();
00094                     KTrader::OfferListIterator oend = offers.end();
00095                     for( ; eit != eend || oit != oend; ++eit, ++oit )
00096                         details += QString( "<tr><td><b>%1</b></td><td>%2</td></tr>" )
00097                             .arg( ( *oit )->name() ).arg( *eit );
00098                     details += "</table></qt>";
00099 
00100                     KMessageBox::detailedError( 0,
00101                             i18n( "Unable to use any of the available Multimedia Backends" ), details );
00102                 }
00103             }
00104 #endif
00105         }
00106 
00107         Backend * backend;
00108         KService::Ptr service;
00109 
00110         QValueList<void*> objects;
00111 };
00112 
00113 Factory * Factory::m_self = 0;
00114 
00115 Factory * Factory::self()
00116 {
00117     if( ! m_self )
00118         m_self = new Factory();
00119     return m_self;
00120 }
00121 
00122 Factory::Factory()
00123     : DCOPObject( "KDEMMFactory" )
00124     , d( new Private )
00125 {
00126     connectDCOPSignal( 0, 0, "kdemmBackendChanged()", "kdemmBackendChanged()", false);
00127 }
00128 
00129 Factory::~Factory()
00130 {
00131     delete d;
00132 }
00133 
00134 void Factory::kdemmBackendChanged()
00135 {
00136     if( d->backend )
00137     {
00138         // wouw, if we want to switch on the fly we have to exchange the
00139         // (Video)Player and Channel classes without the API user noticing. That
00140         // would mean to implement a kind of smartwrapper:
00141         // Player: Interface that accesses Player_skel which is abstract and is
00142         // implemented by the Backend. The API user would only get access to the
00143         // Player class and if you want to switch the backend you can delete the
00144         // Player_skel object and put another implementation in its place.
00145         //
00146         // Now we tell the kdemm using app to help us. With the first signal we
00147         // tell 'em to delete all the (Video)Players and Channels and with the
00148         // second to recreate them all again.
00149         emit deleteYourObjects();
00150         if( d->objects.size() > 0 )
00151         {
00152             kdWarning( 600 ) << "we were asked to change the backend but the application did\n"
00153                 "not free all references to objects created by the factory. Therefor we can not\n"
00154                 "change the backend without crashing. Now we have to wait for a restart to make\n"
00155                 "backendswitching possible." << endl;
00156             // in case there were objects deleted give 'em a chance to recreate
00157             // them now
00158             emit recreateObjects();
00159             return;
00160         }
00161         delete d->backend;
00162         d->backend = 0;
00163     }
00164     d->createBackend();
00165     emit recreateObjects();
00166 }
00167 
00168 void Factory::objectDestroyed( QObject * obj )
00169 {
00170     kdDebug( 600 ) << k_funcinfo << obj << endl;
00171     void * p = ( void* )obj;
00172     d->objects.remove( p );
00173 }
00174 
00175 Player * Factory::createPlayer()
00176 {
00177     if( d->backend )
00178     {
00179         Player * p = d->backend->createPlayer();
00180         connect( p, SIGNAL( destroyed( QObject* ) ), SLOT( objectDestroyed( QObject* ) ) );
00181         d->objects.append( p );
00182         return p;
00183     }
00184     else
00185         return 0;
00186 }
00187 
00188 VideoPlayer * Factory::createVideoPlayer()
00189 {
00190     if( d->backend )
00191     {
00192         VideoPlayer * vp = d->backend->createVideoPlayer();
00193         connect( vp, SIGNAL( destroyed( QObject* ) ), SLOT( objectDestroyed( QObject* ) ) );
00194         d->objects.append( vp );
00195         return vp;
00196     }
00197     else
00198         return 0;
00199 }
00200 
00201 bool Factory::playSoundEvent(const KURL & url)
00202 {
00203     if( d->backend )
00204         return d->backend->playSoundEvent(url);
00205     else
00206         return false;
00207 }
00208 
00209 Channel * Factory::createChannel( const QString & title, const QString & channeltype,
00210         Channel::Direction direction )
00211 {
00212     if( d->backend )
00213     {
00214         Channel * c = d->backend->createChannel( title, channeltype, direction );
00215         connect( c, SIGNAL( destroyed( QObject* ) ), SLOT( objectDestroyed( QObject* ) ) );
00216         d->objects.append( c );
00217         return c;
00218     }
00219     else
00220         return 0;
00221 }
00222 
00223 QStringList Factory::availableChannels( Channel::Direction direction ) const
00224 {
00225     if( d->backend )
00226         return d->backend->availableChannels( direction );
00227     else
00228         return QStringList();
00229 }
00230 
00231 QStringList Factory::playableMimeTypes() const
00232 {
00233     if( d->backend )
00234         return d->backend->playableMimeTypes();
00235     else
00236         return QStringList();
00237 }
00238 
00239 bool Factory::isMimeTypePlayable( const QString & type ) const
00240 {
00241     if( d->backend )
00242     {
00243         KMimeType::Ptr mimetype = KMimeType::mimeType( type );
00244         QStringList mimetypes = playableMimeTypes();
00245         for( QStringList::ConstIterator i=mimetypes.begin(); i!=mimetypes.end(); i++ )
00246             if( mimetype->is( *i ) )
00247                 return true;
00248     }
00249     return false;
00250 }
00251 
00252 QString Factory::backendName() const
00253 {
00254     if( d->service )
00255         return d->service->name();
00256     else
00257         return QString::null;
00258 }
00259 
00260 QString Factory::backendComment() const
00261 {
00262     if( d->service )
00263         return d->service->comment();
00264     else
00265         return QString::null;
00266 }
00267 
00268 QString Factory::backendVersion() const
00269 {
00270     if( d->service )
00271         return d->service->property( "X-KDE-MMBackendInfo-Version" ).toString();
00272     else
00273         return QString::null;
00274 }
00275 
00276 QString Factory::backendIcon() const
00277 {
00278     if( d->service )
00279         return d->service->icon();
00280     else
00281         return QString::null;
00282 }
00283 
00284 QString Factory::backendWebsite() const
00285 {
00286     if( d->service )
00287         return d->service->property( "X-KDE-MMBackendInfo-Website" ).toString();
00288     else
00289         return QString::null;
00290 }
00291 
00292 }} //namespaces
00293 
00294 #include "factory.moc"
00295 
00296 // vim: sw=4 ts=4 noet
KDE Logo
This file is part of the documentation for kdemm Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Sep 13 04:04:37 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003