summaryrefslogtreecommitdiffstats
path: root/src/imports/mimetypes/qdeclarativemimetype.cpp
blob: 91dab97c45896d20107b25284c9961382cafda50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtMimeTypes addon of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativemimetype_p.h"   // Basis

#include "qmimemagicrulematcher_p.h"

#include <QtCore/QDebug>
#include <qmimetype_p.h>

// ------------------------------------------------------------------------------------------------

extern bool isQMimeTypeDebuggingActivated;

#ifndef QT_NO_DEBUG_OUTPUT
#define DBG() if (isQMimeTypeDebuggingActivated) qDebug() << static_cast<const void *>(this) << Q_FUNC_INFO
#else
#define DBG() if (0) qDebug() << static_cast<const void *>(this) << Q_FUNC_INFO
#endif

// ------------------------------------------------------------------------------------------------

/*!
    \qmlclass MimeType QDeclarativeMimeType
    \brief The QML MimeType element describes types of file or data, represented by a MIME type string.
 */

// ------------------------------------------------------------------------------------------------

/*!
    \class QDeclarativeMimeType
    \brief The QDeclarativeMimeType class is the QML wrapper for the class QMimeType which describes types of file or data, represented by a MIME type string.
    \inherits QObject
 */

// ------------------------------------------------------------------------------------------------

/*!
    \fn QDeclarativeMimeType::QDeclarativeMimeType(QObject *theParent);
    \brief Performs default initialization of the contained MimeType, and attaches the object to the specified \a theParent for destruction.
 */
QDeclarativeMimeType::QDeclarativeMimeType(QObject *theParent) :
        QObject(theParent),
        m_MimeType()
{
    DBG() << "name():" << name();
    //DBG() << "aliases():" << aliases();
    //DBG() << "comment():" << comment();
    DBG() << "genericIconName():" << genericIconName();
    DBG() << "iconName():" << iconName();
    DBG() << "globPatterns():" << globPatterns();
    DBG() << "suffixes():" << suffixes();
    DBG() << "preferredSuffix():" << preferredSuffix();
}

// ------------------------------------------------------------------------------------------------

/*!
    \fn QDeclarativeMimeType::QDeclarativeMimeType(const QMimeType &other, QObject *theParent);
    \brief Copies the specified MimeType \a other, and attaches the object to the specified \a theParent for destruction.
 */
QDeclarativeMimeType::QDeclarativeMimeType(const QMimeType &other, QObject *theParent) :
        QObject(theParent),
        m_MimeType(other)
{
    DBG() << "name():" << name();
    //DBG() << "aliases():" << aliases();
    //DBG() << "comment():" << comment();
    DBG() << "genericIconName():" << genericIconName();
    DBG() << "iconName():" << iconName();
    DBG() << "globPatterns():" << globPatterns();
    DBG() << "suffixes():" << suffixes();
    DBG() << "preferredSuffix():" << preferredSuffix();
}

// ------------------------------------------------------------------------------------------------

#ifdef Q_COMPILER_RVALUE_REFS
/*!
    \fn QDeclarativeMimeType::QDeclarativeMimeType(const QMimeType &other, QObject *theParent);
    \brief Moves the specified MimeType \a other, and attaches the object to the specified \a theParent for destruction.
 */
QDeclarativeMimeType::QDeclarativeMimeType(QMimeType &&other, QObject *theParent) :
        QObject(theParent),
        m_MimeType(other)
{
    DBG() << "name():" << name();
    //DBG() << "aliases():" << aliases();
    //DBG() << "comment():" << comment();
    DBG() << "genericIconName():" << genericIconName();
    DBG() << "iconName():" << iconName();
    DBG() << "globPatterns():" << globPatterns();
    DBG() << "suffixes():" << suffixes();
    DBG() << "preferredSuffix():" << preferredSuffix();
}
#endif

// ------------------------------------------------------------------------------------------------

/*!
    \fn QDeclarativeMimeType::~QDeclarativeMimeType();
    \brief Destroys the the contained MimeType.
 */
QDeclarativeMimeType::~QDeclarativeMimeType()
{
    DBG() << "name():" << name();
    //DBG() << "aliases():" << aliases();
    //DBG() << "comment():" << comment();
    DBG() << "genericIconName():" << genericIconName();
    DBG() << "iconName():" << iconName();
    DBG() << "globPatterns():" << globPatterns();
    DBG() << "suffixes():" << suffixes();
    DBG() << "preferredSuffix():" << preferredSuffix();
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlmethod void MimeType::assign(MimeType other)
    Assigns from another object.
 */

// ------------------------------------------------------------------------------------------------

/*!
    \fn void QDeclarativeMimeType::assign(QDeclarativeMimeType *other);
    \brief Assigns from the object \a other.
 */
void QDeclarativeMimeType::assign(QDeclarativeMimeType *other)
{
    if (other == 0) {
        qWarning() << Q_FUNC_INFO << "other is 0!";
        m_MimeType = QMimeType();
        return;
    }

    m_MimeType = other->m_MimeType;
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlmethod bool MimeType::equals(MimeType other)
    Compares with the other object for equality.
 */

// ------------------------------------------------------------------------------------------------

/*!
    \fn bool QDeclarativeMimeType::equals(QDeclarativeMimeType *other) const;
    \brief Returns if the \a other object has the same value.
 */
bool QDeclarativeMimeType::equals(QDeclarativeMimeType *other) const
{
    if (other == 0) {
        qWarning() << Q_FUNC_INFO << "other is 0!";
        return false;
    }

    return this == other || m_MimeType == other->m_MimeType;
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlmethod object MimeType::properties()
    Returns the properties as a JavaScript object.
 */

#define ASSIGN_TO_PROPERTY(name) \
    result[#name] = name();

/*!
    \fn QVariantMap QDeclarativeMimeType::properties() const;
    \brief Returns the properties as a JavaScript object.
 */
QVariantMap QDeclarativeMimeType::properties() const
{
    QVariantMap result;
    ASSIGN_TO_PROPERTY(name)
    //ASSIGN_TO_PROPERTY(aliases)
    //ASSIGN_TO_PROPERTY(comment)
    ASSIGN_TO_PROPERTY(genericIconName)
    ASSIGN_TO_PROPERTY(iconName)
    ASSIGN_TO_PROPERTY(globPatterns)
    return result;
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlmethod void MimeType::assignProperties(object other)
    Assigns all properties from a JavaScript object.
 */

// ------------------------------------------------------------------------------------------------

#define ASSIGN_FROM_PROPERTY(name, setter, variantType, converter) \
    if (!other.contains(#name)) { \
        if (false) { \
            qDebug() << static_cast<const void *>(this) << Q_FUNC_INFO << "Variant does not contain" << #name; \
        } \
    } \
    else if (other[#name].type() != QVariant::variantType) { \
        if (false) { \
            qDebug() << static_cast<const void *>(this) << Q_FUNC_INFO << "Variant for" << #name << "has wrong type" << other[#name].type() << "with" << other[#name]; \
        } \
    } \
    else { \
        setter(other[#name].converter()); \
    }

/*!
    \fn void QDeclarativeMimeType::assignProperties(const QVariantMap &other);
    \brief Assigns all properties from the JavaScript object \a other.
 */
void QDeclarativeMimeType::assignProperties(const QVariantMap &other)
{
    m_MimeType = QMimeType(QMimeTypePrivate());
    ASSIGN_FROM_PROPERTY(name, setName, String, toString)
    //ASSIGN_FROM_PROPERTY(aliases, setAliases, List, toList)
    //ASSIGN_FROM_PROPERTY(comment, setComment, String, toString)
    ASSIGN_FROM_PROPERTY(genericIconName, setGenericIconName, String, toString)
    ASSIGN_FROM_PROPERTY(iconName, setIconName, String, toString)
    ASSIGN_FROM_PROPERTY(globPatterns, setGlobPatterns, List, toList)
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlmethod bool MimeType::equalsProperties(object other)
    Compares if the properties of the specified JavaScript object are equal.
 */

// ------------------------------------------------------------------------------------------------

#define EQUALS_PROPERTY(name, variantType, converter) \
    if (!other.contains(#name)) { \
        if (false) { \
            qDebug() << static_cast<const void *>(this) << Q_FUNC_INFO << "Variant does not contain" << #name << name(); \
        } \
        return false; \
    } \
    \
    if (other[#name].type() != QVariant::variantType) { \
        if (false) { \
            qDebug() << static_cast<const void *>(this) << Q_FUNC_INFO << "Variant for" << #name << "has wrong type" << other[#name].type() << "for" << name() << "with" << other[#name]; \
        } \
        return false; \
    } \
    if (name() != other[#name].converter()) { \
        if (false) { \
            qDebug() << static_cast<const void *>(this) << Q_FUNC_INFO << "Values for" << #name << "differ:" << name() << other[#name]; \
        } \
        return false; \
    }

/*!
    \fn bool QDeclarativeMimeType::equalsProperties(const QVariantMap &other) const;
    \brief Returns if the properties of the specified JavaScript object \a other are equal.
 */
bool QDeclarativeMimeType::equalsProperties(const QVariantMap &other) const
{
    EQUALS_PROPERTY(name, String, toString)
    //EQUALS_PROPERTY(aliases, List, toList)
    //EQUALS_PROPERTY(comment, String, toString)
    EQUALS_PROPERTY(genericIconName, String, toString)
    EQUALS_PROPERTY(iconName, String, toString)
    EQUALS_PROPERTY(globPatterns, List, toList)

    return true;
}

// ------------------------------------------------------------------------------------------------

/*!
    \fn QMimeType QDeclarativeMimeType::mimeType() const;
    \brief Returns the contained MimeType.
 */
QMimeType QDeclarativeMimeType::mimeType() const
{
    return m_MimeType;
}

// ------------------------------------------------------------------------------------------------

/*!
    \property QDeclarativeMimeType::isValid
    \brief the indication if the object contains valid data.
 */

// ------------------------------------------------------------------------------------------------

bool QDeclarativeMimeType::isValid() const
{
    return m_MimeType.isValid();
}

// ------------------------------------------------------------------------------------------------

#ifndef Q_COMPILER_RVALUE_REFS
QMIMETYPE_BUILDER
#else
QMIMETYPE_BUILDER_FROM_RVALUE_REFS
#endif

// ------------------------------------------------------------------------------------------------

/*!
    \qmlproperty QString MimeType::name
    name of the MIME type
 */

// ------------------------------------------------------------------------------------------------

/*!
    \property QDeclarativeMimeType::name
    \brief the name of the MIME type
 */

// ------------------------------------------------------------------------------------------------

QString QDeclarativeMimeType::name() const
{
    return m_MimeType.name();
}

// ------------------------------------------------------------------------------------------------

void QDeclarativeMimeType::setName(const QString &newName)
{
#ifndef Q_COMPILER_RVALUE_REFS
    m_MimeType = buildQMimeType(newName, m_MimeType.genericIconName(), m_MimeType.iconName(), m_MimeType.globPatterns());
#else
    m_MimeType = buildQMimeType(QString(newName), m_MimeType.genericIconName(), m_MimeType.iconName(), m_MimeType.globPatterns());
#endif
}

// ------------------------------------------------------------------------------------------------

#if 0
QVariantList QDeclarativeMimeType::aliases() const
{
    QVariantList result;

    foreach (const QString &str, m_MimeType.aliases()) {
        result << str;
    }

    return result;
}

// ------------------------------------------------------------------------------------------------

void QDeclarativeMimeType::setAliases(const QVariantList &newAliases)
{
    QList<QString> newAliasesStringList;

    foreach (const QVariant &variant, newAliases) {
        if (variant.type() != QVariant::String) {
            qWarning() << Q_FUNC_INFO << "variant" << variant << "is not a string!";
            continue;
        }

        newAliasesStringList << variant.toString();
    }

#ifndef Q_COMPILER_RVALUE_REFS
    m_MimeType = buildQMimeType(m_MimeType.name(), newAliasesStringList, m_MimeType.comment(), m_MimeType.genericIconName(), m_MimeType.iconName(), m_MimeType.globPatterns());
#else
    m_MimeType = buildQMimeType(m_MimeType.name(), QVariantList(newAliasesStringList), m_MimeType.comment(), m_MimeType.genericIconName(), m_MimeType.iconName(), m_MimeType.globPatterns());
#endif
}

// ------------------------------------------------------------------------------------------------

QString QDeclarativeMimeType::comment() const
{
    return m_MimeType.comment();
}

// ------------------------------------------------------------------------------------------------

void QDeclarativeMimeType::setComment(const QString &newComment)
{
#ifndef Q_COMPILER_RVALUE_REFS
    m_MimeType = buildQMimeType(m_MimeType.name(), /*m_MimeType.aliases(),*/ newComment, m_MimeType.genericIconName(), m_MimeType.iconName(), m_MimeType.globPatterns());
#else
    m_MimeType = buildQMimeType(m_MimeType.name(), /*m_MimeType.aliases(),*/ QString(newComment), m_MimeType.genericIconName(), m_MimeType.iconName(), m_MimeType.globPatterns());
#endif
}

// ------------------------------------------------------------------------------------------------
#endif

/*!
    \qmlproperty QString MimeType::genericIconName
    file name of an icon image that represents the MIME type
 */

// ------------------------------------------------------------------------------------------------

/*!
    \property QDeclarativeMimeType::genericIconName
    \brief the file name of an icon image that represents the MIME type
 */

// ------------------------------------------------------------------------------------------------

QString QDeclarativeMimeType::genericIconName() const
{
    return m_MimeType.genericIconName();
}

// ------------------------------------------------------------------------------------------------

void QDeclarativeMimeType::setGenericIconName(const QString &newGenericIconName)
{
#ifndef Q_COMPILER_RVALUE_REFS
    m_MimeType = buildQMimeType(m_MimeType.name(), newGenericIconName, m_MimeType.iconName(), m_MimeType.globPatterns());
#else
    m_MimeType = buildQMimeType(m_MimeType.name(), QString(newGenericIconName), m_MimeType.iconName(), m_MimeType.globPatterns());
#endif
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlproperty QString MimeType::iconName
    file name of an icon image that represents the MIME type
 */

// ------------------------------------------------------------------------------------------------

/*!
    \property QDeclarativeMimeType::iconName
    \brief the file name of an icon image that represents the MIME type
 */

// ------------------------------------------------------------------------------------------------

QString QDeclarativeMimeType::iconName() const
{
    return m_MimeType.iconName();
}

// ------------------------------------------------------------------------------------------------

void QDeclarativeMimeType::setIconName(const QString &newIconName)
{
#ifndef Q_COMPILER_RVALUE_REFS
    m_MimeType = buildQMimeType(m_MimeType.name(), m_MimeType.genericIconName(), newIconName, m_MimeType.globPatterns());
#else
    m_MimeType = buildQMimeType(m_MimeType.name(), m_MimeType.genericIconName(), QString(newIconName), m_MimeType.globPatterns());
#endif
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlproperty QVariantList MimeType::globPatterns
    list of glob matching patterns
 */

// ------------------------------------------------------------------------------------------------

/*!
    \property QDeclarativeMimeType::globPatterns
    \brief the list of glob matching patterns
 */

// ------------------------------------------------------------------------------------------------

QVariantList QDeclarativeMimeType::globPatterns() const
{
    QVariantList result;

    foreach (const QString &str, m_MimeType.globPatterns()) {
        result << str;
    }

    return result;
}

// ------------------------------------------------------------------------------------------------

void QDeclarativeMimeType::setGlobPatterns(const QVariantList &newGlobPatterns)
{
    QList<QString> newGlobPatternsStringList;

    foreach (const QVariant &variant, newGlobPatterns) {
        if (variant.type() != QVariant::String) {
            qWarning() << Q_FUNC_INFO << "variant" << variant << "is not a string!";
            continue;
        }

        newGlobPatternsStringList << variant.toString();
    }

#ifndef Q_COMPILER_RVALUE_REFS
    m_MimeType = buildQMimeType(m_MimeType.name(), m_MimeType.genericIconName(), m_MimeType.iconName(), newGlobPatternsStringList);
#else
    m_MimeType = buildQMimeType(m_MimeType.name(), m_MimeType.genericIconName(), m_MimeType.iconName(), QStringList(newGlobPatternsStringList));
#endif
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlproperty QVariantList MimeType::suffixes
    known suffixes for the MIME type
 */

// ------------------------------------------------------------------------------------------------

/*!
    \property QDeclarativeMimeType::suffixes
    \brief the known suffixes for the MIME type
 */

// ------------------------------------------------------------------------------------------------

QVariantList QDeclarativeMimeType::suffixes() const
{
    QVariantList result;

    foreach (const QString &str, m_MimeType.suffixes()) {
        result << str;
    }

    return result;
}

// ------------------------------------------------------------------------------------------------

/*!
    \qmlproperty QString MimeType::preferredSuffix
    preferred suffix for the MIME type
 */

// ------------------------------------------------------------------------------------------------

/*!
    \property QDeclarativeMimeType::preferredSuffix
    \brief the preferred suffix for the MIME type
 */

// ------------------------------------------------------------------------------------------------

QString QDeclarativeMimeType::preferredSuffix() const
{
    return m_MimeType.preferredSuffix();
}

// ------------------------------------------------------------------------------------------------

#undef DBG