kio Library API Documentation

kservice.cpp

00001 /*  This file is part of the KDE libraries
00002  *  Copyright (C) 1999 - 2001 Waldo Bastian <bastian@kde.org>
00003  *  Copyright (C) 1999        David Faure   <faure@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 version 2 as published by the Free Software Foundation;
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  *  Boston, MA 02111-1307, USA.
00018  **/
00019 
00020 // $Id: kservice.cpp,v 1.154.2.1 2004/01/20 19:11:41 waba Exp $
00021 
00022 #include <config.h>
00023 
00024 #include "kservice.h"
00025 #include "kservice_p.h"
00026 
00027 #include <sys/types.h>
00028 #include <sys/stat.h>
00029 
00030 #include <stddef.h>
00031 #include <unistd.h>
00032 #include <stdlib.h>
00033 
00034 #include <qstring.h>
00035 #include <qfile.h>
00036 #include <qtl.h>
00037 #include <qfile.h>
00038 
00039 #include <ksimpleconfig.h>
00040 #include <kstddirs.h>
00041 #include <kapplication.h>
00042 #include <kdebug.h>
00043 #include <kdesktopfile.h>
00044 #include <kglobal.h>
00045 #include <kiconloader.h>
00046 #include <klocale.h>
00047 #include <kconfigbase.h>
00048 #include <kstandarddirs.h>
00049 #include <dcopclient.h>
00050 
00051 #include "kservicefactory.h"
00052 #include "kservicetypefactory.h"
00053 #include "kservicetype.h"
00054 #include "kuserprofile.h"
00055 #include "ksycoca.h"
00056 
00057 class KService::KServicePrivate
00058 {
00059 public:
00060   QStringList categories;
00061   QString menuId;
00062 };
00063 
00064 KService::KService( const QString & _name, const QString &_exec, const QString &_icon)
00065  : KSycocaEntry( QString::null)
00066 {
00067   d = new KServicePrivate;
00068   m_bValid = true;
00069   m_bDeleted = false;
00070   m_strType = "Application";
00071   m_strName = _name;
00072   m_strExec = _exec;
00073   m_strIcon = _icon;
00074   m_bTerminal = false;
00075   m_bAllowAsDefault = true;
00076   m_initialPreference = 10;
00077 }
00078 
00079 
00080 KService::KService( const QString & _fullpath )
00081  : KSycocaEntry( _fullpath)
00082 {
00083   KDesktopFile config( _fullpath );
00084 
00085   init(&config);
00086 }
00087 
00088 KService::KService( KDesktopFile *config )
00089  : KSycocaEntry( config->fileName())
00090 {
00091   init(config);
00092 }
00093 
00094 void
00095 KService::init( KDesktopFile *config )
00096 {
00097   d = new KServicePrivate;
00098   m_bValid = true;
00099 
00100   bool absPath = (entryPath()[0] == '/');
00101 
00102   config->setDesktopGroup();
00103 
00104   QMap<QString, QString> entryMap = config->entryMap(config->group());
00105 
00106   entryMap.remove("Encoding"); // reserved as part of Desktop Entry Standard
00107   entryMap.remove("Version");  // reserved as part of Desktop Entry Standard
00108 
00109   m_bDeleted = config->readBoolEntry( "Hidden", false );
00110   entryMap.remove("Hidden");
00111   if (m_bDeleted)
00112   {
00113     m_bValid = false;
00114     return;
00115   } else if(!absPath) {
00116     QStringList list=KGlobal::dirs()->findAllResources(QFile::encodeName(config->resource()), entryPath());
00117     bool ok=false;
00118     for(QStringList::ConstIterator it=list.fromLast(); !ok && it!=list.end(); it--) {
00119       if(!access(QFile::encodeName(*it), R_OK))
00120         ok=true;
00121     }
00122     if(!ok) {
00123       m_bValid = false;
00124       return;
00125     }
00126   }
00127 
00128   m_strName = config->readEntry( "Name" );
00129   entryMap.remove("Name");
00130   if ( m_strName.isEmpty() )
00131   {
00132     if (config->readEntry( "Exec" ).isEmpty())
00133     {
00134       m_bValid = false;
00135       return;
00136     }
00137     // Try to make up a name.
00138     m_strName = entryPath();
00139     int i = m_strName.findRev('/');
00140     m_strName = m_strName.mid(i+1);
00141     i = m_strName.findRev('.');
00142     if (i != -1)
00143        m_strName = m_strName.left(i);
00144   }
00145 
00146   m_strType = config->readEntry( "Type" );
00147   entryMap.remove("Type");
00148   if ( m_strType.isEmpty() )
00149   {
00150     /*kdWarning(7012) << "The desktop entry file " << entryPath()
00151                     << " has no Type=... entry."
00152                     << " It should be \"Application\" or \"Service\"" << endl;
00153     m_bValid = false;
00154     return;*/
00155     m_strType = "Application";
00156   } else if ( m_strType != "Application" && m_strType != "Service" )
00157   {
00158     kdWarning(7012) << "The desktop entry file " << entryPath()
00159                     << " has Type=" << m_strType
00160                     << " instead of \"Application\" or \"Service\"" << endl;
00161     m_bValid = false;
00162     return;
00163   }
00164 
00165   // In case Try Exec is set, check if the application is available
00166   if (!config->tryExec()) {
00167       m_bDeleted = true;
00168       m_bValid = false;
00169       return;
00170   }
00171 
00172   QString resource = config->resource();
00173 
00174   if ( (m_strType == "Application") &&
00175        (!resource.isEmpty()) &&
00176        (resource != "apps") &&
00177        !absPath)
00178   {
00179     kdWarning(7012) << "The desktop entry file " << entryPath()
00180            << " has Type=" << m_strType << " but is located under \"" << resource
00181            << "\" instead of \"apps\"" << endl;
00182     m_bValid = false;
00183     return;
00184   }
00185 
00186   if ( (m_strType == "Service") &&
00187        (!resource.isEmpty()) &&
00188        (resource != "services") &&
00189        !absPath)
00190   {
00191     kdWarning(7012) << "The desktop entry file " << entryPath()
00192            << " has Type=" << m_strType << " but is located under \"" << resource
00193            << "\" instead of \"services\"" << endl;
00194     m_bValid = false;
00195     return;
00196   }
00197 
00198   QString name = entryPath();
00199   int pos = name.findRev('/');
00200   if (pos != -1)
00201      name = name.mid(pos+1);
00202   pos = name.find('.');
00203   if (pos != -1)
00204      name = name.left(pos);
00205 
00206   m_strExec = config->readPathEntry( "Exec" );
00207   entryMap.remove("Exec");
00208 
00209   m_strIcon = config->readEntry( "Icon", "unknown" );
00210   entryMap.remove("Icon");
00211   m_bTerminal = (config->readBoolEntry( "Terminal" )); // should be a property IMHO
00212   entryMap.remove("Terminal");
00213   m_strTerminalOptions = config->readEntry( "TerminalOptions" ); // should be a property IMHO
00214   entryMap.remove("TerminalOptions");
00215   m_strPath = config->readPathEntry( "Path" );
00216   entryMap.remove("Path");
00217   m_strComment = config->readEntry( "Comment" );
00218   entryMap.remove("Comment");
00219   m_strGenName = config->readEntry( "GenericName" );
00220   entryMap.remove("GenericName");
00221   QString untranslatedGenericName = config->readEntryUntranslated( "GenericName" );
00222   entryMap.insert("UntranslatedGenericName", untranslatedGenericName);
00223 
00224   m_lstKeywords = config->readListEntry("Keywords");
00225   entryMap.remove("Keywords");
00226   d->categories = config->readListEntry("Categories", ';');
00227   entryMap.remove("Categories");
00228   m_strLibrary = config->readEntry( "X-KDE-Library" );
00229   entryMap.remove("X-KDE-Library");
00230   m_strInit = config->readEntry("X-KDE-Init" );
00231   entryMap.remove("X-KDE-Init");
00232 
00233   m_lstServiceTypes = config->readListEntry( "ServiceTypes" );
00234   entryMap.remove("ServiceTypes");
00235   // For compatibility with KDE 1.x
00236   m_lstServiceTypes += config->readListEntry( "MimeType", ';' );
00237   entryMap.remove("MimeType");
00238 
00239   if ( m_strType == "Application" && !m_lstServiceTypes.contains("Application") )
00240     // Applications implement the service type "Application" ;-)
00241     m_lstServiceTypes += "Application";
00242 
00243   QString dcopServiceType = config->readEntry("X-DCOP-ServiceType").lower();
00244   entryMap.remove("X-DCOP-ServiceType");
00245   if (dcopServiceType == "unique")
00246      m_DCOPServiceType = DCOP_Unique;
00247   else if (dcopServiceType == "multi")
00248      m_DCOPServiceType = DCOP_Multi;
00249   else if (dcopServiceType == "wait")
00250      m_DCOPServiceType = DCOP_Wait;
00251   else
00252      m_DCOPServiceType = DCOP_None;
00253 
00254   m_strDesktopEntryName = name.lower();
00255 
00256   m_bAllowAsDefault = config->readBoolEntry( "AllowDefault", true );
00257   entryMap.remove("AllowDefault");
00258 
00259   m_initialPreference = config->readNumEntry( "InitialPreference", 1 );
00260   entryMap.remove("InitialPreference");
00261 
00262   // Store all additional entries in the property map.
00263   // A QMap<QString,QString> would be easier for this but we can't
00264   // brake BC, so we have to store it in m_mapProps.
00265 //  qWarning("Path = %s", entryPath().latin1());
00266   QMap<QString,QString>::ConstIterator it = entryMap.begin();
00267   for( ; it != entryMap.end();++it)
00268   {
00269 //     qWarning("   Key = %s Data = %s", it.key().latin1(), it.data().latin1());
00270      m_mapProps.insert( it.key(), QVariant( it.data()));
00271   }
00272 }
00273 
00274 KService::KService( QDataStream& _str, int offset ) : KSycocaEntry( _str, offset )
00275 {
00276   d = new KServicePrivate;
00277   load( _str );
00278 }
00279 
00280 KService::~KService()
00281 {
00282   //debug("KService::~KService()");
00283   delete d;
00284 }
00285 
00286 QPixmap KService::pixmap( KIcon::Group _group, int _force_size, int _state, QString * _path ) const
00287 {
00288   KIconLoader *iconLoader=KGlobal::iconLoader();
00289   if (!iconLoader->extraDesktopThemesAdded())
00290   {
00291       QPixmap pixmap=iconLoader->loadIcon( m_strIcon, _group, _force_size, _state, _path, true );
00292       if (!pixmap.isNull() ) return pixmap;
00293 
00294       iconLoader->addExtraDesktopThemes();
00295   }
00296 
00297   return iconLoader->loadIcon( m_strIcon, _group, _force_size, _state, _path );
00298 }
00299 
00300 void KService::load( QDataStream& s )
00301 {
00302   // dummies are here because of fields that were removed, to keep bin compat.
00303   // Feel free to re-use, but fields for Applications only (not generic services)
00304   // should rather be added to application.desktop
00305   Q_INT8 def, term, dummy1, dummy2;
00306   Q_INT8 dst, initpref;
00307   QString dummyStr1, dummyStr2;
00308   int dummyI1, dummyI2;
00309   Q_UINT32 dummyUI32;
00310 
00311   // WARNING: IN KDE 3.x THIS NEEDS TO REMAIN COMPATIBLE WITH KDE 2.x!
00312   // !! This data structure should remain binary compatible at all times !!
00313   // You may add new fields at the end. Make sure to update the version
00314   // number in ksycoca.h
00315   s >> m_strType >> m_strName >> m_strExec >> m_strIcon
00316     >> term >> m_strTerminalOptions
00317     >> m_strPath >> m_strComment >> m_lstServiceTypes >> def >> m_mapProps
00318     >> m_strLibrary >> dummyI1 >> dummyI2
00319     >> dst
00320     >> m_strDesktopEntryName
00321     >> dummy1 >> dummyStr1 >> initpref >> dummyStr2 >> dummy2
00322     >> m_lstKeywords >> m_strInit >> dummyUI32 >> m_strGenName
00323     >> d->categories >> d->menuId;
00324 
00325   m_bAllowAsDefault = def;
00326   m_bTerminal = term;
00327   m_DCOPServiceType = (DCOPServiceType_t) dst;
00328   m_initialPreference = initpref;
00329 
00330   m_bValid = true;
00331 }
00332 
00333 void KService::save( QDataStream& s )
00334 {
00335   KSycocaEntry::save( s );
00336   Q_INT8 def = m_bAllowAsDefault, initpref = m_initialPreference;
00337   Q_INT8 term = m_bTerminal;
00338   Q_INT8 dst = (Q_INT8) m_DCOPServiceType;
00339   Q_INT8 dummy1 = 0, dummy2 = 0; // see ::load
00340   QString dummyStr1, dummyStr2;
00341   int dummyI1 = 0, dummyI2 = 0;
00342   Q_UINT32 dummyUI32 = 0;
00343 
00344   // WARNING: IN KDE 3.x THIS NEEDS TO REMAIN COMPATIBLE WITH KDE 2.x!
00345   // !! This data structure should remain binary compatible at all times !!
00346   // You may add new fields at the end. Make sure to update the version
00347   // number in ksycoca.h
00348   s << m_strType << m_strName << m_strExec << m_strIcon
00349     << term << m_strTerminalOptions
00350     << m_strPath << m_strComment << m_lstServiceTypes << def << m_mapProps
00351     << m_strLibrary << dummyI1 << dummyI2
00352     << dst
00353     << m_strDesktopEntryName
00354     << dummy1 << dummyStr1 << initpref << dummyStr2 << dummy2
00355     << m_lstKeywords << m_strInit << dummyUI32 << m_strGenName
00356     << d->categories << d->menuId;
00357 }
00358 
00359 bool KService::hasServiceType( const QString& _servicetype ) const
00360 {
00361   if (!m_bValid) return false; // safety test
00362 
00363   //kdDebug(7012) << "Testing " << m_strDesktopEntryName << " for " << _servicetype << endl;
00364 
00365   KMimeType::Ptr mimePtr = KMimeType::mimeType( _servicetype );
00366   if ( mimePtr && mimePtr == KMimeType::defaultMimeTypePtr() )
00367       mimePtr = 0;
00368 
00369   bool isNumber;
00370   // For each service type we are associated with, if it doesn't
00371   // match then we try its parent service types.
00372   QStringList::ConstIterator it = m_lstServiceTypes.begin();
00373   for( ; it != m_lstServiceTypes.end(); ++it )
00374   {
00375       (*it).toInt(&isNumber);
00376       if (isNumber)
00377          continue;
00378       //kdDebug(7012) << "    has " << (*it) << endl;
00379       KServiceType::Ptr ptr = KServiceType::serviceType( *it );
00380       if ( ptr && ptr->inherits( _servicetype ) )
00381           return true;
00382 
00383       // The mimetype inheritance ("is also") works the other way.
00384       // e.g. if we're looking for a handler for mimePtr==smb-workgroup
00385       // then a handler for inode/directory is ok.
00386       if ( mimePtr && mimePtr->is( *it ) )
00387           return true;
00388   }
00389   return false;
00390 }
00391 
00392 int KService::initialPreferenceForMimeType( const QString& mimeType ) const
00393 {
00394   if (!m_bValid) return 0; // safety test
00395 
00396   bool isNumber;
00397 
00398   // For each service type we are associated with
00399   QStringList::ConstIterator it = m_lstServiceTypes.begin();
00400   for( ; it != m_lstServiceTypes.end(); ++it )
00401   {
00402       (*it).toInt(&isNumber);
00403       if (isNumber)
00404          continue;
00405       //kdDebug(7012) << "    has " << (*it) << endl;
00406       KServiceType::Ptr ptr = KServiceType::serviceType( *it );
00407       if ( !ptr || !ptr->inherits( mimeType ) )
00408           continue;
00409 
00410       int initalPreference = m_initialPreference;
00411       ++it;
00412       if (it != m_lstServiceTypes.end())
00413       {
00414          int i = (*it).toInt(&isNumber);
00415          if (isNumber)
00416             initalPreference = i;
00417       }
00418       return initalPreference;
00419   }
00420 
00421   KMimeType::Ptr mimePtr = KMimeType::mimeType( mimeType );
00422   if ( mimePtr && mimePtr == KMimeType::defaultMimeTypePtr() )
00423       mimePtr = 0;
00424 
00425   // Try its parent service types.
00426   it = m_lstServiceTypes.begin();
00427   for( ; it != m_lstServiceTypes.end(); ++it )
00428   {
00429       (*it).toInt(&isNumber);
00430       if (isNumber)
00431          continue;
00432 
00433       // The mimetype inheritance ("is also") works the other way.
00434       // e.g. if we're looking for a handler for mimePtr==smb-workgroup
00435       // then a handler for inode/directory is ok.
00436       if ( !mimePtr || !mimePtr->is( *it ) )
00437           continue;
00438 
00439       int initalPreference = m_initialPreference;
00440       ++it;
00441       if (it != m_lstServiceTypes.end())
00442       {
00443          int i = (*it).toInt(&isNumber);
00444          if (isNumber)
00445             initalPreference = i;
00446       }
00447       return initalPreference;
00448   }
00449   return 0;
00450 }
00451 
00452 class KServiceReadProperty : public KConfigBase
00453 {
00454 public:
00455    KServiceReadProperty(const QString &_key, const QCString &_value)
00456     : key(_key), value(_value) { }
00457 
00458    bool internalHasGroup(const QCString &) const { /*qDebug("hasGroup(const QCString &)");*/ return false; }
00459 
00460    QStringList groupList() const { return QStringList(); }
00461 
00462    QMap<QString,QString> entryMap(const QString &) const
00463       { return QMap<QString,QString>(); }
00464 
00465    void reparseConfiguration() { }
00466 
00467    KEntryMap internalEntryMap( const QString &) const { return KEntryMap(); }
00468 
00469    KEntryMap internalEntryMap() const { return KEntryMap(); }
00470 
00471    void putData(const KEntryKey &, const KEntry&, bool) { }
00472 
00473    KEntry lookupData(const KEntryKey &) const
00474    { KEntry entry; entry.mValue = value; return entry; }
00475 protected:
00476    QString key;
00477    QCString value;
00478 };
00479 
00480 QVariant KService::property( const QString& _name) const
00481 {
00482    return property( _name, QVariant::Invalid);
00483 }
00484 
00485 // Return a string QVariant if string isn't null, and invalid variant otherwise
00486 // (the variant must be invalid if the field isn't in the .desktop file)
00487 // This allows trader queries like "exist Library" to work.
00488 static QVariant makeStringVariant( const QString& string )
00489 {
00490     // Using isEmpty here would be wrong.
00491     // Empty is "specified but empty", null is "not specified" (in the .desktop file)
00492     return string.isNull() ? QVariant() : QVariant( string );
00493 }
00494 
00495 QVariant KService::property( const QString& _name, QVariant::Type t ) const
00496 {
00497   if ( _name == "Type" )
00498     return QVariant( m_strType ); // can't be null
00499   else if ( _name == "Name" )
00500     return QVariant( m_strName ); // can't be null
00501   else if ( _name == "Exec" )
00502     return makeStringVariant( m_strExec );
00503   else if ( _name == "Icon" )
00504     return makeStringVariant( m_strIcon );
00505   else if ( _name == "Terminal" )
00506     return QVariant( static_cast<int>(m_bTerminal) );
00507   else if ( _name == "TerminalOptions" )
00508     return makeStringVariant( m_strTerminalOptions );
00509   else if ( _name == "Path" )
00510     return makeStringVariant( m_strPath );
00511   else if ( _name == "Comment" )
00512     return makeStringVariant( m_strComment );
00513   else if ( _name == "GenericName" )
00514     return makeStringVariant( m_strGenName );
00515   else if ( _name == "ServiceTypes" )
00516     return QVariant( m_lstServiceTypes );
00517   else if ( _name == "AllowAsDefault" )
00518     return QVariant( static_cast<int>(m_bAllowAsDefault) );
00519   else if ( _name == "InitialPreference" )
00520     return QVariant( m_initialPreference );
00521   else if ( _name == "Library" )
00522     return makeStringVariant( m_strLibrary );
00523   else if ( _name == "DesktopEntryPath" ) // can't be null
00524     return QVariant( entryPath() );
00525   else if ( _name == "DesktopEntryName")
00526     return QVariant( m_strDesktopEntryName ); // can't be null
00527   else if ( _name == "Categories")
00528     return QVariant( d->categories );
00529   else if ( _name == "Keywords")
00530     return QVariant( m_lstKeywords );
00531 
00532   // Ok we need to convert the property from a QString to its real type.
00533   // Maybe the caller helped us.
00534   if (t == QVariant::Invalid)
00535   {
00536     // No luck, let's ask KServiceTypeFactory what the type of this property
00537     // is supposed to be.
00538     t = KServiceTypeFactory::self()->findPropertyTypeByName(_name);
00539     if (t == QVariant::Invalid)
00540     {
00541       kdDebug(7012) << "Request for unknown property '" << _name << "'\n";
00542       return QVariant(); // Unknown property: Invalid variant.
00543     }
00544   }
00545 
00546   // Then we use a homebuild class based on KConfigBase to convert the QString.
00547   // For some often used property types we do the conversion ourselves.
00548   QMap<QString,QVariant>::ConstIterator it = m_mapProps.find( _name );
00549   if ( (it == m_mapProps.end()) || (!it.data().isValid()))
00550   {
00551      //kdDebug(7012) << "Property not found " << _name << endl;
00552      return QVariant(); // No property set.
00553   }
00554 
00555   switch(t)
00556   {
00557     case QVariant::String:
00558         return it.data();
00559     case QVariant::Bool:
00560     case QVariant::Int:
00561         {
00562            QString aValue = it.data().toString();
00563            int val = 0;
00564            if (aValue == "true" || aValue == "on" || aValue == "yes")
00565               val = 1;
00566            else
00567            {
00568               bool bOK;
00569               val = aValue.toInt( &bOK );
00570               if( !bOK )
00571                  val = 0;
00572            }
00573            if (t == QVariant::Bool)
00574            {
00575                return QVariant((bool)val, 1);
00576            }
00577            return QVariant(val);
00578         }
00579     default:
00580         // All others
00581         KServiceReadProperty ksrp(_name, it.data().toString().utf8());
00582         return ksrp.readPropertyEntry(_name, t);
00583   }
00584 }
00585 
00586 QStringList KService::propertyNames() const
00587 {
00588   QStringList res;
00589 
00590   QMap<QString,QVariant>::ConstIterator it = m_mapProps.begin();
00591   for( ; it != m_mapProps.end(); ++it )
00592     res.append( it.key() );
00593 
00594   res.append( "Type" );
00595   res.append( "Name" );
00596   res.append( "Comment" );
00597   res.append( "GenericName" );
00598   res.append( "Icon" );
00599   res.append( "Exec" );
00600   res.append( "Terminal" );
00601   res.append( "TerminalOptions" );
00602   res.append( "Path" );
00603   res.append( "ServiceTypes" );
00604   res.append( "AllowAsDefault" );
00605   res.append( "InitialPreference" );
00606   res.append( "Library" );
00607   res.append( "DesktopEntryPath" );
00608   res.append( "DesktopEntryName" );
00609   res.append( "Keywords" );
00610   res.append( "Categories" );
00611 
00612   return res;
00613 }
00614 
00615 KService::List KService::allServices()
00616 {
00617   return KServiceFactory::self()->allServices();
00618 }
00619 
00620 KService::Ptr KService::serviceByName( const QString& _name )
00621 {
00622   KService * s = KServiceFactory::self()->findServiceByName( _name );
00623   return KService::Ptr( s );
00624 }
00625 
00626 KService::Ptr KService::serviceByDesktopPath( const QString& _name )
00627 {
00628   KService * s = KServiceFactory::self()->findServiceByDesktopPath( _name );
00629   return KService::Ptr( s );
00630 }
00631 
00632 KService::Ptr KService::serviceByDesktopName( const QString& _name )
00633 {
00634   KService * s = KServiceFactory::self()->findServiceByDesktopName( _name.lower() );
00635   if (!s && !_name.startsWith("kde-"))
00636      s = KServiceFactory::self()->findServiceByDesktopName( "kde-"+_name.lower() );
00637   return KService::Ptr( s );
00638 }
00639 
00640 KService::Ptr KService::serviceByMenuId( const QString& _name )
00641 {
00642   KService * s = KServiceFactory::self()->findServiceByMenuId( _name );
00643   return KService::Ptr( s );
00644 }
00645 
00646 KService::Ptr KService::serviceByStorageId( const QString& _storageId )
00647 {
00648   KService::Ptr service = KService::serviceByMenuId( _storageId );
00649   if (service)
00650      return service;
00651 
00652   service = KService::serviceByDesktopPath(_storageId);
00653   if (service)
00654      return service;
00655 
00656   if (_storageId.startsWith("/") && QFile::exists(_storageId))
00657      return new KService(_storageId);
00658 
00659   QString tmp = _storageId;
00660   tmp = tmp.mid(tmp.findRev('/')+1); // Strip dir
00661 
00662   if (tmp.endsWith(".desktop"))
00663      tmp.truncate(tmp.length()-8);
00664 
00665   if (tmp.endsWith(".kdelnk"))
00666      tmp.truncate(tmp.length()-7);
00667 
00668   service = KService::serviceByDesktopName(tmp);
00669 
00670   return service;
00671 }
00672 
00673 KService::List KService::allInitServices()
00674 {
00675   return KServiceFactory::self()->allInitServices();
00676 }
00677 
00678 bool KService::substituteUid() const {
00679   QVariant v = property("X-KDE-SubstituteUID", QVariant::Bool);
00680   return v.isValid() && v.toBool();
00681 }
00682 
00683 QString KService::username() const {
00684   // See also KDesktopFile::tryExec()
00685   QString user;
00686   QVariant v = property("X-KDE-Username", QVariant::String);
00687   user = v.isValid() ? v.toString() : QString::null;
00688   if (user.isEmpty())
00689      user = ::getenv("ADMIN_ACCOUNT");
00690   if (user.isEmpty())
00691      user = "root";
00692   return user;
00693 }
00694 
00695 bool KService::noDisplay() const {
00696   QMap<QString,QVariant>::ConstIterator it = m_mapProps.find( "NoDisplay" );
00697   if ( (it != m_mapProps.end()) && (it.data().isValid()))
00698   {
00699      QString aValue = it.data().toString().lower();
00700      if (aValue == "true" || aValue == "on" || aValue == "yes")
00701         return true;
00702   }
00703 
00704   it = m_mapProps.find( "OnlyShowIn" );
00705   if ( (it != m_mapProps.end()) && (it.data().isValid()))
00706   {
00707      QString aValue = it.data().toString();
00708      QStringList aList = QStringList::split(';', aValue);
00709      if (!aList.contains("KDE"))
00710         return true;
00711   }
00712 
00713   it = m_mapProps.find( "NotShowIn" );
00714   if ( (it != m_mapProps.end()) && (it.data().isValid()))
00715   {
00716      QString aValue = it.data().toString();
00717      QStringList aList = QStringList::split(';', aValue);
00718      if (aList.contains("KDE"))
00719         return true;
00720   }
00721   return false;
00722 }
00723 
00724 QString KService::untranslatedGenericName() const {
00725   QVariant v = property("UntranslatedGenericName", QVariant::String);
00726   return v.isValid() ? v.toString() : QString::null;
00727 }
00728 
00729 QString KService::parentApp() const {
00730   QMap<QString,QVariant>::ConstIterator it = m_mapProps.find( "X-KDE-ParentApp" );
00731   if ( (it == m_mapProps.end()) || (!it.data().isValid()))
00732   {
00733      return QString::null;
00734   }
00735 
00736   return it.data().toString();
00737 }
00738 
00739 bool KService::allowMultipleFiles() const {
00740   // Can we pass multiple files on the command line or do we have to start the application for every single file ?
00741   if ( m_strExec.find( "%F" ) != -1 || m_strExec.find( "%U" ) != -1 ||
00742        m_strExec.find( "%N" ) != -1 || m_strExec.find( "%D" ) != -1 )
00743     return true;
00744   else
00745     return false;
00746 }
00747 
00748 QStringList KService::categories() const
00749 {
00750   return d->categories;
00751 }
00752 
00753 QString KService::menuId() const
00754 {
00755   return d->menuId;
00756 }
00757 
00758 void KService::setMenuId(const QString &menuId)
00759 {
00760   d->menuId = menuId;
00761 }
00762 
00763 QString KService::storageId() const
00764 {
00765   if (!d->menuId.isEmpty())
00766      return d->menuId;
00767   return entryPath();
00768 }
00769 
00770 QString KService::locateLocal()
00771 {
00772   if (d->menuId.isEmpty() || desktopEntryPath().startsWith(".hidden") ||
00773       (!desktopEntryPath().startsWith("/") && d->categories.isEmpty()))
00774      return KDesktopFile::locateLocal(desktopEntryPath());
00775 
00776   return ::locateLocal("xdgdata-apps", d->menuId);
00777 }
00778 
00779 QString KService::newServicePath(bool showInMenu, const QString &suggestedName,
00780                                 QString *menuId, const QStringList *reservedMenuIds)
00781 {
00782    QString base = suggestedName;
00783    if (!showInMenu)
00784      base.prepend("kde-");
00785 
00786    QString result;
00787    for(int i = 1; true; i++)
00788    {
00789       if (i == 1)
00790          result = base + ".desktop";
00791       else
00792          result = base + QString("-%1.desktop").arg(i);
00793 
00794       if (reservedMenuIds && reservedMenuIds->contains(result))
00795          continue;
00796 
00797       // Lookup service by menu-id
00798       KService::Ptr s = serviceByMenuId(result);
00799       if (s)
00800          continue;
00801 
00802       if (showInMenu)
00803       {
00804          if (!locate("xdgdata-apps", result).isEmpty())
00805             continue;
00806       }
00807       else
00808       {
00809          QString file = result.mid(4); // Strip "kde-"
00810          if (!locate("apps", ".hidden/"+file).isEmpty())
00811             continue;
00812       }
00813 
00814       break;
00815    }
00816    if (menuId)
00817       *menuId = result;
00818 
00819    if (showInMenu)
00820    {
00821        return ::locateLocal("xdgdata-apps", result);
00822    }
00823    else
00824    {
00825        QString file = result.mid(4); // Strip "kde-"
00826        return ::locateLocal("apps", ".hidden/"+file);
00827    }
00828 }
00829 
00830 
00831 void KService::virtual_hook( int id, void* data )
00832 { KSycocaEntry::virtual_hook( id, data ); }
00833 
00834 
00835 void KService::rebuildKSycoca(QWidget *parent)
00836 {
00837   KServiceProgressDialog dlg(parent, "ksycoca_progress",
00838                       i18n("Updating System Configuration"),
00839                       i18n("Updating system configuration."));
00840 
00841   QByteArray data;
00842   DCOPClient *client = kapp->dcopClient();
00843 
00844   int result = client->callAsync("kded", "kbuildsycoca", "recreate()",
00845                data, &dlg, SLOT(slotFinished()));
00846 
00847   if (result)
00848   {
00849      dlg.exec();
00850   }
00851 }
00852 
00853 KServiceProgressDialog::KServiceProgressDialog(QWidget *parent, const char *name,
00854                           const QString &caption, const QString &text)
00855  : KProgressDialog(parent, name, caption, text, true)
00856 {
00857   connect(&m_timer, SIGNAL(timeout()), this, SLOT(slotProgress()));
00858   progressBar()->setTotalSteps(20);
00859   m_timeStep = 700;
00860   m_timer.start(m_timeStep);
00861   setAutoClose(false);
00862 }
00863 
00864 void
00865 KServiceProgressDialog::slotProgress()
00866 {
00867   int p = progressBar()->progress();
00868   if (p == 18)
00869   {
00870      progressBar()->reset();
00871      progressBar()->setProgress(1);
00872      m_timeStep = m_timeStep * 2;
00873      m_timer.start(m_timeStep);
00874   }
00875   else
00876   {
00877      progressBar()->setProgress(p+1);
00878   }
00879 }
00880 
00881 void
00882 KServiceProgressDialog::slotFinished()
00883 {
00884   progressBar()->setProgress(20);
00885   m_timer.stop();
00886   QTimer::singleShot(1000, this, SLOT(close()));
00887 }
00888 
00889 #include "kservice_p.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Aug 4 05:25:35 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003