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
#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->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 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
QMap<QString,Service>::ConstIterator it = m_mapServices.find( _service );
00211
if ( it == m_mapServices.end() )
00212
return 0;
00213
00214
return it.data().m_iPreference;
00215 }
00216
00217 bool KServiceTypeProfile::allowAsDefault(
const QString& _service )
const
00218
{
00219
00220
KService::Ptr s = KService::serviceByName( _service );
00221
if ( s && !s->allowAsDefault() )
00222
return false;
00223
00224
00225
QMap<QString,Service>::ConstIterator it = m_mapServices.find( _service );
00226
if ( it == m_mapServices.end() )
00227
return 0;
00228
00229
return it.data().m_bAllowAsDefault;
00230 }
00231
00232 KServiceTypeProfile*
KServiceTypeProfile::serviceTypeProfile(
const QString& _servicetype,
const QString& _genericServiceType )
00233 {
00234 initStatic();
00235
static const QString& app_str = KGlobal::staticQString(
"Application");
00236
00237
const QString &_genservicetype = ((!_genericServiceType.isEmpty()) ? _genericServiceType : app_str);
00238
00239
QPtrListIterator<KServiceTypeProfile> it( *s_lstProfiles );
00240
for( ; it.current(); ++it )
00241
if (( it.current()->m_strServiceType == _servicetype ) &&
00242 ( it.current()->m_strGenericServiceType == _genservicetype))
00243
return it.current();
00244
00245
return 0;
00246 }
00247
00248
00249 KServiceTypeProfile::OfferList KServiceTypeProfile::offers()
const
00250
{
00251
OfferList offers;
00252
00253
kdDebug(7014) <<
"KServiceTypeProfile::offers serviceType=" << m_strServiceType <<
" genericServiceType=" << m_strGenericServiceType <<
endl;
00254
KService::List list = KServiceType::offers( m_strServiceType );
00255
QValueListIterator<KService::Ptr> it = list.begin();
00256
for( ; it != list.end(); ++it )
00257 {
00258
00259
if ( m_strGenericServiceType.isEmpty() || (*it)->hasServiceType( m_strGenericServiceType ) )
00260 {
00261
00262
QMap<QString,Service>::ConstIterator it2 = m_mapServices.find( (*it)->name() );
00263
00264
if( it2 != m_mapServices.end() )
00265 {
00266
00267
if ( it2.data().m_iPreference > 0 ) {
00268
bool allow = (*it)->allowAsDefault();
00269
if ( allow )
00270 allow = it2.data().m_bAllowAsDefault;
00271
KServiceOffer o( (*it), it2.data().m_iPreference, allow );
00272 offers.append( o );
00273 }
00274 }
00275
else
00276 {
00277
00278
00279
KServiceOffer o( (*it), 0, (*it)->allowAsDefault() );
00280 offers.append( o );
00281 }
00282 }
00283
00284 }
00285
00286 qBubbleSort( offers );
00287
00288
00289
return offers;
00290 }
00291
00292 KService::Ptr KServiceTypeProfile::preferredService(
const QString & _serviceType,
const QString & _genericServiceType )
00293 {
00294
OfferList lst =
offers( _serviceType, _genericServiceType );
00295
00296 OfferList::Iterator itOff = lst.begin();
00297
00298
00299
00300
if( itOff != lst.end() && (*itOff).allowAsDefault() )
00301
return (*itOff).service();
00302
00303
00304
return 0L;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313 KServiceOffer::KServiceOffer()
00314 {
00315 m_iPreference = -1;
00316 }
00317
00318 KServiceOffer::KServiceOffer(
const KServiceOffer& _o )
00319 {
00320 m_pService = _o.
m_pService;
00321 m_iPreference = _o.
m_iPreference;
00322 m_bAllowAsDefault = _o.
m_bAllowAsDefault;
00323 }
00324
00325 KServiceOffer::KServiceOffer(
KService::Ptr _service,
int _pref,
bool _default )
00326 {
00327 m_pService = _service;
00328 m_iPreference = _pref;
00329 m_bAllowAsDefault = _default;
00330 }
00331
00332
00333 bool KServiceOffer::operator< (
const KServiceOffer& _o )
const
00334
{
00335
00336
if ( _o.
m_bAllowAsDefault && !m_bAllowAsDefault )
00337
return false;
00338
if ( !_o.
m_bAllowAsDefault && m_bAllowAsDefault )
00339
return true;
00340
00341
00342
00343
return _o.
m_iPreference < m_iPreference;
00344 }