1/* This file is part of the KDE libraries
2 * Copyright 1999-2007 David Faure <faure@kde.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kbuildmimetypefactory.h"
21#include "kmimetyperepository_p.h"
22#include "ksycoca.h"
23#include "ksycocadict_p.h"
24#include "ksycocaresourcelist.h"
25
26#include <kglobal.h>
27#include <kstandarddirs.h>
28#include <kdebug.h>
29#include <klocale.h>
30#include <assert.h>
31#include <QtCore/QHash>
32
33KBuildMimeTypeFactory::KBuildMimeTypeFactory() :
34 KMimeTypeFactory()
35{
36 m_resourceList = new KSycocaResourceList;
37 // We want all xml files under xdgdata-mime - but not packages/*.xml
38 m_resourceList->add( "xdgdata-mime", "*.xml" );
39}
40
41// return all resource types for this factory
42// i.e. first arguments to m_resourceList->add() above
43QStringList KBuildMimeTypeFactory::resourceTypes()
44{
45 return QStringList() << "xdgdata-mime";
46}
47
48KBuildMimeTypeFactory::~KBuildMimeTypeFactory()
49{
50 delete m_resourceList;
51}
52
53KMimeTypeFactory::MimeTypeEntry::Ptr KBuildMimeTypeFactory::findMimeTypeEntryByName(const QString &_name, KMimeType::FindByNameOption options)
54{
55 assert (KSycoca::self()->isBuilding());
56
57 QString name = _name;
58 if (options & KMimeType::ResolveAliases) {
59 name = KMimeTypeRepository::self()->canonicalName(_name);
60 }
61
62 // We're building a database - the mime type must be in memory
63 KSycocaEntry::Ptr servType = m_entryDict->value( name );
64 return MimeTypeEntry::Ptr::staticCast( servType );
65}
66
67KSycocaEntry::List KBuildMimeTypeFactory::allEntries() const
68{
69 assert (KSycoca::self()->isBuilding());
70 KSycocaEntry::List lst;
71 KSycocaEntryDict::Iterator itmime = m_entryDict->begin();
72 const KSycocaEntryDict::Iterator endmime = m_entryDict->end();
73 for( ; itmime != endmime ; ++itmime )
74 lst.append( *itmime );
75 return lst;
76}
77
78KSycocaEntry* KBuildMimeTypeFactory::createEntry(const QString &file, const char *resource) const
79{
80 Q_UNUSED(resource);
81
82 // file=text/plain.xml -> name=plain.xml dirName=text
83 const int pos = file.lastIndexOf('/');
84 if (pos == -1) // huh?
85 return 0;
86 const QString dirName = file.left(pos);
87 if (dirName == "packages") // special subdir
88 return 0;
89
90 const int dot = file.lastIndexOf('.');
91 if (dot == -1) // huh?
92 return 0;
93 const QString name = file.left(dot);
94
95 //kDebug() << "Creating mimetype" << name << "from file" << file;
96
97 MimeTypeEntry* e = new MimeTypeEntry(file, name);
98 return e;
99}
100
101void KBuildMimeTypeFactory::saveHeader(QDataStream &str)
102{
103 KSycocaFactory::saveHeader(str);
104}
105
106void KBuildMimeTypeFactory::save(QDataStream &str)
107{
108 KSycocaFactory::save(str);
109
110 str << (qint32) 0;
111
112 const int endOfFactoryData = str.device()->pos();
113
114 // Update header (pass #3)
115 saveHeader(str);
116
117 // Seek to end.
118 str.device()->seek(endOfFactoryData);
119}
120
121void KBuildMimeTypeFactory::createFakeMimeType(const QString& name)
122{
123 const QString file = name; // hack
124 KSycocaEntry::Ptr entry = m_entryDict->value(file);
125 if (!entry) {
126 MimeTypeEntry* e = new MimeTypeEntry(file, name);
127 entry = e;
128 }
129
130 Q_ASSERT(entry && entry->isValid());
131 addEntry(entry);
132}
133