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 | |
13 | class KServiceOfferPrivate |
14 | { |
15 | public: |
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 | |
28 | KServiceOffer::KServiceOffer() |
29 | : d(new KServiceOfferPrivate) |
30 | { |
31 | } |
32 | |
33 | KServiceOffer::KServiceOffer(const KServiceOffer &_o) |
34 | : d(new KServiceOfferPrivate) |
35 | { |
36 | *d = *_o.d; |
37 | } |
38 | |
39 | KServiceOffer::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 | |
47 | KServiceOffer::~KServiceOffer() = default; |
48 | |
49 | KServiceOffer &KServiceOffer::operator=(const KServiceOffer &rhs) |
50 | { |
51 | if (this == &rhs) { |
52 | return *this; |
53 | } |
54 | |
55 | *d = *rhs.d; |
56 | return *this; |
57 | } |
58 | |
59 | bool 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 | |
73 | int KServiceOffer::preference() const |
74 | { |
75 | return d->preference; |
76 | } |
77 | |
78 | void KServiceOffer::setPreference(int p) |
79 | { |
80 | d->preference = p; |
81 | } |
82 | |
83 | KService::Ptr KServiceOffer::service() const |
84 | { |
85 | return d->pService; |
86 | } |
87 | |
88 | bool KServiceOffer::isValid() const |
89 | { |
90 | return d->preference >= 0; |
91 | } |
92 | |
93 | void KServiceOffer::setMimeTypeInheritanceLevel(int level) |
94 | { |
95 | d->mimeTypeInheritanceLevel = level; |
96 | } |
97 | |
98 | int KServiceOffer::mimeTypeInheritanceLevel() const |
99 | { |
100 | return d->mimeTypeInheritanceLevel; |
101 | } |
102 | |
103 | QDebug 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 |