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

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
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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