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.
197 lines
5.7 KiB
197 lines
5.7 KiB
15 years ago
|
/*
|
||
|
* This file is part of the KDE project.
|
||
|
* Copyright (C) 2003 Carsten Pfeiffer <pfeiffer@kde.org>
|
||
|
*
|
||
|
* You can Freely distribute this program under the GNU Library General Public
|
||
|
* License. See the file "COPYING" for the exact licensing terms.
|
||
|
*/
|
||
|
|
||
12 years ago
|
#include "tdefilemetapreview.h"
|
||
15 years ago
|
|
||
13 years ago
|
#include <tqlayout.h>
|
||
15 years ago
|
|
||
12 years ago
|
#include <tdeio/previewjob.h>
|
||
15 years ago
|
#include <klibloader.h>
|
||
|
#include <kimagefilepreview.h>
|
||
|
#include <kmimetype.h>
|
||
|
|
||
|
bool KFileMetaPreview::s_tryAudioPreview = true;
|
||
|
|
||
15 years ago
|
KFileMetaPreview::KFileMetaPreview( TQWidget *parent, const char *name )
|
||
15 years ago
|
: KPreviewWidgetBase( parent, name ),
|
||
|
haveAudioPreview( false )
|
||
|
{
|
||
14 years ago
|
TQHBoxLayout *layout = new TQHBoxLayout( this, 0, 0 );
|
||
15 years ago
|
m_stack = new TQWidgetStack( this );
|
||
14 years ago
|
layout->addWidget( m_stack );
|
||
15 years ago
|
|
||
|
// ###
|
||
|
// m_previewProviders.setAutoDelete( true );
|
||
|
initPreviewProviders();
|
||
|
}
|
||
|
|
||
|
KFileMetaPreview::~KFileMetaPreview()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void KFileMetaPreview::initPreviewProviders()
|
||
|
{
|
||
|
m_previewProviders.clear();
|
||
|
// hardcoded so far
|
||
|
|
||
|
// image previews
|
||
|
KImageFilePreview *imagePreview = new KImageFilePreview( m_stack );
|
||
|
(void) m_stack->addWidget( imagePreview );
|
||
|
m_stack->raiseWidget( imagePreview );
|
||
13 years ago
|
resize( imagePreview->sizeHint() );
|
||
15 years ago
|
|
||
15 years ago
|
TQStringList mimeTypes = imagePreview->supportedMimeTypes();
|
||
|
TQStringList::ConstIterator it = mimeTypes.begin();
|
||
15 years ago
|
for ( ; it != mimeTypes.end(); ++it )
|
||
|
{
|
||
13 years ago
|
// tqDebug(".... %s", (*it).latin1());
|
||
15 years ago
|
m_previewProviders.insert( *it, imagePreview );
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
KPreviewWidgetBase * KFileMetaPreview::previewProviderFor( const TQString& mimeType )
|
||
15 years ago
|
{
|
||
13 years ago
|
// tqDebug("### looking for: %s", mimeType.latin1());
|
||
15 years ago
|
// often the first highlighted item, where we can be sure, there is no plugin
|
||
|
// (this "folders reflect icons" is a konq-specific thing, right?)
|
||
|
if ( mimeType == "inode/directory" )
|
||
|
return 0L;
|
||
|
|
||
14 years ago
|
KPreviewWidgetBase *provider = m_previewProviders.find( mimeType );
|
||
15 years ago
|
if ( provider )
|
||
|
return provider;
|
||
|
|
||
13 years ago
|
//tqDebug("#### didn't find anything for: %s", mimeType.latin1());
|
||
15 years ago
|
|
||
|
if ( s_tryAudioPreview &&
|
||
|
!mimeType.startsWith("text/") && !mimeType.startsWith("image/") )
|
||
|
{
|
||
|
if ( !haveAudioPreview )
|
||
|
{
|
||
|
KPreviewWidgetBase *audioPreview = createAudioPreview( m_stack );
|
||
|
if ( audioPreview )
|
||
|
{
|
||
|
haveAudioPreview = true;
|
||
|
(void) m_stack->addWidget( audioPreview );
|
||
15 years ago
|
TQStringList mimeTypes = audioPreview->supportedMimeTypes();
|
||
|
TQStringList::ConstIterator it = mimeTypes.begin();
|
||
15 years ago
|
for ( ; it != mimeTypes.end(); ++it )
|
||
|
m_previewProviders.insert( *it, audioPreview );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// with the new mimetypes from the audio-preview, try again
|
||
14 years ago
|
provider = m_previewProviders.find( mimeType );
|
||
15 years ago
|
if ( provider )
|
||
|
return provider;
|
||
|
|
||
|
// ### mimetype may be image/* for example, try that
|
||
14 years ago
|
int index = mimeType.find( '/' );
|
||
15 years ago
|
if ( index > 0 )
|
||
|
{
|
||
14 years ago
|
provider = m_previewProviders.find( mimeType.left( index + 1 ) + "*" );
|
||
15 years ago
|
if ( provider )
|
||
|
return provider;
|
||
|
}
|
||
|
|
||
|
KMimeType::Ptr mimeInfo = KMimeType::mimeType( mimeType );
|
||
|
if ( mimeInfo )
|
||
|
{
|
||
|
// check mime type inheritance
|
||
15 years ago
|
TQString parentMimeType = mimeInfo->parentMimeType();
|
||
15 years ago
|
while ( !parentMimeType.isEmpty() )
|
||
|
{
|
||
14 years ago
|
provider = m_previewProviders.find( parentMimeType );
|
||
15 years ago
|
if ( provider )
|
||
|
return provider;
|
||
|
|
||
|
KMimeType::Ptr parentMimeInfo = KMimeType::mimeType( parentMimeType );
|
||
|
if ( !parentMimeInfo ) break;
|
||
|
|
||
|
parentMimeType = parentMimeInfo->parentMimeType();
|
||
|
}
|
||
|
|
||
13 years ago
|
// check X-TDE-Text property
|
||
|
TQVariant textProperty = mimeInfo->property( "X-TDE-text" );
|
||
15 years ago
|
if ( textProperty.isValid() && textProperty.type() == TQVariant::Bool )
|
||
15 years ago
|
{
|
||
|
if ( textProperty.toBool() )
|
||
|
{
|
||
14 years ago
|
provider = m_previewProviders.find( "text/plain" );
|
||
15 years ago
|
if ( provider )
|
||
|
return provider;
|
||
|
|
||
14 years ago
|
provider = m_previewProviders.find( "text/*" );
|
||
15 years ago
|
if ( provider )
|
||
|
return provider;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return 0L;
|
||
|
}
|
||
|
|
||
|
void KFileMetaPreview::showPreview(const KURL &url)
|
||
|
{
|
||
|
KMimeType::Ptr mt = KMimeType::findByURL( url );
|
||
|
KPreviewWidgetBase *provider = previewProviderFor( mt->name() );
|
||
|
if ( provider )
|
||
|
{
|
||
|
if ( provider != m_stack->visibleWidget() ) // stop the previous preview
|
||
|
clearPreview();
|
||
|
|
||
|
m_stack->setEnabled( true );
|
||
|
m_stack->raiseWidget( provider );
|
||
|
provider->showPreview( url );
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
clearPreview();
|
||
|
m_stack->setEnabled( false );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KFileMetaPreview::clearPreview()
|
||
|
{
|
||
|
if ( m_stack->visibleWidget() )
|
||
|
static_cast<KPreviewWidgetBase*>( m_stack->visibleWidget() )->clearPreview();
|
||
|
}
|
||
|
|
||
15 years ago
|
void KFileMetaPreview::addPreviewProvider( const TQString& mimeType,
|
||
15 years ago
|
KPreviewWidgetBase *provider )
|
||
|
{
|
||
|
m_previewProviders.insert( mimeType, provider );
|
||
|
}
|
||
|
|
||
|
void KFileMetaPreview::clearPreviewProviders()
|
||
|
{
|
||
15 years ago
|
TQDictIterator<KPreviewWidgetBase> it( m_previewProviders );
|
||
15 years ago
|
for ( ; it.current(); ++it )
|
||
|
m_stack->removeWidget( it.current() );
|
||
|
|
||
|
m_previewProviders.clear();
|
||
|
}
|
||
|
|
||
|
// static
|
||
15 years ago
|
KPreviewWidgetBase * KFileMetaPreview::createAudioPreview( TQWidget *parent )
|
||
15 years ago
|
{
|
||
12 years ago
|
KLibFactory *factory = KLibLoader::self()->factory( "tdefileaudiopreview" );
|
||
15 years ago
|
if ( !factory )
|
||
|
{
|
||
|
s_tryAudioPreview = false;
|
||
|
return 0L;
|
||
|
}
|
||
|
|
||
12 years ago
|
return dynamic_cast<KPreviewWidgetBase*>( factory->create( TQT_TQOBJECT(parent), "tdefileaudiopreview" ));
|
||
15 years ago
|
}
|
||
|
|
||
|
void KFileMetaPreview::virtual_hook( int, void* ) {}
|
||
|
|
||
12 years ago
|
#include "tdefilemetapreview.moc"
|