KDECore
kservicetypetrader.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2000 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 "kservicetypetrader.h" 00021 00022 #include "ktraderparsetree_p.h" 00023 #include <kservicetypeprofile.h> 00024 #include <kdebug.h> 00025 #include "kservicetype.h" 00026 #include "kservicetypefactory.h" 00027 #include "kservicefactory.h" 00028 00029 using namespace KTraderParse; 00030 00031 // -------------------------------------------------- 00032 00033 namespace KServiceTypeProfile { 00034 KServiceOfferList sortServiceTypeOffers( const KServiceOfferList& list, const QString& servicetype ); 00035 } 00036 00037 KServiceTypeTrader* KServiceTypeTrader::self() 00038 { 00039 K_GLOBAL_STATIC(KServiceTypeTrader, s_globalServiceTypeTrader) 00040 return s_globalServiceTypeTrader; 00041 } 00042 00043 KServiceTypeTrader::KServiceTypeTrader() 00044 : d(0) 00045 { 00046 } 00047 00048 KServiceTypeTrader::~KServiceTypeTrader() 00049 { 00050 } 00051 00052 // shared with KMimeTypeTrader 00053 void KServiceTypeTrader::applyConstraints( KService::List& lst, 00054 const QString& constraint ) 00055 { 00056 if ( lst.isEmpty() || constraint.isEmpty() ) 00057 return; 00058 00059 const ParseTreeBase::Ptr constr = parseConstraints( constraint ); // for ownership 00060 const ParseTreeBase* pConstraintTree = constr.data(); // for speed 00061 00062 if (!constr) { // parse error 00063 lst.clear(); 00064 } else { 00065 // Find all services matching the constraint 00066 // and remove the other ones 00067 KService::List::iterator it = lst.begin(); 00068 while( it != lst.end() ) 00069 { 00070 if ( matchConstraint( pConstraintTree, (*it), lst ) != 1 ) 00071 it = lst.erase( it ); 00072 else 00073 ++it; 00074 } 00075 } 00076 } 00077 00078 #if 0 00079 static void dumpOfferList( const KServiceOfferList& offers ) 00080 { 00081 kDebug(7014) << "Sorted list:"; 00082 OfferList::Iterator itOff = offers.begin(); 00083 for( ; itOff != offers.end(); ++itOff ) 00084 kDebug(7014) << (*itOff).service()->name() << " allow-as-default=" << (*itOff).allowAsDefault() << " preference=" << (*itOff).preference(); 00085 } 00086 #endif 00087 00088 static KServiceOfferList weightedOffers( const QString& serviceType ) 00089 { 00090 //kDebug(7014) << "KServiceTypeTrader::weightedOffers( " << serviceType << " )"; 00091 00092 KServiceType::Ptr servTypePtr = KServiceTypeFactory::self()->findServiceTypeByName( serviceType ); 00093 if ( !servTypePtr ) { 00094 kWarning(7014) << "KServiceTypeTrader: serviceType " << serviceType << " not found"; 00095 return KServiceOfferList(); 00096 } 00097 if ( servTypePtr->serviceOffersOffset() == -1 ) // no offers in ksycoca 00098 return KServiceOfferList(); 00099 00100 // First, get all offers known to ksycoca. 00101 const KServiceOfferList services = KServiceFactory::self()->offers( servTypePtr->offset(), servTypePtr->serviceOffersOffset() ); 00102 00103 const KServiceOfferList offers = KServiceTypeProfile::sortServiceTypeOffers( services, serviceType ); 00104 //kDebug(7014) << "Found profile: " << offers.count() << " offers"; 00105 00106 #if 0 00107 dumpOfferList( offers ); 00108 #endif 00109 00110 return offers; 00111 } 00112 00113 KService::List KServiceTypeTrader::defaultOffers( const QString& serviceType, 00114 const QString& constraint ) const 00115 { 00116 KServiceType::Ptr servTypePtr = KServiceTypeFactory::self()->findServiceTypeByName( serviceType ); 00117 if ( !servTypePtr ) { 00118 kWarning(7014) << "KServiceTypeTrader: serviceType " << serviceType << " not found"; 00119 return KService::List(); 00120 } 00121 if ( servTypePtr->serviceOffersOffset() == -1 ) 00122 return KService::List(); 00123 00124 KService::List lst = 00125 KServiceFactory::self()->serviceOffers( servTypePtr->offset(), servTypePtr->serviceOffersOffset() ); 00126 00127 applyConstraints( lst, constraint ); 00128 00129 //kDebug(7014) << "query for serviceType " << serviceType << constraint 00130 // << " : returning " << lst.count() << " offers" << endl; 00131 return lst; 00132 } 00133 00134 KService::List KServiceTypeTrader::query( const QString& serviceType, 00135 const QString& constraint ) const 00136 { 00137 if ( !KServiceTypeProfile::hasProfile( serviceType ) ) 00138 { 00139 // Fast path: skip the profile stuff if there's none (to avoid kservice->serviceoffer->kservice) 00140 // The ordering according to initial preferences is done by kbuildsycoca 00141 return defaultOffers( serviceType, constraint ); 00142 } 00143 00144 KService::List lst; 00145 // Get all services of this service type. 00146 const KServiceOfferList offers = weightedOffers( serviceType ); 00147 00148 // Now extract only the services; the weighting was only used for sorting. 00149 KServiceOfferList::const_iterator itOff = offers.begin(); 00150 for( ; itOff != offers.end(); ++itOff ) 00151 lst.append( (*itOff).service() ); 00152 00153 applyConstraints( lst, constraint ); 00154 00155 //kDebug(7014) << "query for serviceType " << serviceType << constraint 00156 // << " : returning " << lst.count() << " offers" << endl; 00157 return lst; 00158 } 00159 00160 KService::Ptr KServiceTypeTrader::preferredService( const QString & serviceType ) const 00161 { 00162 const KServiceOfferList offers = weightedOffers( serviceType ); 00163 00164 KServiceOfferList::const_iterator itOff = offers.begin(); 00165 // Look for the first one that is allowed as default. 00166 // Since the allowed-as-default are first anyway, we only have 00167 // to look at the first one to know. 00168 if( itOff != offers.end() && (*itOff).allowAsDefault() ) 00169 return (*itOff).service(); 00170 00171 //kDebug(7014) << "No offers, or none allowed as default"; 00172 return KService::Ptr(); 00173 }
KDE 4.6 API Reference