• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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     const QStringList globFiles = KGlobal::dirs()->findAllResources("xdgdata-mime", "globs");
00037     //kDebug() << globFiles;
00038     parseGlobs(globFiles);
00039 }
00040 
00041 void KMimeFileParser::parseGlobs(const QStringList& globFiles)
00042 {
00043     QStringList parsedFiles;
00044     m_mimeTypeGlobs = parseGlobFiles(globFiles, parsedFiles);
00045     m_allMimeTypes = m_mimeTypeGlobs.uniqueKeys();
00046 
00047     // This is just to fill in KMimeType::patterns. This has no real effect
00048     // on the actual mimetype matching.
00049     Q_FOREACH(const QString& mimeTypeName, m_allMimeTypes) {
00050         KMimeType::Ptr mimeType = m_mimeTypeFactory->findMimeTypeByName(mimeTypeName, KMimeType::DontResolveAlias);
00051         if (!mimeType) {
00052             kWarning(7012) << "one of glob files in" << parsedFiles << "refers to unknown mimetype" << mimeTypeName;
00053             m_mimeTypeGlobs.remove(mimeTypeName);
00054         } else {
00055             const GlobList globs = m_mimeTypeGlobs.value(mimeTypeName);
00056             QStringList patterns;
00057             Q_FOREACH(const Glob& glob, globs)
00058                 patterns.append(glob.pattern);
00059             mimeType->setPatterns(patterns);
00060         }
00061     }
00062 }
00063 
00064 KMimeFileParser::AllGlobs KMimeFileParser::parseGlobFiles(const QStringList& globFiles, QStringList& parsedFiles)
00065 {
00066     KMimeFileParser::AllGlobs allGlobs;
00067     QListIterator<QString> globIter(globFiles);
00068     globIter.toBack();
00069     // At each level, we must be able to override (not just add to) the information that we read at higher levels.
00070     // This is why we don't directly call mimetype->addPattern, nor can we use the same qhash for everything.
00071     while (globIter.hasPrevious()) { // global first, then local
00072         Format format = OldGlobs;
00073         QString fileName = globIter.previous();
00074         QString fileNamev2 = fileName + '2'; // NOTE: this relies on u-m-d always generating the old globs file
00075         if (QFile::exists(fileNamev2)) {
00076             fileName = fileNamev2;
00077             format = Globs2WithWeight;
00078         }
00079         parsedFiles << fileName;
00080         QFile globFile(fileName);
00081         kDebug(7021) << "Now parsing" << fileName;
00082         const QHash<QString, GlobList> thisLevelGlobs = parseGlobFile(&globFile, format);
00083         if (allGlobs.isEmpty())
00084             allGlobs = thisLevelGlobs;
00085         else {
00086             // We insert stuff multiple times into the hash, and we only look at the last inserted later on.
00087             allGlobs.unite(thisLevelGlobs);
00088         }
00089     }
00090     return allGlobs;
00091 }
00092 
00093 // uses a QIODevice to make unit tests possible
00094 QHash<QString, KMimeFileParser::GlobList> KMimeFileParser::parseGlobFile(QIODevice* file, Format format)
00095 {
00096     QHash<QString, GlobList> globs;
00097     if (!file->open(QIODevice::ReadOnly))
00098         return globs;
00099     QTextStream stream(file);
00100     //stream.setCodec("UTF-8"); // should be all latin1
00101     QString line;
00102     while (!stream.atEnd()) {
00103         line = stream.readLine();
00104         if (line.isEmpty() || line.startsWith('#'))
00105             continue;
00106         int pos = line.indexOf(':');
00107         if (pos == -1) // syntax error
00108             continue;
00109         int weight = 50;
00110         if (format == Globs2WithWeight) {
00111             weight = line.left(pos).toInt();
00112             line = line.mid(pos+1);
00113             pos = line.indexOf(':', pos + 1);
00114             if (pos == -1) // syntax error
00115                 continue;
00116         }
00117         const QString mimeTypeName = line.left(pos);
00118         const QString pattern = line.mid(pos+1);
00119         Q_ASSERT(!pattern.isEmpty());
00120         GlobList& globList = globs[mimeTypeName]; // find or create entry
00121         // Check for duplicates, like when installing kde.xml and freedesktop.org.xml
00122         // in the same prefix, and they both have text/plain:*.txt
00123         if (!globList.containsPattern(pattern)) {
00124             //if (mimeTypeName == "text/plain")
00125             //    kDebug() << "Adding pattern" << pattern << "to mimetype" << mimeTypeName << "from globs file, with weight" << weight;
00126             globList.append(Glob(weight, pattern));
00127         }
00128     }
00129     return globs;
00130 }

KDED

Skip menu "KDED"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal