# kio Library API Documentation

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

# 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 <kiconloader.h>
    00025 #include <ktrader.h>
    00026 #include <kmimetype.h>
    00027 #include <klibloader.h>
    00028 #include <kstaticdeleter.h>
    00029 #include <kparts/componentfactory.h>
    00030
    00031 #include "kurifilter.h"
    00032
    00033 template class QPtrList<KURIFilterPlugin>;
    00034
    00035 KURIFilterPlugin::KURIFilterPlugin( QObject *parent, const char *name, double pri )
    00036                  :QObject( parent, name )
    00037 {
    00038     m_strName = QString::fromLatin1( name );
    00039     m_dblPriority = pri;
    00040 }
    00041
    00042 void KURIFilterPlugin::setFilteredURI( KURIFilterData& data, const KURL& uri ) const
    00043 {
    00044     if ( data.uri() != uri )
    00045     {
    00046         data.m_pURI = uri;
    00047         data.m_bChanged = true;
    00048     }
    00049 }
    00050
    00051 class KURIFilterDataPrivate
    00052 {
    00053 public:
    00054     KURIFilterDataPrivate() {};
    00055     QString abs_path;
    00056     QString args;
    00057     QString typedString;
    00058 };
    00059
    00060 KURIFilterData::KURIFilterData( const KURIFilterData& data )
    00061 {
    00062     m_iType = data.m_iType;
    00063     m_pURI = data.m_pURI;
    00064     m_strErrMsg = data.m_strErrMsg;
    00065     m_strIconName = data.m_strIconName;
    00066     m_bChanged = data.m_bChanged;
    00067     m_bCheckForExecutables = data.m_bCheckForExecutables;
    00068     d = new KURIFilterDataPrivate;
    00069     d->abs_path = data.absolutePath();
    00070     d->typedString = data.typedString();
    00071     d->args = data.argsAndOptions();
    00072 }
    00073
    00074 KURIFilterData::~KURIFilterData()
    00075 {
    00076     delete d;
    00077     d = 0;
    00078 }
    00079
    00080 void KURIFilterData::init( const KURL& url )
    00081 {
    00082     m_iType = KURIFilterData::UNKNOWN;
    00083     m_pURI = url;
    00084     m_strErrMsg = QString::null;
    00085     m_strIconName = QString::null;
    00086     m_bCheckForExecutables = true;
    00087     m_bChanged = true;
    00088     d = new KURIFilterDataPrivate;
    00089     d->typedString = url.url();
    00090 }
    00091
    00092 void KURIFilterData::init( const QString& url )
    00093 {
    00094     m_iType = KURIFilterData::UNKNOWN;
    00095     m_pURI = url;
    00096     m_strErrMsg = QString::null;
    00097     m_strIconName = QString::null;
    00098     m_bCheckForExecutables = true;
    00099     m_bChanged = true;
    00100     d = new KURIFilterDataPrivate;
    00101     d->typedString = url;
    00102 }
    00103
    00104 QString KURIFilterData::typedString() const
    00105 {
    00106     return d->typedString;
    00107 }
    00108
    00109 void KURIFilterData::setCheckForExecutables( bool check )
    00110 {
    00111     m_bCheckForExecutables = check;
    00112 }
    00113
    00114 bool KURIFilterData::hasArgsAndOptions() const
    00115 {
    00116     return !d->args.isEmpty();
    00117 }
    00118
    00119 bool KURIFilterData::hasAbsolutePath() const
    00120 {
    00121     return !d->abs_path.isEmpty();
    00122 }
    00123
    00124 bool KURIFilterData::setAbsolutePath( const QString& absPath )
    00125 {
    00126     // Since a malformed URL could possibly be a relative
    00127     // URL we tag it as a possible local resource...
    00128     if( (!m_pURI.isValid() || m_pURI.isLocalFile()) )
    00129     {
    00130         d->abs_path = absPath;
    00131         return true;
    00132     }
    00133     return false;
    00134 }
    00135
    00136 QString KURIFilterData::absolutePath() const
    00137 {
    00138     return d->abs_path;
    00139 }
    00140
    00141 QString KURIFilterData::argsAndOptions() const
    00142 {
    00143     return d->args;
    00144 }
    00145
    00146 QString KURIFilterData::iconName()
    00147 {
    00148     if( m_bChanged )
    00149     {
    00150         switch ( m_iType )
    00151         {
    00152             case KURIFilterData::LOCAL_FILE:
    00153             case KURIFilterData::LOCAL_DIR:
    00154             case KURIFilterData::NET_PROTOCOL:
    00155             {
    00156                 m_strIconName = KMimeType::iconForURL( m_pURI );
    00157                 break;
    00158             }
    00159             case KURIFilterData::EXECUTABLE:
    00160             {
    00161                 QString exeName = m_pURI.url();
    00162                 exeName = exeName.mid( exeName.findRev( '/' ) + 1 ); // strip path if given
    00163                 KService::Ptr service = KService::serviceByDesktopName( exeName );
    00164                 if (service && service->icon() != QString::fromLatin1( "unknown" ))
    00165                     m_strIconName = service->icon();
    00166                 // Try to find an icon with the same name as the binary (useful for non-kde apps)
    00167                 else if ( !KGlobal::iconLoader()->loadIcon( exeName, KIcon::NoGroup, 16, KIcon::DefaultState, 0, true ).isNull() )
    00168                     m_strIconName = exeName;
    00169                 else
    00170                     // not found, use default
    00171                     m_strIconName = QString::fromLatin1("exec");
    00172                 break;
    00173             }
    00174             case KURIFilterData::HELP:
    00175             {
    00176                 m_strIconName = QString::fromLatin1("khelpcenter");
    00177                 break;
    00178             }
    00179             case KURIFilterData::SHELL:
    00180             {
    00181                 m_strIconName = QString::fromLatin1("konsole");
    00182                 break;
    00183             }
    00184             case KURIFilterData::ERROR:
    00185             case KURIFilterData::BLOCKED:
    00186             {
    00187                 m_strIconName = QString::fromLatin1("error");
    00188                 break;
    00189             }
    00190             default:
    00191                 m_strIconName = QString::null;
    00192                 break;
    00193         }
    00194         m_bChanged = false;
    00195     }
    00196     return m_strIconName;
    00197 }
    00198
    00199 //********************************************  KURIFilterPlugin **********************************************
    00200 void KURIFilterPlugin::setArguments( KURIFilterData& data, const QString& args ) const
    00201 {
    00202     data.d->args = args;
    00203 }
    00204
    00205 //********************************************  KURIFilter **********************************************
    00206 KURIFilter *KURIFilter::m_self;
    00207 static KStaticDeleter<KURIFilter> kurifiltersd;
    00208
    00209 KURIFilter *KURIFilter::self()
    00210 {
    00211     if (!m_self)
    00212         m_self = kurifiltersd.setObject(m_self, new KURIFilter);
    00213     return m_self;
    00214 }
    00215
    00216 KURIFilter::KURIFilter()
    00217 {
    00218     m_lstPlugins.setAutoDelete(true);
    00219     loadPlugins();
    00220 }
    00221
    00222 KURIFilter::~KURIFilter()
    00223 {
    00224     m_self = 0;
    00225 }
    00226
    00227 bool KURIFilter::filterURI( KURIFilterData& data, const QStringList& filters )
    00228 {
    00229     bool filtered = false;
    00230     KURIFilterPluginList use_plugins;
    00231
    00232     // If we have a filter list, only include the once
    00233     // explicitly specified by it. Otherwise, use all available filters...
    00234     if( filters.isEmpty() )
    00235         use_plugins = m_lstPlugins;  // Use everything that is loaded...
    00236     else
    00237     {
    00238         //kdDebug() << "Named plugins requested..."  << endl;
    00239         for( QStringList::ConstIterator lst = filters.begin(); lst != filters.end(); ++lst )
    00240         {
    00241             QPtrListIterator<KURIFilterPlugin> it( m_lstPlugins );
    00242             for( ; it.current() ; ++it )
    00243             {
    00244                 if( (*lst) == it.current()->name() )
    00245                 {
    00246                     //kdDebug() << "Will use filter plugin named: " << it.current()->name() << endl;
    00247                     use_plugins.append( it.current() );
    00248                     break;  // We already found it ; so lets test the next named filter...
    00249                 }
    00250             }
    00251         }
    00252     }
    00253
    00254     QPtrListIterator<KURIFilterPlugin> it( use_plugins );
    00255     //kdDebug() << "Using " << use_plugins.count() << " out of the "
    00256     //          << m_lstPlugins.count() << " available plugins" << endl;
    00257     for (; it.current() && !filtered; ++it)
    00258     {
    00259         //kdDebug() << "Using a filter plugin named: " << it.current()->name() << endl;
    00260         filtered |= it.current()->filterURI( data );
    00261     }
    00262     return filtered;
    00263 }
    00264
    00265 bool KURIFilter::filterURI( KURL& uri, const QStringList& filters )
    00266 {
    00267     KURIFilterData data = uri;
    00268     bool filtered = filterURI( data, filters );
    00269     if( filtered ) uri = data.uri();
    00270     return filtered;
    00271 }
    00272
    00273 bool KURIFilter::filterURI( QString& uri, const QStringList& filters )
    00274 {
    00275     KURIFilterData data = uri;
    00276     bool filtered = filterURI( data, filters );
    00277     if( filtered )  uri = data.uri().url();
    00278     return filtered;
    00279
    00280 }
    00281
    00282 KURL KURIFilter::filteredURI( const KURL &uri, const QStringList& filters )
    00283 {
    00284     KURIFilterData data = uri;
    00285     filterURI( data, filters );
    00286     return data.uri();
    00287 }
    00288
    00289 QString KURIFilter::filteredURI( const QString &uri, const QStringList& filters )
    00290 {
    00291     KURIFilterData data = uri;
    00292     filterURI( data, filters );
    00293     return data.uri().url();
    00294 }
    00295
    00296 QPtrListIterator<KURIFilterPlugin> KURIFilter::pluginsIterator() const
    00297 {
    00298     return QPtrListIterator<KURIFilterPlugin>(m_lstPlugins);
    00299 }
    00300
    00301 QStringList KURIFilter::pluginNames() const
    00302 {
    00303     QStringList list;
    00304     for(QPtrListIterator<KURIFilterPlugin> i = pluginsIterator(); *i; ++i)
    00305         list.append((*i)->name());
    00306     return list;
    00307 }
    00308
    00309 void KURIFilter::loadPlugins()
    00310 {
    00311     KTrader::OfferList offers = KTrader::self()->query( "KURIFilter/Plugin" );
    00312
    00313     KTrader::OfferList::ConstIterator it = offers.begin();
    00314     KTrader::OfferList::ConstIterator end = offers.end();
    00315
    00316     for (; it != end; ++it )
    00317     {
    00318       KURIFilterPlugin *plugin = KParts::ComponentFactory::createInstanceFromService<KURIFilterPlugin>( *it, 0, (*it)->desktopEntryName().latin1() );
    00319       if ( plugin )
    00320         m_lstPlugins.append( plugin );
    00321     }
    00322
    00323     // NOTE: Plugin priority is now determined by
    00324     // the entry in the .desktop files...
    00325     // TODO: Config dialog to differentiate "system"
    00326     // plugins from "user-defined" ones...
    00327     // m_lstPlugins.sort();
    00328 }
    00329
    00330 void KURIFilterPlugin::virtual_hook( int, void* )
    00331 { /*BASE::virtual_hook( id, data );*/ }
    00332
    00333 #include "kurifilter.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:24:04 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003