summaryrefslogtreecommitdiffstats
path: root/kio/kfile/kfilemetapreview.cpp
blob: 155e1194c04339faa3201fb46f59fc100541fc6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * This file is part of the KDE project.
 * Copyright (C) 2003 Carsten Pfeiffer <[email protected]>
 *
 * You can Freely distribute this program under the GNU Library General Public
 * License. See the file "COPYING" for the exact licensing terms.
 */

#include "kfilemetapreview.h"

#include <tqlayout.h>

#include <kio/previewjob.h>
#include <klibloader.h>
#include <kimagefilepreview.h>
#include <kmimetype.h>

bool KFileMetaPreview::s_tryAudioPreview = true;

KFileMetaPreview::KFileMetaPreview( TQWidget *parent, const char *name )
    : KPreviewWidgetBase( parent, name ),
      haveAudioPreview( false )
{
    TQHBoxLayout *layout = new TQHBoxLayout( this, 0, 0 );
    m_stack = new TQWidgetStack( this );
    layout->addWidget( m_stack );

    // ###
//     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 );
    resize( imagePreview->sizeHint() );

    TQStringList mimeTypes = imagePreview->supportedMimeTypes();
    TQStringList::ConstIterator it = mimeTypes.begin();
    for ( ; it != mimeTypes.end(); ++it )
    {
//         tqDebug(".... %s", (*it).latin1());
        m_previewProviders.insert( *it, imagePreview );
    }
}

KPreviewWidgetBase * KFileMetaPreview::previewProviderFor( const TQString& mimeType )
{
//     tqDebug("### looking for: %s", mimeType.latin1());
    // 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;

    KPreviewWidgetBase *provider = m_previewProviders.find( mimeType );
    if ( provider )
        return provider;

//tqDebug("#### didn't find anything for: %s", mimeType.latin1());

    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 );
                TQStringList mimeTypes = audioPreview->supportedMimeTypes();
                TQStringList::ConstIterator it = mimeTypes.begin();
                for ( ; it != mimeTypes.end(); ++it )
                    m_previewProviders.insert( *it, audioPreview );
            }
        }
    }

    // with the new mimetypes from the audio-preview, try again
    provider = m_previewProviders.find( mimeType );
    if ( provider )
        return provider;

    // ### mimetype may be image/* for example, try that
    int index = mimeType.find( '/' );
    if ( index > 0 )
    {
        provider = m_previewProviders.find( mimeType.left( index + 1 ) + "*" );
        if ( provider )
            return provider;
    }

    KMimeType::Ptr mimeInfo = KMimeType::mimeType( mimeType );
    if ( mimeInfo )
    {
        // check mime type inheritance
        TQString parentMimeType = mimeInfo->parentMimeType();
        while ( !parentMimeType.isEmpty() )
        {
            provider = m_previewProviders.find( parentMimeType );
            if ( provider )
                return provider;

            KMimeType::Ptr parentMimeInfo = KMimeType::mimeType( parentMimeType );
            if ( !parentMimeInfo ) break;

            parentMimeType = parentMimeInfo->parentMimeType();
        }

        // check X-TDE-Text property
        TQVariant textProperty = mimeInfo->property( "X-TDE-text" );
        if ( textProperty.isValid() && textProperty.type() == TQVariant::Bool )
        {
            if ( textProperty.toBool() )
            {
                provider = m_previewProviders.find( "text/plain" );
                if ( provider )
                    return provider;

                provider = m_previewProviders.find( "text/*" );
                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();
}

void KFileMetaPreview::addPreviewProvider( const TQString& mimeType,
                                           KPreviewWidgetBase *provider )
{
    m_previewProviders.insert( mimeType, provider );
}

void KFileMetaPreview::clearPreviewProviders()
{
    TQDictIterator<KPreviewWidgetBase> it( m_previewProviders );
    for ( ; it.current(); ++it )
        m_stack->removeWidget( it.current() );

    m_previewProviders.clear();
}

// static
KPreviewWidgetBase * KFileMetaPreview::createAudioPreview( TQWidget *parent )
{
    KLibFactory *factory = KLibLoader::self()->factory( "kfileaudiopreview" );
    if ( !factory )
    {
        s_tryAudioPreview = false;
        return 0L;
    }

    return dynamic_cast<KPreviewWidgetBase*>( factory->create( TQT_TQOBJECT(parent), "kfileaudiopreview" ));
}

void KFileMetaPreview::virtual_hook( int, void* ) {}

#include "kfilemetapreview.moc"