File: TagsFromFileNameGuesser.cpp

package info (click to toggle)
amarok 3.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,168 kB
  • sloc: cpp: 195,056; xml: 4,322; ansic: 2,634; javascript: 673; ruby: 528; python: 507; sh: 252; makefile: 12
file content (146 lines) | stat: -rw-r--r-- 5,848 bytes parent folder | download | duplicates (2)
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
/****************************************************************************************
 * Copyright (c) 2010 Sergey Ivanov <123kash@gmail.com>                                 *
 *                                                                                      *
 * 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.                                                                             *
 *                                                                                      *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
 *                                                                                      *
 * You should have received a copy of the GNU General Public License along with         *
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
 ****************************************************************************************/

#include "TagsFromFileNameGuesser.h"

#include <QRegularExpression>
#include <QStringList>

const QStringList m_schemes( QStringList()
    //01 Artist - Title.ext
    << QStringLiteral("^%track%\\W*-?\\W*%artist%\\W*-\\W*%title%\\.+(?:\\w{2,5})$")
    //01 Title.ext
    << QStringLiteral("^%track%\\W*-?\\W*%title%\\.+?:\\w{2,5}$")
    //Album - 01 - Artist - Title.ext
    << QStringLiteral("^%album%\\W*-\\W*%track%\\W*-\\W*%artist%\\W*-\\W*%title%\\.+(?:\\w{2,5})$")
    //Artist - Album - 01 - Title.ext
    << QStringLiteral("^%artist%\\W*-\\W*%album%\\W*-\\W*%track%\\W*-\\W*%title%\\.+(?:\\w{2,5})$")
    // Artist - Album - Title.ext
    << QStringLiteral("^%artist%\\W*-\\W*%album%\\W*-\\W*%title%\\.+(?:\\w{2,5})$")
    //Artist - Title.ext
    << QStringLiteral("^%artist%\\W*-\\W*%title%\\.+(?:\\w{2,5})$")
    //Title.ext
    << QStringLiteral("^%title%\\.+(?:\\w{2,5})$")
);

const QRegularExpression m_digitalFields( QStringLiteral("(%(?:discnumber|track|year)%)") );
const QRegularExpression m_literalFields( QStringLiteral("(%(?:album|albumartist|artist|comment|composer|genre|title)%)") );

quint64
fieldName( const QString &field )
{
    if( field == QLatin1String("album") )
        return Meta::valAlbum;
    else if( field == QLatin1String("albumartist") )
        return Meta::valAlbumArtist;
    else if( field == QLatin1String("artist") )
        return Meta::valArtist;
    else if( field == QLatin1String("comment") )
        return Meta::valComment;
    else if( field == QLatin1String("composer") )
        return Meta::valComposer;
    else if( field == QLatin1String("discnumber") )
        return Meta::valDiscNr;
    else if( field == QLatin1String("genre") )
        return Meta::valGenre;
    else if( field == QLatin1String("title") )
        return Meta::valTitle;
    else if( field == QLatin1String("track") )
        return Meta::valTrackNr;
    else if( field == QLatin1String("year") )
        return Meta::valYear;

    return 0;
}

QList< qint64 >
parseTokens( const QString &scheme )
{
    QRegularExpression rxm( QStringLiteral("%(\\w+)%") );
    QList< qint64 > tokens;

    int pos = 0;
    qint64 field;
    while( ( pos = scheme.indexOf( rxm, pos ) ) != -1 )
    {
        QRegularExpressionMatch rmatch = rxm.match( scheme, pos );
        field = fieldName( rmatch.captured( 1 ) );
        if( field )
            tokens << field;
        pos += rmatch.capturedLength();
    }

    return tokens;
}

Meta::FieldHash
Meta::Tag::TagGuesser::guessTagsByScheme( const QString &fileName, const QString &scheme,
                                          bool cutTrailingSpaces, bool convertUnderscores,
                                          bool isRegExp )
{
    Meta::FieldHash metadata;

    QRegularExpression rx;

    QString m_fileName = fileName;
    QString m_scheme = scheme;

    QList< qint64 > tokens = parseTokens( m_scheme );
    
    // Screen all special symbols
    if( !isRegExp )
        m_scheme = m_scheme.replace( QRegularExpression( QStringLiteral("([~!\\^&*()\\-+\\[\\]{}\\\\:\"?\\.])" )), QStringLiteral("\\\\1") );
    
    QRegularExpression spaces( QStringLiteral("(\\s+)") );
    rx.setPattern( m_scheme.replace( spaces, QStringLiteral("\\s+") )
                           .replace( m_digitalFields, QStringLiteral("(\\d+)") )
                           .replace( m_literalFields, QStringLiteral("(.+)") )
                           .replace( QLatin1String("%ignore%"), QLatin1String("(?:.+)") ) );

    QRegularExpressionMatch rmatch = QRegularExpression(QRegularExpression::anchoredPattern(rx.pattern())).match( m_fileName );
    if( !rmatch.hasMatch() )
        return metadata;

    QString value;
    for( int i = 0; i < tokens.count(); i++ )
    {
        value = rmatch.captured( i + 1 );
        if( convertUnderscores )
            value.replace( QLatin1Char('_'), QLatin1Char(' ') );
        if( cutTrailingSpaces )
            value = value.trimmed();
        metadata.insert( tokens[i], value );
    }
    return metadata;
}

Meta::FieldHash
Meta::Tag::TagGuesser::guessTags( const QString &fileName )
{
    QString tmpStr = fileName;
    int pos = 0;
    if( ( pos = fileName.lastIndexOf( QLatin1Char('/') ) ) != -1 )
            tmpStr = fileName.mid( pos + 1 );

    for( const auto &scheme : m_schemes )
    {
        Meta::FieldHash metadata = guessTagsByScheme( tmpStr, scheme, true, true, true );
        if( !metadata.isEmpty() )
            return metadata;
    }

    return Meta::FieldHash();
}