mimetypes/qmimetype.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qmimetype.h" -
43 -
44#include "qmimetype_p.h" -
45#include "qmimedatabase_p.h" -
46#include "qmimeprovider_p.h" -
47 -
48#include "qmimeglobpattern_p.h" -
49 -
50#include <QtCore/QDebug> -
51#include <QtCore/QLocale> -
52 -
53#include <memory> -
54 -
55QT_BEGIN_NAMESPACE -
56 -
57QMimeTypePrivate::QMimeTypePrivate() -
58 : loaded(false) -
59{}
executed: }
Execution Count:5476
5476
60 -
61QMimeTypePrivate::QMimeTypePrivate(const QMimeType &other) -
62 : name(other.d->name), -
63 localeComments(other.d->localeComments), -
64 genericIconName(other.d->genericIconName), -
65 iconName(other.d->iconName), -
66 globPatterns(other.d->globPatterns), -
67 loaded(other.d->loaded) -
68{}
never executed: }
0
69 -
70void QMimeTypePrivate::clear() -
71{ -
72 name.clear();
never executed (the execution status of this line is deduced): name.clear();
-
73 localeComments.clear();
never executed (the execution status of this line is deduced): localeComments.clear();
-
74 genericIconName.clear();
never executed (the execution status of this line is deduced): genericIconName.clear();
-
75 iconName.clear();
never executed (the execution status of this line is deduced): iconName.clear();
-
76 globPatterns.clear();
never executed (the execution status of this line is deduced): globPatterns.clear();
-
77 loaded = false;
never executed (the execution status of this line is deduced): loaded = false;
-
78}
never executed: }
0
79 -
80void QMimeTypePrivate::addGlobPattern(const QString &pattern) -
81{ -
82 globPatterns.append(pattern);
never executed (the execution status of this line is deduced): globPatterns.append(pattern);
-
83}
never executed: }
0
84 -
85/*! -
86 \class QMimeType -
87 \inmodule QtCore -
88 \ingroup shared -
89 \brief The QMimeType class describes types of file or data, represented by a MIME type string. -
90 -
91 \since 5.0 -
92 -
93 For instance a file named "readme.txt" has the MIME type "text/plain". -
94 The MIME type can be determined from the file name, or from the file -
95 contents, or from both. MIME type determination can also be done on -
96 buffers of data not coming from files. -
97 -
98 Determining the MIME type of a file can be useful to make sure your -
99 application supports it. It is also useful in file-manager-like applications -
100 or widgets, in order to display an appropriate icon() for the file, or even -
101 the descriptive comment() in detailed views. -
102 -
103 To check if a file has the expected MIME type, you should use inherits() -
104 rather than a simple string comparison based on the name(). This is because -
105 MIME types can inherit from each other: for instance a C source file is -
106 a specific type of plain text file, so text/x-csrc inherits text/plain. -
107 -
108 \sa QMimeDatabase -
109 */ -
110 -
111/*! -
112 \fn QMimeType::QMimeType(); -
113 Constructs this QMimeType object initialized with default property values that indicate an invalid MIME type. -
114 */ -
115QMimeType::QMimeType() : -
116 d(new QMimeTypePrivate()) -
117{ -
118}
executed: }
Execution Count:18
18
119 -
120/*! -
121 \fn QMimeType::QMimeType(const QMimeType &other); -
122 Constructs this QMimeType object as a copy of \a other. -
123 */ -
124QMimeType::QMimeType(const QMimeType &other) : -
125 d(other.d) -
126{ -
127}
executed: }
Execution Count:3388
3388
128 -
129/*! -
130 \fn QMimeType &QMimeType::operator=(const QMimeType &other); -
131 Assigns the data of \a other to this QMimeType object, and returns a reference to this object. -
132 */ -
133QMimeType &QMimeType::operator=(const QMimeType &other) -
134{ -
135 if (d != other.d)
partially evaluated: d != other.d
TRUEFALSE
yes
Evaluation Count:16
no
Evaluation Count:0
0-16
136 d = other.d;
executed: d = other.d;
Execution Count:16
16
137 return *this;
executed: return *this;
Execution Count:16
16
138} -
139 -
140/*! -
141 \fn QMimeType::QMimeType(const QMimeTypePrivate &dd); -
142 Assigns the data of the QMimeTypePrivate \a dd to this QMimeType object, and returns a reference to this object. -
143 \internal -
144 */ -
145QMimeType::QMimeType(const QMimeTypePrivate &dd) : -
146 d(new QMimeTypePrivate(dd)) -
147{ -
148}
executed: }
Execution Count:5458
5458
149 -
150/*! -
151 \fn void QMimeType::swap(QMimeType &other); -
152 Swaps QMimeType \a other with this QMimeType object. -
153 -
154 This operation is very fast and never fails. -
155 -
156 The swap() method helps with the implementation of assignment -
157 operators in an exception-safe way. For more information consult -
158 \l {http://en.wikibooks.org/wiki/More_C++_Idioms/Copy-and-swap} -
159 {More C++ Idioms - Copy-and-swap}. -
160 */ -
161 -
162/*! -
163 \fn QMimeType::~QMimeType(); -
164 Destroys the QMimeType object, and releases the d pointer. -
165 */ -
166QMimeType::~QMimeType() -
167{ -
168} -
169 -
170/*! -
171 \fn bool QMimeType::operator==(const QMimeType &other) const; -
172 Returns true if \a other equals this QMimeType object, otherwise returns false. -
173 The name is the unique identifier for a mimetype, so two mimetypes with -
174 the same name, are equal. -
175 */ -
176bool QMimeType::operator==(const QMimeType &other) const -
177{ -
178 return d == other.d || d->name == other.d->name;
never executed: return d == other.d || d->name == other.d->name;
0
179} -
180 -
181/*! -
182 \fn bool QMimeType::operator!=(const QMimeType &other) const; -
183 Returns true if \a other does not equal this QMimeType object, otherwise returns false. -
184 */ -
185 -
186/*! -
187 \fn bool QMimeType::isValid() const; -
188 Returns true if the QMimeType object contains valid data, otherwise returns false. -
189 A valid MIME type has a non-empty name(). -
190 The invalid MIME type is the default-constructed QMimeType. -
191 */ -
192bool QMimeType::isValid() const -
193{ -
194 return !d->name.isEmpty();
executed: return !d->name.isEmpty();
Execution Count:2118
2118
195} -
196 -
197/*! -
198 \fn bool QMimeType::isDefault() const; -
199 Returns true if this MIME type is the default MIME type which -
200 applies to all files: application/octet-stream. -
201 */ -
202bool QMimeType::isDefault() const -
203{ -
204 return d->name == QMimeDatabasePrivate::instance()->defaultMimeType();
executed: return d->name == QMimeDatabasePrivate::instance()->defaultMimeType();
Execution Count:9
9
205} -
206 -
207/*! -
208 \fn QString QMimeType::name() const; -
209 Returns the name of the MIME type. -
210 */ -
211QString QMimeType::name() const -
212{ -
213 return d->name;
executed: return d->name;
Execution Count:4837
4837
214} -
215 -
216/*! -
217 Returns the description of the MIME type to be displayed on user interfaces. -
218 -
219 The system language (QLocale::system().name()) is used to select the appropriate translation. -
220 */ -
221QString QMimeType::comment() const -
222{ -
223 QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
executed (the execution status of this line is deduced): QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
-
224 -
225 QStringList languageList;
executed (the execution status of this line is deduced): QStringList languageList;
-
226 languageList << QLocale::system().name();
executed (the execution status of this line is deduced): languageList << QLocale::system().name();
-
227 languageList << QLocale::system().uiLanguages();
executed (the execution status of this line is deduced): languageList << QLocale::system().uiLanguages();
-
228 Q_FOREACH (const QString &language, languageList) {
executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(languageList)> _container_(languageList); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &language = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
229 const QString lang = language == QLatin1String("C") ? QLatin1String("en_US") : language;
partially evaluated: language == QLatin1String("C")
TRUEFALSE
yes
Evaluation Count:6
no
Evaluation Count:0
0-6
230 const QString comm = d->localeComments.value(lang);
executed (the execution status of this line is deduced): const QString comm = d->localeComments.value(lang);
-
231 if (!comm.isEmpty())
partially evaluated: !comm.isEmpty()
TRUEFALSE
yes
Evaluation Count:6
no
Evaluation Count:0
0-6
232 return comm;
executed: return comm;
Execution Count:6
6
233 const int pos = lang.indexOf(QLatin1Char('_'));
never executed (the execution status of this line is deduced): const int pos = lang.indexOf(QLatin1Char('_'));
-
234 if (pos != -1) {
never evaluated: pos != -1
0
235 // "pt_BR" not found? try just "pt" -
236 const QString shortLang = lang.left(pos);
never executed (the execution status of this line is deduced): const QString shortLang = lang.left(pos);
-
237 const QString commShort = d->localeComments.value(shortLang);
never executed (the execution status of this line is deduced): const QString commShort = d->localeComments.value(shortLang);
-
238 if (!commShort.isEmpty())
never evaluated: !commShort.isEmpty()
0
239 return commShort;
never executed: return commShort;
0
240 }
never executed: }
0
241 }
never executed: }
0
242 -
243 // Use the mimetype name as fallback -
244 return d->name;
never executed: return d->name;
0
245} -
246 -
247/*! -
248 \fn QString QMimeType::genericIconName() const; -
249 Returns the file name of a generic icon that represents the MIME type. -
250 -
251 This should be used if the icon returned by iconName() cannot be found on -
252 the system. It is used for categories of similar types (like spreadsheets -
253 or archives) that can use a common icon. -
254 The freedesktop.org Icon Naming Specification lists a set of such icon names. -
255 -
256 The icon name can be given to QIcon::fromTheme() in order to load the icon. -
257 */ -
258QString QMimeType::genericIconName() const -
259{ -
260 QMimeDatabasePrivate::instance()->provider()->loadGenericIcon(*d);
executed (the execution status of this line is deduced): QMimeDatabasePrivate::instance()->provider()->loadGenericIcon(*d);
-
261 if (d->genericIconName.isEmpty()) {
evaluated: d->genericIconName.isEmpty()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:2
2
262 // From the spec: -
263 // If the generic icon name is empty (not specified by the mimetype definition) -
264 // then the mimetype is used to generate the generic icon by using the top-level -
265 // media type (e.g. "video" in "video/ogg") and appending "-x-generic" -
266 // (i.e. "video-x-generic" in the previous example). -
267 QString group = name();
executed (the execution status of this line is deduced): QString group = name();
-
268 const int slashindex = group.indexOf(QLatin1Char('/'));
executed (the execution status of this line is deduced): const int slashindex = group.indexOf(QLatin1Char('/'));
-
269 if (slashindex != -1)
partially evaluated: slashindex != -1
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
0-2
270 group = group.left(slashindex);
executed: group = group.left(slashindex);
Execution Count:2
2
271 return group + QLatin1String("-x-generic");
executed: return group + QLatin1String("-x-generic");
Execution Count:2
2
272 } -
273 return d->genericIconName;
executed: return d->genericIconName;
Execution Count:2
2
274} -
275 -
276/*! -
277 \fn QString QMimeType::iconName() const; -
278 Returns the file name of an icon image that represents the MIME type. -
279 -
280 The icon name can be given to QIcon::fromTheme() in order to load the icon. -
281 */ -
282QString QMimeType::iconName() const -
283{ -
284 QMimeDatabasePrivate::instance()->provider()->loadIcon(*d);
executed (the execution status of this line is deduced): QMimeDatabasePrivate::instance()->provider()->loadIcon(*d);
-
285 if (d->iconName.isEmpty()) {
partially evaluated: d->iconName.isEmpty()
TRUEFALSE
yes
Evaluation Count:4
no
Evaluation Count:0
0-4
286 // Make default icon name from the mimetype name -
287 d->iconName = name();
executed (the execution status of this line is deduced): d->iconName = name();
-
288 const int slashindex = d->iconName.indexOf(QLatin1Char('/'));
executed (the execution status of this line is deduced): const int slashindex = d->iconName.indexOf(QLatin1Char('/'));
-
289 if (slashindex != -1)
partially evaluated: slashindex != -1
TRUEFALSE
yes
Evaluation Count:4
no
Evaluation Count:0
0-4
290 d->iconName[slashindex] = QLatin1Char('-');
executed: d->iconName[slashindex] = QLatin1Char('-');
Execution Count:4
4
291 }
executed: }
Execution Count:4
4
292 return d->iconName;
executed: return d->iconName;
Execution Count:4
4
293} -
294 -
295/*! -
296 \fn QStringList QMimeType::globPatterns() const; -
297 Returns the list of glob matching patterns. -
298 */ -
299QStringList QMimeType::globPatterns() const -
300{ -
301 QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
executed (the execution status of this line is deduced): QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
-
302 return d->globPatterns;
executed: return d->globPatterns;
Execution Count:9
9
303} -
304 -
305/*! -
306 A type is a subclass of another type if any instance of the first type is -
307 also an instance of the second. For example, all image/svg+xml files are also -
308 text/xml, text/plain and application/octet-stream files. Subclassing is about -
309 the format, rather than the category of the data (for example, there is no -
310 'generic spreadsheet' class that all spreadsheets inherit from). -
311 Conversely, the parent mimetype of image/svg+xml is text/xml. -
312 -
313 A mimetype can have multiple parents. For instance application/x-perl -
314 has two parents: application/x-executable and text/plain. This makes -
315 it possible to both execute perl scripts, and to open them in text editors. -
316*/ -
317QStringList QMimeType::parentMimeTypes() const -
318{ -
319 return QMimeDatabasePrivate::instance()->provider()->parents(d->name);
executed: return QMimeDatabasePrivate::instance()->provider()->parents(d->name);
Execution Count:8
8
320} -
321 -
322static void collectParentMimeTypes(const QString &mime, QStringList &allParents) -
323{ -
324 QStringList parents = QMimeDatabasePrivate::instance()->provider()->parents(mime);
executed (the execution status of this line is deduced): QStringList parents = QMimeDatabasePrivate::instance()->provider()->parents(mime);
-
325 foreach (const QString &parent, parents) {
executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(parents)> _container_(parents); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &parent = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
326 // I would use QSet, but since order matters I better not -
327 if (!allParents.contains(parent))
evaluated: !allParents.contains(parent)
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:2
2-12
328 allParents.append(parent);
executed: allParents.append(parent);
Execution Count:12
12
329 }
executed: }
Execution Count:14
14
330 // We want a breadth-first search, so that the least-specific parent (octet-stream) is last -
331 // This means iterating twice, unfortunately. -
332 foreach (const QString &parent, parents) {
executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(parents)> _container_(parents); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &parent = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
333 collectParentMimeTypes(parent, allParents);
executed (the execution status of this line is deduced): collectParentMimeTypes(parent, allParents);
-
334 }
executed: }
Execution Count:14
14
335}
executed: }
Execution Count:18
18
336 -
337/*! -
338 Return all the parent mimetypes of this mimetype, direct and indirect. -
339 This includes the parent(s) of its parent(s), etc. -
340 -
341 For instance, for image/svg+xml the list would be: -
342 application/xml, text/plain, application/octet-stream. -
343 -
344 Note that application/octet-stream is the ultimate parent for all types -
345 of files (but not directories). -
346*/ -
347QStringList QMimeType::allAncestors() const -
348{ -
349 QStringList allParents;
executed (the execution status of this line is deduced): QStringList allParents;
-
350 collectParentMimeTypes(d->name, allParents);
executed (the execution status of this line is deduced): collectParentMimeTypes(d->name, allParents);
-
351 return allParents;
executed: return allParents;
Execution Count:4
4
352} -
353 -
354/*! -
355 Return the list of aliases of this mimetype. -
356 -
357 For instance, for text/csv, the returned list would be: -
358 text/x-csv, text/x-comma-separated-values. -
359 -
360 Note that all QMimeType instances refer to proper mimetypes, -
361 never to aliases directly. -
362 -
363 The order of the aliases in the list is undefined. -
364*/ -
365QStringList QMimeType::aliases() const -
366{ -
367 return QMimeDatabasePrivate::instance()->provider()->listAliases(d->name);
executed: return QMimeDatabasePrivate::instance()->provider()->listAliases(d->name);
Execution Count:4
4
368} -
369 -
370/*! -
371 Returns the known suffixes for the MIME type. -
372 No leading dot is included, so for instance this would return "jpg", "jpeg" for image/jpeg. -
373 */ -
374QStringList QMimeType::suffixes() const -
375{ -
376 QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
executed (the execution status of this line is deduced): QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
-
377 -
378 QStringList result;
executed (the execution status of this line is deduced): QStringList result;
-
379 foreach (const QString &pattern, d->globPatterns) {
executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(d->globPatterns)> _container_(d->globPatterns); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (const QString &pattern = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
380 // Not a simple suffix if it looks like: README or *. or *.* or *.JP*G or *.JP? -
381 if (pattern.startsWith(QLatin1String("*.")) &&
evaluated: pattern.startsWith(QLatin1String("*."))
TRUEFALSE
yes
Evaluation Count:16
yes
Evaluation Count:2
2-16
382 pattern.length() > 2 &&
partially evaluated: pattern.length() > 2
TRUEFALSE
yes
Evaluation Count:16
no
Evaluation Count:0
0-16
383 pattern.indexOf(QLatin1Char('*'), 2) < 0 && pattern.indexOf(QLatin1Char('?'), 2) < 0) {
partially evaluated: pattern.indexOf(QLatin1Char('*'), 2) < 0
TRUEFALSE
yes
Evaluation Count:16
no
Evaluation Count:0
partially evaluated: pattern.indexOf(QLatin1Char('?'), 2) < 0
TRUEFALSE
yes
Evaluation Count:16
no
Evaluation Count:0
0-16
384 const QString suffix = pattern.mid(2);
executed (the execution status of this line is deduced): const QString suffix = pattern.mid(2);
-
385 result.append(suffix);
executed (the execution status of this line is deduced): result.append(suffix);
-
386 }
executed: }
Execution Count:16
16
387 }
executed: }
Execution Count:18
18
388 -
389 return result;
executed: return result;
Execution Count:9
9
390} -
391 -
392/*! -
393 Returns the preferred suffix for the MIME type. -
394 No leading dot is included, so for instance this would return "pdf" for application/pdf. -
395 The return value can be empty, for mime types which do not have any suffixes associated. -
396 */ -
397QString QMimeType::preferredSuffix() const -
398{ -
399 const QStringList suffixList = suffixes();
executed (the execution status of this line is deduced): const QStringList suffixList = suffixes();
-
400 return suffixList.isEmpty() ? QString() : suffixList.at(0);
executed: return suffixList.isEmpty() ? QString() : suffixList.at(0);
Execution Count:9
9
401} -
402 -
403/*! -
404 \fn QString QMimeType::filterString() const; -
405 Returns a filter string usable for a file dialog. -
406*/ -
407QString QMimeType::filterString() const -
408{ -
409 QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
never executed (the execution status of this line is deduced): QMimeDatabasePrivate::instance()->provider()->loadMimeTypePrivate(*d);
-
410 QString filter;
never executed (the execution status of this line is deduced): QString filter;
-
411 -
412 if (!d->globPatterns.empty()) {
never evaluated: !d->globPatterns.empty()
0
413 filter += comment() + QLatin1String(" (");
never executed (the execution status of this line is deduced): filter += comment() + QLatin1String(" (");
-
414 for (int i = 0; i < d->globPatterns.size(); ++i) {
never evaluated: i < d->globPatterns.size()
0
415 if (i != 0)
never evaluated: i != 0
0
416 filter += QLatin1Char(' ');
never executed: filter += QLatin1Char(' ');
0
417 filter += d->globPatterns.at(i);
never executed (the execution status of this line is deduced): filter += d->globPatterns.at(i);
-
418 }
never executed: }
0
419 filter += QLatin1Char(')');
never executed (the execution status of this line is deduced): filter += QLatin1Char(')');
-
420 }
never executed: }
0
421 -
422 return filter;
never executed: return filter;
0
423} -
424 -
425/*! -
426 \fn bool QMimeType::inherits(const QString &mimeTypeName) const; -
427 Returns true if this mimetype is \a mimeTypeName, -
428 or inherits \a mimeTypeName (see parentMimeTypes()), -
429 or \a mimeTypeName is an alias for this mimetype. -
430 */ -
431bool QMimeType::inherits(const QString &mimeTypeName) const -
432{ -
433 if (d->name == mimeTypeName)
evaluated: d->name == mimeTypeName
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:26
2-26
434 return true;
executed: return true;
Execution Count:2
2
435 return QMimeDatabasePrivate::instance()->inherits(d->name, mimeTypeName);
executed: return QMimeDatabasePrivate::instance()->inherits(d->name, mimeTypeName);
Execution Count:26
26
436} -
437 -
438QT_END_NAMESPACE -
439 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial