• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kservicetypeprofile.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE libraries
00002  *  Copyright (C) 1999 Torben Weis <weis@kde.org>
00003  *  Copyright (C) 2006 David Faure <faure@kde.org>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation;
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
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 // servicetype -> profile
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     // Make sure that a KServiceTypeFactory gets created.
00063     (void) KServiceTypeFactory::self();
00064 
00065     // Read the service type profiles from servicetype_profilerc (new in kde4)
00066     // See writeServiceTypeProfile for a description of the file format.
00067     // ### Since this new format names groups after servicetypes maybe we can even
00068     // avoid doing any init upfront, and just look up the group when asked...
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             //kDebug(7014) << "KServiceTypeProfile::initStatic adding service " << serviceId << " to profile for " << type << " with preference " << pref;
00088             p->addService( serviceId, pref );
00089         }
00090     }
00091 }
00092 
00093 //static
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         //kDebug(7014) << "KServiceTypeProfile::offers considering " << servPtr->storageId();
00124         // Look into the profile (if there's one), to find this service's preference.
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                 //kDebug(7014) << "found in mapServices pref=" << pref;
00133                 if ( pref > 0 ) { // 0 disables the service
00134                     offers.append( KServiceOffer( servPtr, pref, 0, servPtr->allowAsDefault() ) );
00135                 }
00136                 foundInProfile = true;
00137             }
00138         }
00139         if ( !foundInProfile )
00140         {
00141             // This offer isn't in the profile
00142             // This can be because we have no profile at all, or because the
00143             // services have been installed after the profile was written,
00144             // but it's also the case for any service that's neither App nor ReadOnlyPart, e.g. RenameDlg/Plugin
00145             //kDebug(7014) << "not found in mapServices. Appending.";
00146 
00147             // If there's a profile, we use 0 as the preference to ensure new apps don't take over existing apps (which default to 1)
00148             offers.append( KServiceOffer( servPtr,
00149                                           profile ? 0 : (*it).preference(),
00150                                           0,
00151                                           servPtr->allowAsDefault() ) );
00152         }
00153     }
00154 
00155     qStableSort( offers );
00156 
00157     //kDebug(7014) << "KServiceTypeProfile::offers returning " << offers.count() << " offers";
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      * [ServiceType]
00173      * NumEntries=3
00174      * Entry0_Service=serv.desktop
00175      * Entry0_Preference=10
00176      * Entry1_Service=otherserv.desktop
00177      * Entry1_Preference=5
00178      * Entry2_Service=broken_service.desktop
00179      * Entry2_Preference=0
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     // Drop the whole cache...
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 }

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal