TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tdefileaudiopreview.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "tdefileaudiopreview.h"
  2. #include <tqcheckbox.h>
  3. #include <tqhbox.h>
  4. #include <tqlayout.h>
  5. #include <tqvgroupbox.h>
  6. #include <tdeglobal.h>
  7. #include <tdeconfig.h>
  8. #include <klibloader.h>
  9. #include <tdelocale.h>
  10. #include <tdemediaplayer/player.h>
  11. #include <kmimetype.h>
  12. #include <tdeparts/componentfactory.h>
  13. #include <kplayobjectfactory.h>
  14. #include <config-tdefile.h>
  15. class KFileAudioPreviewFactory : public KLibFactory
  16. {
  17. protected:
  18. virtual TQObject *createObject( TQObject *parent, const char *name,
  19. const char *className, const TQStringList & args)
  20. {
  21. Q_UNUSED(className);
  22. Q_UNUSED(args);
  23. return TQT_TQOBJECT(new KFileAudioPreview( dynamic_cast<TQWidget*>( parent ), name ));
  24. }
  25. };
  26. K_EXPORT_COMPONENT_FACTORY( tdefileaudiopreview, KFileAudioPreviewFactory )
  27. ///////////////////////////////////////////////////////////////////
  28. ///////////////////////////////////////////////////////////////////
  29. class KFileAudioPreview::KFileAudioPreviewPrivate
  30. {
  31. public:
  32. KFileAudioPreviewPrivate( TQWidget *parent )
  33. {
  34. player = KParts::ComponentFactory::createInstanceFromQuery<KMediaPlayer::Player>( "KMediaPlayer/Player", TQString(), TQT_TQOBJECT(parent) );
  35. }
  36. ~KFileAudioPreviewPrivate()
  37. {
  38. delete player;
  39. }
  40. KMediaPlayer::Player *player;
  41. };
  42. KFileAudioPreview::KFileAudioPreview( TQWidget *parent, const char *name )
  43. : KPreviewWidgetBase( parent, name )
  44. {
  45. TDEGlobal::locale()->insertCatalogue("tdefileaudiopreview");
  46. TQStringList formats = KDE::PlayObjectFactory::mimeTypes();
  47. // ###
  48. TQStringList::ConstIterator it = formats.begin();
  49. for ( ; it != formats.end(); ++it )
  50. m_supportedFormats.insert( *it, (void*) 1 );
  51. TQVGroupBox *box = new TQVGroupBox( i18n("Media Player"), this );
  52. TQVBoxLayout *layout = new TQVBoxLayout( this );
  53. layout->addWidget( box );
  54. (void) new TQWidget( box ); // spacer
  55. d = new KFileAudioPreviewPrivate( 0L ); // not box -- being reparented anyway
  56. if ( d->player ) // only if there actually is a component...
  57. {
  58. setSupportedMimeTypes( formats );
  59. KMediaPlayer::View *view = d->player->view();
  60. view->setEnabled( false );
  61. // if we have access to the video widget, show it above the player
  62. // So, reparent first the video widget, then the view.
  63. if ( view->videoWidget() )
  64. {
  65. TQHBox *frame = new TQHBox( box );
  66. frame->setFrameStyle( TQFrame::Panel | TQFrame::Sunken );
  67. frame->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ) );
  68. view->videoWidget()->reparent( frame, TQPoint(0,0) );
  69. }
  70. view->reparent( box, TQPoint(0,0) );
  71. }
  72. m_autoPlay = new TQCheckBox( i18n("Play &automatically"), box );
  73. TDEConfigGroup config( TDEGlobal::config(), ConfigGroup );
  74. m_autoPlay->setChecked( config.readBoolEntry( "Autoplay sounds", true ) );
  75. connect( m_autoPlay, TQT_SIGNAL(toggled(bool)), TQT_SLOT(toggleAuto(bool)) );
  76. }
  77. KFileAudioPreview::~KFileAudioPreview()
  78. {
  79. TDEConfigGroup config( TDEGlobal::config(), ConfigGroup );
  80. config.writeEntry( "Autoplay sounds", m_autoPlay->isChecked() );
  81. delete d;
  82. }
  83. void KFileAudioPreview::showPreview( const KURL &url )
  84. {
  85. if ( !d->player || !url.isValid() )
  86. return;
  87. KMimeType::Ptr mt = KMimeType::findByURL( url );
  88. bool supported = m_supportedFormats.find( mt->name() );
  89. d->player->view()->setEnabled( supported );
  90. if ( !supported )
  91. return;
  92. static_cast<KParts::ReadOnlyPart*>(d->player)->openURL( url );
  93. if ( m_autoPlay->isChecked() )
  94. d->player->play();
  95. }
  96. void KFileAudioPreview::clearPreview()
  97. {
  98. if ( d->player )
  99. {
  100. d->player->stop();
  101. d->player->closeURL();
  102. }
  103. }
  104. void KFileAudioPreview::toggleAuto( bool on )
  105. {
  106. if ( !d->player )
  107. return;
  108. if ( on && m_currentURL.isValid() && d->player->view()->isEnabled() )
  109. d->player->play();
  110. else
  111. d->player->stop();
  112. }
  113. void KFileAudioPreview::virtual_hook( int, void* )
  114. {}
  115. #include "tdefileaudiopreview.moc"