kuserprofile.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kuserprofile.h"
00020 #include "kservice.h"
00021 #include "kservicetype.h"
00022 #include "kservicetypefactory.h"
00023
00024 #include <kconfig.h>
00025 #include <kapplication.h>
00026 #include <kglobal.h>
00027 #include <kdebug.h>
00028
00029 #include <qtl.h>
00030
00031 template class QPtrList<KServiceTypeProfile>;
00032
00033
00034
00035
00036
00037
00038
00039 QPtrList<KServiceTypeProfile>* KServiceTypeProfile::s_lstProfiles = 0L;
00040 bool KServiceTypeProfile::s_configurationMode = false;
00041
00042 void KServiceTypeProfile::initStatic()
00043 {
00044 if ( s_lstProfiles )
00045 return;
00046
00047
00048 (void) KServiceTypeFactory::self();
00049
00050 s_lstProfiles = new QPtrList<KServiceTypeProfile>;
00051
00052 KConfig config( "profilerc", true, false);
00053
00054 static const QString & defaultGroup = KGlobal::staticQString("<default>");
00055
00056 QStringList tmpList = config.groupList();
00057 for (QStringList::Iterator aIt = tmpList.begin();
00058 aIt != tmpList.end(); ++aIt) {
00059 if ( *aIt == defaultGroup )
00060 continue;
00061
00062 config.setGroup( *aIt );
00063
00064 QString appDesktopPath = config.readEntry( "Application" );
00065
00066 KService::Ptr pService = KService::serviceByDesktopPath( appDesktopPath );
00067
00068 if ( pService ) {
00069 QString application = pService->name();
00070 QString type = config.readEntry( "ServiceType" );
00071 QString type2 = config.readEntry( "GenericServiceType" );
00072 if (type2.isEmpty())
00073 type2 = (pService->type() == "Application") ? "Application" : "KParts/ReadOnlyPart";
00074 int pref = config.readNumEntry( "Preference" );
00075
00076 if ( !type.isEmpty() )
00077 {
00078 KServiceTypeProfile* p =
00079 KServiceTypeProfile::serviceTypeProfile( type, type2 );
00080
00081 if ( !p )
00082 p = new KServiceTypeProfile( type, type2 );
00083
00084 bool allow = config.readBoolEntry( "AllowAsDefault" );
00085
00086 p->addService( application, pref, allow );
00087 }
00088 }
00089 }
00090 }
00091
00092
00093 KServiceTypeProfile::OfferList KServiceTypeProfile::offers( const QString& _servicetype, const QString& _genericServiceType )
00094 {
00095 OfferList offers;
00096 QStringList serviceList;
00097 kdDebug(7014) << "KServiceTypeProfile::offers( " << _servicetype << "," << _genericServiceType << " )" << endl;
00098
00099
00100
00101 if ( _genericServiceType.isEmpty() )
00102 {
00103 initStatic();
00104
00105
00106 QPtrListIterator<KServiceTypeProfile> it( *s_lstProfiles );
00107 for( ; it.current(); ++it )
00108 if ( it.current()->m_strServiceType == _servicetype )
00109 {
00110 offers += it.current()->offers();
00111 }
00112
00113 }
00114 else
00115 {
00116 KServiceTypeProfile* profile = serviceTypeProfile( _servicetype, _genericServiceType );
00117 if ( profile )
00118 {
00119
00120 offers += profile->offers();
00121 }
00122 else
00123 {
00124
00125 profile = serviceTypeProfile( _genericServiceType, _servicetype );
00126 if ( profile )
00127 {
00128
00129 offers += profile->offers();
00130 }
00131 }
00132 }
00133
00134
00135 OfferList::Iterator itOffers = offers.begin();
00136 for( ; itOffers != offers.end(); ++itOffers )
00137 serviceList += (*itOffers).service()->desktopEntryPath();
00138
00139
00140
00141
00142
00143 KService::List list = KServiceType::offers( _servicetype );
00144
00145 QValueListIterator<KService::Ptr> it = list.begin();
00146 for( ; it != list.end(); ++it )
00147 {
00148 if (_genericServiceType.isEmpty() || (*it)->hasServiceType( _genericServiceType ))
00149 {
00150
00151 if ( serviceList.find( (*it)->desktopEntryPath() ) == serviceList.end() )
00152 {
00153 bool allow = (*it)->allowAsDefault();
00154 KServiceOffer o( (*it), (*it)->initialPreference(), allow );
00155 offers.append( o );
00156
00157 }
00158
00159
00160 }
00161 }
00162
00163 qBubbleSort( offers );
00164
00165 #if 0
00166
00167 kdDebug(7014) << "Sorted list:" << endl;
00168 OfferList::Iterator itOff = offers.begin();
00169 for( ; itOff != offers.end(); ++itOff )
00170 kdDebug(7014) << (*itOff).service()->name() << " allow-as-default=" << (*itOff).allowAsDefault() << endl;
00171 #endif
00172
00173 kdDebug(7014) << "Returning " << offers.count() << " offers" << endl;
00174 return offers;
00175 }
00176
00177 KServiceTypeProfile::KServiceTypeProfile( const QString& _servicetype, const QString& _genericServiceType )
00178 {
00179 initStatic();
00180
00181 m_strServiceType = _servicetype;
00182 m_strGenericServiceType = _genericServiceType;
00183
00184 s_lstProfiles->append( this );
00185 }
00186
00187 KServiceTypeProfile::~KServiceTypeProfile()
00188 {
00189 Q_ASSERT( s_lstProfiles );
00190
00191 s_lstProfiles->removeRef( this );
00192 }
00193
00194 void KServiceTypeProfile::addService( const QString& _service,
00195 int _preference, bool _allow_as_default )
00196 {
00197 m_mapServices[ _service ].m_iPreference = _preference;
00198 m_mapServices[ _service ].m_bAllowAsDefault = _allow_as_default;
00199 }
00200
00201 int KServiceTypeProfile::preference( const QString& _service ) const
00202 {
00203 QMap<QString,Service>::ConstIterator it = m_mapServices.find( _service );
00204 if ( it == m_mapServices.end() )
00205 return 0;
00206
00207 return it.data().m_iPreference;
00208 }
00209
00210 bool KServiceTypeProfile::allowAsDefault( const QString& _service ) const
00211 {
00212
00213 KService::Ptr s = KService::serviceByName( _service );
00214 if ( s && !s->allowAsDefault() )
00215 return false;
00216
00217
00218 QMap<QString,Service>::ConstIterator it = m_mapServices.find( _service );
00219 if ( it == m_mapServices.end() )
00220 return 0;
00221
00222 return it.data().m_bAllowAsDefault;
00223 }
00224
00225 KServiceTypeProfile* KServiceTypeProfile::serviceTypeProfile( const QString& _servicetype, const QString& _genericServiceType )
00226 {
00227 initStatic();
00228 static const QString& app_str = KGlobal::staticQString("Application");
00229
00230 const QString &_genservicetype = ((!_genericServiceType.isEmpty()) ? _genericServiceType : app_str);
00231
00232 QPtrListIterator<KServiceTypeProfile> it( *s_lstProfiles );
00233 for( ; it.current(); ++it )
00234 if (( it.current()->m_strServiceType == _servicetype ) &&
00235 ( it.current()->m_strGenericServiceType == _genservicetype))
00236 return it.current();
00237
00238 return 0;
00239 }
00240
00241
00242 KServiceTypeProfile::OfferList KServiceTypeProfile::offers() const
00243 {
00244 OfferList offers;
00245
00246 kdDebug(7014) << "KServiceTypeProfile::offers serviceType=" << m_strServiceType << " genericServiceType=" << m_strGenericServiceType << endl;
00247 KService::List list = KServiceType::offers( m_strServiceType );
00248 QValueListIterator<KService::Ptr> it = list.begin();
00249 for( ; it != list.end(); ++it )
00250 {
00251
00252 if ( m_strGenericServiceType.isEmpty() || (*it)->hasServiceType( m_strGenericServiceType ) )
00253 {
00254
00255 QMap<QString,Service>::ConstIterator it2 = m_mapServices.find( (*it)->name() );
00256
00257 if( it2 != m_mapServices.end() )
00258 {
00259
00260 if ( it2.data().m_iPreference > 0 ) {
00261 bool allow = (*it)->allowAsDefault();
00262 if ( allow )
00263 allow = it2.data().m_bAllowAsDefault;
00264 KServiceOffer o( (*it), it2.data().m_iPreference, allow );
00265 offers.append( o );
00266 }
00267 }
00268 else
00269 {
00270
00271 KServiceOffer o( (*it), 1, (*it)->allowAsDefault() );
00272 offers.append( o );
00273 }
00274 }
00275
00276 }
00277
00278 qBubbleSort( offers );
00279
00280
00281 return offers;
00282 }
00283
00284 KService::Ptr KServiceTypeProfile::preferredService( const QString & _serviceType, const QString & _genericServiceType )
00285 {
00286 OfferList lst = offers( _serviceType, _genericServiceType );
00287
00288 OfferList::Iterator itOff = lst.begin();
00289
00290
00291
00292 if( itOff != lst.end() && (*itOff).allowAsDefault() )
00293 return (*itOff).service();
00294
00295 kdDebug(7014) << "No offers, or none allowed as default" << endl;
00296 return 0L;
00297 }
00298
00299
00300
00301
00302
00303
00304
00305 KServiceOffer::KServiceOffer()
00306 {
00307 m_iPreference = -1;
00308 }
00309
00310 KServiceOffer::KServiceOffer( const KServiceOffer& _o )
00311 {
00312 m_pService = _o.m_pService;
00313 m_iPreference = _o.m_iPreference;
00314 m_bAllowAsDefault = _o.m_bAllowAsDefault;
00315 }
00316
00317 KServiceOffer::KServiceOffer( KService::Ptr _service, int _pref, bool _default )
00318 {
00319 m_pService = _service;
00320 m_iPreference = _pref;
00321 m_bAllowAsDefault = _default;
00322 }
00323
00324
00325 bool KServiceOffer::operator< ( const KServiceOffer& _o ) const
00326 {
00327
00328 if ( _o.m_bAllowAsDefault && !m_bAllowAsDefault )
00329 return false;
00330 if ( !_o.m_bAllowAsDefault && m_bAllowAsDefault )
00331 return true;
00332
00333
00334
00335 return _o.m_iPreference < m_iPreference;
00336 }
This file is part of the documentation for kdelibs Version 3.1.5.