File indexing completed on 2024-12-08 09:43:22
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2000 Torben Weis <weis@kde.org> 0004 SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "kserviceoffer.h" 0010 0011 #include <QDebug> 0012 0013 class KServiceOfferPrivate 0014 { 0015 public: 0016 KServiceOfferPrivate() 0017 : preference(-1) 0018 , mimeTypeInheritanceLevel(0) 0019 , pService(nullptr) 0020 { 0021 } 0022 0023 int preference; 0024 int mimeTypeInheritanceLevel; 0025 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 71) 0026 bool bAllowAsDefault = false; 0027 #endif 0028 KService::Ptr pService; 0029 }; 0030 0031 KServiceOffer::KServiceOffer() 0032 : d(new KServiceOfferPrivate) 0033 { 0034 } 0035 0036 KServiceOffer::KServiceOffer(const KServiceOffer &_o) 0037 : d(new KServiceOfferPrivate) 0038 { 0039 *d = *_o.d; 0040 } 0041 0042 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 71) 0043 KServiceOffer::KServiceOffer(const KService::Ptr &_service, int _pref, int mimeTypeInheritanceLevel, bool _default) 0044 : d(new KServiceOfferPrivate) 0045 { 0046 d->pService = _service; 0047 d->preference = _pref; 0048 d->mimeTypeInheritanceLevel = mimeTypeInheritanceLevel; 0049 d->bAllowAsDefault = _default; 0050 } 0051 #endif 0052 0053 KServiceOffer::KServiceOffer(const KService::Ptr &_service, int _pref, int mimeTypeInheritanceLevel) 0054 : d(new KServiceOfferPrivate) 0055 { 0056 d->pService = _service; 0057 d->preference = _pref; 0058 d->mimeTypeInheritanceLevel = mimeTypeInheritanceLevel; 0059 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 71) 0060 d->bAllowAsDefault = true; 0061 #endif 0062 } 0063 0064 KServiceOffer::~KServiceOffer() = default; 0065 0066 KServiceOffer &KServiceOffer::operator=(const KServiceOffer &rhs) 0067 { 0068 if (this == &rhs) { 0069 return *this; 0070 } 0071 0072 *d = *rhs.d; 0073 return *this; 0074 } 0075 0076 bool KServiceOffer::operator<(const KServiceOffer &_o) const 0077 { 0078 // First check mimetype inheritance level. 0079 // Direct mimetype association is preferred above association via parent mimetype 0080 // So, the smaller the better. 0081 if (d->mimeTypeInheritanceLevel != _o.d->mimeTypeInheritanceLevel) { 0082 return d->mimeTypeInheritanceLevel < _o.d->mimeTypeInheritanceLevel; 0083 } 0084 0085 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 71) 0086 // Put offers allowed as default FIRST. 0087 if (_o.d->bAllowAsDefault && !d->bAllowAsDefault) { 0088 return false; // _o is default and not 'this'. 0089 } 0090 if (!_o.d->bAllowAsDefault && d->bAllowAsDefault) { 0091 return true; // 'this' is default but not _o. 0092 } 0093 // Both offers are allowed or not allowed as default 0094 #endif 0095 0096 // Finally, use preference to sort them 0097 // The bigger the better, but we want the better FIRST 0098 return _o.d->preference < d->preference; 0099 } 0100 0101 #if KSERVICE_BUILD_DEPRECATED_SINCE(5, 67) 0102 bool KServiceOffer::allowAsDefault() const 0103 { 0104 return d->bAllowAsDefault; 0105 } 0106 #endif 0107 0108 int KServiceOffer::preference() const 0109 { 0110 return d->preference; 0111 } 0112 0113 void KServiceOffer::setPreference(int p) 0114 { 0115 d->preference = p; 0116 } 0117 0118 KService::Ptr KServiceOffer::service() const 0119 { 0120 return d->pService; 0121 } 0122 0123 bool KServiceOffer::isValid() const 0124 { 0125 return d->preference >= 0; 0126 } 0127 0128 void KServiceOffer::setMimeTypeInheritanceLevel(int level) 0129 { 0130 d->mimeTypeInheritanceLevel = level; 0131 } 0132 0133 int KServiceOffer::mimeTypeInheritanceLevel() const 0134 { 0135 return d->mimeTypeInheritanceLevel; 0136 } 0137 0138 QDebug operator<<(QDebug dbg, const KServiceOffer &offer) 0139 { 0140 QDebugStateSaver saver(dbg); 0141 dbg.nospace() << offer.service()->storageId() << " " << offer.preference(); 0142 if (offer.mimeTypeInheritanceLevel() > 0) { 0143 dbg << " (inheritance level " << offer.mimeTypeInheritanceLevel() << ")"; 0144 } 0145 return dbg; 0146 }