KService

kservicetypetrader.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2000 Torben Weis <[email protected]>
4  SPDX-FileCopyrightText: 2006 David Faure <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "kservicetypetrader.h"
10 
11 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 90)
12 
13 #include "kservicefactory_p.h"
14 #include "kservicetype.h"
15 #include "kservicetypefactory_p.h"
16 #include "ksycoca.h"
17 #include "ksycoca_p.h"
18 #include "ktraderparsetree_p.h"
19 #include <kservicetypeprofile.h>
20 
21 #include "servicesdebug.h"
22 
23 using namespace KTraderParse;
24 
25 // --------------------------------------------------
26 
27 namespace KServiceTypeProfile
28 {
29 KServiceOfferList sortServiceTypeOffers(const KServiceOfferList &list, const QString &servicetype);
30 }
31 
32 class KServiceTypeTraderSingleton
33 {
34 public:
35  KServiceTypeTrader instance;
36 };
37 
38 Q_GLOBAL_STATIC(KServiceTypeTraderSingleton, s_globalServiceTypeTrader)
39 
41 {
42  return &s_globalServiceTypeTrader()->instance;
43 }
44 
45 KServiceTypeTrader::KServiceTypeTrader()
46  : d(nullptr)
47 {
48 }
49 
51 {
52 }
53 
54 // shared with KMimeTypeTrader
56 {
57  if (lst.isEmpty() || constraint.isEmpty()) {
58  return;
59  }
60 
61  const ParseTreeBase::Ptr constr = parseConstraints(constraint); // for ownership
62  const ParseTreeBase *pConstraintTree = constr.data(); // for speed
63 
64  if (!constr) { // parse error
65  lst.clear();
66  } else {
67  // Find all services matching the constraint
68  // and remove the other ones
69  auto isMatch = [=](const KService::Ptr &service) {
70  return matchConstraint(pConstraintTree, service, lst) != 1;
71  };
72 
73  lst.erase(std::remove_if(lst.begin(), lst.end(), isMatch), lst.end());
74  }
75 }
76 
77 KServiceOfferList KServiceTypeTrader::weightedOffers(const QString &serviceType) // static, internal
78 {
79  // qDebug() << "KServiceTypeTrader::weightedOffers( " << serviceType << " )";
80 
82  KServiceType::Ptr servTypePtr = KSycocaPrivate::self()->serviceTypeFactory()->findServiceTypeByName(serviceType);
83  if (!servTypePtr) {
84  qCWarning(SERVICES) << "KServiceTypeTrader: serviceType" << serviceType << "not found";
85  return KServiceOfferList();
86  }
87  if (servTypePtr->serviceOffersOffset() == -1) { // no offers in ksycoca
88  return KServiceOfferList();
89  }
90 
91  // First, get all offers known to ksycoca.
92  const KServiceOfferList services = KSycocaPrivate::self()->serviceFactory()->offers(servTypePtr->offset(), servTypePtr->serviceOffersOffset());
93 
94  const KServiceOfferList offers = KServiceTypeProfile::sortServiceTypeOffers(services, serviceType);
95 
96  return offers;
97 }
98 
99 KService::List KServiceTypeTrader::defaultOffers(const QString &serviceType, const QString &constraint) const
100 {
102  KServiceType::Ptr servTypePtr = KSycocaPrivate::self()->serviceTypeFactory()->findServiceTypeByName(serviceType);
103  if (!servTypePtr) {
104  qCWarning(SERVICES) << "KServiceTypeTrader: serviceType" << serviceType << "not found";
105  return KService::List();
106  }
107  if (servTypePtr->serviceOffersOffset() == -1) {
108  return KService::List();
109  }
110 
111  KService::List lst = KSycocaPrivate::self()->serviceFactory()->serviceOffers(servTypePtr->offset(), servTypePtr->serviceOffersOffset());
112 
113  applyConstraints(lst, constraint);
114 
115  // qDebug() << "query for serviceType " << serviceType << constraint
116  // << " : returning " << lst.count() << " offers" << endl;
117  return lst;
118 }
119 
120 KService::List KServiceTypeTrader::query(const QString &serviceType, const QString &constraint) const
121 {
122  if (!KServiceTypeProfile::hasProfile(serviceType)) {
123  // Fast path: skip the profile stuff if there's none (to avoid kservice->serviceoffer->kservice)
124  // The ordering according to initial preferences is done by kbuildsycoca
125  return defaultOffers(serviceType, constraint);
126  }
127 
128  // Get all services of this service type.
129  const KServiceOfferList offers = weightedOffers(serviceType);
130  KService::List lst;
131  lst.reserve(offers.size());
132 
133  // Now extract only the services; the weighting was only used for sorting.
134  std::transform(offers.cbegin(), offers.cend(), std::back_inserter(lst), [](const KServiceOffer &offer) {
135  return offer.service();
136  });
137 
138  applyConstraints(lst, constraint);
139 
140  // qDebug() << "query for serviceType " << serviceType << constraint
141  // << " : returning " << lst.count() << " offers" << endl;
142  return lst;
143 }
144 
146 {
147  const KServiceOfferList offers = weightedOffers(serviceType);
148 
149  KServiceOfferList::const_iterator itOff = offers.begin();
150 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 67)
151  // Look for the first one that is allowed as default.
152  // Since the allowed-as-default are first anyway, we only have
153  // to look at the first one to know.
154  if (itOff != offers.end() && (*itOff).allowAsDefault()) {
155 #else
156  if (itOff != offers.end()) {
157 #endif
158  return (*itOff).service();
159  }
160 
161  // qDebug() << "No offers, or none allowed as default";
162  return KService::Ptr();
163 }
164 
165 #endif
static KSycoca * self()
Get or create the only instance of KSycoca (read-only)
Definition: ksycoca.cpp:379
KSERVICE_EXPORT bool hasProfile(const QString &serviceType)
void ensureCacheValid()
Ensures the ksycoca database is up to date.
Definition: ksycoca.cpp:826
KService::Ptr preferredService(const QString &serviceType) const
Returns the preferred service for serviceType.
Returns the offers in the profile for the requested service type.
QList< Ptr > List
A list of shared data pointers for KService.
Definition: kservice.h:60
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
void reserve(int alloc)
int size() const const
bool isEmpty() const const
~KServiceTypeTrader()
Standard destructor.
bool isEmpty() const const
QList::const_iterator cend() const const
QExplicitlySharedDataPointer< KService > Ptr
A shared data pointer for KService.
Definition: kservice.h:56
QList::iterator erase(QList::iterator pos)
static void applyConstraints(KService::List &lst, const QString &constraint)
KService::List defaultOffers(const QString &serviceType, const QString &constraint=QString()) const
Returns all offers associated with a given servicetype, IGNORING the user preference.
QList::const_iterator cbegin() const const
void clear()
QList::iterator begin()
KService::List query(const QString &servicetype, const QString &constraint=QString()) const
The main function in the KServiceTypeTrader class.
QList::iterator end()
Holds the user's preference of a service.
Definition: kserviceoffer.h:30
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Mar 20 2023 03:56:29 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.