File: CodecBase.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (160 lines) | stat: -rw-r--r-- 5,749 bytes parent folder | download
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
/*************************************************************************
         CodecBase.cpp  -  base class for Encoder and Decoder
                             -------------------
    begin                : Mon Mar 11 2002
    copyright            : (C) 2002 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <algorithm>

#include <QFileInfo>
#include <QMimeDatabase>
#include <QMimeType>
#include <QString>
#include <QStringList>
#include <QtGlobal>
#include <QUrl>

#include "libkwave/CodecBase.h"
#include "libkwave/String.h"

/***************************************************************************/
Kwave::CodecBase::CodecBase()
    :m_supported_mime_types(), m_supported_compression_types()
{
}

/***************************************************************************/
Kwave::CodecBase::~CodecBase()
{
    m_supported_mime_types.clear();
    m_supported_compression_types.clear();
}

/***************************************************************************/
void Kwave::CodecBase::addMimeType(const char *name,
                                   const QString &description,
                                   const char *patterns)
{
    const QString type_name = _(name);
    if (type_name.contains(_(","))) {
        // list of mime types -> call recursively for each of them
        QStringList types = type_name.split(_(","), Qt::SkipEmptyParts);
        foreach (const QString &mt, types) {
            addMimeType(mt.trimmed().toUtf8().data(), description, patterns);
        }
        return;
    }

    Kwave::CodecBase::MimeType type;
    QMimeDatabase db;
    QMimeType t = db.mimeTypeForName(type_name);

    if (t.isDefault() || t.name().isEmpty()) {
//      qWarning("mime type '%s' not registered, using built-in!", name);
        type.name        = type_name;
        type.description = description;
        type.patterns    = _(patterns).split(_("; "), Qt::SkipEmptyParts);
    } else {
        type.description = t.comment();
        type.patterns    = t.globPatterns();

        if (t.name() != type_name) {
            // type has been translated (maybe un-alias'ed)
            // manually add the original name
            type.name    = type_name;
            m_supported_mime_types.append(type);
        }

        if  (!supports(t.name())) {
            // new type or new alias
            type.name    = t.name();
        }
    }
    m_supported_mime_types.append(type);
}

/***************************************************************************/
void Kwave::CodecBase::addCompression(Kwave::Compression::Type compression)
{
    if (m_supported_compression_types.contains(compression)) return;

    m_supported_compression_types.append(compression);
    std::sort(m_supported_compression_types.begin(),
              m_supported_compression_types.end(), std::greater<int>());
}

/***************************************************************************/
bool Kwave::CodecBase::supports(const QMimeType &mimetype)
{
    return supports(mimetype.name());
}

/***************************************************************************/
bool Kwave::CodecBase::supports(const QString &mimetype_name)
{
    foreach (const Kwave::CodecBase::MimeType &mime, m_supported_mime_types) {
        if (mime.name == mimetype_name) return true;
    }
    return false;
}

/***************************************************************************/
QStringList Kwave::CodecBase::extensions(const QString &mimetype_name) const
{
    QStringList result;
    foreach (const Kwave::CodecBase::MimeType &mime, m_supported_mime_types) {
        if (mime.name == mimetype_name) {
            foreach (const QString &ext, mime.patterns)
                if (!result.contains(ext)) result.append(ext);
        }
    }
    return result;
}

/***************************************************************************/
const QList<Kwave::CodecBase::MimeType> Kwave::CodecBase::mimeTypes()
{
    return m_supported_mime_types;
}

/***************************************************************************/
const QList<Kwave::Compression::Type> Kwave::CodecBase::compressionTypes()
{
    return m_supported_compression_types;
}

/***************************************************************************/
QString Kwave::CodecBase::mimeTypeOf(const QUrl &url)
{
    // get the extension of the file
    QFileInfo file(url.fileName());
    QString suffix = file.suffix();

    if (!suffix.length()) return QMimeType().name();
    suffix = _("*.") + suffix;

    // try to find in the list of supported mime types
    QListIterator<Kwave::CodecBase::MimeType> it(m_supported_mime_types);
    while (it.hasNext()) {
        Kwave::CodecBase::MimeType mime_type = it.next();
        if (mime_type.patterns.contains(suffix.toLower()))
            return mime_type.name;
    }
    return QMimeType().name();
}

/***************************************************************************/
/***************************************************************************/