KDECore
kservicetypeprofile.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 "kservicetypeprofile.h"
00021 #include "kservicetypeprofile_p.h"
00022 #include <QMutex>
00023 #include "kservice.h"
00024 #include "kservicetype.h"
00025 #include "kservicetypefactory.h"
00026 #include "kservicefactory.h"
00027
00028 #include <kconfig.h>
00029 #include <kstandarddirs.h>
00030 #include <kdebug.h>
00031 #include <kconfiggroup.h>
00032
00033 #include <QtCore/QHash>
00034 #include <QtAlgorithms>
00035
00036
00037 class KServiceTypeProfiles : public QHash<QString, KServiceTypeProfileEntry *>
00038 {
00039 public:
00040 KServiceTypeProfiles() { m_parsed = false; ensureParsed(); }
00041 ~KServiceTypeProfiles() { clear(); }
00042 void clear() {
00043 QMutexLocker lock(&m_mutex);
00044 qDeleteAll( *this );
00045 QHash<QString, KServiceTypeProfileEntry *>::clear();
00046 m_parsed = false;
00047 }
00048 bool hasProfile(const QString& serviceType) {
00049 QMutexLocker lock(&m_mutex);
00050 ensureParsed();
00051 return contains(serviceType);
00052 }
00053 void ensureParsed();
00054 QMutex m_mutex;
00055 private:
00056 bool m_parsed;
00057 };
00058
00059
00060 K_GLOBAL_STATIC(KServiceTypeProfiles, s_serviceTypeProfiles)
00061
00062 static bool s_configurationMode = false;
00063
00064 void KServiceTypeProfiles::ensureParsed()
00065 {
00066 if (m_parsed)
00067 return;
00068 m_parsed = true;
00069
00070
00071 (void) KServiceTypeFactory::self();
00072
00073
00074
00075
00076
00077 KConfig configFile( "servicetype_profilerc", KConfig::NoGlobals );
00078 const QStringList tmpList = configFile.groupList();
00079 for (QStringList::const_iterator aIt = tmpList.begin();
00080 aIt != tmpList.end(); ++aIt) {
00081 const QString type = *aIt;
00082 KConfigGroup config(&configFile, type);
00083 const int count = config.readEntry( "NumberOfEntries", 0 );
00084 KServiceTypeProfileEntry* p = this->value( type, 0 );
00085 if ( !p ) {
00086 p = new KServiceTypeProfileEntry();
00087 this->insert( type, p );
00088 }
00089
00090 for ( int i = 0; i < count; ++i ) {
00091 const QString num = QString::number(i);
00092 const QString serviceId = config.readEntry( "Entry" + num + "_Service", QString() );
00093 Q_ASSERT(!serviceId.isEmpty());
00094 const int pref = config.readEntry( "Entry" + num + "_Preference", 0 );
00095
00096 p->addService( serviceId, pref );
00097 }
00098 }
00099 }
00100
00101
00102 void KServiceTypeProfile::clearCache()
00103 {
00104 if (s_serviceTypeProfiles.exists())
00105 s_serviceTypeProfiles->clear();
00106 }
00107
00115 namespace KServiceTypeProfile {
00116 KServiceOfferList sortServiceTypeOffers( const KServiceOfferList& list, const QString& servicetype );
00117 }
00118
00119 KServiceOfferList KServiceTypeProfile::sortServiceTypeOffers( const KServiceOfferList& list, const QString& serviceType )
00120 {
00121 QMutexLocker lock(&s_serviceTypeProfiles->m_mutex);
00122 s_serviceTypeProfiles->ensureParsed();
00123 KServiceTypeProfileEntry* profile = s_serviceTypeProfiles->value(serviceType, 0);
00124
00125 KServiceOfferList offers;
00126
00127 KServiceOfferList::const_iterator it = list.begin();
00128 const KServiceOfferList::const_iterator end = list.end();
00129 for( ; it != end; ++it )
00130 {
00131 const KService::Ptr servPtr = (*it).service();
00132
00133
00134 bool foundInProfile = false;
00135 if ( profile )
00136 {
00137 QMap<QString,int>::ConstIterator it2 = profile->m_mapServices.constFind( servPtr->storageId() );
00138 if( it2 != profile->m_mapServices.constEnd() )
00139 {
00140 const int pref = it2.value();
00141
00142 if ( pref > 0 ) {
00143 offers.append( KServiceOffer( servPtr, pref, 0, servPtr->allowAsDefault() ) );
00144 }
00145 foundInProfile = true;
00146 }
00147 }
00148 if ( !foundInProfile )
00149 {
00150
00151
00152
00153
00154
00155
00156
00157 offers.append( KServiceOffer( servPtr,
00158 profile ? 0 : (*it).preference(),
00159 0,
00160 servPtr->allowAsDefault() ) );
00161 }
00162 }
00163
00164 qStableSort( offers );
00165
00166
00167 return offers;
00168 }
00169
00170 bool KServiceTypeProfile::hasProfile( const QString& serviceType )
00171 {
00172 return s_serviceTypeProfiles->hasProfile(serviceType);
00173 }
00174
00175 void KServiceTypeProfile::writeServiceTypeProfile( const QString& serviceType,
00176 const KService::List& services,
00177 const KService::List& disabledServices )
00178 {
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 KConfig configFile( "servicetype_profilerc", KConfig::SimpleConfig);
00191 configFile.deleteGroup( serviceType );
00192
00193 KConfigGroup config(&configFile, serviceType );
00194 const int count = services.count();
00195 config.writeEntry( "NumberOfEntries", count + disabledServices.count() );
00196 KService::List::ConstIterator servit = services.begin();
00197 int i = 0;
00198 for( ; servit != services.end(); ++servit, ++i ) {
00199 const QString num = QString::number(i);
00200 config.writeEntry( "Entry" + num + "_Service", (*servit)->storageId() );
00201 config.writeEntry( "Entry" + num + "_Preference", count - i );
00202 }
00203 servit = disabledServices.begin();
00204 for( ; servit != disabledServices.end(); ++servit, ++i ) {
00205 const QString num = QString::number(i);
00206 config.writeEntry( "Entry" + num + "_Service", (*servit)->storageId() );
00207 config.writeEntry( "Entry" + num + "_Preference", 0 );
00208 }
00209 configFile.sync();
00210
00211
00212 clearCache();
00213 }
00214
00215 void KServiceTypeProfile::deleteServiceTypeProfile( const QString& serviceType)
00216 {
00217 KConfig config( "servicetype_profilerc", KConfig::SimpleConfig );
00218 config.deleteGroup( serviceType );
00219 config.sync();
00220
00221
00222
00223 if (s_serviceTypeProfiles.exists()) {
00224 delete s_serviceTypeProfiles->take( serviceType );
00225 }
00226 }
00227
00228 void KServiceTypeProfile::setConfigurationMode()
00229 {
00230 s_configurationMode = true;
00231 }
00232
00233 bool KServiceTypeProfile::configurationMode()
00234 {
00235 return s_configurationMode;
00236 }