konq_sound.cc

00001 /* This file is part of the KDE Project
00002    Copyright (c) 2001 Malte Starostik <malte@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., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include <kartsdispatcher.h>
00020 #include <kdebug.h>
00021 #include <kplayobjectfactory.h>
00022 #include <soundserver.h>
00023 
00024 #include "konq_sound.h"
00025 
00026 using namespace std;
00027 
00028 class KonqSoundPlayerImpl : public KonqSoundPlayer
00029 {
00030 public:
00031     KonqSoundPlayerImpl();
00032     virtual ~KonqSoundPlayerImpl();
00033 
00034     virtual const QStringList &mimeTypes();
00035     virtual void play(const QString &fileName);
00036     virtual void stop();
00037     virtual bool isPlaying();
00038 
00039 private:
00040     QStringList m_mimeTypes;
00041 
00042     KArtsDispatcher     m_dispatcher;
00043     Arts::SoundServerV2 m_soundServer;
00044     KDE::PlayObjectFactory *m_factory;
00045     KDE::PlayObject        *m_player;
00046 };
00047 
00048 KonqSoundPlayerImpl::KonqSoundPlayerImpl()
00049     : m_player(0)
00050 {
00051     m_soundServer = Arts::Reference("global:Arts_SoundServerV2");
00052     m_factory = new KDE::PlayObjectFactory(m_soundServer);
00053 }
00054 
00055 KonqSoundPlayerImpl::~KonqSoundPlayerImpl()
00056 {
00057     delete m_player;
00058     delete m_factory;
00059 }
00060 
00061 const QStringList &KonqSoundPlayerImpl::mimeTypes()
00062 {
00063     if (m_mimeTypes.isEmpty())
00064     {
00065         Arts::TraderQuery query;
00066         vector<Arts::TraderOffer> *offers = query.query();
00067 
00068         for (vector<Arts::TraderOffer>::iterator it = offers->begin();
00069             it != offers->end(); ++it)
00070         {
00071             vector<string> *prop = (*it).getProperty("MimeType");
00072             for (vector<string>::iterator mt = prop->begin();
00073                 mt != prop->end(); ++mt)
00074                 if ((*mt).length()) // && (*mt).find("video/") == string::npos)
00075                     m_mimeTypes << (*mt).c_str();
00076             delete prop;
00077         }
00078         delete offers;
00079     }
00080     return m_mimeTypes;
00081 }
00082 
00083 void KonqSoundPlayerImpl::play(const QString &fileName)
00084 {
00085     if (m_soundServer.isNull())
00086         return;
00087 
00088     delete m_player;
00089     if ((m_player = m_factory->createPlayObject(fileName, true)))
00090     {
00091         if (m_player->isNull())
00092             stop();
00093         else
00094             m_player->play();
00095     }
00096 }
00097 
00098 void KonqSoundPlayerImpl::stop()
00099 {
00100     delete m_player;
00101     m_player = 0;
00102 }
00103 
00104 bool KonqSoundPlayerImpl::isPlaying()
00105 {
00106     return m_player ? (m_player->state() == Arts::posPlaying) : false;
00107 }
00108 
00109 class KonqSoundFactory : public KLibFactory
00110 {
00111 public:
00112     KonqSoundFactory(QObject *parent = 0, const char *name = 0)
00113         : KLibFactory(parent, name) {};
00114     virtual ~KonqSoundFactory() {};
00115 
00116 protected:
00117     virtual QObject *createObject(QObject * = 0, const char * = 0,
00118         const char *className = "QObject", const QStringList &args = QStringList());
00119 };
00120 
00121 QObject *KonqSoundFactory::createObject(QObject *, const char *,
00122     const char *className, const QStringList &)
00123 {
00124     if (qstrcmp(className, "KonqSoundPlayer") == 0)
00125         return new KonqSoundPlayerImpl();
00126     return 0;
00127 }
00128 
00129 extern "C"
00130 {
00131     KDE_EXPORT KLibFactory *init_konq_sound()
00132     {
00133         return new KonqSoundFactory();
00134     }
00135 }
00136 
00137 // vim: ts=4 sw=4 noet
KDE Home | KDE Accessibility Home | Description of Access Keys