kfileaudiopreview.cpp

00001 #include "kfileaudiopreview.h"
00002 
00003 #include <qcheckbox.h>
00004 #include <qhbox.h>
00005 #include <qlayout.h>
00006 #include <qvgroupbox.h>
00007 
00008 #include <kglobal.h>
00009 #include <kconfig.h>
00010 #include <klibloader.h>
00011 #include <klocale.h>
00012 #include <kmediaplayer/player.h>
00013 #include <kmimetype.h>
00014 #include <kparts/componentfactory.h>
00015 
00016 #include <kplayobjectfactory.h>
00017 
00018 #include <config-kfile.h>
00019 
00020 class KFileAudioPreviewFactory : public KLibFactory
00021 {
00022 protected:
00023     virtual QObject *createObject( QObject *parent, const char *name,
00024                            const char *, const QStringList & )
00025     {
00026         return new KFileAudioPreview( dynamic_cast<QWidget*>( parent ), name );
00027     }
00028 };
00029 
00030 K_EXPORT_COMPONENT_FACTORY( kfileaudiopreview, KFileAudioPreviewFactory )
00031 
00032 
00033 
00034 
00035 
00036 
00037 class KFileAudioPreview::KFileAudioPreviewPrivate
00038 {
00039 public:
00040     KFileAudioPreviewPrivate( QWidget *parent )
00041     {
00042         player = KParts::ComponentFactory::createInstanceFromQuery<KMediaPlayer::Player>( "KMediaPlayer/Player", QString::null, parent );
00043     }
00044 
00045     ~KFileAudioPreviewPrivate()
00046     {
00047         delete player;
00048     }
00049 
00050     KMediaPlayer::Player *player;
00051 };
00052 
00053 
00054 KFileAudioPreview::KFileAudioPreview( QWidget *parent, const char *name )
00055     : KPreviewWidgetBase( parent, name )
00056 {
00057     KGlobal::locale()->insertCatalogue("kfileaudiopreview");    
00058 
00059     QStringList formats = KDE::PlayObjectFactory::mimeTypes();
00060     // ###
00061     QStringList::ConstIterator it = formats.begin();
00062     for ( ; it != formats.end(); ++it )
00063         m_supportedFormats.insert( *it, (void*) 1 );
00064 
00065     QVGroupBox *box = new QVGroupBox( i18n("Media Player"), this );
00066     QVBoxLayout *layout = new QVBoxLayout( this );
00067     layout->addWidget( box );
00068 
00069     (void) new QWidget( box ); // spacer
00070 
00071     d = new KFileAudioPreviewPrivate( 0L ); // not box -- being reparented anyway
00072     if ( d->player ) // only if there actually is a component...
00073     {
00074         setSupportedMimeTypes( formats );
00075         KMediaPlayer::View *view = d->player->view();
00076         view->setEnabled( false );
00077 
00078         // if we have access to the video widget, show it above the player
00079         // So, reparent first the video widget, then the view.
00080         if ( view->videoWidget() )
00081         {
00082             QHBox *frame = new QHBox( box );
00083             frame->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00084             frame->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00085             view->videoWidget()->reparent( frame, QPoint(0,0) );
00086         }
00087 
00088         view->reparent( box, QPoint(0,0) );
00089     }
00090 
00091     m_autoPlay = new QCheckBox( i18n("Play &automatically"), box );
00092     KConfigGroup config( KGlobal::config(), ConfigGroup );
00093     m_autoPlay->setChecked( config.readBoolEntry( "Autoplay sounds", true ) );
00094     connect( m_autoPlay, SIGNAL(toggled(bool)), SLOT(toggleAuto(bool)) );
00095 }
00096 
00097 KFileAudioPreview::~KFileAudioPreview()
00098 {
00099     KConfigGroup config( KGlobal::config(), ConfigGroup );
00100     config.writeEntry( "Autoplay sounds", m_autoPlay->isChecked() );
00101 
00102     delete d;
00103 }
00104 
00105 void KFileAudioPreview::showPreview( const KURL &url )
00106 {
00107     if ( !d->player || !url.isValid() )
00108         return;
00109 
00110     KMimeType::Ptr mt = KMimeType::findByURL( url );
00111     bool supported = m_supportedFormats.find( mt->name() );
00112     d->player->view()->setEnabled( supported );
00113     if ( !supported )
00114         return;
00115 
00116     static_cast<KParts::ReadOnlyPart*>(d->player)->openURL( url );
00117     if ( m_autoPlay->isChecked() )
00118         d->player->play();
00119 }
00120 
00121 void KFileAudioPreview::clearPreview()
00122 {
00123     if ( d->player )
00124     {
00125         d->player->stop();
00126         d->player->closeURL();
00127     }
00128 }
00129 
00130 void KFileAudioPreview::toggleAuto( bool on )
00131 {
00132     if ( !d->player )
00133         return;
00134 
00135     if ( on && m_currentURL.isValid() && d->player->view()->isEnabled() )
00136         d->player->play();
00137     else
00138         d->player->stop();
00139 }
00140 
00141 void KFileAudioPreview::virtual_hook( int, void* )
00142 {}
00143 
00144 #include "kfileaudiopreview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys