File indexing completed on 2024-04-21 03:56:52

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999-2007 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kbuildmimetypefactory_p.h"
0009 #include "ksycoca.h"
0010 #include "ksycocadict_p.h"
0011 #include "ksycocaresourcelist_p.h"
0012 
0013 #include <QDebug>
0014 #include <QHash>
0015 #include <QIODevice>
0016 #include <QStandardPaths>
0017 #include <assert.h>
0018 
0019 KBuildMimeTypeFactory::KBuildMimeTypeFactory(KSycoca *db)
0020     : KMimeTypeFactory(db)
0021 {
0022     // We want all xml files under xdgdata/mime - but not mime/packages/*.xml
0023     m_resourceList.emplace_back("xdgdata-mime", QStringLiteral("mime"), QStringLiteral("*.xml"));
0024 }
0025 
0026 KBuildMimeTypeFactory::~KBuildMimeTypeFactory()
0027 {
0028 }
0029 
0030 KSycocaEntry::List KBuildMimeTypeFactory::allEntries() const
0031 {
0032     assert(sycoca()->isBuilding());
0033     return m_entryDict->values();
0034 }
0035 
0036 KSycocaEntry *KBuildMimeTypeFactory::createEntry(const QString &file) const
0037 {
0038     // file=text/plain.xml  ->  name=plain.xml dirName=text
0039     Q_ASSERT(!file.startsWith(QLatin1String("mime/")));
0040 
0041     const int pos = file.lastIndexOf(QLatin1Char('/'));
0042     if (pos == -1) { // huh?
0043         return nullptr;
0044     }
0045     const auto dirName = QStringView(file).left(pos);
0046     if (dirName == QLatin1String("packages")) { // special subdir
0047         return nullptr;
0048     }
0049 
0050     const int dot = file.lastIndexOf(QLatin1Char('.'));
0051     if (dot == -1) { // huh?
0052         return nullptr;
0053     }
0054     const QString name = file.left(dot);
0055 
0056     // qDebug() << "Creating MIME type" << name << "from file" << file;
0057 
0058     MimeTypeEntry *e = new MimeTypeEntry(file, name);
0059     return e;
0060 }
0061 
0062 void KBuildMimeTypeFactory::saveHeader(QDataStream &str)
0063 {
0064     KSycocaFactory::saveHeader(str);
0065 }
0066 
0067 void KBuildMimeTypeFactory::save(QDataStream &str)
0068 {
0069     KSycocaFactory::save(str);
0070 
0071     str << qint32(0);
0072 
0073     const qint64 endOfFactoryData = str.device()->pos();
0074 
0075     // Update header (pass #3)
0076     saveHeader(str);
0077 
0078     // Seek to end.
0079     str.device()->seek(endOfFactoryData);
0080 }
0081 
0082 KMimeTypeFactory::MimeTypeEntry::Ptr KBuildMimeTypeFactory::createFakeMimeType(const QString &name)
0083 {
0084     const QString file = name; // hack
0085     KSycocaEntry::Ptr entry = m_entryDict->value(file);
0086     if (!entry) {
0087         MimeTypeEntry *e = new MimeTypeEntry(file, name);
0088         entry = e;
0089     }
0090 
0091     Q_ASSERT(entry && entry->isValid());
0092     addEntry(entry);
0093     return KMimeTypeFactory::MimeTypeEntry::Ptr(static_cast<MimeTypeEntry *>(entry.data()));
0094 }