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

« back to all changes in this revision

Viewing changes to src/core/import/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 "importparser.h"
 
28
#include "frame.h"
 
29
#include "genres.h"
 
30
#include "qtcompatmac.h"
 
31
 
 
32
/**
 
33
 * Get help text for format codes supported by setFormat().
 
34
 *
 
35
 * @return help text.
 
36
 */
 
37
QString ImportParser::getFormatToolTip()
 
38
{
 
39
  QString str;
 
40
  str += "<table>\n";
 
41
 
 
42
  str += "<tr><td>%s</td><td>%{title}</td><td>";
 
43
  str += QCM_translate("Title");
 
44
  str += "</td></tr>\n";
 
45
 
 
46
  str += "<tr><td>%l</td><td>%{album}</td><td>";
 
47
  str += QCM_translate("Album");
 
48
  str += "</td></tr>\n";
 
49
 
 
50
  str += "<tr><td>%a</td><td>%{artist}</td><td>";
 
51
  str += QCM_translate("Artist");
 
52
  str += "</td></tr>\n";
 
53
 
 
54
  str += "<tr><td>%c</td><td>%{comment}</td><td>";
 
55
  str += QCM_translate("Comment");
 
56
  str += "</td></tr>\n";
 
57
 
 
58
  str += "<tr><td>%y</td><td>%{year}</td><td>";
 
59
  str += QCM_translate(I18N_NOOP("Year"));
 
60
  str += "</td></tr>\n";
 
61
 
 
62
  str += "<tr><td>%t</td><td>%{track}</td><td>";
 
63
  str += QCM_translate("Track");
 
64
  str += "</td></tr>\n";
 
65
 
 
66
  str += "<tr><td>%g</td><td>%{genre}</td><td>";
 
67
  str += QCM_translate("Genre");
 
68
  str += "</td></tr>\n";
 
69
 
 
70
  str += "<tr><td>%d</td><td>%{duration}</td><td>";
 
71
  str += QCM_translate(I18N_NOOP("Length"));
 
72
  str += "</td></tr>\n";
 
73
 
 
74
  str += "</table>\n";
 
75
  return str;
 
76
}
 
77
 
 
78
/**
 
79
 * Set import format.
 
80
 *
 
81
 * @param fmt format regexp
 
82
 * @param enableTrackIncr enable automatic track increment if no %t is found
 
83
 */
 
84
void ImportParser::setFormat(const QString& fmt, bool enableTrackIncr)
 
85
{
 
86
  static const struct {
 
87
    const char* from;
 
88
    const char* to;
 
89
  } codeToName[] = {
 
90
    { "%s", "%{title}" },
 
91
    { "%l", "%{album}" },
 
92
    { "%a", "%{artist}" },
 
93
    { "%c", "%{comment}" },
 
94
    { "%y", "%{date}" },
 
95
    { "%t", "%{track number}" },
 
96
    { "%g", "%{genre}" },
 
97
    { "%d", "%{__duration}" },
 
98
    { "%{year}", "%{date}" },
 
99
    { "%{track}", "%{track number}" },
 
100
    { "%{tracknumber}", "%{track number}" },
 
101
    { "%{duration}", "%{__duration}" },
 
102
  };
 
103
  int percentIdx = 0, nr = 1, lastIdx = fmt.length() - 1;
 
104
  m_pattern = fmt;
 
105
  for (unsigned i = 0; i < sizeof(codeToName) / sizeof(codeToName[0]); ++i) {
 
106
    m_pattern.replace(codeToName[i].from, codeToName[i].to);
 
107
  }
 
108
 
 
109
  m_codePos.clear();
 
110
  while (((percentIdx = m_pattern.indexOf("%{", percentIdx)) >= 0) &&
 
111
         (percentIdx < lastIdx)) {
 
112
    int closingBracePos = m_pattern.indexOf("}(", percentIdx + 2);
 
113
    if (closingBracePos > percentIdx + 2) {
 
114
      QString code =
 
115
        m_pattern.mid(percentIdx + 2, closingBracePos - percentIdx - 2).toLower();
 
116
      m_codePos[code] = nr;
 
117
      percentIdx = closingBracePos + 2;
 
118
      ++nr;
 
119
    } else {
 
120
      percentIdx += 2;
 
121
    }
 
122
  }
 
123
 
 
124
  if (enableTrackIncr && !m_codePos.contains("track number")) {
 
125
    m_trackIncrEnabled = true;
 
126
    m_trackIncrNr = 1;
 
127
  } else {
 
128
    m_trackIncrEnabled = false;
 
129
    m_trackIncrNr = 0;
 
130
  }
 
131
 
 
132
  m_pattern.remove(QRegExp("%\\{[^}]+\\}"));
 
133
  m_re.setPattern(m_pattern);
 
134
}
 
135
 
 
136
/**
 
137
 * Get next tags in text buffer.
 
138
 *
 
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).
 
144
 */
 
145
bool ImportParser::getNextTags(const QString& text, FrameCollection& frames, int& pos)
 
146
{
 
147
  int idx, oldpos = pos;
 
148
  if (m_pattern.isEmpty()) {
 
149
    m_trackDuration.clear();
 
150
    return false;
 
151
  }
 
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"
 
157
    int lastDsp = dsp;
 
158
    while ((idx = m_re.indexIn(text, dsp)) != -1) {
 
159
      QString durationStr = m_re.cap(m_codePos["__duration"]);
 
160
      int duration;
 
161
      QRegExp durationRe("(\\d+):(\\d+)");
 
162
      if (durationRe.indexIn(durationStr) != -1) {
 
163
        duration = durationRe.cap(1).toInt() * 60 +
 
164
          durationRe.cap(2).toInt();
 
165
      } else {
 
166
        duration = durationStr.toInt();
 
167
      }
 
168
      m_trackDuration.append(duration);
 
169
 
 
170
      dsp = idx + m_re.matchedLength();
 
171
      if (dsp > lastDsp) { /* avoid endless loop */
 
172
        lastDsp = dsp;
 
173
      } else {
 
174
        break;
 
175
      }
 
176
    }
 
177
  }
 
178
  if ((idx = m_re.indexIn(text, pos)) != -1) {
 
179
    for (QMap<QString, int>::iterator it = m_codePos.begin();
 
180
         it != m_codePos.end();
 
181
         ++it) {
 
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);
 
186
      }
 
187
    }
 
188
    if (m_trackIncrEnabled) {
 
189
      frames.setTrack(m_trackIncrNr++);
 
190
    }
 
191
    pos = idx + m_re.matchedLength();
 
192
    if (pos > oldpos) { /* avoid endless loop */
 
193
      return true;
 
194
    }
 
195
  }
 
196
  return false;
 
197
}