• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

kurifilter.cpp

Go to the documentation of this file.
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 "kurifilter.h"
00022 
00023 #include <config.h>
00024 #include <QPixmap>
00025 
00026 #include <kdebug.h>
00027 #include <kiconloader.h>
00028 #include <kservicetypetrader.h>
00029 #include <kmimetype.h>
00030 
00031 typedef QList<KUriFilterPlugin *> KUriFilterPluginList;
00032 
00033 class KUriFilterDataPrivate
00034 {
00035 public:
00036     explicit KUriFilterDataPrivate( const KUrl& url, const QString& typedUrl ) :
00037         bCheckForExecutables(true),
00038         bChanged(true),
00039         pURI(url),
00040         iType(KUriFilterData::Unknown),
00041         typedString(typedUrl)
00042     {
00043     }
00044     void setData( const KUrl& url, const QString& typedUrl )
00045     {
00046         bCheckForExecutables = true;
00047         bChanged = true;
00048         strErrMsg.clear();
00049         strIconName.clear();
00050         pURI = url;
00051         iType = KUriFilterData::Unknown;
00052         typedString = typedUrl;
00053     }
00054     KUriFilterDataPrivate( KUriFilterDataPrivate * data )
00055     {
00056         iType = data->iType;
00057         pURI = data->pURI;
00058         strErrMsg = data->strErrMsg;
00059         strIconName = data->strIconName;
00060         bChanged = data->bChanged;
00061         bCheckForExecutables = data->bCheckForExecutables;
00062         abs_path = data->abs_path;
00063         typedString = data->typedString;
00064         args = data->args;
00065     }
00066     bool bCheckForExecutables;
00067     bool bChanged;
00068 
00069     QString strErrMsg;
00070     QString strIconName;
00071 
00072     KUrl pURI;
00073     KUriFilterData::UriTypes iType;
00074 
00075     QString abs_path;
00076     QString args;
00077     QString typedString;
00078 };
00079 
00080 KUriFilterData::KUriFilterData()
00081     : d( new KUriFilterDataPrivate( KUrl(), QString() ) )
00082 {
00083 }
00084 
00085 KUriFilterData::KUriFilterData( const KUrl& url )
00086     : d( new KUriFilterDataPrivate( url, url.url() ) )
00087 {
00088 }
00089 
00090 KUriFilterData::KUriFilterData( const QString& url )
00091     : d( new KUriFilterDataPrivate( KUrl(url), url ) )
00092 {
00093     d->typedString = url;
00094 }
00095 
00096 
00097 KUriFilterData::KUriFilterData( const KUriFilterData& data )
00098     : d( new KUriFilterDataPrivate( data.d ) )
00099 {
00100 }
00101 
00102 KUriFilterData::~KUriFilterData()
00103 {
00104     delete d;
00105 }
00106 
00107 KUrl KUriFilterData::uri() const
00108 {
00109     return d->pURI;
00110 }
00111 
00112 QString KUriFilterData::errorMsg() const
00113 {
00114     return d->strErrMsg;
00115 }
00116 
00117 KUriFilterData::UriTypes KUriFilterData::uriType() const
00118 {
00119     return d->iType;
00120 }
00121 
00122 void KUriFilterData::setData( const KUrl& url )
00123 {
00124     d->setData(url, url.url());
00125 }
00126 
00127 void KUriFilterData::setData( const QString& url )
00128 {
00129     d->setData(KUrl(url), url);
00130 }
00131 
00132 bool KUriFilterData::setAbsolutePath( const QString& absPath )
00133 {
00134     // Since a malformed URL could possibly be a relative
00135     // URL we tag it as a possible local resource...
00136     if( (d->pURI.protocol().isEmpty() || d->pURI.isLocalFile()) )
00137     {
00138         d->abs_path = absPath;
00139         return true;
00140     }
00141     return false;
00142 }
00143 
00144 QString KUriFilterData::absolutePath() const
00145 {
00146     return d->abs_path;
00147 }
00148 
00149 bool KUriFilterData::hasAbsolutePath() const
00150 {
00151     return !d->abs_path.isEmpty();
00152 }
00153 
00154 QString KUriFilterData::argsAndOptions() const
00155 {
00156     return d->args;
00157 }
00158 
00159 bool KUriFilterData::hasArgsAndOptions() const
00160 {
00161     return !d->args.isEmpty();
00162 }
00163 
00164 QString KUriFilterData::iconName()
00165 {
00166     if( d->bChanged )
00167     {
00168         switch ( d->iType )
00169         {
00170             case KUriFilterData::LocalFile:
00171             case KUriFilterData::LocalDir:
00172             case KUriFilterData::NetProtocol:
00173             {
00174                 d->strIconName = KMimeType::iconNameForUrl( d->pURI );
00175                 break;
00176             }
00177             case KUriFilterData::Executable:
00178             {
00179                 QString exeName = d->pURI.url();
00180                 exeName = exeName.mid( exeName.lastIndexOf( '/' ) + 1 ); // strip path if given
00181                 KService::Ptr service = KService::serviceByDesktopName( exeName );
00182                 if (service && service->icon() != QLatin1String( "unknown" ))
00183                     d->strIconName = service->icon();
00184                 // Try to find an icon with the same name as the binary (useful for non-kde apps)
00185                 else if ( !KIconLoader::global()->loadIcon( exeName, KIconLoader::NoGroup, 16, KIconLoader::DefaultState, QStringList(), 0, true ).isNull() )
00186                     d->strIconName = exeName;
00187                 else
00188                     // not found, use default
00189                     d->strIconName = QLatin1String("system-run");
00190                 break;
00191             }
00192             case KUriFilterData::Help:
00193             {
00194                 d->strIconName = QLatin1String("khelpcenter");
00195                 break;
00196             }
00197             case KUriFilterData::Shell:
00198             {
00199                 d->strIconName = QLatin1String("konsole");
00200                 break;
00201             }
00202             case KUriFilterData::Error:
00203             case KUriFilterData::Blocked:
00204             {
00205                 d->strIconName = QLatin1String("error");
00206                 break;
00207             }
00208             default:
00209                 d->strIconName.clear();
00210                 break;
00211         }
00212         d->bChanged = false;
00213     }
00214     return d->strIconName;
00215 }
00216 
00217 void KUriFilterData::setCheckForExecutables( bool check )
00218 {
00219     d->bCheckForExecutables = check;
00220 }
00221 
00222 bool KUriFilterData::checkForExecutables() const
00223 {
00224     return d->bCheckForExecutables;
00225 }
00226 
00227 QString KUriFilterData::typedString() const
00228 {
00229     return d->typedString;
00230 }
00231 
00232 KUriFilterData& KUriFilterData::operator=( const KUrl& url )
00233 {
00234     d->setData(url, url.url());
00235     return *this;
00236 }
00237 
00238 KUriFilterData& KUriFilterData::operator=( const QString& url )
00239 {
00240     d->setData(KUrl(url), url);
00241     return *this;
00242 }
00243 
00244 /*************************  KUriFilterPlugin ******************************/
00245 
00246 KUriFilterPlugin::KUriFilterPlugin( const QString & name, QObject *parent )
00247     : QObject( parent ), d( 0 )
00248 {
00249     setObjectName( name );
00250 }
00251 
00252 KCModule *KUriFilterPlugin::configModule( QWidget*, const char* ) const
00253 {
00254     return 0;
00255 }
00256 
00257 QString KUriFilterPlugin::configName() const
00258 {
00259     return objectName();
00260 }
00261 
00262 void KUriFilterPlugin::setFilteredUri( KUriFilterData& data, const KUrl& uri ) const
00263 {
00264     if ( data.uri() != uri )
00265     {
00266         data.d->pURI = uri;
00267         data.d->bChanged = true;
00268     }
00269 }
00270 
00271 void KUriFilterPlugin::setErrorMsg ( KUriFilterData& data,
00272                                      const QString& errmsg ) const
00273 {
00274     data.d->strErrMsg = errmsg;
00275 }
00276 
00277 void KUriFilterPlugin::setUriType ( KUriFilterData& data,
00278                                     KUriFilterData::UriTypes type) const
00279 {
00280     data.d->iType = type;
00281     data.d->bChanged = true;
00282 }
00283 
00284 void KUriFilterPlugin::setArguments( KUriFilterData& data,
00285                                      const QString& args ) const
00286 {
00287     data.d->args = args;
00288 }
00289 
00290 /*******************************  KUriFilter ******************************/
00291 
00292 class KUriFilterPrivate
00293 {
00294 public:
00295     KUriFilterPrivate() {}
00296     ~KUriFilterPrivate()
00297     {
00298         qDeleteAll(lstPlugins);
00299     }
00300     QList<KUriFilterPlugin *> lstPlugins;
00301 };
00302 
00303 KUriFilter *KUriFilter::self()
00304 {
00305     K_GLOBAL_STATIC(KUriFilter, m_self)
00306     return m_self;
00307 }
00308 
00309 KUriFilter::KUriFilter()
00310     : d(new KUriFilterPrivate())
00311 {
00312     loadPlugins();
00313 }
00314 
00315 KUriFilter::~KUriFilter()
00316 {
00317     delete d;
00318 }
00319 
00320 static KUriFilterPlugin* findPluginByName( const KUriFilterPluginList& lst, const QString& name )
00321 {
00322     for ( KUriFilterPluginList::const_iterator it = lst.begin(), end = lst.end();
00323           it != end ; ++it ) {
00324         if ( (*it)->objectName() == name )
00325             return *it;
00326     }
00327     return 0;
00328 }
00329 
00330 bool KUriFilter::filterUri( KUriFilterData& data, const QStringList& filters )
00331 {
00332     KUriFilterPluginList use_plugins;
00333 
00334     // If we have a filter list, only include the once
00335     // explicitly specified by it. Otherwise, use all available filters...
00336     if( filters.isEmpty() )
00337         use_plugins = d->lstPlugins;  // Use everything that is loaded...
00338     else {
00339         //kDebug() << "Named plugins requested...";
00340         for( QStringList::ConstIterator lst = filters.begin(); lst != filters.end(); ++lst ) {
00341             KUriFilterPlugin* plugin = findPluginByName( d->lstPlugins, *lst );
00342             if (plugin) {
00343                 //kDebug() << "Will use filter plugin named: " << plugin->objectName();
00344                 use_plugins.append(plugin);
00345             }
00346         }
00347     }
00348 
00349     //kDebug() << "Using" << use_plugins.count() << "out of the"
00350     //          << d->lstPlugins.count() << "available plugins";
00351     bool filtered = false;
00352     for ( KUriFilterPluginList::const_iterator it = use_plugins.constBegin(), end = use_plugins.constEnd();
00353           it != end; ++it ) {
00354         //kDebug() << "Using a filter plugin named: " << (*it)->objectName();
00355         if( (*it)->filterUri( data ))
00356             filtered = true;
00357     }
00358     return filtered;
00359 }
00360 
00361 bool KUriFilter::filterUri( KUrl& uri, const QStringList& filters )
00362 {
00363     KUriFilterData data(uri);
00364     bool filtered = filterUri( data, filters );
00365     if( filtered ) uri = data.uri();
00366     return filtered;
00367 }
00368 
00369 bool KUriFilter::filterUri( QString& uri, const QStringList& filters )
00370 {
00371     KUriFilterData data(uri);
00372     bool filtered = filterUri( data, filters );
00373     if( filtered )  uri = data.uri().url();
00374     return filtered;
00375 }
00376 
00377 KUrl KUriFilter::filteredUri( const KUrl &uri, const QStringList& filters )
00378 {
00379     KUriFilterData data(uri);
00380     filterUri( data, filters );
00381     return data.uri();
00382 }
00383 
00384 QString KUriFilter::filteredUri( const QString &uri, const QStringList& filters )
00385 {
00386     KUriFilterData data(uri);
00387     filterUri( data, filters );
00388     return data.uri().url();
00389 }
00390 
00391 QStringList KUriFilter::pluginNames() const
00392 {
00393     QStringList list;
00394     Q_FOREACH( KUriFilterPlugin* plugin, d->lstPlugins )
00395         list.append(plugin->objectName());
00396     return list;
00397 }
00398 
00399 void KUriFilter::loadPlugins()
00400 {
00401     const KService::List offers = KServiceTypeTrader::self()->query( "KUriFilter/Plugin" );
00402 
00403     foreach (const KService::Ptr &ptr, offers) {
00404         KUriFilterPlugin *plugin = ptr->createInstance<KUriFilterPlugin>();
00405         if (!plugin)
00406             continue;
00407         Q_ASSERT( !plugin->objectName().isEmpty() );
00408         d->lstPlugins.append( plugin );
00409     }
00410 
00411     // NOTE: Plugin priority is determined by
00412     // the InitialPreference entry in the .desktop files,
00413     // so the trader result is already sorted.
00414 
00415     // TODO: Config dialog to differentiate "system"
00416     // plugins from "user-defined" ones...
00417     // d->lstPlugins.sort();
00418 }
00419 
00420 #include "kurifilter.moc"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal