• Skip to content
  • Skip to link menu
KDE 4.1 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() { 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     // Make sure that a KServiceTypeFactory gets created.
00061     (void) KServiceTypeFactory::self();
00062 
00063     {
00064         // Read the service type profiles from servicetype_profilerc (new in kde4)
00065         // See writeServiceTypeProfile for a description of the file format.
00066         // ### Since this new format names groups after servicetypes maybe we can even
00067         // avoid doing any init upfront, and just look up the group when asked...
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                 //kDebug(7014) << "KServiceTypeProfile::initStatic adding service " << serviceId << " to profile for " << type << " with preference " << pref;
00087                 p->addService( serviceId, pref );
00088             }
00089         }
00090     }
00091 }
00092 
00093 //static
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         //kDebug(7014) << "KServiceTypeProfile::offers considering " << servPtr->storageId();
00125         // Look into the profile (if there's one), to find this service's preference.
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                 //kDebug(7014) << "found in mapServices pref=" << pref;
00134                 if ( pref > 0 ) { // 0 disables the service
00135                     offers.append( KServiceOffer( servPtr, pref, 0, servPtr->allowAsDefault() ) );
00136                 }
00137                 foundInProfile = true;
00138             }
00139         }
00140         if ( !foundInProfile )
00141         {
00142             // This offer isn't in the profile
00143             // This can be because we have no profile at all, or because the
00144             // services have been installed after the profile was written,
00145             // but it's also the case for any service that's neither App nor ReadOnlyPart, e.g. RenameDlg/Plugin
00146             //kDebug(7014) << "not found in mapServices. Appending.";
00147 
00148             // 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)
00149             offers.append( KServiceOffer( servPtr,
00150                                           profile ? 0 : (*it).preference(),
00151                                           0,
00152                                           servPtr->allowAsDefault() ) );
00153         }
00154     }
00155 
00156     qStableSort( offers );
00157 
00158     //kDebug(7014) << "KServiceTypeProfile::offers returning " << offers.count() << " offers";
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      * [ServiceType]
00174      * NumEntries=3
00175      * Entry0_Service=serv.desktop
00176      * Entry0_Preference=10
00177      * Entry1_Service=otherserv.desktop
00178      * Entry1_Preference=5
00179      * Entry2_Service=broken_service.desktop
00180      * Entry2_Preference=0
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     // Drop the whole cache...
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 }

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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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