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