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.
krename/krename/fileplugin.cpp

216 lines
6.0 KiB

/***************************************************************************
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();
}
#include "fileplugin.moc"