KDECore
kservicetype.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kservicetype.h"
00021 #include "kservicetype_p.h"
00022 #include "ksycoca.h"
00023 #include "kservice.h"
00024 #include "kservicetypefactory.h"
00025 #include "kservicefactory.h"
00026 #include "kservicetypeprofile.h"
00027 #include <assert.h>
00028 #include <kdebug.h>
00029 #include <kdesktopfile.h>
00030 #include <kconfiggroup.h>
00031
00032 template QDataStream& operator>> <QString, QVariant>(QDataStream&, QMap<QString, QVariant>&);
00033 template QDataStream& operator<< <QString, QVariant>(QDataStream&, const QMap<QString, QVariant>&);
00034
00035 KServiceType::KServiceType( KServiceTypePrivate &dd, const QString& _name,
00036 const QString& _comment )
00037 : KSycocaEntry(dd)
00038 {
00039 Q_D(KServiceType);
00040 d->m_strName = _name;
00041 d->m_strComment = _comment;
00042 }
00043
00044 KServiceType::KServiceType( KDesktopFile *config )
00045 : KSycocaEntry(*new KServiceTypePrivate(config->fileName()))
00046 {
00047 Q_D(KServiceType);
00048 d->init(config);
00049 }
00050
00051 void
00052 KServiceTypePrivate::init( KDesktopFile *config )
00053 {
00054
00055
00056 KConfigGroup desktopGroup = config->desktopGroup();
00057
00058 m_strName = desktopGroup.readEntry( "MimeType" );
00059
00060
00061 if ( m_strName.isEmpty() ) {
00062 m_strName = desktopGroup.readEntry( "X-KDE-ServiceType" );
00063 }
00064
00065 m_strComment = desktopGroup.readEntry("Comment");
00066 deleted = desktopGroup.readEntry("Hidden", false);
00067
00068
00069
00070 QString sDerived = desktopGroup.readEntry( "X-KDE-Derived" );
00071 m_bDerived = !sDerived.isEmpty();
00072 if ( m_bDerived )
00073 m_mapProps.insert( "X-KDE-Derived", sDerived );
00074
00075 const QStringList tmpList = config->groupList();
00076 QStringList::const_iterator gIt = tmpList.begin();
00077
00078 for( ; gIt != tmpList.end(); ++gIt ) {
00079 if ( (*gIt).startsWith( "Property::" ) ) {
00080 KConfigGroup cg(config, *gIt );
00081 QVariant v = QVariant::nameToType( cg.readEntry( "Type" ).toLatin1().constData() );
00082 v = cg.readEntry( "Value", v );
00083
00084 if ( v.isValid() )
00085 m_mapProps.insert( (*gIt).mid( 10 ), v );
00086 }
00087 }
00088
00089 gIt = tmpList.begin();
00090 for( ; gIt != tmpList.end(); ++gIt ) {
00091 if( (*gIt).startsWith( "PropertyDef::" ) ) {
00092 KConfigGroup cg(config, *gIt);
00093 m_mapPropDefs.insert( (*gIt).mid( 13 ),
00094 QVariant::nameToType( cg.readEntry( "Type" ).toLatin1().constData() ) );
00095 }
00096 }
00097 }
00098
00099 KServiceType::KServiceType( QDataStream& _str, int offset )
00100 : KSycocaEntry(*new KServiceTypePrivate(_str, offset))
00101 {
00102 }
00103
00104 KServiceType::KServiceType( KServiceTypePrivate &dd)
00105 : KSycocaEntry(dd)
00106 {
00107 }
00108
00109 void
00110 KServiceTypePrivate::load( QDataStream& _str )
00111 {
00112 qint8 b;
00113 QString dummy;
00114 _str >> m_strName >> dummy >> m_strComment >> m_mapProps >> m_mapPropDefs
00115 >> b >> m_serviceOffersOffset;
00116 m_bDerived = m_mapProps.contains("X-KDE-Derived");
00117 }
00118
00119 void
00120 KServiceTypePrivate::save( QDataStream& _str )
00121 {
00122 KSycocaEntryPrivate::save( _str );
00123
00124
00125
00126 _str << m_strName << QString() << m_strComment << m_mapProps << m_mapPropDefs
00127 << (qint8) 1 << m_serviceOffersOffset;
00128 }
00129
00130 KServiceType::~KServiceType()
00131 {
00132 }
00133
00134 QString KServiceType::parentServiceType() const
00135 {
00136 const QVariant v = property("X-KDE-Derived");
00137 return v.toString();
00138 }
00139
00140 bool KServiceType::inherits( const QString& servTypeName ) const
00141 {
00142 if ( name() == servTypeName )
00143 return true;
00144 QString st = parentServiceType();
00145 while ( !st.isEmpty() )
00146 {
00147 KServiceType::Ptr ptr = KServiceType::serviceType( st );
00148 if (!ptr) return false;
00149 if ( ptr->name() == servTypeName )
00150 return true;
00151 st = ptr->parentServiceType();
00152 }
00153 return false;
00154 }
00155
00156 QVariant
00157 KServiceTypePrivate::property( const QString& _name ) const
00158 {
00159 QVariant v;
00160
00161 if ( _name == "Name" )
00162 v = QVariant( m_strName );
00163 else if ( _name == "Comment" )
00164 v = QVariant( m_strComment );
00165 else
00166 v = m_mapProps.value( _name );
00167
00168 return v;
00169 }
00170
00171 QStringList
00172 KServiceTypePrivate::propertyNames() const
00173 {
00174 QStringList res = m_mapProps.keys();
00175 res.append( "Name" );
00176 res.append( "Comment" );
00177 return res;
00178 }
00179
00180 QVariant::Type
00181 KServiceType::propertyDef( const QString& _name ) const
00182 {
00183 Q_D(const KServiceType);
00184 return static_cast<QVariant::Type>( d->m_mapPropDefs.value( _name, QVariant::Invalid ) );
00185 }
00186
00187 QStringList
00188 KServiceType::propertyDefNames() const
00189 {
00190 Q_D(const KServiceType);
00191 return d->m_mapPropDefs.keys();
00192 }
00193
00194 KServiceType::Ptr KServiceType::serviceType( const QString& _name )
00195 {
00196 return KServiceTypeFactory::self()->findServiceTypeByName( _name );
00197 }
00198
00199 KServiceType::List KServiceType::allServiceTypes()
00200 {
00201 return KServiceTypeFactory::self()->allServiceTypes();
00202 }
00203
00204 KServiceType::Ptr KServiceType::parentType()
00205 {
00206 Q_D(KServiceType);
00207 if (d->m_parentTypeLoaded)
00208 return d->parentType;
00209
00210 d->m_parentTypeLoaded = true;
00211
00212 const QString parentSt = parentServiceType();
00213 if (parentSt.isEmpty())
00214 return KServiceType::Ptr();
00215
00216 d->parentType = KServiceTypeFactory::self()->findServiceTypeByName( parentSt );
00217 if (!d->parentType)
00218 kWarning(7009) << "'" << entryPath() << "' specifies undefined mimetype/servicetype '"<< parentSt << "'";
00219 return d->parentType;
00220 }
00221
00222 void KServiceType::setServiceOffersOffset( int offset )
00223 {
00224 Q_D(KServiceType);
00225 Q_ASSERT( offset != -1 );
00226 d->m_serviceOffersOffset = offset;
00227 }
00228
00229 int KServiceType::serviceOffersOffset() const
00230 {
00231 Q_D(const KServiceType);
00232 return d->m_serviceOffersOffset;
00233 }
00234
00235 QString KServiceType::comment() const
00236 {
00237 Q_D(const KServiceType);
00238 return d->comment();
00239 }
00240
00241
00242 QString KServiceType::desktopEntryPath() const
00243 {
00244 return entryPath();
00245 }
00246
00247 bool KServiceType::isDerived() const
00248 {
00249 Q_D(const KServiceType);
00250 return d->m_bDerived;
00251 }
00252
00253 QMap<QString,QVariant::Type> KServiceType::propertyDefs() const
00254 {
00255 Q_D(const KServiceType);
00256 return d->m_mapPropDefs;
00257 }