# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# kfilemetapreview.cpp

    00001 /*
    00002  * This file is part of the KDE project.
    00003  * Copyright (C) 2003 Carsten Pfeiffer <pfeiffer@kde.org>
    00004  *
    00005  * You can Freely distribute this program under the GNU Library General Public
    00006  * License. See the file "COPYING" for the exact licensing terms.
    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 //     m_previewProviders.setAutoDelete( true );
    00030     initPreviewProviders();
    00031 }
    00032
    00033 KFileMetaPreview::~KFileMetaPreview()
    00034 {
    00035 }
    00036
    00037 void KFileMetaPreview::initPreviewProviders()
    00038 {
    00039     m_previewProviders.clear();
    00040     // hardcoded so far
    00041
    00042     // image previews
    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 //         qDebug(".... %s", (*it).latin1());
    00053         m_previewProviders.insert( *it, imagePreview );
    00054     }
    00055 }
    00056
    00057 KPreviewWidgetBase * KFileMetaPreview::previewProviderFor( const QString& mimeType )
    00058 {
    00059 //     qDebug("### looking for: %s", mimeType.latin1());
    00060     // often the first highlighted item, where we can be sure, there is no plugin
    00061     // (this "folders reflect icons" is a konq-specific thing, right?)
    00062     if ( mimeType == "inode/directory" )
    00063         return 0L;
    00064
    00065     KPreviewWidgetBase *provider = m_previewProviders.find( mimeType );
    00066     if ( provider )
    00067         return provider;
    00068
    00069 //qDebug("#### didn't find anything for: %s", mimeType.latin1());
    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     // with the new mimetypes from the audio-preview, try again
    00090     provider = m_previewProviders.find( mimeType );
    00091     if ( provider )
    00092         return provider;
    00093
    00094     // ### mimetype may be image/* for example, try that
    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     return 0L;
    00104 }
    00105
    00106 void KFileMetaPreview::showPreview(const KURL &url)
    00107 {
    00108     KMimeType::Ptr mt = KMimeType::findByURL( url );
    00109     KPreviewWidgetBase *provider = previewProviderFor( mt->name() );
    00110     if ( provider )
    00111     {
    00112         if ( provider != m_stack->visibleWidget() ) // stop the previous preview
    00113             clearPreview();
    00114
    00115         m_stack->setEnabled( true );
    00116         m_stack->raiseWidget( provider );
    00117         provider->showPreview( url );
    00118     }
    00119     else
    00120     {
    00121         clearPreview();
    00122         m_stack->setEnabled( false );
    00123     }
    00124 }
    00125
    00126 void KFileMetaPreview::clearPreview()
    00127 {
    00128     if ( m_stack->visibleWidget() )
    00129         static_cast<KPreviewWidgetBase*>( m_stack->visibleWidget() )->clearPreview();
    00130 }
    00131
    00132 void KFileMetaPreview::addPreviewProvider( const QString& mimeType,
    00133                                            KPreviewWidgetBase *provider )
    00134 {
    00135     m_previewProviders.insert( mimeType, provider );
    00136 }
    00137
    00138 void KFileMetaPreview::clearPreviewProviders()
    00139 {
    00140     QDictIterator<KPreviewWidgetBase> it( m_previewProviders );
    00141     for ( ; it.current(); ++it )
    00142         m_stack->removeWidget( it.current() );
    00143
    00144     m_previewProviders.clear();
    00145 }
    00146
    00147 // static
    00148 KPreviewWidgetBase * KFileMetaPreview::createAudioPreview( QWidget *parent )
    00149 {
    00150     KLibFactory *factory = KLibLoader::self()->factory( "kfileaudiopreview" );
    00151     if ( !factory )
    00152     {
    00153         s_tryAudioPreview = false;
    00154         return 0L;
    00155     }
    00156
    00157     return dynamic_cast<KPreviewWidgetBase*>( factory->create( parent, "kfileaudiopreview" ));
    00158 }
    00159
    00160 void KFileMetaPreview::virtual_hook( int, void* ) {}
    00161
    00162 #include "kfilemetapreview.moc"

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:23:58 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003