1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
6 | */ |
7 | |
8 | #ifndef KMIMEASSOCIATIONS_H |
9 | #define KMIMEASSOCIATIONS_H |
10 | |
11 | #include <QHash> |
12 | #include <QSet> |
13 | #include <kserviceoffer.h> |
14 | class KConfigGroup; |
15 | class KServiceFactory; |
16 | |
17 | struct ServiceTypeOffersData { |
18 | QList<KServiceOffer> offers; // service + initial preference + allow as default |
19 | QSet<KService::Ptr> offerSet; // for quick contains() check |
20 | QSet<KService::Ptr> removedOffers; // remember removed offers explicitly |
21 | }; |
22 | |
23 | class KOfferHash |
24 | { |
25 | public: |
26 | KOfferHash() |
27 | { |
28 | } |
29 | KOfferHash(const KOfferHash &) = delete; |
30 | KOfferHash &operator=(const KOfferHash &) = delete; |
31 | QList<KServiceOffer> offersFor(const QString &serviceType) const |
32 | { |
33 | auto it = m_serviceTypeData.constFind(key: serviceType); |
34 | if (it != m_serviceTypeData.cend()) { |
35 | return it.value().offers; |
36 | } |
37 | return QList<KServiceOffer>(); |
38 | } |
39 | void addServiceOffer(const QString &serviceType, const KServiceOffer &offer); |
40 | void removeServiceOffer(const QString &serviceType, const KService::Ptr &service); |
41 | bool hasRemovedOffer(const QString &serviceType, const KService::Ptr &service) const; |
42 | |
43 | const QHash<QString, ServiceTypeOffersData> &serviceTypeData() const |
44 | { |
45 | return m_serviceTypeData; |
46 | } |
47 | |
48 | private: |
49 | QHash<QString, ServiceTypeOffersData> m_serviceTypeData; |
50 | }; |
51 | |
52 | /** |
53 | * Parse mimeapps.list files and: |
54 | * - modify MIME type associations in the relevant services (using KServiceFactory) |
55 | * - remember preference order specified by user |
56 | */ |
57 | class KMimeAssociations |
58 | { |
59 | public: |
60 | explicit KMimeAssociations(KOfferHash &offerHash, KServiceFactory *serviceFactory); |
61 | |
62 | static QStringList mimeAppsFiles(); |
63 | |
64 | // Read mimeapps.list files |
65 | void parseAllMimeAppsList(); |
66 | |
67 | void parseMimeAppsList(const QString &file, int basePreference); |
68 | |
69 | private: |
70 | static QStringList mimeAppsDirs(); |
71 | |
72 | void parseAddedAssociations(const KConfigGroup &group, const QString &file, int basePreference); |
73 | void parseRemovedAssociations(const KConfigGroup &group, const QString &file); |
74 | |
75 | KOfferHash &m_offerHash; |
76 | KServiceFactory *m_serviceFactory; |
77 | }; |
78 | |
79 | #endif /* KMIMEASSOCIATIONS_H */ |
80 | |