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/>.
27
#include "importparser.h"
30
#include "qtcompatmac.h"
33
* Get help text for format codes supported by setFormat().
37
QString ImportParser::getFormatToolTip()
42
str += "<tr><td>%s</td><td>%{title}</td><td>";
43
str += QCM_translate("Title");
44
str += "</td></tr>\n";
46
str += "<tr><td>%l</td><td>%{album}</td><td>";
47
str += QCM_translate("Album");
48
str += "</td></tr>\n";
50
str += "<tr><td>%a</td><td>%{artist}</td><td>";
51
str += QCM_translate("Artist");
52
str += "</td></tr>\n";
54
str += "<tr><td>%c</td><td>%{comment}</td><td>";
55
str += QCM_translate("Comment");
56
str += "</td></tr>\n";
58
str += "<tr><td>%y</td><td>%{year}</td><td>";
59
str += QCM_translate(I18N_NOOP("Year"));
60
str += "</td></tr>\n";
62
str += "<tr><td>%t</td><td>%{track}</td><td>";
63
str += QCM_translate("Track");
64
str += "</td></tr>\n";
66
str += "<tr><td>%g</td><td>%{genre}</td><td>";
67
str += QCM_translate("Genre");
68
str += "</td></tr>\n";
70
str += "<tr><td>%d</td><td>%{duration}</td><td>";
71
str += QCM_translate(I18N_NOOP("Length"));
72
str += "</td></tr>\n";
81
* @param fmt format regexp
82
* @param enableTrackIncr enable automatic track increment if no %t is found
84
void ImportParser::setFormat(const QString& fmt, bool enableTrackIncr)
92
{ "%a", "%{artist}" },
93
{ "%c", "%{comment}" },
95
{ "%t", "%{track number}" },
97
{ "%d", "%{__duration}" },
98
{ "%{year}", "%{date}" },
99
{ "%{track}", "%{track number}" },
100
{ "%{tracknumber}", "%{track number}" },
101
{ "%{duration}", "%{__duration}" },
103
int percentIdx = 0, nr = 1, lastIdx = fmt.length() - 1;
105
for (unsigned i = 0; i < sizeof(codeToName) / sizeof(codeToName[0]); ++i) {
106
m_pattern.replace(codeToName[i].from, codeToName[i].to);
110
while (((percentIdx = m_pattern.indexOf("%{", percentIdx)) >= 0) &&
111
(percentIdx < lastIdx)) {
112
int closingBracePos = m_pattern.indexOf("}(", percentIdx + 2);
113
if (closingBracePos > percentIdx + 2) {
115
m_pattern.mid(percentIdx + 2, closingBracePos - percentIdx - 2).toLower();
116
m_codePos[code] = nr;
117
percentIdx = closingBracePos + 2;
124
if (enableTrackIncr && !m_codePos.contains("track number")) {
125
m_trackIncrEnabled = true;
128
m_trackIncrEnabled = false;
132
m_pattern.remove(QRegExp("%\\{[^}]+\\}"));
133
m_re.setPattern(m_pattern);
137
* Get next tags in text buffer.
139
* @param text text buffer containing data from file or clipboard
140
* @param frames frames for output
141
* @param pos current position in buffer, will be updated to point
142
* behind current match (to be used for next call)
143
* @return true if tags found (pos is index behind match).
145
bool ImportParser::getNextTags(const QString& text, FrameCollection& frames, int& pos)
147
int idx, oldpos = pos;
148
if (m_pattern.isEmpty()) {
149
m_trackDuration.clear();
152
if (!m_codePos.contains("__duration")) {
153
m_trackDuration.clear();
154
} else if (pos == 0) {
155
m_trackDuration.clear();
156
int dsp = 0; // "duration search pos"
158
while ((idx = m_re.indexIn(text, dsp)) != -1) {
159
QString durationStr = m_re.cap(m_codePos["__duration"]);
161
QRegExp durationRe("(\\d+):(\\d+)");
162
if (durationRe.indexIn(durationStr) != -1) {
163
duration = durationRe.cap(1).toInt() * 60 +
164
durationRe.cap(2).toInt();
166
duration = durationStr.toInt();
168
m_trackDuration.append(duration);
170
dsp = idx + m_re.matchedLength();
171
if (dsp > lastDsp) { /* avoid endless loop */
178
if ((idx = m_re.indexIn(text, pos)) != -1) {
179
for (QMap<QString, int>::iterator it = m_codePos.begin();
180
it != m_codePos.end();
182
QString name = it.key();
183
QString str = m_re.cap(*it);
184
if (!str.isEmpty() && !name.startsWith("__")) {
185
frames.setValue(Frame::getTypeFromName(name), str);
188
if (m_trackIncrEnabled) {
189
frames.setTrack(m_trackIncrNr++);
191
pos = idx + m_re.matchedLength();
192
if (pos > oldpos) { /* avoid endless loop */