tagguesser.cpp 6.63 KB
Newer Older
1 2
/**
 * Copyright (C) 2003 Frerich Raabe <raabe@kde.org>
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * 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/>.
15
 */
16

17
#include "tagguesser.h"
18
#include "juk_debug.h"
19 20 21

#include <kapplication.h>
#include <kconfig.h>
22
#include <kglobal.h>
23
#include <kmacroexpander.h>
24
#include <qhash.h>
25
#include <kconfiggroup.h>
26 27 28 29 30 31 32 33 34 35

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
36
    int i = s.indexOf('%');
37
    while (i > -1) {
38
        switch (s[ i + 1 ].toLatin1()) {
39 40 41 42 43 44 45 46 47 48 49 50 51
            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
52
        i = s.indexOf('%', i + 1);
53 54 55 56 57 58
    }
    m_regExp.setPattern(composeRegExp(s));
}

bool FileNameScheme::matches(const QString &fileName) const
{
59 60 61 62
    /* 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
63
    stripped.truncate(stripped.lastIndexOf('.'));
64
    return m_regExp.exactMatch(stripped);
65 66 67 68 69
}

QString FileNameScheme::title() const
{
    if(m_titleField == -1)
70
        return QString();
71 72 73 74 75 76
    return m_regExp.capturedTexts()[ m_titleField ];
}

QString FileNameScheme::artist() const
{
    if(m_artistField == -1)
77
        return QString();
78 79 80 81 82 83
    return m_regExp.capturedTexts()[ m_artistField ];
}

QString FileNameScheme::album() const
{
    if(m_albumField == -1)
84
        return QString();
85 86 87 88 89 90
    return m_regExp.capturedTexts()[ m_albumField ];
}

QString FileNameScheme::track() const
{
    if(m_trackField == -1)
91
        return QString();
92 93 94 95 96 97
    return m_regExp.capturedTexts()[ m_trackField ];
}

QString FileNameScheme::comment() const
{
    if(m_commentField == -1)
98
        return QString();
99 100 101 102 103
    return m_regExp.capturedTexts()[ m_commentField ];
}

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

106
    KConfigGroup config(KSharedConfig::openConfig(), "TagGuesser");
107

108 109 110 111 112
    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_]+)");
113

Tim Beaulen's avatar
Tim Beaulen committed
114
    QString regExp = QRegExp::escape(s.simplified());
115 116
    regExp = ".*" + regExp;
    regExp.replace(' ', "\\s+");
117
    regExp = KMacroExpander::expandMacros(regExp, substitutions);
118
    regExp += "[^/]*$";
119 120 121
    return regExp;
}

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

126
    KConfigGroup config(KSharedConfig::openConfig(), "TagGuesser");
127
    schemes = config.readEntry("Filename schemes", QStringList());
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
    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";
154 155 156
        schemes += "%a/%A/[%T] %t [%c]";
        schemes += "%a/%A/[%T] %t (%c)";
        schemes += "%a/%A/[%T] %t";
157
    }
158 159
    return schemes;
}
160

161 162
void TagGuesser::setSchemeStrings(const QStringList &schemes)
{
163
    KSharedConfig::Ptr cfg = KSharedConfig::openConfig();
164 165
    KConfigGroup group(cfg, "TagGuesser");
    group.writeEntry("Filename schemes", schemes);
166
    cfg->sync();
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

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

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

void TagGuesser::loadSchemes()
{
    const QStringList schemes = schemeStrings();
183 184 185 186 187 188 189 190
    QStringList::ConstIterator it = schemes.begin();
    QStringList::ConstIterator end = schemes.end();
    for ( ; it != end; ++it )
        m_schemes += FileNameScheme( *it );
}

void TagGuesser::guess(const QString &absFileName)
{
Jan Gerrit Marker's avatar
Jan Gerrit Marker committed
191 192 193 194 195
    m_title.clear();
    m_artist.clear();
    m_album.clear();
    m_track.clear();
    m_comment.clear();
196

Laurent Montel's avatar
Laurent Montel committed
197 198
    FileNameScheme::List::ConstIterator it = m_schemes.constBegin();
    FileNameScheme::List::ConstIterator end = m_schemes.constEnd();
199 200 201
    for (; it != end; ++it) {
        const FileNameScheme schema(*it);
        if(schema.matches(absFileName)) {
Tim Beaulen's avatar
Tim Beaulen committed
202 203 204 205 206
            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();
207
            break;
208 209 210 211
        }
    }
}

212 213 214 215 216 217
QString TagGuesser::capitalizeWords(const QString &s)
{
    if(s.isEmpty())
        return s;

    QString result = s;
Dirk Mueller's avatar
Dirk Mueller committed
218
    result[ 0 ] = result[ 0 ].toUpper();
219 220

    const QRegExp wordRegExp("\\s\\w");
Stephan Kulow's avatar
Stephan Kulow committed
221
    int i = result.indexOf( wordRegExp );
222
    while ( i > -1 ) {
Dirk Mueller's avatar
Dirk Mueller committed
223
        result[ i + 1 ] = result[ i + 1 ].toUpper();
Stephan Kulow's avatar
Stephan Kulow committed
224
        i = result.indexOf( wordRegExp, ++i );
225 226 227 228 229
    }

    return result;
}

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