KDED
kmimefileparser.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 * Copyright 2007 David Faure <faure@kde.org> 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kmimefileparser.h" 00021 #include <kglobal.h> 00022 #include <kmimetype.h> 00023 #include <kstandarddirs.h> 00024 #include <kmimetypefactory.h> 00025 #include <kdebug.h> 00026 #include <QtCore/QTextStream> 00027 #include <QtCore/QFile> 00028 00029 KMimeFileParser::KMimeFileParser(KMimeTypeFactory* mimeTypeFactory) 00030 : m_mimeTypeFactory(mimeTypeFactory) 00031 { 00032 } 00033 00034 void KMimeFileParser::parseGlobs() 00035 { 00036 // TODO parse globs2 file instead if it exists. 00037 // This will fix http://bugs.freedesktop.org/show_bug.cgi?id=15436 00038 const QStringList globFiles = KGlobal::dirs()->findAllResources("xdgdata-mime", "globs"); 00039 //kDebug() << globFiles; 00040 parseGlobs(globFiles); 00041 } 00042 00043 void KMimeFileParser::parseGlobs(const QStringList& globFiles) 00044 { 00045 QListIterator<QString> globIter(globFiles); 00046 globIter.toBack(); 00047 // At each level, we must be able to override (not just add to) the information that we read at higher levels. 00048 // This is why we don't directly call mimetype->addPattern, nor can we use the same qhash for everything. 00049 QHash<QString, QStringList> mimeTypeGlobs; 00050 while (globIter.hasPrevious()) { // global first, then local 00051 const QString fileName = globIter.previous(); 00052 QFile globFile(fileName); 00053 kDebug(7021) << "Now parsing" << fileName; 00054 const QHash<QString, QStringList> thisLevelGlobs = parseGlobFile(&globFile); 00055 if (mimeTypeGlobs.isEmpty()) 00056 mimeTypeGlobs = thisLevelGlobs; 00057 else { 00058 // We insert stuff multiple times into the hash, and we only look at the last inserted later on. 00059 mimeTypeGlobs.unite(thisLevelGlobs); 00060 } 00061 } 00062 00063 const QStringList allMimes = mimeTypeGlobs.uniqueKeys(); 00064 Q_FOREACH(const QString& mimeTypeName, allMimes) { 00065 KMimeType::Ptr mimeType = m_mimeTypeFactory->findMimeTypeByName(mimeTypeName); 00066 if (!mimeType) { 00067 kWarning(7012) << "one of glob files in" << globFiles << "refers to unknown mimetype" << mimeTypeName; 00068 } else { 00069 mimeType->setPatterns(mimeTypeGlobs.value(mimeTypeName)); 00070 } 00071 } 00072 } 00073 00074 // uses a QIODevice to make unit tests possible 00075 QHash<QString, QStringList> KMimeFileParser::parseGlobFile(QIODevice* file) 00076 { 00077 QHash<QString, QStringList> globs; 00078 if (!file->open(QIODevice::ReadOnly)) 00079 return globs; 00080 QTextStream stream(file); 00081 //stream.setCodec("UTF-8"); // should be all latin1 00082 QString line; 00083 while (!stream.atEnd()) { 00084 line = stream.readLine(); 00085 if (line.isEmpty() || line.startsWith('#')) 00086 continue; 00087 const int pos = line.indexOf(':'); 00088 if (pos == -1) // syntax error 00089 continue; 00090 const QString mimeTypeName = line.left(pos); 00091 const QString pattern = line.mid(pos+1); 00092 Q_ASSERT(!pattern.isEmpty()); 00093 //if (mimeTypeName == "text/plain") 00094 // kDebug() << "Adding pattern" << pattern << "to mimetype" << mimeTypeName << "from globs file"; 00095 QStringList& patterns = globs[mimeTypeName]; // find or create entry 00096 if (!patterns.contains(pattern)) // ### I miss a QStringList::makeUnique or something... 00097 patterns.append(pattern); 00098 } 00099 return globs; 00100 }