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., 51 Franklin Street, Fifth Floor,
00018  *  Boston, MA 02110-1301, 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::s_self;
00207 static KStaticDeleter<KURIFilter> kurifiltersd;
00208 
00209 KURIFilter *KURIFilter::self()
00210 {
00211     if (!s_self)
00212         s_self = kurifiltersd.setObject(s_self, new KURIFilter);
00213     return s_self;
00214 }
00215 
00216 KURIFilter::KURIFilter()
00217 {
00218     m_lstPlugins.setAutoDelete(true);
00219     loadPlugins();
00220 }
00221 
00222 KURIFilter::~KURIFilter()
00223 {
00224 }
00225 
00226 bool KURIFilter::filterURI( KURIFilterData& data, const QStringList& filters )
00227 {
00228     bool filtered = false;
00229     KURIFilterPluginList use_plugins;
00230 
00231     // If we have a filter list, only include the once
00232     // explicitly specified by it. Otherwise, use all available filters...
00233     if( filters.isEmpty() )
00234         use_plugins = m_lstPlugins;  // Use everything that is loaded...
00235     else
00236     {
00237         //kdDebug() << "Named plugins requested..."  << endl;
00238         for( QStringList::ConstIterator lst = filters.begin(); lst != filters.end(); ++lst )
00239         {
00240             QPtrListIterator<KURIFilterPlugin> it( m_lstPlugins );
00241             for( ; it.current() ; ++it )
00242             {
00243                 if( (*lst) == it.current()->name() )
00244                 {
00245                     //kdDebug() << "Will use filter plugin named: " << it.current()->name() << endl;
00246                     use_plugins.append( it.current() );
00247                     break;  // We already found it ; so lets test the next named filter...
00248                 }
00249             }
00250         }
00251     }
00252 
00253     QPtrListIterator<KURIFilterPlugin> it( use_plugins );
00254     //kdDebug() << "Using " << use_plugins.count() << " out of the "
00255     //          << m_lstPlugins.count() << " available plugins" << endl;
00256     for (; it.current() && !filtered; ++it)
00257     {
00258         //kdDebug() << "Using a filter plugin named: " << it.current()->name() << endl;
00259         filtered |= it.current()->filterURI( data );
00260     }
00261     return filtered;
00262 }
00263 
00264 bool KURIFilter::filterURI( KURL& uri, const QStringList& filters )
00265 {
00266     KURIFilterData data = uri;
00267     bool filtered = filterURI( data, filters );
00268     if( filtered ) uri = data.uri();
00269     return filtered;
00270 }
00271 
00272 bool KURIFilter::filterURI( QString& uri, const QStringList& filters )
00273 {
00274     KURIFilterData data = uri;
00275     bool filtered = filterURI( data, filters );
00276     if( filtered )  uri = data.uri().url();
00277     return filtered;
00278 
00279 }
00280 
00281 KURL KURIFilter::filteredURI( const KURL &uri, const QStringList& filters )
00282 {
00283     KURIFilterData data = uri;
00284     filterURI( data, filters );
00285     return data.uri();
00286 }
00287 
00288 QString KURIFilter::filteredURI( const QString &uri, const QStringList& filters )
00289 {
00290     KURIFilterData data = uri;
00291     filterURI( data, filters );
00292     return data.uri().url();
00293 }
00294 
00295 QPtrListIterator<KURIFilterPlugin> KURIFilter::pluginsIterator() const
00296 {
00297     return QPtrListIterator<KURIFilterPlugin>(m_lstPlugins);
00298 }
00299 
00300 QStringList KURIFilter::pluginNames() const
00301 {
00302     QStringList list;
00303     for(QPtrListIterator<KURIFilterPlugin> i = pluginsIterator(); *i; ++i)
00304         list.append((*i)->name());
00305     return list;
00306 }
00307 
00308 void KURIFilter::loadPlugins()
00309 {
00310     KTrader::OfferList offers = KTrader::self()->query( "KURIFilter/Plugin" );
00311 
00312     KTrader::OfferList::ConstIterator it = offers.begin();
00313     KTrader::OfferList::ConstIterator end = offers.end();
00314 
00315     for (; it != end; ++it )
00316     {
00317       KURIFilterPlugin *plugin = KParts::ComponentFactory::createInstanceFromService<KURIFilterPlugin>( *it, 0, (*it)->desktopEntryName().latin1() );
00318       if ( plugin )
00319         m_lstPlugins.append( plugin );
00320     }
00321 
00322     // NOTE: Plugin priority is now determined by
00323     // the entry in the .desktop files...
00324     // TODO: Config dialog to differentiate "system"
00325     // plugins from "user-defined" ones...
00326     // m_lstPlugins.sort();
00327 }
00328 
00329 void KURIFilterPlugin::virtual_hook( int, void* )
00330 { /*BASE::virtual_hook( id, data );*/ }
00331 
00332 #include "kurifilter.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys