interfaces
kfileaudiopreview.cpp
Go to the documentation of this file.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 *className, const QStringList & args)
00025 {
00026 Q_UNUSED(className);
00027 Q_UNUSED(args);
00028 return new KFileAudioPreview( dynamic_cast<QWidget*>( parent ), name );
00029 }
00030 };
00031
00032 K_EXPORT_COMPONENT_FACTORY( kfileaudiopreview, KFileAudioPreviewFactory )
00033
00034
00035
00036
00037
00038
00039 class KFileAudioPreview::KFileAudioPreviewPrivate
00040 {
00041 public:
00042 KFileAudioPreviewPrivate( QWidget *parent )
00043 {
00044 player = KParts::ComponentFactory::createInstanceFromQuery<KMediaPlayer::Player>( "KMediaPlayer/Player", QString::null, parent );
00045 }
00046
00047 ~KFileAudioPreviewPrivate()
00048 {
00049 delete player;
00050 }
00051
00052 KMediaPlayer::Player *player;
00053 };
00054
00055
00056 KFileAudioPreview::KFileAudioPreview( QWidget *parent, const char *name )
00057 : KPreviewWidgetBase( parent, name )
00058 {
00059 KGlobal::locale()->insertCatalogue("kfileaudiopreview");
00060
00061 QStringList formats = KDE::PlayObjectFactory::mimeTypes();
00062
00063 QStringList::ConstIterator it = formats.begin();
00064 for ( ; it != formats.end(); ++it )
00065 m_supportedFormats.insert( *it, (void*) 1 );
00066
00067 QVGroupBox *box = new QVGroupBox( i18n("Media Player"), this );
00068 QVBoxLayout *layout = new QVBoxLayout( this );
00069 layout->addWidget( box );
00070
00071 (void) new QWidget( box );
00072
00073 d = new KFileAudioPreviewPrivate( 0L );
00074 if ( d->player )
00075 {
00076 setSupportedMimeTypes( formats );
00077 KMediaPlayer::View *view = d->player->view();
00078 view->setEnabled( false );
00079
00080
00081
00082 if ( view->videoWidget() )
00083 {
00084 QHBox *frame = new QHBox( box );
00085 frame->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00086 frame->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00087 view->videoWidget()->reparent( frame, QPoint(0,0) );
00088 }
00089
00090 view->reparent( box, QPoint(0,0) );
00091 }
00092
00093 m_autoPlay = new QCheckBox( i18n("Play &automatically"), box );
00094 KConfigGroup config( KGlobal::config(), ConfigGroup );
00095 m_autoPlay->setChecked( config.readBoolEntry( "Autoplay sounds", true ) );
00096 connect( m_autoPlay, SIGNAL(toggled(bool)), SLOT(toggleAuto(bool)) );
00097 }
00098
00099 KFileAudioPreview::~KFileAudioPreview()
00100 {
00101 KConfigGroup config( KGlobal::config(), ConfigGroup );
00102 config.writeEntry( "Autoplay sounds", m_autoPlay->isChecked() );
00103
00104 delete d;
00105 }
00106
00107 void KFileAudioPreview::showPreview( const KURL &url )
00108 {
00109 if ( !d->player || !url.isValid() )
00110 return;
00111
00112 KMimeType::Ptr mt = KMimeType::findByURL( url );
00113 bool supported = m_supportedFormats.find( mt->name() );
00114 d->player->view()->setEnabled( supported );
00115 if ( !supported )
00116 return;
00117
00118 static_cast<KParts::ReadOnlyPart*>(d->player)->openURL( url );
00119 if ( m_autoPlay->isChecked() )
00120 d->player->play();
00121 }
00122
00123 void KFileAudioPreview::clearPreview()
00124 {
00125 if ( d->player )
00126 {
00127 d->player->stop();
00128 d->player->closeURL();
00129 }
00130 }
00131
00132 void KFileAudioPreview::toggleAuto( bool on )
00133 {
00134 if ( !d->player )
00135 return;
00136
00137 if ( on && m_currentURL.isValid() && d->player->view()->isEnabled() )
00138 d->player->play();
00139 else
00140 d->player->stop();
00141 }
00142
00143 void KFileAudioPreview::virtual_hook( int, void* )
00144 {}
00145
00146 #include "kfileaudiopreview.moc"