2 ******************************************************************************
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @addtogroup GCSPlugins GCS Plugins
9 * @addtogroup CorePlugin Core Plugin
11 * @brief The Core GCS plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifndef MIMEDATABASE_H
30 #define MIMEDATABASE_H
32 #include <coreplugin/core_global.h>
33 #include <QtCore/QStringList>
34 #include <QtCore/QSharedDataPointer>
35 #include <QtCore/QSharedPointer>
36 #include <QtCore/QByteArray>
47 class MimeDatabasePrivate
;
50 class BaseMimeTypeParser
;
51 class FileMatchContext
;
54 /* Magic (file contents) matcher interface. */
55 class CORE_EXPORT IMagicMatcher
{
56 Q_DISABLE_COPY(IMagicMatcher
)
60 // Check for a match on contents of a file
61 virtual bool matches(const QByteArray
&data
) const = 0;
62 // Return a priority value from 1..100
63 virtual int priority() const = 0;
64 virtual ~IMagicMatcher() {}
67 /* Utility class: A standard Magic match rule based on contents. Provides
68 * static factory methods for creation (currently only for "string". This can
69 * be extended to handle "little16"/"big16", etc.). */
70 class CORE_EXPORT MagicRule
{
71 Q_DISABLE_COPY(MagicRule
)
73 explicit MagicRule(const QByteArray
&pattern
, int startPos
, int endPos
);
74 bool matches(const QByteArray
&data
) const;
76 // Convenience factory methods
77 static MagicRule
*createStringRule(const QString
&c
, int startPos
, int endPos
);
80 const QByteArray m_pattern
;
85 /* Utility class: A Magic matcher that checks a number of rules based on
86 * operator "or". It is used for rules parsed from XML files. */
87 class CORE_EXPORT MagicRuleMatcher
: public IMagicMatcher
{
88 Q_DISABLE_COPY(MagicRuleMatcher
)
90 typedef QSharedPointer
<MagicRule
> MagicRuleSharedPointer
;
93 void add(const MagicRuleSharedPointer
&rule
);
94 virtual bool matches(const QByteArray
&data
) const;
96 virtual int priority() const;
97 void setPriority(int p
);
100 typedef QList
<MagicRuleSharedPointer
> MagicRuleList
;
101 MagicRuleList m_list
;
105 /* Mime type data used in the OpenPilot GCS. Contains most information from
106 * standard mime type XML database files.
108 * - Only magic of type "string" is supported. In addition, C++ classes
109 * derived from IMagicMatcher can be added to check on contents
110 * - acronyms, language-specific comments
112 * - List of suffixes and preferred suffix (derived from glob patterns).
114 class CORE_EXPORT MimeType
{
116 /* Return value of a glob match, which is higher than magic */
117 enum { GlobMatchPriority
= 101 };
120 MimeType(const MimeType
&);
121 MimeType
&operator=(const MimeType
&);
126 operator bool() const;
128 bool isTopLevel() const;
130 QString
type() const;
131 void setType(const QString
&type
);
133 QStringList
aliases() const;
134 void setAliases(const QStringList
&);
136 QString
comment() const;
137 void setComment(const QString
&comment
);
139 QString
localeComment(const QString
&locale
= QString() /* en, de...*/) const;
140 void setLocaleComment(const QString
&locale
, const QString
&comment
);
142 QList
<QRegExp
> globPatterns() const;
143 void setGlobPatterns(const QList
<QRegExp
> &);
145 QStringList
subClassesOf() const;
146 void setSubClassesOf(const QStringList
&);
148 // Extension over standard mime data
149 QStringList
suffixes() const;
150 void setSuffixes(const QStringList
&);
152 // Extension over standard mime data
153 QString
preferredSuffix() const;
154 bool setPreferredSuffix(const QString
&);
156 // Check for type or one of the aliases
157 bool matchesType(const QString
&type
) const;
158 // Check glob patterns and magic. Returns the match priority (0 no match,
159 // 1..100 indicating a magic match or GlobMatchPriority indicating an
160 // exact glob match).
161 unsigned matchesFile(const QFileInfo
&file
) const;
163 // Return a filter string usable for a file dialog
164 QString
filterString() const;
167 void addMagicMatcher(const QSharedPointer
<IMagicMatcher
> &matcher
);
169 friend QDebug
operator<<(QDebug d
, const MimeType
&mt
);
172 explicit MimeType(const MimeTypeData
&d
);
173 unsigned matchesFile(Internal::FileMatchContext
&c
) const;
175 friend class Internal::BaseMimeTypeParser
;
176 friend class MimeDatabasePrivate
;
177 QSharedDataPointer
<MimeTypeData
> m_d
;
180 /* A Mime data base to which the plugins can add the mime types they handle.
181 * When adding a "text/plain" to it, the mimetype will receive a magic matcher
182 * that checks for text files that do not match the globs by heuristics.
184 * A good testcase is to run it over '/usr/share/mime/<*>/<*>.xml' on Linux. */
186 class CORE_EXPORT MimeDatabase
{
187 Q_DISABLE_COPY(MimeDatabase
)
193 bool addMimeTypes(const QString
&fileName
, QString
*errorMessage
);
194 bool addMimeTypes(QIODevice
*device
, QString
*errorMessage
);
195 bool addMimeType(const MimeType
&mt
);
197 // Returns a mime type or Null one if none found
198 MimeType
findByType(const QString
&type
) const;
199 // Returns a mime type or Null one if none found
200 MimeType
findByFile(const QFileInfo
&f
) const;
203 QString
preferredSuffixByType(const QString
&type
) const;
204 QString
preferredSuffixByFile(const QFileInfo
&f
) const;
206 // Return all known suffixes
207 QStringList
suffixes() const;
208 bool setPreferredSuffix(const QString
&typeOrAlias
, const QString
&suffix
);
210 QStringList
filterStrings() const;
212 friend QDebug
operator<<(QDebug d
, const MimeDatabase
&mt
);
215 MimeDatabasePrivate
*m_d
;
219 #endif // MIMEDATABASE_H