kio Library API Documentation

kurifilter.cpp

00001 /* This file is part of the KDE libraries
00002  *  Copyright (C) 2000 Yves Arrouye <yves@realnames.com>
00003  *  Copyright (C) 2000 Dawit Alemayehu <adawit at kde.org>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  *  Boston, MA 02111-1307, USA.
00019  **/
00020 
00021 #include <config.h>
00022 
00023 #include <kdebug.h>
00024 #include <ktrader.h>
00025 #include <kmimetype.h>
00026 #include <klibloader.h>
00027 #include <kstaticdeleter.h>
00028 #include <kparts/componentfactory.h>
00029 
00030 #include "kurifilter.h"
00031 
00032 template class QPtrList<KURIFilterPlugin>;
00033 
00034 KURIFilterPlugin::KURIFilterPlugin( QObject *parent, const char *name, double pri )
00035                  :QObject( parent, name )
00036 {
00037     m_strName = QString::fromLatin1( name );
00038     m_dblPriority = pri;
00039 }
00040 
00041 void KURIFilterPlugin::setFilteredURI( KURIFilterData& data, const KURL& uri ) const
00042 {
00043     if ( data.uri() != uri )
00044     {
00045         data.m_pURI = uri;
00046         data.m_bChanged = true;
00047     }
00048 }
00049 
00050 class KURIFilterDataPrivate
00051 {
00052 public:
00053     KURIFilterDataPrivate() {};
00054     QString abs_path;
00055     QString args;
00056     QString typedString;
00057 };
00058 
00059 KURIFilterData::KURIFilterData( const KURIFilterData& data )
00060 {
00061     m_iType = data.m_iType;
00062     m_pURI = data.m_pURI;
00063     m_strErrMsg = data.m_strErrMsg;
00064     m_strIconName = data.m_strIconName;
00065     m_bChanged = data.m_bChanged;
00066     m_bCheckForExecutables = data.m_bCheckForExecutables;
00067     d = new KURIFilterDataPrivate;
00068     d->abs_path = data.absolutePath();
00069     d->typedString = data.typedString();
00070     d->args = data.argsAndOptions();
00071 }
00072 
00073 KURIFilterData::~KURIFilterData()
00074 {
00075     delete d;
00076     d = 0;
00077 }
00078 
00079 void KURIFilterData::init( const KURL& url )
00080 {
00081     m_iType = KURIFilterData::UNKNOWN;
00082     m_pURI = url;
00083     m_strErrMsg = QString::null;
00084     m_strIconName = QString::null;
00085     m_bCheckForExecutables = true;
00086     m_bChanged = true;
00087     d = new KURIFilterDataPrivate;
00088     d->typedString = url.url();
00089 }
00090 
00091 void KURIFilterData::init( const QString& url )
00092 {
00093     m_iType = KURIFilterData::UNKNOWN;
00094     m_pURI = url;
00095     m_strErrMsg = QString::null;
00096     m_strIconName = QString::null;
00097     m_bCheckForExecutables = true;
00098     m_bChanged = true;
00099     d = new KURIFilterDataPrivate;
00100     d->typedString = url;
00101 }
00102 
00103 QString KURIFilterData::typedString() const
00104 {
00105     return d->typedString;
00106 }
00107 
00108 void KURIFilterData::setCheckForExecutables( bool check )
00109 {
00110     m_bCheckForExecutables = check;
00111 }
00112 
00113 bool KURIFilterData::hasArgsAndOptions() const
00114 {
00115     return !d->args.isEmpty();
00116 }
00117 
00118 bool KURIFilterData::hasAbsolutePath() const
00119 {
00120     return !d->abs_path.isEmpty();
00121 }
00122 
00123 bool KURIFilterData::setAbsolutePath( const QString& absPath )
00124 {
00125     // Since a malformed URL could possibly be a relative
00126     // URL we tag it as a possible local resource...
00127     if( (!m_pURI.isValid() || m_pURI.isLocalFile()) )
00128     {
00129         d->abs_path = absPath;
00130         return true;
00131     }
00132     return false;
00133 }
00134 
00135 QString KURIFilterData::absolutePath() const
00136 {
00137     return d->abs_path;
00138 }
00139 
00140 QString KURIFilterData::argsAndOptions() const
00141 {
00142     return d->args;
00143 }
00144 
00145 QString KURIFilterData::iconName()
00146 {
00147     if( m_bChanged )
00148     {
00149         switch ( m_iType )
00150         {
00151             case KURIFilterData::LOCAL_FILE:
00152             case KURIFilterData::LOCAL_DIR:
00153             case KURIFilterData::NET_PROTOCOL:
00154             {
00155                 m_strIconName = KMimeType::iconForURL( m_pURI );
00156                 break;
00157             }
00158             case KURIFilterData::EXECUTABLE:
00159             {
00160                 QString tmp = m_pURI.url();
00161                 tmp = tmp.mid( tmp.findRev( '/' ) + 1 ); // strip path if given
00162                 KService::Ptr service = KService::serviceByDesktopName( tmp );
00163                 if (service)
00164                     m_strIconName = service->icon();
00165                 else
00166                     m_strIconName = QString::fromLatin1("exec");
00167                 break;
00168             }
00169             case KURIFilterData::HELP:
00170             {
00171                 m_strIconName = QString::fromLatin1("khelpcenter");
00172                 break;
00173             }
00174             case KURIFilterData::SHELL:
00175             {
00176                 m_strIconName = QString::fromLatin1("konsole");
00177                 break;
00178             }
00179             case KURIFilterData::ERROR:
00180             case KURIFilterData::BLOCKED:
00181             {
00182                 m_strIconName = QString::fromLatin1("error");
00183                 break;
00184             }
00185             default:
00186                 m_strIconName = QString::null;
00187                 break;
00188         }
00189         m_bChanged = false;
00190     }
00191     return m_strIconName;
00192 }
00193 
00194 //********************************************  KURIFilterPlugin **********************************************
00195 void KURIFilterPlugin::setArguments( KURIFilterData& data, const QString& args ) const
00196 {
00197     data.d->args = args;
00198 }
00199 
00200 //********************************************  KURIFilter **********************************************
00201 KURIFilter *KURIFilter::m_self;
00202 static KStaticDeleter<KURIFilter> kurifiltersd;
00203 
00204 KURIFilter *KURIFilter::self()
00205 {
00206     if (!m_self)
00207         m_self = kurifiltersd.setObject(m_self, new KURIFilter);
00208     return m_self;
00209 }
00210 
00211 KURIFilter::KURIFilter()
00212 {
00213     m_lstPlugins.setAutoDelete(true);
00214     loadPlugins();
00215 }
00216 
00217 KURIFilter::~KURIFilter()
00218 {
00219     m_self = 0;
00220 }
00221 
00222 bool KURIFilter::filterURI( KURIFilterData& data, const QStringList& filters )
00223 {
00224     bool filtered = false;
00225     KURIFilterPluginList use_plugins;
00226 
00227     // If we have a filter list, only include the once
00228     // explicitly specified by it. Otherwise, use all available filters...
00229     if( filters.isEmpty() )
00230         use_plugins = m_lstPlugins;  // Use everything that is loaded...
00231     else
00232     {
00233         //kdDebug() << "Named plugins requested..."  << endl;
00234         for( QStringList::ConstIterator lst = filters.begin(); lst != filters.end(); ++lst )
00235         {
00236             QPtrListIterator<KURIFilterPlugin> it( m_lstPlugins );
00237             for( ; it.current() ; ++it )
00238             {
00239                 if( (*lst) == it.current()->name() )
00240                 {
00241                     //kdDebug() << "Will use filter plugin named: " << it.current()->name() << endl;
00242                     use_plugins.append( it.current() );
00243                     break;  // We already found it ; so lets test the next named filter...
00244                 }
00245             }
00246         }
00247     }
00248 
00249     QPtrListIterator<KURIFilterPlugin> it( use_plugins );
00250     //kdDebug() << "Using " << use_plugins.count() << " out of the "
00251     //          << m_lstPlugins.count() << " available plugins" << endl;
00252     for (; it.current() && !filtered; ++it)
00253     {
00254         //kdDebug() << "Using a filter plugin named: " << it.current()->name() << endl;
00255         filtered |= it.current()->filterURI( data );
00256     }
00257     return filtered;
00258 }
00259 
00260 bool KURIFilter::filterURI( KURL& uri, const QStringList& filters )
00261 {
00262     KURIFilterData data = uri;
00263     bool filtered = filterURI( data, filters );
00264     if( filtered ) uri = data.uri();
00265     return filtered;
00266 }
00267 
00268 bool KURIFilter::filterURI( QString& uri, const QStringList& filters )
00269 {
00270     KURIFilterData data = uri;
00271     bool filtered = filterURI( data, filters );
00272     if( filtered )  uri = data.uri().url();
00273     return filtered;
00274 
00275 }
00276 
00277 KURL KURIFilter::filteredURI( const KURL &uri, const QStringList& filters )
00278 {
00279     KURIFilterData data = uri;
00280     filterURI( data, filters );
00281     return data.uri();
00282 }
00283 
00284 QString KURIFilter::filteredURI( const QString &uri, const QStringList& filters )
00285 {
00286     KURIFilterData data = uri;
00287     filterURI( data, filters );
00288     return data.uri().url();
00289 }
00290 
00291 QPtrListIterator<KURIFilterPlugin> KURIFilter::pluginsIterator() const
00292 {
00293     return QPtrListIterator<KURIFilterPlugin>(m_lstPlugins);
00294 }
00295 
00296 QStringList KURIFilter::pluginNames() const
00297 {
00298     QStringList list;
00299     for(QPtrListIterator<KURIFilterPlugin> i = pluginsIterator(); *i; ++i)
00300         list.append((*i)->name());
00301     return list;
00302 }
00303 
00304 void KURIFilter::loadPlugins()
00305 {
00306     KTrader::OfferList offers = KTrader::self()->query( "KURIFilter/Plugin" );
00307 
00308     KTrader::OfferList::ConstIterator it = offers.begin();
00309     KTrader::OfferList::ConstIterator end = offers.end();
00310 
00311     for (; it != end; ++it )
00312     {
00313       KURIFilterPlugin *plugin = KParts::ComponentFactory::createInstanceFromService<KURIFilterPlugin>( *it, 0, (*it)->desktopEntryName().latin1() );
00314       if ( plugin )
00315         m_lstPlugins.append( plugin );
00316     }
00317 
00318     // NOTE: Plugin priority is now determined by
00319     // the entry in the .desktop files...
00320     // TODO: Config dialog to differentiate "system"
00321     // plugins from "user-defined" ones...
00322     // m_lstPlugins.sort();
00323 }
00324 
00325 void KURIFilterPlugin::virtual_hook( int, void* )
00326 { /*BASE::virtual_hook( id, data );*/ }
00327 
00328 #include "kurifilter.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 3 19:23:43 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003