summaryrefslogtreecommitdiffstats
path: root/juk/tagguesser.cpp
blob: 3881d1249ded9963d62ce3bb9983b264562e953e (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * tagguesser.cpp - (c) 2003 Frerich Raabe <[email protected]>
 *
 * 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>
#include <kdebug.h>
#include <kglobal.h>
#include <kmacroexpander.h>

FileNameScheme::FileNameScheme(const TQString &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 TQString &fileName) const
{
    /* Strip extension ('.mp3') because '.' may be part of a title, and thus
     * does not work as a separator.
     */
    TQString stripped = fileName;
    stripped.truncate(stripped.findRev('.'));
    return m_regExp.exactMatch(stripped);
}

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

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

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

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

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

TQString FileNameScheme::composeRegExp(const TQString &s) const
{
    TQMap<TQChar, TQString> substitutions;

    KConfigGroup config(TDEGlobal::config(), "TagGuesser");

    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_]+)");

    TQString regExp = TQRegExp::escape(s.simplifyWhiteSpace());
    regExp = ".*" + regExp;
    regExp.replace(' ', "\\s+");
    regExp = KMacroExpander::expandMacros(regExp, substitutions);
    regExp += "[^/]*$";
    return regExp;
}

TQStringList TagGuesser::schemeStrings()
{
    TQStringList schemes;

    KConfigGroup config(TDEGlobal::config(), "TagGuesser");
    schemes = config.readListEntry("Filename schemes");

    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";
        schemes += "%a/%A/[%T] %t [%c]";
        schemes += "%a/%A/[%T] %t (%c)";
        schemes += "%a/%A/[%T] %t";
    }
    return schemes;
}

void TagGuesser::setSchemeStrings(const TQStringList &schemes)
{
    KConfig *cfg = kapp->config();
    {
        KConfigGroupSaver saver(cfg, "TagGuesser");
        cfg->writeEntry("Filename schemes", schemes);
    }
    cfg->sync();
}

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

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

void TagGuesser::loadSchemes()
{
    const TQStringList schemes = schemeStrings();
    TQStringList::ConstIterator it = schemes.begin();
    TQStringList::ConstIterator end = schemes.end();
    for ( ; it != end; ++it )
        m_schemes += FileNameScheme( *it );
}

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

    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)) {
            m_title = capitalizeWords(schema.title().replace('_', " ")).stripWhiteSpace();
            m_artist = capitalizeWords(schema.artist().replace('_', " ")).stripWhiteSpace();
            m_album = capitalizeWords(schema.album().replace('_', " ")).stripWhiteSpace();
            m_track = schema.track().stripWhiteSpace();
            m_comment = schema.comment().replace('_', " ").stripWhiteSpace();
            break;
        }
    }
}

TQString TagGuesser::capitalizeWords(const TQString &s)
{
    if(s.isEmpty())
        return s;

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

    const TQRegExp 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;
}

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