• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • services
kservicetypetrader.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2000 Torben Weis <weis@kde.org>
3 Copyright (C) 2006 David Faure <faure@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kservicetypetrader.h"
21
22#include "ktraderparsetree_p.h"
23#include <kservicetypeprofile.h>
24#include <kdebug.h>
25#include "kservicetype.h"
26#include "kservicetypefactory.h"
27#include "kservicefactory.h"
28
29using namespace KTraderParse;
30
31// --------------------------------------------------
32
33namespace KServiceTypeProfile {
34 KServiceOfferList sortServiceTypeOffers( const KServiceOfferList& list, const QString& servicetype );
35}
36
37KServiceTypeTrader* KServiceTypeTrader::self()
38{
39 K_GLOBAL_STATIC(KServiceTypeTrader, s_globalServiceTypeTrader)
40 return s_globalServiceTypeTrader;
41}
42
43KServiceTypeTrader::KServiceTypeTrader()
44 : d(0)
45{
46}
47
48KServiceTypeTrader::~KServiceTypeTrader()
49{
50}
51
52// shared with KMimeTypeTrader
53void KServiceTypeTrader::applyConstraints( KService::List& lst,
54 const QString& constraint )
55{
56 if ( lst.isEmpty() || constraint.isEmpty() )
57 return;
58
59 const ParseTreeBase::Ptr constr = parseConstraints( constraint ); // for ownership
60 const ParseTreeBase* pConstraintTree = constr.data(); // for speed
61
62 if (!constr) { // parse error
63 lst.clear();
64 } else {
65 // Find all services matching the constraint
66 // and remove the other ones
67 KService::List::iterator it = lst.begin();
68 while( it != lst.end() )
69 {
70 if ( matchConstraint( pConstraintTree, (*it), lst ) != 1 )
71 it = lst.erase( it );
72 else
73 ++it;
74 }
75 }
76}
77
78#if 0
79static void dumpOfferList( const KServiceOfferList& offers )
80{
81 kDebug(7014) << "Sorted list:";
82 OfferList::Iterator itOff = offers.begin();
83 for( ; itOff != offers.end(); ++itOff )
84 kDebug(7014) << (*itOff).service()->name() << " allow-as-default=" << (*itOff).allowAsDefault() << " preference=" << (*itOff).preference();
85}
86#endif
87
88static KServiceOfferList weightedOffers( const QString& serviceType )
89{
90 //kDebug(7014) << "KServiceTypeTrader::weightedOffers( " << serviceType << " )";
91
92 KServiceType::Ptr servTypePtr = KServiceTypeFactory::self()->findServiceTypeByName( serviceType );
93 if ( !servTypePtr ) {
94 kWarning(7014) << "KServiceTypeTrader: serviceType " << serviceType << " not found";
95 return KServiceOfferList();
96 }
97 if ( servTypePtr->serviceOffersOffset() == -1 ) // no offers in ksycoca
98 return KServiceOfferList();
99
100 // First, get all offers known to ksycoca.
101 const KServiceOfferList services = KServiceFactory::self()->offers( servTypePtr->offset(), servTypePtr->serviceOffersOffset() );
102
103 const KServiceOfferList offers = KServiceTypeProfile::sortServiceTypeOffers( services, serviceType );
104 //kDebug(7014) << "Found profile: " << offers.count() << " offers";
105
106#if 0
107 dumpOfferList( offers );
108#endif
109
110 return offers;
111}
112
113KService::List KServiceTypeTrader::defaultOffers( const QString& serviceType,
114 const QString& constraint ) const
115{
116 KServiceType::Ptr servTypePtr = KServiceTypeFactory::self()->findServiceTypeByName( serviceType );
117 if ( !servTypePtr ) {
118 kWarning(7014) << "KServiceTypeTrader: serviceType " << serviceType << " not found";
119 return KService::List();
120 }
121 if ( servTypePtr->serviceOffersOffset() == -1 )
122 return KService::List();
123
124 KService::List lst =
125 KServiceFactory::self()->serviceOffers( servTypePtr->offset(), servTypePtr->serviceOffersOffset() );
126
127 applyConstraints( lst, constraint );
128
129 //kDebug(7014) << "query for serviceType " << serviceType << constraint
130 // << " : returning " << lst.count() << " offers" << endl;
131 return lst;
132}
133
134KService::List KServiceTypeTrader::query( const QString& serviceType,
135 const QString& constraint ) const
136{
137 if ( !KServiceTypeProfile::hasProfile( serviceType ) )
138 {
139 // Fast path: skip the profile stuff if there's none (to avoid kservice->serviceoffer->kservice)
140 // The ordering according to initial preferences is done by kbuildsycoca
141 return defaultOffers( serviceType, constraint );
142 }
143
144 KService::List lst;
145 // Get all services of this service type.
146 const KServiceOfferList offers = weightedOffers( serviceType );
147
148 // Now extract only the services; the weighting was only used for sorting.
149 KServiceOfferList::const_iterator itOff = offers.begin();
150 for( ; itOff != offers.end(); ++itOff )
151 lst.append( (*itOff).service() );
152
153 applyConstraints( lst, constraint );
154
155 //kDebug(7014) << "query for serviceType " << serviceType << constraint
156 // << " : returning " << lst.count() << " offers" << endl;
157 return lst;
158}
159
160KService::Ptr KServiceTypeTrader::preferredService( const QString & serviceType ) const
161{
162 const KServiceOfferList offers = weightedOffers( serviceType );
163
164 KServiceOfferList::const_iterator itOff = offers.begin();
165 // Look for the first one that is allowed as default.
166 // Since the allowed-as-default are first anyway, we only have
167 // to look at the first one to know.
168 if( itOff != offers.end() && (*itOff).allowAsDefault() )
169 return (*itOff).service();
170
171 //kDebug(7014) << "No offers, or none allowed as default";
172 return KService::Ptr();
173}
KServiceFactory::serviceOffers
KService::List serviceOffers(int serviceTypeOffset, int serviceOffersOffset)
Definition: kservicefactory.cpp:245
KServiceFactory::offers
KServiceOfferList offers(int serviceTypeOffset, int serviceOffersOffset)
Definition: kservicefactory.cpp:209
KServiceFactory::self
static KServiceFactory * self()
Definition: kservicefactory.cpp:81
KServiceTypeFactory::findServiceTypeByName
virtual KServiceType::Ptr findServiceTypeByName(const QString &_name)
Find a service type in the database file (allocates it) Overloaded by KBuildServiceTypeFactory to ret...
Definition: kservicetypefactory.cpp:68
KServiceTypeFactory::self
static KServiceTypeFactory * self()
Definition: kservicetypefactory.cpp:63
KServiceTypeTrader
KDE's trader interface (similar to the CORBA Trader), which provides a way to query the KDE infrastru...
Definition: kservicetypetrader.h:71
KServiceTypeTrader::applyConstraints
static void applyConstraints(KService::List &lst, const QString &constraint)
Definition: kservicetypetrader.cpp:53
KServiceTypeTrader::preferredService
KService::Ptr preferredService(const QString &serviceType) const
Returns the preferred service for serviceType.
Definition: kservicetypetrader.cpp:160
KServiceTypeTrader::defaultOffers
KService::List defaultOffers(const QString &serviceType, const QString &constraint=QString()) const
Returns all offers associated with a given servicetype, IGNORING the user preference.
Definition: kservicetypetrader.cpp:113
KServiceTypeTrader::self
static KServiceTypeTrader * self()
This is a static pointer to the KServiceTypeTrader singleton.
Definition: kservicetypetrader.cpp:37
KServiceTypeTrader::~KServiceTypeTrader
~KServiceTypeTrader()
Standard destructor.
Definition: kservicetypetrader.cpp:48
KServiceTypeTrader::query
KService::List query(const QString &servicetype, const QString &constraint=QString()) const
The main function in the KServiceTypeTrader class.
Definition: kservicetypetrader.cpp:134
KServiceType::serviceOffersOffset
int serviceOffersOffset() const
Definition: kservicetype.cpp:226
KService::Ptr
KSharedPtr< KService > Ptr
Definition: kservice.h:61
KService::List
QList< Ptr > List
Definition: kservice.h:62
KSharedPtr< ParseTreeBase >
KSharedPtr::data
T * data()
Definition: ksharedptr.h:111
KSycocaEntry::offset
int offset() const
Definition: ksycocaentry.cpp:133
KTraderParse::ParseTreeBase
Definition: ktraderparsetree_p.h:99
QList
Definition: kaboutdata.h:33
QString
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
This macro makes it easy to use non-POD types as global statics.
Definition: kglobal.h:221
kDebug
#define kDebug
Definition: kdebug.h:316
kWarning
#define kWarning
Definition: kdebug.h:322
kdebug.h
kservicefactory.h
KServiceOfferList
QList< KServiceOffer > KServiceOfferList
A list of weighted offers.
Definition: kserviceoffer.h:134
kservicetype.h
kservicetypefactory.h
kservicetypeprofile.h
weightedOffers
static KServiceOfferList weightedOffers(const QString &serviceType)
Definition: kservicetypetrader.cpp:88
kservicetypetrader.h
ktraderparsetree_p.h
KServiceTypeProfile
Returns the offers in the profile for the requested service type.
Definition: kservicetypeprofile.cpp:116
KServiceTypeProfile::sortServiceTypeOffers
KServiceOfferList sortServiceTypeOffers(const KServiceOfferList &list, const QString &servicetype)
Definition: kservicetypeprofile.cpp:120
KServiceTypeProfile::hasProfile
bool hasProfile(const QString &serviceType)
Definition: kservicetypeprofile.cpp:171
KTraderParse
Definition: ktraderparse.cpp:38
KTraderParse::matchConstraint
int matchConstraint(const ParseTreeBase *_tree, const KService::Ptr &_service, const KService::List &_list)
Definition: ktraderparsetree.cpp:611
KTraderParse::parseConstraints
ParseTreeBase::Ptr parseConstraints(const QString &_constr)
Definition: ktraderparse.cpp:52
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal