• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.11.5 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 
29 using namespace KTraderParse;
30 
31 // --------------------------------------------------
32 
33 namespace KServiceTypeProfile {
34  KServiceOfferList sortServiceTypeOffers( const KServiceOfferList& list, const QString& servicetype );
35 }
36 
37 KServiceTypeTrader* KServiceTypeTrader::self()
38 {
39  K_GLOBAL_STATIC(KServiceTypeTrader, s_globalServiceTypeTrader)
40  return s_globalServiceTypeTrader;
41 }
42 
43 KServiceTypeTrader::KServiceTypeTrader()
44  : d(0)
45 {
46 }
47 
48 KServiceTypeTrader::~KServiceTypeTrader()
49 {
50 }
51 
52 // shared with KMimeTypeTrader
53 void 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
79 static 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 
88 static 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 
113 KService::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 
134 KService::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 
160 KService::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 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Sep 23 2014 09:53:09 by doxygen 1.8.3.1 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.11.5 API Reference

Skip menu "kdelibs-4.11.5 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