kio Library API Documentation

kservicetype.cpp

00001 /*  This file is part of the KDE libraries
00002  *  Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
00003  *                     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 #include "kservice.h"
00021 #include "ksycoca.h"
00022 #include "kservicetype.h"
00023 #include "kservicetypefactory.h"
00024 #include "kservicefactory.h"
00025 #include "kuserprofile.h"
00026 #include <assert.h>
00027 #include <kdebug.h>
00028 #include <kdesktopfile.h>
00029 
00030 template QDataStream& operator>> <QString, QVariant>(QDataStream&, QMap<QString, QVariant>&);
00031 template QDataStream& operator<< <QString, QVariant>(QDataStream&, const QMap<QString, QVariant>&);
00032 
00033 KServiceType::KServiceType( const QString & _fullpath)
00034  : KSycocaEntry(_fullpath)
00035 {
00036   KDesktopFile config( _fullpath );
00037 
00038   init(&config);
00039 }
00040 
00041 KServiceType::KServiceType( KDesktopFile *config )
00042  : KSycocaEntry(config->filename())
00043 {
00044   init(config);
00045 }
00046 
00047 void
00048 KServiceType::init( KDesktopFile *config)
00049 {
00050   // Is it a mimetype ?
00051   m_strName = config->readEntry( "MimeType" );
00052 
00053   // Or is it a servicetype ?
00054   if ( m_strName.isEmpty() )
00055   {
00056     m_strName = config->readEntry( "X-KDE-ServiceType" );
00057   }
00058 
00059   m_strComment = config->readComment();
00060   m_bDeleted = config->readBoolEntry( "Hidden", false );
00061   m_strIcon = config->readIcon();
00062 
00063   // We store this as property to preserve BC, we can't change that
00064   // because KSycoca needs to remain BC between KDE 2.x and KDE 3.x
00065   QString sDerived = config->readEntry( "X-KDE-Derived" );
00066   m_bDerived = !sDerived.isEmpty();
00067   if ( m_bDerived )
00068     m_mapProps.insert( "X-KDE-Derived", sDerived );
00069 
00070   QStringList tmpList = config->groupList();
00071   QStringList::Iterator gIt = tmpList.begin();
00072 
00073   for( ; gIt != tmpList.end(); ++gIt )
00074   {
00075     if ( (*gIt).find( "Property::" ) == 0 )
00076     {
00077       config->setGroup( *gIt );
00078       QVariant v = config->readPropertyEntry( "Value",
00079                    QVariant::nameToType( config->readEntry( "Type" ).ascii() ) );
00080       if ( v.isValid() )
00081           m_mapProps.insert( (*gIt).mid( 10 ), v );
00082     }
00083   }
00084 
00085   gIt = tmpList.begin();
00086   for( ; gIt != tmpList.end(); ++gIt )
00087   {
00088     if( (*gIt).find( "PropertyDef::" ) == 0 )
00089     {
00090       config->setGroup( *gIt );
00091       m_mapPropDefs.insert( (*gIt).mid( 13 ),
00092                 QVariant::nameToType( config->readEntry( "Type" ).ascii() ) );
00093     }
00094   }
00095 
00096   m_bValid = !m_strName.isEmpty();
00097 }
00098 
00099 KServiceType::KServiceType( const QString & _fullpath, const QString& _type,
00100                             const QString& _icon, const QString& _comment )
00101  : KSycocaEntry(_fullpath)
00102 {
00103   m_strName = _type;
00104   m_strIcon = _icon;
00105   m_strComment = _comment;
00106   m_bValid = !m_strName.isEmpty();
00107 }
00108 
00109 KServiceType::KServiceType( QDataStream& _str, int offset )
00110  : KSycocaEntry( _str, offset )
00111 {
00112   load( _str);
00113 }
00114 
00115 void
00116 KServiceType::load( QDataStream& _str )
00117 {
00118   Q_INT8 b;
00119   _str >> m_strName >> m_strIcon >> m_strComment >> m_mapProps >> m_mapPropDefs
00120        >> b;
00121   m_bValid = b;
00122   m_bDerived = m_mapProps.contains("X-KDE-Derived");
00123 }
00124 
00125 void
00126 KServiceType::save( QDataStream& _str )
00127 {
00128   KSycocaEntry::save( _str );
00129   // !! This data structure should remain binary compatible at all times !!
00130   // You may add new fields at the end. Make sure to update the version
00131   // number in ksycoca.h
00132   _str << m_strName << m_strIcon << m_strComment << m_mapProps << m_mapPropDefs
00133        << (Q_INT8)m_bValid;
00134 }
00135 
00136 KServiceType::~KServiceType()
00137 {
00138 }
00139 
00140 QString KServiceType::parentServiceType() const
00141 {
00142   QVariant v = property("X-KDE-Derived");
00143   return v.toString();
00144 }
00145 
00146 bool KServiceType::inherits( const QString& servTypeName ) const
00147 {
00148   if ( name() == servTypeName )
00149       return true;
00150   QString st = parentServiceType();
00151   while ( !st.isEmpty() )
00152   {
00153       KServiceType::Ptr ptr = KServiceType::serviceType( st );
00154       if (!ptr) return false; //error
00155       if ( ptr->name() == servTypeName )
00156           return true;
00157       st = ptr->parentServiceType();
00158   }
00159   return false;
00160 }
00161 
00162 QVariant
00163 KServiceType::property( const QString& _name ) const
00164 {
00165   QVariant v;
00166 
00167   if ( _name == "Name" )
00168     v = QVariant( m_strName );
00169   else if ( _name == "Icon" )
00170     v = QVariant( m_strIcon );
00171   else if ( _name == "Comment" )
00172     v = QVariant( m_strComment );
00173   else {
00174     QMap<QString,QVariant>::ConstIterator it = m_mapProps.find( _name );
00175     if ( it != m_mapProps.end() )
00176       v = it.data();
00177   }
00178 
00179   return v;
00180 }
00181 
00182 QStringList
00183 KServiceType::propertyNames() const
00184 {
00185   QStringList res;
00186 
00187   QMap<QString,QVariant>::ConstIterator it = m_mapProps.begin();
00188   for( ; it != m_mapProps.end(); ++it )
00189     res.append( it.key() );
00190 
00191   res.append( "Name" );
00192   res.append( "Comment" );
00193   res.append( "Icon" );
00194 
00195   return res;
00196 }
00197 
00198 QVariant::Type
00199 KServiceType::propertyDef( const QString& _name ) const
00200 {
00201   QMap<QString,QVariant::Type>::ConstIterator it = m_mapPropDefs.find( _name );
00202   if ( it == m_mapPropDefs.end() )
00203     return QVariant::Invalid;
00204   return it.data();
00205 }
00206 
00207 QStringList
00208 KServiceType::propertyDefNames() const
00209 {
00210   QStringList l;
00211 
00212   QMap<QString,QVariant::Type>::ConstIterator it = m_mapPropDefs.begin();
00213   for( ; it != m_mapPropDefs.end(); ++it )
00214     l.append( it.key() );
00215 
00216   return l;
00217 }
00218 
00219 KServiceType::Ptr KServiceType::serviceType( const QString& _name )
00220 {
00221   KServiceType * p = KServiceTypeFactory::self()->findServiceTypeByName( _name );
00222   return KServiceType::Ptr( p );
00223 }
00224 
00225 KService::List KServiceType::offers( const QString& _servicetype )
00226 {
00227   KService::List lst;
00228 
00229   // Services associated directly with this servicetype (the normal case)
00230   KServiceType * serv = KServiceTypeFactory::self()->findServiceTypeByName( _servicetype );
00231   if ( serv )
00232     lst += KServiceFactory::self()->offers( serv->offset() );
00233   else
00234     kdWarning(7009) << "KServiceType::offers : servicetype " << _servicetype << " not found" << endl;
00235   bool isAMimeType = serv ? serv->isType( KST_KMimeType ) : false;
00236   delete serv;
00237 
00238   //QValueListIterator<KService::Ptr> it = lst.begin();
00239   //for( ; it != lst.end(); ++it )
00240   //    kdDebug() << (*it).data() << " " << (*it)->name() << endl;
00241 
00242   // Support for all/* is deactivated by KServiceTypeProfile::configurationMode()
00243   // (and makes no sense when querying for an "all" servicetype itself
00244   // nor for non-mimetypes service types)
00245   if ( !KServiceTypeProfile::configurationMode()
00246        && isAMimeType
00247        && _servicetype.left(4) != "all/" )
00248   {
00249     // Support for services associated with "all"
00250     KServiceType * servAll = KServiceTypeFactory::self()->findServiceTypeByName( "all/all" );
00251     if ( servAll )
00252     {
00253         KService::List newOffers = KServiceFactory::self()->offers( servAll->offset() );
00254         // Look if we already have those services from the initial query, to avoid duplicates
00255         QValueListIterator<KService::Ptr> it = newOffers.begin();
00256         for( ; it != newOffers.end(); ++it )
00257         {
00258             bool found = false;
00259             QValueListIterator<KService::Ptr> it2 = lst.begin();
00260             for( ; it2 != lst.end() && !found; ++it2 )
00261                 found = (*it)->desktopEntryPath() == (*it2)->desktopEntryPath();
00262             if ( !found )
00263             {
00264                 (*it)->setInitialPreference( 0 ); // all/* associations are less prioritary
00265                 lst += *it;
00266             }
00267         }
00268         //kdDebug(7009) << "all/all found, got " << newOffers.count() << " more offers" << endl;
00269     }
00270     else
00271       kdWarning(7009) << "KServiceType::offers : servicetype all/all not found" << endl;
00272     delete servAll;
00273 
00274     // Support for services associated with "allfiles"
00275     if ( _servicetype != "inode/directory" && _servicetype != "inode/directory-locked" )
00276     {
00277       KServiceType * servAllFiles = KServiceTypeFactory::self()->findServiceTypeByName( "all/allfiles" );
00278       if ( servAllFiles )
00279       {
00280         KService::List newOffers = KServiceFactory::self()->offers( servAllFiles->offset() );
00281         // Look if we already have those services from the initial query, to avoid duplicates
00282         QValueListIterator<KService::Ptr> it = newOffers.begin();
00283         for( ; it != newOffers.end(); ++it )
00284         {
00285             bool found = false;
00286             QValueListIterator<KService::Ptr> it2 = lst.begin();
00287             for( ; it2 != lst.end() && !found; ++it2 )
00288                 found = (*it)->desktopEntryPath() == (*it2)->desktopEntryPath();
00289             if ( !found )
00290             {
00291                 (*it)->setInitialPreference( 0 ); // all/* associations are less prioritary
00292                 lst += *it;
00293             }
00294         }
00295         //kdDebug(7009) << "all/allfiles found, got " << newOffers.count() << " more offers" << endl;
00296       }
00297       else
00298         kdWarning(7009) << "KServiceType::offers : servicetype all/allfiles not found" << endl;
00299       delete servAllFiles;
00300     }
00301   }
00302 
00303   return lst;
00304 }
00305 
00306 KServiceType::List KServiceType::allServiceTypes()
00307 {
00308   return KServiceTypeFactory::self()->allServiceTypes();
00309 }
00310 
00311 void KServiceType::virtual_hook( int id, void* data )
00312 { KSycocaEntry::virtual_hook( id, data ); }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:32 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001