File indexing completed on 2024-11-03 09:58:47

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #ifndef KMIMEASSOCIATIONS_H
0009 #define KMIMEASSOCIATIONS_H
0010 
0011 #include <QHash>
0012 #include <QSet>
0013 #include <kserviceoffer.h>
0014 class KConfigGroup;
0015 class KServiceFactory;
0016 
0017 struct ServiceTypeOffersData {
0018     QList<KServiceOffer> offers; // service + initial preference + allow as default
0019     QSet<KService::Ptr> offerSet; // for quick contains() check
0020     QSet<KService::Ptr> removedOffers; // remember removed offers explicitly
0021 };
0022 
0023 class KOfferHash
0024 {
0025 public:
0026     KOfferHash()
0027     {
0028     }
0029     KOfferHash(const KOfferHash &) = delete;
0030     KOfferHash &operator=(const KOfferHash &) = delete;
0031     QList<KServiceOffer> offersFor(const QString &serviceType) const
0032     {
0033         auto it = m_serviceTypeData.constFind(serviceType);
0034         if (it != m_serviceTypeData.cend()) {
0035             return it.value().offers;
0036         }
0037         return QList<KServiceOffer>();
0038     }
0039     void addServiceOffer(const QString &serviceType, const KServiceOffer &offer);
0040     void removeServiceOffer(const QString &serviceType, const KService::Ptr &service);
0041     bool hasRemovedOffer(const QString &serviceType, const KService::Ptr &service) const;
0042 
0043     const QHash<QString, ServiceTypeOffersData> &serviceTypeData() const
0044     {
0045         return m_serviceTypeData;
0046     }
0047 
0048 private:
0049     QHash<QString, ServiceTypeOffersData> m_serviceTypeData;
0050 };
0051 
0052 /**
0053  * Parse mimeapps.list files and:
0054  * - modify MIME type associations in the relevant services (using KServiceFactory)
0055  * - remember preference order specified by user
0056  */
0057 class KMimeAssociations
0058 {
0059 public:
0060     explicit KMimeAssociations(KOfferHash &offerHash, KServiceFactory *serviceFactory);
0061 
0062     static QStringList mimeAppsFiles();
0063 
0064     // Read mimeapps.list files
0065     void parseAllMimeAppsList();
0066 
0067     void parseMimeAppsList(const QString &file, int basePreference);
0068 
0069 private:
0070     static QStringList mimeAppsDirs();
0071 
0072     void parseAddedAssociations(const KConfigGroup &group, const QString &file, int basePreference);
0073     void parseRemovedAssociations(const KConfigGroup &group, const QString &file);
0074 
0075     KOfferHash &m_offerHash;
0076     KServiceFactory *m_serviceFactory;
0077 };
0078 
0079 #endif /* KMIMEASSOCIATIONS_H */