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