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   
00051   
00052   
00053   
00054   s_lstProfiles = new QPtrList<KServiceTypeProfile>;
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->name();
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 
00088         bool allow = config.readBoolEntry( "AllowAsDefault" );
00089         
00090         p->addService( application, pref, allow );
00091       }
00092     }
00093   }
00094 }
00095 
00096 
00097 KServiceTypeProfile::OfferList KServiceTypeProfile::offers( const QString& _servicetype, const QString& _genericServiceType )
00098 {
00099     OfferList offers;
00100     QStringList serviceList;
00101     kdDebug(7014) << "KServiceTypeProfile::offers( " << _servicetype << "," << _genericServiceType << " )" << endl;
00102 
00103     
00104     
00105     if ( _genericServiceType.isEmpty() )
00106     {
00107         initStatic();
00108         
00109         
00110         QPtrListIterator<KServiceTypeProfile> it( *s_lstProfiles );
00111         for( ; it.current(); ++it )
00112             if ( it.current()->m_strServiceType == _servicetype )
00113             {
00114                 offers += it.current()->offers();
00115             }
00116         
00117     }
00118     else
00119     {
00120         KServiceTypeProfile* profile = serviceTypeProfile( _servicetype, _genericServiceType );
00121         if ( profile )
00122         {
00123             
00124             offers += profile->offers();
00125         }
00126         else
00127         {
00128             
00129             profile = serviceTypeProfile( _genericServiceType, _servicetype );
00130             if ( profile )
00131             {
00132                 
00133                 offers += profile->offers();
00134             }
00135         }
00136     }
00137 
00138     
00139     OfferList::Iterator itOffers = offers.begin();
00140     for( ; itOffers != offers.end(); ++itOffers )
00141         serviceList += (*itOffers).service()->desktopEntryPath(); 
00142     
00143 
00144     
00145     
00146     
00147     KService::List list = KServiceType::offers( _servicetype );
00148     
00149     QValueListIterator<KService::Ptr> it = list.begin();
00150     for( ; it != list.end(); ++it )
00151     {
00152         if (_genericServiceType.isEmpty()  || (*it)->hasServiceType( _genericServiceType ))
00153         {
00154             
00155             if ( serviceList.find( (*it)->desktopEntryPath() ) == serviceList.end() )
00156             {
00157                 bool allow = (*it)->allowAsDefault();
00158                 KServiceOffer o( (*it), (*it)->initialPreferenceForMimeType(_servicetype), allow );
00159                 offers.append( o );
00160                 
00161             }
00162             
00163             
00164         }
00165     }
00166 
00167     qBubbleSort( offers );
00168 
00169 #if 0
00170     
00171     kdDebug(7014) << "Sorted list:" << endl;
00172     OfferList::Iterator itOff = offers.begin();
00173     for( ; itOff != offers.end(); ++itOff )
00174         kdDebug(7014) << (*itOff).service()->name() << " allow-as-default=" << (*itOff).allowAsDefault() << endl;
00175 #endif
00176 
00177     kdDebug(7014) << "Returning " << offers.count() << " offers" << endl;
00178     return offers;
00179 }
00180 
00181 KServiceTypeProfile::KServiceTypeProfile( const QString& _servicetype, const QString& _genericServiceType )
00182 {
00183   initStatic();
00184 
00185   m_strServiceType = _servicetype;
00186   m_strGenericServiceType = _genericServiceType;
00187 
00188   s_lstProfiles->append( this );
00189 }
00190 
00191 KServiceTypeProfile::~KServiceTypeProfile()
00192 {
00193   Q_ASSERT( s_lstProfiles );
00194 
00195   s_lstProfiles->removeRef( this );
00196 }
00197 
00198 void KServiceTypeProfile::addService( const QString& _service,
00199                       int _preference, bool _allow_as_default )
00200 {
00201   m_mapServices[ _service ].m_iPreference = _preference;
00202   m_mapServices[ _service ].m_bAllowAsDefault = _allow_as_default;
00203 }
00204 
00205 int KServiceTypeProfile::preference( const QString& _service ) const
00206 {
00207   QMap<QString,Service>::ConstIterator it = m_mapServices.find( _service );
00208   if ( it == m_mapServices.end() )
00209     return 0;
00210 
00211   return it.data().m_iPreference;
00212 }
00213 
00214 bool KServiceTypeProfile::allowAsDefault( const QString& _service ) const
00215 {
00216   
00217   KService::Ptr s = KService::serviceByName( _service );
00218   if ( s && !s->allowAsDefault() )
00219     return false;
00220 
00221   
00222   QMap<QString,Service>::ConstIterator it = m_mapServices.find( _service );
00223   if ( it == m_mapServices.end() )
00224     return 0;
00225 
00226   return it.data().m_bAllowAsDefault;
00227 }
00228 
00229 KServiceTypeProfile* KServiceTypeProfile::serviceTypeProfile( const QString& _servicetype, const QString& _genericServiceType )
00230 {
00231   initStatic();
00232   static const QString& app_str = KGlobal::staticQString("Application");
00233 
00234   const QString &_genservicetype  = ((!_genericServiceType.isEmpty()) ? _genericServiceType : app_str);
00235 
00236   QPtrListIterator<KServiceTypeProfile> it( *s_lstProfiles );
00237   for( ; it.current(); ++it )
00238     if (( it.current()->m_strServiceType == _servicetype ) &&
00239         ( it.current()->m_strGenericServiceType == _genservicetype))
00240       return it.current();
00241 
00242   return 0;
00243 }
00244 
00245 
00246 KServiceTypeProfile::OfferList KServiceTypeProfile::offers() const
00247 {
00248   OfferList offers;
00249 
00250   kdDebug(7014) << "KServiceTypeProfile::offers serviceType=" << m_strServiceType << " genericServiceType=" << m_strGenericServiceType << endl;
00251   KService::List list = KServiceType::offers( m_strServiceType );
00252   QValueListIterator<KService::Ptr> it = list.begin();
00253   for( ; it != list.end(); ++it )
00254   {
00255     
00256     if ( m_strGenericServiceType.isEmpty() || (*it)->hasServiceType( m_strGenericServiceType ) )
00257     {
00258       
00259       QMap<QString,Service>::ConstIterator it2 = m_mapServices.find( (*it)->name() );
00260 
00261       if( it2 != m_mapServices.end() )
00262       {
00263         
00264         if ( it2.data().m_iPreference > 0 ) {
00265           bool allow = (*it)->allowAsDefault();
00266           if ( allow )
00267             allow = it2.data().m_bAllowAsDefault;
00268           KServiceOffer o( (*it), it2.data().m_iPreference, allow );
00269           offers.append( o );
00270         }
00271       }
00272       else
00273       {
00274         
00275         KServiceOffer o( (*it), 1, (*it)->allowAsDefault() );
00276         offers.append( o );
00277       }
00278     }
00279 
00280   }
00281 
00282   qBubbleSort( offers );
00283 
00284   
00285   return offers;
00286 }
00287 
00288 KService::Ptr KServiceTypeProfile::preferredService( const QString & _serviceType, const QString & _genericServiceType )
00289 {
00290   OfferList lst = offers( _serviceType, _genericServiceType );
00291 
00292   OfferList::Iterator itOff = lst.begin();
00293   
00294   
00295   
00296   if( itOff != lst.end() && (*itOff).allowAsDefault() )
00297     return (*itOff).service();
00298 
00299   kdDebug(7014) << "No offers, or none allowed as default" << endl;
00300   return 0L;
00301 }
00302 
00303 
00304 
00305 
00306 
00307 
00308 
00309 KServiceOffer::KServiceOffer()
00310 {
00311   m_iPreference = -1;
00312 }
00313 
00314 KServiceOffer::KServiceOffer( const KServiceOffer& _o )
00315 {
00316   m_pService = _o.m_pService;
00317   m_iPreference = _o.m_iPreference;
00318   m_bAllowAsDefault = _o.m_bAllowAsDefault;
00319 }
00320 
00321 KServiceOffer::KServiceOffer( KService::Ptr _service, int _pref, bool _default )
00322 {
00323   m_pService = _service;
00324   m_iPreference = _pref;
00325   m_bAllowAsDefault = _default;
00326 }
00327 
00328 
00329 bool KServiceOffer::operator< ( const KServiceOffer& _o ) const
00330 {
00331   
00332   if ( _o.m_bAllowAsDefault && !m_bAllowAsDefault )
00333     return false; 
00334   if ( !_o.m_bAllowAsDefault && m_bAllowAsDefault )
00335     return true; 
00336  
00337  
00338  
00339   return _o.m_iPreference < m_iPreference;
00340 }
 
This file is part of the documentation for kio Library Version 3.2.0.