tagguesser.cpp 6.21 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 <kdeversion.h>
Frerich Raabe's avatar
Frerich Raabe committed
15
#if KDE_IS_VERSION(3,1,90)
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
#    include <kmacroexpander.h>
#endif

#include <qmap.h>
#include <qregexp.h>
#include <qurl.h>

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
95 96
    QMap<QChar, QString> substitutions;

97
    KConfig *cfg = kapp->config();
Scott Wheeler's avatar
Scott Wheeler committed
98
    {
99
        KConfigGroupSaver(cfg, "TagGuesser");
100

101 102 103 104 105
        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
106
    }
107 108 109 110

    QString regExp = QRegExp::escape(s.simplifyWhiteSpace());
    regExp = ".*" + regExp;
    regExp.replace(' ', "\\s+");
Frerich Raabe's avatar
Frerich Raabe committed
111
#if KDE_IS_VERSION(3,1,90)
112
    regExp = KMacroExpander::expandMacros(regExp, substitutions);
113 114 115 116 117 118
#else
    QMap<QChar, QString>::ConstIterator it = substitutions.begin();
    QMap<QChar, QString>::ConstIterator end = substitutions.end();
    for (; it != end; ++it)
        regExp.replace("%" + QString(it.key()), it.data());
#endif
119
    regExp += ".*";
120 121 122
    return regExp;
}

123
QStringList TagGuesser::schemeStrings()
124
{
Scott Wheeler's avatar
Scott Wheeler committed
125 126
    QStringList schemes;

127
    KConfig *cfg = kapp->config();
Scott Wheeler's avatar
Scott Wheeler committed
128
    {
Frerich Raabe's avatar
Frerich Raabe committed
129 130
        KConfigGroupSaver(cfg, "TagGuesser");
        schemes = cfg->readListEntry( "Filename schemes" );
Scott Wheeler's avatar
Scott Wheeler committed
131
    }
132
    if ( schemes.isEmpty() ) {
133 134
        schemes += "%a/%A/[%T] %t [%c]";
        schemes += "%a/%A/[%T] %t (%c)";
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
        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";
    }
161 162
    return schemes;
}
163

164 165
void TagGuesser::setSchemeStrings(const QStringList &schemes)
{
166
    KConfig *cfg = kapp->config();
Scott Wheeler's avatar
Scott Wheeler committed
167
    {
168 169
        KConfigGroupSaver(cfg, "TagGuesser");
        cfg->writeEntry("Filename schemes", schemes);
Scott Wheeler's avatar
Scott Wheeler committed
170
    }
171
    cfg->sync();
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}

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

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

void TagGuesser::loadSchemes()
{
    const QStringList schemes = schemeStrings();
188 189 190 191 192 193 194 195
    QStringList::ConstIterator it = schemes.begin();
    QStringList::ConstIterator end = schemes.end();
    for ( ; it != end; ++it )
        m_schemes += FileNameScheme( *it );
}

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

198 199 200 201 202
    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)) {
203 204 205
            m_title = capitalizeWords(schema.title());
            m_artist = capitalizeWords(schema.artist());
            m_album = capitalizeWords(schema.album());
206 207 208 209 210 211 212
            m_track = schema.track();
            m_comment = schema.comment();
            break;
        }
    }
}

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
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;
}

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