tagguesser.cpp 6.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * tagguesser.cpp - (c) 2003 Frerich Raabe <raabe@kde.org>
 *
 * 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 "tagguesser.h"

#include <kapplication.h>
#include <kconfig.h>
13
#include <kdebug.h>
14
#include <kglobal.h>
15
#include <kmacroexpander.h>
16
#include <qhash.h>
17 18 19 20 21 22 23 24 25 26

FileNameScheme::FileNameScheme(const QString &s)
    : m_regExp(),
    m_titleField(-1),
    m_artistField(-1),
    m_albumField(-1),
    m_trackField(-1),
    m_commentField(-1)
{
    int fieldNumber = 1;
Stephan Kulow's avatar
Stephan Kulow committed
27
    int i = s.indexOf('%');
28
    while (i > -1) {
29
        switch (s[ i + 1 ].toLatin1()) {
30 31 32 33 34 35 36 37 38 39 40 41 42
            case 't': m_titleField = fieldNumber++;
                      break;
            case 'a': m_artistField = fieldNumber++;
                      break;
            case 'A': m_albumField = fieldNumber++;
                      break;
            case 'T': m_trackField = fieldNumber++;
                      break;
            case 'c': m_commentField = fieldNumber++;
                      break;
            default:
                      break;
        }
Stephan Kulow's avatar
Stephan Kulow committed
43
        i = s.indexOf('%', i + 1);
44 45 46 47 48 49
    }
    m_regExp.setPattern(composeRegExp(s));
}

bool FileNameScheme::matches(const QString &fileName) const
{
50 51 52 53
    /* Strip extension ('.mp3') because '.' may be part of a title, and thus
     * does not work as a separator.
     */
    QString stripped = fileName;
Stephan Kulow's avatar
Stephan Kulow committed
54
    stripped.truncate(stripped.lastIndexOf('.'));
55
    return m_regExp.exactMatch(stripped);
56 57 58 59 60
}

QString FileNameScheme::title() const
{
    if(m_titleField == -1)
61
        return QString();
62 63 64 65 66 67
    return m_regExp.capturedTexts()[ m_titleField ];
}

QString FileNameScheme::artist() const
{
    if(m_artistField == -1)
68
        return QString();
69 70 71 72 73 74
    return m_regExp.capturedTexts()[ m_artistField ];
}

QString FileNameScheme::album() const
{
    if(m_albumField == -1)
75
        return QString();
76 77 78 79 80 81
    return m_regExp.capturedTexts()[ m_albumField ];
}

QString FileNameScheme::track() const
{
    if(m_trackField == -1)
82
        return QString();
83 84 85 86 87 88
    return m_regExp.capturedTexts()[ m_trackField ];
}

QString FileNameScheme::comment() const
{
    if(m_commentField == -1)
89
        return QString();
90 91 92 93 94
    return m_regExp.capturedTexts()[ m_commentField ];
}

QString FileNameScheme::composeRegExp(const QString &s) const
{
95
    QHash<QChar, QString> substitutions;
Scott Wheeler's avatar
Scott Wheeler committed
96

97
    KConfigGroup config(KGlobal::config(), "TagGuesser");
98

99 100 101 102 103
    substitutions[ 't' ] = config.readEntry("Title regexp", "([\\w\\s'&_,\\.]+)");
    substitutions[ 'a' ] = config.readEntry("Artist regexp", "([\\w\\s'&_,\\.]+)");
    substitutions[ 'A' ] = config.readEntry("Album regexp", "([\\w\\s'&_,\\.]+)");
    substitutions[ 'T' ] = config.readEntry("Track regexp", "(\\d+)");
    substitutions[ 'c' ] = config.readEntry("Comment regexp", "([\\w\\s_]+)");
104

Tim Beaulen's avatar
Tim Beaulen committed
105
    QString regExp = QRegExp::escape(s.simplified());
106 107
    regExp = ".*" + regExp;
    regExp.replace(' ', "\\s+");
108
    regExp = KMacroExpander::expandMacros(regExp, substitutions);
109
    regExp += "[^/]*$";
110 111 112
    return regExp;
}

113
QStringList TagGuesser::schemeStrings()
114
{
Scott Wheeler's avatar
Scott Wheeler committed
115 116
    QStringList schemes;

117
    KConfigGroup config(KGlobal::config(), "TagGuesser");
118
    schemes = config.readEntry("Filename schemes", QStringList());
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
    if ( schemes.isEmpty() ) {
        schemes += "%a - (%T) - %t [%c]";
        schemes += "%a - (%T) - %t (%c)";
        schemes += "%a - (%T) - %t";
        schemes += "%a - [%T] - %t [%c]";
        schemes += "%a - [%T] - %t (%c)";
        schemes += "%a - [%T] - %t";
        schemes += "%a - %T - %t [%c]";
        schemes += "%a - %T - %t (%c)";
        schemes += "%a - %T - %t";
        schemes += "(%T) %a - %t [%c]";
        schemes += "(%T) %a - %t (%c)";
        schemes += "(%T) %a - %t";
        schemes += "[%T] %a - %t [%c]";
        schemes += "[%T] %a - %t (%c)";
        schemes += "[%T] %a - %t";
        schemes += "%T %a - %t [%c]";
        schemes += "%T %a - %t (%c)";
        schemes += "%T %a - %t";
        schemes += "(%a) %t [%c]";
        schemes += "(%a) %t (%c)";
        schemes += "(%a) %t";
        schemes += "%a - %t [%c]";
        schemes += "%a - %t (%c)";
        schemes += "%a - %t";
145 146 147
        schemes += "%a/%A/[%T] %t [%c]";
        schemes += "%a/%A/[%T] %t (%c)";
        schemes += "%a/%A/[%T] %t";
148
    }
149 150
    return schemes;
}
151

152 153
void TagGuesser::setSchemeStrings(const QStringList &schemes)
{
Laurent Montel's avatar
Laurent Montel committed
154
    KConfig *cfg = KGlobal::config();
155 156
    KConfigGroup group(cfg, "TagGuesser");
    group.writeEntry("Filename schemes", schemes);
157
    cfg->sync();
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

TagGuesser::TagGuesser()
{
    loadSchemes();
}

TagGuesser::TagGuesser(const QString &absFileName)
{
    loadSchemes();
    guess(absFileName);
}

void TagGuesser::loadSchemes()
{
    const QStringList schemes = schemeStrings();
174 175 176 177 178 179 180 181
    QStringList::ConstIterator it = schemes.begin();
    QStringList::ConstIterator end = schemes.end();
    for ( ; it != end; ++it )
        m_schemes += FileNameScheme( *it );
}

void TagGuesser::guess(const QString &absFileName)
{
182
    m_title = m_artist = m_album = m_track = m_comment = QString();
183

184 185 186 187 188
    FileNameScheme::List::ConstIterator it = m_schemes.begin();
    FileNameScheme::List::ConstIterator end = m_schemes.end();
    for (; it != end; ++it) {
        const FileNameScheme schema(*it);
        if(schema.matches(absFileName)) {
Tim Beaulen's avatar
Tim Beaulen committed
189 190 191 192 193
            m_title = capitalizeWords(schema.title().replace('_', " ")).trimmed();
            m_artist = capitalizeWords(schema.artist().replace('_', " ")).trimmed();
            m_album = capitalizeWords(schema.album().replace('_', " ")).trimmed();
            m_track = schema.track().trimmed();
            m_comment = schema.comment().replace('_', " ").trimmed();
194
            break;
195 196 197 198
        }
    }
}

199 200 201 202 203 204
QString TagGuesser::capitalizeWords(const QString &s)
{
    if(s.isEmpty())
        return s;

    QString result = s;
Dirk Mueller's avatar
Dirk Mueller committed
205
    result[ 0 ] = result[ 0 ].toUpper();
206 207

    const QRegExp wordRegExp("\\s\\w");
Stephan Kulow's avatar
Stephan Kulow committed
208
    int i = result.indexOf( wordRegExp );
209
    while ( i > -1 ) {
Dirk Mueller's avatar
Dirk Mueller committed
210
        result[ i + 1 ] = result[ i + 1 ].toUpper();
Stephan Kulow's avatar
Stephan Kulow committed
211
        i = result.indexOf( wordRegExp, ++i );
212 213 214 215 216
    }

    return result;
}

217
// vim: set et sw=4 tw=0 sta: