2
* \file importparser.cpp
9
* Copyright (C) 2003-2007 Urs Fleisch
11
* This file is part of Kid3.
13
* Kid3 is free software; you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation; either version 2 of the License, or
16
* (at your option) any later version.
18
* Kid3 is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program. If not, see <http://www.gnu.org/licenses/>.
29
#include "importparser.h"
32
* Get help text for format codes supported by setFormat().
36
QString ImportParser::getFormatToolTip()
41
str += "<tr><td>%s</td><td>%{title}</td><td>";
42
str += QCM_translate("Title");
43
str += "</td></tr>\n";
45
str += "<tr><td>%l</td><td>%{album}</td><td>";
46
str += QCM_translate("Album");
47
str += "</td></tr>\n";
49
str += "<tr><td>%a</td><td>%{artist}</td><td>";
50
str += QCM_translate("Artist");
51
str += "</td></tr>\n";
53
str += "<tr><td>%c</td><td>%{comment}</td><td>";
54
str += QCM_translate("Comment");
55
str += "</td></tr>\n";
57
str += "<tr><td>%y</td><td>%{year}</td><td>";
58
str += QCM_translate(I18N_NOOP("Year"));
59
str += "</td></tr>\n";
61
str += "<tr><td>%t</td><td>%{track}</td><td>";
62
str += QCM_translate("Track");
63
str += "</td></tr>\n";
65
str += "<tr><td>%g</td><td>%{genre}</td><td>";
66
str += QCM_translate("Genre");
67
str += "</td></tr>\n";
69
str += "<tr><td>%d</td><td>%{duration}</td><td>";
70
str += QCM_translate(I18N_NOOP("Length"));
71
str += "</td></tr>\n";
80
* @param fmt format regexp
81
* @param enableTrackIncr enable automatic track increment if no %t is found
83
void ImportParser::setFormat(const QString& fmt, bool enableTrackIncr)
91
{ "%a", "%{artist}" },
92
{ "%c", "%{comment}" },
94
{ "%t", "%{track number}" },
96
{ "%d", "%{__duration}" },
97
{ "%{year}", "%{date}" },
98
{ "%{track}", "%{track number}" },
99
{ "%{tracknumber}", "%{track number}" },
100
{ "%{duration}", "%{__duration}" },
102
int percentIdx = 0, nr = 1, lastIdx = fmt.length() - 1;
104
for (unsigned i = 0; i < sizeof(codeToName) / sizeof(codeToName[0]); ++i) {
105
m_pattern.replace(codeToName[i].from, codeToName[i].to);
109
while (((percentIdx = m_pattern.QCM_indexOf("%{", percentIdx)) >= 0) &&
110
(percentIdx < lastIdx)) {
111
int closingBracePos = m_pattern.QCM_indexOf("}(", percentIdx + 2);
112
if (closingBracePos > percentIdx + 2) {
114
m_pattern.mid(percentIdx + 2, closingBracePos - percentIdx - 2).QCM_toLower();
115
m_codePos[code] = nr;
116
percentIdx = closingBracePos + 2;
123
if (enableTrackIncr && !m_codePos.contains("track number")) {
124
m_trackIncrEnabled = true;
127
m_trackIncrEnabled = false;
131
#if QT_VERSION >= 0x030100
132
m_pattern.remove(QRegExp("%\\{[^}]+\\}"));
134
m_pattern.replace(QRegExp("%\\{[^}]+\\}"), "");
136
m_re.setPattern(m_pattern);
140
* Get next tags in text buffer.
142
* @param text text buffer containing data from file or clipboard
143
* @param frames frames for output
144
* @param pos current position in buffer, will be updated to point
145
* behind current match (to be used for next call)
146
* @return true if tags found (pos is index behind match).
148
bool ImportParser::getNextTags(const QString& text, FrameCollection& frames, int& pos)
150
int idx, oldpos = pos;
151
if (m_pattern.isEmpty()) {
152
m_trackDuration.clear();
155
if (!m_codePos.contains("__duration")) {
156
m_trackDuration.clear();
157
} else if (pos == 0) {
158
m_trackDuration.clear();
159
int dsp = 0; // "duration search pos"
161
while ((idx = m_re.QCM_indexIn(text, dsp)) != -1) {
162
QString durationStr = m_re.cap(m_codePos["__duration"]);
164
QRegExp durationRe("(\\d+):(\\d+)");
165
if (durationRe.QCM_indexIn(durationStr) != -1) {
166
duration = durationRe.cap(1).toInt() * 60 +
167
durationRe.cap(2).toInt();
169
duration = durationStr.toInt();
171
m_trackDuration.append(duration);
173
dsp = idx + m_re.matchedLength();
174
if (dsp > lastDsp) { /* avoid endless loop */
181
if ((idx = m_re.QCM_indexIn(text, pos)) != -1) {
182
for (QMap<QString, int>::iterator it = m_codePos.begin();
183
it != m_codePos.end();
185
QString name = it.key();
186
QString str = m_re.cap(*it);
187
if (!str.isEmpty() && !name.startsWith("__")) {
188
frames.insert(Frame(Frame::getTypeFromName(name), str, name, -1));
191
if (m_trackIncrEnabled) {
192
frames.setTrack(m_trackIncrNr++);
194
pos = idx + m_re.matchedLength();
195
if (pos > oldpos) { /* avoid endless loop */