kfileaudiopreview.cpp
00001 #include "kfileaudiopreview.h" 00002 00003 #include <tqcheckbox.h> 00004 #include <tqhbox.h> 00005 #include <tqlayout.h> 00006 #include <tqvgroupbox.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 TQObject *createObject( TQObject *parent, const char *name, 00024 const char *className, const TQStringList & args) 00025 { 00026 Q_UNUSED(className); 00027 Q_UNUSED(args); 00028 return TQT_TQOBJECT(new KFileAudioPreview( dynamic_cast<TQWidget*>( 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( TQWidget *parent ) 00043 { 00044 player = KParts::ComponentFactory::createInstanceFromQuery<KMediaPlayer::Player>( "KMediaPlayer/Player", TQString(), TQT_TQOBJECT(parent) ); 00045 } 00046 00047 ~KFileAudioPreviewPrivate() 00048 { 00049 delete player; 00050 } 00051 00052 KMediaPlayer::Player *player; 00053 }; 00054 00055 00056 KFileAudioPreview::KFileAudioPreview( TQWidget *parent, const char *name ) 00057 : KPreviewWidgetBase( parent, name ) 00058 { 00059 KGlobal::locale()->insertCatalogue("kfileaudiopreview"); 00060 00061 TQStringList formats = KDE::PlayObjectFactory::mimeTypes(); 00062 // ### 00063 TQStringList::ConstIterator it = formats.begin(); 00064 for ( ; it != formats.end(); ++it ) 00065 m_supportedFormats.insert( *it, (void*) 1 ); 00066 00067 TQVGroupBox *box = new TQVGroupBox( i18n("Media Player"), this ); 00068 TQVBoxLayout *layout = new TQVBoxLayout( this ); 00069 layout->addWidget( box ); 00070 00071 (void) new TQWidget( box ); // spacer 00072 00073 d = new KFileAudioPreviewPrivate( 0L ); // not box -- being reparented anyway 00074 if ( d->player ) // only if there actually is a component... 00075 { 00076 setSupportedMimeTypes( formats ); 00077 KMediaPlayer::View *view = d->player->view(); 00078 view->setEnabled( false ); 00079 00080 // if we have access to the video widget, show it above the player 00081 // So, reparent first the video widget, then the view. 00082 if ( view->videoWidget() ) 00083 { 00084 TQHBox *frame = new TQHBox( box ); 00085 frame->setFrameStyle( TQFrame::Panel | TQFrame::Sunken ); 00086 frame->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ) ); 00087 view->videoWidget()->reparent( frame, TQPoint(0,0) ); 00088 } 00089 00090 view->reparent( box, TQPoint(0,0) ); 00091 } 00092 00093 m_autoPlay = new TQCheckBox( i18n("Play &automatically"), box ); 00094 KConfigGroup config( KGlobal::config(), ConfigGroup ); 00095 m_autoPlay->setChecked( config.readBoolEntry( "Autoplay sounds", true ) ); 00096 connect( m_autoPlay, TQT_SIGNAL(toggled(bool)), TQT_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"