KDECore
kserviceoffer.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2000 Torben Weis <weis@kde.org> 00003 Copyright (C) 2006 David Faure <faure@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kserviceoffer.h" 00021 00022 class KServiceOffer::Private 00023 { 00024 public: 00025 Private() 00026 : preference(-1), 00027 mimeTypeInheritanceLevel(0), 00028 bAllowAsDefault( false ), 00029 pService( 0 ) 00030 { 00031 } 00032 00033 int preference; 00034 int mimeTypeInheritanceLevel; 00035 bool bAllowAsDefault; 00036 KService::Ptr pService; 00037 }; 00038 00039 KServiceOffer::KServiceOffer() 00040 : d( new Private ) 00041 { 00042 } 00043 00044 KServiceOffer::KServiceOffer( const KServiceOffer& _o ) 00045 : d( new Private ) 00046 { 00047 *d = *_o.d; 00048 } 00049 00050 KServiceOffer::KServiceOffer( const KService::Ptr& _service, int _pref, int mimeTypeInheritanceLevel, bool _default ) 00051 : d( new Private ) 00052 { 00053 d->pService = _service; 00054 d->preference = _pref; 00055 d->mimeTypeInheritanceLevel = mimeTypeInheritanceLevel; 00056 d->bAllowAsDefault = _default; 00057 } 00058 00059 KServiceOffer::~KServiceOffer() 00060 { 00061 delete d; 00062 } 00063 00064 KServiceOffer& KServiceOffer::operator=( const KServiceOffer& rhs ) 00065 { 00066 if ( this == &rhs ) { 00067 return *this; 00068 } 00069 00070 *d = *rhs.d; 00071 return *this; 00072 } 00073 00074 bool KServiceOffer::operator< ( const KServiceOffer& _o ) const 00075 { 00076 // First check mimetype inheritance level. 00077 // Direct mimetype association is preferred above association via parent mimetype 00078 // So, the smaller the better. 00079 if (d->mimeTypeInheritanceLevel != _o.d->mimeTypeInheritanceLevel) 00080 return d->mimeTypeInheritanceLevel < _o.d->mimeTypeInheritanceLevel; 00081 00082 // Put offers allowed as default FIRST. 00083 if ( _o.d->bAllowAsDefault && !d->bAllowAsDefault ) 00084 return false; // _o is default and not 'this'. 00085 if ( !_o.d->bAllowAsDefault && d->bAllowAsDefault ) 00086 return true; // 'this' is default but not _o. 00087 // Both offers are allowed or not allowed as default 00088 00089 // Finally, use preference to sort them 00090 // The bigger the better, but we want the better FIRST 00091 return _o.d->preference < d->preference; 00092 } 00093 00094 bool KServiceOffer::allowAsDefault() const 00095 { 00096 return d->bAllowAsDefault; 00097 } 00098 00099 int KServiceOffer::preference() const 00100 { 00101 return d->preference; 00102 } 00103 00104 void KServiceOffer::setPreference( int p ) 00105 { 00106 d->preference = p; 00107 } 00108 00109 KService::Ptr KServiceOffer::service() const 00110 { 00111 return d->pService; 00112 } 00113 00114 bool KServiceOffer::isValid() const 00115 { 00116 return d->preference >= 0; 00117 } 00118 00119 void KServiceOffer::setMimeTypeInheritanceLevel(int level) 00120 { 00121 d->mimeTypeInheritanceLevel = level; 00122 } 00123 00124 int KServiceOffer::mimeTypeInheritanceLevel() const 00125 { 00126 return d->mimeTypeInheritanceLevel; 00127 } 00128