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@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 };
00057 
00058 KURIFilterData::KURIFilterData( const KURIFilterData& data )
00059 {
00060     m_iType = data.m_iType;
00061     m_pURI = data.m_pURI;
00062     m_strErrMsg = data.m_strErrMsg;
00063     m_strIconName = data.m_strIconName;
00064     m_bChanged = data.m_bChanged;
00065     d = new KURIFilterDataPrivate;
00066     d->abs_path = data.absolutePath();
00067 }
00068 
00069 KURIFilterData::~KURIFilterData()
00070 {
00071     delete d;
00072     d = 0;
00073 }
00074 
00075 void KURIFilterData::init( const KURL& url )
00076 {
00077     m_iType = KURIFilterData::UNKNOWN;
00078     m_pURI = url;
00079     m_strErrMsg = QString::null;
00080     m_strIconName = QString::null;
00081     m_bFiltered = true;  //deprecated!!! Always returns true!
00082     m_bChanged = true;
00083     d = new KURIFilterDataPrivate;
00084 }
00085 
00086 bool KURIFilterData::hasArgsAndOptions() const
00087 {
00088     return !d->args.isEmpty();
00089 }
00090 
00091 bool KURIFilterData::hasAbsolutePath() const
00092 {
00093     return !d->abs_path.isEmpty();
00094 }
00095 
00096 bool KURIFilterData::setAbsolutePath( const QString& absPath )
00097 {
00098     // Since a malformed URL could possibly be a relative
00099     // URL we tag it as a possible local resource...
00100     if( (m_pURI.isMalformed() || m_pURI.isLocalFile()) )
00101     {
00102         d->abs_path = absPath;
00103         return true;
00104     }
00105     return false;
00106 }
00107 
00108 QString KURIFilterData::absolutePath() const
00109 {
00110     return d->abs_path;
00111 }
00112 
00113 QString KURIFilterData::argsAndOptions() const
00114 {
00115     return d->args;
00116 }
00117 
00118 QString KURIFilterData::iconName()
00119 {
00120     if( m_bChanged )
00121     {
00122         switch ( m_iType )
00123         {
00124             case KURIFilterData::LOCAL_FILE:
00125             case KURIFilterData::LOCAL_DIR:
00126             case KURIFilterData::NET_PROTOCOL:
00127             {
00128                 m_strIconName = KMimeType::iconForURL( m_pURI );
00129                 break;
00130             }
00131             case KURIFilterData::EXECUTABLE:
00132             {
00133                 KService::Ptr service = KService::serviceByDesktopName( m_pURI.url() );
00134                 if (service)
00135                     m_strIconName = service->icon();
00136                 else
00137                     m_strIconName = QString::fromLatin1("exec");
00138                 break;
00139             }
00140             case KURIFilterData::HELP:
00141             {
00142                 m_strIconName = QString::fromLatin1("khelpcenter");
00143                 break;
00144             }
00145             case KURIFilterData::SHELL:
00146             {
00147                 m_strIconName = QString::fromLatin1("konsole");
00148                 break;
00149             }
00150             case KURIFilterData::ERROR:
00151             case KURIFilterData::BLOCKED:
00152             {
00153                 m_strIconName = QString::fromLatin1("error");
00154                 break;
00155             }
00156             default:
00157                 m_strIconName = QString::null;
00158                 break;
00159         }
00160         m_bChanged = false;
00161     }
00162     return m_strIconName;
00163 }
00164 
00165 //********************************************  KURIFilterPlugin **********************************************
00166 void KURIFilterPlugin::setArguments( KURIFilterData& data, const QString& args ) const
00167 {
00168     data.d->args = args;
00169 }
00170 
00171 //********************************************  KURIFilter **********************************************
00172 KURIFilter *KURIFilter::m_self = 0;
00173 KStaticDeleter<KURIFilter> kurifiltersd;
00174 
00175 KURIFilter *KURIFilter::self()
00176 {
00177     if (!m_self)
00178         m_self = kurifiltersd.setObject(new KURIFilter);
00179     return m_self;
00180 }
00181 
00182 KURIFilter::KURIFilter()
00183 {
00184     m_lstPlugins.setAutoDelete(true);
00185     loadPlugins();
00186 }
00187 
00188 KURIFilter::~KURIFilter()
00189 {
00190     m_self = 0;
00191 }
00192 
00193 bool KURIFilter::filterURI( KURIFilterData& data, const QStringList& filters )
00194 {
00195     bool filtered = false;
00196     KURIFilterPluginList use_plugins;
00197 
00198     // If we have a filter list, only include the once
00199     // explicitly specified by it.  Otherwise use all
00200     // available filters...
00201     if( filters.isEmpty() )
00202         use_plugins = m_lstPlugins;  // Use everything that is loaded...
00203     else
00204     {
00205         //kdDebug() << "Named plugins requested..."  << endl;
00206         for( QStringList::ConstIterator lst = filters.begin(); lst != filters.end(); ++lst )
00207         {
00208             QPtrListIterator<KURIFilterPlugin> it( m_lstPlugins );
00209             for( ; it.current() ; ++it )
00210             {
00211                 if( (*lst) == it.current()->name() )
00212                 {
00213                     //kdDebug() << "Will use filter plugin named: " << it.current()->name() << endl;
00214                     use_plugins.append( it.current() );
00215                     break;  // We already found it ; so lets test the next named filter...
00216                 }
00217             }
00218         }
00219     }
00220 
00221     QPtrListIterator<KURIFilterPlugin> it( use_plugins );
00222     //kdDebug() << "Using " << use_plugins.count() << " out of the "
00223     //          << m_lstPlugins.count() << " avaliable plugins" << endl;
00224     for (; it.current() && !filtered; ++it)
00225     {
00226         //kdDebug() << "Using a filter plugin named: " << it.current()->name() << endl;
00227         filtered |= it.current()->filterURI( data );
00228     }
00229     return filtered;
00230 }
00231 
00232 bool KURIFilter::filterURI( KURL& uri, const QStringList& filters )
00233 {
00234     KURIFilterData data = uri;
00235     bool filtered = filterURI( data, filters );
00236     if( filtered ) uri = data.uri();
00237     return filtered;
00238 }
00239 
00240 bool KURIFilter::filterURI( QString& uri, const QStringList& filters )
00241 {
00242     KURIFilterData data = uri;
00243     bool filtered = filterURI( data, filters );
00244     if( filtered )  uri = data.uri().url();
00245     return filtered;
00246 
00247 }
00248 
00249 KURL KURIFilter::filteredURI( const KURL &uri, const QStringList& filters )
00250 {
00251     KURIFilterData data = uri;
00252     filterURI( data, filters );
00253     return data.uri();
00254 }
00255 
00256 QString KURIFilter::filteredURI( const QString &uri, const QStringList& filters )
00257 {
00258     KURIFilterData data = uri;
00259     filterURI( data, filters );
00260     return data.uri().url();
00261 }
00262 
00263 QPtrListIterator<KURIFilterPlugin> KURIFilter::pluginsIterator() const
00264 {
00265     return QPtrListIterator<KURIFilterPlugin>(m_lstPlugins);
00266 }
00267 
00268 QStringList KURIFilter::pluginNames() const
00269 {
00270     QStringList list;
00271     for(QPtrListIterator<KURIFilterPlugin> i = pluginsIterator(); *i; ++i)
00272         list.append((*i)->name());
00273     return list;
00274 }
00275 
00276 void KURIFilter::loadPlugins()
00277 {
00278     KTrader::OfferList offers = KTrader::self()->query( "KURIFilter/Plugin" );
00279     KTrader::OfferList::ConstIterator it = offers.begin();
00280     KTrader::OfferList::ConstIterator end = offers.end();
00281 
00282     for (; it != end; ++it )
00283     {
00284     KURIFilterPlugin *plugin = 
00285         KParts::ComponentFactory::createInstanceFromService<KURIFilterPlugin>( *it,
00286                                                                                    0,
00287                                            (*it)->desktopEntryName().latin1() );
00288     if ( plugin )
00289         m_lstPlugins.append( plugin );
00290     }
00291      // NOTE: Plugin priority is now determined by
00292      // the entry in the .desktop files...
00293      // TODO: Config dialog to differentiate "system"
00294      // plugins from "user-defined" ones...
00295      // m_lstPlugins.sort();
00296 }
00297 
00298 void KURIFilterPlugin::virtual_hook( int, void* )
00299 { /*BASE::virtual_hook( id, data );*/ }
00300 
00301 #include "kurifilter.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:32 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001