• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDED

kmimeassociations.cpp

Go to the documentation of this file.
00001 /*  This file is part of the KDE libraries
00002  *  Copyright 2008  David Faure  <faure@kde.org>
00003  *
00004  *  This library is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License or ( at
00007  *  your option ) version 3 or, at the discretion of KDE e.V. ( which shall
00008  *  act as a proxy as in section 14 of the GPLv3 ), any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  *  Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kmimeassociations.h"
00022 #include <kservice.h>
00023 #include <kconfiggroup.h>
00024 #include <kconfig.h>
00025 #include <kdebug.h>
00026 #include <kglobal.h>
00027 #include <kstandarddirs.h>
00028 
00029 KMimeAssociations::KMimeAssociations(KOfferHash& offerHash)
00030     : m_offerHash(offerHash)
00031 {
00032 }
00033 
00034 /*
00035 
00036 The goal of this class is to parse mimeapps.list files, which are used to
00037 let users configure the application-mimetype associations.
00038 
00039 Example file:
00040 
00041 [Added Associations]
00042 text/plain=kate.desktop;
00043 
00044 [Removed Associations]
00045 text/plain=gnome-gedit.desktop;gnu-emacs.desktop;
00046 
00047 
00048 
00049 */
00050 
00051 bool KMimeAssociations::parseAllMimeAppsList()
00052 {
00053     // Using the "merged view" from KConfig is not enough since we -add- at every level, we don't replace.
00054     const QStringList mimeappsFiles = KGlobal::dirs()->findAllResources("xdgdata-apps", "mimeapps.list");
00055     if (mimeappsFiles.isEmpty())
00056         return false;
00057 
00058     int basePreference = 1000; // start high :)
00059     QListIterator<QString> mimeappsIter( mimeappsFiles );
00060     mimeappsIter.toBack();
00061     while (mimeappsIter.hasPrevious()) { // global first, then local.
00062         const QString mimeappsFile = mimeappsIter.previous();
00063         kDebug(7021) << "Parsing" << mimeappsFile;
00064         parseMimeAppsList(mimeappsFile, basePreference);
00065         basePreference -= 50;
00066     }
00067     return true;
00068 }
00069 
00070 void KMimeAssociations::parseMimeAppsList(const QString& file, int basePreference)
00071 {
00072     KConfig profile(file, KConfig::SimpleConfig);
00073     parseAddedAssociations(KConfigGroup(&profile, "Added Associations"), file, basePreference);
00074     parseRemovedAssociations(KConfigGroup(&profile, "Removed Associations"), file);
00075 
00076     // KDE extension for parts and plugins, see settings/filetypes/mimetypedata.cpp
00077     parseAddedAssociations(KConfigGroup(&profile, "Added KDE Service Associations"), file, basePreference);
00078     parseRemovedAssociations(KConfigGroup(&profile, "Removed KDE Service Associations"), file);
00079 }
00080 
00081 void KMimeAssociations::parseAddedAssociations(const KConfigGroup& group, const QString& file, int basePreference)
00082 {
00083     Q_FOREACH(const QString& mime, group.keyList()) {
00084         int pref = basePreference;
00085         const QStringList services = group.readXdgListEntry(mime);
00086         Q_FOREACH(const QString &service, services) {
00087             KService::Ptr pService = KService::serviceByStorageId(service);
00088             if (!pService) {
00089                 kDebug(7021) << file << "specifies unknown service" << service << "in" << group.name();
00090             } else {
00091                 //kDebug(7021) << "adding mime" << mime << "to service" << pService->entryPath() << "pref=" << pref;
00092                 m_offerHash.addServiceOffer(mime, KServiceOffer(pService, pref, 0, pService->allowAsDefault()));
00093                 --pref;
00094             }
00095         }
00096     }
00097 }
00098 
00099 void KMimeAssociations::parseRemovedAssociations(const KConfigGroup& group, const QString& file)
00100 {
00101     Q_FOREACH(const QString& mime, group.keyList()) {
00102         const QStringList services = group.readXdgListEntry(mime);
00103         Q_FOREACH(const QString& service, services) {
00104             KService::Ptr pService = KService::serviceByStorageId(service);
00105             if (!pService) {
00106                 kDebug(7021) << file << "specifies unknown service" << service << "in" << group.name();
00107             } else {
00108                 //kDebug(7021) << "removing mime" << mime << "from service" << service;
00109                 m_offerHash.removeServiceOffer(mime, pService);
00110             }
00111         }
00112     }
00113 }
00114 
00115 void KOfferHash::addServiceOffer(const QString& serviceType, const KServiceOffer& offer)
00116 {
00117     KService::Ptr service = offer.service();
00118     //kDebug(7021) << "Adding" << service->entryPath() << "to" << serviceType << offer.preference();
00119     ServiceTypeOffersData& data = m_serviceTypeData[serviceType]; // find or create
00120     QList<KServiceOffer>& offers = data.offers;
00121     QSet<KService::Ptr>& offerSet = data.offerSet;
00122     if ( !offerSet.contains( service ) ) {
00123         offers.append( offer );
00124         offerSet.insert( service );
00125     } else {
00126         // kDebug(7021) << service.offers() << service->entryPath() << "already in" << serviceType;
00127         // This happens when mimeapps.list mentions a service (to make it preferred)
00128         // Update initialPreference to qMax(existing offer, new offer)
00129         QMutableListIterator<KServiceOffer> sfit(data.offers);
00130         while (sfit.hasNext()) {
00131             if (sfit.next().service() == service) // we can compare KService::Ptrs because they are from the memory hash
00132                 sfit.value().setPreference( qMax(sfit.value().preference(), offer.preference()) );
00133         }
00134     }
00135 }
00136 
00137 void KOfferHash::removeServiceOffer(const QString& serviceType, KService::Ptr service)
00138 {
00139     QHash<QString, ServiceTypeOffersData>::iterator it = m_serviceTypeData.find(serviceType);
00140     if (it != m_serviceTypeData.end()) {
00141         ServiceTypeOffersData& data = *it;
00142         data.offerSet.remove(service);
00143         QMutableListIterator<KServiceOffer> sfit(data.offers);
00144         while (sfit.hasNext()) {
00145             if (sfit.next().service()->storageId() == service->storageId())
00146                 sfit.remove();
00147         }
00148     }
00149 }

KDED

Skip menu "KDED"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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