File indexing completed on 2024-07-14 03:55:24

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     KService::Ptr pService;
0026 };
0027 
0028 KServiceOffer::KServiceOffer()
0029     : d(new KServiceOfferPrivate)
0030 {
0031 }
0032 
0033 KServiceOffer::KServiceOffer(const KServiceOffer &_o)
0034     : d(new KServiceOfferPrivate)
0035 {
0036     *d = *_o.d;
0037 }
0038 
0039 KServiceOffer::KServiceOffer(const KService::Ptr &_service, int _pref, int mimeTypeInheritanceLevel)
0040     : d(new KServiceOfferPrivate)
0041 {
0042     d->pService = _service;
0043     d->preference = _pref;
0044     d->mimeTypeInheritanceLevel = mimeTypeInheritanceLevel;
0045 }
0046 
0047 KServiceOffer::~KServiceOffer() = default;
0048 
0049 KServiceOffer &KServiceOffer::operator=(const KServiceOffer &rhs)
0050 {
0051     if (this == &rhs) {
0052         return *this;
0053     }
0054 
0055     *d = *rhs.d;
0056     return *this;
0057 }
0058 
0059 bool KServiceOffer::operator<(const KServiceOffer &_o) const
0060 {
0061     // First check mimetype inheritance level.
0062     // Direct mimetype association is preferred above association via parent mimetype
0063     // So, the smaller the better.
0064     if (d->mimeTypeInheritanceLevel != _o.d->mimeTypeInheritanceLevel) {
0065         return d->mimeTypeInheritanceLevel < _o.d->mimeTypeInheritanceLevel;
0066     }
0067 
0068     // Finally, use preference to sort them
0069     // The bigger the better, but we want the better FIRST
0070     return _o.d->preference < d->preference;
0071 }
0072 
0073 int KServiceOffer::preference() const
0074 {
0075     return d->preference;
0076 }
0077 
0078 void KServiceOffer::setPreference(int p)
0079 {
0080     d->preference = p;
0081 }
0082 
0083 KService::Ptr KServiceOffer::service() const
0084 {
0085     return d->pService;
0086 }
0087 
0088 bool KServiceOffer::isValid() const
0089 {
0090     return d->preference >= 0;
0091 }
0092 
0093 void KServiceOffer::setMimeTypeInheritanceLevel(int level)
0094 {
0095     d->mimeTypeInheritanceLevel = level;
0096 }
0097 
0098 int KServiceOffer::mimeTypeInheritanceLevel() const
0099 {
0100     return d->mimeTypeInheritanceLevel;
0101 }
0102 
0103 QDebug operator<<(QDebug dbg, const KServiceOffer &offer)
0104 {
0105     QDebugStateSaver saver(dbg);
0106     dbg.nospace() << offer.service()->storageId() << " " << offer.preference();
0107     if (offer.mimeTypeInheritanceLevel() > 0) {
0108         dbg << " (inheritance level " << offer.mimeTypeInheritanceLevel() << ")";
0109     }
0110     return dbg;
0111 }