kio
kfilemetapreview.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "kfilemetapreview.h"
00010
00011 #include <qlayout.h>
00012
00013 #include <kio/previewjob.h>
00014 #include <klibloader.h>
00015 #include <kimagefilepreview.h>
00016 #include <kmimetype.h>
00017
00018 bool KFileMetaPreview::s_tryAudioPreview = true;
00019
00020 KFileMetaPreview::KFileMetaPreview( QWidget *parent, const char *name )
00021 : KPreviewWidgetBase( parent, name ),
00022 haveAudioPreview( false )
00023 {
00024 QHBoxLayout *layout = new QHBoxLayout( this, 0, 0 );
00025 m_stack = new QWidgetStack( this );
00026 layout->addWidget( m_stack );
00027
00028
00029
00030 initPreviewProviders();
00031 }
00032
00033 KFileMetaPreview::~KFileMetaPreview()
00034 {
00035 }
00036
00037 void KFileMetaPreview::initPreviewProviders()
00038 {
00039 m_previewProviders.clear();
00040
00041
00042
00043 KImageFilePreview *imagePreview = new KImageFilePreview( m_stack );
00044 (void) m_stack->addWidget( imagePreview );
00045 m_stack->raiseWidget( imagePreview );
00046 resize( imagePreview->sizeHint() );
00047
00048 QStringList mimeTypes = imagePreview->supportedMimeTypes();
00049 QStringList::ConstIterator it = mimeTypes.begin();
00050 for ( ; it != mimeTypes.end(); ++it )
00051 {
00052
00053 m_previewProviders.insert( *it, imagePreview );
00054 }
00055 }
00056
00057 KPreviewWidgetBase * KFileMetaPreview::previewProviderFor( const QString& mimeType )
00058 {
00059
00060
00061
00062 if ( mimeType == "inode/directory" )
00063 return 0L;
00064
00065 KPreviewWidgetBase *provider = m_previewProviders.find( mimeType );
00066 if ( provider )
00067 return provider;
00068
00069
00070
00071 if ( s_tryAudioPreview &&
00072 !mimeType.startsWith("text/") && !mimeType.startsWith("image/") )
00073 {
00074 if ( !haveAudioPreview )
00075 {
00076 KPreviewWidgetBase *audioPreview = createAudioPreview( m_stack );
00077 if ( audioPreview )
00078 {
00079 haveAudioPreview = true;
00080 (void) m_stack->addWidget( audioPreview );
00081 QStringList mimeTypes = audioPreview->supportedMimeTypes();
00082 QStringList::ConstIterator it = mimeTypes.begin();
00083 for ( ; it != mimeTypes.end(); ++it )
00084 m_previewProviders.insert( *it, audioPreview );
00085 }
00086 }
00087 }
00088
00089
00090 provider = m_previewProviders.find( mimeType );
00091 if ( provider )
00092 return provider;
00093
00094
00095 int index = mimeType.find( '/' );
00096 if ( index > 0 )
00097 {
00098 provider = m_previewProviders.find( mimeType.left( index + 1 ) + "*" );
00099 if ( provider )
00100 return provider;
00101 }
00102
00103 KMimeType::Ptr mimeInfo = KMimeType::mimeType( mimeType );
00104 if ( mimeInfo )
00105 {
00106
00107 QString parentMimeType = mimeInfo->parentMimeType();
00108 while ( !parentMimeType.isEmpty() )
00109 {
00110 provider = m_previewProviders.find( parentMimeType );
00111 if ( provider )
00112 return provider;
00113
00114 KMimeType::Ptr parentMimeInfo = KMimeType::mimeType( parentMimeType );
00115 if ( !parentMimeInfo ) break;
00116
00117 parentMimeType = parentMimeInfo->parentMimeType();
00118 }
00119
00120
00121 QVariant textProperty = mimeInfo->property( "X-KDE-text" );
00122 if ( textProperty.isValid() && textProperty.type() == QVariant::Bool )
00123 {
00124 if ( textProperty.toBool() )
00125 {
00126 provider = m_previewProviders.find( "text/plain" );
00127 if ( provider )
00128 return provider;
00129
00130 provider = m_previewProviders.find( "text/*" );
00131 if ( provider )
00132 return provider;
00133 }
00134 }
00135 }
00136
00137 return 0L;
00138 }
00139
00140 void KFileMetaPreview::showPreview(const KURL &url)
00141 {
00142 KMimeType::Ptr mt = KMimeType::findByURL( url );
00143 KPreviewWidgetBase *provider = previewProviderFor( mt->name() );
00144 if ( provider )
00145 {
00146 if ( provider != m_stack->visibleWidget() )
00147 clearPreview();
00148
00149 m_stack->setEnabled( true );
00150 m_stack->raiseWidget( provider );
00151 provider->showPreview( url );
00152 }
00153 else
00154 {
00155 clearPreview();
00156 m_stack->setEnabled( false );
00157 }
00158 }
00159
00160 void KFileMetaPreview::clearPreview()
00161 {
00162 if ( m_stack->visibleWidget() )
00163 static_cast<KPreviewWidgetBase*>( m_stack->visibleWidget() )->clearPreview();
00164 }
00165
00166 void KFileMetaPreview::addPreviewProvider( const QString& mimeType,
00167 KPreviewWidgetBase *provider )
00168 {
00169 m_previewProviders.insert( mimeType, provider );
00170 }
00171
00172 void KFileMetaPreview::clearPreviewProviders()
00173 {
00174 QDictIterator<KPreviewWidgetBase> it( m_previewProviders );
00175 for ( ; it.current(); ++it )
00176 m_stack->removeWidget( it.current() );
00177
00178 m_previewProviders.clear();
00179 }
00180
00181
00182 KPreviewWidgetBase * KFileMetaPreview::createAudioPreview( QWidget *parent )
00183 {
00184 KLibFactory *factory = KLibLoader::self()->factory( "kfileaudiopreview" );
00185 if ( !factory )
00186 {
00187 s_tryAudioPreview = false;
00188 return 0L;
00189 }
00190
00191 return dynamic_cast<KPreviewWidgetBase*>( factory->create( parent, "kfileaudiopreview" ));
00192 }
00193
00194 void KFileMetaPreview::virtual_hook( int, void* ) {}
00195
00196 #include "kfilemetapreview.moc"