tagguesser.cpp 5.86 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 <kmacroexpander.h>
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


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;
    int i = s.find('%');
    while (i > -1) {
        switch (s[ i + 1 ]) {
            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;
        }
        i = s.find('%', i + 1);
    }
    m_regExp.setPattern(composeRegExp(s));
}

bool FileNameScheme::matches(const QString &fileName) const
{
    return m_regExp.exactMatch(fileName);
}

QString FileNameScheme::title() const
{
    if(m_titleField == -1)
        return QString::null;
    return m_regExp.capturedTexts()[ m_titleField ];
}

QString FileNameScheme::artist() const
{
    if(m_artistField == -1)
        return QString::null;
    return m_regExp.capturedTexts()[ m_artistField ];
}

QString FileNameScheme::album() const
{
    if(m_albumField == -1)
        return QString::null;
    return m_regExp.capturedTexts()[ m_albumField ];
}

QString FileNameScheme::track() const
{
    if(m_trackField == -1)
        return QString::null;
    return m_regExp.capturedTexts()[ m_trackField ];
}

QString FileNameScheme::comment() const
{
    if(m_commentField == -1)
        return QString::null;
    return m_regExp.capturedTexts()[ m_commentField ];
}

QString FileNameScheme::composeRegExp(const QString &s) const
{
Scott Wheeler's avatar
Scott Wheeler committed
89 90
    QMap<QChar, QString> substitutions;

91
    KConfig *cfg = kapp->config();
Scott Wheeler's avatar
Scott Wheeler committed
92
    {
93
        KConfigGroupSaver saver(cfg, "TagGuesser");
94

95 96 97 98 99
        substitutions[ 't' ] = cfg->readEntry("Title regexp", "([\\w\\s'&]+)");
        substitutions[ 'a' ] = cfg->readEntry("Artist regexp", "([\\w\\s'&]+)");
        substitutions[ 'A' ] = cfg->readEntry("Album regexp", "([\\w\\s'&]+)");
        substitutions[ 'T' ] = cfg->readEntry("Track regexp", "(\\d+)");
        substitutions[ 'c' ] = cfg->readEntry("Comment regexp", "([\\w\\s]+)");
Frerich Raabe's avatar
Frerich Raabe committed
100
    }
101 102 103 104

    QString regExp = QRegExp::escape(s.simplifyWhiteSpace());
    regExp = ".*" + regExp;
    regExp.replace(' ', "\\s+");
105
    regExp = KMacroExpander::expandMacros(regExp, substitutions);
106
    regExp += ".*";
107 108 109
    return regExp;
}

110
QStringList TagGuesser::schemeStrings()
111
{
Scott Wheeler's avatar
Scott Wheeler committed
112 113
    QStringList schemes;

114
    KConfig *cfg = kapp->config();
Scott Wheeler's avatar
Scott Wheeler committed
115
    {
116
        KConfigGroupSaver saver(cfg, "TagGuesser");
Frerich Raabe's avatar
Frerich Raabe committed
117
        schemes = cfg->readListEntry( "Filename schemes" );
Scott Wheeler's avatar
Scott Wheeler committed
118
    }
119
    if ( schemes.isEmpty() ) {
120 121
        schemes += "%a/%A/[%T] %t [%c]";
        schemes += "%a/%A/[%T] %t (%c)";
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
        schemes += "%a/%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 += "%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";
    }
148 149
    return schemes;
}
150

151 152
void TagGuesser::setSchemeStrings(const QStringList &schemes)
{
153
    KConfig *cfg = kapp->config();
Scott Wheeler's avatar
Scott Wheeler committed
154
    {
155
        KConfigGroupSaver saver(cfg, "TagGuesser");
156
        cfg->writeEntry("Filename schemes", schemes);
Scott Wheeler's avatar
Scott Wheeler committed
157
    }
158
    cfg->sync();
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

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

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

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

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

185 186 187 188 189
    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)) {
190 191 192
            m_title = capitalizeWords(schema.title());
            m_artist = capitalizeWords(schema.artist());
            m_album = capitalizeWords(schema.album());
193 194 195 196 197 198 199
            m_track = schema.track();
            m_comment = schema.comment();
            break;
        }
    }
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
QString TagGuesser::capitalizeWords(const QString &s)
{
    if(s.isEmpty())
        return s;

    QString result = s;
    result[ 0 ] = result[ 0 ].upper();

    const QRegExp wordRegExp("\\s\\w");
    int i = result.find( wordRegExp );
    while ( i > -1 ) {
        result[ i + 1 ] = result[ i + 1 ].upper();
        i = result.find( wordRegExp, ++i );
    }

    return result;
}

218
// vim:ts=4:sw=4:noet