summaryrefslogtreecommitdiffstats
path: root/krename/fileplugin.cpp
blob: 4ed83b9a8857c473c6613dada2eecc9190d554b3 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/***************************************************************************
                          fileplugin.cpp  -  description
                             -------------------
    begin                : Mon Jul 1 2002
    copyright            : (C) 2002 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "fileplugin.h"

// TQt includes
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqregexp.h>

// KDE includes
#include <tdeapplication.h>
#include <tdefilemetainfo.h>
#include <klineedit.h>
#include <tdelistbox.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <kpushbutton.h>

FilePlugin::FilePlugin( KService* service )
{
    if(!service) {
        setupKeys();
        return;
    }
        
    KFileMetaInfoProvider* mip = KFileMetaInfoProvider::self();
    m_name = service->name();
    m_comment = service->comment();
    TQStringList options = service->serviceTypes();
    for( unsigned int i = 0; i < options.count(); i++ ) {
        if( options[i] != "KFilePlugin" ) {
            m_mimetype = options[i];
            const KFileMimeTypeInfo* info = mip->mimeTypeInfo( m_mimetype );
            if( info )               
                keys = info->supportedKeys();

            fileplugin = mip->plugin( m_mimetype );
           
            KMimeType::Ptr mime = KMimeType::mimeType( m_mimetype );
            m_icon = mime->icon( TQString(), true );  // arguments are unused
            
            setPattern( mime );
        }
    }
}

FilePlugin::~FilePlugin()
{ }

void FilePlugin::setPattern( KMimeType::Ptr mime )
{
    TQStringList pattern = mime->patterns();
    if( pattern.count() ) {
        m_pattern = pattern[0];
        if( m_pattern.startsWith( "*." ) )
            m_pattern = m_pattern.right( m_pattern.length() - 2 );
    }
    
    // TODO: REFACTOR
    // We need a pattern
    if( m_pattern.isEmpty() ) {
        int a = 0;
        a = m_name.find( "-" );
        if( a > -1 )
            m_pattern = m_name.left( a ).lower();
        else {
            a = m_pattern.find( " " );
            if( a > -1 )
                m_pattern = m_name.left( a ).lower();
            else 
                m_pattern = m_name;
        }
    }
    
    setupKeys();
}

void FilePlugin::setupKeys()
{
    for( unsigned int i = 0; i < keys.count(); i++ ) 
        keys[i] = getPattern() + keys[i];
}

const TQString FilePlugin::getName() const
{
    return m_name;
}

const TQString FilePlugin::getAccelName() const
{
    return "&" + getName();
}

const TQString FilePlugin::getPattern() const
{
    return m_pattern;
}

const int FilePlugin::type() const
{
    return TYPE_BRACKET;
}

bool FilePlugin::checkError()
{
    return true;
}

void FilePlugin::drawInterface( TQWidget* w, TQVBoxLayout* l )
{
    TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Expanding, TQSizePolicy::Expanding );
    
    TQHBoxLayout* hbox = new TQHBoxLayout( 0, 6, 6 );
    
    TQLabel* pix = new TQLabel( w );
    pix->setPixmap( kapp->iconLoader()->loadIcon( m_icon, TDEIcon::Desktop ) );
    
    hbox->addWidget( pix );
    hbox->addWidget( new TQLabel( "<qt><b>"+getName()+"</b></qt>", w ) );
    hbox->addItem( spacer );

    l->addLayout( hbox );    
    l->addWidget( new TQLabel( m_comment, w ) );
    l->addWidget( new TQLabel( i18n("Supported tokens:"), w ) );

    TDEListBox* list = new TDEListBox( w );
    list->setColumnMode( TDEListBox::FitToWidth );

    for( unsigned int i = 0; i < keys.count(); i++ )
        list->insertItem( "[" + keys[i] + "]" );
    
    l->addWidget( list );
    l->setStretchFactor( list, 2 );
}

TQString FilePlugin::processFile( BatchRenamer* b, int i, TQString token, int )
{
    TQString filename = BatchRenamer::buildFilename( &b->files()[i].src );
    
    token = token.lower();

    /*
     * Check if we have something cached for this file
     */
    if( cache.contains( filename + "::" + token ) )
        return cache[filename + "::" + token ];

    for( unsigned int i = 0; i < keys.count(); i++ ) {
        if( token.lower() == keys[i].lower() ) {
            KFileMetaInfo meta( filename );
            if( meta.isValid() ) {
                TQString k = keys[i];
                if( k.startsWith( getPattern() ) )
                    k = k.mid( getPattern().length(), k.length() - getPattern().length() );
                    
                TQString ret = meta.item( k ).string( true ).stripWhiteSpace();

                if( cache.count() >= CACHE_MAX )
                    cache.remove( cache.begin() );

                cache.insert( filename + "::" + token, ret );
                kapp->processEvents();
                return ret;                    
            }
        }
    }
    
    return TQString();
}

void FilePlugin::addHelp( HelpDialogData* data )
{
    TQStringList list;
    for( unsigned int i = 0; i < keys.count(); i++ ) 
        list.append( "[" + keys[i] + "]" + ";;" + keys[i] );
    
    data->add( getName(), &list, getIcon() );
}

const TQPixmap FilePlugin::getIcon() const 
{
    return kapp->iconLoader()->loadIcon( m_icon, TDEIcon::Small );
}

bool FilePlugin::supports( const TQString & token )
{
    for( unsigned int i = 0; i < keys.count(); i++ )
        if( TQRegExp( keys[i].lower() ).exactMatch( token.lower() ) )
            return true;
            
    return false;
}

void FilePlugin::clearCache()
{
    cache.clear();
}