1/* This file is part of the KDE libraries
2 * Copyright 2008 David Faure <faure@kde.org>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License or ( at
7 * your option ) version 3 or, at the discretion of KDE e.V. ( which shall
8 * act as a proxy as in section 14 of the GPLv3 ), any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef KDED_KMIMEASSOCIATIONS_H
22#define KDED_KMIMEASSOCIATIONS_H
23
24#include <QSet>
25#include <QStringList>
26#include <QHash>
27#include <kserviceoffer.h>
28class KMimeTypeFactory;
29class KConfigGroup;
30
31struct ServiceTypeOffersData {
32 QList<KServiceOffer> offers; // service + initial preference + allow as default
33 QSet<KService::Ptr> offerSet; // for quick contains() check
34 QSet<KService::Ptr> removedOffers; // remember removed offers explicitly
35};
36
37class KOfferHash
38{
39public:
40 KOfferHash() {}
41 QList<KServiceOffer> offersFor(const QString& serviceType) const {
42 QHash<QString, ServiceTypeOffersData>::const_iterator it = m_serviceTypeData.find(serviceType);
43 if (it != m_serviceTypeData.end())
44 return (*it).offers;
45 return QList<KServiceOffer>();
46 }
47 void addServiceOffer(const QString& serviceType, const KServiceOffer& offer);
48 void removeServiceOffer(const QString& serviceType, KService::Ptr service);
49 bool hasRemovedOffer(const QString& serviceType, KService::Ptr service) const;
50
51private:
52 KOfferHash(const KOfferHash&); // forbidden
53 QHash<QString, ServiceTypeOffersData> m_serviceTypeData;
54};
55
56
57/**
58 * Parse mimeapps.list files and:
59 * - modify mimetype associations in the relevant services (using KServiceFactory)
60 * - remember preferrence order specified by user
61 */
62class KMimeAssociations
63{
64public:
65 explicit KMimeAssociations(KOfferHash& offerHash);
66
67 // Read mimeapps.list files
68 bool parseAllMimeAppsList();
69
70 void parseMimeAppsList(const QString& file, int basePreference);
71
72private:
73 void parseAddedAssociations(const KConfigGroup& group, const QString& file, int basePreference);
74 void parseRemovedAssociations(const KConfigGroup& group, const QString& file);
75
76 KOfferHash& m_offerHash;
77};
78
79#endif /* KMIMEASSOCIATIONS_H */
80