~ubuntu-branches/ubuntu/wily/kid3/wily-proposed

« back to all changes in this revision

Viewing changes to kid3/importparser.cpp

  • Committer: Package Import Robot
  • Author(s): Ana Beatriz Guerrero Lopez, Patrick Matthäi, Ana Beatriz Guerrero Lopez
  • Date: 2011-11-13 16:34:13 UTC
  • mfrom: (1.1.13) (2.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20111113163413-5y0anlc4dqf511uh
Tags: 2.0.1-1
* New upstream release.

[ Patrick Matthäi ]
* Adjust build system.
* Add build dependency xsltproc.

[ Ana Beatriz Guerrero Lopez ]
* Some more adjustments to the build system taken from upstream's deb/
* directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * \file importparser.cpp
3
 
 * Import parser.
4
 
 *
5
 
 * \b Project: Kid3
6
 
 * \author Urs Fleisch
7
 
 * \date 17 Sep 2003
8
 
 *
9
 
 * Copyright (C) 2003-2007  Urs Fleisch
10
 
 *
11
 
 * This file is part of Kid3.
12
 
 *
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.
17
 
 *
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.
22
 
 *
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/>.
25
 
 */
26
 
 
27
 
#include "frame.h"
28
 
#include "genres.h"
29
 
#include "importparser.h"
30
 
 
31
 
/**
32
 
 * Get help text for format codes supported by setFormat().
33
 
 *
34
 
 * @return help text.
35
 
 */
36
 
QString ImportParser::getFormatToolTip()
37
 
{
38
 
        QString str;
39
 
        str += "<table>\n";
40
 
 
41
 
        str += "<tr><td>%s</td><td>%{title}</td><td>";
42
 
        str += QCM_translate("Title");
43
 
        str += "</td></tr>\n";
44
 
 
45
 
        str += "<tr><td>%l</td><td>%{album}</td><td>";
46
 
        str += QCM_translate("Album");
47
 
        str += "</td></tr>\n";
48
 
 
49
 
        str += "<tr><td>%a</td><td>%{artist}</td><td>";
50
 
        str += QCM_translate("Artist");
51
 
        str += "</td></tr>\n";
52
 
 
53
 
        str += "<tr><td>%c</td><td>%{comment}</td><td>";
54
 
        str += QCM_translate("Comment");
55
 
        str += "</td></tr>\n";
56
 
 
57
 
        str += "<tr><td>%y</td><td>%{year}</td><td>";
58
 
        str += QCM_translate(I18N_NOOP("Year"));
59
 
        str += "</td></tr>\n";
60
 
 
61
 
        str += "<tr><td>%t</td><td>%{track}</td><td>";
62
 
        str += QCM_translate("Track");
63
 
        str += "</td></tr>\n";
64
 
 
65
 
        str += "<tr><td>%g</td><td>%{genre}</td><td>";
66
 
        str += QCM_translate("Genre");
67
 
        str += "</td></tr>\n";
68
 
 
69
 
        str += "<tr><td>%d</td><td>%{duration}</td><td>";
70
 
        str += QCM_translate(I18N_NOOP("Length"));
71
 
        str += "</td></tr>\n";
72
 
 
73
 
        str += "</table>\n";
74
 
        return str;
75
 
}
76
 
 
77
 
/**
78
 
 * Set import format.
79
 
 *
80
 
 * @param fmt format regexp
81
 
 * @param enableTrackIncr enable automatic track increment if no %t is found
82
 
 */
83
 
void ImportParser::setFormat(const QString& fmt, bool enableTrackIncr)
84
 
{
85
 
        static const struct {
86
 
                const char* from;
87
 
                const char* to;
88
 
        } codeToName[] = {
89
 
                { "%s", "%{title}" },
90
 
                { "%l", "%{album}" },
91
 
                { "%a", "%{artist}" },
92
 
                { "%c", "%{comment}" },
93
 
                { "%y", "%{date}" },
94
 
                { "%t", "%{track number}" },
95
 
                { "%g", "%{genre}" },
96
 
                { "%d", "%{__duration}" },
97
 
                { "%{year}", "%{date}" },
98
 
                { "%{track}", "%{track number}" },
99
 
                { "%{tracknumber}", "%{track number}" },
100
 
                { "%{duration}", "%{__duration}" },
101
 
        };
102
 
        int percentIdx = 0, nr = 1, lastIdx = fmt.length() - 1;
103
 
        m_pattern = fmt;
104
 
        for (unsigned i = 0; i < sizeof(codeToName) / sizeof(codeToName[0]); ++i) {
105
 
                m_pattern.replace(codeToName[i].from, codeToName[i].to);
106
 
        }
107
 
 
108
 
        m_codePos.clear();
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) {
113
 
                        QString code =
114
 
                                m_pattern.mid(percentIdx + 2, closingBracePos - percentIdx - 2).QCM_toLower();
115
 
                        m_codePos[code] = nr;
116
 
                        percentIdx = closingBracePos + 2;
117
 
                        ++nr;
118
 
                } else {
119
 
                        percentIdx += 2;
120
 
                }
121
 
        }
122
 
 
123
 
        if (enableTrackIncr && !m_codePos.contains("track number")) {
124
 
                m_trackIncrEnabled = true;
125
 
                m_trackIncrNr = 1;
126
 
        } else {
127
 
                m_trackIncrEnabled = false;
128
 
                m_trackIncrNr = 0;
129
 
        }
130
 
 
131
 
#if QT_VERSION >= 0x030100
132
 
        m_pattern.remove(QRegExp("%\\{[^}]+\\}"));
133
 
#else
134
 
        m_pattern.replace(QRegExp("%\\{[^}]+\\}"), "");
135
 
#endif
136
 
        m_re.setPattern(m_pattern);
137
 
}
138
 
 
139
 
/**
140
 
 * Get next tags in text buffer.
141
 
 *
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).
147
 
 */
148
 
bool ImportParser::getNextTags(const QString& text, FrameCollection& frames, int& pos)
149
 
{
150
 
        int idx, oldpos = pos;
151
 
        if (m_pattern.isEmpty()) {
152
 
                m_trackDuration.clear();
153
 
                return false;
154
 
        }
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"
160
 
                int lastDsp = dsp;
161
 
                while ((idx = m_re.QCM_indexIn(text, dsp)) != -1) {
162
 
                        QString durationStr = m_re.cap(m_codePos["__duration"]);
163
 
                        int 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();
168
 
                        } else {
169
 
                                duration = durationStr.toInt();
170
 
                        }
171
 
                        m_trackDuration.append(duration);
172
 
 
173
 
                        dsp = idx + m_re.matchedLength();
174
 
                        if (dsp > lastDsp) { /* avoid endless loop */
175
 
                                lastDsp = dsp;
176
 
                        } else {
177
 
                                break;
178
 
                        }
179
 
                }
180
 
        }
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();
184
 
                                 ++it) {
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));
189
 
                        }
190
 
                }
191
 
                if (m_trackIncrEnabled) {
192
 
                        frames.setTrack(m_trackIncrNr++);
193
 
                }
194
 
                pos = idx + m_re.matchedLength();
195
 
                if (pos > oldpos) { /* avoid endless loop */
196
 
                        return true;
197
 
                }
198
 
        }
199
 
        return false;
200
 
}