• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • Contact Us
 

KDECore

kmimetypetrader.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 "kmimetypetrader.h"
00021 
00022 #include "kservicetypeprofile.h"
00023 #include "kservicetype.h"
00024 #include "kservicetypetrader.h"
00025 #include "kmimetype.h"
00026 #include "kservicefactory.h"
00027 #include "kmimetypefactory.h"
00028 #include "kmimetyperepository_p.h"
00029 
00030 #include <kdebug.h>
00031 
00032 class KMimeTypeTrader::Private
00033 {
00034 public:
00035     Private() {}
00036 };
00037 
00038 KMimeTypeTrader* KMimeTypeTrader::self()
00039 {
00040     K_GLOBAL_STATIC(KMimeTypeTrader, s_self)
00041     return s_self;
00042 }
00043 
00044 KMimeTypeTrader::KMimeTypeTrader()
00045     : d(new Private())
00046 {
00047 }
00048 
00049 KMimeTypeTrader::~KMimeTypeTrader()
00050 {
00051     delete d;
00052 }
00053 
00054 static KServiceOfferList mimeTypeSycocaOffers(const QString& mimeType)
00055 {
00056     KServiceOfferList lst;
00057 
00058     const QString mime = KMimeTypeRepository::self()->canonicalName(mimeType);
00059     KMimeTypeFactory *factory = KMimeTypeFactory::self();
00060     const int offset = factory->entryOffset(mime);
00061     if ( !offset ) {
00062         if (!mimeType.startsWith(QLatin1String("x-scheme-handler/"))) // don't warn for unknown scheme handler mimetypes
00063             kWarning(7014) << "KMimeTypeTrader: mimeType" << mimeType << "not found";
00064         return lst; // empty
00065     }
00066 
00067     const int serviceOffersOffset = factory->serviceOffersOffset(mime);
00068     if ( serviceOffersOffset > -1 ) {
00069         lst = KServiceFactory::self()->offers(offset, serviceOffersOffset);
00070     }
00071     return lst;
00072 }
00073 
00074 static KService::List mimeTypeSycocaServiceOffers(const QString& mimeType)
00075 {
00076     KService::List lst;
00077     const QString mime = KMimeTypeRepository::self()->canonicalName(mimeType);
00078     KMimeTypeFactory *factory = KMimeTypeFactory::self();
00079     const int offset = factory->entryOffset(mime);
00080     if ( !offset ) {
00081         kWarning(7014) << "KMimeTypeTrader: mimeType" << mimeType << "not found";
00082         return lst; // empty
00083     }
00084     const int serviceOffersOffset = factory->serviceOffersOffset(mime);
00085     if ( serviceOffersOffset > -1 ) {
00086         lst = KServiceFactory::self()->serviceOffers(offset, serviceOffersOffset);
00087     }
00088     return lst;
00089 }
00090 
00091 #define CHECK_SERVICETYPE(genericServiceTypePtr) \
00092     if (!genericServiceTypePtr) { \
00093         kError(7014) << "KMimeTypeTrader: couldn't find service type" << genericServiceType << \
00094             "\nPlease ensure that the .desktop file for it is installed; then run kbuildsycoca4."; \
00095         return; \
00096     }
00097 
00104 static void filterMimeTypeOffers(KServiceOfferList& list, const QString& genericServiceType)
00105 {
00106     KServiceType::Ptr genericServiceTypePtr = KServiceType::serviceType(genericServiceType);
00107     CHECK_SERVICETYPE(genericServiceTypePtr);
00108 
00109     QMutableListIterator<KServiceOffer> it(list);
00110     while(it.hasNext()) {
00111         const KService::Ptr servPtr = it.next().service();
00112         // Expand servPtr->hasServiceType( genericServiceTypePtr ) to avoid lookup each time:
00113         if (!KServiceFactory::self()->hasOffer(genericServiceTypePtr->offset(),
00114                                                genericServiceTypePtr->serviceOffersOffset(),
00115                                                servPtr->offset())
00116             || !servPtr->showInKDE()) {
00117             it.remove();
00118         }
00119     }
00120 }
00121 
00122 static void filterMimeTypeOffers(KService::List& list, const QString& genericServiceType)
00123 {
00124     KServiceType::Ptr genericServiceTypePtr = KServiceType::serviceType(genericServiceType);
00125     CHECK_SERVICETYPE(genericServiceTypePtr);
00126 
00127     QMutableListIterator<KService::Ptr> it(list);
00128     while(it.hasNext()) {
00129         const KService::Ptr servPtr = it.next();
00130         // Expand servPtr->hasServiceType( genericServiceTypePtr ) to avoid lookup each time:
00131         if (!KServiceFactory::self()->hasOffer(genericServiceTypePtr->offset(),
00132                                                genericServiceTypePtr->serviceOffersOffset(),
00133                                                servPtr->offset())
00134             || !servPtr->showInKDE()) {
00135             it.remove();
00136         }
00137     }
00138 }
00139 
00140 #undef CHECK_SERVICETYPE
00141 
00142 KService::List KMimeTypeTrader::query( const QString& mimeType,
00143                                        const QString& genericServiceType,
00144                                        const QString& constraint ) const
00145 {
00146     // Get all services of this mime type.
00147     KService::List lst = mimeTypeSycocaServiceOffers(mimeType);
00148     filterMimeTypeOffers(lst, genericServiceType);
00149 
00150     KServiceTypeTrader::applyConstraints(lst, constraint);
00151 
00152     kDebug(7014) << "query for mimeType " << mimeType << ", " << genericServiceType
00153                  << " : returning " << lst.count() << " offers" << endl;
00154     return lst;
00155 }
00156 
00157 KService::Ptr KMimeTypeTrader::preferredService( const QString & mimeType, const QString & genericServiceType )
00158 {
00159     // First, get all offers known to ksycoca.
00160     KServiceOfferList offers = mimeTypeSycocaOffers( mimeType );
00161 
00162     // Assign preferences from the profile to those offers - and filter for genericServiceType
00163     Q_ASSERT(!genericServiceType.isEmpty());
00164     filterMimeTypeOffers(offers, genericServiceType);
00165 
00166     KServiceOfferList::const_iterator itOff = offers.constBegin();
00167     // Look for the first one that is allowed as default.
00168     // Since the allowed-as-default are first anyway, we only have
00169     // to look at the first one to know.
00170     if( itOff != offers.constEnd() && (*itOff).allowAsDefault() )
00171         return (*itOff).service();
00172 
00173     //kDebug(7014) << "No offers, or none allowed as default";
00174     return KService::Ptr();
00175 }

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal