1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1999-2007 David Faure <faure@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kbuildmimetypefactory_p.h"
9#include "ksycoca.h"
10#include "ksycocadict_p.h"
11#include "ksycocaresourcelist_p.h"
12
13#include <QDebug>
14#include <QHash>
15#include <QIODevice>
16#include <QStandardPaths>
17#include <assert.h>
18
19KBuildMimeTypeFactory::KBuildMimeTypeFactory(KSycoca *db)
20 : KMimeTypeFactory(db)
21{
22 // We want all xml files under xdgdata/mime - but not mime/packages/*.xml
23 m_resourceList.emplace_back(args: "xdgdata-mime", QStringLiteral("mime"), QStringLiteral("*.xml"));
24}
25
26KBuildMimeTypeFactory::~KBuildMimeTypeFactory()
27{
28}
29
30KSycocaEntry::List KBuildMimeTypeFactory::allEntries() const
31{
32 assert(sycoca()->isBuilding());
33 return m_entryDict->values();
34}
35
36KSycocaEntry *KBuildMimeTypeFactory::createEntry(const QString &file) const
37{
38 // file=text/plain.xml -> name=plain.xml dirName=text
39 Q_ASSERT(!file.startsWith(QLatin1String("mime/")));
40
41 const int pos = file.lastIndexOf(c: QLatin1Char('/'));
42 if (pos == -1) { // huh?
43 return nullptr;
44 }
45 const auto dirName = QStringView(file).left(n: pos);
46 if (dirName == QLatin1String("packages")) { // special subdir
47 return nullptr;
48 }
49
50 const int dot = file.lastIndexOf(c: QLatin1Char('.'));
51 if (dot == -1) { // huh?
52 return nullptr;
53 }
54 const QString name = file.left(n: dot);
55
56 // qDebug() << "Creating MIME type" << name << "from file" << file;
57
58 MimeTypeEntry *e = new MimeTypeEntry(file, name);
59 return e;
60}
61
62void KBuildMimeTypeFactory::saveHeader(QDataStream &str)
63{
64 KSycocaFactory::saveHeader(str);
65}
66
67void KBuildMimeTypeFactory::save(QDataStream &str)
68{
69 KSycocaFactory::save(str);
70
71 str << qint32(0);
72
73 const qint64 endOfFactoryData = str.device()->pos();
74
75 // Update header (pass #3)
76 saveHeader(str);
77
78 // Seek to end.
79 str.device()->seek(pos: endOfFactoryData);
80}
81
82KMimeTypeFactory::MimeTypeEntry::Ptr KBuildMimeTypeFactory::createFakeMimeType(const QString &name)
83{
84 const QString file = name; // hack
85 KSycocaEntry::Ptr entry = m_entryDict->value(key: file);
86 if (!entry) {
87 MimeTypeEntry *e = new MimeTypeEntry(file, name);
88 entry = e;
89 }
90
91 Q_ASSERT(entry && entry->isValid());
92 addEntry(newEntry: entry);
93 return KMimeTypeFactory::MimeTypeEntry::Ptr(static_cast<MimeTypeEntry *>(entry.data()));
94}
95

source code of kservice/src/sycoca/kbuildmimetypefactory.cpp