1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 Torben Weis <weis@kde.org>
4 SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kserviceoffer.h"
10
11#include <QDebug>
12
13class KServiceOfferPrivate
14{
15public:
16 KServiceOfferPrivate()
17 : preference(-1)
18 , mimeTypeInheritanceLevel(0)
19 , pService(nullptr)
20 {
21 }
22
23 int preference;
24 int mimeTypeInheritanceLevel;
25 KService::Ptr pService;
26};
27
28KServiceOffer::KServiceOffer()
29 : d(new KServiceOfferPrivate)
30{
31}
32
33KServiceOffer::KServiceOffer(const KServiceOffer &_o)
34 : d(new KServiceOfferPrivate)
35{
36 *d = *_o.d;
37}
38
39KServiceOffer::KServiceOffer(const KService::Ptr &_service, int _pref, int mimeTypeInheritanceLevel)
40 : d(new KServiceOfferPrivate)
41{
42 d->pService = _service;
43 d->preference = _pref;
44 d->mimeTypeInheritanceLevel = mimeTypeInheritanceLevel;
45}
46
47KServiceOffer::~KServiceOffer() = default;
48
49KServiceOffer &KServiceOffer::operator=(const KServiceOffer &rhs)
50{
51 if (this == &rhs) {
52 return *this;
53 }
54
55 *d = *rhs.d;
56 return *this;
57}
58
59bool KServiceOffer::operator<(const KServiceOffer &_o) const
60{
61 // First check mimetype inheritance level.
62 // Direct mimetype association is preferred above association via parent mimetype
63 // So, the smaller the better.
64 if (d->mimeTypeInheritanceLevel != _o.d->mimeTypeInheritanceLevel) {
65 return d->mimeTypeInheritanceLevel < _o.d->mimeTypeInheritanceLevel;
66 }
67
68 // Finally, use preference to sort them
69 // The bigger the better, but we want the better FIRST
70 return _o.d->preference < d->preference;
71}
72
73int KServiceOffer::preference() const
74{
75 return d->preference;
76}
77
78void KServiceOffer::setPreference(int p)
79{
80 d->preference = p;
81}
82
83KService::Ptr KServiceOffer::service() const
84{
85 return d->pService;
86}
87
88bool KServiceOffer::isValid() const
89{
90 return d->preference >= 0;
91}
92
93void KServiceOffer::setMimeTypeInheritanceLevel(int level)
94{
95 d->mimeTypeInheritanceLevel = level;
96}
97
98int KServiceOffer::mimeTypeInheritanceLevel() const
99{
100 return d->mimeTypeInheritanceLevel;
101}
102
103QDebug operator<<(QDebug dbg, const KServiceOffer &offer)
104{
105 QDebugStateSaver saver(dbg);
106 dbg.nospace() << offer.service()->storageId() << " " << offer.preference();
107 if (offer.mimeTypeInheritanceLevel() > 0) {
108 dbg << " (inheritance level " << offer.mimeTypeInheritanceLevel() << ")";
109 }
110 return dbg;
111}
112

source code of kservice/src/services/kserviceoffer.cpp