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