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