kplayobjectfactory.cc

00001     /*
00002 
00003     Copyright (C) 2001 Nikolas Zimmermann <wildfox@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 
00020     */
00021 
00022 #include <kio/kmimetype.h>
00023 #include "kplayobject.h"
00024 #include "artskde.h"
00025 #include "kplayobjectfactory.h"
00026 #include "kplayobjectfactory_p.h"
00027 #include "kplayobjectcreator.h"
00028 #include "kioinputstream_impl.h"
00029 #include "kartsdispatcher.h"
00030 #include "kartsserver.h"
00031 
00032 #include <qfile.h>
00033 #include <kdebug.h>
00034 #include "kaudiomanagerplay.h"
00035 #include <flowsystem.h>
00036 #include <kio/netaccess.h>
00037 
00038 using namespace std;
00039 
00040 KPlayObjectFactory::KPlayObjectFactory(Arts::SoundServerV2 server)
00041 {
00042     m_server = server;
00043     m_allowStreaming = true;
00044     m_stream = false;
00045 }
00046 
00047 KPlayObjectFactory::KPlayObjectFactory(KArtsServer* server)
00048 {
00049     m_server = server->server();
00050     m_allowStreaming = true;
00051     m_stream = false;
00052 }
00053 
00054 KPlayObjectFactory::~KPlayObjectFactory()
00055 {
00056 }
00057 
00058 KPlayObject *KPlayObjectFactory::createPlayObject(const KURL& url, bool createBUS)
00059 {
00060     KMimeType::Ptr mimetype = KMimeType::findByURL(url);
00061     return createPlayObject(url, mimetype->name(), createBUS);
00062 }
00063 
00064 
00065 KPlayObject *KPlayObjectFactory::createPlayObject(const KURL& _url, const QString &mimetype, bool createBUS)
00066 {
00067     // WHY DOES BROKEN KIO_MEDIA GIVE WRONG URLS?
00068     // I hate it
00069     // I hate it
00070     // It sucks
00071     // kio_media please die
00072     KURL url = KIO::NetAccess::mostLocalURL(_url, 0);
00073 
00074     if(!m_server.isNull())
00075     {
00076         if(mimetype == "application/octet-stream" && m_allowStreaming)
00077         {
00078             Arts::KIOInputStream instream;
00079             instream.openURL(url.url().latin1());
00080 
00081             m_stream = true;
00082 
00083             // TODO: what else than hardcoding audio/x-mp3 ?
00084             return new KPlayObject(m_server.createPlayObjectForStream(instream, string("audio/x-mp3"), createBUS), true);
00085         }
00086         else
00087             return new KPlayObject(m_server.createPlayObjectForURL(string(QFile::encodeName(url.path())), string(mimetype.latin1()), createBUS), false);
00088     }
00089     else
00090         return new KPlayObject();
00091 }
00092 
00093 
00094 
00095 //
00096 
00097 KDE::PlayObjectFactory::PlayObjectFactory(Arts::SoundServerV2 server)
00098 {
00099     d = new PrivateData;
00100     d->server = server;
00101     d->amanPlay = 0;
00102     d->helper = 0;
00103     d->allowStreaming = true;
00104     d->isStream = false;
00105 }
00106 
00107 KDE::PlayObjectFactory::PlayObjectFactory(KArtsServer* server)
00108 {
00109     d = new PrivateData;
00110     d->server = server->server();
00111     d->amanPlay = 0;
00112     d->helper = 0;
00113     d->allowStreaming = true;
00114     d->isStream = false;
00115 }
00116 
00117 KDE::PlayObjectFactory::~PlayObjectFactory()
00118 {
00119     delete d->helper;
00120     delete d;
00121 }
00122 
00123 void KDE::PlayObjectFactory::setAudioManagerPlay( KAudioManagerPlay * amanPlay )
00124 {
00125     d->amanPlay = amanPlay;
00126     if( ! d->helper )
00127         d->helper = new POFHelper;
00128 }
00129 
00130 KDE::PlayObject *KDE::PlayObjectFactory::createPlayObject(const KURL& url, bool createBUS)
00131 {
00132     KMimeType::Ptr mimetype = KMimeType::findByURL(url);
00133     return createPlayObject(url, mimetype->name(), createBUS);
00134 }
00135 
00136 KDE::PlayObject *KDE::PlayObjectFactory::createPlayObject(const KURL& _url, const QString &mimetype, bool createBUS)
00137 {
00138     // WHY DOES BROKEN KIO_MEDIA GIVE WRONG URLS?
00139     // I hate it
00140     // I hate it
00141     // It sucks
00142     // kio_media please die
00143     KURL url = KIO::NetAccess::mostLocalURL(_url, 0);
00144     
00145     // return a NULL playobject if the server is NULL
00146     if ( d->server.isNull() || url.isEmpty() )
00147         return new KDE::PlayObject();
00148 
00149     // if the program wants to use it's own Synth_AMAN_PLAY we don't need a
00150     // bus
00151     if( d->amanPlay && createBUS )
00152     {
00153         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;
00154         createBUS = false;
00155     }
00156 
00157     // decide if it's a local file. mpeglib provides cdda reading and decoding, so we prefer that over kio_audiocd
00158     if ( url.isLocalFile() || !d->allowStreaming || (url.protocol() == "audiocd" && mimetype == "application/x-cda" && mimeTypes().contains( "application/x-cda" ) ) )
00159     {
00160         // we rely on the delivered mimetype if it's a local file
00161         d->playObj = new KDE::PlayObject( d->server.createPlayObjectForURL( string( QFile::encodeName( url.path() ) ), string( mimetype.latin1() ), createBUS ), false );
00162     }
00163     else
00164     {
00165         // if non-local, let the KPlayObject figure out the mimetype itself
00166         // this invokes asynchronous creation automatically
00167         d->playObj = new KDE::PlayObject( d->server, url, true, createBUS );
00168     }
00169 
00170     if( d->playObj->isNull() )
00171     {
00172         delete d->playObj;
00173         d->playObj = 0;
00174         return new KDE::PlayObject(); // return a NULL playobject
00175     }
00176 
00177     if( d->amanPlay )
00178     {
00179         d->helper->po = d->playObj;
00180         d->helper->ap = d->amanPlay;
00181         if( d->playObj->object().isNull() && d->amanPlay )
00182             QObject::connect( d->playObj, SIGNAL( playObjectCreated() ), d->helper, SLOT( connectAmanPlay() ) );
00183         else
00184             d->helper->connectAmanPlay();
00185     }
00186 
00187     return d->playObj;
00188 }
00189 
00190 QStringList KDE::PlayObjectFactory::mimeTypes(void)
00191 {
00192     KArtsDispatcher dispatcher; // we need such a thing, otherwise we crash
00193     Arts::TraderQuery query;
00194     vector<Arts::TraderOffer> *offers = query.query();
00195 
00196     QStringList results;
00197     for(vector<Arts::TraderOffer>::iterator offer = offers->begin();
00198         offer != offers->end(); ++offer)
00199     {
00200         vector<string> *mimetypes = (*offer).getProperty("MimeType");
00201 
00202         for(vector<string>::iterator mimetype = mimetypes->begin();
00203             mimetype != mimetypes->end(); ++mimetype)
00204         {
00205             QString name = QString::fromLocal8Bit((*mimetype).c_str()).stripWhiteSpace();
00206             if(KMimeType::mimeType(name))
00207                 results.append(name);
00208         }
00209 
00210         delete mimetypes;
00211     }
00212     delete offers;
00213 
00214     // clean out duplicates
00215     results.sort();
00216     for(QStringList::iterator result = results.begin(); result != results.end(); )
00217     {
00218         QStringList::iterator previous = result;
00219         ++result;
00220         if(result != results.end() && *result == *previous)
00221         {
00222             results.remove(result);
00223             result = previous;
00224         }
00225     }
00226 
00227     return results;
00228 }
00229 
00230 /* ### KDE4
00231 void KDE::PlayObjectFactory::connectAmanPlay()
00232 {
00233     kdDebug( 400 ) << k_funcinfo << endl;
00234     if( d->playObj->object().isNull() )
00235         return;
00236 
00237     d->amanPlay->start();
00238     d->playObj->object()._node()->start();
00239     Arts::connect( d->playObj->object(), "left" , d->amanPlay->amanPlay(), "left" );
00240     Arts::connect( d->playObj->object(), "right", d->amanPlay->amanPlay(), "right" );
00241 }
00242 */
00243 
00244 void KDE::POFHelper::connectAmanPlay()
00245 {
00246     kdDebug( 400 ) << k_funcinfo << endl;
00247     if( po->object().isNull() )
00248         return;
00249 
00250     ap->start();
00251     po->object()._node()->start();
00252     Arts::connect( po->object(), "left" , ap->amanPlay(), "left" );
00253     Arts::connect( po->object(), "right", ap->amanPlay(), "right" );
00254 }
00255 
00256 #include "kplayobjectfactory_p.moc"
00257 
00258 // vim: sw=4 ts=4 noet
KDE Home | KDE Accessibility Home | Description of Access Keys