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 void KURIFilterData::reinit(const KURL &url) 00105 { 00106 delete d; 00107 init(url); 00108 } 00109 00110 void KURIFilterData::reinit(const QString &url) 00111 { 00112 delete d; 00113 init(url); 00114 } 00115 00116 QString KURIFilterData::typedString() const 00117 { 00118 return d->typedString; 00119 } 00120 00121 void KURIFilterData::setCheckForExecutables( bool check ) 00122 { 00123 m_bCheckForExecutables = check; 00124 } 00125 00126 bool KURIFilterData::hasArgsAndOptions() const 00127 { 00128 return !d->args.isEmpty(); 00129 } 00130 00131 bool KURIFilterData::hasAbsolutePath() const 00132 { 00133 return !d->abs_path.isEmpty(); 00134 } 00135 00136 bool KURIFilterData::setAbsolutePath( const QString& absPath ) 00137 { 00138 // Since a malformed URL could possibly be a relative 00139 // URL we tag it as a possible local resource... 00140 if( (!m_pURI.isValid() || m_pURI.isLocalFile()) ) 00141 { 00142 d->abs_path = absPath; 00143 return true; 00144 } 00145 return false; 00146 } 00147 00148 QString KURIFilterData::absolutePath() const 00149 { 00150 return d->abs_path; 00151 } 00152 00153 QString KURIFilterData::argsAndOptions() const 00154 { 00155 return d->args; 00156 } 00157 00158 QString KURIFilterData::iconName() 00159 { 00160 if( m_bChanged ) 00161 { 00162 switch ( m_iType ) 00163 { 00164 case KURIFilterData::LOCAL_FILE: 00165 case KURIFilterData::LOCAL_DIR: 00166 case KURIFilterData::NET_PROTOCOL: 00167 { 00168 m_strIconName = KMimeType::iconForURL( m_pURI ); 00169 break; 00170 } 00171 case KURIFilterData::EXECUTABLE: 00172 { 00173 QString exeName = m_pURI.url(); 00174 exeName = exeName.mid( exeName.findRev( '/' ) + 1 ); // strip path if given 00175 KService::Ptr service = KService::serviceByDesktopName( exeName ); 00176 if (service && service->icon() != QString::fromLatin1( "unknown" )) 00177 m_strIconName = service->icon(); 00178 // Try to find an icon with the same name as the binary (useful for non-kde apps) 00179 else if ( !KGlobal::iconLoader()->loadIcon( exeName, KIcon::NoGroup, 16, KIcon::DefaultState, 0, true ).isNull() ) 00180 m_strIconName = exeName; 00181 else 00182 // not found, use default 00183 m_strIconName = QString::fromLatin1("exec"); 00184 break; 00185 } 00186 case KURIFilterData::HELP: 00187 { 00188 m_strIconName = QString::fromLatin1("khelpcenter"); 00189 break; 00190 } 00191 case KURIFilterData::SHELL: 00192 { 00193 m_strIconName = QString::fromLatin1("konsole"); 00194 break; 00195 } 00196 case KURIFilterData::ERROR: 00197 case KURIFilterData::BLOCKED: 00198 { 00199 m_strIconName = QString::fromLatin1("error"); 00200 break; 00201 } 00202 default: 00203 m_strIconName = QString::null; 00204 break; 00205 } 00206 m_bChanged = false; 00207 } 00208 return m_strIconName; 00209 } 00210 00211 //******************************************** KURIFilterPlugin ********************************************** 00212 void KURIFilterPlugin::setArguments( KURIFilterData& data, const QString& args ) const 00213 { 00214 data.d->args = args; 00215 } 00216 00217 //******************************************** KURIFilter ********************************************** 00218 KURIFilter *KURIFilter::s_self; 00219 static KStaticDeleter<KURIFilter> kurifiltersd; 00220 00221 KURIFilter *KURIFilter::self() 00222 { 00223 if (!s_self) 00224 s_self = kurifiltersd.setObject(s_self, new KURIFilter); 00225 return s_self; 00226 } 00227 00228 KURIFilter::KURIFilter() 00229 { 00230 m_lstPlugins.setAutoDelete(true); 00231 loadPlugins(); 00232 } 00233 00234 KURIFilter::~KURIFilter() 00235 { 00236 } 00237 00238 bool KURIFilter::filterURI( KURIFilterData& data, const QStringList& filters ) 00239 { 00240 bool filtered = false; 00241 KURIFilterPluginList use_plugins; 00242 00243 // If we have a filter list, only include the once 00244 // explicitly specified by it. Otherwise, use all available filters... 00245 if( filters.isEmpty() ) 00246 use_plugins = m_lstPlugins; // Use everything that is loaded... 00247 else 00248 { 00249 //kdDebug() << "Named plugins requested..." << endl; 00250 for( QStringList::ConstIterator lst = filters.begin(); lst != filters.end(); ++lst ) 00251 { 00252 QPtrListIterator<KURIFilterPlugin> it( m_lstPlugins ); 00253 for( ; it.current() ; ++it ) 00254 { 00255 if( (*lst) == it.current()->name() ) 00256 { 00257 //kdDebug() << "Will use filter plugin named: " << it.current()->name() << endl; 00258 use_plugins.append( it.current() ); 00259 break; // We already found it ; so lets test the next named filter... 00260 } 00261 } 00262 } 00263 } 00264 00265 QPtrListIterator<KURIFilterPlugin> it( use_plugins ); 00266 //kdDebug() << "Using " << use_plugins.count() << " out of the " 00267 // << m_lstPlugins.count() << " available plugins" << endl; 00268 for (; it.current() && !filtered; ++it) 00269 { 00270 //kdDebug() << "Using a filter plugin named: " << it.current()->name() << endl; 00271 filtered |= it.current()->filterURI( data ); 00272 } 00273 return filtered; 00274 } 00275 00276 bool KURIFilter::filterURI( KURL& uri, const QStringList& filters ) 00277 { 00278 KURIFilterData data = uri; 00279 bool filtered = filterURI( data, filters ); 00280 if( filtered ) uri = data.uri(); 00281 return filtered; 00282 } 00283 00284 bool KURIFilter::filterURI( QString& uri, const QStringList& filters ) 00285 { 00286 KURIFilterData data = uri; 00287 bool filtered = filterURI( data, filters ); 00288 if( filtered ) uri = data.uri().url(); 00289 return filtered; 00290 00291 } 00292 00293 KURL KURIFilter::filteredURI( const KURL &uri, const QStringList& filters ) 00294 { 00295 KURIFilterData data = uri; 00296 filterURI( data, filters ); 00297 return data.uri(); 00298 } 00299 00300 QString KURIFilter::filteredURI( const QString &uri, const QStringList& filters ) 00301 { 00302 KURIFilterData data = uri; 00303 filterURI( data, filters ); 00304 return data.uri().url(); 00305 } 00306 00307 QPtrListIterator<KURIFilterPlugin> KURIFilter::pluginsIterator() const 00308 { 00309 return QPtrListIterator<KURIFilterPlugin>(m_lstPlugins); 00310 } 00311 00312 QStringList KURIFilter::pluginNames() const 00313 { 00314 QStringList list; 00315 for(QPtrListIterator<KURIFilterPlugin> i = pluginsIterator(); *i; ++i) 00316 list.append((*i)->name()); 00317 return list; 00318 } 00319 00320 void KURIFilter::loadPlugins() 00321 { 00322 KTrader::OfferList offers = KTrader::self()->query( "KURIFilter/Plugin" ); 00323 00324 KTrader::OfferList::ConstIterator it = offers.begin(); 00325 KTrader::OfferList::ConstIterator end = offers.end(); 00326 00327 for (; it != end; ++it ) 00328 { 00329 KURIFilterPlugin *plugin = KParts::ComponentFactory::createInstanceFromService<KURIFilterPlugin>( *it, 0, (*it)->desktopEntryName().latin1() ); 00330 if ( plugin ) 00331 m_lstPlugins.append( plugin ); 00332 } 00333 00334 // NOTE: Plugin priority is now determined by 00335 // the entry in the .desktop files... 00336 // TODO: Config dialog to differentiate "system" 00337 // plugins from "user-defined" ones... 00338 // m_lstPlugins.sort(); 00339 } 00340 00341 void KURIFilterPlugin::virtual_hook( int, void* ) 00342 { /*BASE::virtual_hook( id, data );*/ } 00343 00344 #include "kurifilter.moc"